Skip to content

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

fix: janky cursor on enter in codeblock

thecodrr7fe4a8e2ece7
diff --git a/packages/editor/dist/es/extensions/codeblock/codeblock.js b/packages/editor/dist/es/extensions/codeblock/codeblock.js
index 4a1ceb3e8..ae363d69a 100644
--- a/packages/editor/dist/es/extensions/codeblock/codeblock.js
+++ b/packages/editor/dist/es/extensions/codeblock/codeblock.js
@@ -166,8 +166,8 @@ export const CodeBlock = Node.create({
if (!codeblock)
return false;
return editor.commands.setTextSelection({
- from: codeblock.pos,
- to: codeblock.pos + codeblock.node.nodeSize,
+ from: codeblock.pos + 1,
+ to: codeblock.pos + codeblock.node.nodeSize - 1,
});
},
// remove code block when at start of document or code block is empty
diff --git a/packages/editor/dist/es/extensions/codeblock/highlighter.js b/packages/editor/dist/es/extensions/codeblock/highlighter.js
index d6d5b3ff9..59de5f0bb 100644
--- a/packages/editor/dist/es/extensions/codeblock/highlighter.js
+++ b/packages/editor/dist/es/extensions/codeblock/highlighter.js
@@ -39,28 +39,7 @@ function getLineDecoration(from, line, total, isActive) {
total,
from,
};
- // Prosemirror has a selection issue with the widget decoration
- // on the first line. To work around that we use inline decoration
- // for the first line.
- if (line === 1 ||
- // Android Composition API (aka the virtual keyboard) doesn't behave well
- // with Decoration widgets so we have to resort to inline line numbers.
- isAndroid()) {
- return Decoration.inline(from, from + 1, attributes, spec);
- }
- return Decoration.widget(from, () => {
- const element = document.createElement("span");
- element.classList.add("line-number-widget");
- if (isActive)
- element.classList.add("active");
- element.innerHTML = attributes["data-line"];
- return element;
- }, Object.assign(Object.assign({}, spec), {
- // should rerender when any of these change:
- // 1. line number
- // 2. line active state
- // 3. the max length of all lines
- key: `${line}-${isActive ? "active" : ""}-${maxLength}` }));
+ return Decoration.inline(from, from + 1, attributes, spec);
}
function getDecorations({ doc, name, defaultLanguage, caretPosition, }) {
const decorations = [];
@@ -99,8 +78,12 @@ export function HighlighterPlugin({ name, defaultLanguage, }) {
return new Plugin({
key,
state: {
- init: () => {
- return DecorationSet.empty;
+ init: (config, state) => {
+ return getDecorations({
+ doc: state.doc,
+ name,
+ defaultLanguage,
+ });
},
apply: (transaction, decorationSet, oldState, newState) => {
const oldNodeName = oldState.selection.$head.parent.type.name;
@@ -154,9 +137,10 @@ export function HighlighterPlugin({ name, defaultLanguage, }) {
prevState.selection.$from.parent.type.name === name) &&
prevState.selection.$from.pos !== nextState.selection.$from.pos;
findChildren(nextState.doc, (node) => node.type.name === name).forEach((block) => {
+ var _a;
const { node, pos } = block;
const attributes = Object.assign({}, node.attrs);
- if (docChanged) {
+ if (docChanged || !((_a = attributes.lines) === null || _a === void 0 ? void 0 : _a.length)) {
const lines = toCodeLines(node.textContent, pos);
attributes.lines = lines.slice();
}
diff --git a/packages/editor/src/extensions/code-block/code-block.ts b/packages/editor/src/extensions/code-block/code-block.ts
index e9b72cf69..04b5a2ff4 100644
--- a/packages/editor/src/extensions/code-block/code-block.ts
+++ b/packages/editor/src/extensions/code-block/code-block.ts
@@ -262,10 +262,11 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
$anchor,
(node) => node.type.name === this.type.name
);
+
if (!codeblock) return false;
return editor.commands.setTextSelection({
- from: codeblock.pos,
- to: codeblock.pos + codeblock.node.nodeSize,
+ from: codeblock.pos + 1,
+ to: codeblock.pos + codeblock.node.nodeSize - 1,
});
},
// remove code block when at start of document or code block is empty
diff --git a/packages/editor/src/extensions/code-block/highlighter.ts b/packages/editor/src/extensions/code-block/highlighter.ts
index 734659399..37c7cfccf 100644
--- a/packages/editor/src/extensions/code-block/highlighter.ts
+++ b/packages/editor/src/extensions/code-block/highlighter.ts
@@ -72,36 +72,7 @@ function getLineDecoration(
from,
};
- // Prosemirror has a selection issue with the widget decoration
- // on the first line. To work around that we use inline decoration
- // for the first line.
- if (
- line === 1 ||
- // Android Composition API (aka the virtual keyboard) doesn't behave well
- // with Decoration widgets so we have to resort to inline line numbers.
- isAndroid()
- ) {
- return Decoration.inline(from, from + 1, attributes, spec);
- }
-
- return Decoration.widget(
- from,
- () => {
- const element = document.createElement("span");
- element.classList.add("line-number-widget");
- if (isActive) element.classList.add("active");
- element.innerHTML = attributes["data-line"];
- return element;
- },
- {
- ...spec,
- // should rerender when any of these change:
- // 1. line number
- // 2. line active state
- // 3. the max length of all lines
- key: `${line}-${isActive ? "active" : ""}-${maxLength}`,
- }
- );
+ return Decoration.inline(from, from + 1, attributes, spec);
}
function getDecorations({
@@ -167,8 +138,12 @@ export function HighlighterPlugin({
key,
state: {
- init: () => {
- return DecorationSet.empty;
+ init: (config, state) => {
+ return getDecorations({
+ doc: state.doc,
+ name,
+ defaultLanguage,
+ });
},
apply: (
transaction,
@@ -258,7 +233,7 @@ export function HighlighterPlugin({
const { node, pos } = block;
const attributes = { ...node.attrs };
- if (docChanged) {
+ if (docChanged || !attributes.lines?.length) {
const lines = toCodeLines(node.textContent, pos);
attributes.lines = lines.slice();
}