8
0

tableui.js 15 KB

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