Skip to content

Commit

Permalink
Adds attributes to toggleList (#3776)
Browse files Browse the repository at this point in the history
* Adds attributes to toggleList

When dealing with different variants of bullet lists, I wanted to adopt the same technique I used for different paragraph variants. Since `wrapInList` is capable of receiving attributes, just like `setNode` is, I don't see any reason why `toggleList` should not be capable of the same. 

Here's my bullet list extension in action that is in need of attributes support.

```js
export const CustomBulletList = BulletList.extend({
  content: 'listItem*',

  addAttributes() {
    return {
      variant: {
        default: DEFAULT_LIST,

        renderHTML: attributes => {
          return {
            class: `list-${attributes.variant}`,
          };
        },
      },
    };
  },

  addCommands() {
    return {
      toggleBulletList: attributes => (c) => {
        return c.commands.toggleListCustom(this.name, this.options.itemTypeName, attributes);
      },
    };
  },
});
```

* Update toggle-list.md

* Update toggle-list.md
  • Loading branch information
katerlouis authored Mar 3, 2023
1 parent 10a4a46 commit b2ec513
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
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

0 comments on commit b2ec513

Please sign in to comment.