Skip to content

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

perf: add progressive loading in quill

thecodrrdba4a7a6d5d5
diff --git a/apps/web/src/components/editor/editor.css b/apps/web/src/components/editor/editor.css
index 14894163c..342b5ea44 100644
--- a/apps/web/src/components/editor/editor.css
+++ b/apps/web/src/components/editor/editor.css
@@ -258,3 +258,7 @@ code {
text-decoration: line-through;
text-decoration-color: var(--primary);
}
+
+.ql-editor code {
+ background-color: var(--bgSecondary) !important;
+}
diff --git a/apps/web/src/components/editor/index.js b/apps/web/src/components/editor/index.js
index d67c8648f..e8390de2b 100644
--- a/apps/web/src/components/editor/index.js
+++ b/apps/web/src/components/editor/index.js
@@ -1,5 +1,4 @@
import React, { useEffect, useMemo, useRef, Suspense } from "react";
-//import ReactQuill from "./react-quill";
import { Flex } from "rebass";
import Properties from "../properties";
import {
@@ -33,7 +32,6 @@ function Editor(props) {
const isFocusMode = useAppStore((store) => store.isFocusMode);
const isMobile = useMobile();
const isTablet = useTablet();
-
const isTrial = useUserStore(
(store) => store.user?.subscription?.status === SUBSCRIPTION_STATUS.TRIAL
);
@@ -59,8 +57,19 @@ function Editor(props) {
} = editorstore.get().session;
const { quill } = quillRef.current;
- quill.setContents(data, "init");
- // quill.history.clear();
+ const pages = opsToPages(data);
+ quillRef.current.init(pages, data);
+
+ const editorScroll = document.querySelector(".editorScroll");
+ while (
+ editorScroll.scrollHeight <= editorScroll.clientHeight &&
+ quillRef.current.currentPage < pages.length
+ ) {
+ const page = pages[++quillRef.current.currentPage];
+ if (!page) break;
+ quill.updateContents(getNextPage(page, quillRef.current.quill), "init");
+ }
+
quill.history.stack = {
undo: new ObservableArray("undo"),
redo: new ObservableArray("redo"),
@@ -72,11 +81,28 @@ function Editor(props) {
quill.history.stack.redo = new ObservableArray("redo");
};
- if (!data || !data.length) return;
- const text = quill.getText();
- quill.setSelection(text.length, 0, "init");
+ function onScroll(e) {
+ const scrollBottom = e.target.scrollTop + e.target.clientHeight + 10;
+ const maxScrollHeight = e.target.scrollHeight - 80;
+ if (scrollBottom > maxScrollHeight) {
+ const page = pages[++quillRef.current.currentPage];
+ if (!page) {
+ editorScroll.removeEventListener("scroll", onScroll);
+ return;
+ }
+ quill.updateContents(
+ getNextPage(page, quillRef.current.quill),
+ "init"
+ );
+ }
+ }
+ editorScroll.addEventListener("scroll", onScroll);
+ return () => {
+ editorScroll.removeEventListener("scroll", onScroll);
+ };
}
- }, [sessionState, quillRef, contentType, sessionId]);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [quillRef, contentType, sessionId]);
return (
<Flex
@@ -107,7 +133,7 @@ function Editor(props) {
}}
transition={{ duration: 0.3, ease: "easeOut" }}
maxWidth={"900px"}
- mt={[0, 0, 0]}
+ mt={[0, 0, 25]}
>
<Header />
{contentType === "delta" && (
@@ -135,7 +161,11 @@ function Editor(props) {
setSession((state) => {
state.session.content = {
type: "delta",
- data: delta,
+ data: appendPages(
+ delta,
+ quillRef.current.pages,
+ quillRef.current.currentPage
+ ),
};
});
}}
@@ -149,3 +179,28 @@ function Editor(props) {
);
}
export default Editor;
+
+function opsToPages(delta, pageSize = 20) {
+ const totalPages = delta.length / pageSize;
+ const pages = [];
+ for (var i = 0; i < totalPages; ++i) {
+ const start = i * pageSize;
+ const end = start + pageSize;
+ pages.push(delta.slice(start, end));
+ }
+ return pages;
+}
+
+function getNextPage(page, quill) {
+ const length = quill.getLength();
+ page.splice(0, 0, { retain: length - 1 });
+ return page;
+}
+
+function appendPages(delta, pages, currentPage) {
+ if (pages.length === currentPage + 1) return delta;
+ for (var i = currentPage; i < pages.length; ++i) {
+ delta.push(...pages[i]);
+ }
+ return delta;
+}
diff --git a/apps/web/src/components/editor/react-quill.js b/apps/web/src/components/editor/react-quill.js
index a4e0f48a8..24495c3d5 100644
--- a/apps/web/src/components/editor/react-quill.js
+++ b/apps/web/src/components/editor/react-quill.js
@@ -96,12 +96,16 @@ const quillModules = (isSimple, isFocusMode, isMobile) => ({
},
});
+// const wordCountRegex = /\b\S+\b/g;
export default class ReactQuill extends Component {
/**
* @type {Quill}
*/
quill;
changeTimeout;
+ words = 0;
+ currentPage = 0;
+ pages = [];
getEditor() {
return this.quill.editor;
}
@@ -173,10 +177,10 @@ export default class ReactQuill extends Component {
this.quill.off("text-change", this.textChangeHandler);
}
- textChangeHandler = (_delta, _oldDelta, source) => {
- if (this.props.onWordCountChanged)
- this.props.onWordCountChanged(this.getWordCount());
+ textChangeHandler = (delta, _oldDelta, source) => {
if (source === "init") return;
+ if (this.props.onWordCountChanged)
+ this.props.onWordCountChanged(this.getWordCount(delta));
clearTimeout(this.changeTimeout);
this.changeTimeout = setTimeout(
this.props.onChange,
@@ -184,9 +188,25 @@ export default class ReactQuill extends Component {
);
};
- getWordCount() {
- let text = this.quill.getText();
- return (text.split(/\b\S+\b/g) || []).length;
+ getWordCount(delta) {
+ const wordCount = delta.reduce((prev, curr) => {
+ if (!curr.insert || curr.insert.image) return prev;
+ const text = curr.insert.trim();
+ if (text <= 1) return prev;
+ const count = countWords(text); // curr.insert.split(wordCountRegex).length;
+ return prev + count;
+ }, 0);
+ this.words += wordCount;
+ return this.words;
+ }
+
+ init(pages, delta) {
+ this.pages = pages;
+ this.currentPage = 0;
+ this.words = 0;
+ this.words = this.getWordCount(delta);
+ this.props.onWordCountChanged(this.words);
+ this.quill.setContents(pages[this.currentPage], "init");
}
render() {
@@ -202,3 +222,13 @@ export default class ReactQuill extends Component {
);
}
}
+
+function countWords(str) {
+ let count = 1;
+ for (var i = 0; i < str.length; ++i) {
+ const char = str[i];
+ if (char === " " || char === "\n" || char === "\t" || char === "\r")
+ ++count;
+ }
+ return count;
+}
diff --git a/apps/web/src/components/editor/toolbar.js b/apps/web/src/components/editor/toolbar.js
index b027fe8ce..3973450b1 100644
--- a/apps/web/src/components/editor/toolbar.js
+++ b/apps/web/src/components/editor/toolbar.js
@@ -8,7 +8,6 @@ import { EventManagers } from "../../utils/observablearray";
import Animated from "../animated";
function Toolbar(props) {
- console.log("TOOLBACR");
const { quill } = props;
const sessionState = useStore((store) => store.session.state);
const [undoable, setUndoable] = useState(false);
@@ -22,6 +21,7 @@ function Toolbar(props) {
useEffect(() => {
const editorScroll = document.querySelector(".editorScroll");
+
function onScroll(e) {
const headerOffset = document.querySelector(".editorTitle").scrollHeight;
const hideOffset = headerOffset + 60;
diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.js
index dca2d22ec..dc6d9435f 100644
--- a/apps/web/src/stores/editor-store.js
+++ b/apps/web/src/stores/editor-store.js
@@ -79,6 +79,7 @@ class EditorStore extends BaseStore {
...DEFAULT_SESSION,
...note,
content: content || DEFAULT_SESSION.content,
+ totalWords: state.session.totalWords,
state: SESSION_STATES.new,
};
});
@@ -131,7 +132,6 @@ class EditorStore extends BaseStore {
};
newSession = (context = {}) => {
- console.log("Session");
appStore.setIsEditorOpen(true);
this.set((state) => {
state.session = {
@@ -156,6 +156,7 @@ class EditorStore extends BaseStore {
};
setSession = (set) => {
+ console.log("setSession");
const oldSession = { ...this.get().session };
this.set(set);
this.saveSession(oldSession);
diff --git a/apps/web/src/utils/use-hash-routes.js b/apps/web/src/utils/use-hash-routes.js
index e0f6ae696..43814a844 100644
--- a/apps/web/src/utils/use-hash-routes.js
+++ b/apps/web/src/utils/use-hash-routes.js
@@ -4,10 +4,8 @@ import useHashLocation from "./use-hash-location";
export default function useHashRoutes(routes) {
const [location] = useHashLocation();
const matcher = makeMatcher();
- console.log("Location", location);
for (var key in routes) {
const [match, params] = matcher(key, location);
- console.log("isMatch:", match, key, location);
if (match) {
return routes[key](params);
}
diff --git a/apps/web/src/utils/useHashLocation.js b/apps/web/src/utils/useHashLocation.js
index 164539fb9..3a533338c 100644
--- a/apps/web/src/utils/useHashLocation.js
+++ b/apps/web/src/utils/useHashLocation.js
@@ -4,7 +4,6 @@ import { hashNavigate } from "../navigation";
// returns the current hash location in a normalized form
// (excluding the leading '#' symbol)
const currentLocation = () => {
- console.log("currentLocation", window.location);
return window.location.hash.replace(/^#/, "") || "/";
};