mobile: update word count on exit

This commit is contained in:
ammarahm-ed
2023-04-18 01:05:06 +05:00
parent af65055ce9
commit 176c46e3f2
2 changed files with 31 additions and 25 deletions

View File

@@ -106,6 +106,7 @@ typeof globalThis.editorTitle !== "undefined" && editorTitle.current && editorTi
if (editorController.content) editorController.content.current = null; if (editorController.content) editorController.content.current = null;
editorController.onUpdate(); editorController.onUpdate();
editorController.setTitle(null); editorController.setTitle(null);
editorController.countWords(0);
typeof globalThis.statusBar !== "undefined" && statusBar.current.set({date:"",saved:""}); typeof globalThis.statusBar !== "undefined" && statusBar.current.set({date:"",saved:""});
` `
); );

View File

@@ -65,6 +65,7 @@ export type EditorController = {
titlePlaceholder: string; titlePlaceholder: string;
openLink: (url: string) => boolean; openLink: (url: string) => boolean;
setTitlePlaceholder: React.Dispatch<React.SetStateAction<string>>; setTitlePlaceholder: React.Dispatch<React.SetStateAction<string>>;
countWords: (ms: number) => void;
}; };
export function useEditorController(update: () => void): EditorController { export function useEditorController(update: () => void): EditorController {
@@ -84,21 +85,34 @@ export function useEditorController(update: () => void): EditorController {
post(EventTypes.title, title); post(EventTypes.title, title);
}, []); }, []);
const contentChange = useCallback((editor: Editor) => { const countWords = useCallback((ms = 300) => {
const currentSessionId = globalThis.sessionId; if (typeof timers.current.wordCounter === "number")
post(EventTypes.contentchange); clearTimeout(timers.current.wordCounter);
if (!editor) return; timers.current.wordCounter = setTimeout(() => {
if (typeof timers.current.change === "number") { console.time("wordCounter");
clearTimeout(timers.current?.change); statusBar?.current?.updateWords();
} console.timeEnd("wordCounter");
timers.current.change = setTimeout(() => { }, ms);
htmlContentRef.current = editor.getHTML();
post(EventTypes.content, htmlContentRef.current, currentSessionId);
}, 300);
countWords(5000);
}, []); }, []);
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( const scroll = useCallback(
(_event: React.UIEvent<HTMLDivElement, UIEvent>) => {}, (_event: React.UIEvent<HTMLDivElement, UIEvent>) => {},
[] []
@@ -152,19 +166,9 @@ export function useEditorController(update: () => void): EditorController {
} }
post(type); // Notify that message was delivered successfully. 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(() => { useEffect(() => {
if (!isReactNative()) return; // Subscribe only in react native webview. if (!isReactNative()) return; // Subscribe only in react native webview.
const isSafari = navigator.vendor.match(/apple/i); const isSafari = navigator.vendor.match(/apple/i);
@@ -208,6 +212,7 @@ export function useEditorController(update: () => void): EditorController {
previewAttachment, previewAttachment,
content: htmlContentRef, content: htmlContentRef,
openLink, openLink,
onUpdate: onUpdate onUpdate: onUpdate,
countWords
}; };
} }