Skip to content

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

web: save selection state before switching note

Abdullah Atta211ab4716551
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index b99f6dda6..8d48ca772 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -155,7 +155,6 @@ export default function TabsView() {
const session = useEditorStore
.getState()
.getSession(tab.sessionId);
- console.log("rendering tab", tab, session);
if (!session) return null;
return (
<Freeze key={session.id} freeze={tab.id !== activeTab?.id}>
@@ -497,6 +496,14 @@ export function Editor(props: EditorProps) {
return () => unsub();
}, []);
+ useEffect(() => {
+ return () => {
+ const editor = useEditorManager.getState().getEditor(id)?.editor;
+ const selection = editor?.getSelection();
+ if (selection) Config.set(`${id}:selection`, selection);
+ };
+ }, [id]);
+
return (
<EditorChrome {...props}>
<Tiptap
diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx
index 6530dc25e..e8b45433e 100644
--- a/apps/web/src/components/editor/tiptap.tsx
+++ b/apps/web/src/components/editor/tiptap.tsx
@@ -604,7 +604,11 @@ function toIEditor(editor: Editor): IEditor {
),
startSearch: () => editor.commands.startSearch(),
getContent: () =>
- getHTMLFromFragment(editor.state.doc.content, editor.schema)
+ getHTMLFromFragment(editor.state.doc.content, editor.schema),
+ getSelection: () => {
+ const { from, to } = editor.state.selection;
+ return { from, to };
+ }
};
}
diff --git a/apps/web/src/components/editor/types.ts b/apps/web/src/components/editor/types.ts
index f6bba6473..b1fff4983 100644
--- a/apps/web/src/components/editor/types.ts
+++ b/apps/web/src/components/editor/types.ts
@@ -40,4 +40,5 @@ export interface IEditor {
sendAttachmentProgress: (hash: string, progress: number) => void;
startSearch: () => void;
getContent: () => string;
+ getSelection: () => { from: number; to: number };
}