Skip to content

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

mobile: fix editor content update on realtime sync

Ammar Ahmed2e8b234b63df
diff --git a/packages/editor-mobile/src/components/editor.tsx b/packages/editor-mobile/src/components/editor.tsx
index 239085216..7507cc3ab 100644
--- a/packages/editor-mobile/src/components/editor.tsx
+++ b/packages/editor-mobile/src/components/editor.tsx
@@ -255,6 +255,9 @@ const Tiptap = ({
return !containerRef.current
? []
: getTableOfContents(containerRef.current);
+ },
+ scrollTo: (top) => {
+ containerRef.current?.scrollTo({ top, behavior: "auto" });
}
});
const controllerRef = useRef(controller);
diff --git a/packages/editor-mobile/src/hooks/useEditorController.ts b/packages/editor-mobile/src/hooks/useEditorController.ts
index 8ed06e4a8..0d7a99f28 100644
--- a/packages/editor-mobile/src/hooks/useEditorController.ts
+++ b/packages/editor-mobile/src/hooks/useEditorController.ts
@@ -123,10 +123,12 @@ export type EditorController = {
};
export function useEditorController({
update,
- getTableOfContents
+ getTableOfContents,
+ scrollTo
}: {
update: () => void;
getTableOfContents: () => any[];
+ scrollTo: (top: number) => void;
}): EditorController {
const passwordInputRef = useRef<HTMLInputElement | null>(null);
const tab = useTabContext();
@@ -331,22 +333,29 @@ export function useEditorController({
switch (type) {
case "native:updatehtml": {
htmlContentRef.current = value;
+ logger("info", "UPDATING NOTE HTML");
if (tabRef.current.id !== useTabStore.getState().currentTab) {
updateTabOnFocus.current = true;
} else {
if (!editor) break;
- const { from, to } = editor.state.selection;
+
+ const noteState = tabRef.current?.noteId
+ ? useTabStore.getState().noteState[tabRef.current?.noteId]
+ : null;
+
editor?.commands.setContent(htmlContentRef.current, false, {
preserveWhitespace: true
});
- if (editor.isFocused && (to !== 1 || from !== 1)) {
- logger("info", "Setting focus", to, from);
+ if (noteState) {
editor.commands.setTextSelection({
- from,
- to
+ from: noteState.from,
+ to: noteState.to
});
}
+
+ scrollTo?.(noteState?.top || 0);
+
countWords(0);
}