From 131583097ca4debb71d730880d8d04ca7a7d2cb8 Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Tue, 25 Aug 2026 20:35:08 +0500 Subject: [PATCH] web: store editor selections in page-independent positions --- apps/web/src/components/editor/tiptap.tsx | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx index f7e7e085a..3d85d77b1 100644 --- a/apps/web/src/components/editor/tiptap.tsx +++ b/apps/web/src/components/editor/tiptap.tsx @@ -38,7 +38,9 @@ import { getTableOfContents, getChangedNodes, LinkAttributes, + fromFlatPosition, profiler, + toFlatPosition, type VirtualizationMode, type Selection } from "@notesnook/editor"; @@ -385,7 +387,11 @@ function TipTap(props: TipTapProps) { }, onSelectionUpdate: debounce(({ editor, transaction }) => { const isEmptySelection = transaction.selection.empty; - if (onSelectionChange) onSelectionChange(transaction.selection); + if (onSelectionChange) + onSelectionChange({ + from: toFlatPosition(editor.state.doc, transaction.selection.from), + to: toFlatPosition(editor.state.doc, transaction.selection.to) + }); useEditorManager.getState().updateEditor(id, (old) => { const oldSelected = old.statistics?.words?.selected; const oldWords = old.statistics?.words.total || 0; @@ -807,7 +813,14 @@ function toIEditor(editor: Editor): IEditor { return { focus: ({ position, scrollIntoView } = {}) => { if (typeof position === "object") - editor.chain().focus().setTextSelection(position).run(); + editor + .chain() + .focus() + .setTextSelection({ + from: fromFlatPosition(editor.state.doc, position.from), + to: fromFlatPosition(editor.state.doc, position.to) + }) + .run(); else editor.commands.focus(position, { scrollIntoView @@ -847,7 +860,10 @@ function toIEditor(editor: Editor): IEditor { ), getSelection: () => { const { from, to } = editor.state.selection; - return { from, to }; + return { + from: toFlatPosition(editor.state.doc, from), + to: toFlatPosition(editor.state.doc, to) + }; } }; }