tableui.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import { _clear as clearTranslations, add as addTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import TableEditing from '../src/tableediting';
  10. import TableUI from '../src/tableui';
  11. import InsertTableView from '../src/ui/inserttableview';
  12. import SwitchButtonView from '@ckeditor/ckeditor5-ui/src/button/switchbuttonview';
  13. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  14. import ListSeparatorView from '@ckeditor/ckeditor5-ui/src/list/listseparatorview';
  15. import SplitButtonView from '@ckeditor/ckeditor5-ui/src/dropdown/button/splitbuttonview';
  16. describe( 'TableUI', () => {
  17. let editor, element;
  18. testUtils.createSinonSandbox();
  19. before( () => {
  20. addTranslations( 'en', {} );
  21. addTranslations( 'pl', {} );
  22. } );
  23. after( () => {
  24. clearTranslations();
  25. } );
  26. beforeEach( () => {
  27. element = document.createElement( 'div' );
  28. document.body.appendChild( element );
  29. return ClassicTestEditor
  30. .create( element, {
  31. plugins: [ TableEditing, TableUI ]
  32. } )
  33. .then( newEditor => {
  34. editor = newEditor;
  35. } );
  36. } );
  37. afterEach( () => {
  38. element.remove();
  39. return editor.destroy();
  40. } );
  41. describe( 'insertTable dropdown', () => {
  42. let insertTable;
  43. beforeEach( () => {
  44. insertTable = editor.ui.componentFactory.create( 'insertTable' );
  45. insertTable.isOpen = true; // Dropdown is lazy loaded, so make sure its open (#6193).
  46. } );
  47. it( 'should register insertTable button', () => {
  48. expect( insertTable ).to.be.instanceOf( DropdownView );
  49. expect( insertTable.buttonView.label ).to.equal( 'Insert table' );
  50. expect( insertTable.buttonView.icon ).to.match( /<svg / );
  51. } );
  52. it( 'should bind to insertTable command', () => {
  53. const command = editor.commands.get( 'insertTable' );
  54. command.isEnabled = true;
  55. expect( insertTable.buttonView.isOn ).to.be.true;
  56. expect( insertTable.buttonView.isEnabled ).to.be.true;
  57. command.isEnabled = false;
  58. expect( insertTable.buttonView.isEnabled ).to.be.false;
  59. } );
  60. it( 'should execute insertTable command on button execute event', () => {
  61. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  62. const tableSizeView = insertTable.panelView.children.first;
  63. tableSizeView.rows = 2;
  64. tableSizeView.columns = 7;
  65. insertTable.fire( 'execute' );
  66. sinon.assert.calledOnce( executeSpy );
  67. sinon.assert.calledWithExactly( executeSpy, 'insertTable', { rows: 2, columns: 7 } );
  68. } );
  69. it( 'should reset rows & columns on dropdown open', () => {
  70. insertTable.isOpen = true;
  71. const tableSizeView = insertTable.panelView.children.first;
  72. expect( tableSizeView.rows ).to.equal( 0 );
  73. expect( tableSizeView.columns ).to.equal( 0 );
  74. tableSizeView.rows = 2;
  75. tableSizeView.columns = 2;
  76. insertTable.buttonView.fire( 'open' );
  77. expect( tableSizeView.rows ).to.equal( 0 );
  78. expect( tableSizeView.columns ).to.equal( 0 );
  79. } );
  80. it( 'is not fully initialized when not open', () => {
  81. const dropdown = editor.ui.componentFactory.create( 'insertTable' );
  82. for ( const childView of dropdown.panelView.children ) {
  83. expect( childView ).not.to.be.instanceOf( InsertTableView );
  84. }
  85. } );
  86. } );
  87. describe( 'tableRow dropdown', () => {
  88. let dropdown;
  89. beforeEach( () => {
  90. dropdown = editor.ui.componentFactory.create( 'tableRow' );
  91. } );
  92. it( 'have button with proper properties set', () => {
  93. expect( dropdown ).to.be.instanceOf( DropdownView );
  94. const button = dropdown.buttonView;
  95. expect( button.isOn ).to.be.false;
  96. expect( button.tooltip ).to.be.true;
  97. expect( button.label ).to.equal( 'Row' );
  98. expect( button.icon ).to.match( /<svg / );
  99. } );
  100. it( 'should have proper items in panel', () => {
  101. const listView = dropdown.listView;
  102. const labels = listView.items.map( item => item instanceof ListSeparatorView ? '|' : item.children.first.label );
  103. expect( labels ).to.deep.equal(
  104. [ 'Header row', '|', 'Insert row above', 'Insert row below', 'Delete row', 'Select row' ]
  105. );
  106. } );
  107. it( 'should bind items in panel to proper commands', () => {
  108. const items = dropdown.listView.items;
  109. const setRowHeaderCommand = editor.commands.get( 'setTableRowHeader' );
  110. const insertRowBelowCommand = editor.commands.get( 'insertTableRowBelow' );
  111. const insertRowAboveCommand = editor.commands.get( 'insertTableRowAbove' );
  112. const removeRowCommand = editor.commands.get( 'removeTableRow' );
  113. const selectRowCommand = editor.commands.get( 'selectTableRow' );
  114. setRowHeaderCommand.isEnabled = true;
  115. insertRowBelowCommand.isEnabled = true;
  116. insertRowAboveCommand.isEnabled = true;
  117. removeRowCommand.isEnabled = true;
  118. selectRowCommand.isEnabled = true;
  119. expect( items.first.children.first.isEnabled ).to.be.true;
  120. expect( items.get( 2 ).children.first.isEnabled ).to.be.true;
  121. expect( items.get( 3 ).children.first.isEnabled ).to.be.true;
  122. expect( items.get( 4 ).children.first.isEnabled ).to.be.true;
  123. expect( items.get( 5 ).children.first.isEnabled ).to.be.true;
  124. expect( dropdown.buttonView.isEnabled ).to.be.true;
  125. setRowHeaderCommand.isEnabled = false;
  126. expect( items.first.children.first.isEnabled ).to.be.false;
  127. expect( dropdown.buttonView.isEnabled ).to.be.true;
  128. insertRowAboveCommand.isEnabled = false;
  129. expect( items.get( 2 ).children.first.isEnabled ).to.be.false;
  130. expect( dropdown.buttonView.isEnabled ).to.be.true;
  131. insertRowBelowCommand.isEnabled = false;
  132. expect( items.get( 3 ).children.first.isEnabled ).to.be.false;
  133. expect( dropdown.buttonView.isEnabled ).to.be.true;
  134. removeRowCommand.isEnabled = false;
  135. expect( items.get( 4 ).children.first.isEnabled ).to.be.false;
  136. expect( dropdown.buttonView.isEnabled ).to.be.true;
  137. selectRowCommand.isEnabled = false;
  138. expect( items.get( 5 ).children.first.isEnabled ).to.be.false;
  139. expect( dropdown.buttonView.isEnabled ).to.be.false;
  140. } );
  141. it( 'should focus view after command execution', () => {
  142. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  143. dropdown.listView.items.first.children.first.fire( 'execute' );
  144. sinon.assert.calledOnce( focusSpy );
  145. } );
  146. it( 'executes command when it\'s executed', () => {
  147. const spy = sinon.stub( editor, 'execute' );
  148. dropdown.listView.items.first.children.first.fire( 'execute' );
  149. expect( spy.calledOnce ).to.be.true;
  150. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'setTableRowHeader' );
  151. } );
  152. it( 'should use a toggle switch for the setTableRowHeader item', () => {
  153. const items = dropdown.listView.items;
  154. expect( items.first.children.first ).to.be.instanceOf( SwitchButtonView );
  155. } );
  156. it( 'should bind set header row command value to dropdown item', () => {
  157. const items = dropdown.listView.items;
  158. const setRowHeaderCommand = editor.commands.get( 'setTableRowHeader' );
  159. setRowHeaderCommand.value = false;
  160. expect( items.first.children.first.isOn ).to.be.false;
  161. setRowHeaderCommand.value = true;
  162. expect( items.first.children.first.isOn ).to.be.true;
  163. } );
  164. } );
  165. describe( 'tableColumn dropdown', () => {
  166. let dropdown;
  167. beforeEach( () => {
  168. dropdown = editor.ui.componentFactory.create( 'tableColumn' );
  169. } );
  170. it( 'have button with proper properties set', () => {
  171. expect( dropdown ).to.be.instanceOf( DropdownView );
  172. const button = dropdown.buttonView;
  173. expect( button.isOn ).to.be.false;
  174. expect( button.tooltip ).to.be.true;
  175. expect( button.label ).to.equal( 'Column' );
  176. expect( button.icon ).to.match( /<svg / );
  177. } );
  178. it( 'should have proper items in panel', () => {
  179. const listView = dropdown.listView;
  180. const labels = listView.items.map( item => item instanceof ListSeparatorView ? '|' : item.children.first.label );
  181. expect( labels ).to.deep.equal(
  182. [ 'Header column', '|', 'Insert column left', 'Insert column right', 'Delete column', 'Select column' ]
  183. );
  184. } );
  185. it( 'should bind items in panel to proper commands (LTR content)', () => {
  186. const items = dropdown.listView.items;
  187. const setColumnHeaderCommand = editor.commands.get( 'setTableColumnHeader' );
  188. const insertColumnLeftCommand = editor.commands.get( 'insertTableColumnLeft' );
  189. const insertColumnRightCommand = editor.commands.get( 'insertTableColumnRight' );
  190. const removeColumnCommand = editor.commands.get( 'removeTableColumn' );
  191. const selectColumnCommand = editor.commands.get( 'selectTableColumn' );
  192. setColumnHeaderCommand.isEnabled = true;
  193. insertColumnLeftCommand.isEnabled = true;
  194. insertColumnRightCommand.isEnabled = true;
  195. removeColumnCommand.isEnabled = true;
  196. selectColumnCommand.isEnabled = true;
  197. expect( items.first.children.first.isEnabled ).to.be.true;
  198. expect( items.get( 2 ).children.first.isEnabled ).to.be.true;
  199. expect( items.get( 3 ).children.first.isEnabled ).to.be.true;
  200. expect( items.get( 4 ).children.first.isEnabled ).to.be.true;
  201. expect( items.get( 5 ).children.first.isEnabled ).to.be.true;
  202. expect( dropdown.buttonView.isEnabled ).to.be.true;
  203. setColumnHeaderCommand.isEnabled = false;
  204. expect( items.first.children.first.isEnabled ).to.be.false;
  205. expect( dropdown.buttonView.isEnabled ).to.be.true;
  206. insertColumnLeftCommand.isEnabled = false;
  207. expect( items.get( 2 ).children.first.isEnabled ).to.be.false;
  208. expect( dropdown.buttonView.isEnabled ).to.be.true;
  209. insertColumnRightCommand.isEnabled = false;
  210. expect( items.get( 3 ).children.first.isEnabled ).to.be.false;
  211. removeColumnCommand.isEnabled = false;
  212. expect( items.get( 4 ).children.first.isEnabled ).to.be.false;
  213. selectColumnCommand.isEnabled = false;
  214. expect( items.get( 5 ).children.first.isEnabled ).to.be.false;
  215. expect( dropdown.buttonView.isEnabled ).to.be.false;
  216. } );
  217. it( 'should bind items in panel to proper commands (RTL content)', () => {
  218. const element = document.createElement( 'div' );
  219. document.body.appendChild( element );
  220. return ClassicTestEditor
  221. .create( element, {
  222. language: {
  223. ui: 'en',
  224. content: 'ar'
  225. },
  226. plugins: [ TableEditing, TableUI ]
  227. } )
  228. .then( editor => {
  229. const dropdown = editor.ui.componentFactory.create( 'tableColumn' );
  230. const items = dropdown.listView.items;
  231. expect( items.get( 2 ).children.first.label ).to.equal( 'Insert column left' );
  232. expect( items.get( 2 ).children.first.commandName ).to.equal( 'insertTableColumnRight' );
  233. expect( items.get( 3 ).children.first.label ).to.equal( 'Insert column right' );
  234. expect( items.get( 3 ).children.first.commandName ).to.equal( 'insertTableColumnLeft' );
  235. element.remove();
  236. return editor.destroy();
  237. } );
  238. } );
  239. it( 'should focus view after command execution', () => {
  240. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  241. dropdown.listView.items.first.children.first.fire( 'execute' );
  242. sinon.assert.calledOnce( focusSpy );
  243. } );
  244. it( 'executes command when it\'s executed', () => {
  245. const spy = sinon.stub( editor, 'execute' );
  246. dropdown.listView.items.first.children.first.fire( 'execute' );
  247. expect( spy.calledOnce ).to.be.true;
  248. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'setTableColumnHeader' );
  249. } );
  250. it( 'should use a toggle switch for the setTableColumnHeader item', () => {
  251. const items = dropdown.listView.items;
  252. expect( items.first.children.first ).to.be.instanceOf( SwitchButtonView );
  253. } );
  254. it( 'should bind set header column command value to dropdown item', () => {
  255. const items = dropdown.listView.items;
  256. const setColumnHeaderCommand = editor.commands.get( 'setTableColumnHeader' );
  257. setColumnHeaderCommand.value = false;
  258. expect( items.first.children.first.isOn ).to.be.false;
  259. setColumnHeaderCommand.value = true;
  260. expect( items.first.children.first.isOn ).to.be.true;
  261. } );
  262. } );
  263. describe( 'mergeTableCell split button', () => {
  264. let dropdown, command;
  265. beforeEach( () => {
  266. dropdown = editor.ui.componentFactory.create( 'mergeTableCells' );
  267. command = editor.commands.get( 'mergeTableCells' );
  268. } );
  269. it( 'have button with proper properties set', () => {
  270. expect( dropdown ).to.be.instanceOf( DropdownView );
  271. const button = dropdown.buttonView;
  272. expect( button.isOn ).to.be.false;
  273. expect( button.tooltip ).to.be.true;
  274. expect( button.label ).to.equal( 'Merge cells' );
  275. expect( button.icon ).to.match( /<svg / );
  276. } );
  277. it( 'should have a split button', () => {
  278. expect( dropdown.buttonView ).to.be.instanceOf( SplitButtonView );
  279. } );
  280. it( 'should have #isEnabled always true regardless of the "mergeTableCells" command state', () => {
  281. command.isEnabled = false;
  282. expect( dropdown.isEnabled ).to.be.true;
  283. command.isEnabled = true;
  284. expect( dropdown.isEnabled ).to.be.true;
  285. } );
  286. it( 'should execute the "mergeTableCells" command when the main part of the split button is clicked', () => {
  287. const spy = sinon.stub( editor, 'execute' );
  288. dropdown.buttonView.fire( 'execute' );
  289. sinon.assert.calledOnce( spy );
  290. sinon.assert.calledWithExactly( spy, 'mergeTableCells' );
  291. } );
  292. it( 'should have the dropdown part of the split button always enabled no matter the "mergeTableCells" command state', () => {
  293. command.isEnabled = true;
  294. expect( dropdown.buttonView.arrowView.isEnabled ).to.be.true;
  295. command.isEnabled = false;
  296. expect( dropdown.buttonView.arrowView.isEnabled ).to.be.true;
  297. } );
  298. it( 'should have proper items in panel', () => {
  299. const listView = dropdown.listView;
  300. const labels = listView.items.map( item => item instanceof ListSeparatorView ? '|' : item.children.first.label );
  301. expect( labels ).to.deep.equal( [
  302. 'Merge cell up',
  303. 'Merge cell right',
  304. 'Merge cell down',
  305. 'Merge cell left',
  306. '|',
  307. 'Split cell vertically',
  308. 'Split cell horizontally'
  309. ] );
  310. } );
  311. it( 'should bind items in panel to proper commands (LTR content)', () => {
  312. const items = dropdown.listView.items;
  313. const mergeCellUpCommand = editor.commands.get( 'mergeTableCellUp' );
  314. const mergeCellRightCommand = editor.commands.get( 'mergeTableCellRight' );
  315. const mergeCellDownCommand = editor.commands.get( 'mergeTableCellDown' );
  316. const mergeCellLeftCommand = editor.commands.get( 'mergeTableCellLeft' );
  317. const splitCellVerticallyCommand = editor.commands.get( 'splitTableCellVertically' );
  318. const splitCellHorizontallyCommand = editor.commands.get( 'splitTableCellHorizontally' );
  319. mergeCellUpCommand.isEnabled = true;
  320. mergeCellRightCommand.isEnabled = true;
  321. mergeCellDownCommand.isEnabled = true;
  322. mergeCellLeftCommand.isEnabled = true;
  323. splitCellVerticallyCommand.isEnabled = true;
  324. splitCellHorizontallyCommand.isEnabled = true;
  325. const mergeCellUpButton = items.first;
  326. const mergeCellRightButton = items.get( 1 );
  327. const mergeCellDownButton = items.get( 2 );
  328. const mergeCellLeftButton = items.get( 3 );
  329. // separator
  330. const splitVerticallyButton = items.get( 5 );
  331. const splitHorizontallyButton = items.get( 6 );
  332. expect( mergeCellUpButton.children.first.isEnabled ).to.be.true;
  333. expect( mergeCellRightButton.children.first.isEnabled ).to.be.true;
  334. expect( mergeCellDownButton.children.first.isEnabled ).to.be.true;
  335. expect( mergeCellLeftButton.children.first.isEnabled ).to.be.true;
  336. expect( splitVerticallyButton.children.first.isEnabled ).to.be.true;
  337. expect( splitHorizontallyButton.children.first.isEnabled ).to.be.true;
  338. mergeCellUpCommand.isEnabled = false;
  339. expect( mergeCellUpButton.children.first.isEnabled ).to.be.false;
  340. mergeCellRightCommand.isEnabled = false;
  341. expect( mergeCellRightButton.children.first.isEnabled ).to.be.false;
  342. mergeCellDownCommand.isEnabled = false;
  343. expect( mergeCellDownButton.children.first.isEnabled ).to.be.false;
  344. mergeCellLeftCommand.isEnabled = false;
  345. expect( mergeCellLeftButton.children.first.isEnabled ).to.be.false;
  346. splitCellVerticallyCommand.isEnabled = false;
  347. expect( splitVerticallyButton.children.first.isEnabled ).to.be.false;
  348. splitCellHorizontallyCommand.isEnabled = false;
  349. expect( splitHorizontallyButton.children.first.isEnabled ).to.be.false;
  350. } );
  351. it( 'should bind items in panel to proper commands (RTL content)', () => {
  352. const element = document.createElement( 'div' );
  353. document.body.appendChild( element );
  354. return ClassicTestEditor
  355. .create( element, {
  356. language: {
  357. ui: 'en',
  358. content: 'ar'
  359. },
  360. plugins: [ TableEditing, TableUI ]
  361. } )
  362. .then( editor => {
  363. const dropdown = editor.ui.componentFactory.create( 'mergeTableCells' );
  364. const items = dropdown.listView.items;
  365. expect( items.get( 1 ).children.first.label ).to.equal( 'Merge cell right' );
  366. expect( items.get( 1 ).children.first.commandName ).to.equal( 'mergeTableCellLeft' );
  367. expect( items.get( 3 ).children.first.label ).to.equal( 'Merge cell left' );
  368. expect( items.get( 3 ).children.first.commandName ).to.equal( 'mergeTableCellRight' );
  369. element.remove();
  370. return editor.destroy();
  371. } );
  372. } );
  373. it( 'should focus view after command execution', () => {
  374. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  375. dropdown.listView.items.first.children.first.fire( 'execute' );
  376. sinon.assert.calledOnce( focusSpy );
  377. } );
  378. it( 'executes command when it\'s executed', () => {
  379. const spy = sinon.stub( editor, 'execute' );
  380. dropdown.listView.items.first.children.first.fire( 'execute' );
  381. expect( spy.calledOnce ).to.be.true;
  382. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'mergeTableCellUp' );
  383. } );
  384. } );
  385. } );