Skip to content

Commit

Permalink
fix: improve setNode behavior for list items, fix #2261
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Kühn authored and Philipp Kühn committed Dec 13, 2021
1 parent e09fd93 commit f2ced69
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/core/src/commands/setNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,26 @@ declare module '@tiptap/core' {
}
}

export const setNode: RawCommands['setNode'] = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
export const setNode: RawCommands['setNode'] = (typeOrName, attributes = {}) => ({ state, dispatch, chain }) => {
const type = getNodeType(typeOrName, state.schema)

return setBlockType(type, attributes)(state, dispatch)
// TODO: use a fallback like insertContent?
if (!type.isTextblock) {
console.warn('[tiptap warn]: Currently "setNode()" only supports text block nodes.')

return false
}

const canSetBlock = setBlockType(type, attributes)(state)

if (canSetBlock) {
return setBlockType(type, attributes)(state, dispatch)
}

return chain()
.clearNodes()
.command(({ state: updatedState }) => {
return setBlockType(type, attributes)(updatedState, dispatch)
})
.run()
}

0 comments on commit f2ced69

Please sign in to comment.