Skip to content

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

web: improve loading of really long notes

Abdullah Atta0eb61c80fac8
diff --git a/apps/web/src/app.tsx b/apps/web/src/app.tsx
index 0920780fe..f4a1fc8cd 100644
--- a/apps/web/src/app.tsx
+++ b/apps/web/src/app.tsx
@@ -197,13 +197,7 @@ function DesktopAppContents({
bg: "background"
}}
>
- {isAppLoaded && (
- <SuspenseLoader
- fallback={<EditorLoader />}
- component={HashRouter}
- condition={isAppLoaded}
- />
- )}
+ {isAppLoaded && <HashRouter />}
</Flex>
</Allotment.Pane>
</Allotment>
diff --git a/apps/web/src/components/editor/footer.tsx b/apps/web/src/components/editor/footer.tsx
index 34a0bd39b..457baa0b0 100644
--- a/apps/web/src/components/editor/footer.tsx
+++ b/apps/web/src/components/editor/footer.tsx
@@ -17,11 +17,12 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { Flex, Text } from "@theme-ui/components";
+import { Button, Flex, Text } from "@theme-ui/components";
import { SaveState, useEditorStore } from "../../stores/editor-store";
import { Loading, Saved, NotSaved } from "../icons";
import { useNoteStatistics } from "./manager";
import { getFormattedDate } from "@notesnook/common";
+import { MAX_AUTO_SAVEABLE_WORDS } from "./types";
const SAVE_STATE_ICON_MAP = {
"-1": NotSaved,
@@ -34,12 +35,22 @@ function EditorFooter() {
const session = useEditorStore((store) => store.getActiveSession());
if (!session) return null;
- const saveState = session.type === "default" ? session.saveState : null;
+ const saveState =
+ session.type === "default" ? session.saveState : SaveState.NotSaved;
const dateEdited = "note" in session ? session.note.dateEdited : 0;
- const SaveStateIcon = SAVE_STATE_ICON_MAP[saveState ?? -1];
+ const SaveStateIcon = SAVE_STATE_ICON_MAP[saveState];
return (
- <Flex sx={{ alignItems: "center", gap: 2 }}>
+ <Flex sx={{ alignItems: "center", justifyContent: "center", gap: 2 }}>
+ {words.total > MAX_AUTO_SAVEABLE_WORDS ? (
+ <Text
+ className="selectable"
+ variant="subBody"
+ sx={{ color: "paragraph" }}
+ >
+ Auto save: off
+ </Text>
+ ) : null}
<Text
className="selectable"
data-test-id="editor-word-count"
@@ -67,7 +78,7 @@ function EditorFooter() {
saveState === SaveState.Saved
? "accent"
: saveState === SaveState.NotSaved
- ? "red"
+ ? "icon-error"
: "paragraph"
}
/>
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index 993c6fdd4..8eb9a8e83 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -395,7 +395,6 @@ export function Editor(props: EditorProps) {
readonly: false,
focusMode: false
};
- const [isLoading, setIsLoading] = useState(true);
useScrollToBlock(session);
useEffect(() => {
@@ -416,7 +415,7 @@ export function Editor(props: EditorProps) {
}, [id]);
return (
- <EditorChrome isLoading={isLoading} {...props}>
+ <EditorChrome {...props}>
<Tiptap
id={id}
nonce={nonce}
@@ -428,7 +427,6 @@ export function Editor(props: EditorProps) {
onLoad={(editor) => {
restoreSelection(editor, id);
restoreScrollPosition(session);
- setIsLoading(false);
}}
onSelectionChange={({ from, to }) =>
Config.set(`${id}:selection`, { from, to })
@@ -508,9 +506,7 @@ export function Editor(props: EditorProps) {
);
}
-function EditorChrome(
- props: PropsWithChildren<EditorProps & { isLoading: boolean }>
-) {
+function EditorChrome(props: PropsWithChildren<EditorProps>) {
const { id, options, children } = props;
const { focusMode, headless, onRequestFocus } = options || {
headless: false,
diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx
index 38d67747a..14fd063ab 100644
--- a/apps/web/src/components/editor/tiptap.tsx
+++ b/apps/web/src/components/editor/tiptap.tsx
@@ -38,15 +38,16 @@ import {
Attachment,
getTableOfContents
} from "@notesnook/editor";
-import { Flex } from "@theme-ui/components";
+import { Box, Flex } from "@theme-ui/components";
import {
PropsWithChildren,
+ useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef
} from "react";
-import { IEditor } from "./types";
+import { IEditor, MAX_AUTO_SAVEABLE_WORDS } from "./types";
import { useEditorConfig, useToolbarConfig, useEditorManager } from "./manager";
import { useIsUserPremium } from "../../hooks/use-is-user-premium";
import { showBuyDialog } from "../../common/dialog-controller";
@@ -59,6 +60,8 @@ import { LinkAttributes } from "@notesnook/editor/dist/extensions/link";
import { writeToClipboard } from "../../utils/clipboard";
import { useEditorStore } from "../../stores/editor-store";
import { parseInternalLink } from "@notesnook/core";
+import Skeleton from "react-loading-skeleton";
+import { showToast } from "../../utils/toast";
export type OnChangeHandler = (
content: () => string,
@@ -133,7 +136,7 @@ function TipTap(props: TipTapProps) {
} = props;
const isUserPremium = useIsUserPremium();
- // const configure = useConfigureEditor();
+ const autoSave = useRef(true);
const doubleSpacedLines = useSettingsStore(
(store) => store.doubleSpacedParagraphs
);
@@ -159,8 +162,14 @@ function TipTap(props: TipTapProps) {
return {
editorProps: {
handleKeyDown(view, event) {
- if ((event.ctrlKey || event.metaKey) && event.key === "s")
+ if ((event.ctrlKey || event.metaKey) && event.key === "s") {
event.preventDefault();
+ onChange?.(
+ () =>
+ getHTMLFromFragment(editor.state.doc.content, editor.schema),
+ false
+ );
+ }
},
handlePaste: (view, event) => {
const hasText = event.clipboardData?.types?.some((type) =>
@@ -195,13 +204,15 @@ function TipTap(props: TipTapProps) {
const instance = toIEditor(editor as Editor);
if (onLoad) onLoad(instance);
+
+ const totalWords = getTotalWords(editor as Editor);
useEditorManager.getState().setEditor(id, {
editor: instance,
canRedo: editor.can().redo(),
canUndo: editor.can().undo(),
statistics: {
words: {
- total: getTotalWords(editor as Editor),
+ total: totalWords,
selected: 0
}
},
@@ -227,6 +238,8 @@ function TipTap(props: TipTapProps) {
const ignoreEdit = transaction.getMeta("ignoreEdit") as boolean;
if (preventSave || !editor.isEditable || !onChange) return;
+ if (!autoSave.current) return;
+
onChange(
() => getHTMLFromFragment(editor.state.doc.content, editor.schema),
ignoreEdit
@@ -322,6 +335,18 @@ function TipTap(props: TipTapProps) {
};
}, [editor]);
+ useEffect(() => {
+ const unsubscribe = useEditorManager.subscribe(
+ (s) => s.editors[id]?.statistics?.words.total,
+ (totalWords) => {
+ autoSave.current = !totalWords || totalWords < MAX_AUTO_SAVEABLE_WORDS;
+ }
+ );
+ return () => {
+ unsubscribe();
+ };
+ }, []);
+
if (readonly) return null;
return (
<>
@@ -388,6 +413,12 @@ function TiptapWrapper(
>
<TipTap
{...props}
+ onLoad={(editor) => {
+ props.onLoad?.(editor);
+ containerRef.current
+ ?.querySelector(".editor-loading-container")
+ ?.remove();
+ }}
editorContainer={() => {
if (editorContainerRef.current) return editorContainerRef.current;
const editorContainer = document.createElement("div");
@@ -407,6 +438,20 @@ function TiptapWrapper(
fontSize={editorConfig.fontSize}
/>
{props.children}
+ <Box className="editor-loading-container">
+ <Skeleton
+ enableAnimation={false}
+ height={22}
+ style={{ marginTop: 16 }}
+ count={2}
+ />
+ <Skeleton
+ enableAnimation={false}
+ height={22}
+ width={25}
+ style={{ marginTop: 16 }}
+ />
+ </Box>
</Flex>
</PortalProvider>
);
diff --git a/apps/web/src/components/editor/types.ts b/apps/web/src/components/editor/types.ts
index 91ea1cce9..7ba813cdf 100644
--- a/apps/web/src/components/editor/types.ts
+++ b/apps/web/src/components/editor/types.ts
@@ -19,6 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { Attachment } from "@notesnook/editor";
+export const MAX_AUTO_SAVEABLE_WORDS = 100_000;
+
export type NoteStatistics = {
words: {
total: number;
diff --git a/apps/web/src/components/loaders/editor-loader.tsx b/apps/web/src/components/loaders/editor-loader.tsx
index d763d7a32..b5518bc2d 100644
--- a/apps/web/src/components/loaders/editor-loader.tsx
+++ b/apps/web/src/components/loaders/editor-loader.tsx
@@ -23,14 +23,16 @@ import { Box, Flex, Text } from "@theme-ui/components";
import "react-loading-skeleton/dist/skeleton.css";
import Config from "../../utils/config";
import { TextScramble } from "../text-scramble";
-import useHashLocation from "../../hooks/use-hash-location";
+import useHashLocation, {
+ getHashLocation
+} from "../../hooks/use-hash-location";
import makeMatcher from "wouter/matcher";
import { Lock } from "../icons";
const matcher = makeMatcher();
const EDITOR_MARGINS = Config.get("editor:margins", true);
export const EditorLoader = memo(function EditorLoader() {
- const [{ location }] = useHashLocation();
+ const location = getHashLocation();
const [isNoteLoading] = matcher("/notes/:noteId/edit", location);
const [isNoteLocked] = matcher("/notes/:noteId/unlock", location);
diff --git a/apps/web/src/hooks/use-hash-location.ts b/apps/web/src/hooks/use-hash-location.ts
index a274d8722..e01aae83f 100644
--- a/apps/web/src/hooks/use-hash-location.ts
+++ b/apps/web/src/hooks/use-hash-location.ts
@@ -63,3 +63,5 @@ export default function useHashLocation() {
}, []);
return [loc, queryParams, hashNavigate] as const;
}
+
+export const getHashLocation = currentLocation;