Skip to content

Commit d000839

Browse files
committed
fix(block-container): split error
1 parent 38d4191 commit d000839

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

packages/tiptap-extension-block-container/src/blockContainer.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface BlockContainerOptions {
1111
declare module '@tiptap/core' {
1212
interface Commands<ReturnType> {
1313
blockContainer: {
14-
splitBlockContainer: (pos: number) => ReturnType
14+
splitBlockContainer: () => ReturnType
1515
}
1616
}
1717
}
@@ -42,26 +42,30 @@ export const blockContainer = Node.create({
4242
},
4343
addCommands() {
4444
return {
45-
splitBlockContainer: (pos: number) => ({ state: { schema, tr }, dispatch }) => {
46-
const resolved = tr.doc.resolve(pos)
45+
splitBlockContainer: () => ({ state: { selection, doc, schema, tr }, dispatch }) => {
46+
if (!dispatch)
47+
return false
48+
49+
const startPos = selection.from
50+
const resolved = doc.resolve(startPos)
4751
const endPos = resolved.end(resolved.depth)
48-
const newBlockContent = tr.doc.cut(pos, endPos)
52+
const newBlockContent = tr.doc.cut(startPos, endPos)
4953
const newBlock = schema.nodes[this.name].createAndFill()!
5054

51-
if (dispatch) {
52-
tr.insert(endPos + 1, newBlock)
53-
tr.replace(endPos + 1, endPos + 2, newBlockContent.content.size > 0 ? new Slice(Fragment.from(newBlockContent), 0, 0) : undefined)
54-
tr.setSelection(new TextSelection(tr.doc.resolve(endPos + 1)))
55-
tr.delete(pos, endPos)
56-
}
55+
tr.replace(endPos + 1, endPos + 2, newBlockContent.content.size > 0 ? new Slice(Fragment.from(newBlockContent), 0, 0) : new Slice(Fragment.from(newBlock), 0, 0))
56+
tr.setSelection(new TextSelection(tr.doc.resolve(endPos + 1)))
57+
tr.delete(startPos, endPos)
5758

58-
return true
59+
return dispatch(tr)
5960
},
6061
}
6162
},
6263
addKeyboardShortcuts() {
63-
const handleEnter = () => this.editor.commands.first(({ commands, state }) => {
64-
return [() => commands.newlineInCode(), () => commands.splitBlockContainer(state.selection.from)]
64+
const handleEnter = () => this.editor.commands.first(({ commands }) => {
65+
return [
66+
() => commands.newlineInCode(),
67+
() => commands.splitBlockContainer(),
68+
]
6569
})
6670

6771
return {

0 commit comments

Comments
 (0)