diff --git a/.changeset/real-buckets-develop.md b/.changeset/real-buckets-develop.md new file mode 100644 index 00000000000..9e8031f20ef --- /dev/null +++ b/.changeset/real-buckets-develop.md @@ -0,0 +1,5 @@ +--- +"@tiptap/core": minor +--- + +Add the ability to add new attributes to a splitted list item diff --git a/packages/core/src/commands/splitListItem.ts b/packages/core/src/commands/splitListItem.ts index d7323f483a0..1a099d2ea99 100644 --- a/packages/core/src/commands/splitListItem.ts +++ b/packages/core/src/commands/splitListItem.ts @@ -14,14 +14,15 @@ declare module '@tiptap/core' { /** * Splits one list item into two list items. * @param typeOrName The type or name of the node. + * @param overrideAttrs The attributes to ensure on the new node. * @example editor.commands.splitListItem('listItem') */ - splitListItem: (typeOrName: string | NodeType) => ReturnType + splitListItem: (typeOrName: string | NodeType, overrideAttrs?: Record) => ReturnType } } } -export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({ +export const splitListItem: RawCommands['splitListItem'] = (typeOrName, overrideAttrs = {}) => ({ tr, state, dispatch, editor, }) => { const type = getNodeType(typeOrName, state.schema) @@ -70,11 +71,14 @@ export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({ const depthAfter = $from.indexAfter(-1) < $from.node(-2).childCount ? 1 : $from.indexAfter(-2) < $from.node(-3).childCount ? 2 : 3 // Add a second list item with an empty default start node - const newNextTypeAttributes = getSplittedAttributes( - extensionAttributes, - $from.node().type.name, - $from.node().attrs, - ) + const newNextTypeAttributes = { + ...getSplittedAttributes( + extensionAttributes, + $from.node().type.name, + $from.node().attrs, + ), + ...overrideAttrs, + } const nextType = type.contentMatch.defaultType?.createAndFill(newNextTypeAttributes) || undefined wrap = wrap.append(Fragment.from(type.createAndFill(null, nextType) || undefined)) @@ -107,16 +111,22 @@ export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({ const nextType = $to.pos === $from.end() ? grandParent.contentMatchAt(0).defaultType : null - const newTypeAttributes = getSplittedAttributes( - extensionAttributes, - grandParent.type.name, - grandParent.attrs, - ) - const newNextTypeAttributes = getSplittedAttributes( - extensionAttributes, - $from.node().type.name, - $from.node().attrs, - ) + const newTypeAttributes = { + ...getSplittedAttributes( + extensionAttributes, + grandParent.type.name, + grandParent.attrs, + ), + ...overrideAttrs, + } + const newNextTypeAttributes = { + ...getSplittedAttributes( + extensionAttributes, + $from.node().type.name, + $from.node().attrs, + ), + ...overrideAttrs, + } tr.delete($from.pos, $to.pos)