Indentation
Easily control the indentation of specific blocks to highlight important information and improve visual structure.
For instance, this paragraph looks like it belongs to the previous one.
1
100%
Files
'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>
);
}
indent-demo


特性
- 使用 Tab/Shift+Tab 快捷键为块级元素添加缩进。
- 应用具有可自定义偏移量和单位的统一缩进。
- 向目标块级元素注入
indent属性。 - 支持最大缩进深度控制。
Kit 使用
安装
添加缩进功能最快的方法是使用 IndentKit,它包含预配置的 IndentPlugin,针对段落、标题、引用块、代码块和切换元素。
'use client';
import { IndentPlugin } from '@platejs/indent/react';
import { KEYS } from 'platejs';
export const IndentKit = [
IndentPlugin.configure({
inject: {
targetPlugins: [
...KEYS.heading,
KEYS.p,
KEYS.blockquote,
KEYS.codeBlock,
KEYS.toggle,
],
},
options: {
offset: 24,
},
}),
];
- 配置
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 样式。
API
indent
缩进编辑器中的选定块。
outdent
减少选定块的缩进。
setIndent
为选定的块添加缩进偏移量。
类型
SetIndentOptions
用于提供设置文本块缩进的选项。
Hooks
useIndentButton
缩进按钮组件的行为 hook。
useOutdentButton
取消缩进按钮组件的行为 hook。