浏览代码

Toggle text watching on isEnabled state change.

Maciej Gołaszewski 5 年之前
父节点
当前提交
5e6c0502d9
共有 1 个文件被更改,包括 10 次插入10 次删除
  1. 10 10
      packages/ckeditor5-typing/src/textwatcher.js

+ 10 - 10
packages/ckeditor5-typing/src/textwatcher.js

@@ -34,6 +34,16 @@ export default class TextWatcher {
 
 
 		this.set( 'isEnabled', true );
 		this.set( 'isEnabled', true );
 
 
+		// Toggle text watching on isEnabled state change.
+		this.on( 'change:isEnabled', () => {
+			if ( this.isEnabled ) {
+				this._startListening();
+			} else {
+				this.stopListening( model.document.selection );
+				this.stopListening( model.document );
+			}
+		} );
+
 		this._startListening();
 		this._startListening();
 	}
 	}
 
 
@@ -47,11 +57,6 @@ export default class TextWatcher {
 		const document = model.document;
 		const document = model.document;
 
 
 		this.listenTo( document.selection, 'change:range', ( evt, { directChange } ) => {
 		this.listenTo( document.selection, 'change:range', ( evt, { directChange } ) => {
-			// Evaluate text before a selection only when TextWatcher is enabled.
-			if ( !this.isEnabled ) {
-				return;
-			}
-
 			// Indirect changes (i.e. when the user types or external changes are applied) are handled in the document's change event.
 			// Indirect changes (i.e. when the user types or external changes are applied) are handled in the document's change event.
 			if ( !directChange ) {
 			if ( !directChange ) {
 				return;
 				return;
@@ -71,11 +76,6 @@ export default class TextWatcher {
 		} );
 		} );
 
 
 		this.listenTo( document, 'change:data', ( evt, batch ) => {
 		this.listenTo( document, 'change:data', ( evt, batch ) => {
-			// Evaluate text before a selection only when TextWatcher is enabled.
-			if ( !this.isEnabled ) {
-				return;
-			}
-
 			if ( batch.type == 'transparent' ) {
 			if ( batch.type == 'transparent' ) {
 				return;
 				return;
 			}
 			}