From 7d6a4d841e610f216c82b441516042056f96017b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Wed, 17 Jul 2019 15:45:03 +0200 Subject: [PATCH] The 'indentBlock' command should be executed on blocks from a selection according to schema rules. --- src/indentblockcommand.js | 22 ++++++++++++++++------ tests/indentblockcommand.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/indentblockcommand.js b/src/indentblockcommand.js index ef47b25..7d986e3 100644 --- a/src/indentblockcommand.js +++ b/src/indentblockcommand.js @@ -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}. * diff --git a/tests/indentblockcommand.js b/tests/indentblockcommand.js index 0707d75..c6f6113 100644 --- a/tests/indentblockcommand.js +++ b/tests/indentblockcommand.js @@ -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, + 'f[oo' + + 'foo' + + 'f]oo' + ); + command.execute(); + sinon.assert.calledThrice( indentBehavior.getNextIndent ); + } ); + + it( 'should be executed only for blocks that can have indentBlock attribute', () => { + setData( model, + 'f[oo' + + 'foo' + + 'f]oo' + ); + command.execute(); + sinon.assert.calledTwice( indentBehavior.getNextIndent ); + } ); + } ); + } ); + describe( 'indent', () => { describe( 'using classes', () => { beforeEach( () => {