Skip to content

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

mobile: fix editor crash due to position (#5519)

Ammar Ahmed0ea13024e9bc
diff --git a/packages/editor-mobile/src/components/editor.tsx b/packages/editor-mobile/src/components/editor.tsx
index 1ed0712d5..750382b3e 100644
--- a/packages/editor-mobile/src/components/editor.tsx
+++ b/packages/editor-mobile/src/components/editor.tsx
@@ -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,
diff --git a/packages/editor-mobile/src/hooks/useTabStore.ts b/packages/editor-mobile/src/hooks/useTabStore.ts
index 639b466d9..b73ed393d 100644
--- a/packages/editor-mobile/src/hooks/useTabStore.ts
+++ b/packages/editor-mobile/src/hooks/useTabStore.ts
@@ -36,7 +36,7 @@ export type TabItem = {
pinned?: boolean;
};
-type NoteState = {
+export type NoteState = {
top: number;
to: number;
from: number;
diff --git a/packages/editor/src/hooks/use-editor.ts b/packages/editor/src/hooks/use-editor.ts
index d3d09bf9d..9e0e7a196 100644
--- a/packages/editor/src/hooks/use-editor.ts
+++ b/packages/editor/src/hooks/use-editor.ts
@@ -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();