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

Custom keyboard navigation through tables #304

Merged
merged 33 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b3c1a0d
Added custom keyboard navigation through tables.
niegowski Apr 8, 2020
99b1207
Merge branch 'master' into i/3267
niegowski Apr 8, 2020
e5ec2f0
Added some code comments and optimisation of up/down at cell start/end.
niegowski Apr 8, 2020
7fb91e0
Custom table navigation extracted to the separate plugin. Navigation …
niegowski Apr 9, 2020
cfd0110
Updated tab key tests (moved to TableNavigation plugin).
niegowski Apr 9, 2020
9373f5c
Optimised algorithm for finding if selection is in the first/last lin…
niegowski Apr 10, 2020
379e3f7
Table navigation using 'keydown' event's instead of `keystrokes`. Add…
niegowski Apr 14, 2020
8116068
Using `modifySelection` to find navigable selection positions.
niegowski Apr 14, 2020
4b62a7e
Merge branch 'master' into i/3267
oleq Apr 15, 2020
c508001
Fixed isSingleLineRange (missing rect values rounding).
niegowski Apr 15, 2020
9c6a8af
Simplified isSingleLineRange implementation.
niegowski Apr 15, 2020
eebd6f2
Changed _getNearestTextPosition to _getNearestVisibleTextPosition to …
niegowski Apr 15, 2020
05368fe
Added tests for navigation through table cells when those are selecte…
niegowski Apr 16, 2020
26d7453
Added more tests for custom keyboard navigation through tables.
niegowski Apr 17, 2020
29dca14
Updated tests for custom keyboard navigation through tables.
niegowski Apr 17, 2020
b1f6706
Fixed edge case of the selection at the position next to the end of p…
niegowski Apr 20, 2020
d4f1972
Added handling of navigation next/on object elements and additional t…
niegowski Apr 20, 2020
8c35a7e
Added tests for "dual" positions in text.
niegowski Apr 20, 2020
20b41a6
Merge branch 'master' into i/3267
niegowski Apr 20, 2020
973170a
Tweaking visual test.
niegowski Apr 21, 2020
3795e37
Tweaking visual test.
niegowski Apr 21, 2020
af89be5
Disabled unstable test on Gecko.
niegowski Apr 21, 2020
85b4dea
Updated JSDocs.
niegowski Apr 22, 2020
c9b7e5f
Apply suggestions from code review
niegowski Apr 22, 2020
6dd7b32
Updated JSDocs.
niegowski Apr 22, 2020
c0497e6
Added missing license header.
niegowski Apr 22, 2020
1879a5c
Organized imports.
niegowski Apr 22, 2020
83d09d6
Tests: Improved test case naming in TableNavigation tests.
oleq Apr 22, 2020
e3fe69f
Apply suggestions from code review
niegowski Apr 22, 2020
5287e15
Apply suggestions from code review.
niegowski Apr 22, 2020
344e515
Apply suggestions from code review.
niegowski Apr 22, 2020
9760311
Update commend wording.
jodator Apr 23, 2020
92d1567
Merge branch 'master' into i/3267
jodator Apr 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import TableEditing from './tableediting';
import TableUI from './tableui';
import TableSelection from './tableselection';
import TableClipboard from './tableclipboard';
import TableNavigation from './tablenavigation';
import Widget from '@ckeditor/ckeditor5-widget/src/widget';

