计算单词的时间

Get the count of words in tinymce

本文关键字:时间 单词 计算      更新时间:2023-09-26

我在tinymce外有一个单词计数div,它显示单词计数,但不使用wordCount插件,而是使用regex来计数单词。

但是当我在已经键入的文本中添加项目符号或应用粗体时,这个计数没有显示正确的值[它显示计数为3,而我在使用项目符号时只输入了一个单词,并在突出显示已经键入的文本时增加计数2]

有没有人可以建议在正则表达式中做些什么来获得正确的计数,当使用粗体或,斜体,下划线或子弹或使用wordCount插件使用它的输出在统计栏外[在这种情况下,在我的单词计数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);

你可以从TinyMCE的WordCount插件中获得当前的单词计数-你不需要自己计算这个。

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

如果您有一个旧的tinyMCE版本,您可能没有getCount()函数,在这种情况下,您可以为活动编辑器编写(否则传递编辑器的对象):

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