Rus*_*ust 10 javascript jquery tinymce charactercount
标题说明了一切.如何让TinyMCE显示字符数而不是字数?
Mik*_*mov 16
wordcount插件现在可以计算和显示字符:
单击状态栏中的字数统计可在字数和字符数之间切换。
默认模式是“words”,但很容易模拟点击状态栏来切换它。
按照以下方式更改您的编辑器配置:
tinymce.init({
plugins: "wordcount",
// ...
init_instance_callback: function (editor) {
$(editor.getContainer()).find('button.tox-statusbar__wordcount').click(); // if you use jQuery
}
});
Run Code Online (Sandbox Code Playgroud)
就这样。你现在有字符数了。
编写自己的插件.
以下解决方案基于本文.该charactercount插件计算用户看到的实际字符,忽略所有HTML和隐藏字符.
字符数插件:
tinymce.PluginManager.add('charactercount', function (editor) {
var self = this;
function update() {
editor.theme.panel.find('#charactercount').text(['Characters: {0}', self.getCount()]);
}
editor.on('init', function () {
var statusbar = editor.theme.panel && editor.theme.panel.find('#statusbar')[0];
if (statusbar) {
window.setTimeout(function () {
statusbar.insert({
type: 'label',
name: 'charactercount',
text: ['Characters: {0}', self.getCount()],
classes: 'charactercount',
disabled: editor.settings.readonly
}, 0);
editor.on('setcontent beforeaddundo', update);
editor.on('keyup', function (e) {
update();
});
}, 0);
}
});
self.getCount = function () {
var tx = editor.getContent({ format: 'raw' });
var decoded = decodeHtml(tx);
// here we strip all HTML tags
var decodedStripped = decoded.replace(/(<([^>]+)>)/ig, "").trim();
var tc = decodedStripped.length;
return tc;
};
function decodeHtml(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
});
Run Code Online (Sandbox Code Playgroud)
CSS调整:
/* Optional: Adjust the positioning of the character count text. */
label.mce-charactercount {
margin: 2px 0 2px 2px;
padding: 8px;
}
/* Optional: Remove the html path code from the status bar. */
.mce-path {
display: none !important;
}
Run Code Online (Sandbox Code Playgroud)
TinyMCE初始化(使用jQuery)
$('textarea.tinymce').tinymce({
plugins: "charactercount",
statusbar: true,
init_instance_callback: function (editor) {
$('.mce-tinymce').show('fast');
$(editor.getContainer()).find(".mce-path").css("display", "none");
}
// ...
});
Run Code Online (Sandbox Code Playgroud)
PS.使用JS minifier.
init_instance_callback: function (editor) {
editor.on('change', function (e) {
var length = editor.contentDocument.body.innerText.length;
});
}
Run Code Online (Sandbox Code Playgroud)
在init上添加这个。length 是您的字符长度。现在您需要隐藏字数并附加一个带有字符计数器的新字符串。