import '../theme/table.css';
Expand All @@ -26,6 +27,7 @@ import '../theme/table.css';
*
* * {@link module:table/tableediting~TableEditing editing feature},
* * {@link module:table/tableselection~TableSelection selection feature},
* * {@link module:table/tablenavigation~TableNavigation keyboard navigation feature},
* * {@link module:table/tableclipboard~TableClipboard clipboard feature},
* * {@link module:table/tableui~TableUI UI feature}.
*
Expand All @@ -36,7 +38,7 @@ export default class Table extends Plugin {
* @inheritDoc
*/
static get requires() {
jodator marked this conversation as resolved.
Show resolved Hide resolved
return [ TableEditing, TableUI, TableSelection, TableClipboard, Widget ];
return [ TableEditing, TableUI, TableSelection, TableClipboard, TableNavigation, Widget ];
oleq marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
104 changes: 0 additions & 104 deletions src/tableediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import SetHeaderColumnCommand from './commands/setheadercolumncommand';
import MergeCellsCommand from './commands/mergecellscommand';
import SelectRowCommand from './commands/selectrowcommand';
import SelectColumnCommand from './commands/selectcolumncommand';
import { getTableCellsContainingSelection } from './utils';
import TableUtils from '../src/tableutils';

import injectTableLayoutPostFixer from './converters/table-layout-post-fixer';
Expand Down Expand Up @@ -150,11 +149,6 @@ export default class TableEditing extends Plugin {
injectTableLayoutPostFixer( model );
injectTableCellRefreshPostFixer( model );
injectTableCellParagraphPostFixer( model );

// Handle Tab key navigation.
this.editor.keystrokes.set( 'Tab', ( ...args ) => this._handleTabOnSelectedTable( ...args ), { priority: 'low' } );
this.editor.keystrokes.set( 'Tab', this._getTabHandler( true ), { priority: 'low' } );
this.editor.keystrokes.set( 'Shift+Tab', this._getTabHandler( false ), { priority: 'low' } );
}

/**
Expand All @@ -163,102 +157,4 @@ export default class TableEditing extends Plugin {
static get requires() {
return [ TableUtils ];
}

/**
* Handles {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
* when the table widget is selected.
*
* @private
* @param {module:utils/eventinfo~EventInfo} eventInfo
* @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
*/
_handleTabOnSelectedTable( domEventData, cancel ) {
const editor = this.editor;
const selection = editor.model.document.selection;

if ( !selection.isCollapsed && selection.rangeCount === 1 && selection.getFirstRange().isFlat ) {
const selectedElement = selection.getSelectedElement();

if ( !selectedElement || !selectedElement.is( 'table' ) ) {
return;
}

cancel();

editor.model.change( writer => {
writer.setSelection( writer.createRangeIn( selectedElement.getChild( 0 ).getChild( 0 ) ) );
} );
}
}

/**
* Returns a handler for {@link module:engine/view/document~Document#event:keydown keydown} events for the <kbd>Tab</kbd> key executed
* inside table cell.
*
* @private
* @param {Boolean} isForward Whether this handler will move the selection to the next or the previous cell.
*/
_getTabHandler( isForward ) {
const editor = this.editor;

return ( domEventData, cancel ) => {
const selection = editor.model.document.selection;
const tableCell = getTableCellsContainingSelection( selection )[ 0 ];

if ( !tableCell ) {
return;
}

cancel();

const tableRow = tableCell.parent;
const table = tableRow.parent;

const currentRowIndex = table.getChildIndex( tableRow );
const currentCellIndex = tableRow.getChildIndex( tableCell );

const isFirstCellInRow = currentCellIndex === 0;

if ( !isForward && isFirstCellInRow && currentRowIndex === 0 ) {
// It's the first cell of the table - don't do anything (stay in the current position).
return;
}

const isLastCellInRow = currentCellIndex === tableRow.childCount - 1;
const isLastRow = currentRowIndex === table.childCount - 1;

if ( isForward && isLastRow && isLastCellInRow ) {
editor.execute( 'insertTableRowBelow' );

// 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 ) {
const nextRow = table.getChild( currentRowIndex + 1 );

cellToFocus = nextRow.getChild( 0 );
}
// Move to last cell in a previous row.
else if ( !isForward && isFirstCellInRow ) {
const previousRow = table.getChild( currentRowIndex - 1 );

cellToFocus = previousRow.getChild( previousRow.childCount - 1 );
}
// Move to next/previous cell.
else {
cellToFocus = tableRow.getChild( currentCellIndex + ( isForward ? 1 : -1 ) );
}

editor.model.change( writer => {
writer.setSelection( writer.createRangeIn( cellToFocus ) );
} );
};
}
}
Loading