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

Checking if differ returns single change when using inline autoformatter.

Szymon Kupś 7 лет назад
Родитель
Сommit
cd556ba563
1 измененных файлов с 58 добавлено и 57 удалено
  1. 58 57
      packages/ckeditor5-autoformat/src/inlineautoformatediting.js

+ 58 - 57
packages/ckeditor5-autoformat/src/inlineautoformatediting.js

@@ -141,74 +141,75 @@ export default class InlineAutoformatEditing {
 		} );
 
 		editor.model.document.on( 'change', () => {
-			const changes = editor.model.document.differ.getChanges();
+			const selection = editor.model.document.selection;
 
-			for ( const entry of changes ) {
-				if ( entry.type != 'insert' || entry.name != '$text' ) {
-					continue;
-				}
+			// Do nothing if selection is not collapsed.
+			if ( !selection.isCollapsed || !selection.focus || !selection.focus.parent ) {
+				return;
+			}
 
-				const selection = editor.model.document.selection;
+			const changes = Array.from( editor.model.document.differ.getChanges() );
+			const entry = changes[ 0 ];
 
-				if ( !selection.isCollapsed || !selection.focus || !selection.focus.parent ) {
-					continue;
-				}
+			// Typing is only a single change.
+			if ( changes.length != 1 || entry.type !== 'insert' || entry.name != '$text' ) {
+				return;
+			}
+
+			const block = selection.focus.parent;
+			const text = getText( block ).slice( 0, selection.focus.offset );
+			const ranges = testCallback( text );
+			const rangesToFormat = [];
 
-				const block = selection.focus.parent;
-				const text = getText( block ).slice( 0, selection.focus.offset );
-				const ranges = testCallback( text );
-				const rangesToFormat = [];
-
-				// Apply format before deleting text.
-				ranges.format.forEach( range => {
-					if ( range[ 0 ] === undefined || range[ 1 ] === undefined ) {
-						return;
-					}
-
-					rangesToFormat.push( LiveRange.createFromParentsAndOffsets(
-						block, range[ 0 ],
-						block, range[ 1 ]
-					) );
-				} );
-
-				const rangesToRemove = [];
-
-				// Reverse order to not mix the offsets while removing.
-				ranges.remove.slice().reverse().forEach( range => {
-					if ( range[ 0 ] === undefined || range[ 1 ] === undefined ) {
-						return;
-					}
-
-					rangesToRemove.push( LiveRange.createFromParentsAndOffsets(
-						block, range[ 0 ],
-						block, range[ 1 ]
-					) );
-				} );
-
-				if ( !( rangesToFormat.length && rangesToRemove.length ) ) {
-					continue;
+			// Apply format before deleting text.
+			ranges.format.forEach( range => {
+				if ( range[ 0 ] === undefined || range[ 1 ] === undefined ) {
+					return;
 				}
 
-				// Use enqueueChange to create new batch to separate typing batch from the auto-format changes.
-				editor.model.enqueueChange( writer => {
-					const validRanges = editor.model.schema.getValidRanges( rangesToFormat, attributeKey );
+				rangesToFormat.push( LiveRange.createFromParentsAndOffsets(
+					block, range[ 0 ],
+					block, range[ 1 ]
+				) );
+			} );
 
-					// Apply format.
-					formatCallback( writer, validRanges );
+			const rangesToRemove = [];
 
-					// Detach ranges used to apply Autoformat. Prevents memory leaks. #39
-					rangesToFormat.forEach( range => range.detach() );
+			// Reverse order to not mix the offsets while removing.
+			ranges.remove.slice().reverse().forEach( range => {
+				if ( range[ 0 ] === undefined || range[ 1 ] === undefined ) {
+					return;
+				}
 
-					// Remove delimiters.
-					for ( const range of rangesToRemove ) {
-						writer.remove( range );
+				rangesToRemove.push( LiveRange.createFromParentsAndOffsets(
+					block, range[ 0 ],
+					block, range[ 1 ]
+				) );
+			} );
 
-						// Prevents memory leaks.
-						// https://github.com/ckeditor/ckeditor5-autoformat/issues/39
-						range.detach();
-					}
-				} );
+			if ( !( rangesToFormat.length && rangesToRemove.length ) ) {
+				return;
 			}
+
+			// Use enqueueChange to create new batch to separate typing batch from the auto-format changes.
+			editor.model.enqueueChange( writer => {
+				const validRanges = editor.model.schema.getValidRanges( rangesToFormat, attributeKey );
+
+				// Apply format.
+				formatCallback( writer, validRanges );
+
+				// Detach ranges used to apply Autoformat. Prevents memory leaks. #39
+				rangesToFormat.forEach( range => range.detach() );
+
+				// Remove delimiters.
+				for ( const range of rangesToRemove ) {
+					writer.remove( range );
+
+					// Prevents memory leaks.
+					// https://github.com/ckeditor/ckeditor5-autoformat/issues/39
+					range.detach();
+				}
+			} );
 		} );
 	}
 }