docs/api/core/plate-plugin.cn.mdx
Plate 插件是通过 Plate 组件的 plugins 属性传递的对象。
type OnNodeChange = (ctx: PlatePluginContext & {
node: Descendant;
operation: NodeOperation;
prevNode: Descendant;
}) => HandlerReturnType;
参数:
node:操作后的节点operation:发生的节点操作prevNode:操作前的节点注意: 对于 insert_node 和 remove_node 操作,node 和 prevNode 包含相同的值以避免空值情况。
</APISubListItem>
<APISubListItem parent="handlers" name="onTextChange" type="OnTextChange" optional>
每当发生文本操作时调用(插入或删除文本)。
type OnTextChange = (ctx: PlatePluginContext & {
node: Descendant;
operation: TextOperation;
prevText: string;
text: string;
}) => HandlerReturnType;
参数:
node:包含已更改文本的父节点operation:发生的文本操作(insert_text 或 remove_text)prevText:操作前的文本内容text:操作后的文本内容
</APISubListItem>
[ParagraphPlugin.key]
</APISubListItem>
true
</APISubListItem>
'default': 使用 Slate 的默认行为
'directional': 选区亲和性由光标移动方向决定。根据接近方向保持向内或向外亲和性
'outward': 强制向外亲和性。在标记边缘输入不会将标记应用到新文本
'hard': 创建需要两次按键才能跨越的"硬"边缘。使用基于偏移的导航
默认值: undefined (Slate 的默认行为)
true
</APISubListItem>
用于退出断点功能确定嵌套结构中的适当退出点。参见 退出断点。
false
</APISubListItem>
'default': 默认行为'reset': 重置块为默认段落类型'exit': 退出当前块'deleteExit': 向后删除然后退出
</APISubListItem>
'default': 默认行为'exit': 退出当前块'deleteExit': 向后删除然后退出
</APISubListItem>
'default': 默认行为'exit': 退出当前块'lineBreak': 插入换行符'deleteExit': 向后删除然后退出
</APISubListItem>
'default': 默认行为'reset': 重置块为默认段落类型
</APISubListItem>
'default': 默认行为'reset': 重置块为默认段落类型
</APISubListItem>
默认值: type === node.type
示例: matchRules: ({ node }) => Boolean(node.listStyleType)
示例:列表插件设置 match: ({ node }) => !!node.listStyleType 以在段落是列表项时覆盖段落行为。
false
</APISubListItem>
false
</APISubListItem>
plugin.key
</APISubListItem>
'div',leaves 为 'span'100
</APIItem>
extendEditor: ({ editor }) => {
// 示例:集成旧版 Slate 插件
return withYjs(editor);
}
可以是布尔值或对象配置:
type EditOnlyConfig = {
render?: boolean; // 默认: true
handlers?: boolean; // 默认: true
inject?: boolean; // 默认: true
normalizeInitialValue?: boolean; // 默认: false
}
当设置为 true(布尔值)时:
render、handlers 和 inject.nodeProps 仅在编辑器非只读时激活normalizeInitialValue 无论只读状态如何都保持激活当设置为对象时:
true),除了 normalizeInitialValue 默认始终激活(false)false 使其始终激活,无论只读状态如何normalizeInitialValue,设置为 true 使其仅在编辑时激活示例:
// 所有功能(除 normalizeInitialValue 外)都是仅编辑的
editOnly: true
// normalizeInitialValue 是仅编辑的,其他保持默认行为
editOnly: { normalizeInitialValue: true }
// render 始终激活,其他遵循默认行为
editOnly: { render: false }
(config: PlatePluginConfig<C['key'], InferOptions<C>, InferApi<C>, InferTransforms<C>> | ((ctx: PlatePluginContext<C>) => PlatePluginConfig<C['key'], InferOptions<C>, InferApi<C>, InferTransforms<C>>)) => PlatePlugin<C>
(extendConfig: Partial<PlatePlugin> | ((ctx: PlatePluginContext<AnyPluginConfig>) => Partial<PlatePlugin>)) => PlatePlugin
(key: string, extendConfig: Partial<PlatePlugin> | ((ctx: PlatePluginContext<AnyPluginConfig>) => Partial<PlatePlugin>)) => PlatePlugin
(component: NodeComponent) => PlatePlugin<C>
overrideEditor(({ editor, tf: { deleteForward }, api: { isInline } }) => ({
transforms: {
// 重写 transforms
deleteForward(options) {
deleteForward(options);
},
},
api: {
// 重写 API 方法
isInline(element) {
return isInline(element);
},
},
})) => PlatePlugin<C>
(api: (ctx: PlatePluginContext) => Record<string, Function>) => PlatePlugin
(api: (ctx: PlatePluginContext) => Record<string, Function>) => PlatePlugin
(transforms: (ctx: PlatePluginContext) => Record<string, Function>) => PlatePlugin
(transforms: (ctx: PlatePluginContext) => Record<string, Function>) => PlatePlugin
(options: (ctx: PlatePluginContext) => Record<string, any>) => PlatePlugin
有关 Plate 插件特定方面的更详细信息,请参阅 插件配置、插件方法、插件上下文、插件组件 和 插件快捷键 的单独指南。
使用示例:
type MyPluginConfig = PluginConfig<
'myPlugin',
{ customOption: boolean },
{ getData: () => string },
{ customTransform: () => void }
>;
const MyPlugin = createPlatePlugin<MyPluginConfig>({
key: 'myPlugin',
// 插件实现
});