tableui.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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( [ 'Header row', '|', 'Insert row below', 'Insert row above', 'Delete row' ] );
  103. } );
  104. it( 'should bind items in panel to proper commands', () => {
  105. const items = dropdown.listView.items;
  106. const setRowHeaderCommand = editor.commands.get( 'setTableRowHeader' );
  107. const insertRowBelowCommand = editor.commands.get( 'insertTableRowBelow' );
  108. const insertRowAboveCommand = editor.commands.get( 'insertTableRowAbove' );
  109. const removeRowCommand = editor.commands.get( 'removeTableRow' );
  110. setRowHeaderCommand.isEnabled = true;
  111. insertRowBelowCommand.isEnabled = true;
  112. insertRowAboveCommand.isEnabled = true;
  113. removeRowCommand.isEnabled = true;
  114. expect( items.first.children.first.isEnabled ).to.be.true;
  115. expect( items.get( 2 ).children.first.isEnabled ).to.be.true;
  116. expect( items.get( 3 ).children.first.isEnabled ).to.be.true;
  117. expect( items.get( 4 ).children.first.isEnabled ).to.be.true;
  118. expect( dropdown.buttonView.isEnabled ).to.be.true;
  119. setRowHeaderCommand.isEnabled = false;
  120. expect( items.first.children.first.isEnabled ).to.be.false;
  121. expect( dropdown.buttonView.isEnabled ).to.be.true;
  122. insertRowBelowCommand.isEnabled = false;
  123. expect( items.get( 2 ).children.first.isEnabled ).to.be.false;
  124. expect( dropdown.buttonView.isEnabled ).to.be.true;
  125. insertRowAboveCommand.isEnabled = false;
  126. expect( items.get( 3 ).children.first.isEnabled ).to.be.false;
  127. expect( dropdown.buttonView.isEnabled ).to.be.true;
  128. removeRowCommand.isEnabled = false;
  129. expect( items.get( 4 ).children.first.isEnabled ).to.be.false;
  130. expect( dropdown.buttonView.isEnabled ).to.be.false;
  131. } );
  132. it( 'should focus view after command execution', () => {
  133. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  134. dropdown.listView.items.first.children.first.fire( 'execute' );
  135. sinon.assert.calledOnce( focusSpy );
  136. } );
  137. it( 'executes command when it\'s executed', () => {
  138. const spy = sinon.stub( editor, 'execute' );
  139. dropdown.listView.items.first.children.first.fire( 'execute' );
  140. expect( spy.calledOnce ).to.be.true;
  141. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'setTableRowHeader' );
  142. } );
  143. it( 'should use a toggle switch for the setTableRowHeader item', () => {
  144. const items = dropdown.listView.items;
  145. expect( items.first.children.first ).to.be.instanceOf( SwitchButtonView );
  146. } );
  147. it( 'should bind set header row command value to dropdown item', () => {
  148. const items = dropdown.listView.items;
  149. const setRowHeaderCommand = editor.commands.get( 'setTableRowHeader' );
  150. setRowHeaderCommand.value = false;
  151. expect( items.first.children.first.isOn ).to.be.false;
  152. setRowHeaderCommand.value = true;
  153. expect( items.first.children.first.isOn ).to.be.true;
  154. } );
  155. } );
  156. describe( 'tableColumn dropdown', () => {
  157. let dropdown;
  158. beforeEach( () => {
  159. dropdown = editor.ui.componentFactory.create( 'tableColumn' );
  160. } );
  161. it( 'have button with proper properties set', () => {
  162. expect( dropdown ).to.be.instanceOf( DropdownView );
  163. const button = dropdown.buttonView;
  164. expect( button.isOn ).to.be.false;
  165. expect( button.tooltip ).to.be.true;
  166. expect( button.label ).to.equal( 'Column' );
  167. expect( button.icon ).to.match( /<svg / );
  168. } );
  169. it( 'should have proper items in panel', () => {
  170. const listView = dropdown.listView;
  171. const labels = listView.items.map( item => item instanceof ListSeparatorView ? '|' : item.children.first.label );
  172. expect( labels ).to.deep.equal( [ 'Header column', '|', 'Insert column left', 'Insert column right', 'Delete column' ] );
  173. } );
  174. it( 'should bind items in panel to proper commands (LTR content)', () => {
  175. const items = dropdown.listView.items;
  176. const setColumnHeaderCommand = editor.commands.get( 'setTableColumnHeader' );
  177. const insertColumnLeftCommand = editor.commands.get( 'insertTableColumnLeft' );
  178. const insertColumnRightCommand = editor.commands.get( 'insertTableColumnRight' );
  179. const removeColumnCommand = editor.commands.get( 'removeTableColumn' );
  180. setColumnHeaderCommand.isEnabled = true;
  181. insertColumnLeftCommand.isEnabled = true;
  182. insertColumnRightCommand.isEnabled = true;
  183. removeColumnCommand.isEnabled = true;
  184. expect( items.first.children.first.isEnabled ).to.be.true;
  185. expect( items.get( 2 ).children.first.isEnabled ).to.be.true;
  186. expect( items.get( 3 ).children.first.isEnabled ).to.be.true;
  187. expect( items.get( 4 ).children.first.isEnabled ).to.be.true;
  188. expect( dropdown.buttonView.isEnabled ).to.be.true;
  189. setColumnHeaderCommand.isEnabled = false;
  190. expect( items.first.children.first.isEnabled ).to.be.false;
  191. expect( dropdown.buttonView.isEnabled ).to.be.true;
  192. insertColumnLeftCommand.isEnabled = false;
  193. expect( items.get( 2 ).children.first.isEnabled ).to.be.false;
  194. expect( dropdown.buttonView.isEnabled ).to.be.true;
  195. insertColumnRightCommand.isEnabled = false;
  196. expect( items.get( 3 ).children.first.isEnabled ).to.be.false;
  197. removeColumnCommand.isEnabled = false;
  198. expect( items.get( 4 ).children.first.isEnabled ).to.be.false;
  199. expect( dropdown.buttonView.isEnabled ).to.be.false;
  200. } );
  201. it( 'should bind items in panel to proper commands (RTL content)', () => {
  202. const element = document.createElement( 'div' );
  203. document.body.appendChild( element );
  204. return ClassicTestEditor
  205. .create( element, {
  206. language: {
  207. ui: 'en',
  208. content: 'ar'
  209. },
  210. plugins: [ TableEditing, TableUI ]
  211. } )
  212. .then( editor => {
  213. const dropdown = editor.ui.componentFactory.create( 'tableColumn' );
  214. const items = dropdown.listView.items;
  215. expect( items.get( 2 ).children.first.label ).to.equal( 'Insert column left' );
  216. expect( items.get( 2 ).children.first.commandName ).to.equal( 'insertTableColumnRight' );
  217. expect( items.get( 3 ).children.first.label ).to.equal( 'Insert column right' );
  218. expect( items.get( 3 ).children.first.commandName ).to.equal( 'insertTableColumnLeft' );
  219. element.remove();
  220. return editor.destroy();
  221. } );
  222. } );
  223. it( 'should focus view after command execution', () => {
  224. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  225. dropdown.listView.items.first.children.first.fire( 'execute' );
  226. sinon.assert.calledOnce( focusSpy );
  227. } );
  228. it( 'executes command when it\'s executed', () => {
  229. const spy = sinon.stub( editor, 'execute' );
  230. dropdown.listView.items.first.children.first.fire( 'execute' );
  231. expect( spy.calledOnce ).to.be.true;
  232. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'setTableColumnHeader' );
  233. } );
  234. it( 'should use a toggle switch for the setTableColumnHeader item', () => {
  235. const items = dropdown.listView.items;
  236. expect( items.first.children.first ).to.be.instanceOf( SwitchButtonView );
  237. } );
  238. it( 'should bind set header column command value to dropdown item', () => {
  239. const items = dropdown.listView.items;
  240. const setColumnHeaderCommand = editor.commands.get( 'setTableColumnHeader' );
  241. setColumnHeaderCommand.value = false;
  242. expect( items.first.children.first.isOn ).to.be.false;
  243. setColumnHeaderCommand.value = true;
  244. expect( items.first.children.first.isOn ).to.be.true;
  245. } );
  246. } );
  247. describe( 'mergeTableCell dropdown', () => {
  248. let dropdown;
  249. beforeEach( () => {
  250. dropdown = editor.ui.componentFactory.create( 'mergeTableCells' );
  251. } );
  252. it( 'have button with proper properties set', () => {
  253. expect( dropdown ).to.be.instanceOf( DropdownView );
  254. const button = dropdown.buttonView;
  255. expect( button.isOn ).to.be.false;
  256. expect( button.tooltip ).to.be.true;
  257. expect( button.label ).to.equal( 'Merge cells' );
  258. expect( button.icon ).to.match( /<svg / );
  259. } );
  260. it( 'should have proper items in panel', () => {
  261. const listView = dropdown.listView;
  262. const labels = listView.items.map( item => item instanceof ListSeparatorView ? '|' : item.children.first.label );
  263. expect( labels ).to.deep.equal( [
  264. 'Merge cell up',
  265. 'Merge cell right',
  266. 'Merge cell down',
  267. 'Merge cell left',
  268. '|',
  269. 'Split cell vertically',
  270. 'Split cell horizontally'
  271. ] );
  272. } );
  273. it( 'should bind items in panel to proper commands (LTR content)', () => {
  274. const items = dropdown.listView.items;
  275. const mergeCellUpCommand = editor.commands.get( 'mergeTableCellUp' );
  276. const mergeCellRightCommand = editor.commands.get( 'mergeTableCellRight' );
  277. const mergeCellDownCommand = editor.commands.get( 'mergeTableCellDown' );
  278. const mergeCellLeftCommand = editor.commands.get( 'mergeTableCellLeft' );
  279. const splitCellVerticallyCommand = editor.commands.get( 'splitTableCellVertically' );
  280. const splitCellHorizontallyCommand = editor.commands.get( 'splitTableCellHorizontally' );
  281. mergeCellUpCommand.isEnabled = true;
  282. mergeCellRightCommand.isEnabled = true;
  283. mergeCellDownCommand.isEnabled = true;
  284. mergeCellLeftCommand.isEnabled = true;
  285. splitCellVerticallyCommand.isEnabled = true;
  286. splitCellHorizontallyCommand.isEnabled = true;
  287. expect( items.first.children.first.isEnabled ).to.be.true;
  288. expect( items.get( 1 ).children.first.isEnabled ).to.be.true;
  289. expect( items.get( 2 ).children.first.isEnabled ).to.be.true;
  290. expect( items.get( 3 ).children.first.isEnabled ).to.be.true;
  291. expect( items.get( 5 ).children.first.isEnabled ).to.be.true;
  292. expect( items.get( 6 ).children.first.isEnabled ).to.be.true;
  293. expect( dropdown.buttonView.isEnabled ).to.be.true;
  294. mergeCellUpCommand.isEnabled = false;
  295. expect( items.first.children.first.isEnabled ).to.be.false;
  296. expect( dropdown.buttonView.isEnabled ).to.be.true;
  297. mergeCellRightCommand.isEnabled = false;
  298. expect( items.get( 1 ).children.first.isEnabled ).to.be.false;
  299. expect( dropdown.buttonView.isEnabled ).to.be.true;
  300. mergeCellDownCommand.isEnabled = false;
  301. expect( items.get( 2 ).children.first.isEnabled ).to.be.false;
  302. mergeCellLeftCommand.isEnabled = false;
  303. expect( items.get( 3 ).children.first.isEnabled ).to.be.false;
  304. splitCellVerticallyCommand.isEnabled = false;
  305. expect( items.get( 5 ).children.first.isEnabled ).to.be.false;
  306. splitCellHorizontallyCommand.isEnabled = false;
  307. expect( items.get( 6 ).children.first.isEnabled ).to.be.false;
  308. expect( dropdown.buttonView.isEnabled ).to.be.false;
  309. } );
  310. it( 'should bind items in panel to proper commands (RTL content)', () => {
  311. const element = document.createElement( 'div' );
  312. document.body.appendChild( element );
  313. return ClassicTestEditor
  314. .create( element, {
  315. language: {
  316. ui: 'en',
  317. content: 'ar'
  318. },
  319. plugins: [ TableEditing, TableUI ]
  320. } )
  321. .then( editor => {
  322. const dropdown = editor.ui.componentFactory.create( 'mergeTableCells' );
  323. const items = dropdown.listView.items;
  324. expect( items.get( 1 ).children.first.label ).to.equal( 'Merge cell right' );
  325. expect( items.get( 1 ).children.first.commandName ).to.equal( 'mergeTableCellLeft' );
  326. expect( items.get( 3 ).children.first.label ).to.equal( 'Merge cell left' );
  327. expect( items.get( 3 ).children.first.commandName ).to.equal( 'mergeTableCellRight' );
  328. element.remove();
  329. return editor.destroy();
  330. } );
  331. } );
  332. it( 'should focus view after command execution', () => {
  333. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  334. dropdown.listView.items.first.children.first.fire( 'execute' );
  335. sinon.assert.calledOnce( focusSpy );
  336. } );
  337. it( 'executes command when it\'s executed', () => {
  338. const spy = sinon.stub( editor, 'execute' );
  339. dropdown.listView.items.first.children.first.fire( 'execute' );
  340. expect( spy.calledOnce ).to.be.true;
  341. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'mergeTableCellUp' );
  342. } );
  343. } );
  344. } );