Loading...
功能特性
- 智能自动补全,可用于提及用户、页面或任何引用
- 通过可配置字符触发(默认:
@
) - 键盘导航与选择
- 可自定义的提及数据与渲染方式
套件使用方式
安装
最快捷的添加提及功能方式是使用 MentionKit
,它包含预配置的 MentionPlugin
和 MentionInputPlugin
以及它们的 Plate UI 组件。
MentionElement
: 渲染提及元素MentionInputElement
: 渲染提及输入组合框
添加套件
import { createPlateEditor } from 'platejs/react';
import { MentionKit } from '@/components/editor/plugins/mention-kit';
const editor = createPlateEditor({
plugins: [
// ...其他插件
...MentionKit,
],
});
手动配置方式
安装
pnpm add @platejs/mention
添加插件
import { MentionPlugin, MentionInputPlugin } from '@platejs/mention/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...其他插件
MentionPlugin,
MentionInputPlugin,
],
});
配置插件
import { MentionPlugin, MentionInputPlugin } from '@platejs/mention/react';
import { createPlateEditor } from 'platejs/react';
import { MentionElement, MentionInputElement } from '@/components/ui/mention-node';
const editor = createPlateEditor({
plugins: [
// ...其他插件
MentionPlugin.configure({
options: {
trigger: '@',
triggerPreviousCharPattern: /^$|^[\s"']$/,
insertSpaceAfterMention: false,
},
}).withComponent(MentionElement),
MentionInputPlugin.withComponent(MentionInputElement),
],
});
options.trigger
: 触发提及组合框的字符options.triggerPreviousCharPattern
: 触发字符前一个字符的正则表达式模式。套件使用/^$|^[\s"']$/
允许在行首、空白符后或引号后触发提及options.insertSpaceAfterMention
: 是否在插入提及后自动添加空格withComponent
: 分配用于渲染提及和提及输入的UI组件
插件说明
MentionPlugin
提供提及功能的插件。扩展自 TriggerComboboxPluginOptions。
MentionInputPlugin
提供提及输入功能的插件。
API接口
api.insert.mention
在当前选区插入提及元素。
getMentionOnSelectItem
获取处理提及组合框项选择的处理器。