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

Adds attributes to toggleList #3776

Merged
merged 3 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions docs/api/commands/toggle-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ The type of node that should be used for the wrapping list

The type of node that should be used for the list items

`keepMarks?: boolean`

If marks should be kept as list items or not

`attributes?: Record<string, any>`

The attributes that should be applied to the list. **This is optional.**

## Usage
```js
// toggle a bullet list with list items
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/commands/toggleList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ declare module '@tiptap/core' {
/**
* Toggle between different list types.
*/
toggleList: (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType, keepMarks?: boolean) => ReturnType;
toggleList: (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType, keepMarks?: boolean, attributes?: Record<string, any>) => ReturnType;
}
}
}

export const toggleList: RawCommands['toggleList'] = (listTypeOrName, itemTypeOrName, keepMarks) => ({
export const toggleList: RawCommands['toggleList'] = (listTypeOrName, itemTypeOrName, keepMarks, attributes = {}) => ({
editor, tr, state, dispatch, chain, commands, can,
}) => {
const { extensions, splittableMarks } = editor.extensionManager
Expand Down Expand Up @@ -114,15 +114,15 @@ export const toggleList: RawCommands['toggleList'] = (listTypeOrName, itemTypeOr
return chain()
// try to convert node to default node if needed
.command(() => {
const canWrapInList = can().wrapInList(listType)
const canWrapInList = can().wrapInList(listType, attributes)

if (canWrapInList) {
return true
}

return commands.clearNodes()
})
.wrapInList(listType)
.wrapInList(listType, attributes)
.command(() => joinListBackwards(tr, listType))
.command(() => joinListForwards(tr, listType))
.run()
Expand All @@ -132,7 +132,7 @@ export const toggleList: RawCommands['toggleList'] = (listTypeOrName, itemTypeOr
chain()
// try to convert node to default node if needed
.command(() => {
const canWrapInList = can().wrapInList(listType)
const canWrapInList = can().wrapInList(listType, attributes)

const filteredMarks = marks.filter(mark => splittableMarks.includes(mark.type.name))

Expand All @@ -144,7 +144,7 @@ export const toggleList: RawCommands['toggleList'] = (listTypeOrName, itemTypeOr

return commands.clearNodes()
})
.wrapInList(listType)
.wrapInList(listType, attributes)
.command(() => joinListBackwards(tr, listType))
.command(() => joinListForwards(tr, listType))
.run()
Expand Down