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

@@ -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
};
}