mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-24 03:39:01 +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.
|
||||
* @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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user