Skip to content

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

feat: add editor focus mode and improve editor task list

thecodrrfff9453dc086
diff --git a/apps/web/package.json b/apps/web/package.json
index d23b443bf..e3d5f180c 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -22,7 +22,6 @@
"qclone": "^1.0.4",
"quill": "https://github.com/thecodrr/quill.git#1.3.7",
"quill-magic-url": "^1.0.3",
- "quill-task-list": "https://github.com/CJEnright/quill-task-list",
"react": "^16.13.1",
"react-app-polyfill": "^1.0.6",
"react-dom": "^16.13.1",
diff --git a/apps/web/src/components/editor/editor.css b/apps/web/src/components/editor/editor.css
index 3c4098f6b..24746793d 100644
--- a/apps/web/src/components/editor/editor.css
+++ b/apps/web/src/components/editor/editor.css
@@ -206,9 +206,15 @@ code {
.ql-editor {
font-size: 16px;
+ line-height: 1.6 !important;
color: var(--text) !important;
padding: 10px 0px 10px 0px !important;
}
+
+.ql-editor > p {
+ margin-bottom: 20px !important;
+}
+
.ql-editor.ql-blank::before {
left: 0px !important;
right: 0px !important;
@@ -223,6 +229,7 @@ code {
.ql-editor ul[data-checked="true"] > li::before {
font-size: 20px;
}
+
.ql-editor ul[data-checked="true"] > li::before {
color: var(--primary) !important;
}
diff --git a/apps/web/src/components/editor/index.js b/apps/web/src/components/editor/index.js
index 7a6bcb686..50bfc9b8f 100644
--- a/apps/web/src/components/editor/index.js
+++ b/apps/web/src/components/editor/index.js
@@ -140,6 +140,7 @@ function Editor() {
ref={quillRef}
refresh={sessionState === SESSION_STATES.new}
isSimple={!isLoggedin || (isLoggedin && !isTrial)}
+ isFocusMode={isFocusMode}
onFocus={() => {
toggleProperties(false);
}}
diff --git a/apps/web/src/components/editor/modules/betterlist/index.js b/apps/web/src/components/editor/modules/betterlist/index.js
new file mode 100644
index 000000000..32ae7ceb2
--- /dev/null
+++ b/apps/web/src/components/editor/modules/betterlist/index.js
@@ -0,0 +1,19 @@
+import Quill from "quill";
+
+const List = Quill.import("formats/list");
+
+class BetterList extends List {
+ constructor(el) {
+ super(el);
+
+ const isCheckList = el.hasAttribute("data-checked");
+ el.addEventListener("touchstart", (e) => {
+ if (!isCheckList) {
+ return;
+ }
+ e.preventDefault();
+ });
+ }
+}
+
+Quill.register("formats/lists", BetterList);
diff --git a/apps/web/src/components/editor/modules/focus/focus.css b/apps/web/src/components/editor/modules/focus/focus.css
new file mode 100644
index 000000000..8ff34a871
--- /dev/null
+++ b/apps/web/src/components/editor/modules/focus/focus.css
@@ -0,0 +1,11 @@
+.quill-focus .ql-editor * {
+ transition: opacity 300ms;
+}
+
+.quill-focus .ql-editor :not(.focused-blot) {
+ opacity: 0.7;
+}
+
+.quill-focus .focused-blot {
+ opacity: 1;
+}
diff --git a/apps/web/src/components/editor/modules/focus/index.js b/apps/web/src/components/editor/modules/focus/index.js
new file mode 100644
index 000000000..eeb7c7f53
--- /dev/null
+++ b/apps/web/src/components/editor/modules/focus/index.js
@@ -0,0 +1,67 @@
+import "./focus.css";
+
+class Focus {
+ constructor(quill, options) {
+ this.quill = quill;
+ this.options = options;
+ this.container = quill.container;
+ this.focusClass = options.focusClass || "focused-blot";
+ if (options.enabled) {
+ this.toggle(true);
+ }
+ }
+
+ onSelectionChanged = (range) => {
+ this.highlightBlot(range);
+ };
+
+ onTextChanged = () => {
+ const selection = this.quill.getSelection();
+ this.highlightBlot(selection);
+ };
+
+ toggle(enabled) {
+ if (enabled) {
+ this.quill.container.classList.add("quill-focus");
+ this.removeHighlighting();
+ this.quill.on("selection-change", this.onSelectionChanged);
+ this.quill.on("text-change", this.onTextChanged);
+ } else {
+ this.removeHighlighting();
+ this.quill.container.classList.remove("quill-focus");
+ this.quill.off("selection-change", this.onSelectionChanged);
+ this.quill.off("text-change", this.onTextChanged);
+ }
+ }
+
+ removeHighlighting = () => {
+ this.container.querySelectorAll("." + this.focusClass).forEach((blot) => {
+ if (blot && blot.classList) {
+ blot.classList.remove(this.focusClass);
+ }
+ });
+ };
+
+ highlightBlot = (range) => {
+ this.removeHighlighting();
+ if (range && range.index !== undefined) {
+ var [line] = this.quill.getLine(range.index);
+ if (line && line.domNode) {
+ line.domNode.classList.add(this.focusClass);
+ this.recursiveWalkUpward(line.domNode, (parent) => {
+ parent.classList.add(this.focusClass);
+ });
+ }
+ }
+ };
+
+ recursiveWalkUpward(domNode, cb) {
+ var parent = domNode.parentNode;
+ while (!parent.classList.contains("ql-editor")) {
+ cb(parent);
+ parent = parent.parentNode;
+ }
+ }
+}
+
+export default Focus;
diff --git a/apps/web/src/components/editor/react-quill.js b/apps/web/src/components/editor/react-quill.js
index 908e27b8d..310a566aa 100644
--- a/apps/web/src/components/editor/react-quill.js
+++ b/apps/web/src/components/editor/react-quill.js
@@ -2,34 +2,18 @@ import React, { Component } from "react";
import Quill from "quill";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.core.css";
+import "./modules/betterlist";
import MarkdownShortcuts from "./modules/markdown";
import MagicUrl from "quill-magic-url";
import { Text } from "rebass";
+import QuillFocus from "./modules/focus";
Quill.register("modules/markdownShortcuts", MarkdownShortcuts);
Quill.register("modules/magicUrl", MagicUrl);
+Quill.register("modules/focus", QuillFocus);
-const List = Quill.import("formats/list");
-
-class BetterList extends List {
- constructor(el) {
- super(el);
-
- const isCheckList = el.hasAttribute("data-checked");
- el.addEventListener("touchstart", (e) => {
- if (!isCheckList) {
- return;
- }
- e.preventDefault();
- });
- }
-}
-
-Quill.register("formats/lists", BetterList);
-
-const quillModules = (isSimple) => ({
+const quillModules = (_isSimple, isFocusMode) => ({
toolbar: [
- //
["bold", "italic", "underline", "strike", "blockquote"],
[{ header: "2" }, { header: "3" }, { header: [false, 4, 5, 6] }],
[
@@ -38,13 +22,8 @@ const quillModules = (isSimple) => ({
{ align: "right" },
{ align: "justify" },
],
- [
- { list: "ordered" },
- { list: "bullet" },
- { list: "check" },
- { indent: "-1" },
- { indent: "+1" },
- ],
+ [{ list: "ordered" }, { list: "bullet" }, { list: "check" }],
+ [{ indent: "-1" }, { indent: "+1" }],
[{ size: ["small", false, "large", "huge"] }],
["code-block", { script: "sub" }, { script: "super" }],
[{ color: [] }, { background: [] }],
@@ -55,6 +34,10 @@ const quillModules = (isSimple) => ({
markdownShortcuts: {},
magicUrl: true,
history: { maxStack: 1000 * 5 },
+ focus: {
+ enabled: isFocusMode,
+ focusClass: "focused-blot", // Defaults to .focused-blot.
+ },
});
export default class ReactQuill extends Component {
@@ -68,13 +51,26 @@ export default class ReactQuill extends Component {
}
shouldComponentUpdate(nextProps) {
+ console.log("should", nextProps.isFocusMode, this.props.isFocusMode);
return (
this.props.readOnly !== nextProps.readOnly ||
- this.props.isSimple !== nextProps.isSimple
+ this.props.isSimple !== nextProps.isSimple ||
+ this.props.isFocusMode !== nextProps.isFocusMode
);
}
+ componentDidUpdate() {
+ const { isFocusMode } = this.props;
+
+ const focus = this.quill.getModule("focus");
+ focus.toggle(isFocusMode);
+ }
+
componentDidMount() {
+ this._initializeQuill();
+ }
+
+ _initializeQuill() {
const {
placeholder,
container,
@@ -86,12 +82,14 @@ export default class ReactQuill extends Component {
modules,
id,
isSimple,
+ isFocusMode,
} = this.props;
+
this.quill = new Quill("#" + id, {
placeholder,
bounds: container,
scrollingContainer: scrollContainer,
- modules: modules || quillModules(isSimple),
+ modules: modules || quillModules(isSimple, isFocusMode),
theme: "snow",
readOnly,
});
diff --git a/apps/web/yarn.lock b/apps/web/yarn.lock
index 8479fdc07..21caa19ce 100644
--- a/apps/web/yarn.lock
+++ b/apps/web/yarn.lock
@@ -10643,10 +10643,6 @@ quill-magic-url@^1.0.3:
normalize-url "^3.0.1"
quill-delta "^3.6.2"
-"quill-task-list@https://github.com/CJEnright/quill-task-list":
- version "0.0.0"
- resolved "https://github.com/CJEnright/quill-task-list#5a850aebb4bdc134715ad81ee42ffd5d49f5eef3"
-
"quill@https://github.com/thecodrr/quill.git#1.3.7":
version "1.3.7"
resolved "https://github.com/thecodrr/quill.git#adbb4f12348764ceca69deaf3c507d8601fdea6e"