Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

T/11: Fix indent block command on nested blocks. #15

Merged
merged 5 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/indentblockcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ 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' ) ) {
this.isEnabled = false;

Expand Down Expand Up @@ -95,7 +94,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' ) );
}
Expand Down
47 changes: 45 additions & 2 deletions tests/indentblockcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' } );
} );
} );

Expand All @@ -46,6 +57,14 @@ describe( 'IndentBlockCommand', () => {
command = new IndentBlockCommand( editor, indentBehavior );
} );

describe( 'refresh()', () => {
it( 'should be disabled if a top-most block disallows indentBlock attribute', () => {
setData( model, '[<parentBlock><paragraph>foo</paragraph></parentBlock>]' );
command.refresh();
expect( command.isEnabled ).to.be.false;
} );
} );

describe( 'execute()', () => {
it( 'should be executed for all selected blocks', () => {
setData( model,
Expand All @@ -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 );
jodator marked this conversation as resolved.
Show resolved Hide resolved
} );
} );
} );

Expand Down Expand Up @@ -117,6 +146,20 @@ describe( 'IndentBlockCommand', () => {
command.execute();
expect( getData( model ) ).to.equal( '<paragraph blockIndent="indent-3">f[]oo</paragraph>' );
} );

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();
expect( getData( model ) ).to.equal(
'<paragraph blockIndent="indent-1">f[oo</paragraph>' +
'<parentBlock><paragraph>foo</paragraph><paragraph>foo</paragraph></parentBlock>' +
'<paragraph blockIndent="indent-1">f]oo</paragraph>'
);
} );
} );
} );

Expand Down