Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: fix selection/focus is lost when editor state is updated

Abdullah Attac51406ca0479
diff --git a/packages/editor/src/hooks/use-editor.ts b/packages/editor/src/hooks/use-editor.ts
index f65197826..025c9ec24 100644
--- a/packages/editor/src/hooks/use-editor.ts
+++ b/packages/editor/src/hooks/use-editor.ts
@@ -60,7 +60,10 @@ export const useEditor = (
editor.schema,
options.parseOptions
);
- const selection = resolveFocusPosition(doc, options.autofocus);
+ const selection =
+ editor.state.selection ||
+ resolveFocusPosition(doc, options.autofocus);
+ const oldIsFocused = editor.isFocused;
editor.view.updateState(
EditorState.create({
doc,
@@ -68,6 +71,7 @@ export const useEditor = (
selection: selection || undefined
})
);
+ if (oldIsFocused && !editor.isFocused) editor.commands.focus();
}
editor.options = { ...editor.options, ...options };