tableediting.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 SetHeaderRowCommand from '../src/commands/setheaderrowcommand';
  19. import SetHeaderColumnCommand from '../src/commands/setheadercolumncommand';
  20. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  21. describe( 'TableEditing', () => {
  22. let editor, model;
  23. beforeEach( () => {
  24. return VirtualTestEditor
  25. .create( {
  26. plugins: [ TableEditing, Paragraph, UndoEditing ]
  27. } )
  28. .then( newEditor => {
  29. editor = newEditor;
  30. model = editor.model;
  31. } );
  32. } );
  33. afterEach( () => {
  34. editor.destroy();
  35. } );
  36. it( 'should set proper schema rules', () => {
  37. } );
  38. it( 'adds insertTable command', () => {
  39. expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
  40. } );
  41. it( 'adds insertRowAbove command', () => {
  42. expect( editor.commands.get( 'insertTableRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  43. } );
  44. it( 'adds insertRowBelow command', () => {
  45. expect( editor.commands.get( 'insertTableRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  46. } );
  47. it( 'adds insertColumnBefore command', () => {
  48. expect( editor.commands.get( 'insertTableColumnBefore' ) ).to.be.instanceOf( InsertColumnCommand );
  49. } );
  50. it( 'adds insertColumnAfter command', () => {
  51. expect( editor.commands.get( 'insertTableColumnAfter' ) ).to.be.instanceOf( InsertColumnCommand );
  52. } );
  53. it( 'adds removeRow command', () => {
  54. expect( editor.commands.get( 'removeTableRow' ) ).to.be.instanceOf( RemoveRowCommand );
  55. } );
  56. it( 'adds removeColumn command', () => {
  57. expect( editor.commands.get( 'removeTableColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  58. } );
  59. it( 'adds splitCellVertically command', () => {
  60. expect( editor.commands.get( 'splitTableCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  61. } );
  62. it( 'adds splitCellHorizontally command', () => {
  63. expect( editor.commands.get( 'splitTableCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  64. } );
  65. it( 'adds mergeCellRight command', () => {
  66. expect( editor.commands.get( 'mergeTableCellRight' ) ).to.be.instanceOf( MergeCellCommand );
  67. } );
  68. it( 'adds mergeCellLeft command', () => {
  69. expect( editor.commands.get( 'mergeTableCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
  70. } );
  71. it( 'adds mergeCellDown command', () => {
  72. expect( editor.commands.get( 'mergeTableCellDown' ) ).to.be.instanceOf( MergeCellCommand );
  73. } );
  74. it( 'adds mergeCellUp command', () => {
  75. expect( editor.commands.get( 'mergeTableCellUp' ) ).to.be.instanceOf( MergeCellCommand );
  76. } );
  77. it( 'adds setColumnHeader command', () => {
  78. expect( editor.commands.get( 'setTableColumnHeader' ) ).to.be.instanceOf( SetHeaderColumnCommand );
  79. } );
  80. it( 'adds setRowHeader command', () => {
  81. expect( editor.commands.get( 'setTableRowHeader' ) ).to.be.instanceOf( SetHeaderRowCommand );
  82. } );
  83. describe( 'conversion in data pipeline', () => {
  84. describe( 'model to view', () => {
  85. it( 'should create tbody section', () => {
  86. setModelData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  87. expect( editor.getData() ).to.equal(
  88. '<figure class="table">' +
  89. '<table>' +
  90. '<tbody>' +
  91. '<tr><td>foo</td></tr>' +
  92. '</tbody>' +
  93. '</table>' +
  94. '</figure>'
  95. );
  96. } );
  97. it( 'should create thead section', () => {
  98. setModelData( model, '<table headingRows="1"><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  99. expect( editor.getData() ).to.equal(
  100. '<figure class="table">' +
  101. '<table>' +
  102. '<thead>' +
  103. '<tr><th>foo</th></tr>' +
  104. '</thead>' +
  105. '</table>' +
  106. '</figure>'
  107. );
  108. } );
  109. } );
  110. describe( 'view to model', () => {
  111. it( 'should convert table', () => {
  112. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  113. expect( getModelData( model, { withoutSelection: true } ) )
  114. .to.equal( '<table><tableRow><tableCell>foo</tableCell></tableRow></table>' );
  115. } );
  116. } );
  117. } );
  118. describe( 'caret movement', () => {
  119. let domEvtDataStub;
  120. beforeEach( () => {
  121. domEvtDataStub = {
  122. keyCode: getCode( 'Tab' ),
  123. preventDefault: sinon.spy(),
  124. stopPropagation: sinon.spy()
  125. };
  126. } );
  127. it( 'should do nothing if not tab pressed', () => {
  128. setModelData( model, modelTable( [
  129. [ '11', '12[]' ]
  130. ] ) );
  131. domEvtDataStub.keyCode = getCode( 'a' );
  132. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  133. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  134. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  135. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  136. [ '11', '12[]' ]
  137. ] ) );
  138. } );
  139. it( 'should do nothing if Ctrl+Tab is pressed', () => {
  140. setModelData( model, modelTable( [
  141. [ '11', '12[]' ]
  142. ] ) );
  143. domEvtDataStub.ctrlKey = true;
  144. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  145. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  146. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  147. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  148. [ '11', '12[]' ]
  149. ] ) );
  150. } );
  151. describe( 'on TAB', () => {
  152. it( 'should do nothing if selection is not in a table', () => {
  153. setModelData( model, '[]' + modelTable( [
  154. [ '11', '12' ]
  155. ] ) );
  156. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  157. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  158. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  159. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  160. [ '11', '12' ]
  161. ] ) );
  162. } );
  163. it( 'should move to next cell', () => {
  164. setModelData( model, modelTable( [
  165. [ '11[]', '12' ]
  166. ] ) );
  167. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  168. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  169. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  170. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  171. [ '11', '[12]' ]
  172. ] ) );
  173. } );
  174. it( 'should create another row and move to first cell in new row', () => {
  175. setModelData( model, modelTable( [
  176. [ '11', '[12]' ]
  177. ] ) );
  178. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  179. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  180. [ '11', '12' ],
  181. [ '[]', '' ]
  182. ] ) );
  183. } );
  184. it( 'should move to the first cell of next row if on end of a row', () => {
  185. setModelData( model, modelTable( [
  186. [ '11', '12[]' ],
  187. [ '21', '22' ]
  188. ] ) );
  189. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  190. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  191. [ '11', '12' ],
  192. [ '[21]', '22' ]
  193. ] ) );
  194. } );
  195. describe( 'on table widget selected', () => {
  196. beforeEach( () => {
  197. editor.model.schema.register( 'block', {
  198. allowWhere: '$block',
  199. allowContentOf: '$block',
  200. isObject: true
  201. } );
  202. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  203. } );
  204. it( 'should move caret to the first table cell on TAB', () => {
  205. const spy = sinon.spy();
  206. editor.editing.view.document.on( 'keydown', spy );
  207. setModelData( model, '[' + modelTable( [
  208. [ '11', '12' ]
  209. ] ) + ']' );
  210. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  211. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  212. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  213. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  214. [ '[11]', '12' ]
  215. ] ) );
  216. // Should cancel event - so no other tab handler is called.
  217. sinon.assert.notCalled( spy );
  218. } );
  219. it( 'shouldn\'t do anything on other blocks', () => {
  220. const spy = sinon.spy();
  221. editor.editing.view.document.on( 'keydown', spy );
  222. setModelData( model, '[<block>foo</block>]' );
  223. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  224. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  225. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  226. expect( formatTable( getModelData( model ) ) ).to.equal( '[<block>foo</block>]' );
  227. // Should not cancel event.
  228. sinon.assert.calledOnce( spy );
  229. } );
  230. } );
  231. } );
  232. describe( 'on SHIFT+TAB', () => {
  233. beforeEach( () => {
  234. domEvtDataStub.shiftKey = true;
  235. } );
  236. it( 'should do nothing if selection is not in a table', () => {
  237. setModelData( model, '[]' + modelTable( [
  238. [ '11', '12' ]
  239. ] ) );
  240. domEvtDataStub.keyCode = getCode( 'Tab' );
  241. domEvtDataStub.shiftKey = true;
  242. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  243. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  244. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  245. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  246. [ '11', '12' ]
  247. ] ) );
  248. } );
  249. it( 'should move to previous cell', () => {
  250. setModelData( model, modelTable( [
  251. [ '11', '12[]' ]
  252. ] ) );
  253. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  254. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  255. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  256. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  257. [ '[11]', '12' ]
  258. ] ) );
  259. } );
  260. it( 'should not move if caret is in first table cell', () => {
  261. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  262. [ '[]11', '12' ]
  263. ] ) );
  264. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  265. expect( formatTable( getModelData( model ) ) ).to.equal(
  266. '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
  267. );
  268. } );
  269. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  270. setModelData( model, modelTable( [
  271. [ '11', '12' ],
  272. [ '[]21', '22' ]
  273. ] ) );
  274. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  275. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  276. [ '11', '[12]' ],
  277. [ '21', '22' ]
  278. ] ) );
  279. } );
  280. } );
  281. } );
  282. } );