Contribute to this guide

guide插件配置

# 配置约定

正如我们在本教程开头所了解到的,编辑器接受一个配置对象,允许您更改其默认行为和外观。CKEditor 5 中使用的约定是为每个插件提供一个唯一的对象键,以避免冲突并使每个配置部分的作用一目了然。

在本教程中,我们将向 `highlight` 插件添加一个选项来配置其键盘快捷键。此配置将包含在一个可选的 `highlight` 键中。

const editor = await ClassicEditor.create( element, {
    // Other options are omitted for readability - do not remove them.
    highlight: {

    }
} );

# 添加新的配置键

一旦我们知道了插件将接受的配置对象的形状,让我们定义一个新的配置键和默认值。如果在编辑器配置中未提供配置,将使用这些值。

为此,请在 `Highlight` 函数顶部添加以下代码

editor.config.define( 'highlight', {
    keystroke: 'Ctrl+Alt+H'
} );

传递给该方法的第一个参数是配置对象键的名称,第二个参数是默认值。

# 加载配置选项

我们已将 `keystroke` 键的默认值定义为 `'Ctrl+Alt+H'`,这是我们目前在插件中硬编码的值。让我们更新插件以从配置中读取此值。

在上面定义的配置之后,添加以下行以读取 `keystroke` 配置

const keystroke = editor.config.get( 'highlight.keystroke' );

然后,替换插件中 `'Ctrl+Alt+H'` 字符串的两个出现位置

button.set( {
    label: t( 'Highlight' ),
    withText: true,
    tooltip: true,
    isToggleable: true,
    keystroke // Update this line.
} );
editor.keystrokes.set( keystroke, 'highlight' ); // Update this line.

# 测试更改

让我们测试一下我们的更改。在浏览器中,选择编辑器中的一些文本,然后按 Ctrl + Alt + H 键查看默认配置是否像之前一样正常工作。

然后,打开 `src/main.js` 文件,并将编辑器的配置更新为将突出显示键盘快捷键更改为 Ctrl + Alt + 9

const editor = await ClassicEditor.create( element, {
    // Other options are omitted for readability - do not remove them.
    highlight: {
        keystroke: 'Ctrl+Alt+9'
    }
} );

最后,在浏览器中,选择编辑器中的一些文本,然后按 Ctrl + Alt + 9 键确认新快捷键是否正常工作。

# 结束了吗?

恭喜您完成了本教程!

但这并不是结束。如果您想继续学习,请访问我们文档的 框架部分