Stat Plugin
- Features
- showCharsCounter
- countHTMLChars
- countTextSpaces
- showWordsCounter
- Show Both Counters (Default)
- Show Only Character Counter
- Show Only Word Counter
- Count HTML Characters
- Count Text with Spaces
- Count Text without Spaces
- Disable All Counters
- Full Configuration
- Initialization
- ReInit Process
- Calculation (Throttled)
- Update Triggers
- Throttling
- Multi-Place Support
- Internationalization
- Counting Logic
- Cleanup
- Edge Cases
- Notes
- Typical Use Case
- Classes
Displays real-time statistics in the status bar showing character count and word count. This plugin helps users track document length and stay within limits for content requirements.
Features
- Character counter in status bar
- Word counter in status bar
- Multiple counting modes:
- Text characters only (default)
- HTML characters (includes tags)
- Text with/without spaces
- Real-time updates on content change
- Throttled calculations for performance
- Multi-place editor support
- Internationalized labels
- Configurable display options
showCharsCounter
Type: boolean
Default: true
When enabled, displays character count in the status bar. Shows text-only characters by default (without HTML tags).
Example:
const editor = Jodit.make('#editor', {
showCharsCounter: true
});
// Status bar shows: "Chars: 11" for "<div>Hello world!</div>"
countHTMLChars
Type: boolean
Default: false
When enabled, counts all characters including HTML tags and attributes. When disabled (default), only counts visible text characters.
Example:
// Count only text characters (default)
const editor1 = Jodit.make('#editor1', {
countHTMLChars: false
});
// "<div>Hello world!</div>" = 11 chars (text only)
// Count all HTML characters
const editor2 = Jodit.make('#editor2', {
countHTMLChars: true
});
// "<div>Hello world!</div>" = 24 chars (including tags)
countTextSpaces
Type: boolean
Default: false
When enabled, includes spaces in the character count. When disabled (default), spaces are excluded from count. Only applies when countHTMLChars
is false
.
Example:
// Don't count spaces (default)
const editor1 = Jodit.make('#editor1', {
countTextSpaces: false
});
// "Hello world!" = 11 chars (no space)
// Count spaces
const editor2 = Jodit.make('#editor2', {
countTextSpaces: true
});
// "Hello world!" = 12 chars (includes space)
showWordsCounter
Type: boolean
Default: true
When enabled, displays word count in the status bar. Words are separated by whitespace.
Example:
const editor = Jodit.make('#editor', {
showWordsCounter: true
});
// Status bar shows: "Words: 2" for "Hello world!"
Show Both Counters (Default)
const editor = Jodit.make('#editor', {
showCharsCounter: true,
showWordsCounter: true
});
// Status bar displays:
// "Chars: 11 Words: 2"
Show Only Character Counter
const editor = Jodit.make('#editor', {
showCharsCounter: true,
showWordsCounter: false
});
// Status bar displays only:
// "Chars: 11"
Show Only Word Counter
const editor = Jodit.make('#editor', {
showCharsCounter: false,
showWordsCounter: true
});
// Status bar displays only:
// "Words: 2"
Count HTML Characters
const editor = Jodit.make('#editor', {
showCharsCounter: true,
countHTMLChars: true
});
// For content: <div>Hello world!</div>
// Status bar shows: "Chars: 24" (includes <div> tags)
Count Text with Spaces
const editor = Jodit.make('#editor', {
showCharsCounter: true,
countHTMLChars: false,
countTextSpaces: true
});
// For content: <p>Hello world!</p>
// Status bar shows: "Chars: 12" (includes space between words)
Count Text without Spaces
const editor = Jodit.make('#editor', {
showCharsCounter: true,
countHTMLChars: false,
countTextSpaces: false
});
// For content: <p>Hello world!</p>
// Status bar shows: "Chars: 11" (excludes space)
Disable All Counters
const editor = Jodit.make('#editor', {
showCharsCounter: false,
showWordsCounter: false
});
// Status bar has no stat counters
Full Configuration
const editor = Jodit.make('#editor', {
showCharsCounter: true,
countHTMLChars: true,
countTextSpaces: true,
showWordsCounter: true
});
// Shows both counters with HTML char counting
// "Chars: 24 Words: 2" for <div>Hello world!</div>
Initialization
On plugin afterInit
:
- Creates two
<span>
elements for counters - Stores references in
charCounter
andwordCounter
- Registers events:
afterInit
,changePlace
,afterAddPlace
→reInit()
- Calls
reInit()
to set up initial state
ReInit Process
The reInit()
method:
- If
showCharsCounter
enabled: appendscharCounter
to status bar - If
showWordsCounter
enabled: appendswordCounter
to status bar - Removes old event listeners on
change
andkeyup
- Registers new throttled
calc
handler forchange
andkeyup
- Calls
calc()
to compute initial values
Calculation (Throttled)
The calc()
method (throttled by defaultTimeout
):
Character Count:
- If
countHTMLChars
is true:- Uses
jodit.value
(full HTML) - Counts all characters including tags
- Uses
- If
countHTMLChars
is false:- Uses
jodit.text
(text only) - If
countTextSpaces
is true:- Removes invisible spaces (zero-width, etc.)
- Removes line breaks (
\r\n
) - Counts remaining characters
- If
countTextSpaces
is false:- Removes all spaces (via
SPACE_REG_EXP
) - Counts remaining characters
- Removes all spaces (via
- Uses
- Updates
charCounter.textContent
with formatted string:"Chars: %d"
Word Count:
- Uses
jodit.text
(text only) - Removes invisible spaces
- Splits text by
SPACE_REG_EXP
(whitespace regex) - Filters out empty strings
- Counts array length
- Updates
wordCounter.textContent
with formatted string:"Words: %d"
Update Triggers
Counters recalculate on:
change
event (content modified)keyup
event (after user types)afterInit
event (editor initialized)changePlace
event (current place changed)afterAddPlace
event (new place added)
Throttling
The calc
method is throttled:
- Uses
jodit.async.throttle()
- Delay:
jodit.defaultTimeout
(typically 100-200ms) - Prevents excessive calculations during rapid typing
- Ensures smooth performance with large documents
Multi-Place Support
When editor has multiple places:
afterAddPlace
event triggersreInit()
- Counters added to each place's status bar
- Each place shows its own character/word count
- Calculations independent per place
Internationalization
Counter labels use jodit.i18n()
:
- Default English:
"Chars: %d"
,"Words: %d"
- Translatable via language files
%d
placeholder replaced with count
Counting Logic
Character Counting Modes:
Mode | countHTMLChars | countTextSpaces | Example Input | Count |
---|---|---|---|---|
Text without spaces | false |
false |
<p>Hi yo</p> |
4 |
Text with spaces | false |
true |
<p>Hi yo</p> |
5 |
HTML | true |
N/A | <p>Hi yo</p> |
12 |
Word Counting:
- Always based on text content (no HTML)
- Splits on whitespace (space, tab, newline)
- Empty strings filtered out
- Single word: 1, "Hello world": 2
Cleanup
On beforeDestruct
:
- Safely removes
charCounter
from DOM - Safely removes
wordCounter
from DOM - Unregisters all event listeners
- Nullifies counter references
Edge Cases
- Empty Content: Shows "Chars: 0 Words: 0"
- HTML Tags Only:
<div></div>
= 0 chars (text mode), 11 chars (HTML mode) - Multiple Spaces: Counted as one word separator
- Newlines: Not counted as characters in text mode
- Invisible Spaces: Zero-width spaces removed before counting
- Special Characters: Counted as characters (emoji, Unicode, etc.)
- Status Bar Disabled: Counters not shown if status bar disabled
- Multiple Places: Each place has independent counters
- Large Documents: Throttling prevents performance issues
- No Text: " " (only spaces) = 0 words
Notes
- Plugin is class-based, extends
Plugin
base class - No decorators used
- Counter elements:
<span>
created byjodit.c.span()
- Status bar append with
true
flag (prepend to right side) - The
calc
method throttled withdefaultTimeout
- Uses
INVISIBLE_SPACE_REG_EXP()
to remove zero-width spaces - Uses
SPACE_REG_EXP()
for whitespace matching - Word split regex handles space, tab, newline, etc.
- The
reInit()
method handles multi-place setup - Event listeners:
change
,keyup
,afterInit
,changePlace
,afterAddPlace
- Counters show formatted internationalized strings
- The plugin properly cleans up DOM elements on destruction
- Text extraction via
jodit.text
property (gets visible text) - HTML extraction via
jodit.value
property (gets full HTML) - Character count includes letters, numbers, punctuation
- Word count excludes empty strings from split result
- Throttle prevents recalculation on every keystroke
- Status bar elements appended in order: chars first, then words
- The
filter((e: string) => e.length)
removes empty array items - Line breaks removed via
.replace(/[\r\n]/g, '')
in text+spaces mode - Counter references stored as
Nullable<HTMLElement>
- The plugin supports dynamic place addition/removal
Typical Use Case
Content creators need to track document length for word limits, SEO requirements, or content guidelines. The stat plugin provides this by:
- Displaying real-time character count in status bar
- Displaying real-time word count in status bar
- Updating counts as user types
- Offering flexible counting modes (text vs HTML, with/without spaces)
- Supporting multi-language label translation
This improves user experience by:
- Helping users meet content length requirements
- Providing immediate feedback on document size
- Supporting multiple counting standards
- Preventing exceeding character limits
- No manual counting or external tools needed