Loading...
特性
- 使用 Tab/Shift+Tab 快捷键为块级元素添加缩进。
- 应用具有可自定义偏移量和单位的统一缩进。
- 向目标块级元素注入
indent
属性。 - 支持最大缩进深度控制。
Kit 使用
安装
添加缩进功能最快的方法是使用 IndentKit
,它包含预配置的 IndentPlugin
,针对段落、标题、引用块、代码块和切换元素。
- 配置
Paragraph
、Heading
、Blockquote
、CodeBlock
和Toggle
元素以支持indent
属性。 - 设置自定义缩进间距为
24px
。 - 提供 Tab/Shift+Tab 快捷键用于缩进和取消缩进。
添加 Kit
将 kit 添加到你的插件中:
import { createPlateEditor } from 'platejs/react';
import { IndentKit } from '@/components/editor/plugins/indent-kit';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
...IndentKit,
],
});
手动使用
安装
pnpm add @platejs/indent
添加插件
在创建编辑器时,将 IndentPlugin
包含在你的 Plate 插件数组中。
import { IndentPlugin } from '@platejs/indent/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
IndentPlugin,
],
});
配置插件
你可以配置 IndentPlugin
以针对特定元素并自定义缩进行为。
import { IndentPlugin } from '@platejs/indent/react';
import { KEYS } from 'platejs';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
IndentPlugin.configure({
inject: {
nodeProps: {
styleKey: 'marginLeft',
},
targetPlugins: [...KEYS.heading, KEYS.p, KEYS.blockquote],
},
options: {
offset: 24,
unit: 'px',
indentMax: 10,
},
}),
],
});
inject.nodeProps.styleKey
:将注入的属性映射到 CSSmarginLeft
属性。inject.targetPlugins
:一个插件键数组,指示哪些元素类型可以缩进。options.offset
:缩进偏移量(像素)(默认:24
)。options.unit
:缩进值的单位(默认:'px'
)。options.indentMax
:允许的最大缩进次数。
添加工具栏按钮
你可以将 IndentToolbarButton
添加到你的工具栏中以控制缩进。
插件
IndentPlugin
用于缩进块级元素的插件。它向 inject.targetPlugins
指定的元素注入 indent
属性并应用 marginLeft
样式。
inject.nodeProps.nodeKey optional string
注入到目标元素的属性名称。
- 默认值:
'indent'
- 默认值:
inject.nodeProps.styleKey optional string
用于样式的 CSS 属性名称。
- 默认值:
'marginLeft'
- 默认值:
inject.targetPlugins optional string[]
用于缩进注入的插件键数组。
- 默认值:
['p']
- 默认值:
options.offset optional number
在
(offset * element.indent) + unit
中使用的缩进偏移量。- 默认值:
24
- 默认值:
options.unit optional string
在
(offset * element.indent) + unit
中使用的缩进单位。- 默认值:
'px'
- 默认值:
options.indentMax optional number
允许的最大缩进次数。
API
indent
缩进编辑器中的选定块。
outdent
减少选定块的缩进。
setIndent
为选定的块添加缩进偏移量。
类型
SetIndentOptions
用于提供设置文本块缩进的选项。
Hooks
useIndentButton
缩进按钮组件的行为 hook。
useOutdentButton
取消缩进按钮组件的行为 hook。