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

Commit

Permalink
Fix: The indentBlock command will now operate only on selected top-…
Browse files Browse the repository at this point in the history
…most blocks. Closes #11.
  • Loading branch information
mlewand authored Jul 24, 2019
2 parents 15caf3e + dbfaeda commit 7fac8c0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
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 );
} );
} );
} );

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

0 comments on commit 7fac8c0

Please sign in to comment.