8
0

table-integration.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Widget from '@ckeditor/ckeditor5-widget/src/widget';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  8. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  9. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  10. import {
  11. getData as getModelData,
  12. setData as setModelData
  13. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { parse as parseView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  15. import TableEditing from '../src/tableediting';
  16. import { formatTable, formattedModelTable, modelTable, viewTable } from './_utils/utils';
  17. describe( 'Table feature – integration', () => {
  18. describe( 'with clipboard', () => {
  19. it( 'pastes td as p when pasting into the table', () => {
  20. return VirtualTestEditor
  21. .create( { plugins: [ Paragraph, TableEditing, Widget, Clipboard ] } )
  22. .then( newEditor => {
  23. const editor = newEditor;
  24. const clipboard = editor.plugins.get( 'Clipboard' );
  25. setModelData( editor.model, modelTable( [ [ 'foo[]' ] ] ) );
  26. clipboard.fire( 'inputTransformation', {
  27. content: parseView( '<td>bar</td>' )
  28. } );
  29. expect( formatTable( getModelData( editor.model ) ) ).to.equal( formattedModelTable( [
  30. [ 'foobar[]' ]
  31. ] ) );
  32. } );
  33. } );
  34. it( 'pastes td as p when pasting into the p', () => {
  35. return VirtualTestEditor
  36. .create( { plugins: [ Paragraph, TableEditing, Widget, Clipboard ] } )
  37. .then( newEditor => {
  38. const editor = newEditor;
  39. const clipboard = editor.plugins.get( 'Clipboard' );
  40. setModelData( editor.model, '<paragraph>foo[]</paragraph>' );
  41. clipboard.fire( 'inputTransformation', {
  42. content: parseView( '<td>bar</td>' )
  43. } );
  44. expect( formatTable( getModelData( editor.model ) ) ).to.equal( '<paragraph>foobar[]</paragraph>' );
  45. } );
  46. } );
  47. } );
  48. describe( 'with undo', () => {
  49. let editor, doc, root;
  50. beforeEach( () => {
  51. return VirtualTestEditor
  52. .create( { plugins: [ Paragraph, TableEditing, Widget, UndoEditing ] } )
  53. .then( newEditor => {
  54. editor = newEditor;
  55. doc = editor.model.document;
  56. root = doc.getRoot();
  57. } );
  58. } );
  59. it( 'fixing empty roots should be transparent to undo', () => {
  60. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  61. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  62. editor.data.set( viewTable( [ [ 'foo' ] ] ) );
  63. expect( editor.getData() ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  64. editor.model.change( writer => {
  65. writer.remove( root.getChild( 0 ) );
  66. } );
  67. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  68. editor.execute( 'undo' );
  69. expect( editor.getData() ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  70. editor.execute( 'redo' );
  71. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  72. editor.execute( 'undo' );
  73. expect( editor.getData() ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  74. } );
  75. it( 'fixing empty roots should be transparent to undo - multiple roots', () => {
  76. const otherRoot = doc.createRoot( '$root', 'otherRoot' );
  77. editor.data.set( viewTable( [ [ 'foo' ] ] ), 'main' );
  78. editor.data.set( viewTable( [ [ 'foo' ] ] ), 'otherRoot' );
  79. editor.model.change( writer => {
  80. writer.remove( root.getChild( 0 ) );
  81. } );
  82. editor.model.change( writer => {
  83. writer.remove( otherRoot.getChild( 0 ) );
  84. } );
  85. expect( editor.data.get( 'main' ) ).to.equal( '<p>&nbsp;</p>' );
  86. expect( editor.data.get( 'otherRoot' ) ).to.equal( '<p>&nbsp;</p>' );
  87. editor.execute( 'undo' );
  88. expect( editor.data.get( 'main' ) ).to.equal( '<p>&nbsp;</p>' );
  89. expect( editor.data.get( 'otherRoot' ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  90. editor.execute( 'undo' );
  91. expect( editor.data.get( 'main' ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  92. expect( editor.data.get( 'otherRoot' ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  93. } );
  94. } );
  95. } );