19

The title says it all. How do I get TinyMCE to show character count instead of word count?

enter image description here

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77

5 Answers5

20

wordcount plugin now can count and show characters:

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

By default mode is "words", but it's pretty easy emulate click in status bar to switch it.

Alter your editor config following way:

tinymce.init({
   plugins: "wordcount",

   // ... 

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

That's all. You have character count now.

Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
  • 6
    It's a terrible hack, but it works. IMHO this should be an option in the plugin itself. I can't imagine why anyone would want to count words by default anyhow. – AsGoodAsItGets Oct 01 '19 at 16:10
  • 2
    It works, i didnt find better solution for v5, @naXa solutions dont work – Fori Oct 05 '19 at 16:47
  • 1
    Yeah.. But it works! And copy/paste done. I needed the character count because we take the body and send it out via SMS sometimes, so it's useful to know the # characters used. – Rob Bramhall Mar 31 '20 at 23:23
12

Write your own plugin.

The following solution is based on this article. The charactercount plugin counts the actual characters that the user sees, all HTML and hidden characters are ignored. The number is updated on every "key up" event.

Character Count Plugin:

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 Tweaks:

/* 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 Initialization (using 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. Use JS minifier.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
  • I tried using this sample. When I walked thru the code, the statusbar variable returned 'undefined' upon getting to the 'if (statusbar) {" line, so naturally that "if" block does not execute. When I tried expanding editor.theme, I could not find a panel object. I am using tinymce ver 5.0.14 because our security people say this is the correct version I need to be using. What could be wrong? – Mark Nov 27 '20 at 00:15
  • @Mark please [ask a new question](https://stackoverflow.com/questions/ask). And Stack Overflow users will help you. I advise preparing JSFiddle ([example](https://jsfiddle.net/textboxio/tx4Lc8a9/4/)), that clearly demonstrates how to reproduce your problem, and inserting a link to the JSFiddle in your question. – naXa stands with Ukraine Nov 27 '20 at 09:38
8
    init_instance_callback: function (editor) {
editor.on('change', function (e) {
                var length = editor.contentDocument.body.innerText.length;
            });
}

On init add this. length is your character length. Now you need to hide word count and attach a new string with character counter.

DanielWaw
  • 669
  • 7
  • 22
3

I was able to set the wordcount plugin to display characters by default by creating a custom version of the silver theme. Looks like in TinyMCE 5.1.6 the way plugins are rendered is set in the theme file. TinyMCE config:

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

The theme file is a copy of the themes/silver/theme.js and needs to load after TinyMCE.

Changes:

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

and

...
var renderWordCount = function (editor, providersBackstage) {
    ...
    store: {
        mode: 'memory',
        initialValue: {
        mode: 'characters',
        ...
}
...
321zeno
  • 1,264
  • 1
  • 12
  • 24
0

I am using this one in production https://gist.github.com/imanilchaudhari/5a121ea6420eb4b8aa7ee70a5f7074e3 since 2017. this plugin is good.

I initially thought charword count comes with the tinymce builtin plugins, but later found that it is a custom one.

It will show number of character in the bottom status bar

enter image description here

Mohamed Hussain
  • 7,433
  • 14
  • 55
  • 85