docs/(plugins)/(elements)/mention.cn.mdx
@)最快捷的添加提及功能方式是使用 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,
],
});
npm install @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组件提供提及功能的插件。扩展自 TriggerComboboxPluginOptions。
<API name="MentionPlugin"> <APIOptions> <APIItem name="insertSpaceAfterMention" type="boolean" optional> 是否在提及后插入空格。 - **默认值:** `false` </APIItem> <APIItem name="trigger" type="string" optional> 触发提及组合框的字符。 - **默认值:** `'@'` </APIItem> <APIItem name="triggerPreviousCharPattern" type="RegExp" optional> 匹配触发字符前一个字符的模式。 - **默认值:** `/^\s?$/` </APIItem> <APIItem name="createComboboxInput" type="(trigger: string) => TElement" optional> 当触发字符激活时创建输入元素的函数。 </APIItem> </APIOptions> </API>提供提及输入功能的插件。
在当前选区插入提及元素。
<API name="api.insert.mention"> <APIParameters> <APIItem name="options" type="object"> <APISubList> <APISubListItem parent="options" name="search" type="string"> 触发提及的搜索文本。 </APISubListItem> <APISubListItem parent="options" name="value" type="any"> 存储在提及元素中的值。 </APISubListItem> <APISubListItem parent="options" name="key" type="any" optional> 提及元素的可选键值。 </APISubListItem> </APISubList> </APIItem> </APIParameters> </API>获取处理提及组合框项选择的处理器。
<API name="getMentionOnSelectItem"> <APIOptions> <APIItem name="key" type="string" optional> 提及插件的插件键。 - **默认值:** `MentionPlugin.key` </APIItem> </APIOptions> <APIReturns> 处理提及项选择的函数。 </APIReturns> </API>