【问题标题】:Get the count of words in tinymce获取tinymce中的字数
【发布时间】:2016-12-18 13:18:42
【问题描述】:

我在 tinymce 之外有一个字数 div,它显示字数,但不使用 wordCount 插件,而是使用正则表达式来计算字数。

但是当我添加项目符号或对已键入的文本应用粗体时,此计数未显示正确的值[它显示计数为 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);

【问题讨论】:

    标签: javascript regex tinymce word-count


    【解决方案1】:

    您可以从 TinyMCE 的 WordCount 插件获取当前字数 - 您不需要自己计算。

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多