mobile: fix editor loading multiple times

This commit is contained in:
Ammar Ahmed
2024-01-24 22:23:50 +05:00
committed by Abdullah Atta
parent b40fe805e7
commit ae7908e000
9 changed files with 83 additions and 85 deletions

View File

@@ -143,7 +143,6 @@ const Editor = React.memo(
<WebView <WebView
testID={notesnook.editor.id} testID={notesnook.editor.id}
ref={editor.ref} ref={editor.ref}
onLoad={editor.onLoad}
key={renderKey.current} key={renderKey.current}
onRenderProcessGone={onError} onRenderProcessGone={onError}
nestedScrollEnabled nestedScrollEnabled

View File

@@ -39,7 +39,7 @@ export const ProgressBar = () => {
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);
const timer = useRef<NodeJS.Timeout>(); const timer = useRef<NodeJS.Timeout>();
const insets = useGlobalSafeAreaInsets(); const insets = useGlobalSafeAreaInsets();
const [width, setWidth] = useState(400); const [width, setWidth] = useState(0);
const groupProgressInfo = useRef<{ const groupProgressInfo = useRef<{
[name: string]: { [name: string]: {
@@ -54,7 +54,6 @@ export const ProgressBar = () => {
useEffect(() => { useEffect(() => {
if (loading) { if (loading) {
console.log(loading);
if ( if (
loading.current === loading.total && loading.current === loading.total &&
typeof loading.success === "boolean" typeof loading.success === "boolean"
@@ -116,6 +115,7 @@ export const ProgressBar = () => {
justifyContent: "center", justifyContent: "center",
position: "absolute", position: "absolute",
zIndex: visible ? 1 : -1, zIndex: visible ? 1 : -1,
opacity: visible ? 1 : 0,
marginTop: insets.top + 45, marginTop: insets.top + 45,
width: "100%" width: "100%"
}} }}

View File

@@ -42,5 +42,6 @@ export const EventTypes = {
showTabs: "editor-events:show-tabs", showTabs: "editor-events:show-tabs",
tabFocused: "editor-events:tab-focused", tabFocused: "editor-events:tab-focused",
toc: "editor-events:toc", toc: "editor-events:toc",
createInternalLink: "editor-events:create-internal-link" createInternalLink: "editor-events:create-internal-link",
load: "editor-events:load"
}; };

View File

@@ -343,6 +343,12 @@ export const useEditorEvents = (
const data = event.nativeEvent.data; const data = event.nativeEvent.data;
const editorMessage = JSON.parse(data) as EditorMessage<any>; const editorMessage = JSON.parse(data) as EditorMessage<any>;
if (editorMessage.type === EventTypes.load) {
console.log("Editor loaded");
editor.onLoad();
return;
}
if (editorMessage.type === EventTypes.back) { if (editorMessage.type === EventTypes.back) {
return onBackPress(); return onBackPress();
} }

View File

@@ -411,8 +411,6 @@ export const useEditor = (
const noteIsLocked = const noteIsLocked =
event.item.locked && !(event.item as NoteWithContent).content; event.item.locked && !(event.item as NoteWithContent).content;
console.log("noteIsLocked", noteIsLocked);
// If note was already opened in a tab, focus that tab. // If note was already opened in a tab, focus that tab.
if (typeof event.tabId !== "number") { if (typeof event.tabId !== "number") {
if (useTabStore.getState().hasTabForNote(event.item.id)) { if (useTabStore.getState().hasTabForNote(event.item.id)) {
@@ -422,7 +420,6 @@ export const useEditor = (
readonly: event.item.readonly || readonly, readonly: event.item.readonly || readonly,
locked: noteIsLocked locked: noteIsLocked
}); });
console.log(noteIsLocked, "focused tab...");
useTabStore.getState().focusTab(tabId); useTabStore.getState().focusTab(tabId);
} }
console.log("Note already loaded, focusing the tab"); console.log("Note already loaded, focusing the tab");
@@ -437,7 +434,6 @@ export const useEditor = (
} }
} else { } else {
if (lastTabFocused.current !== event.tabId) { if (lastTabFocused.current !== event.tabId) {
console.log("Focused tab");
useTabStore.getState().focusTab(event.tabId); useTabStore.getState().focusTab(event.tabId);
} }
} }
@@ -489,6 +485,7 @@ export const useEditor = (
); );
await postMessage(EditorEvents.title, item.title, tabId); await postMessage(EditorEvents.title, item.title, tabId);
overlay(false);
loadingState.current = currentContents.current[item.id]?.data; loadingState.current = currentContents.current[item.id]?.data;
await postMessage( await postMessage(
@@ -715,8 +712,6 @@ export const useEditor = (
const onLoad = useCallback(async () => { const onLoad = useCallback(async () => {
if (currentNotes.current) overlay(true); if (currentNotes.current) overlay(true);
clearTimeout(timers.current["editor:loaded"]);
timers.current["editor:loaded"] = setTimeout(async () => {
postMessage(EditorEvents.theme, theme); postMessage(EditorEvents.theme, theme);
commands.setInsets( commands.setInsets(
isDefaultEditor ? insets : { top: 0, left: 0, right: 0, bottom: 0 } isDefaultEditor ? insets : { top: 0, left: 0, right: 0, bottom: 0 }
@@ -726,34 +721,15 @@ export const useEditor = (
if (!state.current.ready && (await onReady())) { if (!state.current.ready && (await onReady())) {
state.current.ready = true; state.current.ready = true;
} }
setTimeout(() => overlay(false), 300);
const noteId = useTabStore.getState().getCurrentNoteId(); const noteId = useTabStore.getState().getCurrentNoteId();
async function restoreTabNote() { if (!noteId) {
if (!noteId) return; overlay(false);
const note = await db.notes.note(noteId);
if (!note) {
console.log("Editor loaded with blank note");
loadNote({ newNote: true }); loadNote({ newNote: true });
if (tabBarRef.current?.page === 1) { if (tabBarRef.current?.page === 1) {
state.current.currentlyEditing = false; state.current.currentlyEditing = false;
} }
} }
}
if (noteId) {
if (useSettingStore.getState().isAppLoading) {
const unsub = useSettingStore.subscribe(async (state) => {
if (!state.isAppLoading) {
restoreTabNote();
unsub();
}
});
} else {
restoreTabNote();
}
}
}, 500);
}, [ }, [
onReady, onReady,
postMessage, postMessage,

View File

@@ -29,7 +29,14 @@ import {
} from "@notesnook/editor"; } from "@notesnook/editor";
import { toBlobURL } from "@notesnook/editor/dist/utils/downloader"; import { toBlobURL } from "@notesnook/editor/dist/utils/downloader";
import { useThemeColors } from "@notesnook/theme"; import { useThemeColors } from "@notesnook/theme";
import { useCallback, useLayoutEffect, useMemo, useRef, useState } from "react"; import {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState
} from "react";
import { useEditorController } from "../hooks/useEditorController"; import { useEditorController } from "../hooks/useEditorController";
import { useSettings } from "../hooks/useSettings"; import { useSettings } from "../hooks/useSettings";
import { import {
@@ -47,6 +54,8 @@ import Title from "./title";
globalThis.toBlobURL = toBlobURL as typeof globalThis.toBlobURL; globalThis.toBlobURL = toBlobURL as typeof globalThis.toBlobURL;
let didCallOnLoad = false;
const Tiptap = ({ const Tiptap = ({
settings, settings,
getContentDiv getContentDiv
@@ -214,6 +223,11 @@ const Tiptap = ({
contentPlaceholderRef.current?.appendChild(getContentDiv()); contentPlaceholderRef.current?.appendChild(getContentDiv());
} }
if (!didCallOnLoad) {
didCallOnLoad = true;
post("editor-events:load");
}
const updateScrollPosition = (state: TabStore) => { const updateScrollPosition = (state: TabStore) => {
if (isFocusedRef.current) return; if (isFocusedRef.current) return;
if (state.currentTab === tabRef.current.id) { if (state.currentTab === tabRef.current.id) {
@@ -265,7 +279,7 @@ const Tiptap = ({
return () => { return () => {
unsub(); unsub();
}; };
}, []); }, [getContentDiv]);
const onClickEmptyArea: React.MouseEventHandler<HTMLDivElement> = useCallback( const onClickEmptyArea: React.MouseEventHandler<HTMLDivElement> = useCallback(
(event) => { (event) => {
@@ -466,31 +480,43 @@ const TiptapProvider = (): JSX.Element => {
const settings = useSettings(); const settings = useSettings();
const { colors } = useThemeColors("editor"); const { colors } = useThemeColors("editor");
const contentRef = useRef<HTMLElement>(); const contentRef = useRef<HTMLElement>();
return (
<PortalProvider> const getContentDiv = useCallback(() => {
<Tiptap
settings={settings}
getContentDiv={() => {
if (contentRef.current) { if (contentRef.current) {
logger("info", "return content");
return contentRef.current; return contentRef.current;
} }
logger("info", "new content");
const editorContainer = document.createElement("div"); const editorContainer = document.createElement("div");
editorContainer.classList.add("selectable"); editorContainer.classList.add("selectable");
editorContainer.style.flex = "1"; editorContainer.style.flex = "1";
editorContainer.style.cursor = "text"; editorContainer.style.cursor = "text";
editorContainer.style.padding = "0px 12px"; editorContainer.style.padding = "0px 12px";
editorContainer.style.color = editorContainer.style.color = colors.primary.paragraph;
colors?.primary?.paragraph || colors.primary.paragraph;
editorContainer.style.paddingBottom = `150px`; editorContainer.style.paddingBottom = `150px`;
editorContainer.style.fontSize = `${settings.fontSize}px`; editorContainer.style.fontSize = `${settings.fontSize}px`;
editorContainer.style.fontFamily = editorContainer.style.fontFamily =
getFontById(settings.fontFamily)?.font || "sans-serif"; getFontById(settings.fontFamily)?.font || "sans-serif";
contentRef.current = editorContainer; contentRef.current = editorContainer;
return editorContainer; return editorContainer;
}} // eslint-disable-next-line react-hooks/exhaustive-deps
/> }, []);
useEffect(() => {
if (contentRef.current) {
contentRef.current.style.color = colors.primary.paragraph;
}
}, [colors]);
useEffect(() => {
if (contentRef.current) {
contentRef.current.style.fontSize = `${settings.fontSize}px`;
contentRef.current.style.fontFamily =
getFontById(settings.fontFamily)?.font || "sans-serif";
}
}, [settings.fontSize, settings.fontFamily]);
return (
<PortalProvider>
<Tiptap settings={settings} getContentDiv={getContentDiv} />
</PortalProvider> </PortalProvider>
); );
}; };

View File

@@ -217,16 +217,6 @@ export function useEditorController({
return; return;
} }
if (tab.id === message.tabId) {
logger(
"info",
message.type,
tab.noteId,
"Focused:",
tab.id === useTabStore.getState().currentTab
);
}
const editor = editors[tab.id]; const editor = editors[tab.id];
switch (type) { switch (type) {
case "native:updatehtml": { case "native:updatehtml": {

View File

@@ -17,14 +17,13 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
global.Buffer = require("buffer").Buffer; global.Buffer = require("buffer").Buffer;
import React from "react";
import { createRoot } from "react-dom/client";
import "./index.css";
import App from "./App";
import "@notesnook/editor/styles/katex.min.css";
import "@notesnook/editor/styles/fonts.mobile.css"; import "@notesnook/editor/styles/fonts.mobile.css";
import "@notesnook/editor/styles/katex-fonts.mobile.css"; import "@notesnook/editor/styles/katex-fonts.mobile.css";
import "@notesnook/editor/styles/katex.min.css";
import "@notesnook/editor/styles/styles.css"; import "@notesnook/editor/styles/styles.css";
import { createRoot } from "react-dom/client";
import App from "./App";
import "./index.css";
const rootElement = document.getElementById("root"); const rootElement = document.getElementById("root");
if (rootElement) { if (rootElement) {

View File

@@ -190,7 +190,8 @@ export const EventTypes = {
showTabs: "editor-events:show-tabs", showTabs: "editor-events:show-tabs",
tabFocused: "editor-events:tab-focused", tabFocused: "editor-events:tab-focused",
toc: "editor-events:toc", toc: "editor-events:toc",
createInternalLink: "editor-events:create-internal-link" createInternalLink: "editor-events:create-internal-link",
load: "editor-events:load"
} as const; } as const;
export function randId(prefix: string) { export function randId(prefix: string) {