docs/(plugins)/(functionality)/(combobox)/slash-command.cn.mdx
/ 字符触发添加斜杠命令功能最快的方式是使用 SlashKit,它包含预配置的 SlashPlugin 和 SlashInputPlugin 以及它们的 Plate UI 组件。
SlashInputElement: 渲染带有预定义选项的斜杠命令组合框import { createPlateEditor } from 'platejs/react';
import { SlashKit } from '@/components/editor/plugins/slash-kit';
const editor = createPlateEditor({
plugins: [
// ...其他插件
...SlashKit,
],
});
npm install @platejs/slash-command
import { SlashPlugin, SlashInputPlugin } from '@platejs/slash-command/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...其他插件
SlashPlugin,
SlashInputPlugin,
],
});
import { SlashPlugin, SlashInputPlugin } from '@platejs/slash-command/react';
import { createPlateEditor } from 'platejs/react';
import { SlashInputElement } from '@/components/ui/slash-node';
import { KEYS } from 'platejs';
const editor = createPlateEditor({
plugins: [
// ...其他插件
SlashPlugin.configure({
options: {
trigger: '/',
triggerPreviousCharPattern: /^\s?$/,
triggerQuery: (editor) =>
!editor.api.some({
match: { type: editor.getType(KEYS.codeBlock) },
}),
},
}),
SlashInputPlugin.withComponent(SlashInputElement),
],
});
options.trigger: 触发斜杠命令组合框的字符(默认: /)options.triggerPreviousCharPattern: 匹配触发字符前字符的正则表达式options.triggerQuery: 判断何时启用斜杠命令的函数withComponent: 指定斜杠命令界面的UI组件如何使用斜杠命令:
/ 打开斜杠菜单可用选项包括:
实现斜杠命令功能的插件。扩展自 TriggerComboboxPluginOptions。
<API name="SlashPlugin"> <APIOptions> <APIItem name="trigger" type="string" optional> 触发斜杠命令组合框的字符。 - **默认值:** `'/'` </APIItem> <APIItem name="triggerPreviousCharPattern" type="RegExp" optional> 匹配触发字符前字符的正则表达式。 - **默认值:** `/^\s?$/` </APIItem> <APIItem name="createComboboxInput" type="() => TComboboxInputElement" optional> 创建组合框输入元素的函数。 - **默认值:** ```tsx () => ({ children: [{ text: '' }], type: KEYS.slashInput, }); ``` </APIItem> <APIItem name="triggerQuery" type="(editor: PlateEditor) => boolean" optional> 判断何时启用斜杠命令的函数。可用于在代码块等特定上下文中禁用功能。 </APIItem> </APIOptions> </API>处理斜杠命令插入的输入行为。