Skip to content

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

feat: add handy shortcuts for editor

thecodrr6302bc6d798f
diff --git a/apps/web/src/components/editor/plugins/code/plugin.js b/apps/web/src/components/editor/plugins/code/plugin.js
index 1e1ed083e..93f80adbf 100644
--- a/apps/web/src/components/editor/plugins/code/plugin.js
+++ b/apps/web/src/components/editor/plugins/code/plugin.js
@@ -1,7 +1,7 @@
import tinymce from "tinymce/tinymce";
import hljs from "highlight.js/lib/common";
import "highlight.js/styles/atom-one-light.css";
-import rangy from "rangy/lib/rangy-textrange";
+import { getCharacterRange, moveCaretTo, persistSelection } from "../utils";
const TAB = " ";
const LANGUAGE_SELECT_LABEL_SELECTOR =
@@ -84,16 +84,15 @@ languagesToItems();
var content = editor.selection.getContent({ format: "text" });
content = content.replace(/^\n/gm, "") || " ";
- const rng = editor.selection.getRng();
editor.undoManager.transact(function () {
editor.execCommand(
"mceInsertContent",
false,
- `<code spellcheck="false">${content}</pre>`
+ `<code spellcheck="false">${content}</code>&nbsp;`
);
});
- editor.selection.setRng(rng);
- editor.nodeChanged();
+
+ editor.nodeChanged({ selectionChange: true });
};
var register = function (editor) {
@@ -370,35 +369,10 @@ function applyHighlighting(editor, language, refresh = false, auto = false) {
if (auto) changeSelectedLanguage(editor, language, true);
}
-function getCharacterRange(node) {
- if (!node) return;
- const ranges = rangy.getSelection().saveCharacterRanges(node);
- if (!ranges || !ranges.length) return;
- const { characterRange } = ranges[0];
- return characterRange;
-}
-
function insertAt(str, token, index) {
return str.substring(0, index) + token + str.substring(index);
}
-function moveCaretTo(node, index, endIndex) {
- const newCharacterRange = {
- characterRange: {
- start: index,
- end: endIndex || index,
- },
- };
-
- rangy.getSelection().restoreCharacterRanges(node, [newCharacterRange]);
-}
-
-function persistSelection(node, action) {
- let saved = rangy.getSelection().saveCharacterRanges(node);
- action();
- rangy.getSelection().restoreCharacterRanges(node, saved);
-}
-
function getLanguageFromClassName(className) {
const classes = className.split(" ");
const languageKey = classes.find((c) => c.startsWith("lang"));
diff --git a/apps/web/src/components/editor/plugins/shortcuts/index.js b/apps/web/src/components/editor/plugins/shortcuts/index.js
new file mode 100644
index 000000000..bfc1fc786
--- /dev/null
+++ b/apps/web/src/components/editor/plugins/shortcuts/index.js
@@ -0,0 +1 @@
+import "./plugins";
diff --git a/apps/web/src/components/editor/plugins/shortcuts/plugins.js b/apps/web/src/components/editor/plugins/shortcuts/plugins.js
new file mode 100644
index 000000000..b36a98694
--- /dev/null
+++ b/apps/web/src/components/editor/plugins/shortcuts/plugins.js
@@ -0,0 +1,40 @@
+import tinymce from "tinymce/tinymce";
+import { getCharacterRange, moveCaretTo } from "../utils";
+
+const WRAP_CHARS = {
+ '"': { start: '"', end: '"' },
+ "[": { start: "[", end: "]" },
+ "(": { start: "(", end: ")" },
+ "{": { start: "{", end: "}" },
+ "'": { start: "'", end: "'" },
+ "|": { start: "|", end: "|" },
+};
+
+(function () {
+ tinymce.PluginManager.add("shortcuts", function (editor, url) {
+ editor.on("keydown", function (e) {
+ const content = editor.selection.getContent();
+ const node = editor.selection.getNode();
+ if (node?.nodeName === "PRE") return;
+
+ if (content) {
+ if (e.key === "`") {
+ e.preventDefault();
+ editor.execCommand("mceCode");
+ } else if (!!WRAP_CHARS[e.key]) {
+ e.preventDefault();
+ const char = WRAP_CHARS[e.key];
+ editor.selection.setContent(`${char.start}${content}${char.end} `);
+ }
+ } else if (!!WRAP_CHARS[e.key]) {
+ e.preventDefault();
+ const char = WRAP_CHARS[e.key];
+
+ editor.selection.setContent(`${char.start}${char.end}`);
+
+ const characterRange = getCharacterRange(node);
+ moveCaretTo(node, characterRange.start - 1, characterRange.end - 1);
+ }
+ });
+ });
+})();
diff --git a/apps/web/src/components/editor/plugins/utils.js b/apps/web/src/components/editor/plugins/utils.js
new file mode 100644
index 000000000..22c3a0e94
--- /dev/null
+++ b/apps/web/src/components/editor/plugins/utils.js
@@ -0,0 +1,26 @@
+import rangy from "rangy/lib/rangy-textrange";
+
+export function getCharacterRange(node) {
+ if (!node) return;
+ const ranges = rangy.getSelection().saveCharacterRanges(node);
+ if (!ranges || !ranges.length) return;
+ const { characterRange } = ranges[0];
+ return characterRange;
+}
+
+export function moveCaretTo(node, index, endIndex) {
+ const newCharacterRange = {
+ characterRange: {
+ start: index,
+ end: endIndex || index,
+ },
+ };
+
+ rangy.getSelection().restoreCharacterRanges(node, [newCharacterRange]);
+}
+
+export function persistSelection(node, action) {
+ let saved = rangy.getSelection().saveCharacterRanges(node);
+ action();
+ rangy.getSelection().restoreCharacterRanges(node, saved);
+}
diff --git a/apps/web/src/components/editor/tinymce.js b/apps/web/src/components/editor/tinymce.js
index 51454264d..3b190f87b 100644
--- a/apps/web/src/components/editor/tinymce.js
+++ b/apps/web/src/components/editor/tinymce.js
@@ -24,6 +24,7 @@ import "./plugins/quickimage";
import "./plugins/checklist";
import "./plugins/collapsibleheaders";
import "./plugins/paste";
+import "./plugins/shortcuts";
import "./editor.css";
import { Editor } from "@tinymce/tinymce-react";
import { showBuyDialog } from "../../common/dialog-controller";
@@ -101,7 +102,7 @@ function useSkin() {
}
const plugins =
- "checklist paste importcss searchreplace autolink directionality code quickimage shortlink media table hr advlist lists imagetools noneditable quickbars autoresize collapsibleheaders";
+ "shortcuts checklist paste importcss searchreplace autolink directionality code quickimage shortlink media table hr advlist lists imagetools noneditable quickbars autoresize collapsibleheaders";
function TinyMCE(props) {
const {
changeInterval,