@@ -25,6 +25,9 @@ export class CharacterCount extends ConfigurableComponent {
2525 /** @private */
2626 $textarea
2727
28+ /** @private */
29+ count = 0
30+
2831 /** @private */
2932 $visibleCountMessage
3033
@@ -173,11 +176,15 @@ export class CharacterCount extends ConfigurableComponent {
173176 // When the page is restored after navigating 'back' in some browsers the
174177 // state of form controls is not restored until *after* the DOMContentLoaded
175178 // event is fired, so we need to sync after the pageshow event.
176- window . addEventListener ( 'pageshow' , ( ) => this . updateCountMessage ( ) )
179+ window . addEventListener ( 'pageshow' , ( ) => {
180+ this . updateCount ( )
181+ this . updateCountMessage ( )
182+ } )
177183
178184 // Although we've set up handlers to sync state on the pageshow event, init
179185 // could be called after those events have fired, for example if they are
180186 // added to the page dynamically, so update now too.
187+ this . updateCount ( )
181188 this . updateCountMessage ( )
182189 }
183190
@@ -190,22 +197,23 @@ export class CharacterCount extends ConfigurableComponent {
190197 * @private
191198 */
192199 bindChangeEvents ( ) {
193- this . $textarea . addEventListener ( 'keyup ' , ( ) => this . handleKeyUp ( ) )
200+ this . $textarea . addEventListener ( 'input ' , ( ) => this . handleInput ( ) )
194201
195202 // Bind focus/blur events to start/stop polling
196203 this . $textarea . addEventListener ( 'focus' , ( ) => this . handleFocus ( ) )
197204 this . $textarea . addEventListener ( 'blur' , ( ) => this . handleBlur ( ) )
198205 }
199206
200207 /**
201- * Handle key up event
208+ * Handle input event
202209 *
203210 * Update the visible character counter and keep track of when the last update
204211 * happened for each keypress
205212 *
206213 * @private
207214 */
208- handleKeyUp ( ) {
215+ handleInput ( ) {
216+ this . updateCount ( )
209217 this . updateVisibleCountMessage ( )
210218 this . lastInputTimestamp = Date . now ( )
211219 }
@@ -281,7 +289,7 @@ export class CharacterCount extends ConfigurableComponent {
281289 * @private
282290 */
283291 updateVisibleCountMessage ( ) {
284- const remainingNumber = this . maxLength - this . count ( this . $textarea . value )
292+ const remainingNumber = this . maxLength - this . count
285293 const isError = remainingNumber < 0
286294
287295 // If input is over the threshold, remove the disabled class which renders
@@ -325,19 +333,20 @@ export class CharacterCount extends ConfigurableComponent {
325333
326334 /**
327335 * Count the number of characters (or words, if `config.maxwords` is set)
328- * in the given text
336+ * in the given text, and update the component-wide count
329337 *
330338 * @private
331- * @param {string } text - The text to count the characters of
332- * @returns {number } the number of characters (or words) in the text
333339 */
334- count ( text ) {
340+ updateCount ( ) {
341+ const text = this . $textarea . value
342+
335343 if ( this . config . maxwords ) {
336344 const tokens = text . match ( / \S + / g) ?? [ ] // Matches consecutive non-whitespace chars
337- return tokens . length
345+ this . count = tokens . length
346+ return
338347 }
339348
340- return text . length
349+ this . count = text . length
341350 }
342351
343352 /**
@@ -347,7 +356,7 @@ export class CharacterCount extends ConfigurableComponent {
347356 * @returns {string } Status message
348357 */
349358 getCountMessage ( ) {
350- const remainingNumber = this . maxLength - this . count ( this . $textarea . value )
359+ const remainingNumber = this . maxLength - this . count
351360 const countType = this . config . maxwords ? 'words' : 'characters'
352361 return this . formatCountMessage ( remainingNumber , countType )
353362 }
@@ -392,7 +401,7 @@ export class CharacterCount extends ConfigurableComponent {
392401 }
393402
394403 // Determine the remaining number of characters/words
395- const currentLength = this . count ( this . $textarea . value )
404+ const currentLength = this . count
396405 const maxLength = this . maxLength
397406
398407 const thresholdValue = ( maxLength * this . config . threshold ) / 100
0 commit comments