| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /**
- * @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, stringify as stringifyModel } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import TableEditing from '../src/tableediting';
- import TableSelection from '../src/tableselection';
- // import { assertSelectedCells, modelTable, viewTable } from './_utils/utils';
- import { modelTable } from './_utils/utils';
- // import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
- // import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
- import DocumentFragment from '@ckeditor/ckeditor5-engine/src/model/documentfragment';
- 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( 'init()', () => {
- describe( 'plugin disabling support', () => {
- let plugin;
- beforeEach( () => {
- plugin = editor.plugins.get( TableSelection );
- } );
- it( 'should collapse multi-cell selection when the plugin gets disabled', () => {
- const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
- const lastCell = modelRoot.getNodeByPath( [ 0, 1, 1 ] );
- tableSelection._setCellSelection(
- firstCell,
- lastCell
- );
- plugin.forceDisabled( 'foo' );
- const ranges = [ ...model.document.selection.getRanges() ];
- expect( ranges ).to.have.length( 1 );
- expect( ranges[ 0 ].isCollapsed ).to.be.true;
- expect( ranges[ 0 ].start.path ).to.deep.equal( [ 0, 0, 0, 0, 0 ] );
- } );
- it( 'should do nothing if there were no multi-cell selections', () => {
- plugin.forceDisabled( 'foo' );
- const ranges = [ ...model.document.selection.getRanges() ];
- expect( ranges ).to.have.length( 1 );
- expect( ranges[ 0 ].isCollapsed ).to.be.true;
- expect( ranges[ 0 ].start.path ).to.deep.equal( [ 0, 0, 0, 0, 2 ] );
- } );
- } );
- } );
- describe( 'selection by shift+click', () => {
- it( 'should...', () => {
- // tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- // tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- // tableSelection.stopSelection();
- // assertSelectedCells( model, [
- // [ 1, 1, 0 ],
- // [ 0, 0, 0 ],
- // [ 0, 0, 0 ]
- // ] );
- } );
- } );
- describe( 'selection by mouse drag', () => {
- it( 'should...', () => {
- // tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
- // tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
- // tableSelection.stopSelection();
- // assertSelectedCells( model, [
- // [ 1, 1, 0 ],
- // [ 0, 0, 0 ],
- // [ 0, 0, 0 ]
- // ] );
- } );
- } );
- describe( 'getSelectedTableCells()', () => {
- it( 'should return nothing if selection is not started', () => {
- expect( tableSelection.getSelectedTableCells() ).to.be.null;
- } );
- it( 'should return two table cells', () => {
- const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
- const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
- tableSelection._setCellSelection(
- firstCell,
- lastCell
- );
- expect( 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._setCellSelection(
- firstCell,
- lastCell
- );
- expect( 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._setCellSelection(
- firstCell,
- lastCell
- );
- expect( 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._setCellSelection( firstCell, lastCell );
- expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
- firstCell, modelRoot.getNodeByPath( [ 0, 1, 1 ] ), lastCell
- ] );
- } );
- it( 'should return cells in source order despite backward selection', () => {
- const firstCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
- const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
- tableSelection._setCellSelection( firstCell, lastCell );
- expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
- lastCell, firstCell
- ] );
- } );
- } );
- describe( 'getSelectionAsFragment()', () => {
- it( 'should return undefined if no table cells are selected', () => {
- expect( tableSelection.getSelectionAsFragment() ).to.be.null;
- } );
- it( 'should return document fragment for selected table cells', () => {
- tableSelection._setCellSelection(
- modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
- modelRoot.getNodeByPath( [ 0, 1, 1 ] )
- );
- expect( tableSelection.getSelectionAsFragment() ).to.be.instanceOf( DocumentFragment );
- } );
- it( 'should return cells in the source order in case of forward selection', () => {
- tableSelection._setCellSelection(
- modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
- modelRoot.getNodeByPath( [ 0, 1, 1 ] )
- );
- expect( stringifyModel( tableSelection.getSelectionAsFragment() ) ).to.equal( modelTable( [
- [ '11', '12' ],
- [ '21', '22' ]
- ] ) );
- } );
- it( 'should return cells in the source order in case of backward selection', () => {
- tableSelection._setCellSelection(
- modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
- modelRoot.getNodeByPath( [ 0, 0, 0 ] )
- );
- expect( editor.model.document.selection.isBackward ).to.be.true;
- expect( stringifyModel( tableSelection.getSelectionAsFragment() ) ).to.equal( modelTable( [
- [ '11', '12' ],
- [ '21', '22' ]
- ] ) );
- } );
- } );
- } );
|