From 1ce0d3470e6ddccbb181d13257cc003061b509eb Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 16 Oct 2023 19:13:23 -0400 Subject: [PATCH] Make code block `Enter` behavior the same as quote block (#6010) --- shared/editor/commands/codeFence.ts | 3 +-- shared/editor/nodes/CodeFence.ts | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/shared/editor/commands/codeFence.ts b/shared/editor/commands/codeFence.ts index e1f58eef3..be5294d57 100644 --- a/shared/editor/commands/codeFence.ts +++ b/shared/editor/commands/codeFence.ts @@ -86,8 +86,7 @@ export const newlineInCode: Command = (state, dispatch) => { return false; } const { tr, selection } = state; - const text = selection?.$anchor?.nodeBefore?.text; - + const text = selection.$anchor.nodeBefore?.text; let newText = "\n"; if (text) { diff --git a/shared/editor/nodes/CodeFence.ts b/shared/editor/nodes/CodeFence.ts index 5c11babd9..409cf25c3 100644 --- a/shared/editor/nodes/CodeFence.ts +++ b/shared/editor/nodes/CodeFence.ts @@ -1,5 +1,6 @@ import copy from "copy-to-clipboard"; import Token from "markdown-it/lib/token"; +import { exitCode } from "prosemirror-commands"; import { textblockTypeInputRule } from "prosemirror-inputrules"; import { NodeSpec, @@ -207,10 +208,26 @@ export default class CodeFence extends Node { } keys({ type, schema }: { type: NodeType; schema: Schema }) { - const output = { + const output: Record = { "Shift-Ctrl-\\": toggleBlockType(type, schema.nodes.paragraph), Tab: insertSpaceTab, - Enter: newlineInCode, + Enter: (state, dispatch) => { + if (!isInCode(state)) { + return false; + } + const { selection } = state; + const text = selection.$anchor.nodeBefore?.text; + const selectionAtEnd = + selection.$anchor.parentOffset === + selection.$anchor.parent.nodeSize - 2; + + if (selectionAtEnd && text?.endsWith("\n")) { + exitCode(state, dispatch); + return true; + } + + return newlineInCode(state, dispatch); + }, "Shift-Enter": newlineInCode, };