mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-08 01:53:04 +08:00
feat: update splitListItem to allow setting attrs (#4253)
This commit is contained in:
parent
08b4319215
commit
222f2aca62
5
.changeset/real-buckets-develop.md
Normal file
5
.changeset/real-buckets-develop.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@tiptap/core": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Add the ability to add new attributes to a splitted list item
|
@ -14,14 +14,15 @@ declare module '@tiptap/core' {
|
|||||||
/**
|
/**
|
||||||
* Splits one list item into two list items.
|
* Splits one list item into two list items.
|
||||||
* @param typeOrName The type or name of the node.
|
* @param typeOrName The type or name of the node.
|
||||||
|
* @param overrideAttrs The attributes to ensure on the new node.
|
||||||
* @example editor.commands.splitListItem('listItem')
|
* @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,
|
tr, state, dispatch, editor,
|
||||||
}) => {
|
}) => {
|
||||||
const type = getNodeType(typeOrName, state.schema)
|
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
|
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
|
// Add a second list item with an empty default start node
|
||||||
const newNextTypeAttributes = getSplittedAttributes(
|
const newNextTypeAttributes = {
|
||||||
extensionAttributes,
|
...getSplittedAttributes(
|
||||||
$from.node().type.name,
|
extensionAttributes,
|
||||||
$from.node().attrs,
|
$from.node().type.name,
|
||||||
)
|
$from.node().attrs,
|
||||||
|
),
|
||||||
|
...overrideAttrs,
|
||||||
|
}
|
||||||
const nextType = type.contentMatch.defaultType?.createAndFill(newNextTypeAttributes) || undefined
|
const nextType = type.contentMatch.defaultType?.createAndFill(newNextTypeAttributes) || undefined
|
||||||
|
|
||||||
wrap = wrap.append(Fragment.from(type.createAndFill(null, nextType) || 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 nextType = $to.pos === $from.end() ? grandParent.contentMatchAt(0).defaultType : null
|
||||||
|
|
||||||
const newTypeAttributes = getSplittedAttributes(
|
const newTypeAttributes = {
|
||||||
extensionAttributes,
|
...getSplittedAttributes(
|
||||||
grandParent.type.name,
|
extensionAttributes,
|
||||||
grandParent.attrs,
|
grandParent.type.name,
|
||||||
)
|
grandParent.attrs,
|
||||||
const newNextTypeAttributes = getSplittedAttributes(
|
),
|
||||||
extensionAttributes,
|
...overrideAttrs,
|
||||||
$from.node().type.name,
|
}
|
||||||
$from.node().attrs,
|
const newNextTypeAttributes = {
|
||||||
)
|
...getSplittedAttributes(
|
||||||
|
extensionAttributes,
|
||||||
|
$from.node().type.name,
|
||||||
|
$from.node().attrs,
|
||||||
|
),
|
||||||
|
...overrideAttrs,
|
||||||
|
}
|
||||||
|
|
||||||
tr.delete($from.pos, $to.pos)
|
tr.delete($from.pos, $to.pos)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user