Skip to content

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

mobile: update word count on exit

ammarahm-ed176c46e3f28d
diff --git a/apps/mobile/app/screens/editor/tiptap/commands.ts b/apps/mobile/app/screens/editor/tiptap/commands.ts
index fb641c49b..48871c900 100644
--- a/apps/mobile/app/screens/editor/tiptap/commands.ts
+++ b/apps/mobile/app/screens/editor/tiptap/commands.ts
@@ -106,6 +106,7 @@ typeof globalThis.editorTitle !== "undefined" && editorTitle.current && editorTi
if (editorController.content) editorController.content.current = null;
editorController.onUpdate();
editorController.setTitle(null);
+editorController.countWords(0);
typeof globalThis.statusBar !== "undefined" && statusBar.current.set({date:"",saved:""});
`
);
diff --git a/packages/editor-mobile/src/hooks/useEditorController.ts b/packages/editor-mobile/src/hooks/useEditorController.ts
index b8fbc7f31..3392b83d3 100644
--- a/packages/editor-mobile/src/hooks/useEditorController.ts
+++ b/packages/editor-mobile/src/hooks/useEditorController.ts
@@ -65,6 +65,7 @@ export type EditorController = {
titlePlaceholder: string;
openLink: (url: string) => boolean;
setTitlePlaceholder: React.Dispatch<React.SetStateAction<string>>;
+ countWords: (ms: number) => void;
};
export function useEditorController(update: () => void): EditorController {
@@ -84,21 +85,34 @@ export function useEditorController(update: () => void): EditorController {
post(EventTypes.title, title);
}, []);
- const contentChange = useCallback((editor: Editor) => {
- const currentSessionId = globalThis.sessionId;
- post(EventTypes.contentchange);
- if (!editor) return;
- if (typeof timers.current.change === "number") {
- clearTimeout(timers.current?.change);
- }
- timers.current.change = setTimeout(() => {
- htmlContentRef.current = editor.getHTML();
- post(EventTypes.content, htmlContentRef.current, currentSessionId);
- }, 300);
-
- countWords(5000);
+ const countWords = useCallback((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);
}, []);
+ const contentChange = useCallback(
+ (editor: Editor) => {
+ const currentSessionId = globalThis.sessionId;
+ post(EventTypes.contentchange);
+ if (!editor) return;
+ if (typeof timers.current.change === "number") {
+ clearTimeout(timers.current?.change);
+ }
+ timers.current.change = setTimeout(() => {
+ htmlContentRef.current = editor.getHTML();
+ post(EventTypes.content, htmlContentRef.current, currentSessionId);
+ }, 300);
+
+ countWords(5000);
+ },
+ [countWords]
+ );
+
const scroll = useCallback(
(_event: React.UIEvent<HTMLDivElement, UIEvent>) => {},
[]
@@ -152,19 +166,9 @@ export function useEditorController(update: () => void): EditorController {
}
post(type); // Notify that message was delivered successfully.
},
- [update]
+ [update, countWords]
);
- 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);
@@ -208,6 +212,7 @@ export function useEditorController(update: () => void): EditorController {
previewAttachment,
content: htmlContentRef,
openLink,
- onUpdate: onUpdate
+ onUpdate: onUpdate,
+ countWords
};
}