tinymce count characters

Solutions on MaxInterview for tinymce count characters by the best coders in the world

showing results for - "tinymce count characters"
Sylvester
12 Feb 2020
1tinymce.PluginManager.add('charactercount', function (editor) {
2  var self = this;
3
4  function update() {
5    editor.theme.panel.find('#charactercount').text(['Characters: {0}', self.getCount()]);
6  }
7
8  editor.on('init', function () {
9    var statusbar = editor.theme.panel && editor.theme.panel.find('#statusbar')[0];
10
11    if (statusbar) {
12      window.setTimeout(function () {
13        statusbar.insert({
14          type: 'label',
15          name: 'charactercount',
16          text: ['Characters: {0}', self.getCount()],
17          classes: 'charactercount',
18          disabled: editor.settings.readonly
19        }, 0);
20
21        editor.on('setcontent beforeaddundo', update);
22
23        editor.on('keyup', function (e) {
24            update();
25        });
26      }, 0);
27    }
28  });
29
30  self.getCount = function () {
31    var tx = editor.getContent({ format: 'raw' });
32    var decoded = decodeHtml(tx);
33    // here we strip all HTML tags
34    var decodedStripped = decoded.replace(/(<([^>]+)>)/ig, "").trim();
35    var tc = decodedStripped.length;
36    return tc;
37  };
38
39  function decodeHtml(html) {
40    var txt = document.createElement("textarea");
41    txt.innerHTML = html;
42    return txt.value;
43  }
44});
45