mobile: fix scroll jumping in realtime sync

This commit is contained in:
Ammar Ahmed
2024-12-30 15:37:39 +05:00
committed by Ammar Ahmed
parent 800f3ca5d3
commit 724c69655b
2 changed files with 8 additions and 6 deletions

View File

@@ -269,6 +269,7 @@ const Tiptap = ({
? []
: getTableOfContents(containerRef.current);
},
scrollTop: () => containerRef.current?.scrollTop || 0,
scrollTo: (top) => {
containerRef.current?.scrollTo({ top, behavior: "auto" });
}

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Editor, scrollIntoViewById } from "@notesnook/editor";
import { strings } from "@notesnook/intl";
import {
ThemeDefinition,
useThemeColors,
@@ -41,7 +42,6 @@ import {
import { injectCss, transform } from "../utils/css";
import { pendingSaveRequests } from "../utils/pending-saves";
import { useTabContext, useTabStore } from "./useTabStore";
import { strings } from "@notesnook/intl";
type Attachment = {
hash: string;
@@ -126,11 +126,13 @@ export type EditorController = {
export function useEditorController({
update,
getTableOfContents,
scrollTo
scrollTo,
scrollTop
}: {
update: () => void;
getTableOfContents: () => any[];
scrollTo: (top: number) => void;
scrollTop: () => number;
}): EditorController {
const passwordInputRef = useRef<HTMLInputElement | null>(null);
const tab = useTabContext();
@@ -342,23 +344,22 @@ export function useEditorController({
updateTabOnFocus.current = true;
} else {
if (!editor) break;
const noteState = tabRef.current?.noteId
? useTabStore.getState().noteState[tabRef.current?.noteId]
: null;
const top = scrollTop() || noteState?.top || 0;
editor?.commands.setContent(htmlContentRef.current, false, {
preserveWhitespace: true
});
if (noteState) {
if (noteState && editor.isFocused) {
editor.commands.setTextSelection({
from: noteState.from,
to: noteState.to
});
}
scrollTo?.(noteState?.top || 0);
scrollTo?.(top || 0);
countWords(0);
}