tableui.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 SwitchButtonView from '@ckeditor/ckeditor5-ui/src/button/switchbuttonview';
  12. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  13. import ListSeparatorView from '@ckeditor/ckeditor5-ui/src/list/listseparatorview';
  14. describe( 'TableUI', () => {
  15. let editor, element;
  16. testUtils.createSinonSandbox();
  17. before( () => {
  18. addTranslations( 'en', {} );
  19. addTranslations( 'pl', {} );
  20. } );
  21. after( () => {
  22. clearTranslations();
  23. } );
  24. beforeEach( () => {
  25. element = document.createElement( 'div' );
  26. document.body.appendChild( element );
  27. return ClassicTestEditor
  28. .create( element, {
  29. plugins: [ TableEditing, TableUI ]
  30. } )
  31. .then( newEditor => {
  32. editor = newEditor;
  33. } );
  34. } );
  35. afterEach( () => {
  36. element.remove();
  37. return editor.destroy();
  38. } );
  39. describe( 'insertTable dropdown', () => {
  40. let insertTable;
  41. beforeEach( () => {
  42. insertTable = editor.ui.componentFactory.create( 'insertTable' );
  43. } );
  44. it( 'should register insertTable button', () => {
  45. expect( insertTable ).to.be.instanceOf( DropdownView );
  46. expect( insertTable.buttonView.label ).to.equal( 'Insert table' );
  47. expect( insertTable.buttonView.icon ).to.match( /<svg / );
  48. } );
  49. it( 'should bind to insertTable command', () => {
  50. const command = editor.commands.get( 'insertTable' );
  51. command.isEnabled = true;
  52. expect( insertTable.buttonView.isOn ).to.be.false;
  53. expect( insertTable.buttonView.isEnabled ).to.be.true;
  54. command.isEnabled = false;
  55. expect( insertTable.buttonView.isEnabled ).to.be.false;
  56. } );
  57. it( 'should execute insertTable command on button execute event', () => {
  58. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  59. const tableSizeView = insertTable.panelView.children.first;
  60. tableSizeView.rows = 2;
  61. tableSizeView.columns = 7;
  62. insertTable.fire( 'execute' );
  63. sinon.assert.calledOnce( executeSpy );
  64. sinon.assert.calledWithExactly( executeSpy, 'insertTable', { rows: 2, columns: 7 } );
  65. } );
  66. it( 'should reset rows & columns on dropdown open', () => {
  67. const tableSizeView = insertTable.panelView.children.first;
  68. expect( tableSizeView.rows ).to.equal( 0 );
  69. expect( tableSizeView.columns ).to.equal( 0 );
  70. tableSizeView.rows = 2;
  71. tableSizeView.columns = 2;
  72. insertTable.buttonView.fire( 'open' );
  73. expect( tableSizeView.rows ).to.equal( 0 );
  74. expect( tableSizeView.columns ).to.equal( 0 );
  75. } );
  76. } );
  77. describe( 'tableRow dropdown', () => {
  78. let dropdown;
  79. beforeEach( () => {
  80. dropdown = editor.ui.componentFactory.create( 'tableRow' );
  81. } );
  82. it( 'have button with proper properties set', () => {
  83. expect( dropdown ).to.be.instanceOf( DropdownView );
  84. const button = dropdown.buttonView;
  85. expect( button.isOn ).to.be.false;
  86. expect( button.tooltip ).to.be.true;
  87. expect( button.label ).to.equal( 'Row' );
  88. expect( button.icon ).to.match( /<svg / );
  89. } );
  90. it( 'should have proper items in panel', () => {
  91. const listView = dropdown.listView;
  92. const labels = listView.items.map( item => item instanceof ListSeparatorView ? '|' : item.children.first.label );
  93. expect( labels ).to.deep.equal( [ 'Header row', '|', 'Insert row below', 'Insert row above', 'Delete row' ] );
  94. } );
  95. it( 'should bind items in panel to proper commands', () => {
  96. const items = dropdown.listView.items;
  97. const setRowHeaderCommand = editor.commands.get( 'setTableRowHeader' );
  98. const insertRowBelowCommand = editor.commands.get( 'insertTableRowBelow' );
  99. const insertRowAboveCommand = editor.commands.get( 'insertTableRowAbove' );
  100. const removeRowCommand = editor.commands.get( 'removeTableRow' );
  101. setRowHeaderCommand.isEnabled = true;
  102. insertRowBelowCommand.isEnabled = true;
  103. insertRowAboveCommand.isEnabled = true;
  104. removeRowCommand.isEnabled = true;
  105. expect( items.first.children.first.isEnabled ).to.be.true;
  106. expect( items.get( 2 ).children.first.isEnabled ).to.be.true;
  107. expect( items.get( 3 ).children.first.isEnabled ).to.be.true;
  108. expect( items.get( 4 ).children.first.isEnabled ).to.be.true;
  109. expect( dropdown.buttonView.isEnabled ).to.be.true;
  110. setRowHeaderCommand.isEnabled = false;
  111. expect( items.first.children.first.isEnabled ).to.be.false;
  112. expect( dropdown.buttonView.isEnabled ).to.be.true;
  113. insertRowBelowCommand.isEnabled = false;
  114. expect( items.get( 2 ).children.first.isEnabled ).to.be.false;
  115. expect( dropdown.buttonView.isEnabled ).to.be.true;
  116. insertRowAboveCommand.isEnabled = false;
  117. expect( items.get( 3 ).children.first.isEnabled ).to.be.false;
  118. expect( dropdown.buttonView.isEnabled ).to.be.true;
  119. removeRowCommand.isEnabled = false;
  120. expect( items.get( 4 ).children.first.isEnabled ).to.be.false;
  121. expect( dropdown.buttonView.isEnabled ).to.be.false;
  122. } );
  123. it( 'should focus view after command execution', () => {
  124. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  125. dropdown.listView.items.first.children.first.fire( 'execute' );
  126. sinon.assert.calledOnce( focusSpy );
  127. } );
  128. it( 'executes command when it\'s executed', () => {
  129. const spy = sinon.stub( editor, 'execute' );
  130. dropdown.listView.items.first.children.first.fire( 'execute' );
  131. expect( spy.calledOnce ).to.be.true;
  132. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'setTableRowHeader' );
  133. } );
  134. it( 'should use a toggle switch for the setTableRowHeader item', () => {
  135. const items = dropdown.listView.items;
  136. expect( items.first.children.first ).to.be.instanceOf( SwitchButtonView );
  137. } );
  138. it( 'should bind set header row command value to dropdown item', () => {
  139. const items = dropdown.listView.items;
  140. const setRowHeaderCommand = editor.commands.get( 'setTableRowHeader' );
  141. setRowHeaderCommand.value = false;
  142. expect( items.first.children.first.isOn ).to.be.false;
  143. setRowHeaderCommand.value = true;
  144. expect( items.first.children.first.isOn ).to.be.true;
  145. } );
  146. } );
  147. describe( 'tableColumn dropdown', () => {
  148. let dropdown;
  149. beforeEach( () => {
  150. dropdown = editor.ui.componentFactory.create( 'tableColumn' );
  151. } );
  152. it( 'have button with proper properties set', () => {
  153. expect( dropdown ).to.be.instanceOf( DropdownView );
  154. const button = dropdown.buttonView;
  155. expect( button.isOn ).to.be.false;
  156. expect( button.tooltip ).to.be.true;
  157. expect( button.label ).to.equal( 'Column' );
  158. expect( button.icon ).to.match( /<svg / );
  159. } );
  160. it( 'should have proper items in panel', () => {
  161. const listView = dropdown.listView;
  162. const labels = listView.items.map( item => item instanceof ListSeparatorView ? '|' : item.children.first.label );
  163. expect( labels ).to.deep.equal( [ 'Header column', '|', 'Insert column left', 'Insert column right', 'Delete column' ] );
  164. } );
  165. it( 'should bind items in panel to proper commands', () => {
  166. const items = dropdown.listView.items;
  167. const setColumnHeaderCommand = editor.commands.get( 'setTableColumnHeader' );
  168. const insertColumnLeftCommand = editor.commands.get( 'insertTableColumnLeft' );
  169. const insertColumnRightCommand = editor.commands.get( 'insertTableColumnRight' );
  170. const removeColumnCommand = editor.commands.get( 'removeTableColumn' );
  171. setColumnHeaderCommand.isEnabled = true;
  172. insertColumnLeftCommand.isEnabled = true;
  173. insertColumnRightCommand.isEnabled = true;
  174. removeColumnCommand.isEnabled = true;
  175. expect( items.first.children.first.isEnabled ).to.be.true;
  176. expect( items.get( 2 ).children.first.isEnabled ).to.be.true;
  177. expect( items.get( 3 ).children.first.isEnabled ).to.be.true;
  178. expect( items.get( 4 ).children.first.isEnabled ).to.be.true;
  179. expect( dropdown.buttonView.isEnabled ).to.be.true;
  180. setColumnHeaderCommand.isEnabled = false;
  181. expect( items.first.children.first.isEnabled ).to.be.false;
  182. expect( dropdown.buttonView.isEnabled ).to.be.true;
  183. insertColumnLeftCommand.isEnabled = false;
  184. expect( items.get( 2 ).children.first.isEnabled ).to.be.false;
  185. expect( dropdown.buttonView.isEnabled ).to.be.true;
  186. insertColumnRightCommand.isEnabled = false;
  187. expect( items.get( 3 ).children.first.isEnabled ).to.be.false;
  188. removeColumnCommand.isEnabled = false;
  189. expect( items.get( 4 ).children.first.isEnabled ).to.be.false;
  190. expect( dropdown.buttonView.isEnabled ).to.be.false;
  191. } );
  192. it( 'should focus view after command execution', () => {
  193. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  194. dropdown.listView.items.first.children.first.fire( 'execute' );
  195. sinon.assert.calledOnce( focusSpy );
  196. } );
  197. it( 'executes command when it\'s executed', () => {
  198. const spy = sinon.stub( editor, 'execute' );
  199. dropdown.listView.items.first.children.first.fire( 'execute' );
  200. expect( spy.calledOnce ).to.be.true;
  201. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'setTableColumnHeader' );
  202. } );
  203. it( 'should use a toggle switch for the setTableColumnHeader item', () => {
  204. const items = dropdown.listView.items;
  205. expect( items.first.children.first ).to.be.instanceOf( SwitchButtonView );
  206. } );
  207. it( 'should bind set header column command value to dropdown item', () => {
  208. const items = dropdown.listView.items;
  209. const setColumnHeaderCommand = editor.commands.get( 'setTableColumnHeader' );
  210. setColumnHeaderCommand.value = false;
  211. expect( items.first.children.first.isOn ).to.be.false;
  212. setColumnHeaderCommand.value = true;
  213. expect( items.first.children.first.isOn ).to.be.true;
  214. } );
  215. } );
  216. describe( 'mergeTableCell dropdown', () => {
  217. let dropdown;
  218. beforeEach( () => {
  219. dropdown = editor.ui.componentFactory.create( 'mergeTableCells' );
  220. } );
  221. it( 'have button with proper properties set', () => {
  222. expect( dropdown ).to.be.instanceOf( DropdownView );
  223. const button = dropdown.buttonView;
  224. expect( button.isOn ).to.be.false;
  225. expect( button.tooltip ).to.be.true;
  226. expect( button.label ).to.equal( 'Merge cells' );
  227. expect( button.icon ).to.match( /<svg / );
  228. } );
  229. it( 'should have proper items in panel', () => {
  230. const listView = dropdown.listView;
  231. const labels = listView.items.map( item => item instanceof ListSeparatorView ? '|' : item.children.first.label );
  232. expect( labels ).to.deep.equal( [
  233. 'Merge cell up',
  234. 'Merge cell right',
  235. 'Merge cell down',
  236. 'Merge cell left',
  237. '|',
  238. 'Split cell vertically',
  239. 'Split cell horizontally'
  240. ] );
  241. } );
  242. it( 'should bind items in panel to proper commands', () => {
  243. const items = dropdown.listView.items;
  244. const mergeCellUpCommand = editor.commands.get( 'mergeTableCellUp' );
  245. const mergeCellRightCommand = editor.commands.get( 'mergeTableCellRight' );
  246. const mergeCellDownCommand = editor.commands.get( 'mergeTableCellDown' );
  247. const mergeCellLeftCommand = editor.commands.get( 'mergeTableCellLeft' );
  248. const splitCellVerticallyCommand = editor.commands.get( 'splitTableCellVertically' );
  249. const splitCellHorizontallyCommand = editor.commands.get( 'splitTableCellHorizontally' );
  250. mergeCellUpCommand.isEnabled = true;
  251. mergeCellRightCommand.isEnabled = true;
  252. mergeCellDownCommand.isEnabled = true;
  253. mergeCellLeftCommand.isEnabled = true;
  254. splitCellVerticallyCommand.isEnabled = true;
  255. splitCellHorizontallyCommand.isEnabled = true;
  256. expect( items.first.children.first.isEnabled ).to.be.true;
  257. expect( items.get( 1 ).children.first.isEnabled ).to.be.true;
  258. expect( items.get( 2 ).children.first.isEnabled ).to.be.true;
  259. expect( items.get( 3 ).children.first.isEnabled ).to.be.true;
  260. expect( items.get( 5 ).children.first.isEnabled ).to.be.true;
  261. expect( items.get( 6 ).children.first.isEnabled ).to.be.true;
  262. expect( dropdown.buttonView.isEnabled ).to.be.true;
  263. mergeCellUpCommand.isEnabled = false;
  264. expect( items.first.children.first.isEnabled ).to.be.false;
  265. expect( dropdown.buttonView.isEnabled ).to.be.true;
  266. mergeCellRightCommand.isEnabled = false;
  267. expect( items.get( 1 ).children.first.isEnabled ).to.be.false;
  268. expect( dropdown.buttonView.isEnabled ).to.be.true;
  269. mergeCellDownCommand.isEnabled = false;
  270. expect( items.get( 2 ).children.first.isEnabled ).to.be.false;
  271. mergeCellLeftCommand.isEnabled = false;
  272. expect( items.get( 3 ).children.first.isEnabled ).to.be.false;
  273. splitCellVerticallyCommand.isEnabled = false;
  274. expect( items.get( 5 ).children.first.isEnabled ).to.be.false;
  275. splitCellHorizontallyCommand.isEnabled = false;
  276. expect( items.get( 6 ).children.first.isEnabled ).to.be.false;
  277. expect( dropdown.buttonView.isEnabled ).to.be.false;
  278. } );
  279. it( 'should focus view after command execution', () => {
  280. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  281. dropdown.listView.items.first.children.first.fire( 'execute' );
  282. sinon.assert.calledOnce( focusSpy );
  283. } );
  284. it( 'executes command when it\'s executed', () => {
  285. const spy = sinon.stub( editor, 'execute' );
  286. dropdown.listView.items.first.children.first.fire( 'execute' );
  287. expect( spy.calledOnce ).to.be.true;
  288. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'mergeTableCellUp' );
  289. } );
  290. } );
  291. } );