Skip to content
GitHub

List

The list node is used to represent a list item. It is built on top of prosemirror-flat-list.

import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'

import {
  createEditor,
  type NodeJSON,
} from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'

import { defineExtension } from './extension'
import Toolbar from './toolbar'

export default function Editor() {
  const editor = useMemo(() => {
    return createEditor({ extension: defineExtension(), defaultContent })
  }, [])

  return (
    <ProseKit editor={editor}>
      <div className='box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow flex flex-col bg-white dark:bg-gray-950 color-black dark:color-white'>
        <Toolbar />
        <div className='relative w-full flex-1 box-border overflow-y-scroll'>
          <div ref={editor.mount} className='ProseMirror box-border min-h-full px-[max(4rem,_calc(50%-20rem))] py-8 outline-none outline-0 [&_span[data-mention="user"]]:text-blue-500 [&_span[data-mention="tag"]]:text-violet-500'></div>
        </div>
      </div>
    </ProseKit>
  )
}

const defaultContent: NodeJSON = {
  type: 'doc',
  content: [
    {
      type: 'list',
      attrs: { kind: 'bullet' },
      content: [
        { type: 'paragraph', content: [{ type: 'text', text: 'Bullet List' }] },
      ],
    },
    {
      type: 'list',
      attrs: { kind: 'ordered' },
      content: [
        {
          type: 'paragraph',
          content: [{ type: 'text', text: 'Ordered List' }],
        },
      ],
    },
    {
      type: 'list',
      attrs: { kind: 'task', checked: false },
      content: [
        { type: 'paragraph', content: [{ type: 'text', text: 'Task List ' }] },
      ],
    },
    {
      type: 'list',
      attrs: { kind: 'toggle', collapsed: true },
      content: [
        { type: 'paragraph', content: [{ type: 'text', text: 'Toggle List' }] },
        {
          type: 'list',
          attrs: {
            kind: 'bullet',
          },
          content: [
            { type: 'paragraph', content: [{ type: 'text', text: 'Hidden' }] },
          ],
        },
      ],
    },
  ],
}
import 'prosekit/extensions/list/style.css'

import { defineListfunction defineList(): ListExtension
@public
} from 'prosekit/extensions/list'
const extensionconst extension: ListExtension = defineListfunction defineList(): ListExtension
@public
()

Decreases the indentation of selected list nodes.

editorconst editor: Editor<ListExtension>.commands
Editor<ListExtension>.commands: ToCommandAction<{
    dedentList: [options?: DedentListOptions];
    indentList: [options?: IndentListOptions];
    moveList: [direction: "up" | "down"];
    ... 5 more ...;
    insertList: [attrs?: ListAttributes];
}>
All {@link CommandAction } s defined by the editor.
.dedentList
dedentList: CommandAction
(options?: DedentListOptions | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Increases the indentation of selected list.

editorconst editor: Editor<ListExtension>.commands
Editor<ListExtension>.commands: ToCommandAction<{
    dedentList: [options?: DedentListOptions];
    indentList: [options?: IndentListOptions];
    moveList: [direction: "up" | "down"];
    ... 5 more ...;
    insertList: [attrs?: ListAttributes];
}>
All {@link CommandAction } s defined by the editor.
.indentList
indentList: CommandAction
(options?: IndentListOptions | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Moves up or down selected list nodes.

editorconst editor: Editor<ListExtension>.commands
Editor<ListExtension>.commands: ToCommandAction<{
    dedentList: [options?: DedentListOptions];
    indentList: [options?: IndentListOptions];
    moveList: [direction: "up" | "down"];
    ... 5 more ...;
    insertList: [attrs?: ListAttributes];
}>
All {@link CommandAction } s defined by the editor.
.moveList
moveList: CommandAction
(direction: "up" | "down") => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
('down')

Split the current list node.

editorconst editor: Editor<ListExtension>.commands
Editor<ListExtension>.commands: ToCommandAction<{
    dedentList: [options?: DedentListOptions];
    indentList: [options?: IndentListOptions];
    moveList: [direction: "up" | "down"];
    ... 5 more ...;
    insertList: [attrs?: ListAttributes];
}>
All {@link CommandAction } s defined by the editor.
.splitList
splitList: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Toggle the collapsed attribute of the list node.

editorconst editor: Editor<ListExtension>.commands
Editor<ListExtension>.commands: ToCommandAction<{
    dedentList: [options?: DedentListOptions];
    indentList: [options?: IndentListOptions];
    moveList: [direction: "up" | "down"];
    ... 5 more ...;
    insertList: [attrs?: ListAttributes];
}>
All {@link CommandAction } s defined by the editor.
.toggleCollapsed
toggleCollapsed: CommandAction
(options?: ToggleCollapsedOptions | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Wraps the selection in a list with the given type and attributes, or change the list kind if the selection is already in another kind of list, or unwrap the selected list if otherwise.

editorconst editor: Editor<ListExtension>.commands
Editor<ListExtension>.commands: ToCommandAction<{
    dedentList: [options?: DedentListOptions];
    indentList: [options?: IndentListOptions];
    moveList: [direction: "up" | "down"];
    ... 5 more ...;
    insertList: [attrs?: ListAttributes];
}>
All {@link CommandAction } s defined by the editor.
.toggleList
toggleList: CommandAction
(attrs?: ListAttributes | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ kindListAttributes.kind?: string | undefined: 'task', checkedListAttributes.checked?: boolean | undefined: true })

Unwraps the list around the selection.

editorconst editor: Editor<ListExtension>.commands
Editor<ListExtension>.commands: ToCommandAction<{
    dedentList: [options?: DedentListOptions];
    indentList: [options?: IndentListOptions];
    moveList: [direction: "up" | "down"];
    ... 5 more ...;
    insertList: [attrs?: ListAttributes];
}>
All {@link CommandAction } s defined by the editor.
.unwrapList
unwrapList: CommandAction
(options?: UnwrapListOptions | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Wraps the selection in a list with the given type and attributes.

editorconst editor: Editor<ListExtension>.commands
Editor<ListExtension>.commands: ToCommandAction<{
    dedentList: [options?: DedentListOptions];
    indentList: [options?: IndentListOptions];
    moveList: [direction: "up" | "down"];
    ... 5 more ...;
    insertList: [attrs?: ListAttributes];
}>
All {@link CommandAction } s defined by the editor.
.wrapInList
wrapInList: CommandAction
(attrs?: ListAttributes | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ kindListAttributes.kind?: string | undefined: 'bullet' })

To create different types of lists:

  • Type 1. and a space to start an ordered list.
  • Type - or * and a space to start an unordered list.
  • Type [x] or [] and a space to start a task list.
  • Type >> and a space to start a toggle list.