【问题标题】:ckeditor 'paste from Word': retain font color if not blackckeditor 'paste from Word':如果不是黑色,则保留字体颜色
【发布时间】:2016-05-03 11:07:50
【问题描述】:

当我从 Word 粘贴时,我想摆脱大多数字体样式,但如果它们不是默认字体颜色(黑色),我有办法保留它们。这样,如果我的 Word 文档中的文本有一行红色文本,我就不会丢失它。

PasteFromWord 插件的配置选项包括一种在粘贴 Word 的字体样式时保留它们的方法(pasteFromWordRemoveFontStyles = false),但这为大量字体杂乱无章打开了大门。我只想让任何非默认字体颜色通过。

在 Word 中可以通过使用 H1-H6 样式在很大程度上处理保留字体大小,但我不明白如何在 Word 中为红色文本设置样式并将其粘贴到 CKEditor。任何帮助或想法表示赞赏。谢谢。

【问题讨论】:

    标签: ckeditor


    【解决方案1】:

    这里有一个小技巧,可以通过修改 Paste From Word 插件来保留颜色样式。

    首先,在您的 ckeditor 配置中打开样式过滤器:

    config.pasteFromWordRemoveFontStyles = true;
    

    现在,在“ckeditor/plugins/pastefromword”文件夹下,打开“default.js”。

    找到此代码部分(撰写本文时的第 956 行):

    // Assume MS-Word mostly carry font related styles on <span>,
    // adapting them to editor's convention.
    if ( styleText ) {
        attrs.style = stylesFilter( [
            // Drop 'inline-height' style which make lines overlapping.
            [ 'line-height' ],
            [ ( /^font-family$/ ), null, !removeFontStyles ? styleMigrateFilter( config.font_style, 'family' ) : null ],
            [ ( /^font-size$/ ), null, !removeFontStyles ? styleMigrateFilter( config.fontSize_style, 'size' ) : null ],
            [ ( /^color$/ ), null, !removeFontStyles ? styleMigrateFilter( config.colorButton_foreStyle, 'color' ) : null ],
            [ ( /^background-color$/ ), null, !removeFontStyles ? styleMigrateFilter( config.colorButton_backStyle, 'color' ) : null ]
        ] )( styleText, element ) || '';
    }
    

    在以/^color$/ 开头的行中,将!removeFontStyles 更改为removeFontStyles,因此现在是新行:

    [ ( /^color$/ ), null, removeFontStyles ? styleMigrateFilter( config.colorButton_foreStyle, 'color' ) : null ],
    

    在测试前清除浏览器缓存。

    【讨论】:

    • 感谢您的回复。那不在我的文件中。前几天我也刚下载了插件。我看到的是: if (e = (b = a.attributes) && b.style) b.style = l([ ["line-height"], [/^font-family$/, null, r ? null : n(d.font_style, "family")], [/^font-size$/, null, r ? null : n(d.fontSize_style, "size")], [/^color$/, null , r ? null : n(d.colorButton_foreStyle, "color")], [/^background-color$/, null, r ? null : n(d.colorButton_backStyle, "color")] ])(e, a) || "";
    • @lauren 我从here 下载了插件,它不像下载完整ckeditor 包时获得的版本那样缩小了js。更容易理解和修改...
    • 啊,这更容易阅读,它适用于手动应用的样式。但是,我的 Word 文档使用 Word 样式 - 有没有办法将字体颜色保留添加到该文件中的白名单代码中? (首选,仅当还不是黑色时)。黑名单中有一些看起来很有希望的东西,但它有点超出我的范围。
    • 我将接受您的回复,因为它适用于普通案件。我认为我的问题更大,让 CKE 承认 Word Styles。
    • 我认为允许 Word 样式通过,然后接受它们的一些属性同时拒绝其他属性,将需要深入研究“剪贴板”插件,该插件通常只是将它们完全过滤掉,但我不知道该怎么做。
    猜你喜欢
    • 1970-01-01
    • 2021-03-15
    • 2018-03-01
    • 2011-09-19
    • 2019-11-27
    • 1970-01-01
    • 2011-04-25
    • 2022-08-03
    • 1970-01-01
    相关资源
    最近更新 更多