tableediting.js 13 KB

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