feat: update splitListItem to allow setting attrs (#4253)

This commit is contained in:
Nantris 2024-08-11 16:38:01 -04:00 committed by GitHub
parent 08b4319215
commit 222f2aca62
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 17 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": minor
---
Add the ability to add new attributes to a splitted list item

View File

@ -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<string, any>) => 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)