Skip to content

Commit 222f2ac

Browse files
authoredAug 11, 2024··
feat: update splitListItem to allow setting attrs (#4253)
1 parent 08b4319 commit 222f2ac

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed
 

‎.changeset/real-buckets-develop.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tiptap/core": minor
3+
---
4+
5+
Add the ability to add new attributes to a splitted list item

‎packages/core/src/commands/splitListItem.ts

+27-17
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ declare module '@tiptap/core' {
1414
/**
1515
* Splits one list item into two list items.
1616
* @param typeOrName The type or name of the node.
17+
* @param overrideAttrs The attributes to ensure on the new node.
1718
* @example editor.commands.splitListItem('listItem')
1819
*/
19-
splitListItem: (typeOrName: string | NodeType) => ReturnType
20+
splitListItem: (typeOrName: string | NodeType, overrideAttrs?: Record<string, any>) => ReturnType
2021
}
2122
}
2223
}
2324

24-
export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({
25+
export const splitListItem: RawCommands['splitListItem'] = (typeOrName, overrideAttrs = {}) => ({
2526
tr, state, dispatch, editor,
2627
}) => {
2728
const type = getNodeType(typeOrName, state.schema)
@@ -70,11 +71,14 @@ export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({
7071
const depthAfter = $from.indexAfter(-1) < $from.node(-2).childCount ? 1 : $from.indexAfter(-2) < $from.node(-3).childCount ? 2 : 3
7172

7273
// Add a second list item with an empty default start node
73-
const newNextTypeAttributes = getSplittedAttributes(
74-
extensionAttributes,
75-
$from.node().type.name,
76-
$from.node().attrs,
77-
)
74+
const newNextTypeAttributes = {
75+
...getSplittedAttributes(
76+
extensionAttributes,
77+
$from.node().type.name,
78+
$from.node().attrs,
79+
),
80+
...overrideAttrs,
81+
}
7882
const nextType = type.contentMatch.defaultType?.createAndFill(newNextTypeAttributes) || undefined
7983

8084
wrap = wrap.append(Fragment.from(type.createAndFill(null, nextType) || undefined))
@@ -107,16 +111,22 @@ export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({
107111

108112
const nextType = $to.pos === $from.end() ? grandParent.contentMatchAt(0).defaultType : null
109113

110-
const newTypeAttributes = getSplittedAttributes(
111-
extensionAttributes,
112-
grandParent.type.name,
113-
grandParent.attrs,
114-
)
115-
const newNextTypeAttributes = getSplittedAttributes(
116-
extensionAttributes,
117-
$from.node().type.name,
118-
$from.node().attrs,
119-
)
114+
const newTypeAttributes = {
115+
...getSplittedAttributes(
116+
extensionAttributes,
117+
grandParent.type.name,
118+
grandParent.attrs,
119+
),
120+
...overrideAttrs,
121+
}
122+
const newNextTypeAttributes = {
123+
...getSplittedAttributes(
124+
extensionAttributes,
125+
$from.node().type.name,
126+
$from.node().attrs,
127+
),
128+
...overrideAttrs,
129+
}
120130

121131
tr.delete($from.pos, $to.pos)
122132

0 commit comments

Comments
 (0)
Please sign in to comment.