javascript - Can't seem to preserve inline elements such as <span> , with custom blot - Stack Overflow

I've created a customblot, to handle span conversion but for some reason any text after <span&g

I've created a customblot, to handle span conversion but for some reason any text after <span> is being treated as a span too and causing the following:

Uncaught s: [Parchment] Unable to create [object HTMLSpanElement] blot

this is the test case i've been using

<p id="x4uqkat" class="x4uqkat--text__typography a98db97--highlight__border">this is text <span class="test">is here</span>   replaceable</p> 
</div>

Code below does preserve but it's incorrectly converting replaceable

var Inline = Quill.import('blots/inline');

var Delta = Quill.import('delta');
class CustomSpan extends Inline {
    static blotName = 'customSpan';
    static tagName = 'span';

    static formats(domNode) {
        const className = domNode.getAttribute('class');
        console.log('Formats called with:', className, domNode);
        return className ? { customSpan: className } : {};
    }

    format(name, value) {
        if (name === 'customSpan') {
            if (value) {
                this.domNode.setAttribute('class', value);
            } else {
                this.domNode.removeAttribute('class');
            }
        } else {
            super.format(name, value);
        }
    }
}
    
    
Quill.register(CustomSpan,false);
   
const quill = new Quill("#editor", {
    modules: {
        toolbar: false,
    }, 
    theme: "snow" ,
});

quill.clipboard.addMatcher('SPAN', function(node, delta) {
    const className = node.getAttribute('class');
    console.log('Processing span with class:', className);

    if (!className) return delta; // Ignore spans without class

    let newDelta = new Delta();

    // Apply customSpan format only to the text inside the span
    delta.ops.forEach(op => {
        if (typeof op.insert === 'string') {
            newDelta.insert(op.insert, { customSpan: className });
        } else {
            newDelta.insert(op.insert);
        }
    });
    
    return newDelta;
});

JSFiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745305934a4621709.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信