| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
- import TableEditing from '../src/tableediting';
- import { formatTable, formattedModelTable, modelTable } from './_utils/utils';
- import InsertRowCommand from '../src/commands/insertrowcommand';
- import InsertTableCommand from '../src/commands/inserttablecommand';
- import InsertColumnCommand from '../src/commands/insertcolumncommand';
- import RemoveRowCommand from '../src/commands/removerowcommand';
- import RemoveColumnCommand from '../src/commands/removecolumncommand';
- import SplitCellCommand from '../src/commands/splitcellcommand';
- import MergeCellCommand from '../src/commands/mergecellcommand';
- import SetHeaderRowCommand from '../src/commands/setheaderrowcommand';
- import SetHeaderColumnCommand from '../src/commands/setheadercolumncommand';
- describe( 'TableEditing', () => {
- let editor, model;
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ TableEditing, Paragraph ]
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- } );
- } );
- afterEach( () => {
- editor.destroy();
- } );
- it( 'should set proper schema rules', () => {
- } );
- it( 'adds insertTable command', () => {
- expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
- } );
- it( 'adds insertRowAbove command', () => {
- expect( editor.commands.get( 'insertRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
- } );
- it( 'adds insertRowBelow command', () => {
- expect( editor.commands.get( 'insertRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
- } );
- it( 'adds insertColumnBefore command', () => {
- expect( editor.commands.get( 'insertColumnBefore' ) ).to.be.instanceOf( InsertColumnCommand );
- } );
- it( 'adds insertColumnAfter command', () => {
- expect( editor.commands.get( 'insertColumnAfter' ) ).to.be.instanceOf( InsertColumnCommand );
- } );
- it( 'adds removeRow command', () => {
- expect( editor.commands.get( 'removeRow' ) ).to.be.instanceOf( RemoveRowCommand );
- } );
- it( 'adds removeColumn command', () => {
- expect( editor.commands.get( 'removeColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
- } );
- it( 'adds splitCellVertically command', () => {
- expect( editor.commands.get( 'splitCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
- } );
- it( 'adds splitCellHorizontally command', () => {
- expect( editor.commands.get( 'splitCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
- } );
- it( 'adds mergeCellRight command', () => {
- expect( editor.commands.get( 'mergeCellRight' ) ).to.be.instanceOf( MergeCellCommand );
- } );
- it( 'adds mergeCellLeft command', () => {
- expect( editor.commands.get( 'mergeCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
- } );
- it( 'adds mergeCellDown command', () => {
- expect( editor.commands.get( 'mergeCellDown' ) ).to.be.instanceOf( MergeCellCommand );
- } );
- it( 'adds mergeCellUp command', () => {
- expect( editor.commands.get( 'mergeCellUp' ) ).to.be.instanceOf( MergeCellCommand );
- } );
- it( 'adds setColumnHeader command', () => {
- expect( editor.commands.get( 'setColumnHeader' ) ).to.be.instanceOf( SetHeaderColumnCommand );
- } );
- it( 'adds setRowHeader command', () => {
- expect( editor.commands.get( 'setRowHeader' ) ).to.be.instanceOf( SetHeaderRowCommand );
- } );
- describe( 'conversion in data pipeline', () => {
- describe( 'model to view', () => {
- it( 'should create tbody section', () => {
- setModelData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
- expect( editor.getData() ).to.equal(
- '<table>' +
- '<tbody>' +
- '<tr><td>foo</td></tr>' +
- '</tbody>' +
- '</table>'
- );
- } );
- it( 'should create thead section', () => {
- setModelData( model, '<table headingRows="1"><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
- expect( editor.getData() ).to.equal(
- '<table>' +
- '<thead>' +
- '<tr><th>foo</th></tr>' +
- '</thead>' +
- '</table>'
- );
- } );
- } );
- describe( 'view to model', () => {
- it( 'should convert table', () => {
- editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '<table><tableRow><tableCell>foo</tableCell></tableRow></table>' );
- } );
- } );
- } );
- describe( 'caret movement', () => {
- let domEvtDataStub;
- beforeEach( () => {
- domEvtDataStub = {
- keyCode: getCode( 'Tab' ),
- preventDefault: sinon.spy(),
- stopPropagation: sinon.spy()
- };
- } );
- it( 'should do nothing if not tab pressed', () => {
- setModelData( model, modelTable( [
- [ '11', '12[]' ]
- ] ) );
- domEvtDataStub.keyCode = getCode( 'a' );
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- sinon.assert.notCalled( domEvtDataStub.preventDefault );
- sinon.assert.notCalled( domEvtDataStub.stopPropagation );
- expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
- [ '11', '12[]' ]
- ] ) );
- } );
- it( 'should do nothing if Ctrl+Tab is pressed', () => {
- setModelData( model, modelTable( [
- [ '11', '12[]' ]
- ] ) );
- domEvtDataStub.ctrlKey = true;
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- sinon.assert.notCalled( domEvtDataStub.preventDefault );
- sinon.assert.notCalled( domEvtDataStub.stopPropagation );
- expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
- [ '11', '12[]' ]
- ] ) );
- } );
- describe( 'on TAB', () => {
- it( 'should do nothing if selection is not in a table', () => {
- setModelData( model, '[]' + modelTable( [
- [ '11', '12' ]
- ] ) );
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- sinon.assert.notCalled( domEvtDataStub.preventDefault );
- sinon.assert.notCalled( domEvtDataStub.stopPropagation );
- expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
- [ '11', '12' ]
- ] ) );
- } );
- it( 'should move to next cell', () => {
- setModelData( model, modelTable( [
- [ '11[]', '12' ]
- ] ) );
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- sinon.assert.calledOnce( domEvtDataStub.preventDefault );
- sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
- expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
- [ '11', '[12]' ]
- ] ) );
- } );
- it( 'should create another row and move to first cell in new row', () => {
- setModelData( model, modelTable( [
- [ '11', '[12]' ]
- ] ) );
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
- [ '11', '12' ],
- [ '[]', '' ]
- ] ) );
- } );
- it( 'should move to the first cell of next row if on end of a row', () => {
- setModelData( model, modelTable( [
- [ '11', '12[]' ],
- [ '21', '22' ]
- ] ) );
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
- [ '11', '12' ],
- [ '[21]', '22' ]
- ] ) );
- } );
- describe( 'on table widget selected', () => {
- it( 'should move caret to the first table cell on TAB', () => {
- const spy = sinon.spy();
- editor.editing.view.document.on( 'keydown', spy );
- setModelData( model, '[' + modelTable( [
- [ '11', '12' ]
- ] ) + ']' );
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- sinon.assert.calledOnce( domEvtDataStub.preventDefault );
- sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
- expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
- [ '[11]', '12' ]
- ] ) );
- // Should cancel event - so no other tab handler is called.
- sinon.assert.notCalled( spy );
- } );
- it( 'shouldn\' do anything on other blocks', () => {
- const spy = sinon.spy();
- editor.editing.view.document.on( 'keydown', spy );
- setModelData( model, '[<paragraph>foo</paragraph>]' );
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- sinon.assert.notCalled( domEvtDataStub.preventDefault );
- sinon.assert.notCalled( domEvtDataStub.stopPropagation );
- expect( formatTable( getModelData( model ) ) ).to.equal( '[<paragraph>foo</paragraph>]' );
- // Should not cancel event.
- sinon.assert.calledOnce( spy );
- } );
- } );
- } );
- describe( 'on SHIFT+TAB', () => {
- beforeEach( () => {
- domEvtDataStub.shiftKey = true;
- } );
- it( 'should do nothing if selection is not in a table', () => {
- setModelData( model, '[]' + modelTable( [
- [ '11', '12' ]
- ] ) );
- domEvtDataStub.keyCode = getCode( 'Tab' );
- domEvtDataStub.shiftKey = true;
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- sinon.assert.notCalled( domEvtDataStub.preventDefault );
- sinon.assert.notCalled( domEvtDataStub.stopPropagation );
- expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
- [ '11', '12' ]
- ] ) );
- } );
- it( 'should move to previous cell', () => {
- setModelData( model, modelTable( [
- [ '11', '12[]' ]
- ] ) );
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- sinon.assert.calledOnce( domEvtDataStub.preventDefault );
- sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
- expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
- [ '[11]', '12' ]
- ] ) );
- } );
- it( 'should not move if caret is in first table cell', () => {
- setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
- [ '[]11', '12' ]
- ] ) );
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- expect( formatTable( getModelData( model ) ) ).to.equal(
- '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
- );
- } );
- it( 'should move to the last cell of previous row if on beginning of a row', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '[]21', '22' ]
- ] ) );
- editor.editing.view.document.fire( 'keydown', domEvtDataStub );
- expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
- [ '11', '[12]' ],
- [ '21', '22' ]
- ] ) );
- } );
- } );
- } );
- } );
|