Skip to content

Commit

Permalink
feat: update splitListItem to allow setting attrs (#4253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nantris authored Aug 11, 2024
1 parent 08b4319 commit 222f2ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-buckets-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/core": minor
---

Add the ability to add new attributes to a splitted list item
44 changes: 27 additions & 17 deletions packages/core/src/commands/splitListItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 222f2ac

Please sign in to comment.