Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update splitListItem.ts to support overrideAttrs #4253

Merged
merged 4 commits into from
Aug 11, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions packages/core/src/commands/splitListItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ declare module '@tiptap/core' {
* @param typeOrName The type or name of the node.
* @example editor.commands.splitListItem('listItem')
*/
splitListItem: (typeOrName: string | NodeType) => ReturnType
splitListItem: (typeOrName: string | NodeType, overrideAttrs?: {[key: string]: any}) => ReturnType
}
}
}

export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({
export const splitListItem: RawCommands['splitListItem'] = (typeOrName, overrideAttrs = {}) => ({
Nantris marked this conversation as resolved.
Show resolved Hide resolved
tr, state, dispatch, editor,
}) => {
const type = getNodeType(typeOrName, state.schema)
Expand Down Expand Up @@ -70,11 +70,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 +110,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