TinyMCE添加 

9
我正在尝试覆盖Piranha CMS中的默认tinymce编辑器,就像这里建议的那样:Configure / override Piranha CMS html editor so as not to add &nbsp's to html。我已经花费了大约一个小时来解决这个问题。有很多关于这个问题的资源,但是我无法让它工作。

这是我的tinymce.init的样子。

<script type="text/javascript" src="~/res.ashx/areas/manager/content/js/ext/tiny_mce/tinymce.min.js"></script>
<script type="text/javascript">
    tinymce.init({
        mode: 'specific_textareas',
        editor_selector: "editor",
        apply_source_formatting: false,
        cleanup_on_startup: false,
        trim_span_elements: false,
        cleanup: false,
        convert_urls: false,
        force_br_newlines: true,
        force_p_newlines: false,
        remove_linebreaks: false,
        convert_newlines_to_brs: false,
        forced_root_block: '',
        inline_styles : true,
        entity_encoding: 'raw',
        verify_html: false,
        //forced_root_block: false,
        validate_children: false,
        remove_redundant_brs: false,
        fix_table_elements: false,

        entities: '160,nbsp,38,amp,60,lt,62,gt',

        plugins: [
            "autoresize autolink code hr paste piranhaimage link"
        ],
        width: "100%",
        height: "340",
        autoresize_min_height: 340,
        @if (File.Exists(Server.MapPath("~/areas/manager/content/css/editor.css"))) {
        <text>content_css: "@Url.Content("~/areas/manager/content/css/editor.css")",</text>
        }
        toolbar: "bold italic underline | bullist numlist hr | formatselect removeformat | cut copy paste | link piranhaimage | code",
        paste_auto_cleanup_on_paste: false,
        paste_postprocess: function (pl, o) {
            // remove extra line breaks
            o.node.innerHTML = o.node.innerHTML.replace(/&nbsp;/ig, " ");
            alert("a1");
        },
        cleanup_callback: 'my_cleanup_callback',
    });
    function my_cleanup_callback(type, value) {
        alert("a2");
        switch (type) {
            case 'get_from_editor':
                // Remove &nbsp; characters
                value = value.replace(/&nbsp;/ig, ' ');
                alert("a3");
                break;
            case 'insert_to_editor':
            case 'submit_content':
            case 'get_from_editor_dom':
            case 'insert_to_editor_dom':
            case 'setup_content_dom':
            case 'submit_content_dom':
            default:
                break;
        }
        return value;
    }
</script>

这是我用来粘贴到TinyICE文本区域的HTML示例。
<div class="catelog-box">
    <img src="images/dance.png" alt="dine">
    <div class="cat-detail">
    <h2>Dance</h2>
    <p>Dis purus arcu etiam auctor risus aliquam mid turpis eu vel, nunc rhoncus lacus natoque ridiculus...</p>          
  </div>
</div>

这是在浏览器源代码中的样子: enter image description here 我放置了警报来检查是否实际上触发了“paste_postprocess”和“my_cleanup_callback”,但它们没有。并且我仍然在HTML中有“ ”。
我尝试设置“cleanup:true”和“paste_auto_cleanup_on_paste:true”,但这无法帮助触发“paste_postprocess”和“my_cleanup_callback”。
您将如何解决“ ”问题?
5个回答

16

只需添加entity_encoding: 'raw'即可解决问题。


4
以下代码将清除tinymce内容中的所有内容。
tinymce.init({
    selector: "textarea",
    invalid_elements :'strong,bold,b,em,br,span,div,p,img,a,table,td,th,tr,header,font,body,h,h1,h2,h3,h4,h5',
    invalid_styles: 'color font-size text-decoration font-weight',
    menubar: false,
    toolbar:false,
    statusbar:false,
    forced_root_block : "",
    cleanup: true,
    remove_linebreaks: true,
    convert_newlines_to_brs: false,
    inline_styles : false,
    entity_encoding: 'raw',
    entities: '160,nbsp,38,amp,60,lt,62,gt',
 });}

2

TinyMCE拥有众多选项和设置,但鉴于您提供的链接并且您使用了来自TinyMCE is adding &nbsp instead of the space when using the word paste的清理方法,您是否尝试过设置:

paste_auto_cleanup_on_paste: true

由于这个示例中已经设置,除此之外,我不知道为什么事件没有触发。

最好的问候

Håkan


1

我知道这是旧的,但是将这些CSS属性添加到您的编辑器元素中可以解决问题。这不是一个tinyMCE问题,而是可编辑的div如何工作的问题。

#editor {
    white-space: pre-wrap;
    word-wrap: break-word;
}

干杯


0

我通过在defaultContent选项中添加一个零宽度空格('&#8203;')来解决这个问题。TinyMCE代码似乎会用段落标签替换非断行空格周围的空内容。

通过添加零宽度空格,它在用户界面中看起来是空的,但是可以防止这种后处理。

例如:

let options = { 
   // [...]
   defaultContent: '&#8203;'
   // [...]
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接