javascript - Get the count of words in tinymce - Stack Overflow

I have a word count div outside the tinymce which shows the word count but not using the wordCount plug

I have a word count div outside the tinymce which shows the word count but not using the wordCount plugin but using regex to count the words.

But this count is not showing correct value when i add bullet or apply bold to the already typed text[It is showing count as 3 while i have entered only one word at the time of using bullet and increases the count by 2 while highlighting the already typed text]

Can any one can suggest what to do in the regex to get correct count when using the bold or,italic,underline or bullets or using wordCount plugin to use it's output outside the stauts bar[In this case in my word count div]

Here is the code :

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);

I have a word count div outside the tinymce which shows the word count but not using the wordCount plugin but using regex to count the words.

But this count is not showing correct value when i add bullet or apply bold to the already typed text[It is showing count as 3 while i have entered only one word at the time of using bullet and increases the count by 2 while highlighting the already typed text]

Can any one can suggest what to do in the regex to get correct count when using the bold or,italic,underline or bullets or using wordCount plugin to use it's output outside the stauts bar[In this case in my word count div]

Here is the code :

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);
Share Improve this question asked Aug 11, 2016 at 17:17 Bhavesh TilvaniBhavesh Tilvani 1931 silver badge11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can get the current word count from TinyMCE's WordCount plugin - you should not need to calculate this yourself.

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

If you have an old tinyMCE version, you may not have the getCount() function, in this case you can write, for the active editor (else pass on the editor's object):

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742296841a4417294.html

相关推荐

  • javascript - Get the count of words in tinymce - Stack Overflow

    I have a word count div outside the tinymce which shows the word count but not using the wordCount plug

    23小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信