Explorar el Código

The 'indentBlock' command will operate on top-most blocks.

Maciej Gołaszewski hace 6 años
padre
commit
a00c83f39b

+ 2 - 2
packages/ckeditor5-indent/src/indentblockcommand.js

@@ -53,7 +53,7 @@ export default class IndentBlockCommand extends Command {
 		const editor = this.editor;
 		const model = editor.model;
 
-		const block = first( model.document.selection.getSelectedBlocks() );
+		const block = first( model.document.selection.getTopMostBlocks() );
 
 		// If selection is not in a list item, the command is disabled.
 		if ( !block || !model.schema.checkAttribute( block, 'blockIndent' ) ) {
@@ -95,7 +95,7 @@ export default class IndentBlockCommand extends Command {
 function getBlocksToChange( model ) {
 	const selection = model.document.selection;
 	const schema = model.schema;
-	const blocksInSelection = Array.from( selection.getSelectedBlocks() );
+	const blocksInSelection = Array.from( selection.getTopMostBlocks() );
 
 	return blocksInSelection.filter( block => schema.checkAttribute( block, 'blockIndent' ) );
 }

+ 31 - 2
packages/ckeditor5-indent/tests/indentblockcommand.js

@@ -20,11 +20,22 @@ describe( 'IndentBlockCommand', () => {
 				editor = newEditor;
 				model = editor.model;
 
+				model.schema.register( 'parentBlock', {
+					allowWhere: '$block',
+					isLimit: true,
+					isObject: true,
+					isBlock: true
+				} );
+
 				model.schema.register( 'paragraph', {
 					inheritAllFrom: '$block',
-					allowAttributes: [ 'blockIndent' ]
+					allowAttributes: [ 'blockIndent' ],
+					allowIn: 'parentBlock'
+				} );
+
+				model.schema.register( 'block', {
+					inheritAllFrom: '$block'
 				} );
-				model.schema.register( 'block', { inheritAllFrom: '$block' } );
 			} );
 	} );
 
@@ -46,6 +57,14 @@ describe( 'IndentBlockCommand', () => {
 			command = new IndentBlockCommand( editor, indentBehavior );
 		} );
 
+		describe( 'refresh()', () => {
+			it( 'should be executed for a top-most selected block', () => {
+				setData( model, '[<parentBlock><paragraph>foo</paragraph></parentBlock>]' );
+				command.refresh();
+				sinon.assert.notCalled( indentBehavior.checkEnabled );
+			} );
+		} );
+
 		describe( 'execute()', () => {
 			it( 'should be executed for all selected blocks', () => {
 				setData( model,
@@ -66,6 +85,16 @@ describe( 'IndentBlockCommand', () => {
 				command.execute();
 				sinon.assert.calledTwice( indentBehavior.getNextIndent );
 			} );
+
+			it( 'should be executed only for top-most blocks that can have indentBlock attribute', () => {
+				setData( model,
+					'<paragraph>f[oo</paragraph>' +
+					'<parentBlock><paragraph>foo</paragraph><paragraph>foo</paragraph></parentBlock>' +
+					'<paragraph>f]oo</paragraph>'
+				);
+				command.execute();
+				sinon.assert.calledTwice( indentBehavior.getNextIndent );
+			} );
 		} );
 	} );