Javascript 敲除数据绑定中的TinyMCE字数
我使用TinyMCE编辑器,希望将单词计数值指定给一个敲除变量。 设置编辑器是可以的,它会显示单词计数值,但我无法理解如何获得实际值 这是淘汰值的视图:Javascript 敲除数据绑定中的TinyMCE字数,javascript,jquery,knockout.js,tinymce,Javascript,Jquery,Knockout.js,Tinymce,我使用TinyMCE编辑器,希望将单词计数值指定给一个敲除变量。 设置编辑器是可以的,它会显示单词计数值,但我无法理解如何获得实际值 这是淘汰值的视图: <div data-bind="with: SelectedText"> No. of words is: <span data-bind="value: TextWordCount"></span> </div> 但是我如何将它与TinyMce字数连接起来呢 这是一把小提琴:我对你的小
<div data-bind="with: SelectedText">
No. of words is: <span data-bind="value: TextWordCount"></span>
</div>
但是我如何将它与TinyMce字数连接起来呢
这是一把小提琴:我对你的小提琴做了一些修改,使它可以工作: 首先,
<span data-bind="value: TextWordCount"></span>
必须改用文本绑定:
<span data-bind="text: TextWordCount"></span>
除此之外,在编辑器中键入文本时,您需要连接TinyMCE onKeyUp事件以获取实时更新
此外,克隆数据的代码也有问题
我将TextWordCount从可观察更改为可计算:
self.TextWordCount = ko.computed(function() {
// First remove all html tags
var text = self.TextbatchText().replace(/<[^>]*>/g, '');
// Tiny MCE inserts an extra at the end if you "double space" the end of your sentence. This replaces it with a normal space
text = text.replace(/ /g, ' ');
// This merges all spaces and tabs following each other into a single space
text = text.replace(/[\s\t]+/g, ' ');
// This removes spaces and the begin and end of the text
text = text.replace(/^\s*/, '').replace(/\s*$/, '');
// This splits the string into an array of words separated by a space.
return text.split(' ').length;
});
正如我所承诺的,尽管有点晚,我的解决方案如下。不需要重写或拼凑任何东西。我甚至还冒昧地将这一要求加入到我的最新版本中,您可以看到一个 简而言之,关键是下面的代码段
'setup': function( editor ) {
editor.on('change keyup nodechange', function( args ) {
var accessor = allBindings.get( 'wysiwygCount' );
if (editor.plugins['wordcount']) {
accessor(editor.plugins['wordcount'].getCount());
}
}
}
这不是那么容易。。。仅在/[\s\t]+/上拆分是不够的,因此只有在编辑器中没有使用格式时,您的解决方案才有效。请参见第一部分选择文本:这是第一个文本,然后单击工具栏上的对齐中心:单词数量从5变为7…下面是一个处理编辑器使用的格式的替代方法:nemesv,你的小提琴几乎可以工作了,除了TextWordCount在键入时不会更新,而且它将双空白作为一个单词计算。也许你可以解释一下逻辑表达式,因为它们对我来说是希腊语:What does/[\s\t]+/do,What does/<[^>]+Hallvar,tak代表svar:我真的不明白我应该如何把你的github代码和小提琴结合起来。在自定义绑定中粘贴代码时,仍然会计算双空格和格式化代码。Ingen问题先生!我用更好的字数计算代码更新了Fiddle,现在我测试它时,它似乎起作用了:-我担心,但是在wordcount插件的当前实现中:如果不修改插件或在您自己的代码中复制其功能,就无法将wordcount转换为可观察的字数…我不同意,有一种非常简单的方法可以使用editor.plugins['wordcount'].getCount从wordcount插件中提取值。我将很快提供一个说明这一点的答案。