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

Commit

Permalink
Merge pull request #14 from ckeditor/t/8
Browse files Browse the repository at this point in the history
Fix: The `'indentBlock'` command should be executed on blocks from the selection according to the schema rules. Closes #8.
  • Loading branch information
Reinmar committed Jul 23, 2019
2 parents dfe66f0 + 7d6a4d8 commit 15caf3e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/indentblockcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,36 @@ export default class IndentBlockCommand extends Command {
*/
execute() {
const model = this.editor.model;
const doc = model.document;

const itemsToChange = Array.from( doc.selection.getSelectedBlocks() );
const blocksToChange = getBlocksToChange( model );

model.change( writer => {
for ( const item of itemsToChange ) {
const currentIndent = item.getAttribute( 'blockIndent' );
for ( const block of blocksToChange ) {
const currentIndent = block.getAttribute( 'blockIndent' );

const nextIndent = this._indentBehavior.getNextIndent( currentIndent );

if ( nextIndent ) {
writer.setAttribute( 'blockIndent', nextIndent, item );
writer.setAttribute( 'blockIndent', nextIndent, block );
} else {
writer.removeAttribute( 'blockIndent', item );
writer.removeAttribute( 'blockIndent', block );
}
}
} );
}
}

// Returns blocks from selection that should have blockIndent selection set.
//
// @param {module:engine/model/model~model} model A model.
function getBlocksToChange( model ) {
const selection = model.document.selection;
const schema = model.schema;
const blocksInSelection = Array.from( selection.getSelectedBlocks() );

return blocksInSelection.filter( block => schema.checkAttribute( block, 'blockIndent' ) );
}

/**
* Provides indentation behavior to {@link module:indent/indentblockcommand~IndentBlockCommand}.
*
Expand Down
35 changes: 35 additions & 0 deletions tests/indentblockcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,41 @@ describe( 'IndentBlockCommand', () => {
return editor.destroy();
} );

describe( 'common behavior', () => {
let indentBehavior;

beforeEach( () => {
indentBehavior = {
checkEnabled: sinon.stub().returns( true ),
getNextIndent: sinon.stub()
};

command = new IndentBlockCommand( editor, indentBehavior );
} );

describe( 'execute()', () => {
it( 'should be executed for all selected blocks', () => {
setData( model,
'<paragraph>f[oo</paragraph>' +
'<paragraph>foo</paragraph>' +
'<paragraph>f]oo</paragraph>'
);
command.execute();
sinon.assert.calledThrice( indentBehavior.getNextIndent );
} );

it( 'should be executed only for blocks that can have indentBlock attribute', () => {
setData( model,
'<paragraph>f[oo</paragraph>' +
'<block>foo</block>' +
'<paragraph>f]oo</paragraph>'
);
command.execute();
sinon.assert.calledTwice( indentBehavior.getNextIndent );
} );
} );
} );

describe( 'indent', () => {
describe( 'using classes', () => {
beforeEach( () => {
Expand Down

0 comments on commit 15caf3e

Please sign in to comment.