editor: update word count immediately after note is loaded

Previously when opening note, the word count on mobile would
be 0, then update to actual word count. This has been fixed so
correct word count shows when opening not without any
delay.
This commit is contained in:
ammarahm-ed
2023-04-01 16:16:52 +05:00
committed by Ammar Ahmed
parent 28d718acf6
commit d838c2e8ad
3 changed files with 13 additions and 8 deletions

View File

@@ -33,7 +33,12 @@ function StatusBar({ container }: { container: RefObject<HTMLDivElement> }) {
const currentWords = useRef(words);
const interval = useRef(0);
const statusBar = useRef({
set: setStatus
set: setStatus,
updateWords: () => {
const words = getTotalWords(editor as Editor) + " words";
if (currentWords.current === words) return;
setWords(words);
}
});
globalThis.statusBar = statusBar;
@@ -66,11 +71,10 @@ function StatusBar({ container }: { container: RefObject<HTMLDivElement> }) {
useEffect(() => {
clearInterval(interval.current);
interval.current = setInterval(() => {
const words = getTotalWords(editor as Editor) + " words";
if (currentWords.current === words) return;
setWords(words);
}, 3000) as unknown as number;
interval.current = setInterval(
statusBar.current.updateWords,
3000
) as unknown as number;
return () => {
clearInterval(interval.current);
};

View File

@@ -123,12 +123,13 @@ export function useEditorController(update: () => void): EditorController {
from,
to
});
statusBar.current?.updateWords();
break;
}
case "native:html":
htmlContentRef.current = value;
update();
statusBar.current?.updateWords();
break;
case "native:theme":
useEditorThemeStore.getState().setColors(message.value);
@@ -156,7 +157,6 @@ export function useEditorController(update: () => void): EditorController {
if (isSafari) {
root = window;
}
console.log("recreating messaging");
root.addEventListener("message", onMessage);
return () => {

View File

@@ -51,6 +51,7 @@ declare global {
saved: string;
}>
>;
updateWords: () => void;
}>;
var __PLATFORM__: "ios" | "android";
var readonly: boolean;