diff --git a/@codexteam/ui/dev/pages/components/Editor.vue b/@codexteam/ui/dev/pages/components/Editor.vue index c1680cdc..40c9a82a 100644 --- a/@codexteam/ui/dev/pages/components/Editor.vue +++ b/@codexteam/ui/dev/pages/components/Editor.vue @@ -40,7 +40,7 @@ placeholder="Write something or press / to select a tool" first-block-placeholder="Untitled" autofocus - :inlineToolbar="true" + :inline-toolbar="true" /> diff --git a/@codexteam/ui/package.json b/@codexteam/ui/package.json index 50db201b..2550ce0f 100644 --- a/@codexteam/ui/package.json +++ b/@codexteam/ui/package.json @@ -1,6 +1,6 @@ { "name": "@codexteam/ui", - "version": "0.2.3", + "version": "0.2.5", "type": "module", "sideEffects": [ "*.css", diff --git a/@codexteam/ui/src/vue/components/editor/useEditor.ts b/@codexteam/ui/src/vue/components/editor/useEditor.ts index e9d0b489..8fb7c533 100644 --- a/@codexteam/ui/src/vue/components/editor/useEditor.ts +++ b/@codexteam/ui/src/vue/components/editor/useEditor.ts @@ -126,8 +126,7 @@ export function useEditor(editorConfig: MaybeRefOrGetter, options: * Destroy editor instance after unmount */ onBeforeUnmount(() => { - editor?.destroy(); - editor = undefined; + destroyEditor(); }); return { diff --git a/src/application/services/useNoteEditor.ts b/src/application/services/useNoteEditor.ts index 1d5b4ad5..cdc76f4e 100644 --- a/src/application/services/useNoteEditor.ts +++ b/src/application/services/useNoteEditor.ts @@ -76,6 +76,13 @@ export const useNoteEditor = function useNoteEditor(options: UseNoteEditorOption */ const toolsUserConfigLoaded = ref(false); + /** + * Incremented on each new load request to discard stale async results. + * Prevents race conditions when rapid note switching causes multiple + * concurrent loadToolsScripts invocations. + */ + let currentLoadId = 0; + /** * Combine note and user tools * Undefined when user or note is not loaded @@ -102,10 +109,12 @@ export const useNoteEditor = function useNoteEditor(options: UseNoteEditorOption }); /** - * Downloads passed tools scripts and toggles-on the isEditorReady flag + * Downloads passed tools scripts and returns the loaded config object. + * Does not mutate shared state — the caller is responsible for applying the result * @param toolsConfigs - tools to download + * @returns loaded tools config */ - async function loadToolsScripts(toolsConfigs: EditorTool[]): Promise { + async function loadToolsScripts(toolsConfigs: EditorTool[]): Promise> { const loadedTools = await editorToolsService.getToolsLoaded(toolsConfigs); /** @@ -114,7 +123,7 @@ export const useNoteEditor = function useNoteEditor(options: UseNoteEditorOption */ const loadedToolsWithoutParagraph = loadedTools.filter(tool => tool.tool.name !== 'paragraph'); - toolsUserConfig = Object.fromEntries( + return Object.fromEntries( loadedToolsWithoutParagraph .map(toolClassAndInfo => [ toolClassAndInfo.tool.name, @@ -124,12 +133,6 @@ export const useNoteEditor = function useNoteEditor(options: UseNoteEditorOption }, ]) ); - toolsUserConfigLoaded.value = true; - - /** - * Now all tools are loaded, we're ready to use the editor - */ - isEditorReady.value = true; } /** @@ -144,7 +147,28 @@ export const useNoteEditor = function useNoteEditor(options: UseNoteEditorOption return; } - await loadToolsScripts(tools); + const loadId = ++currentLoadId; + + isEditorReady.value = false; + toolsUserConfigLoaded.value = false; + + const loadedConfig = await loadToolsScripts(tools); + + /** + * If a newer load request has superseded this one — discard stale results + * to prevent overwriting state with tools from a previous note. + */ + if (loadId !== currentLoadId) { + return; + } + + toolsUserConfig = loadedConfig; + toolsUserConfigLoaded.value = true; + + /** + * Now all tools are loaded, we're ready to use the editor + */ + isEditorReady.value = true; }, { immediate: true, // load tools if they are passed to the composable immediately });