Skip to content

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

editor: improve editor performance on mobile

ammarahm-ed41639fd32537
diff --git a/apps/mobile/app/screens/editor/tiptap/commands.ts b/apps/mobile/app/screens/editor/tiptap/commands.ts
index 3eccdfa0f..fb641c49b 100644
--- a/apps/mobile/app/screens/editor/tiptap/commands.ts
+++ b/apps/mobile/app/screens/editor/tiptap/commands.ts
@@ -237,6 +237,10 @@ typeof globalThis.statusBar !== "undefined" && statusBar.current.set({date:"",sa
'response = window.dispatchEvent(new Event("handleBackPress",{cancelable:true}));'
);
};
+
+ keyboardShown = async (keyboardShown: boolean) => {
+ return this.doAsync(`globalThis['keyboardShown']=${keyboardShown};`);
+ };
//todo add replace image function
}
diff --git a/apps/mobile/app/screens/editor/tiptap/use-editor-events.ts b/apps/mobile/app/screens/editor/tiptap/use-editor-events.ts
index 8573fb7fd..b290bab48 100644
--- a/apps/mobile/app/screens/editor/tiptap/use-editor-events.ts
+++ b/apps/mobile/app/screens/editor/tiptap/use-editor-events.ts
@@ -25,6 +25,8 @@ import { useCallback, useEffect, useRef } from "react";
import {
BackHandler,
InteractionManager,
+ Keyboard,
+ KeyboardEventListener,
NativeEventSubscription
} from "react-native";
import { WebViewMessageEvent } from "react-native-webview";
@@ -32,13 +34,12 @@ import { db } from "../../../common/database";
import ManageTagsSheet from "../../../components/sheets/manage-tags";
import { RelationsList } from "../../../components/sheets/relations-list";
import ReminderSheet from "../../../components/sheets/reminder";
-import useKeyboard from "../../../hooks/use-keyboard";
import { DDS } from "../../../services/device-detection";
import {
+ ToastEvent,
eSendEvent,
eSubscribeEvent,
- eUnSubscribeEvent,
- ToastEvent
+ eUnSubscribeEvent
} from "../../../services/event-manager";
import Navigation from "../../../services/navigation";
import { useEditorStore } from "../../../stores/use-editor-store";
@@ -136,7 +137,22 @@ export const useEditorEvents = (
(state) => state.settings?.doubleSpacedLines
);
const tools = useDragState((state) => state.data);
- const { keyboardShown } = useKeyboard();
+
+ useEffect(() => {
+ const handleKeyboardDidShow: KeyboardEventListener = () => {
+ editor.commands.keyboardShown(true);
+ };
+ const handleKeyboardDidHide: KeyboardEventListener = () => {
+ editor.commands.keyboardShown(false);
+ };
+ const subscriptions = [
+ Keyboard.addListener("keyboardDidShow", handleKeyboardDidShow),
+ Keyboard.addListener("keyboardDidHide", handleKeyboardDidHide)
+ ];
+ return () => {
+ subscriptions.forEach((subscription) => subscription.remove());
+ };
+ }, [editor.commands]);
useEffect(() => {
editor.commands.setSettings({
@@ -147,7 +163,6 @@ export const useEditorEvents = (
tools: tools || getDefaultPresets().default,
noHeader: noHeader,
noToolbar: readonly || editorPropReadonly || noToolbar,
- keyboardShown: keyboardShown || false,
doubleSpacedLines: doubleSpacedLines,
corsProxy: corsProxy
});
@@ -160,7 +175,6 @@ export const useEditorEvents = (
deviceMode,
tools,
editor.commands,
- keyboardShown,
doubleSpacedLines,
editorPropReadonly,
noHeader,
diff --git a/apps/mobile/app/screens/editor/tiptap/use-editor.ts b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
index ac54ac643..3cb82af4f 100644
--- a/apps/mobile/app/screens/editor/tiptap/use-editor.ts
+++ b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
@@ -43,9 +43,9 @@ import { NoteType } from "../../../utils/types";
import Commands from "./commands";
import { Content, EditorState, Note, SavePayload } from "./types";
import {
+ EditorEvents,
clearAppState,
defaultState,
- EditorEvents,
getAppState,
isContentInvalid,
isEditorLoaded,
@@ -120,29 +120,9 @@ export const useEditor = (
[editorId]
);
- const onReady = useCallback(async () => {
- if (!(await isEditorLoaded(editorRef, sessionIdRef.current))) {
- overlay(true);
- setLoading(true);
- }
- }, [overlay]);
-
- useEffect(() => {
- state.current.saveCount = 0;
- async () => {
- await commands.setSessionId(sessionIdRef.current);
- if (sessionIdRef.current) {
- if (!state.current?.ready) return;
- await onReady();
- }
- };
- }, [sessionId, loading, commands, onReady]);
-
- useEffect(() => {
- if (loading) {
- setLoading(false);
- }
- }, [loading]);
+ if (loading) {
+ setLoading(false);
+ }
const withTimer = useCallback(
(id: string, fn: () => void, duration: number) => {
@@ -567,6 +547,7 @@ export const useEditor = (
state.current.isRestoringState = true;
state.current.currentlyEditing = true;
state.current.movedAway = false;
+
if (!DDS.isTab) {
tabBarRef.current?.goToPage(1, false);
}
@@ -586,10 +567,6 @@ export const useEditor = (
state.current.isRestoringState = false;
}, [loadNote, overlay]);
- useEffect(() => {
- isDefaultEditor && restoreEditorState();
- }, [isDefaultEditor, restoreEditorState]);
-
useEffect(() => {
eSubscribeEvent(eOnLoadNote + editorId, loadNote);
return () => {
@@ -597,6 +574,30 @@ export const useEditor = (
};
}, [editorId, loadNote, restoreEditorState, isDefaultEditor]);
+ const onContentChanged = () => {
+ lastContentChangeTime.current = Date.now();
+ };
+
+ const onReady = useCallback(async () => {
+ if (!(await isEditorLoaded(editorRef, sessionIdRef.current))) {
+ overlay(true);
+ setLoading(true);
+ } else {
+ isDefaultEditor && restoreEditorState();
+ }
+ }, [overlay, isDefaultEditor, restoreEditorState]);
+
+ useEffect(() => {
+ state.current.saveCount = 0;
+ async () => {
+ await commands.setSessionId(sessionIdRef.current);
+ if (sessionIdRef.current) {
+ if (!state.current?.ready) return;
+ await onReady();
+ }
+ };
+ }, [sessionId, loading, commands, onReady]);
+
const onLoad = useCallback(async () => {
state.current.ready = true;
onReady();
@@ -620,10 +621,6 @@ export const useEditor = (
loadNote
]);
- const onContentChanged = () => {
- lastContentChangeTime.current = Date.now();
- };
-
return {
ref: editorRef,
onLoad,
diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx
index 8bac24c03..689dc5a84 100644
--- a/apps/web/src/components/editor/tiptap.tsx
+++ b/apps/web/src/components/editor/tiptap.tsx
@@ -158,7 +158,6 @@ function TipTap(props: TipTapProps) {
},
downloadOptions,
doubleSpacedLines,
- isKeyboardOpen: true,
isMobile: isMobile || false,
element: editorContainer,
editable: !readonly,
diff --git a/packages/editor-mobile/src/components/editor.tsx b/packages/editor-mobile/src/components/editor.tsx
index 4a12bae17..2eddd37e2 100644
--- a/packages/editor-mobile/src/components/editor.tsx
+++ b/packages/editor-mobile/src/components/editor.tsx
@@ -24,6 +24,7 @@ import {
usePermissionHandler,
useTiptap
} from "@notesnook/editor";
+import { keepLastLineInView } from "@notesnook/editor/dist/extensions/keep-in-view/keep-in-view";
import { Theme, useTheme } from "@notesnook/theme";
import {
forwardRef,
@@ -41,12 +42,10 @@ import Header from "./header";
import StatusBar from "./statusbar";
import Tags from "./tags";
import Title from "./title";
-import { keepLastLineInView } from "@notesnook/editor/dist/extensions/keep-in-view/keep-in-view";
function isIOSBrowser() {
return __PLATFORM__ !== "android";
}
-
const Tiptap = ({
editorTheme,
toolbarTheme,
@@ -104,7 +103,6 @@ const Tiptap = ({
},
content: global.editorController?.content?.current,
isMobile: true,
- isKeyboardOpen: settings.keyboardShown,
doubleSpacedLines: settings.doubleSpacedLines,
onOpenLink: (url) => {
return global.editorController.openLink(url);
@@ -192,7 +190,6 @@ const Tiptap = ({
.focus("end")
.run();
}, []);
-
return (
<>
<div
diff --git a/packages/editor-mobile/src/components/header.tsx b/packages/editor-mobile/src/components/header.tsx
index 7e8033636..08ca86830 100644
--- a/packages/editor-mobile/src/components/header.tsx
+++ b/packages/editor-mobile/src/components/header.tsx
@@ -54,7 +54,7 @@ const Button = ({
);
};
-export default function Header({
+function Header({
noHeader,
settings,
hasUndo,
@@ -296,3 +296,17 @@ export default function Header({
</div>
);
}
+
+export default React.memo(Header, (prev, next) => {
+ if (
+ prev.settings.deviceMode !== next.settings.deviceMode ||
+ prev.settings.fullscreen !== next.settings.fullscreen ||
+ prev.settings.premium !== next.settings.premium ||
+ prev.noHeader !== next.noHeader ||
+ prev.hasRedo !== next.hasRedo ||
+ prev.hasUndo !== next.hasUndo
+ )
+ return false;
+
+ return true;
+});
diff --git a/packages/editor-mobile/src/components/statusbar.tsx b/packages/editor-mobile/src/components/statusbar.tsx
index ffce51478..f7584d27a 100644
--- a/packages/editor-mobile/src/components/statusbar.tsx
+++ b/packages/editor-mobile/src/components/statusbar.tsx
@@ -31,7 +31,6 @@ function StatusBar({ container }: { container: RefObject<HTMLDivElement> }) {
const lastStickyChangeTime = useRef(0);
const [words, setWords] = useState("0 words");
const currentWords = useRef(words);
- const interval = useRef(0);
const statusBar = useRef({
set: setStatus,
updateWords: () => {
@@ -69,17 +68,6 @@ function StatusBar({ container }: { container: RefObject<HTMLDivElement> }) {
currentWords.current = words;
}, [words]);
- useEffect(() => {
- clearInterval(interval.current);
- interval.current = setInterval(
- statusBar.current.updateWords,
- 3000
- ) as unknown as number;
- return () => {
- clearInterval(interval.current);
- };
- }, []);
-
useEffect(() => {
const node = container.current;
node?.addEventListener("scroll", onScroll);
diff --git a/packages/editor-mobile/src/components/tags.tsx b/packages/editor-mobile/src/components/tags.tsx
index db9a01132..b0ef8cea4 100644
--- a/packages/editor-mobile/src/components/tags.tsx
+++ b/packages/editor-mobile/src/components/tags.tsx
@@ -17,11 +17,11 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { useRef, useState } from "react";
+import React, { useRef, useState } from "react";
import { EventTypes } from "../utils";
import styles from "./styles.module.css";
-export default function Tags(): JSX.Element {
+function Tags(): JSX.Element {
const [tags, setTags] = useState<{ title: string; alias: string }[]>([]);
const editorTags = useRef({
setTags: setTags
@@ -122,3 +122,5 @@ export default function Tags(): JSX.Element {
</div>
);
}
+
+export default React.memo(Tags, () => true);
diff --git a/packages/editor-mobile/src/hooks/useEditorController.ts b/packages/editor-mobile/src/hooks/useEditorController.ts
index 3e894a1c0..b8fbc7f31 100644
--- a/packages/editor-mobile/src/hooks/useEditorController.ts
+++ b/packages/editor-mobile/src/hooks/useEditorController.ts
@@ -47,6 +47,7 @@ export type Selection = {
type Timers = {
selectionChange: NodeJS.Timeout | null;
change: NodeJS.Timeout | null;
+ wordCounter: NodeJS.Timeout | null;
};
export type EditorController = {
@@ -72,7 +73,8 @@ export function useEditorController(update: () => void): EditorController {
const htmlContentRef = useRef<string | null>(null);
const timers = useRef<Timers>({
selectionChange: null,
- change: null
+ change: null,
+ wordCounter: null
});
const selectionChange = useCallback((_editor: Editor) => {}, []);
@@ -93,6 +95,8 @@ export function useEditorController(update: () => void): EditorController {
htmlContentRef.current = editor.getHTML();
post(EventTypes.content, htmlContentRef.current, currentSessionId);
}, 300);
+
+ countWords(5000);
}, []);
const scroll = useCallback(
@@ -124,13 +128,13 @@ export function useEditorController(update: () => void): EditorController {
from,
to
});
- statusBar.current?.updateWords();
+ countWords();
break;
}
case "native:html":
htmlContentRef.current = value;
update();
- statusBar.current?.updateWords();
+ countWords();
break;
case "native:theme":
useEditorThemeStore.getState().setColors(message.value);
@@ -151,6 +155,16 @@ export function useEditorController(update: () => void): EditorController {
[update]
);
+ function countWords(ms = 300) {
+ if (typeof timers.current.wordCounter === "number")
+ clearTimeout(timers.current.wordCounter);
+ timers.current.wordCounter = setTimeout(() => {
+ console.time("wordCounter");
+ statusBar?.current?.updateWords();
+ console.timeEnd("wordCounter");
+ }, ms);
+ }
+
useEffect(() => {
if (!isReactNative()) return; // Subscribe only in react native webview.
const isSafari = navigator.vendor.match(/apple/i);
diff --git a/packages/editor-mobile/src/utils/index.ts b/packages/editor-mobile/src/utils/index.ts
index f0d67c2e6..fcf5bbfea 100644
--- a/packages/editor-mobile/src/utils/index.ts
+++ b/packages/editor-mobile/src/utils/index.ts
@@ -37,7 +37,6 @@ export type Settings = {
tools: ToolbarGroupDefinition[];
noToolbar?: boolean;
noHeader?: boolean;
- keyboardShown?: boolean;
doubleSpacedLines?: boolean;
corsProxy: string;
};