From cff76061780df9449a55baedb327757c338c1ad4 Mon Sep 17 00:00:00 2001 From: Kuba Niegowski Date: Mon, 4 May 2020 11:00:56 +0200 Subject: [PATCH 1/2] Shift+click should use anchor cell if there is any. --- .../ckeditor5-table/src/tableselection.js | 2 +- .../ckeditor5-table/tests/tableselection.js | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/ckeditor5-table/src/tableselection.js b/packages/ckeditor5-table/src/tableselection.js index 4ef219fbbc8..4284cbb2a3c 100644 --- a/packages/ckeditor5-table/src/tableselection.js +++ b/packages/ckeditor5-table/src/tableselection.js @@ -172,7 +172,7 @@ export default class TableSelection extends Plugin { return; } - const anchorCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ]; + const anchorCell = this.getAnchorCell() || getTableCellsContainingSelection( editor.model.document.selection )[ 0 ]; if ( !anchorCell ) { return; diff --git a/packages/ckeditor5-table/tests/tableselection.js b/packages/ckeditor5-table/tests/tableselection.js index d5ae728870a..c48c8722e15 100644 --- a/packages/ckeditor5-table/tests/tableselection.js +++ b/packages/ckeditor5-table/tests/tableselection.js @@ -227,6 +227,39 @@ describe( 'table selection', () => { expect( preventDefault.called ).to.equal( true ); } ); + it( 'should use the anchor cell from the selection if possible', () => { + const preventDefault = sinon.spy(); + + const domEventDataMock = new DomEventData( view, { + shiftKey: true, + target: view.domConverter.mapViewToDom( + // figure > table > tbody > tr > td + viewDocument.getRoot().getChild( 0 ).getChild( 1 ).getChild( 0 ).getChild( 0 ).getChild( 2 ) + ), + preventDefault + } ); + + tableSelection.setCellSelection( + modelRoot.getNodeByPath( [ 0, 1, 0 ] ), + modelRoot.getNodeByPath( [ 0, 2, 1 ] ) + ); + assertSelectedCells( model, [ + [ 0, 0, 0 ], + [ 1, 1, 0 ], + [ 1, 1, 0 ] + ] ); + + viewDocument.fire( 'mousedown', domEventDataMock ); + + assertSelectedCells( model, [ + [ 1, 1, 1 ], + [ 1, 1, 1 ], + [ 0, 0, 0 ] + ] ); + + expect( preventDefault.called ).to.equal( true ); + } ); + it( 'should ignore `selectionChange` event when selecting cells', () => { const consoleLog = sinon.stub( console, 'log' ); const preventDefault = sinon.spy(); From b22b539a3a28136e444ad2a58ae03e0a80290076 Mon Sep 17 00:00:00 2001 From: Kuba Niegowski Date: Mon, 4 May 2020 11:00:56 +0200 Subject: [PATCH 2/2] Shift+click should use anchor cell if there is any. --- .../ckeditor5-table/src/tableselection.js | 2 +- .../ckeditor5-table/tests/tableselection.js | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/ckeditor5-table/src/tableselection.js b/packages/ckeditor5-table/src/tableselection.js index 4ef219fbbc8..4284cbb2a3c 100644 --- a/packages/ckeditor5-table/src/tableselection.js +++ b/packages/ckeditor5-table/src/tableselection.js @@ -172,7 +172,7 @@ export default class TableSelection extends Plugin { return; } - const anchorCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ]; + const anchorCell = this.getAnchorCell() || getTableCellsContainingSelection( editor.model.document.selection )[ 0 ]; if ( !anchorCell ) { return; diff --git a/packages/ckeditor5-table/tests/tableselection.js b/packages/ckeditor5-table/tests/tableselection.js index d5ae728870a..c48c8722e15 100644 --- a/packages/ckeditor5-table/tests/tableselection.js +++ b/packages/ckeditor5-table/tests/tableselection.js @@ -227,6 +227,39 @@ describe( 'table selection', () => { expect( preventDefault.called ).to.equal( true ); } ); + it( 'should use the anchor cell from the selection if possible', () => { + const preventDefault = sinon.spy(); + + const domEventDataMock = new DomEventData( view, { + shiftKey: true, + target: view.domConverter.mapViewToDom( + // figure > table > tbody > tr > td + viewDocument.getRoot().getChild( 0 ).getChild( 1 ).getChild( 0 ).getChild( 0 ).getChild( 2 ) + ), + preventDefault + } ); + + tableSelection.setCellSelection( + modelRoot.getNodeByPath( [ 0, 1, 0 ] ), + modelRoot.getNodeByPath( [ 0, 2, 1 ] ) + ); + assertSelectedCells( model, [ + [ 0, 0, 0 ], + [ 1, 1, 0 ], + [ 1, 1, 0 ] + ] ); + + viewDocument.fire( 'mousedown', domEventDataMock ); + + assertSelectedCells( model, [ + [ 1, 1, 1 ], + [ 1, 1, 1 ], + [ 0, 0, 0 ] + ] ); + + expect( preventDefault.called ).to.equal( true ); + } ); + it( 'should ignore `selectionChange` event when selecting cells', () => { const consoleLog = sinon.stub( console, 'log' ); const preventDefault = sinon.spy();