8
0
Просмотр исходного кода

Proposed an alternative solution to obtain the actual numbers of words and characters in the document on demand.

Aleksander Nowodzinski 6 лет назад
Родитель
Сommit
c27203e2c1

+ 45 - 7
packages/ckeditor5-word-count/src/wordcount.js

@@ -69,6 +69,22 @@ export default class WordCount extends Plugin {
 		 */
 		this.set( 'words', 0 );
 
+		// Don't wait for #update event to set the value of the properties but obtain it right away.
+		// This way, accessing the properties directly returns precise numbers, e.g. for validation, etc..
+		// If not accessed directly, the properties will be refreshed upon #update anyway.
+		Object.defineProperties( this, {
+			characters: {
+				get() {
+					return ( this.characters = this._getCharacters() );
+				}
+			},
+			words: {
+				get() {
+					return ( this.words = this._getWords() );
+				}
+			}
+		} );
+
 		/**
 		 * Label used to display words value in {@link #wordCountContainer output container}
 		 *
@@ -238,6 +254,31 @@ export default class WordCount extends Plugin {
 		return this._outputView.element;
 	}
 
+	/**
+	 * Determines the number of characters in the current editor's model.
+	 *
+	 * @private
+	 * @returns {Number}
+	 */
+	_getCharacters() {
+		const txt = modelElementToPlainText( this.editor.model.document.getRoot() );
+
+		return txt.replace( /\n/g, '' ).length;
+	}
+
+	/**
+	 * Determines the number of words in the current editor's model.
+	 *
+	 * @private
+	 * @returns {Number}
+	 */
+	_getWords() {
+		const txt = modelElementToPlainText( this.editor.model.document.getRoot() );
+		const detectedWords = txt.match( this._wordsMatchRegExp ) || [];
+
+		return detectedWords.length;
+	}
+
 	/**
 	 * Determines the number of words and characters in the current editor's model and assigns it to {@link #characters} and {@link #words}.
 	 * It also fires the {@link #event:update}.
@@ -246,15 +287,12 @@ export default class WordCount extends Plugin {
 	 * @fires update
 	 */
 	_refreshStats() {
-		const txt = modelElementToPlainText( this.editor.model.document.getRoot() );
-		const detectedWords = txt.match( this._wordsMatchRegExp ) || [];
-
-		this.characters = txt.replace( /\n/g, '' ).length;
-		this.words = detectedWords.length;
+		const words = this.words = this._getWords();
+		const characters = this.characters = this._getCharacters();
 
 		this.fire( 'update', {
-			words: this.words,
-			characters: this.characters
+			words,
+			characters
 		} );
 	}
 }

+ 8 - 0
packages/ckeditor5-word-count/tests/manual/wordcount.js

@@ -28,6 +28,14 @@ ClassicEditor
 		document.getElementById( 'destroy-editor' ).addEventListener( 'click', () => {
 			editor.destroy();
 		} );
+
+		editor.plugins.get( 'WordCount' ).on( 'change:words', ( evt, name, value ) => {
+			console.log( 'WordCount:change:words', value );
+		} );
+
+		editor.plugins.get( 'WordCount' ).on( 'change:characters', ( evt, name, value ) => {
+			console.log( 'WordCount:change:characters', value );
+		} );
 	} )
 	.catch( err => {
 		console.error( err.stack );