From c23e9dbc2391f9ebf8bbaa28e9e6ba49544b105e Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Mon, 6 May 2019 12:55:15 +0200 Subject: [PATCH 1/4] Fix: Table rows will not be added on tab key press if the associated command is disabled. --- src/tableediting.js | 13 +++++++++++-- tests/tableediting.js | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/tableediting.js b/src/tableediting.js index c47fc27a..3bc922dc 100644 --- a/src/tableediting.js +++ b/src/tableediting.js @@ -214,16 +214,25 @@ export default class TableEditing extends Plugin { } const isLastCellInRow = currentCellIndex === tableRow.childCount - 1; - const isLastRow = currentRowIndex === table.childCount - 1; + let isLastRow = currentRowIndex === table.childCount - 1; if ( isForward && isLastRow && isLastCellInRow ) { - editor.plugins.get( 'TableUtils' ).insertRows( table, { at: table.childCount } ); + editor.execute( 'insertTableRowBelow' ); + + // Re-evaluate `isLastRow`. If `insertTableRowBelow` execution didn't add any row (because it was disabled or it got + // overwritten in some way) this will still be `false`. But if the row was added it will change to `true`. + isLastRow = currentRowIndex === table.childCount - 1; } let cellToFocus; // Move to first cell in next row. if ( isForward && isLastCellInRow ) { + if ( isLastRow ) { + // It's the last cell of a table - don't do anything (stay in current position). + return; + } + const nextRow = table.getChild( currentRowIndex + 1 ); cellToFocus = nextRow.getChild( 0 ); diff --git a/tests/tableediting.js b/tests/tableediting.js index 05175e0e..e27d164e 100644 --- a/tests/tableediting.js +++ b/tests/tableediting.js @@ -294,6 +294,22 @@ describe( 'TableEditing', () => { ] ) ); } ); + it( 'should not create another row and not move the caret if insertTableRowBelow command is disabled', () => { + setModelData( model, modelTable( [ + [ '11', '[12]' ] + ] ) ); + + const insertTableRowBelowCommand = editor.commands.get( 'insertTableRowBelow' ); + + insertTableRowBelowCommand.forceDisabled( 'test' ); + + editor.editing.view.document.fire( 'keydown', domEvtDataStub ); + + expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [ + [ '11', '[12]' ], + ] ) ); + } ); + it( 'should move to the first cell of next row if on end of a row', () => { setModelData( model, modelTable( [ [ '11', '12[]' ], From 72cdffb9b606f5602f25d61a8b4a2516af85853a Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Mon, 6 May 2019 13:09:17 +0200 Subject: [PATCH 2/4] Docs: Fixed in-code comment. --- src/tableediting.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tableediting.js b/src/tableediting.js index 3bc922dc..3d59ca1b 100644 --- a/src/tableediting.js +++ b/src/tableediting.js @@ -220,7 +220,7 @@ export default class TableEditing extends Plugin { editor.execute( 'insertTableRowBelow' ); // Re-evaluate `isLastRow`. If `insertTableRowBelow` execution didn't add any row (because it was disabled or it got - // overwritten in some way) this will still be `false`. But if the row was added it will change to `true`. + // overwritten in some way) this will still be `true`. But if the row was added it will change to `false`. isLastRow = currentRowIndex === table.childCount - 1; } From 6ea949585101c883e13aa6ad9ee087683c95246a Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Mon, 6 May 2019 13:35:23 +0200 Subject: [PATCH 3/4] Tests: Slight change to a test. --- tests/tableediting.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tableediting.js b/tests/tableediting.js index e27d164e..023d2eed 100644 --- a/tests/tableediting.js +++ b/tests/tableediting.js @@ -296,7 +296,7 @@ describe( 'TableEditing', () => { it( 'should not create another row and not move the caret if insertTableRowBelow command is disabled', () => { setModelData( model, modelTable( [ - [ '11', '[12]' ] + [ '11', '12[]' ] ] ) ); const insertTableRowBelowCommand = editor.commands.get( 'insertTableRowBelow' ); @@ -306,7 +306,7 @@ describe( 'TableEditing', () => { editor.editing.view.document.fire( 'keydown', domEvtDataStub ); expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [ - [ '11', '[12]' ], + [ '11', '12[]' ] ] ) ); } ); From e6cc3bcfb59b64e9573cb5ff61f2dfd2b706716d Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Mon, 6 May 2019 13:44:24 +0200 Subject: [PATCH 4/4] Other: Moved some code around. --- src/tableediting.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/tableediting.js b/src/tableediting.js index 3d59ca1b..6ebe224a 100644 --- a/src/tableediting.js +++ b/src/tableediting.js @@ -214,25 +214,22 @@ export default class TableEditing extends Plugin { } const isLastCellInRow = currentCellIndex === tableRow.childCount - 1; - let isLastRow = currentRowIndex === table.childCount - 1; + const isLastRow = currentRowIndex === table.childCount - 1; if ( isForward && isLastRow && isLastCellInRow ) { editor.execute( 'insertTableRowBelow' ); - // Re-evaluate `isLastRow`. If `insertTableRowBelow` execution didn't add any row (because it was disabled or it got - // overwritten in some way) this will still be `true`. But if the row was added it will change to `false`. - isLastRow = currentRowIndex === table.childCount - 1; + // Check if the command actually added a row. If `insertTableRowBelow` execution didn't add a row (because it was disabled + // or it got overwritten) do not change the selection. + if ( currentRowIndex === table.childCount - 1 ) { + return; + } } let cellToFocus; // Move to first cell in next row. if ( isForward && isLastCellInRow ) { - if ( isLastRow ) { - // It's the last cell of a table - don't do anything (stay in current position). - return; - } - const nextRow = table.getChild( currentRowIndex + 1 ); cellToFocus = nextRow.getChild( 0 );