1.富文本在初始化之后,更改配置参数,如何重新加载?
假设需求是默认关闭顶部的工具栏,当富文本聚焦的时候再打开顶部的工具栏,代码如下:
const init = {
...其他配置,
setup: function(editor) {
// 监听聚焦事件
editor.on('focus', () => {
// 先删除插件
editor.editorManager.execCommand('mceRemoveEditor', true, editor.id)
// 将工具栏打开
init.toolbar = true;
// 重新添加
editor.editorManager.execCommand('mceAddEditor', true, editor.id)
});
}
}
2.如何在工具栏添加自定义按钮?
const init = {
...其他配置,
setup: function(editor) {
// 通过editor公共api registry注册一个icon,第一个参数是自定义名称,第二个是svg
editor.ui.registry.addIcon('triangleUp', '<svg height="24" width="24"><path d="M12 0 L24 24 L0 24 Z" /></svg>' );
// 然后在注册一个按钮,text名称自定义,icon为上一步注册的icon名称
// onAction则是监听按钮点击事件
editor.ui.registry.addButton('customInsertButton', {
text: 'My Button',
icon: 'triangleUp',
onAction: function () {
editor.insertContent(' <strong>It\'s my button!</strong> ');
}
});
}
}