Skip to content

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

feat: improve editor quick keyboard shortcuts plugin

thecodrrfb24df460bb0
diff --git a/apps/web/src/components/editor/plugins/shortcuts/plugins.js b/apps/web/src/components/editor/plugins/shortcuts/plugins.js
index afa6cd5e2..4560744fa 100644
--- a/apps/web/src/components/editor/plugins/shortcuts/plugins.js
+++ b/apps/web/src/components/editor/plugins/shortcuts/plugins.js
@@ -1,5 +1,10 @@
import tinymce from "tinymce/tinymce";
-import { getCharacterRange, moveCaretTo } from "../utils";
+import {
+ getCharacterRange,
+ getNextCharacter,
+ getPreviousCharacter,
+ moveCaretTo,
+} from "../utils";
const WRAP_CHARS = {
'"': { start: '"', end: '"' },
@@ -26,14 +31,33 @@ const WRAP_CHARS = {
const char = WRAP_CHARS[e.key];
editor.selection.setContent(`${char.start}${content}${char.end}`);
}
+ } else if (e.code === "Backspace") {
+ const characterRange = getCharacterRange(node);
+ const nextChar = getNextCharacter(node, characterRange);
+ const prevChar = getPreviousCharacter(node, characterRange);
+ const isWrapChar = !!WRAP_CHARS[nextChar];
+ if (isWrapChar && nextChar === prevChar) {
+ e.preventDefault();
+ editor.execCommand("ForwardDelete");
+ editor.execCommand("Delete");
+ }
} 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);
+ const nextChar = getNextCharacter(node, characterRange);
+ const prevChar = getPreviousCharacter(node, characterRange);
+
+ if (nextChar === e.key) {
+ moveCaretTo(node, characterRange.start + 1, characterRange.end + 1);
+ } else if (!prevChar.trim()) {
+ editor.selection.setContent(`${char.start}${char.end}`);
+ characterRange.start += char.start.length + char.end.length;
+ characterRange.end += char.start.length + char.end.length;
+ moveCaretTo(node, characterRange.start - 1, characterRange.end - 1);
+ } else {
+ editor.selection.setContent(`${e.key}`);
+ }
}
});
});
diff --git a/apps/web/src/components/editor/plugins/utils.js b/apps/web/src/components/editor/plugins/utils.js
index 22c3a0e94..53e42eba2 100644
--- a/apps/web/src/components/editor/plugins/utils.js
+++ b/apps/web/src/components/editor/plugins/utils.js
@@ -8,6 +8,22 @@ export function getCharacterRange(node) {
return characterRange;
}
+export function getNextCharacter(node, range) {
+ if (!range) return "";
+ return (node.textContent || node.innerText).substring(
+ range.end,
+ range.end + 1
+ );
+}
+
+export function getPreviousCharacter(node, range) {
+ if (!range) return "";
+ return (node.textContent || node.innerText).substring(
+ range.start,
+ range.start - 1
+ );
+}
+
export function moveCaretTo(node, index, endIndex) {
const newCharacterRange = {
characterRange: {