簡體   English   中英

TinyMCE粘貼為純文本

[英]TinyMCE Paste As Plain Text

這是網上RTE的常見問題之一。 你能指導我如何:

  1. 粘貼為PLAIN TEXT
  2. 保留HTML但刪除WORD / HTML樣式

我想直接在paste(paste_preprocess回調)上執行它,而不打開Paste插件提供的對話框。

有什么想法/經歷嗎?

謝謝,

伊姆蘭

這是我做粘貼純文本的方法。

1. paste_preprocess設置(在tinymce init中)

paste_preprocess : function(pl, o) {
  //example: keep bold,italic,underline and paragraphs
  //o.content = strip_tags( o.content,'<b><u><i><p>' );

  // remove all tags => plain text
  o.content = strip_tags( o.content,'' );
},

2. function strip_tags(在主文檔上)

// Strips HTML and PHP tags from a string 
// returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
// example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>');
// returns 2: '<p>Kevin van Zonneveld</p>'
// example 3: strip_tags("<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>", "<a>");
// returns 3: '<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>'
// example 4: strip_tags('1 < 5 5 > 1');
// returns 4: '1 < 5 5 > 1'
function strip_tags (str, allowed_tags)
{

    var key = '', allowed = false;
    var matches = [];    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = ''; 
    var replacer = function (search, replace, str) {
        return str.split(search).join(replace);
    };
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi);
    }
    str += '';

    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
                // IE7 Hack
            continue;
        }

        // Save HTML tag
        html = matches[key].toString();
        // Is tag not in allowed list? Remove from str!
        allowed = false;

        // Go through all allowed tags
        for (k in allowed_array) {            // Init
            allowed_tag = allowed_array[k];
            i = -1;

            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}

            // Determine
            if (i == 0) {                allowed = true;
                break;
            }
        }
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
    return str;
}

實際上,你現在可以這樣做:

plugins: 'paste',
...
paste_auto_cleanup_on_paste : true,
paste_remove_styles: true,
paste_remove_styles_if_webkit: true,
paste_strip_class_attributes: true,

歸功於: http//www.miuaiga.com/index.cfm/2010/1/7/New-TinyMCE-lets-you-paste-as-plain-text-automatically

現在有一個新選項取代了以上所有內容:

tinymce.init({
   paste_as_text: true
});

請參閱http://www.tinymce.com/wiki.php/Configuration:paste_as_text

或者在settings.py中的django-tinymce中:

TINYMCE_DEFAULT_CONFIG = {
   'paste_as_text': True,
}

正在尋找這個..對於TinyMCE,您可以使用內置粘貼作為文本行為。 只需使用以下值設置tinymce init即可。

資料來源:jerome.chevreau, http ://www.tinymce.com/forum/viewtopic.php?id = 6788

//add paste plugin
plugins : 'paste',
//Keeps Paste Text feature active until user deselects the Paste as Text button
paste_text_sticky : true,
//select pasteAsPlainText on startup
setup : function(ed) {
    ed.onInit.add(function(ed) {
        ed.pasteAsPlainText = true;
    });
}

我用過這個:

    oninit: function (ed) {
        ed.pasteAsPlainText = true;
    }

隨着

paste_text_sticky: true

我使用過@Thariama解決方案,但我遇到了一個問題。

在paste_preprocess函數中:

paste_preprocess : function(pl, o) {
  o.content = StripTags( o.content,'' );
  console.log(o.content);
},

Tinymce將字符串返回為:

原始字符串:

"<h1>History.js Test Suite</h1>
        <p>HTML5 Browsers must pass the HTML4+HTML5 tests, HTML4 Browsers must pass the HTML4 tests and should fail the HTML5 tests.</p>"

返回字符串:

&lt;h1&gt;History.js Test Suite&lt;/h1&gt;<br /> <br />&lt;p&gt;HTML5 Browsers must pass the HTML4+HTML5 tests, HTML4 Browsers must pass the HTML4 tests and should fail the HTML5 tests.&lt;/p&gt;

我發現最好的兩個字符串。

var $str1 = '&lt;h1&gt;History.js Test Suite&lt;/h1&gt;<br /> <br />&lt;p&gt;HTML5 Browsers must pass the HTML4+HTML5 tests, HTML4 Browsers must pass the HTML4 tests and should fail the HTML5 tests.&lt;/p&gt;';


function StripTags(string) {

  var decoded_string = $("<div/>").html(string).text();
  return $("<div/>").html(decoded_string).text();

}

console.log(StripTags($str1));

輸出:

History.js Test Suite HTML5 Browsers must pass the HTML4+HTML5 tests, HTML4 Browsers must pass the HTML4 tests and should fail the HTML5 tests.

參考鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM