From a0d54175ae731830b58742b13474e8234f653467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Wed, 17 Jul 2019 16:03:49 +0200 Subject: [PATCH 1/5] The 'indentBlock' command will operate on top-most blocks. --- src/indentblockcommand.js | 4 ++-- tests/indentblockcommand.js | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/indentblockcommand.js b/src/indentblockcommand.js index 7d986e3..9d0b3df 100644 --- a/src/indentblockcommand.js +++ b/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' ) ); } diff --git a/tests/indentblockcommand.js b/tests/indentblockcommand.js index c6f6113..2720aec 100644 --- a/tests/indentblockcommand.js +++ b/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, '[foo]' ); + 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, + 'f[oo' + + 'foofoo' + + 'f]oo' + ); + command.execute(); + sinon.assert.calledTwice( indentBehavior.getNextIndent ); + } ); } ); } ); From d4dffe758892348033bff8887f59267c62686749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Tue, 23 Jul 2019 18:13:53 +0200 Subject: [PATCH 2/5] Remove wrong comment. --- src/indentblockcommand.js | 1 - tests/indentblockcommand.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/indentblockcommand.js b/src/indentblockcommand.js index 9d0b3df..48d27d8 100644 --- a/src/indentblockcommand.js +++ b/src/indentblockcommand.js @@ -55,7 +55,6 @@ export default class IndentBlockCommand extends Command { 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; diff --git a/tests/indentblockcommand.js b/tests/indentblockcommand.js index 2720aec..5257c35 100644 --- a/tests/indentblockcommand.js +++ b/tests/indentblockcommand.js @@ -58,7 +58,7 @@ describe( 'IndentBlockCommand', () => { } ); describe( 'refresh()', () => { - it( 'should be executed for a top-most selected block', () => { + it( 'should not call indentBehavior.checkEnabled() for a selected block', () => { setData( model, '[foo]' ); command.refresh(); sinon.assert.notCalled( indentBehavior.checkEnabled ); From 7e71eb0dc2fabe6e7f29600ace53892fe2633e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Tue, 23 Jul 2019 18:23:36 +0200 Subject: [PATCH 3/5] Add test for top-most block case in indent using classes suite. --- tests/indentblockcommand.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/indentblockcommand.js b/tests/indentblockcommand.js index 5257c35..46b5a51 100644 --- a/tests/indentblockcommand.js +++ b/tests/indentblockcommand.js @@ -146,6 +146,20 @@ describe( 'IndentBlockCommand', () => { command.execute(); expect( getData( model ) ).to.equal( 'f[]oo' ); } ); + + it( 'should be executed only for top-most blocks that can have indentBlock attribute', () => { + setData( model, + 'f[oo' + + 'foofoo' + + 'f]oo' + ); + command.execute(); + expect( getData( model ) ).to.equal( + 'f[oo' + + 'foofoo' + + 'f]oo' + ); + } ); } ); } ); From 127ba7b22a267751643fbc5ee2b619206b40e0d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Tue, 23 Jul 2019 18:25:44 +0200 Subject: [PATCH 4/5] Rename test. --- tests/indentblockcommand.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/indentblockcommand.js b/tests/indentblockcommand.js index 46b5a51..f472aab 100644 --- a/tests/indentblockcommand.js +++ b/tests/indentblockcommand.js @@ -58,7 +58,7 @@ describe( 'IndentBlockCommand', () => { } ); describe( 'refresh()', () => { - it( 'should not call indentBehavior.checkEnabled() for a selected block', () => { + it( 'should not call indentBehavior.checkEnabled() for a selected block that cannot have indentBlock attribute', () => { setData( model, '[foo]' ); command.refresh(); sinon.assert.notCalled( indentBehavior.checkEnabled ); From dbfaeda4ebfde819deeb08d2f006cb07ff93c6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Tue, 23 Jul 2019 18:36:35 +0200 Subject: [PATCH 5/5] Change refresh() method common behavior test assertion. --- tests/indentblockcommand.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/indentblockcommand.js b/tests/indentblockcommand.js index f472aab..b5ec05d 100644 --- a/tests/indentblockcommand.js +++ b/tests/indentblockcommand.js @@ -58,10 +58,10 @@ describe( 'IndentBlockCommand', () => { } ); describe( 'refresh()', () => { - it( 'should not call indentBehavior.checkEnabled() for a selected block that cannot have indentBlock attribute', () => { + it( 'should be disabled if a top-most block disallows indentBlock attribute', () => { setData( model, '[foo]' ); command.refresh(); - sinon.assert.notCalled( indentBehavior.checkEnabled ); + expect( command.isEnabled ).to.be.false; } ); } );