CKeditor解决WPS复制内容粘贴后转成空格问题

920 阅读1分钟

找到ckeditor5中paste-from-office插件中的space.js文件,修改如下代码:

export function normalizeSpacerunSpans( htmlDocument ) {
    htmlDocument.querySelectorAll( 'span[style*=spacerun]' ).forEach( el => {
        // const innerTextLength = el.innerText.length || 0;
        
        // el.innerHTML = Array( innerTextLength + 1 ).join( '\u00A0 ' ).substr( 0, innerTextLength );
        // For paste contents from WPS Office, the span's inner text is not all of spaces or blank,
        // so we should keep the origin contents, or replace ONLY space characters.
        if ( /[^\b]/.test( el.innerText.trim() ) === false ) {
            const innerTextLength = el.innerText.length || 0;
            el.innerHTML = Array( innerTextLength + 1 ).join( '\u00A0 ' ).substr( 0, innerTextLength );
        }
    } );
}

如下图所示:

lQLPDhrmvcX3ZBDNAvXNB32wuFx2PJEKTlYBp0oS3sBYAA_1917_757.png