You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CKEditor 5插入图片属性转换异常:src未按预期更新

解决CKEditor 5图片URL转换时src属性未更新的问题

问题出在两处核心错误:

  • 属性转换规则指定的模型属性名错误(误用source而非imageInline原生的src)
  • 未同步处理属性更新时的双属性(src和data-source)联动修改

修改后的完整转换代码如下:

editor.conversion
    .for('downcast')
    // 处理图片元素初始创建
    .elementToElement( {
        model: 'imageInline',
        view: ( modelElement, { writer } ) => {
            const imgSrc = modelElement.getAttribute('src');
            return writer.createContainerElement(
                'img', { 
                    src: imgSrc.split('?')[0], 
                    'data-source': imgSrc 
                }
            );
        },
        converterPriority: 'high'
    } )
    // 处理图片src属性更新时的同步转换
    .attributeToAttribute( {
        model: {
            name: 'imageInline',
            key: 'src' // 修正为模型实际属性名
        },
        view: modelAttributeValue => ([
            { key: 'src', value: modelAttributeValue.split('?')[0] },
            { key: 'data-source', value: modelAttributeValue }
        ]),
        converterPriority: 'high'
    } );

关键说明:

  1. 修正属性匹配:将属性转换规则中的source改为src,匹配imageInline模型的原生属性,确保属性更新时能触发转换逻辑。
  2. 双属性同步更新:通过返回属性数组,在src属性变化时同时更新视图层的src(去除查询参数)和data-source(保留完整URL)。
  3. 高优先级覆盖默认逻辑:保持converterPriority: 'high',确保自定义转换逻辑覆盖CKEditor默认的图片属性转换规则。

内容的提问来源于stack exchange,提问作者Robby Ceyssens

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 19:18:25