tableediting.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import TableEditing from '../src/tableediting';
  10. import { formatTable, formattedModelTable, modelTable } from './_utils/utils';
  11. import InsertRowCommand from '../src/commands/insertrowcommand';
  12. import InsertTableCommand from '../src/commands/inserttablecommand';
  13. import InsertColumnCommand from '../src/commands/insertcolumncommand';
  14. import RemoveRowCommand from '../src/commands/removerowcommand';
  15. import RemoveColumnCommand from '../src/commands/removecolumncommand';
  16. import SplitCellCommand from '../src/commands/splitcellcommand';
  17. import MergeCellCommand from '../src/commands/mergecellcommand';
  18. import SetTableHeadersCommand from '../src/commands/settableheaderscommand';
  19. describe( 'TableEditing', () => {
  20. let editor, model;
  21. beforeEach( () => {
  22. return VirtualTestEditor
  23. .create( {
  24. plugins: [ TableEditing, Paragraph ]
  25. } )
  26. .then( newEditor => {
  27. editor = newEditor;
  28. model = editor.model;
  29. } );
  30. } );
  31. afterEach( () => {
  32. editor.destroy();
  33. } );
  34. it( 'should set proper schema rules', () => {
  35. } );
  36. it( 'adds insertTable command', () => {
  37. expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
  38. } );
  39. it( 'adds insertRowAbove command', () => {
  40. expect( editor.commands.get( 'insertRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  41. } );
  42. it( 'adds insertRowBelow command', () => {
  43. expect( editor.commands.get( 'insertRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  44. } );
  45. it( 'adds insertColumnBefore command', () => {
  46. expect( editor.commands.get( 'insertColumnBefore' ) ).to.be.instanceOf( InsertColumnCommand );
  47. } );
  48. it( 'adds insertColumnAfter command', () => {
  49. expect( editor.commands.get( 'insertColumnAfter' ) ).to.be.instanceOf( InsertColumnCommand );
  50. } );
  51. it( 'adds removeRow command', () => {
  52. expect( editor.commands.get( 'removeRow' ) ).to.be.instanceOf( RemoveRowCommand );
  53. } );
  54. it( 'adds removeColumn command', () => {
  55. expect( editor.commands.get( 'removeColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  56. } );
  57. it( 'adds splitCellVertically command', () => {
  58. expect( editor.commands.get( 'splitCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  59. } );
  60. it( 'adds splitCellHorizontally command', () => {
  61. expect( editor.commands.get( 'splitCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  62. } );
  63. it( 'adds mergeCellRight command', () => {
  64. expect( editor.commands.get( 'mergeCellRight' ) ).to.be.instanceOf( MergeCellCommand );
  65. } );
  66. it( 'adds mergeCellLeft command', () => {
  67. expect( editor.commands.get( 'mergeCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
  68. } );
  69. it( 'adds mergeCellDown command', () => {
  70. expect( editor.commands.get( 'mergeCellDown' ) ).to.be.instanceOf( MergeCellCommand );
  71. } );
  72. it( 'adds mergeCellUp command', () => {
  73. expect( editor.commands.get( 'mergeCellUp' ) ).to.be.instanceOf( MergeCellCommand );
  74. } );
  75. it( 'adds setTableHeaders command', () => {
  76. expect( editor.commands.get( 'setTableHeaders' ) ).to.be.instanceOf( SetTableHeadersCommand );
  77. } );
  78. describe( 'conversion in data pipeline', () => {
  79. describe( 'model to view', () => {
  80. it( 'should create tbody section', () => {
  81. setModelData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  82. expect( editor.getData() ).to.equal(
  83. '<table>' +
  84. '<tbody>' +
  85. '<tr><td>foo</td></tr>' +
  86. '</tbody>' +
  87. '</table>'
  88. );
  89. } );
  90. it( 'should create thead section', () => {
  91. setModelData( model, '<table headingRows="1"><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  92. expect( editor.getData() ).to.equal(
  93. '<table>' +
  94. '<thead>' +
  95. '<tr><th>foo</th></tr>' +
  96. '</thead>' +
  97. '</table>'
  98. );
  99. } );
  100. } );
  101. describe( 'view to model', () => {
  102. it( 'should convert table', () => {
  103. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  104. expect( getModelData( model, { withoutSelection: true } ) )
  105. .to.equal( '<table><tableRow><tableCell>foo</tableCell></tableRow></table>' );
  106. } );
  107. } );
  108. } );
  109. describe( 'caret movement', () => {
  110. let domEvtDataStub;
  111. beforeEach( () => {
  112. domEvtDataStub = {
  113. keyCode: getCode( 'Tab' ),
  114. preventDefault: sinon.spy(),
  115. stopPropagation: sinon.spy()
  116. };
  117. } );
  118. it( 'should do nothing if not tab pressed', () => {
  119. setModelData( model, modelTable( [
  120. [ '11', '12[]' ]
  121. ] ) );
  122. domEvtDataStub.keyCode = getCode( 'a' );
  123. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  124. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  125. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  126. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  127. [ '11', '12[]' ]
  128. ] ) );
  129. } );
  130. it( 'should do nothing if Ctrl+Tab is pressed', () => {
  131. setModelData( model, modelTable( [
  132. [ '11', '12[]' ]
  133. ] ) );
  134. domEvtDataStub.ctrlKey = true;
  135. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  136. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  137. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  138. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  139. [ '11', '12[]' ]
  140. ] ) );
  141. } );
  142. describe( 'on TAB', () => {
  143. it( 'should do nothing if selection is not in a table', () => {
  144. setModelData( model, '[]' + modelTable( [
  145. [ '11', '12' ]
  146. ] ) );
  147. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  148. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  149. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  150. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  151. [ '11', '12' ]
  152. ] ) );
  153. } );
  154. it( 'should move to next cell', () => {
  155. setModelData( model, modelTable( [
  156. [ '11[]', '12' ]
  157. ] ) );
  158. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  159. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  160. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  161. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  162. [ '11', '[12]' ]
  163. ] ) );
  164. } );
  165. it( 'should create another row and move to first cell in new row', () => {
  166. setModelData( model, modelTable( [
  167. [ '11', '[12]' ]
  168. ] ) );
  169. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  170. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  171. [ '11', '12' ],
  172. [ '[]', '' ]
  173. ] ) );
  174. } );
  175. it( 'should move to the first cell of next row if on end of a row', () => {
  176. setModelData( model, modelTable( [
  177. [ '11', '12[]' ],
  178. [ '21', '22' ]
  179. ] ) );
  180. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  181. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  182. [ '11', '12' ],
  183. [ '[21]', '22' ]
  184. ] ) );
  185. } );
  186. describe( 'on table widget selected', () => {
  187. it( 'should move caret to the first table cell on TAB', () => {
  188. const spy = sinon.spy();
  189. editor.editing.view.document.on( 'keydown', spy );
  190. setModelData( model, '[' + modelTable( [
  191. [ '11', '12' ]
  192. ] ) + ']' );
  193. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  194. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  195. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  196. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  197. [ '[11]', '12' ]
  198. ] ) );
  199. // Should cancel event - so no other tab handler is called.
  200. sinon.assert.notCalled( spy );
  201. } );
  202. it( 'shouldn\' do anything on other blocks', () => {
  203. const spy = sinon.spy();
  204. editor.editing.view.document.on( 'keydown', spy );
  205. setModelData( model, '[<paragraph>foo</paragraph>]' );
  206. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  207. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  208. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  209. expect( formatTable( getModelData( model ) ) ).to.equal( '[<paragraph>foo</paragraph>]' );
  210. // Should not cancel event.
  211. sinon.assert.calledOnce( spy );
  212. } );
  213. } );
  214. } );
  215. describe( 'on SHIFT+TAB', () => {
  216. beforeEach( () => {
  217. domEvtDataStub.shiftKey = true;
  218. } );
  219. it( 'should do nothing if selection is not in a table', () => {
  220. setModelData( model, '[]' + modelTable( [
  221. [ '11', '12' ]
  222. ] ) );
  223. domEvtDataStub.keyCode = getCode( 'Tab' );
  224. domEvtDataStub.shiftKey = true;
  225. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  226. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  227. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  228. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  229. [ '11', '12' ]
  230. ] ) );
  231. } );
  232. it( 'should move to previous cell', () => {
  233. setModelData( model, modelTable( [
  234. [ '11', '12[]' ]
  235. ] ) );
  236. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  237. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  238. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  239. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  240. [ '[11]', '12' ]
  241. ] ) );
  242. } );
  243. it( 'should not move if caret is in first table cell', () => {
  244. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  245. [ '[]11', '12' ]
  246. ] ) );
  247. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  248. expect( formatTable( getModelData( model ) ) ).to.equal(
  249. '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
  250. );
  251. } );
  252. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  253. setModelData( model, modelTable( [
  254. [ '11', '12' ],
  255. [ '[]21', '22' ]
  256. ] ) );
  257. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  258. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  259. [ '11', '[12]' ],
  260. [ '21', '22' ]
  261. ] ) );
  262. } );
  263. } );
  264. } );
  265. } );