tableui.js 17 KB

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