From 9c0831e2251a74dddf0c4fbd9f7291b04404e263 Mon Sep 17 00:00:00 2001 From: iskaktoltay Date: Tue, 19 Nov 2024 12:23:48 +0500 Subject: [PATCH] added creation of new block at the same nesting level when exiting code block with enter --- .../tiptap-extension-code-block/code-block.ts | 73 ++++++++++++++++--- 1 file changed, 63 insertions(+), 10 deletions(-) diff --git a/frontend/apps/desktop/src/editor/tiptap-extension-code-block/code-block.ts b/frontend/apps/desktop/src/editor/tiptap-extension-code-block/code-block.ts index 19a93f32..258eacfc 100644 --- a/frontend/apps/desktop/src/editor/tiptap-extension-code-block/code-block.ts +++ b/frontend/apps/desktop/src/editor/tiptap-extension-code-block/code-block.ts @@ -8,6 +8,7 @@ import { import {Fragment, Slice} from '@tiptap/pm/model' import {Plugin, PluginKey, TextSelection} from '@tiptap/pm/state' import {BlockNoteDOMAttributes, getBlockInfoFromPos, mergeCSSClasses} from '..' +import {getGroupInfoFromPos} from '../blocknote/core/extensions/Blocks/helpers/getGroupInfoFromPos' declare module '@tiptap/core' { interface Commands { @@ -374,7 +375,7 @@ export const CodeBlock = Node.create({ return true }, - // exit node on if at end of the block and at the new line or add a new line + // exit node on enter if at end of the block and at the new line or add a new line Enter: ({editor}) => { const {state, view} = editor const {selection} = state @@ -390,16 +391,68 @@ export const CodeBlock = Node.create({ const endsWithNewline = codeBlock.textContent.endsWith('\n') if (isAtEnd && endsWithNewline) { const nextBlockPos = codePos.end() + 2 - editor - .chain() - .focus(nextBlockPos) - .command(({tr}) => { - tr.delete($from.pos - 1, $from.pos) - return true - }) - .run() + const {group, container, $pos, depth} = getGroupInfoFromPos( + codePos.pos, + state, + ) + if ( + group.type.name === 'blockGroup' && + group.lastChild?.firstChild?.eq(codeBlock) + ) { + editor + .chain() + .command(({tr}) => { + if (group.child(group.childCount - 1).childCount > 1) { + const groupContent = group.content + const lastBlockContent = groupContent.lastChild!.lastChild! + const newBlockContent = [ + state.schema.nodes['paragraph'].createAndFill()!, + lastBlockContent, + ] + const newContainer = state.schema.nodes[ + 'blockContainer' + ].createAndFill(null, newBlockContent)! + const replaceContainer = state.schema.nodes[ + 'blockContainer' + ].createAndFill(container?.attrs, codeBlock)! + const newGroupContent = group.content + .replaceChild(group.childCount - 1, replaceContainer) + .addToEnd(newContainer) + const newGroup = state.schema.nodes[ + 'blockGroup' + ].createAndFill(group.attrs, newGroupContent)! + const groupPos = state.doc.resolve($pos.after(depth + 1)) + tr.replaceRangeWith( + groupPos.start(), + groupPos.end(), + newGroup, + ) + } else { + const newContainer = + state.schema.nodes['blockContainer'].createAndFill()! + tr.insert(nextBlockPos, newContainer) + } + return false + }) + .focus(nextBlockPos) + .command(({tr}) => { + tr.delete($from.pos - 1, $from.pos) + return true + }) + .run() + return true + } else { + editor + .chain() + .focus(nextBlockPos) + .command(({tr}) => { + tr.delete($from.pos - 1, $from.pos) + return true + }) + .run() - return true + return true + } } let tr = state.tr tr = tr.replaceSelectionWith(state.schema.text('\n'))