Joomla 编辑器脚本
JoomlaEditor 和 JoomlaEditorButton 为不同编辑器脚本的客户端集成提供了一个 API,同时保持跨扩展通信。这允许其他 Joomla 扩展与编辑器及其内容进行交互。
要在页面上启用它,请使用 WebAssetManager $wa->useScript('editors')(这也会加载 editor-api 和 editor-decorator 模块)。
API 包含以下元素
JoomlaEditorDecorator- 为编辑器提供了一个通用的接口以遵循。JoomlaEditor- 负责编辑器的注册和获取活动编辑器。JoomlaEditorButton- 负责注册编辑器按钮及其操作。
它们由 editor-api 模块提供,可以按以下方式导入
import { JoomlaEditor, JoomlaEditorButton, JoomlaEditorDecorator } from 'editor-api';
工作原理
编辑器脚本应提供(并使用 JoomlaEditor.register(editor) 向其注册)自己的 JoomlaEditorDecorator 实例,并实现所需的方法。任何时候当用户与编辑器或编辑器按钮之一交互时,编辑器脚本都应使用 JoomlaEditor.setActive(editor) 将此实例标记为活动状态。
每个编辑器按钮可以运行一个操作,此操作应事先使用 JoomlaEditorButton.registerAction(name, handler) 注册。其中
name- 是操作名称。handler- 要为此操作执行的函数。
当编辑器脚本决定将其界面集成到 Joomla 编辑器按钮中(例如在 Joomla TinyMCE 集成中完成),那么编辑器脚本负责为这些按钮调用按钮操作 JoomlaEditorButton.runAction(name, options)。其中
name- 是需要运行的操作的名称。options- 将传递给操作handler的选项对象。
有关示例,请参阅 如何创建编辑器插件。
JoomlaEditor 方法
register(editor)注册新的编辑器(实现JoomlaEditorDecorator)。unregister(editor)取消注册编辑器实例(通过JoomlaEditorDecorator或编辑器的id)。get(id)如果存在,则按id返回实例。setActive(editor)设置当前活动编辑器,即处于焦点的编辑器(通过JoomlaEditorDecorator或编辑器的id)。getActive()返回最后一个活动编辑器(如果存在)。
JoomlaEditorButton 方法
registerAction(name, handler)注册新的按钮操作或覆盖现有操作。runAction(name, options, button)执行操作。getActionHandler(name)按操作名称获取注册的处理程序。
操作处理程序是一个函数(或可调用对象),它将获得 JoomlaEditorDecorator 的活动编辑器实例和按钮选项
JoomlaEditorButton.registerAction('example', (editor, options) => {
alret('Doing the Example action! For editor ' + editor.getType());
});