2016-08-11 14 views
1

我有一個字計數DIV其顯示字計數,但不使用插件的wordCount但使用正則表達式來算的話TinyMCE的外面。獲取單詞的計數TinyMCE的

但是,當我添加項目符號或將粗體應用於已輸入的文本時,此計數不會顯示正確的值[它顯示計數爲3,而在使用項目符號時只輸入一個單詞並將計數增加2同時突出顯示已經輸入的文字]

任何人都可以建議在正則表達式中如何使用粗體或斜體下劃線或項目符號或使用wordCount插件在stauts欄外使用它的輸出時,在這種情況下,我的字數DIV]

下面是代碼:

tinymceConfig = { 
mode:"exact", 
elements:"essay", 
menubar: false, 
statusbar: false, 
plugins: "autoresize", 
content_css : '../../theme/css/Language/editor.css', 
toolbar : "bold italic underline bullist", 
resize:"height", 
autoresize_max_height: 325, 
setup : function(editor) { 
    if ($('#essay').prop('readonly')) { 
     editor.settings.readonly = true; 
    } 

    editor.on('keydown', function (evt) { 
     var wordCount = 0; 
     var valid_keys = [8, 46]; 
     text = editor.getContent().replace(/(< ([^>]+)<)/g, '').replace(/\s+/g, ' '); 
     text = text.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); 
     wordCount = text.split(' ').length-1; 

     if(wordCount >= Helpers.constants.MAX_WORDS && valid_keys.indexOf(evt.keyCode) == -1) 
     { 
      evt.preventDefault(); 
      Helpers.prompt('You have reached the maximum word limit.'); 
      //evt.stopPropagation(); 
      return false; 
     } 
    }); 

    editor.on('keyup', function (evt) { 
     var text = ''; 
     clearTimeout(saveEssayIntervalId); 
     saveEssayIntervalId = setTimeout(function() { 
      saveEssay('silent'); 
     }, 3000); 

     text = editor.getContent().replace(/(< ([^>]+)<)/g, '').replace(/\s+/g, ' '); 
     text = text.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); 
     var wordCount = text.split(' ').length; 

     $("#essayContainer .textAreaAfter").html("[ Words entered: "+wordCount+" ]"); 
    }); 
} }; 
tinyMCE.init(tinymceConfig); 

回答

2

您可以從TinyMCE的WordCount插件獲取當前的字數 - 您不需要自己計算。

theEditor = tinymce.activeEditor; 
wordCount = theEditor.plugins.wordcount.getCount(); 
0

當我做邁克爾的回答,我得到:

Uncaught TypeError: tinyMCE.activeEditor.plugins.wordcount.getCount is not a function

+0

和使用TinyMCE的(全部小寫)同樣的問題...我得到這個有很多與它好像有TinyMCE的相關的問題是不同版本之間的代碼之間的主要區別。 –

+0

var wordCount = tinyMCE.get(「your_editor_id」))。plugins.wordcount.getCount(); – Yovav

1

如果你有一箇舊tinyMCE的版本,你可能沒有getCount()功能,在這種情況下,你可以寫,爲活動編輯(否則通過編輯器的對象):

var editor = tinyMCE.activeEditor, 
    words = editor.plugins.wordcount._getCount(editor);