Skip to content

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

feat: extract collapsible header code to a plugin

thecodrr604a4fba9c73
diff --git a/apps/web/src/components/editor/editor.css b/apps/web/src/components/editor/editor.css
index 46e3c2991..fb4491076 100644
--- a/apps/web/src/components/editor/editor.css
+++ b/apps/web/src/components/editor/editor.css
@@ -110,11 +110,6 @@ tox-tbtn tox-tbtn--select {
content: "H5";
}
-.mce-content-body blockquote::before {
- content: "QUOTE";
- margin-left: 0px !important;
-}
-
.mce-content-body .c::before {
color: var(--static);
background-color: var(--primary);
diff --git a/apps/web/src/components/editor/plugins/collapsibleheaders/index.js b/apps/web/src/components/editor/plugins/collapsibleheaders/index.js
new file mode 100644
index 000000000..dfd8f6603
--- /dev/null
+++ b/apps/web/src/components/editor/plugins/collapsibleheaders/index.js
@@ -0,0 +1 @@
+import "./plugin";
diff --git a/apps/web/src/components/editor/plugins/collapsibleheaders/plugin.js b/apps/web/src/components/editor/plugins/collapsibleheaders/plugin.js
new file mode 100644
index 000000000..df15e7a2d
--- /dev/null
+++ b/apps/web/src/components/editor/plugins/collapsibleheaders/plugin.js
@@ -0,0 +1,76 @@
+import tinymce from "tinymce/tinymce";
+
+const COLLAPSED_KEY = "c";
+const HIDDEN_KEY = "h";
+const collapsibleTags = { HR: 1, H2: 2, H3: 3, H4: 4, H5: 5 };
+
+function toggleElementVisibility(element, toggleState) {
+ if (!toggleState) element.classList.remove(HIDDEN_KEY);
+ else element.classList.add(HIDDEN_KEY);
+}
+
+function collapseElement(target) {
+ let sibling = target.nextSibling;
+ const isTargetCollapsed = target.classList.contains(COLLAPSED_KEY);
+ let skip = false;
+
+ while (
+ sibling &&
+ (!collapsibleTags[sibling.tagName] ||
+ collapsibleTags[sibling.tagName] > collapsibleTags[target.tagName])
+ ) {
+ const isCollapsed = sibling.classList.contains(COLLAPSED_KEY);
+ if (!isTargetCollapsed) {
+ if (isCollapsed) {
+ skip = true;
+ toggleElementVisibility(sibling, isTargetCollapsed);
+ } else if (skip && collapsibleTags[sibling.tagName]) {
+ skip = false;
+ }
+ }
+ if (!skip) {
+ toggleElementVisibility(sibling, isTargetCollapsed);
+ }
+ if (!sibling.nextSibling) break;
+ sibling = sibling.nextSibling;
+ }
+}
+
+(function () {
+ tinymce.PluginManager.add("collapsibleheaders", function (editor, url) {
+ editor.on(
+ "mousedown touchstart",
+ function (e) {
+ const { target } = e;
+ if (
+ e.offsetX < 0 &&
+ collapsibleTags[target.tagName] &&
+ target.parentElement.tagName === "DIV"
+ ) {
+ e.preventDefault();
+ e.stopImmediatePropagation();
+ e.stopPropagation();
+ editor.undoManager.transact(() => {
+ if (target.classList.contains(COLLAPSED_KEY)) {
+ target.classList.remove(COLLAPSED_KEY);
+ } else {
+ target.classList.add(COLLAPSED_KEY);
+ }
+ collapseElement(target);
+ // onSave();
+ });
+ }
+ },
+ { capture: true, passive: false }
+ );
+
+ editor.on("NewBlock", function (event) {
+ const { newBlock } = event;
+ const target = newBlock?.previousElementSibling;
+ if (target?.classList.contains(COLLAPSED_KEY)) {
+ target.classList.remove(COLLAPSED_KEY);
+ collapseElement(target);
+ }
+ });
+ });
+})();
diff --git a/apps/web/src/components/editor/tinymce.js b/apps/web/src/components/editor/tinymce.js
index e142c7751..0dc9e6775 100644
--- a/apps/web/src/components/editor/tinymce.js
+++ b/apps/web/src/components/editor/tinymce.js
@@ -23,14 +23,15 @@ import "./plugins/code";
import "./plugins/shortlink";
import "./plugins/quickimage";
import "./plugins/checklist";
+import "./plugins/collapsibleheaders";
import "./editor.css";
import { Editor } from "@tinymce/tinymce-react";
-import { isUserPremium } from "../../common";
import { showBuyDialog } from "../../common/dialog-controller";
import { useStore as useThemeStore } from "../../stores/theme-store";
import { isTablet } from "../../utils/dimensions";
import { KeyboardEventManager } from "../../utils/keyboard";
import { showToast } from "../../utils/toast";
+import { useIsUserPremium } from "../../hooks/use-is-user-premium";
const markdownPatterns = [
{ start: "```", replacement: "<pre></pre>" },
@@ -99,42 +100,8 @@ function useSkin() {
: [host + "/skins/notesnook-dark", host + "/skins/notesnook"];
}
-const COLLAPSED_KEY = "c";
-const HIDDEN_KEY = "h";
-const collapsibleTags = { HR: 1, H2: 2, H3: 3, H4: 4, H5: 5 };
-
-function toggleElementVisibility(element, toggleState) {
- if (!toggleState) element.classList.remove(HIDDEN_KEY);
- else element.classList.add(HIDDEN_KEY);
-}
-
-function collapseElement(target) {
- let sibling = target.nextSibling;
- const isTargetCollapsed = target.classList.contains(COLLAPSED_KEY);
- let skip = false;
-
- while (
- sibling &&
- (!collapsibleTags[sibling.tagName] ||
- collapsibleTags[sibling.tagName] > collapsibleTags[target.tagName])
- ) {
- const isCollapsed = sibling.classList.contains(COLLAPSED_KEY);
- if (!isTargetCollapsed) {
- if (isCollapsed) {
- skip = true;
- toggleElementVisibility(sibling, isTargetCollapsed);
- } else if (skip && collapsibleTags[sibling.tagName]) {
- skip = false;
- }
- }
- if (!skip) {
- toggleElementVisibility(sibling, isTargetCollapsed);
- }
- if (!sibling.nextSibling) break;
- sibling = sibling.nextSibling;
- }
-}
-
+const plugins =
+ "checklist paste importcss searchreplace autolink directionality code quickimage shortlink media table hr advlist lists imagetools noneditable quickbars autoresize collapsibleheaders";
function TinyMCE(props) {
const {
changeInterval,
@@ -149,6 +116,7 @@ function TinyMCE(props) {
onInit,
} = props;
const [oldSkin, newSkin] = useSkin();
+ const isUserPremium = useIsUserPremium();
const tinymceRef = editorRef;
useEffect(() => {
tinymceRef.current.editor.dom.styleSheetLoader.unload(
@@ -176,9 +144,7 @@ function TinyMCE(props) {
statusbar: false,
link_quicklink: true,
width: "100%",
- plugins: isUserPremium()
- ? "checklist 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 noneditable quickbars autoresize",
+ plugins: isUserPremium ? `${plugins} textpattern` : plugins,
toolbar_mode: isTablet() ? "scrolling" : "sliding",
contextmenu: false,
quickbars_insert_toolbar: false,
@@ -234,14 +200,7 @@ function TinyMCE(props) {
block: "nearest",
});
});
- editor.on("NewBlock", (e) => {
- const { newBlock } = e;
- const target = newBlock?.previousElementSibling;
- if (target?.classList.contains(COLLAPSED_KEY)) {
- target.classList.remove(COLLAPSED_KEY);
- collapseElement(target);
- }
- });
+ editor.on("NewBlock", (e) => {});
},
toolbar_persist: true,
toolbar_sticky: false,
@@ -250,30 +209,6 @@ function TinyMCE(props) {
fixed_toolbar_container: "#editorToolbar",
autoresize_bottom_margin: 100,
}}
- // onNodeChange={(e) => {
-
- // }}
- onClick={(e, editor) => {
- const { target } = e;
- if (
- e.offsetX < 0 &&
- collapsibleTags[target.tagName] &&
- target.parentElement.tagName === "DIV"
- ) {
- e.preventDefault();
- e.stopImmediatePropagation();
- e.stopPropagation();
- editor.undoManager.transact(() => {
- if (target.classList.contains(COLLAPSED_KEY)) {
- target.classList.remove(COLLAPSED_KEY);
- } else {
- target.classList.add(COLLAPSED_KEY);
- }
- collapseElement(target);
- onSave();
- });
- }
- }}
onBeforeExecCommand={async (command) => {
if (
premiumCommands.some((cmd) => {
@@ -290,7 +225,7 @@ function TinyMCE(props) {
}
return isPremium;
}) &&
- !isUserPremium()
+ !isUserPremium
) {
command.preventDefault();
command.stopImmediatePropagation();
diff --git a/apps/web/src/hooks/use-is-user-premium.js b/apps/web/src/hooks/use-is-user-premium.js
new file mode 100644
index 000000000..4723dc6ee
--- /dev/null
+++ b/apps/web/src/hooks/use-is-user-premium.js
@@ -0,0 +1,22 @@
+import { SUBSCRIPTION_STATUS } from "../common";
+import {
+ useStore as useUserStore,
+ store as userstore,
+} from "../stores/user-store";
+
+export function useIsUserPremium() {
+ const user = useUserStore((store) => store.user);
+ return isUserPremium(user);
+}
+
+export function isUserPremium(user) {
+ if (process.env.REACT_APP_CI) return true;
+ if (!user) user = userstore.get().user;
+
+ const subStatus = user?.subscription?.type;
+ return (
+ subStatus === SUBSCRIPTION_STATUS.BETA ||
+ subStatus === SUBSCRIPTION_STATUS.PREMIUM ||
+ subStatus === SUBSCRIPTION_STATUS.TRIAL
+ );
+}