Skip to content

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

editor: refactor `toggleCodeBlock`

Abdullah Attafba350ed1e25
diff --git a/packages/editor/src/extensions/code-block/code-block.ts b/packages/editor/src/extensions/code-block/code-block.ts
index 66ad51eb7..be5b483f9 100644
--- a/packages/editor/src/extensions/code-block/code-block.ts
+++ b/packages/editor/src/extensions/code-block/code-block.ts
@@ -234,29 +234,20 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
},
toggleCodeBlock:
(attributes) =>
- ({ commands, state }) => {
- const { from, to } = state.selection;
-
+ ({ commands, state, tr }) => {
const isInsideCodeBlock = this.editor.isActive(this.type.name);
if (!isInsideCodeBlock) {
+ const { from, to } = state.selection;
const text = state.doc.textBetween(from, to, "\n");
-
- const newNode = this.type.create(
- { ...attributes, id: createCodeblockId() },
- state.schema.text(text)
+ tr.replaceSelectionWith(
+ this.type.create(
+ { ...attributes, id: createCodeblockId() },
+ state.schema.text(text)
+ )
);
-
- state.tr.replaceSelectionWith(newNode);
-
- commands.setTextSelection({
- from: from,
- to: to
- });
-
- return true;
- } else {
- return commands.clearNodes();
+ return commands.setTextSelection({ from, to: tr.mapping.map(to) });
}
+ return commands.clearNodes();
},
changeCodeBlockIndentation:
(options) =>