┌──────────────────────────────────────────────────────────────────────────────────┐ │ 📄 shadcn/directory/udecode/plate/(plugins)/(functionality)/block-placeholder.cn │ └──────────────────────────────────────────────────────────────────────────────────┘
╔══════════════════════════════════════════════════════════════════════════════════════════════╗
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
最快速添加块占位符的方式是使用 BlockPlaceholderKit,它包含预配置的 BlockPlaceholderPlugin。
import { createPlateEditor } from 'platejs/react';
import { BlockPlaceholderKit } from '@/components/editor/plugins/block-placeholder-kit';
const editor = createPlateEditor({
plugins: [
// ...其他插件
...BlockPlaceholderKit,
],
});
</Steps>
块占位符功能已包含在 Plate 核心包中。
import { BlockPlaceholderPlugin } from 'platejs/react';
import { BlockPlaceholderPlugin } from 'platejs/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...其他插件
BlockPlaceholderPlugin,
],
});
为不同块类型配置占位内容:
import { KEYS } from 'platejs';
import { BlockPlaceholderPlugin } from 'platejs/react';
BlockPlaceholderPlugin.configure({
options: {
placeholders: {
[KEYS.p]: '输入内容...',
[KEYS.h1]: '输入标题...',
[KEYS.blockquote]: '输入引用...',
[KEYS.codeBlock]: '输入代码...',
},
},
});
自定义外观和可见性规则:
import { KEYS } from 'platejs';
import { BlockPlaceholderPlugin } from 'platejs/react';
BlockPlaceholderPlugin.configure({
options: {
className: 'before:absolute before:cursor-text before:opacity-30 before:content-[attr(placeholder)]',
placeholders: {
[KEYS.p]: '输入内容...',
},
query: ({ path }) => {
// 仅在最外层块显示占位符
return path.length === 1;
},
},
});
placeholders: 块类型与占位文本的映射className: 应用于带占位符块的 CSS 类query: 决定哪些块应显示占位符的函数BlockPlaceholderPlugin用于在空白块中显示占位文本的插件。
当满足以下所有条件时,插件会显示占位符:
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╚══════════════════════════════════════════════════════════════════════════════════════════════╝