Kaynağa Gözat

Other: Refactor outdent list on backspace key to improve performance.

Maciej Gołaszewski 8 yıl önce
ebeveyn
işleme
caa166862e
1 değiştirilmiş dosya ile 27 ekleme ve 11 silme
  1. 27 11
      packages/ckeditor5-list/src/list.js

+ 27 - 11
packages/ckeditor5-list/src/list.js

@@ -62,25 +62,41 @@ export default class List extends Plugin {
 		} );
 
 		// Overwrite default Backspace key behavior.
-		// If Backspace key is pressed with selection collapsed on first position in first list item, outdent it.
+		// If Backspace key is pressed with selection collapsed on first position in first list item, outdent it. #83
 		this.listenTo( this.editor.editing.view, 'delete', ( evt, data ) => {
-			const selection = this.editor.document.selection;
-			const positionParent = selection.getLastPosition().parent;
+			// Check conditions from those that require less computations like those immediately available.
+			if ( data.direction !== 'backward' ) {
+				return;
+			}
 
-			const isInListItem = positionParent.name === 'listItem';
-			const previousIsNotAListItem = !positionParent.previousSibling || positionParent.previousSibling.name !== 'listItem';
+			const selection = this.editor.document.selection;
 
-			const isBackward = data.direction === 'backward';
+			if ( !selection.isCollapsed ) {
+				return;
+			}
 
 			const firstPosition = selection.getFirstPosition();
-			const isAtStart = firstPosition.isAtStart;
 
-			if ( isInListItem && previousIsNotAListItem && isBackward && selection.isCollapsed && isAtStart ) {
-				this.editor.execute( 'outdentList' );
+			if ( !firstPosition.isAtStart ) {
+				return;
+			}
 
-				data.preventDefault();
-				evt.stop();
+			const positionParent = firstPosition.parent;
+
+			if ( positionParent.name !== 'listItem' ) {
+				return;
 			}
+
+			const previousIsAListItem = positionParent.previousSibling && positionParent.previousSibling.name === 'listItem';
+
+			if ( previousIsAListItem ) {
+				return;
+			}
+
+			this.editor.execute( 'outdentList' );
+
+			data.preventDefault();
+			evt.stop();
 		}, { priority: 'high' } );
 
 		const getCommandExecuter = commandName => {