Skip to content

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

fix: editor loading jank & simplify content loading

thecodrraeba6ff241c0
diff --git a/apps/web/src/app.js b/apps/web/src/app.js
index afb3d45ad..957a745bd 100644
--- a/apps/web/src/app.js
+++ b/apps/web/src/app.js
@@ -9,6 +9,7 @@ import useTablet from "./utils/use-tablet";
import { LazyMotion, domAnimation } from "framer-motion";
import useDatabase from "./hooks/use-database";
import Loader from "./components/loader";
+import EditorLoading from "./components/editor/loading";
const GlobalMenuWrapper = React.lazy(() =>
import("./components/global-menu-wrapper")
@@ -148,12 +149,7 @@ function App() {
flexDirection="column"
>
<SuspenseLoader
- fallback={
- <Loader
- title="Fun fact"
- text="Notesnook was released in January 2021 by a team of only 3 people."
- />
- }
+ fallback={<EditorLoading />}
component={HashRouter}
condition={isAppLoaded}
/>
diff --git a/apps/web/src/components/editor/index.js b/apps/web/src/components/editor/index.js
index 6673dee46..360a1e47b 100644
--- a/apps/web/src/components/editor/index.js
+++ b/apps/web/src/components/editor/index.js
@@ -1,4 +1,11 @@
-import React, { useEffect, useRef, Suspense, useCallback } from "react";
+import React, {
+ useEffect,
+ useRef,
+ Suspense,
+ useCallback,
+ useMemo,
+ useState,
+} from "react";
import { Flex } from "rebass";
import Properties from "../properties";
import {
@@ -19,8 +26,8 @@ const ReactMCE = React.lazy(() => import("./tinymce"));
function Editor({ noteId, nonce }) {
const editorRef = useRef();
- const sessionState = useStore((store) => store.session.state);
- const sessionNonce = useStore((store) => store.session.nonce);
+ const [isEditorLoading, setIsEditorLoading] = useState(true);
+ const sessionId = useStore((store) => store.session.id);
const contentType = useStore((store) => store.session.content?.type);
const setSession = useStore((store) => store.setSession);
const saveSession = useStore((store) => store.saveSession);
@@ -32,6 +39,10 @@ function Editor({ noteId, nonce }) {
const isFocusMode = useAppStore((store) => store.isFocusMode);
const isMobile = useMobile();
const isTablet = useTablet();
+ const isSessionReady = useMemo(
+ () => nonce || sessionId || editorRef.current?.editor?.initialized,
+ [nonce, sessionId, editorRef]
+ );
useEffect(() => {
init();
@@ -39,6 +50,7 @@ function Editor({ noteId, nonce }) {
const startSession = useCallback(
async function startSession(noteId) {
+ console.log("starting session", nonce, noteId);
if (noteId === 0) newSession(nonce);
else if (noteId) {
await openSession(noteId);
@@ -47,13 +59,6 @@ function Editor({ noteId, nonce }) {
[newSession, openSession, nonce]
);
- useEffect(() => {
- if (!editorRef.current?.editor) return;
- (async () => {
- await startSession(noteId);
- })();
- }, [startSession, noteId, nonce]);
-
const setContent = useCallback(() => {
const {
id,
@@ -62,12 +67,8 @@ function Editor({ noteId, nonce }) {
const editor = editorRef.current?.editor;
if (!editor) return;
- clearTimeout(editor.changeTimeout);
async function setContents() {
if (!editor.initialized) return;
-
- // NOTE: workaround to not fire onEditorChange event on content load
- editor.isLoading = true;
editor.setContent(data, { format: "html" });
editor.undoManager.reset();
@@ -75,29 +76,52 @@ function Editor({ noteId, nonce }) {
editorstore.set((state) => (state.session.state = SESSION_STATES.stale));
if (id) await db.attachments.downloadImages(id);
- }
- if (!isMobile) editor.focus();
+ editor.focus();
+ }
setContents();
- }, [isMobile]);
+ }, []);
- useEffect(() => {
- if (sessionState === SESSION_STATES.new) {
- setContent();
+ const clearContent = useCallback(() => {
+ const editor = editorRef.current?.editor;
+ console.log(editor);
+ if (!editor) return;
+
+ async function clearContents() {
+ if (!editor.initialized) return;
+ editor.setContent("<p><br/></p>", { format: "html" });
+ editor.undoManager.reset();
+ editor.setDirty(false);
+
+ editor.focus();
}
- }, [
- editorRef,
- isMobile,
- contentType,
- sessionState,
- sessionNonce,
- setContent,
- ]);
+ clearContents();
+ }, []);
+
+ useEffect(() => {
+ if (!sessionId || !editorstore.get().session.contentId) return;
+ setContent();
+ }, [sessionId, setContent]);
+
+ useEffect(() => {
+ if (!nonce) return;
+
+ clearContent();
+ }, [nonce, clearContent]);
+
+ useEffect(() => {
+ (async () => {
+ await startSession(noteId);
+ })();
+ }, [startSession, noteId, nonce]);
+
+ // if (!isSessionReady) return <EditorLoading />;
return (
<Flex
flexDirection="column"
+ id="editorContainer"
flex={1}
sx={{
position: "relative",
@@ -105,6 +129,19 @@ function Editor({ noteId, nonce }) {
overflow: "hidden",
}}
>
+ {isEditorLoading ? (
+ <Flex
+ sx={{
+ position: "absolute",
+ width: "full",
+ height: "full",
+ bg: "background",
+ zIndex: 999,
+ }}
+ >
+ <EditorLoading />
+ </Flex>
+ ) : null}
<Toolbar />
<Flex
variant="columnFill"
@@ -124,6 +161,7 @@ function Editor({ noteId, nonce }) {
borderBottomColor: "border",
justifyContent: "center",
alignItems: "center",
+ minHeight: "39px",
}}
/>
<AnimatedFlex
@@ -153,33 +191,40 @@ function Editor({ noteId, nonce }) {
>
<Header />
- <Suspense fallback={<EditorLoading />}>
- {contentType === "tiny" ? (
- <>
- <ReactMCE
- editorRef={editorRef}
- onFocus={() => toggleProperties(false)}
- onSave={saveSession}
- onChange={(content) => {
- if (!content) return;
- if (!content.length) content = "<p><br></pr>";
- setSession((state) => {
- state.session.content = {
- type: "tiny",
- data: content,
- };
- });
- }}
- changeInterval={100}
- onWordCountChanged={updateWordCount}
- onInit={async () => {
- await startSession(noteId);
- setContent();
- }}
- />
- </>
- ) : null}
- </Suspense>
+ {isSessionReady && (
+ <Suspense fallback={<div />}>
+ {contentType === "tiny" ? (
+ <>
+ <ReactMCE
+ editorRef={editorRef}
+ onFocus={() => toggleProperties(false)}
+ onSave={saveSession}
+ sessionId={sessionId}
+ initialValue={editorstore.get()?.session?.content?.data}
+ onChange={(content) => {
+ if (!content || content === "<p><br></pr>") return;
+
+ setSession((state) => {
+ state.session.content = {
+ type: "tiny",
+ data: content,
+ };
+ });
+ }}
+ changeInterval={100}
+ onWordCountChanged={updateWordCount}
+ onInit={(editor) => {
+ editor.focus();
+ setTimeout(() => {
+ setIsEditorLoading(false);
+ // a short delay to make sure toolbar has rendered.
+ }, 100);
+ }}
+ />
+ </>
+ ) : null}
+ </Suspense>
+ )}
</AnimatedFlex>
</Flex>
<Properties noteId={noteId} />
diff --git a/apps/web/src/components/editor/loading.js b/apps/web/src/components/editor/loading.js
index ea7cd1608..732799225 100644
--- a/apps/web/src/components/editor/loading.js
+++ b/apps/web/src/components/editor/loading.js
@@ -1,4 +1,3 @@
-import React from "react";
import { Flex, Text } from "rebass";
import * as Icon from "../icons";
diff --git a/apps/web/src/components/editor/tinymce.js b/apps/web/src/components/editor/tinymce.js
index a311b5b39..cf202c892 100644
--- a/apps/web/src/components/editor/tinymce.js
+++ b/apps/web/src/components/editor/tinymce.js
@@ -124,7 +124,7 @@ const plugins = {
pro: "textpattern picker",
};
-const changeEvents = "change input compositionend setcontent paste";
+const changeEvents = "change input compositionend paste";
function TinyMCE(props) {
const {
@@ -138,6 +138,7 @@ function TinyMCE(props) {
onFocus,
editorRef,
onInit,
+ sessionId,
} = props;
const [oldSkin, newSkin] = useSkin();
const isUserPremium = useIsUserPremium();
@@ -184,6 +185,7 @@ function TinyMCE(props) {
return (
<Editor
+ id={sessionId}
ref={tinymceRef}
onFocus={onFocus}
initialValue={initialValue}
@@ -225,7 +227,8 @@ function TinyMCE(props) {
init_instance_callback: (editor) => {
editor.serializer.addTempAttr("data-progress");
clearTimeout(editor.changeTimeout);
- onInit(editor);
+ onInit && onInit(editor);
+ console.log("init");
},
setup: (editor) => {
editor.on("tap", (e) => {
@@ -249,12 +252,8 @@ function TinyMCE(props) {
if (onWordCountChanged) onWordCountChanged(editor.countWords());
if (!editor.getHTML) return;
- if (editor.isLoading) {
- editor.isLoading = false;
- return;
- }
+
editor.getHTML().then((html) => {
- console.log("GOT CHNAGED");
onChange(html, editor);
});
}, changeInterval);
diff --git a/apps/web/src/components/unlock/index.js b/apps/web/src/components/unlock/index.js
index 1adc52ec6..4fbc2e570 100644
--- a/apps/web/src/components/unlock/index.js
+++ b/apps/web/src/components/unlock/index.js
@@ -46,8 +46,8 @@ function Unlock(props) {
useEffect(() => {
clearSession(false);
- setSelectedNote(noteId);
setIsEditorOpen(true);
+ setImmediate(() => setSelectedNote(noteId));
}, [clearSession, setIsEditorOpen, setSelectedNote, noteId]);
return (
diff --git a/apps/web/src/navigation/hash-routes.js b/apps/web/src/navigation/hash-routes.js
index 68025efd9..512d5f399 100644
--- a/apps/web/src/navigation/hash-routes.js
+++ b/apps/web/src/navigation/hash-routes.js
@@ -69,8 +69,8 @@ const hashroutes = {
"/notes/:noteId/edit": ({ noteId }) => {
closeOpenedDialog();
return (
- <Suspense fallback={<EditorLoading text="Opening note..." />}>
- <Editor noteId={noteId} nonce={Math.random()} />
+ <Suspense fallback={<EditorLoading />}>
+ <Editor noteId={noteId} />
</Suspense>
);
},