Compare commits

...

2 Commits

Author SHA1 Message Date
Ammar Ahmed
60e12d5079 Merge branch 'master' into fix-editor-crash-pos
Signed-off-by: Ammar Ahmed <40239442+ammarahm-ed@users.noreply.github.com>
2024-05-08 09:50:52 +05:00
Ammar Ahmed
6242928c09 mobile: fix editor crash due to position 2024-05-07 14:47:00 +05:00
4 changed files with 41 additions and 38 deletions

View File

@@ -22,3 +22,4 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
sourcemaps

View File

@@ -40,6 +40,7 @@ import {
import { useEditorController } from "../hooks/useEditorController";
import { useSettings } from "../hooks/useSettings";
import {
NoteState,
TabItem,
TabStore,
useTabContext,
@@ -77,6 +78,33 @@ const Tiptap = ({
const isFocusedRef = useRef<boolean>(false);
tabRef.current = tab;
function restoreNoteSelection(state?: NoteState) {
if (!tabRef.current.noteId) return;
const noteState =
state || useTabStore.getState().noteState[tabRef.current.noteId];
if (noteState.to || noteState.from) {
const size = editors[tabRef.current.id]?.state.doc.content.size || 0;
if (
noteState.to > 0 &&
noteState.to <= size &&
noteState.from > 0 &&
noteState.from <= size
) {
editors[tabRef.current.id]?.chain().setTextSelection({
to: noteState.to,
from: noteState.from
});
}
}
containerRef.current?.scrollTo({
left: 0,
top: noteState.top || 0,
behavior: "auto"
});
}
usePermissionHandler({
claims: {
premium: settings.premium
@@ -192,31 +220,7 @@ const Tiptap = ({
},
onCreate() {
setTimeout(() => {
const noteState = tabRef.current.noteId
? useTabStore.getState().noteState[tabRef.current.noteId]
: undefined;
const top = noteState?.top;
logger(
"info",
"editor.onCreate",
tabRef.current?.noteId,
noteState?.top,
noteState?.to,
noteState?.from
);
if (noteState?.to || noteState?.from) {
editors[tabRef.current.id]?.chain().setTextSelection({
to: noteState.to,
from: noteState.from
});
}
logger("info", "Scrolling to", top, useTabStore.getState().noteState);
containerRef.current?.scrollTo({
left: 0,
top: top || 0,
behavior: "auto"
});
restoreNoteSelection();
}, 32);
},
downloadOptions: {
@@ -285,6 +289,7 @@ const Tiptap = ({
? state.noteState[tabRef.current.noteId]
: undefined;
if (noteState) {
if (
containerRef.current &&
containerRef.current?.scrollHeight < noteState.top
@@ -292,17 +297,8 @@ const Tiptap = ({
console.log("Container too small to scroll.");
return;
}
containerRef.current?.scrollTo({
left: 0,
top: noteState.top,
behavior: "auto"
});
if (noteState.to || noteState.from) {
editors[tabRef.current.id]?.chain().setTextSelection({
to: noteState.to,
from: noteState.from
});
}
restoreNoteSelection(noteState);
} else {
containerRef.current?.scrollTo({
left: 0,

View File

@@ -36,7 +36,7 @@ export type TabItem = {
pinned?: boolean;
};
type NoteState = {
export type NoteState = {
top: number;
to: number;
from: number;

View File

@@ -70,7 +70,13 @@ export const useEditor = (
EditorState.create({
doc,
plugins: editor.extensionManager.plugins,
selection: selection || undefined
selection:
selection.from > 0 &&
selection.from <= doc.content.size &&
selection.to > 0 &&
selection.to <= doc.content.size
? selection
: undefined
})
);
if (oldIsFocused && !editor.isFocused) editor.commands.focus();