gpt4 book ai didi

javascript - 获取tinymce中的单词数

转载 作者:行者123 更新时间:2023-11-29 17:56:09 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于javascript - 获取tinymce中的单词数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38902493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com