Odoo 编辑器

Odoo Editor 是 Odoo 自有的富文本编辑器。其源码位于 odoo-editor 目录。

Powerbox

Powerbox 是一个用户界面组件,包含按 类别 分组的 命令。在编辑器中输入 / 时它会出现。用户输入文本时可以过滤命令,并可用方向键进行导航。

输入 “/” 后打开的 Powerbox。

修改 Powerbox

同一时间只能实例化一个 Powerbox,这项工作由编辑器本身完成。其 Powerbox 实例可以在 powerbox 实例变量中找到。要修改 Powerbox 的内容和选项,请在编辑器实例化之前修改传给它的选项。

Important

切勿自行实例化 Powerbox,始终使用当前编辑器自身的实例。

Example

假设我们想为 Powerbox 新增一个名为 Document 的命令,且仅针对 mass_mailing 模块。我们想把它加入一个名为 Documentation 的新类别,并让它显示在 Powerbox 的最顶部。

mass_mailing extends web_editor 的 Wysiwyg 类,后者在其 start 方法中实例化编辑器。在此之前,它会调用自己的 _getPowerboxOptions 方法,可方便地重写该方法以添加我们的新命令。

由于 mass_mailing 已经重写了 _getPowerboxOptions,我们只需在其中添加新命令即可:

_getPowerboxOptions: function () {
    const options = this._super();
    // (existing code before the return statement)
    options.categories.push({
        name: _t('Documentation'),
        priority: 300,
    });
    options.commands.push({
        name: _t('Document'),
        category: _t('Documentation'),
        description: _t("Add this text to your mailing's documentation"),
        fontawesome: 'fa-book',
        priority: 1, // This is the only command in its category anyway.
    });
    return options;
}

Important

为了让命令和类别的名称与描述能够被翻译,请务必将它们包裹在 _t 函数中。

Tip

为避免优先级失控,请不要随意指定优先级数值:先查看已有的其他优先级,再据此选择合适的值(就像设置 z-index 时那样)。

打开自定义的 Powerbox

可以绕过所有既有的类别和命令,用自定义的一组类别和命令打开 Powerbox。为此,请调用 Powerbox 的 open 方法,并传入自定义的命令和类别。

粘贴图片 URL 时,使用自定义类别和命令打开的 Powerbox。

Example

我们需要 Powerbox 的当前实例,它可以在当前编辑器中找到。在 Wysiwyg 类 中,它对应 this.odooEditor.powerbox。

现在,用自定义的 “Document” 命令和自定义的 “Documentation” 类别打开它:

this.odooEditor.powerbox.open(
    [{
        name: _t('Document'),
        category: _t('Documentation'),
        description: _t("Add this text to your mailing's documentation"),
        fontawesome: 'fa-book',
        priority: 1, // This is the only command in its category anyway.
    }],
    [{
        name: _t('Documentation'),
        priority: 300,
    }]
);

过滤命令

过滤命令有三种方式:

  1. 通过 powerboxFilters Powerbox 选项。

  2. 通过给定 命令 的 isDisabled 条目。

  3. 用户打开 Powerbox 后,只需输入文本即可过滤命令。系统会将该文本与类别和命令的名称进行模糊匹配。

使用单词 “head” 过滤命令后的 Powerbox。

开发手册

类别

名称

类型

说明

name

string

类别的名称

priority

number

用于对类别排序:优先级更高的类别在 Powerbox 中显示得更靠上(优先级相同的类别按字母顺序排列)

Note

如果存在多个同名类别,它们将被合并为一个,其优先级取最后声明的那个类别的优先级。

命令

名称

类型

说明

name

string

命令的名称

category

string

命令所属类别的名称

description

string

用于简短描述命令的文本

fontawesome

string

将作为命令图标使用的 Font Awesome 图标名称

priority

number

用于对命令排序:优先级更高的命令在 Powerbox 中显示得更靠上(优先级相同的命令按字母顺序排列)

callback

function (() => void)

选中命令时要执行的函数(可以是异步的)

isDisabled (可选)

function (() => void)

用于在特定条件下禁用命令的函数(当其返回 true 时,该命令将被禁用)

Note

如果命令指向的类别尚不存在,该类别将被创建并追加到 Powerbox 的末尾。

选项

以下选项可以传递给 OdooEditor,随后会被传给 Powerbox 实例:

名称

类型

说明

commands

array of commands

要在编辑器默认命令基础上添加的命令

categories

array of categories

要在编辑器默认类别基础上添加的类别

powerboxFilters

array of functions (commands => commands)

用于过滤 Powerbox 中所显示命令的函数

getContextFromParentRect

function (() => DOMRect)

返回编辑器某个祖先元素的 DOMRect 的函数(当编辑器位于 iframe 中时很有用)