| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import { defaultConversion, defaultSchema, modelTable } from './_utils/utils';
- import TableSelection from '../src/tableselection';
- import { getData as getViewData } from '../../ckeditor5-engine/src/dev-utils/view';
- describe( 'TableSelection', () => {
- let editor, model, root, tableSelection;
- beforeEach( () => {
- return VirtualTestEditor.create( {
- plugins: [ TableSelection ]
- } ).then( newEditor => {
- editor = newEditor;
- model = editor.model;
- root = model.document.getRoot( 'main' );
- tableSelection = editor.plugins.get( TableSelection );
- defaultSchema( model.schema );
- defaultConversion( editor.conversion );
- } );
- } );
- afterEach( () => {
- return editor.destroy();
- } );
- describe( '#pluginName', () => {
- it( 'should provide plugin name', () => {
- expect( TableSelection.pluginName ).to.equal( 'TableSelection' );
- } );
- } );
- describe( 'start()', () => {
- it( 'should start selection', () => {
- setData( model, modelTable( [
- [ '00[]', '01' ],
- [ '10', '11' ]
- ] ) );
- const nodeByPath = root.getNodeByPath( [ 0, 0, 0 ] );
- tableSelection.startSelection( nodeByPath );
- expect( tableSelection.isSelecting ).to.be.true;
- } );
- it( 'update selection to single table cell', () => {
- setData( model, modelTable( [
- [ '00[]', '01' ],
- [ '10', '11' ]
- ] ) );
- const nodeByPath = root.getNodeByPath( [ 0, 0, 0 ] );
- tableSelection.startSelection( nodeByPath );
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ nodeByPath ] );
- } );
- it( 'should set view selection', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02' ],
- [ '10', '11', '12' ]
- ] ) );
- tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
- expect( getViewData( editor.editing.view ) ).to.equal(
- '<figure class="table">' +
- '<table>' +
- '<tbody>' +
- '<tr>' +
- '[<td>00</td>]<td>01</td><td>02</td>' +
- '</tr>' +
- '<tr>' +
- '<td>10</td><td>11</td><td>12</td>' +
- '</tr>' +
- '</tbody>' +
- '</table>' +
- '</figure>'
- );
- } );
- } );
- describe( 'stop()', () => {
- it( 'should stop selection', () => {
- setData( model, modelTable( [
- [ '00[]', '01' ],
- [ '10', '11' ]
- ] ) );
- const nodeByPath = root.getNodeByPath( [ 0, 0, 0 ] );
- tableSelection.startSelection( nodeByPath );
- expect( tableSelection.isSelecting ).to.be.true;
- tableSelection.stopSelection( nodeByPath );
- expect( tableSelection.isSelecting ).to.be.false;
- } );
- it( 'update selection to passed table cell', () => {
- setData( model, modelTable( [
- [ '00[]', '01' ],
- [ '10', '11' ]
- ] ) );
- const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
- const endNode = root.getNodeByPath( [ 0, 1, 1 ] );
- tableSelection.startSelection( startNode );
- tableSelection.stopSelection( endNode );
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
- startNode,
- root.getNodeByPath( [ 0, 0, 1 ] ),
- root.getNodeByPath( [ 0, 1, 0 ] ),
- endNode
- ] );
- } );
- it( 'should not update selection if alredy stopped', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02' ],
- [ '10', '11', '12' ]
- ] ) );
- const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
- const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
- tableSelection.startSelection( startNode );
- tableSelection.stopSelection( firstEndNode );
- expect( tableSelection.isSelecting ).to.be.false;
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
- const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
- tableSelection.stopSelection( secondEndNode );
- expect( tableSelection.isSelecting ).to.be.false;
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
- } );
- it( 'should not update selection if table cell is from another parent', () => {
- setData( model, modelTable( [
- [ '00[]', '01' ]
- ] ) + modelTable( [
- [ 'aa', 'bb' ]
- ] ) );
- tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.stopSelection( root.getNodeByPath( [ 1, 0, 1 ] ) );
- expect( tableSelection.isSelecting ).to.be.false;
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
- } );
- it( 'should update view selection', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02' ],
- [ '10', '11', '12' ]
- ] ) );
- const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
- const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
- tableSelection.startSelection( startNode );
- tableSelection.stopSelection( firstEndNode );
- expect( getViewData( editor.editing.view ) ).to.equal(
- '<figure class="table">' +
- '<table>' +
- '<tbody>' +
- '<tr>' +
- '[<td>00</td>][<td>01</td>]<td>02</td>' +
- '</tr>' +
- '<tr>' +
- '<td>10</td><td>11</td><td>12</td>' +
- '</tr>' +
- '</tbody>' +
- '</table>' +
- '</figure>'
- );
- } );
- } );
- describe( 'update()', () => {
- it( 'should update selection', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02' ],
- [ '10', '11', '12' ]
- ] ) );
- const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
- const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
- tableSelection.startSelection( startNode );
- tableSelection.updateSelection( firstEndNode );
- expect( tableSelection.isSelecting ).to.be.true;
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
- const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
- tableSelection.updateSelection( secondEndNode );
- expect( tableSelection.isSelecting ).to.be.true;
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode, secondEndNode ] );
- } );
- it( 'should not update selection if stopped', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02' ],
- [ '10', '11', '12' ]
- ] ) );
- const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
- const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
- tableSelection.startSelection( startNode );
- tableSelection.updateSelection( firstEndNode );
- expect( tableSelection.isSelecting ).to.be.true;
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
- tableSelection.stopSelection( firstEndNode );
- expect( tableSelection.isSelecting ).to.be.false;
- const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
- tableSelection.updateSelection( secondEndNode );
- expect( tableSelection.isSelecting ).to.be.false;
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
- } );
- it( 'should not update selection if table cell is from another parent', () => {
- setData( model, modelTable( [
- [ '00[]', '01' ]
- ] ) + modelTable( [
- [ 'aa', 'bb' ]
- ] ) );
- tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
- tableSelection.updateSelection( root.getNodeByPath( [ 1, 0, 1 ] ) );
- expect( tableSelection.isSelecting ).to.be.true;
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
- } );
- it( 'should update view selection', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02' ],
- [ '10', '11', '12' ]
- ] ) );
- const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
- const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
- tableSelection.startSelection( startNode );
- tableSelection.updateSelection( firstEndNode );
- expect( getViewData( editor.editing.view ) ).to.equal(
- '<figure class="table">' +
- '<table>' +
- '<tbody>' +
- '<tr>' +
- '[<td>00</td>][<td>01</td>]<td>02</td>' +
- '</tr>' +
- '<tr>' +
- '<td>10</td><td>11</td><td>12</td>' +
- '</tr>' +
- '</tbody>' +
- '</table>' +
- '</figure>'
- );
- } );
- } );
- describe( 'getSelection()', () => {
- it( 'should return empty array if not started', () => {
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [] );
- } );
- it( 'should return block of selected nodes', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02', '03' ],
- [ '10', '11', '12', '13' ],
- [ '20', '21', '22', '23' ],
- [ '30', '31', '32', '33' ]
- ] ) );
- tableSelection.startSelection( root.getNodeByPath( [ 0, 1, 1 ] ) );
- tableSelection.updateSelection( root.getNodeByPath( [ 0, 2, 2 ] ) );
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
- root.getNodeByPath( [ 0, 1, 1 ] ),
- root.getNodeByPath( [ 0, 1, 2 ] ),
- root.getNodeByPath( [ 0, 2, 1 ] ),
- root.getNodeByPath( [ 0, 2, 2 ] )
- ] );
- } );
- it( 'should return block of selected nodes (inverted selection)', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02', '03' ],
- [ '10', '11', '12', '13' ],
- [ '20', '21', '22', '23' ],
- [ '30', '31', '32', '33' ]
- ] ) );
- tableSelection.startSelection( root.getNodeByPath( [ 0, 2, 2 ] ) );
- tableSelection.updateSelection( root.getNodeByPath( [ 0, 1, 1 ] ) );
- expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
- root.getNodeByPath( [ 0, 1, 1 ] ),
- root.getNodeByPath( [ 0, 1, 2 ] ),
- root.getNodeByPath( [ 0, 2, 1 ] ),
- root.getNodeByPath( [ 0, 2, 2 ] )
- ] );
- } );
- } );
- } );
|