CKEditor 5能否为<div>元素启用widgettypearound(原Magic Line)功能?
解决CKEditor 5 Widget Type Around支持
元素的方案
针对Drupal 10环境下CKEditor 5 40.2.0版本中Widget Type Around不识别
的问题,以下是几个可行的解决思路:
1. 自定义CKEditor 5插件(原生支持方案)
你可以开发一个轻量自定义插件,扩展Widget Type Around的元素识别范围:
- 创建一个Drupal自定义模块,在模块里注册CKEditor 5插件
- 在插件的
init()方法中,获取编辑器的WidgetTypeAround插件实例,修改其判断逻辑,让它把元素纳入触发范围 - 核心代码示例:
export default class DivWidgetTypeAround extends Plugin { static get requires() { return [ 'WidgetTypeAround' ]; } init() { const editor = this.editor; const widgetTypeAround = editor.plugins.get( 'WidgetTypeAround' ); // 重写判断逻辑,加入<div>元素的识别 const originalShouldShowWidget = widgetTypeAround._shouldShowWidget; widgetTypeAround._shouldShowWidget = ( viewElement ) => { if ( viewElement.is( 'div' ) ) { return true; } return originalShouldShowWidget.call( widgetTypeAround, viewElement ); }; } }
- 按照Drupal CKEditor 5插件的注册规范,把这个插件绑定到你的编辑器配置中
2. 借助现有模块快速配置
如果不想写自定义插件,可以用CKEditor 5 Extra Config模块:
- 安装并启用该模块后,进入Drupal后台的编辑器配置页面(路径:
/admin/config/content/formats) - 找到你使用的CKEditor 5编辑器配置,在「Extra JavaScript configuration」字段中添加以下配置:
{ "widgetTypeAround": { "additionalElements": [ "div" ] } }
注:部分版本可能需要调整配置键名,如果上述不生效,可以尝试直接修改WidgetTypeAround的识别规则
3. 临时替代方案
如果只是临时需要编辑
内容,除了切换源码模式,还可以:
- 给目标添加
contenteditable="true"属性,让浏览器允许直接在元素内放置光标 - 或者在前后手动插入空段落,完成编辑后再删除多余段落
内容的提问来源于stack exchange,提问作者Argyro Kazaki
相关产品推荐
相关产品推荐

