Word Count plugin
The Word Count plugin adds functionality for counting words to the TinyMCE editor.
When loaded, the Word Count plugin:
-
places a word and character counter in the status bar.
-
By default the counter appears adjacent the Powered by Tiny product attribution logo.
-
Clicking the counter in the status bar switches the display between word count and character count.
-
-
adds a Word count command to the Tools menu.
-
choosing this command presents a Word Count dialog showing the word and character count for the current document. If there is a current selection, the word and character count for the selection is also shown.
-
if
wordcount
is also added to the toolbar configuration, a Word Count toolbar item presents that functions equivalently to the Tools → Word count menu command.
-
Word count logic
The Word count plugin counts ‘words’ by delimiting strings at pre-defined word boundaries.
Word boundary characers
Some characters treated as word boundaries
Name | Example and notes |
---|---|
Space |
This includes space character entities such as |
Em-dashes |
— |
En-dashes |
– |
Question marks |
? |
Commas |
, |
Semi-colons |
; |
Exclamation marks |
! |
Solidus (aka forward slash) |
/ |
NOTE: This feature is only available for TinyMCE 6.5 and later. |
|
Circumflex |
^ |
Numero sign |
№ |
Tilde |
~ |
Plus sign |
+ |
Vertical bar (aka pipe character) |
| |
Dollar sign |
$ |
Grave accent |
` |
Some characters not treated as word boundaries
Name | Example and notes |
---|---|
Hyphens |
- |
Fraction slash |
⁄ |
Equals sign |
= |
Full-stops (aka periods) |
. Note that space characters are word boundaries. Practically speaking, it is full-stops embedded between letters without a space either side that don’t function as word boundary characters. |
Colons |
: Note that space characters are word boundaries. Practically speaking, it is colons embedded between letters without a space either side that don’t function as word boundary characters. |
Empty block elements |
Also, space characters inside otherwise empty elements — for example |
Character count logic
The Word Count plugin counts glyphs.
The Word Count plugin does not count characters added for document management purposes.
Consider an end-user enters the following:
1↵
↵
2↵
↵
That is, they enter a numeric one, followed by two hard returns, and then a numeric two, followed by a further two hard returns.
By default, TinyMCE stores this entered data as follows:
<p>1</p>
<p> </p>
<p>2</p>
<p> </p>
The character count for this data is 2.
The Word Count does not add either of the
characters placed by TinyMCE to the document character count.
Basic setup
tinymce.init({
selector: 'textarea', // change this value according to your HTML
plugins: 'wordcount',
toolbar: 'wordcount'
});
Toolbar buttons
The Word Count plugin provides the following toolbar buttons:
Toolbar button identifier | Description |
---|---|
|
Opens a word count dialog showing word and character counts. |
These toolbar buttons can be added to the editor using:
-
The
toolbar
configuration option. -
The
quickbars_insert_toolbar
configuration option.
Menu items
The Word Count plugin provides the following menu items:
Menu item identifier | Default Menu Location | Description |
---|---|---|
|
Tools |
Opens a word count dialog showing word and character counts. |
These menu items can be added to the editor using:
-
The
menu
configuration option. -
The
contextmenu
configuration option.
Commands
The Word Count plugin provides the following TinyMCE command.
Command | Description |
---|---|
mceWordCount |
Opens the Word Count summary dialog. |
tinymce.activeEditor.execCommand('mceWordCount');
API
The Word Count plugin exposes an API for retrieving the word and character count of either the whole document or the current editor selection. Following is an example of how to retrieve each property.
Example: using the wordcount
plugin APIs
const wordcount = tinymce.activeEditor.plugins.wordcount;
console.log(wordcount.body.getWordCount());
console.log(wordcount.body.getCharacterCount());
console.log(wordcount.body.getCharacterCountWithoutSpaces());
console.log(wordcount.selection.getWordCount());
console.log(wordcount.selection.getCharacterCount());
console.log(wordcount.selection.getCharacterCountWithoutSpaces());