TinyMCE,显示字符数而不是字数

TinyMCE, show character count instead of word count

标题说明了一切。如何让 TinyMCE 显示字符数而不是字数?

编写自己的插件。

以下解决方案基于this articlecharactercount 插件计算用户看到的实际字符,所有 HTML 和隐藏字符都将被忽略。每个 "key up" 事件都会更新该数字。

字符计数插件:

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;
  }
});

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;
}

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");
  }
  // ...
});

ps。使用 JS 压缩器。

    init_instance_callback: function (editor) {
editor.on('change', function (e) {
                var length = editor.contentDocument.body.innerText.length;
            });
}

在初始化时添加这个。 length 是你的字符长度。现在您需要隐藏字数并附加一个带有字符计数器的新字符串。

wordcount 插件现在可以统计和显示字符:

Clicking Word Count in the status bar switches between counting words and characters.

默认模式是"words",但很容易模拟点击状态栏来切换它。

按照以下方式更改您的编辑器配置:

tinymce.init({
   plugins: "wordcount",

   // ... 

   init_instance_callback: function (editor) {
      $(editor.getContainer()).find('button.tox-statusbar__wordcount').click();  // if you use jQuery
   }
});

就是这样。您现在有字符数了。

我能够通过创建银色主题的自定义版本将 wordcount 插件设置为默认显示字符。看起来在 TinyMCE 5.1.6 中插件呈现的方式是在主题文件中设置的。 TinyMCE 配置:

{
    selector: '.tinymce',
    theme: 'silver-custom',
    ...
}

主题文件是themes/silver/theme.js的副本,需要在TinyMCE之后加载。

变化:

 ...
 function Theme () {
      global.add('silver-custom', function (editor) {
 ...

...
var renderWordCount = function (editor, providersBackstage) {
    ...
    store: {
        mode: 'memory',
        initialValue: {
        mode: 'characters',
        ...
}
...

我从 2017 年开始就在生产中使用这个 https://gist.github.com/imanilchaudhari/5a121ea6420eb4b8aa7ee70a5f7074e3。这个插件很好。

一开始以为charwordcount是tinymce自带的插件,后来发现是自定义的

底部状态栏会显示字符数