Skip to content

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

fix: editor word counter

thecodrr25a0bef45c02
diff --git a/apps/web/package.json b/apps/web/package.json
index 12f9b81ac..f2e8e8e16 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -32,6 +32,7 @@
},
"devDependencies": {
"@types/hookrouter": "^2.2.3",
+ "@types/quill": "^2.0.3",
"babel-eslint": "^10.1.0",
"babel-loader": "8.1.0",
"eslint": "^6.8.0",
@@ -71,4 +72,4 @@
"last 4 edge version"
]
}
-}
\ No newline at end of file
+}
diff --git a/apps/web/src/components/editor/header.js b/apps/web/src/components/editor/header.js
index c06ccd7d3..bb175df2c 100644
--- a/apps/web/src/components/editor/header.js
+++ b/apps/web/src/components/editor/header.js
@@ -6,7 +6,6 @@ import { useStore as useAppStore } from "../../stores/app-store";
import TitleBox from "./title-box";
import { useStore, SESSION_STATES } from "../../stores/editor-store";
import { timeConverter } from "../../utils/time";
-import { countWords } from "../../utils/string";
import { showToast } from "../../utils/toast";
const TextSeperator = () => {
@@ -21,7 +20,7 @@ function Header() {
const title = useStore((store) => store.session.title);
const dateEdited = useStore((store) => store.session.dateEdited);
const id = useStore((store) => store.session.id);
- const text = useStore((store) => store.session.content.text);
+ const totalWords = useStore((store) => store.session.totalWords);
const isSaving = useStore((store) => store.session.isSaving);
const sessionState = useStore((store) => store.session.state);
const setSession = useStore((store) => store.setSession);
@@ -68,23 +67,13 @@ function Header() {
sx={{
display: "flex",
alignItems: "center",
- marginTop: dateEdited || text?.length || id ? 0 : [0, 2, 2],
- marginBottom: dateEdited || text?.length || id ? [1, 2, 2] : 0,
}}
>
- {dateEdited > 0 ? (
- <>
- {timeConverter(dateEdited)}
- <TextSeperator />
- </>
- ) : null}
- {text.length > 0 ? (
- <>
- {countWords(text) + " words"}
- <TextSeperator />
- </>
- ) : null}
- {id ? <>{isSaving ? "Saving" : "Saved"}</> : null}
+ {timeConverter(dateEdited) || "-"}
+ <TextSeperator />
+ {id ? totalWords + " words" : "-"}
+ <TextSeperator />
+ {id ? (isSaving ? "Saving" : "Saved") : "-"}
</Text>
</Flex>
<Flex
diff --git a/apps/web/src/components/editor/index.js b/apps/web/src/components/editor/index.js
index 8f8966d44..40a4e2ef7 100644
--- a/apps/web/src/components/editor/index.js
+++ b/apps/web/src/components/editor/index.js
@@ -23,6 +23,7 @@ function Editor() {
const setSession = useStore((store) => store.setSession);
const saveSession = useStore((store) => store.saveSession);
const toggleProperties = useStore((store) => store.toggleProperties);
+ const updateWordCount = useStore((store) => store.updateWordCount);
const init = useStore((store) => store.init);
const isFocusMode = useAppStore((store) => store.isFocusMode);
const isTrial = useUserStore(
@@ -51,6 +52,7 @@ function Editor() {
if (!quillRef || !quillRef.current) return;
if (sessionState === SESSION_STATES.new) {
+ editorstore.set((state) => (state.session.state = SESSION_STATES.stale));
const {
content: { delta },
} = editorstore.get().session;
@@ -61,7 +63,6 @@ function Editor() {
if (!delta.ops || !delta.ops.length) return;
const text = quill.getText();
quill.setSelection(text.length, 0, "init");
- editorstore.set((state) => (state.session.state = SESSION_STATES.stale));
}
}, [sessionState, quillRef]);
@@ -100,6 +101,7 @@ function Editor() {
saveSession();
}}
changeInterval={500}
+ onWordCountChanged={updateWordCount}
onChange={() => {
const { quill } = quillRef.current;
const delta = quill.getContents().ops;
diff --git a/apps/web/src/components/editor/react-quill.js b/apps/web/src/components/editor/react-quill.js
index 702c9cd6d..1ffb31772 100644
--- a/apps/web/src/components/editor/react-quill.js
+++ b/apps/web/src/components/editor/react-quill.js
@@ -51,7 +51,9 @@ const simpleQuillModules = {
};
export default class ReactQuill extends Component {
- /** @private */
+ /**
+ * @type {Quill}
+ */
quill;
changeTimeout;
getEditor() {
@@ -110,6 +112,7 @@ export default class ReactQuill extends Component {
}
textChangeHandler = (_delta, _oldDelta, source) => {
+ this.props.onWordCountChanged(this.getWordCount());
if (source === "init") return;
clearTimeout(this.changeTimeout);
this.changeTimeout = setTimeout(
@@ -118,8 +121,12 @@ export default class ReactQuill extends Component {
);
};
+ getWordCount() {
+ let text = this.quill.getText();
+ return (text.match(/\b\S+\b/g) || []).length;
+ }
+
render() {
- console.log("RERENDERING QUILL");
return <pre onFocus={this.props.onFocus} id={this.props.id} />;
}
}
diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.js
index 9d44f31fe..e2a6b2aeb 100644
--- a/apps/web/src/stores/editor-store.js
+++ b/apps/web/src/stores/editor-store.js
@@ -21,6 +21,7 @@ const DEFAULT_SESSION = {
context: undefined,
colors: [],
dateEdited: 0,
+ totalWords: 0,
content: {
text: "",
delta: {
@@ -183,6 +184,12 @@ class EditorStore extends BaseStore {
);
};
+ updateWordCount = (count) => {
+ this.set((state) => {
+ state.session.totalWords = count;
+ });
+ };
+
/**
* @private internal
* @param {Boolean} isLocked
diff --git a/apps/web/yarn.lock b/apps/web/yarn.lock
index 92d36af8d..2b9b9d719 100644
--- a/apps/web/yarn.lock
+++ b/apps/web/yarn.lock
@@ -1815,6 +1815,14 @@
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.4.tgz#15925414e0ad2cd765bfef58842f7e26a7accb24"
integrity sha512-1HcDas8SEj4z1Wc696tH56G8OlRaH/sqZOynNNB+HF0WOeXPaxTtbYzJY2oEfiUxjSKjhCKr+MvR7dCHcEelug==
+"@types/quill@^2.0.3":
+ version "2.0.3"
+ resolved "https://registry.yarnpkg.com/@types/quill/-/quill-2.0.3.tgz#bddb7d0951c277de6bf006faa29b39bfa3941ebb"
+ integrity sha512-bsMS+1pXXH06aP9oZOw0o7mGQnEhWo9yXoc4Hh8RO33JCgYzDZUJUUQbqzRwPoJV7T4RDwkhV/dCfXhNBUGqPQ==
+ dependencies:
+ parchment "^1.1.2"
+ quill-delta "^4.0.1"
+
"@types/react@*":
version "16.9.49"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.49.tgz#09db021cf8089aba0cdb12a49f8021a69cce4872"
@@ -4828,6 +4836,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.1.2.tgz#4b62c42b8e03de3f848460b639079920695d0154"
integrity sha512-KaJUt+M9t1qaIteSvjc6P3RbMdXsNhK61GRftR6SNxqmhthcd9MGIi4T+o0jD8LUSpSnSKXE20nLtJ3fOHxQig==
+ version "1.2.0"
+ resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.2.0.tgz#73ee11982d86caaf7959828d519cfe927fac5f03"
+ integrity sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==
+
fast-glob@^2.0.2:
version "2.2.7"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-2.2.7.tgz#6953857c3afa475fff92ee6015d52da70a4cd39d"
@@ -7055,6 +7068,11 @@ lodash._reinterpolate@^3.0.0:
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
integrity sha1-DM8tiRZq8Ds2Y8eWU4t1rG4RTZ0=
+lodash.clonedeep@^4.5.0:
+ version "4.5.0"
+ resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
+ integrity sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=
+
lodash.isequal@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
@@ -7978,7 +7996,7 @@ param-case@^3.0.3:
dot-case "^3.0.3"
tslib "^1.10.0"
-parchment@^1.1.4:
+parchment@^1.1.2, parchment@^1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/parchment/-/parchment-1.1.4.tgz#aeded7ab938fe921d4c34bc339ce1168bc2ffde5"
integrity sha512-J5FBQt/pM2inLzg4hEWmzQx/8h8D0CiDxaG3vyp9rKrQRSDgBlhjdP5jQGgosEajXPSQouXGHOmVdgo7QmJuOg==
@@ -9129,6 +9147,15 @@ quill-delta@^3.6.2:
extend "^3.0.2"
fast-diff "1.1.2"
+quill-delta@^4.0.1:
+ version "4.2.2"
+ resolved "https://registry.yarnpkg.com/quill-delta/-/quill-delta-4.2.2.tgz#015397d046e0a3bed087cd8a51f98c11a1b8f351"
+ integrity sha512-qjbn82b/yJzOjstBgkhtBjN2TNK+ZHP/BgUQO+j6bRhWQQdmj2lH6hXG7+nwwLF41Xgn//7/83lxs9n2BkTtTg==
+ dependencies:
+ fast-diff "1.2.0"
+ lodash.clonedeep "^4.5.0"
+ lodash.isequal "^4.5.0"
+
quill-magic-url@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/quill-magic-url/-/quill-magic-url-1.0.3.tgz#ff74a3cc6ce8fcc2a2e2968e9b1cd2c632352d7f"