Parcourir la source

Changed: Even more improvements for IndentCommand.

Szymon Cofalik il y a 8 ans
Parent
commit
fa30c8402b

+ 11 - 2
packages/ckeditor5-list/src/indentcommand.js

@@ -87,6 +87,11 @@ export default class IndentCommand extends Command {
 			}
 
 			// Check whether some of changed list items' type should not be fixed.
+			// But first, reverse `itemsToChange` again -- we always want to perform those fixes starting from first item (source-wise).
+			if ( this._indentBy < 0 ) {
+				itemsToChange = itemsToChange.reverse();
+			}
+
 			for ( let item of itemsToChange ) {
 				_fixType( item, batch );
 			}
@@ -114,8 +119,12 @@ export default class IndentCommand extends Command {
 			let prev = listItem.previousSibling;
 
 			while ( prev && prev.is( 'listItem' ) && prev.getAttribute( 'indent' ) >= indent ) {
-				if ( prev.getAttribute( 'indent' ) == indent && prev.getAttribute( 'type' ) == type ) {
-					return true;
+				if ( prev.getAttribute( 'indent' ) == indent ) {
+					// The item is on the same level.
+					// If it has same type, it means that we found a preceding sibling from the same list.
+					// If it does not have same type, it means that `listItem` is on different list (this can happen only
+					// on top level lists, though).
+					return prev.getAttribute( 'type' ) == type;
 				}
 
 				prev = prev.previousSibling;

+ 35 - 1
packages/ckeditor5-list/tests/indentcommand.js

@@ -67,7 +67,7 @@ describe( 'IndentCommand', () => {
 				expect( command.isEnabled ).to.be.false;
 			} );
 
-			// #53.
+			// Reported in PR #53.
 			it( 'should be false if selection starts in first list item #2', () => {
 				setData(
 					doc,
@@ -81,6 +81,20 @@ describe( 'IndentCommand', () => {
 				expect( command.isEnabled ).to.be.false;
 			} );
 
+			// Reported in PR #53.
+			it( 'should be false if selection starts in first list item #3', () => {
+				setData(
+					doc,
+					'<listItem indent="0" type="bulleted">a</listItem>' +
+					'<listItem indent="1" type="bulleted">b</listItem>' +
+					'<listItem indent="0" type="numbered">c</listItem>' +
+					'<listItem indent="1" type="bulleted">d</listItem>' +
+					'<listItem indent="0" type="bulleted">[]e</listItem>'
+				);
+
+				expect( command.isEnabled ).to.be.false;
+			} );
+
 			it( 'should be false if selection starts in first list item of top level list with different type than previous list', () => {
 				setData(
 					doc,
@@ -365,6 +379,26 @@ describe( 'IndentCommand', () => {
 				);
 			} );
 
+			// Reported in #53.
+			it( 'should fix list type when item is outdented #3', () => {
+				setData(
+					doc,
+					'<listItem indent="0" type="bulleted">a</listItem>' +
+					'<listItem indent="1" type="numbered">[b</listItem>' +
+					'<listItem indent="1" type="numbered">c</listItem>' +
+					'<listItem indent="1" type="numbered">d]</listItem>'
+				);
+
+				command._doExecute();
+
+				expect( getData( doc, { withoutSelection: true } ) ).to.equal(
+					'<listItem indent="0" type="bulleted">a</listItem>' +
+					'<listItem indent="0" type="bulleted">b</listItem>' +
+					'<listItem indent="0" type="bulleted">c</listItem>' +
+					'<listItem indent="0" type="bulleted">d</listItem>'
+				);
+			} );
+
 			it( 'should fix list type when item is outdented to top level (if needed)', () => {
 				setData(
 					doc,