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

I/6346: TableSelection should work with external changes. #256

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 21 additions & 4 deletions src/tableselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class TableSelection extends Plugin {

setupTableSelectionHighlighting( editor, this );

this.listenTo( selection, 'change:range', () => this._clearSelectionOnExternalChange( selection ) );
this.listenTo( selection, 'change:range', () => this._checkSelectionOnExternalChange(), { priority: 'highest' } );
this.listenTo( model, 'deleteContent', ( evt, args ) => this._handleDeleteContent( evt, args ), { priority: 'high' } );
}

Expand Down Expand Up @@ -296,14 +296,31 @@ export default class TableSelection extends Plugin {
}

/**
* Checks if the selection has changed via an external change and if it is required to clear the internal state of the plugin.
* Checks if the selection has changed via an external change and if it is required to update the internal state of the plugin.
*
* @param {module:engine/model/documentselection~DocumentSelection} selection
* @private
*/
_clearSelectionOnExternalChange( selection ) {
_checkSelectionOnExternalChange() {
const model = this.editor.model;
const selection = model.document.selection;

if ( selection.rangeCount <= 1 && this.hasMultiCellSelection ) {
this.clearSelection();

return;
}

const tableCells = Array.from( selection.getSelectedBlocks() )
.map( element => findAncestor( 'tableCell', model.createPositionAt( element, 0 ) ) )
.filter( tableCell => !!tableCell );

const size = tableCells.length;

if ( !this.hasMultiCellSelection && size ) {
// this is undo or external change to the selection so "fix" internal state.

this._startElement = tableCells[ 0 ];
this._endElement = tableCells[ tableCells.length - 1 ];
}
}

Expand Down
43 changes: 43 additions & 0 deletions tests/tableselection-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictest
import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
import Input from '@ckeditor/ckeditor5-typing/src/input';
import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';

describe( 'table selection', () => {
let editor, model, tableSelection, modelRoot, element, viewDocument;
Expand Down Expand Up @@ -151,6 +152,48 @@ describe( 'table selection', () => {
} );
} );

describe( 'TableSelection - undo integration', () => {
afterEach( async () => {
element.remove();
await editor.destroy();
} );

describe( 'on undo', () => {
beforeEach( async () => {
await setupEditor( [ Input, UndoEditing ] );
} );

it( 'should clear contents of the selected table cells and put selection in last cell on backward delete', () => {
tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );

const node = modelRoot.getNodeByPath( [ 0, 0, 0, 0 ] );
editor.execute( 'input', {
text: 'foobar',
range: model.createRangeIn( modelRoot.getNodeByPath( [ 0, 0, 0, 0 ] ) ),
resultRange: model.createRange(
model.createPositionAt( node, 0 ),
model.createPositionAt( node, 0 )
)
} );

assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
[ 'foobar', '12', '13' ],
[ '21', '22', '23' ],
[ '31', '32', '33' ]
] ) );

editor.execute( 'undo' );

assertEqualMarkup( getModelData( model ), modelTable( [
[ { contents: '11', isSelected: true }, { contents: '12', isSelected: true }, '13' ],
[ { contents: '21', isSelected: true }, { contents: '22', isSelected: true }, '23' ],
[ '31', '32', '33' ]
] ) );
} );
} );
} );

async function setupEditor( plugins ) {
element = document.createElement( 'div' );
document.body.appendChild( element );
Expand Down