Skip to content

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

feat: add checklist to tinymce editor

thecodrrece126f31f00
diff --git a/apps/web/src/components/editor/plugins/checklist/index.js b/apps/web/src/components/editor/plugins/checklist/index.js
new file mode 100644
index 000000000..dfd8f6603
--- /dev/null
+++ b/apps/web/src/components/editor/plugins/checklist/index.js
@@ -0,0 +1 @@
+import "./plugin";
diff --git a/apps/web/src/components/editor/plugins/checklist/plugin.js b/apps/web/src/components/editor/plugins/checklist/plugin.js
new file mode 100644
index 000000000..27223944c
--- /dev/null
+++ b/apps/web/src/components/editor/plugins/checklist/plugin.js
@@ -0,0 +1,114 @@
+import tinymce from "tinymce/tinymce";
+
+(function () {
+ tinymce.PluginManager.add("checklist", function (editor, url) {
+ /**
+ * Plugin behaviour for when the Toolbar or Menu item is selected
+ *
+ * @private
+ */
+ function _onAction() {
+ var content = `<ul class="tox-checklist"><li></li></ul>`;
+ editor.undoManager.transact(function () {
+ editor.insertContent(content);
+ });
+ }
+
+ editor.addCommand("InsertCheckList", function (ui, value) {
+ _onAction();
+ });
+
+ editor.ui.registry.addButton("checklist", {
+ icon: "checklist",
+ tooltip: "Insert check list",
+ onAction: _onAction,
+ });
+
+ editor.on(
+ "mousedown",
+ function (event) {
+ var node = event.target;
+ var parent = node.parentElement;
+ if (event.offsetX > 0 || parent.className !== "tox-checklist") {
+ return;
+ }
+ //editor.selection.setRng(range_selection);
+
+ editor.undoManager.transact(function () {
+ event.preventDefault();
+ if (parent.className === "tox-checklist") {
+ if (
+ node.nodeName === "LI" &&
+ node.className === "tox-checklist--checked"
+ ) {
+ node.className = "";
+ } else if (node.nodeName === "LI" && node.className === "") {
+ node.className = "tox-checklist--checked";
+ }
+ }
+ });
+
+ //editor.fire("input",)
+ //editor.selection.setRng(range_selection);
+ },
+ { capture: true, passive: false }
+ );
+
+ editor.on(
+ "touchstart",
+ function (event) {
+ var node = event.target;
+ var parent = node.parentElement;
+ if (
+ event.targetTouches.length > 0 ||
+ event.targetTouches[0].clientX > 45 ||
+ parent.className !== "tox-checklist"
+ ) {
+ return;
+ }
+ //editor.selection.setRng(range_selection);
+ editor.undoManager.transact(function () {
+ event.preventDefault();
+ if (parent.className === "tox-checklist") {
+ node.scrollIntoView(false);
+ if (
+ node.nodeName === "LI" &&
+ node.className === "tox-checklist--checked"
+ ) {
+ node.className = "";
+ } else if (node.nodeName === "LI" && node.className === "") {
+ node.className = "tox-checklist--checked";
+ }
+ }
+ });
+
+ //editor.fire("input",)
+ //editor.selection.setRng(range_selection);
+ },
+ { capture: true, passive: false }
+ );
+
+ editor.on("NodeChange", function (event) {
+ var node = event.target;
+ var parent = node?.parentElement;
+ if (!parent) return;
+ if (parent.className === "tox-checklist") {
+ if (
+ node.nodeName === "LI" &&
+ node.className === "tox-checklist--checked"
+ ) {
+ node.className = "";
+ }
+ }
+ });
+
+ editor.on("NewBlock", function (event) {
+ if (
+ event.newBlock.nodeName === "LI" &&
+ event.newBlock.className === "tox-checklist--checked"
+ ) {
+ event.newBlock.className = "";
+ }
+ });
+ });
+})();
diff --git a/apps/web/src/components/editor/tinymce.js b/apps/web/src/components/editor/tinymce.js
index cc06e8aab..9308739e8 100644
--- a/apps/web/src/components/editor/tinymce.js
+++ b/apps/web/src/components/editor/tinymce.js
@@ -22,6 +22,7 @@ import "tinymce/plugins/media";
import "./plugins/code";
import "./plugins/shortlink";
import "./plugins/quickimage";
+import "./plugins/checklist";
import "./editor.css";
import { Editor } from "@tinymce/tinymce-react";
import { isUserPremium } from "../../common";
@@ -145,7 +146,7 @@ function TinyMCE(props) {
link_quicklink: true,
width: "100%",
plugins:
- "paste importcss searchreplace autolink directionality code quickimage shortlink media table hr advlist lists imagetools textpattern noneditable quickbars autoresize",
+ "checklist paste importcss searchreplace autolink directionality code quickimage shortlink media table hr advlist lists imagetools textpattern noneditable quickbars autoresize",
toolbar_mode: isTablet() ? "scrolling" : "sliding",
contextmenu: false,
quickbars_insert_toolbar: false,
@@ -188,7 +189,7 @@ function TinyMCE(props) {
`,
toolbar: simple
? false
- : `bold italic underline strikethrough blockquote code | fontsizeselect formatselect | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | forecolor backcolor removeformat | hr | image media link table | ltr rtl | searchreplace`,
+ : `bold italic underline strikethrough blockquote code | fontsizeselect formatselect | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist checklist | forecolor backcolor removeformat | hr | image media link table | ltr rtl | searchreplace`,
quickbars_selection_toolbar:
"bold italic underline strikethrough code h2 h3 quicklink blockquote",
mobile: {