Browse Source

Simplified block auto formatting.

Szymon Kupś 7 years ago
parent
commit
3ae41693d5
1 changed files with 22 additions and 21 deletions
  1. 22 21
      packages/ckeditor5-autoformat/src/blockautoformatediting.js

+ 22 - 21
packages/ckeditor5-autoformat/src/blockautoformatediting.js

@@ -64,34 +64,35 @@ export default class BlockAutoformatEditing {
 		}
 
 		editor.model.document.on( 'change', () => {
-			const changes = editor.model.document.differ.getChanges();
+			const changes = Array.from( editor.model.document.differ.getChanges() );
+			const entry = changes[ 0 ];
 
-			for ( const entry of changes ) {
-				if ( entry.type == 'insert' && entry.name == '$text' ) {
-					const item = entry.position.textNode || entry.position.nodeAfter;
+			// Typing is represented by only a single change.
+			if ( changes.length != 1 || entry.type !== 'insert' || entry.name != '$text' || entry.length != 1 ) {
+				return;
+			}
+			const item = entry.position.textNode || entry.position.nodeAfter;
 
-					if ( !item.parent.is( 'paragraph' ) ) {
-						continue;
-					}
+			if ( !item.parent.is( 'paragraph' ) ) {
+				return;
+			}
 
-					const match = pattern.exec( item.data );
+			const match = pattern.exec( item.data );
 
-					if ( !match ) {
-						continue;
-					}
+			if ( !match ) {
+				return;
+			}
 
-					// Use enqueueChange to create new batch to separate typing batch from the auto-format changes.
-					editor.model.enqueueChange( writer => {
-						// Matched range.
-						const range = Range.createFromParentsAndOffsets( item.parent, 0, item.parent, match[ 0 ].length );
+			// Use enqueueChange to create new batch to separate typing batch from the auto-format changes.
+			editor.model.enqueueChange( writer => {
+				// Matched range.
+				const range = Range.createFromParentsAndOffsets( item.parent, 0, item.parent, match[ 0 ].length );
 
-						// Remove matched text.
-						writer.remove( range );
+				// Remove matched text.
+				writer.remove( range );
 
-						callback( { match } );
-					} );
-				}
-			}
+				callback( { match } );
+			} );
 		} );
 	}
 }