| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import TableEditing from '../src/tableediting';
- import TableSelection from '../src/tableselection';
- import { modelTable } from './_utils/utils';
- describe( 'table selection', () => {
- let editor, model, tableSelection, modelRoot;
- beforeEach( async () => {
- editor = await VirtualTestEditor.create( {
- plugins: [ TableEditing, TableSelection, Paragraph ]
- } );
- model = editor.model;
- modelRoot = model.document.getRoot();
- tableSelection = editor.plugins.get( TableSelection );
- setModelData( model, modelTable( [
- [ '11[]', '12', '13' ],
- [ '21', '22', '23' ],
- [ '31', '32', '33' ]
- ] ) );
- } );
- afterEach( async () => {
- await editor.destroy();
- } );
- describe( 'TableSelection', () => {
- describe( 'startSelectingFrom()', () => {
- it( 'should not change model selection', () => {
- const spy = sinon.spy();
- model.document.selection.on( 'change', spy );
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- sinon.assert.notCalled( spy );
- } );
- } );
- describe( 'setSelectingTo()', () => {
- it( 'should set model selection on passed cell if startSelectingFrom() was not used', () => {
- const spy = sinon.spy();
- model.document.selection.on( 'change', spy );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- sinon.assert.calledOnce( spy );
- assertSelectedCells( [
- [ 1, 1, 0 ],
- [ 0, 0, 0 ],
- [ 0, 0, 0 ]
- ] );
- } );
- it( 'should change model selection if valid selection will be set', () => {
- const spy = sinon.spy();
- model.document.selection.on( 'change', spy );
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- sinon.assert.calledOnce( spy );
- } );
- it( 'should not change model selection if passed table cell is from other table then start cell', () => {
- setModelData( model,
- modelTable( [
- [ '11[]', '12', '13' ],
- [ '21', '22', '23' ],
- [ '31', '32', '33' ]
- ] ) +
- modelTable( [
- [ 'a', 'b' ],
- [ 'c', 'd' ]
- ] )
- );
- const spy = sinon.spy();
- model.document.selection.on( 'change', spy );
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 1, 0, 1 ] ) );
- sinon.assert.notCalled( spy );
- } );
- it( 'should select two table cells', () => {
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- assertSelectedCells( [
- [ 1, 1, 0 ],
- [ 0, 0, 0 ],
- [ 0, 0, 0 ]
- ] );
- } );
- it( 'should select four table cells for diagonal selection', () => {
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
- assertSelectedCells( [
- [ 1, 1, 0 ],
- [ 1, 1, 0 ],
- [ 0, 0, 0 ]
- ] );
- } );
- it( 'should select row table cells', () => {
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 2 ] ) );
- assertSelectedCells( [
- [ 1, 1, 1 ],
- [ 0, 0, 0 ],
- [ 0, 0, 0 ]
- ] );
- } );
- it( 'should select column table cells', () => {
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
- assertSelectedCells( [
- [ 0, 1, 0 ],
- [ 0, 1, 0 ],
- [ 0, 1, 0 ]
- ] );
- } );
- it( 'should create proper selection on consecutive changes', () => {
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
- assertSelectedCells( [
- [ 0, 0, 0 ],
- [ 0, 1, 0 ],
- [ 0, 1, 0 ]
- ] );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- assertSelectedCells( [
- [ 0, 1, 0 ],
- [ 0, 1, 0 ],
- [ 0, 0, 0 ]
- ] );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 2 ] ) );
- assertSelectedCells( [
- [ 0, 0, 0 ],
- [ 0, 1, 1 ],
- [ 0, 1, 1 ]
- ] );
- } );
- } );
- describe( 'stopSelection()', () => {
- it( 'should not clear currently selected cells if not cell was passed', () => {
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- tableSelection.stopSelection();
- assertSelectedCells( [
- [ 1, 1, 0 ],
- [ 0, 0, 0 ],
- [ 0, 0, 0 ]
- ] );
- } );
- it( 'should change model selection if cell was passed', () => {
- const spy = sinon.spy();
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- model.document.selection.on( 'change', spy );
- tableSelection.stopSelection( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- sinon.assert.calledOnce( spy );
- } );
- it( 'should extend selection to passed table cell', () => {
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.stopSelection( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- assertSelectedCells( [
- [ 1, 1, 0 ],
- [ 0, 0, 0 ],
- [ 0, 0, 0 ]
- ] );
- } );
- } );
- describe( 'clearSelection()', () => {
- it( 'should not change model selection', () => {
- const spy = sinon.spy();
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- model.document.selection.on( 'change', spy );
- tableSelection.clearSelection();
- sinon.assert.notCalled( spy );
- } );
- it( 'should not reset model selections', () => {
- tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- tableSelection.clearSelection();
- assertSelectedCells( [
- [ 1, 1, 0 ],
- [ 0, 0, 0 ],
- [ 0, 0, 0 ]
- ] );
- } );
- } );
- describe( '* getSelectedTableCells()', () => {
- it( 'should return nothing if selection is not started', () => {
- expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [] );
- } );
- it( 'should return two table cells', () => {
- const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
- const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
- tableSelection.startSelectingFrom( firstCell );
- tableSelection.setSelectingTo( lastCell );
- expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
- firstCell, lastCell
- ] );
- } );
- it( 'should return four table cells for diagonal selection', () => {
- const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
- const lastCell = modelRoot.getNodeByPath( [ 0, 1, 1 ] );
- tableSelection.startSelectingFrom( firstCell );
- tableSelection.setSelectingTo( lastCell );
- expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
- firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), modelRoot.getNodeByPath( [ 0, 1, 0 ] ), lastCell
- ] );
- } );
- it( 'should return row table cells', () => {
- const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
- const lastCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
- tableSelection.startSelectingFrom( firstCell );
- tableSelection.setSelectingTo( lastCell );
- expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
- firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), lastCell
- ] );
- } );
- it( 'should return column table cells', () => {
- const firstCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
- const lastCell = modelRoot.getNodeByPath( [ 0, 2, 1 ] );
- tableSelection.startSelectingFrom( firstCell );
- tableSelection.setSelectingTo( lastCell );
- expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
- firstCell, modelRoot.getNodeByPath( [ 0, 1, 1 ] ), lastCell
- ] );
- } );
- } );
- } );
- // Helper method for asserting selected table cells.
- //
- // To check if a table has expected cells selected pass two dimensional array of truthy and falsy values:
- //
- // assertSelectedCells( [
- // [ 0, 1 ],
- // [ 0, 1 ]
- // ] );
- //
- // The above call will check if table has second column selected (assuming no spans).
- //
- // **Note**: This function operates on child indexes - not rows/columns.
- function assertSelectedCells( tableMap ) {
- const tableIndex = 0;
- for ( let rowIndex = 0; rowIndex < tableMap.length; rowIndex++ ) {
- const row = tableMap[ rowIndex ];
- for ( let cellIndex = 0; cellIndex < row.length; cellIndex++ ) {
- const expectSelected = row[ cellIndex ];
- if ( expectSelected ) {
- assertNodeIsSelected( [ tableIndex, rowIndex, cellIndex ] );
- } else {
- assertNodeIsNotSelected( [ tableIndex, rowIndex, cellIndex ] );
- }
- }
- }
- }
- function assertNodeIsSelected( path ) {
- const node = modelRoot.getNodeByPath( path );
- const selectionRanges = Array.from( model.document.selection.getRanges() );
- expect( selectionRanges.some( range => range.containsItem( node ) ), `Expected node [${ path }] to be selected` ).to.be.true;
- }
- function assertNodeIsNotSelected( path ) {
- const node = modelRoot.getNodeByPath( path );
- const selectionRanges = Array.from( model.document.selection.getRanges() );
- expect( selectionRanges.every( range => !range.containsItem( node ) ), `Expected node [${ path }] to be not selected` ).to.be.true;
- }
- } );
|