Files
components/demo.tsx
'use client';

import * as React from 'react';

import { Plate, usePlateEditor } from 'platejs/react';

import { EditorKit } from '@/components/editor/editor-kit';
import { Editor, EditorContainer } from '@/components/ui/editor';

import { DEMO_VALUES } from './values/demo-values';

export default function Demo({ id }: { id: string }) {
  const editor = usePlateEditor({
    plugins: EditorKit,
    value: DEMO_VALUES[id],
  });

  return (
    <Plate editor={editor}>
      <EditorContainer variant="demo">
        <Editor />
      </EditorContainer>
    </Plate>
  );
}
Turn any block into a list item.
list-demo
list-demo

特性

  • 灵活的块缩进:通过缩进将任何块类型(段落、标题等)转换为列表项。
  • 简化结构:扁平的DOM结构,每个缩进的块都是独立的,不同于List Classic插件
  • 列表类型:支持项目符号列表(无序)和编号列表(有序)。
  • Markdown快捷键:结合自动格式化插件,使用Markdown快捷键(-*1.)创建列表。

有关底层缩进系统的更多信息,请参阅缩进插件

套件使用

安装

添加列表功能的最快方式是使用ListKit,它包含预配置的ListPlugin以及必需的缩进插件,针对段落、标题、引用块、代码块和折叠元素。

'use client';

import { ListPlugin } from '@platejs/list/react';
import { KEYS } from 'platejs';

import { IndentKit } from '@/components/editor/plugins/indent-kit';
import { BlockList } from '@/components/ui/block-list';

export const ListKit = [
  ...IndentKit,
  ListPlugin.configure({
    inject: {
      targetPlugins: [
        ...KEYS.heading,
        KEYS.p,
        KEYS.blockquote,
        KEYS.codeBlock,
        KEYS.toggle,
      ],
    },
    render: {
      belowNodes: BlockList,
    },
  }),
];
  • BlockList:渲染列表包装元素,支持待办事项列表。
  • 包含IndentKit用于底层缩进系统。
  • 配置ParagraphHeadingBlockquoteCodeBlockToggle元素以支持列表功能。

添加套件

将套件添加到插件中:

import { createPlateEditor } from 'platejs/react';
import { ListKit } from '@/components/editor/plugins/list-kit';
 
const editor = createPlateEditor({
  plugins: [
    // ...其他插件,
    ...ListKit,
  ],
});

添加工具栏按钮

您可以将ListToolbarButton添加到工具栏以创建和管理列表。

转换为工具栏按钮

您可以将以下项添加到转换为工具栏按钮中,将块转换为列表:

{
  icon: <ListIcon />,
  label: '项目符号列表',
  value: KEYS.ul,
}
{
  icon: <ListOrderedIcon />,
  label: '编号列表',
  value: KEYS.ol,
}
{
  icon: <SquareIcon />,
  label: '待办列表',
  value: KEYS.listTodo,
}

手动使用

安装

pnpm add @platejs/list @platejs/indent

添加插件

在创建编辑器时,将IndentPluginListPlugin都包含在Plate插件数组中。列表插件依赖于缩进插件。

import { IndentPlugin } from '@platejs/indent/react';
import { ListPlugin } from '@platejs/list/react';
import { createPlateEditor } from 'platejs/react';
 
const editor = createPlateEditor({
  plugins: [
    // ...其他插件,
    IndentPlugin,
    ListPlugin,
  ],
});

配置插件

您可以配置这两个插件以针对特定元素并自定义列表行为。

import { IndentPlugin } from '@platejs/indent/react';
import { ListPlugin } from '@platejs/list/react';
import { KEYS } from 'platejs';
import { createPlateEditor } from 'platejs/react';
import { BlockList } from '@/components/ui/block-list';
 
const editor = createPlateEditor({
  plugins: [
    // ...其他插件,
    IndentPlugin.configure({
      inject: {
        targetPlugins: [...KEYS.heading, KEYS.p, KEYS.blockquote, KEYS.codeBlock],
      },
    }),
    ListPlugin.configure({
      inject: {
        targetPlugins: [...KEYS.heading, KEYS.p, KEYS.blockquote, KEYS.codeBlock],
      },
      render: {
        belowNodes: BlockList,
      },
    }),
  ],
});
  • inject.targetPlugins:插件键的数组,指示哪些元素类型可以成为列表项。
  • render.belowNodes:分配BlockList以渲染列表包装元素。

插件

ListPlugin

用于创建和管理列表的插件。它与缩进插件配合使用,提供灵活的列表功能,任何块都可以通过缩进转换为列表项。

Options

Collapse all

    用于确定兄弟元素缩进列表选项的函数。

    将HTML元素映射到列表样式类型的函数。

API

getNextList

获取具有缩进列表的下一个兄弟entry。

Parameters

Collapse all

    当前元素的entry。

    获取下一个缩进列表的选项。

ReturnsNodeEntry | undefined

Collapse all

    具有缩进列表的下一个兄弟entry,如果未找到则为undefined

getPreviousList

获取具有缩进列表的上一个兄弟entry。

Parameters

Collapse all

    当前元素的entry。

    获取上一个缩进列表的选项。

ReturnsNodeEntry | undefined

Collapse all

    具有缩进列表的上一个兄弟entry,如果未找到则为undefined

indentList

增加所选块的缩进。

OptionsListOptions

Collapse all

    使用的列表样式类型。

    • 默认值: ListStyleType.Disc

outdentList

减少所选块的缩进。

OptionsListOptions

Collapse all

    使用的列表样式类型。

    • 默认值: ListStyleType.Disc

someList

检查所选块是否具有特定的列表样式类型。

Parameters

Collapse all

    要检查的列表样式类型。

toggleList

切换缩进列表。

OptionsListOptions

Collapse all

    使用的列表样式类型。

    覆盖列表项的编号。

    覆盖列表项的编号,仅在列表项是列表中的第一个时生效。

类型

GetSiblingListOptions

用于提供在文本块中获取兄弟缩进列表的选项。

Options

Collapse all

    此函数用于从给定entry获取上一个兄弟entry。

    此函数用于从给定entry获取下一个兄弟entry。

    此函数用于在查找过程中验证兄弟节点。 如果返回false,则检查下一个兄弟节点。

    指示当兄弟节点具有与当前节点相同的缩进级别时是否中断查找。如果为true,则在找到具有相同缩进级别的兄弟节点时停止查找。

    一个函数,接受TNode并返回布尔值或undefined。 此函数用于指定查找过程应停止的条件。

    指示当找到具有较低缩进级别的兄弟节点时是否中断查找。如果为true,则在找到具有较低缩进级别的兄弟节点时停止查找。

    指示当找到具有相同缩进级别但不同列表样式类型的兄弟节点时是否中断查找。如果为true,则在找到这样的兄弟节点时停止查找。

钩子

useListToolbarButton

缩进列表工具栏按钮的行为钩子。

State

Collapse all

    列表样式类型。

    按钮是否被按下。

Returnsobject

Collapse all

    工具栏按钮的属性。