tabletoolbar.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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-editor-classic/src/classiceditor';
  7. import TableToolbar from '../src/tabletoolbar';
  8. import Table from '../src/table';
  9. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  10. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  11. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. import View from '@ckeditor/ckeditor5-ui/src/view';
  14. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import WidgetToolbarRepository from '@ckeditor/ckeditor5-widget/src/widgettoolbarrepository';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. import ImageToolbar from '@ckeditor/ckeditor5-image/src/imagetoolbar';
  18. import Image from '@ckeditor/ckeditor5-image/src/image';
  19. import ImageStyle from '@ckeditor/ckeditor5-image/src/imagestyle';
  20. describe( 'TableToolbar', () => {
  21. testUtils.createSinonSandbox();
  22. describe( 'contentToolbar', () => {
  23. let editor, model, doc, widgetToolbarRepository, toolbar, balloon, editorElement;
  24. beforeEach( () => {
  25. editorElement = global.document.createElement( 'div' );
  26. global.document.body.appendChild( editorElement );
  27. return ClassicTestEditor
  28. .create( editorElement, {
  29. plugins: [ Paragraph, Image, ImageStyle, ImageToolbar, Table, TableToolbar, FakeButton ],
  30. image: {
  31. toolbar: [ 'imageStyle:full', 'imageStyle:side' ]
  32. },
  33. table: {
  34. contentToolbar: [ 'fake_button' ]
  35. }
  36. } )
  37. .then( newEditor => {
  38. editor = newEditor;
  39. model = newEditor.model;
  40. doc = model.document;
  41. widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  42. toolbar = widgetToolbarRepository._toolbarDefinitions.get( 'tableContent' ).view;
  43. balloon = editor.plugins.get( 'ContextualBalloon' );
  44. } );
  45. } );
  46. afterEach( () => {
  47. editorElement.remove();
  48. return editor.destroy();
  49. } );
  50. it( 'should be loaded', () => {
  51. expect( editor.plugins.get( TableToolbar ) ).to.be.instanceOf( TableToolbar );
  52. } );
  53. it( 'should not initialize if there is no configuration', () => {
  54. const editorElement = global.document.createElement( 'div' );
  55. global.document.body.appendChild( editorElement );
  56. return ClassicTestEditor.create( editorElement, {
  57. plugins: [ TableToolbar ]
  58. } )
  59. .then( editor => {
  60. const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  61. expect( widgetToolbarRepository._toolbarDefinitions.get( 'tableContent' ) ).to.be.undefined;
  62. editorElement.remove();
  63. return editor.destroy();
  64. } );
  65. } );
  66. describe( 'toolbar', () => {
  67. it( 'should use the config.table.contenToolbar to create items', () => {
  68. expect( toolbar.items ).to.have.length( 1 );
  69. expect( toolbar.items.get( 0 ).label ).to.equal( 'fake button' );
  70. } );
  71. it( 'should set proper CSS classes', () => {
  72. const spy = sinon.spy( balloon, 'add' );
  73. editor.ui.focusTracker.isFocused = true;
  74. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  75. sinon.assert.calledWithMatch( spy, sinon.match( ( { balloonClassName, view } ) => {
  76. return view === toolbar && balloonClassName === 'ck-toolbar-container';
  77. } ) );
  78. } );
  79. it( 'should set aria-label attribute', () => {
  80. toolbar.render();
  81. expect( toolbar.element.getAttribute( 'aria-label' ) ).to.equal( 'Table toolbar' );
  82. toolbar.destroy();
  83. } );
  84. } );
  85. describe( 'integration with the editor focus', () => {
  86. it( 'should show the toolbar when the editor gains focus and the table is selected', () => {
  87. editor.ui.focusTracker.isFocused = true;
  88. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  89. editor.ui.focusTracker.isFocused = false;
  90. expect( balloon.visibleView ).to.be.null;
  91. editor.ui.focusTracker.isFocused = true;
  92. expect( balloon.visibleView ).to.equal( toolbar );
  93. } );
  94. it( 'should hide the toolbar when the editor loses focus and the table is selected', () => {
  95. editor.ui.focusTracker.isFocused = false;
  96. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  97. editor.ui.focusTracker.isFocused = true;
  98. expect( balloon.visibleView ).to.equal( toolbar );
  99. editor.ui.focusTracker.isFocused = false;
  100. expect( balloon.visibleView ).to.be.null;
  101. } );
  102. } );
  103. describe( 'integration with the editor selection (ui#update event)', () => {
  104. beforeEach( () => {
  105. editor.ui.focusTracker.isFocused = true;
  106. } );
  107. it( 'should not show the toolbar on ui#update when the table is selected', () => {
  108. setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  109. expect( balloon.visibleView ).to.be.null;
  110. } );
  111. it( 'should show the toolbar on ui#update when the table content is selected', () => {
  112. setData(
  113. model,
  114. '<paragraph>[foo]</paragraph><table><tableRow><tableCell><paragraph>bar</paragraph></tableCell></tableRow></table>'
  115. );
  116. expect( balloon.visibleView ).to.be.null;
  117. editor.ui.fire( 'update' );
  118. expect( balloon.visibleView ).to.be.null;
  119. model.change( writer => {
  120. // Select the <tableCell>[bar]</tableCell>
  121. writer.setSelection(
  122. writer.createRangeOn( doc.getRoot().getNodeByPath( [ 1, 0, 0, 0 ] ) )
  123. );
  124. } );
  125. expect( balloon.visibleView ).to.equal( toolbar );
  126. // Make sure successive change does not throw, e.g. attempting
  127. // to insert the toolbar twice.
  128. editor.ui.fire( 'update' );
  129. expect( balloon.visibleView ).to.equal( toolbar );
  130. } );
  131. it( 'should not show the toolbar on ui#update when the image inside table is selected', () => {
  132. setData(
  133. model,
  134. '<paragraph>[foo]</paragraph>' +
  135. '<table><tableRow><tableCell><paragraph>foo</paragraph><image src=""></image></tableCell></tableRow></table>'
  136. );
  137. expect( balloon.visibleView ).to.be.null;
  138. const imageToolbar = widgetToolbarRepository._toolbarDefinitions.get( 'image' ).view;
  139. model.change( writer => {
  140. // Select the <tableCell><paragraph></paragraph>[<image></image>]</tableCell>
  141. const nodeByPath = doc.getRoot().getNodeByPath( [ 1, 0, 0, 1 ] );
  142. writer.setSelection( nodeByPath, 'on' );
  143. } );
  144. expect( balloon.visibleView ).to.equal( imageToolbar );
  145. model.change( writer => {
  146. // Select the <tableCell><paragraph>[]</paragraph><image></image></tableCell>
  147. writer.setSelection(
  148. writer.createPositionAt( doc.getRoot().getNodeByPath( [ 1, 0, 0, 0 ] ), 0 )
  149. );
  150. } );
  151. expect( balloon.visibleView ).to.equal( toolbar );
  152. } );
  153. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  154. setData( model, '<table><tableRow><tableCell><paragraph>x[y]z</paragraph></tableCell></tableRow></table>' );
  155. expect( balloon.visibleView ).to.equal( toolbar );
  156. // Put anything on top of the ContextualBalloon stack above the table toolbar.
  157. const lastView = new View();
  158. lastView.element = document.createElement( 'div' );
  159. balloon.add( {
  160. view: lastView,
  161. position: {
  162. target: document.body
  163. }
  164. } );
  165. expect( balloon.visibleView ).to.equal( lastView );
  166. editor.ui.fire( 'update' );
  167. expect( balloon.visibleView ).to.equal( lastView );
  168. } );
  169. it( 'should hide the toolbar on render if the table is de–selected', () => {
  170. setData(
  171. model,
  172. '<paragraph>foo</paragraph><table><tableRow><tableCell><paragraph>[]</paragraph></tableCell></tableRow></table>'
  173. );
  174. expect( balloon.visibleView ).to.equal( toolbar );
  175. model.change( writer => {
  176. // Select the <paragraph>[...]</paragraph>
  177. writer.setSelection(
  178. writer.createRangeIn( doc.getRoot().getChild( 0 ) )
  179. );
  180. } );
  181. expect( balloon.visibleView ).to.be.null;
  182. // Make sure successive change does not throw, e.g. attempting
  183. // to remove the toolbar twice.
  184. editor.ui.fire( 'update' );
  185. expect( balloon.visibleView ).to.be.null;
  186. } );
  187. } );
  188. } );
  189. describe( 'tableToolbar', () => {
  190. let editor, element, widgetToolbarRepository, balloon, toolbar, model;
  191. beforeEach( () => {
  192. element = document.createElement( 'div' );
  193. document.body.appendChild( element );
  194. return ClassicTestEditor.create( element, {
  195. plugins: [ Paragraph, Table, TableToolbar, FakeButton ],
  196. table: {
  197. tableToolbar: [ 'fake_button' ]
  198. }
  199. } ).then( _editor => {
  200. editor = _editor;
  201. widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  202. toolbar = widgetToolbarRepository._toolbarDefinitions.get( 'table' ).view;
  203. balloon = editor.plugins.get( 'ContextualBalloon' );
  204. model = editor.model;
  205. } );
  206. } );
  207. afterEach( () => {
  208. return editor.destroy()
  209. .then( () => element.remove() );
  210. } );
  211. describe( 'toolbar', () => {
  212. it( 'should not initialize if there is no configuration', () => {
  213. const editorElement = global.document.createElement( 'div' );
  214. global.document.body.appendChild( editorElement );
  215. return ClassicTestEditor.create( editorElement, {
  216. plugins: [ TableToolbar ]
  217. } )
  218. .then( editor => {
  219. const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  220. expect( widgetToolbarRepository._toolbarDefinitions.get( 'table' ) ).to.be.undefined;
  221. editorElement.remove();
  222. return editor.destroy();
  223. } );
  224. } );
  225. it( 'should use the config.table.tableWidget to create items', () => {
  226. expect( toolbar.items ).to.have.length( 1 );
  227. expect( toolbar.items.get( 0 ).label ).to.equal( 'fake button' );
  228. } );
  229. it( 'should set proper CSS classes', () => {
  230. const spy = sinon.spy( balloon, 'add' );
  231. editor.ui.focusTracker.isFocused = true;
  232. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  233. sinon.assert.calledWithMatch( spy, sinon.match( ( { balloonClassName, view } ) => {
  234. return view === toolbar && balloonClassName === 'ck-toolbar-container';
  235. } ) );
  236. } );
  237. } );
  238. describe( 'integration with the editor focus', () => {
  239. it( 'should show the toolbar when the editor gains focus and the table is selected', () => {
  240. editor.ui.focusTracker.isFocused = true;
  241. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  242. editor.ui.focusTracker.isFocused = false;
  243. expect( balloon.visibleView ).to.be.null;
  244. editor.ui.focusTracker.isFocused = true;
  245. expect( balloon.visibleView ).to.equal( toolbar );
  246. } );
  247. it( 'should hide the toolbar when the editor loses focus and the table is selected', () => {
  248. editor.ui.focusTracker.isFocused = false;
  249. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  250. editor.ui.focusTracker.isFocused = true;
  251. expect( balloon.visibleView ).to.equal( toolbar );
  252. editor.ui.focusTracker.isFocused = false;
  253. expect( balloon.visibleView ).to.be.null;
  254. } );
  255. } );
  256. describe( 'integration with the editor selection', () => {
  257. beforeEach( () => {
  258. editor.ui.focusTracker.isFocused = true;
  259. } );
  260. it( 'should show the toolbar on ui#update when the table widget is selected', () => {
  261. setData( editor.model, '<paragraph>[foo]</paragraph><table><tableRow><tableCell></tableCell></tableRow></table>' );
  262. expect( balloon.visibleView ).to.be.null;
  263. editor.ui.fire( 'update' );
  264. expect( balloon.visibleView ).to.be.null;
  265. editor.model.change( writer => {
  266. // Select the [<table></table>]
  267. writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 'on' );
  268. } );
  269. expect( balloon.visibleView ).to.equal( toolbar );
  270. // Make sure successive change does not throw, e.g. attempting
  271. // to insert the toolbar twice.
  272. editor.ui.fire( 'update' );
  273. expect( balloon.visibleView ).to.equal( toolbar );
  274. } );
  275. it( 'should not show the toolbar on ui#update when the selection is inside a table cell', () => {
  276. setData( editor.model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  277. expect( balloon.visibleView ).to.be.null;
  278. editor.ui.fire( 'update' );
  279. expect( balloon.visibleView ).to.be.null;
  280. } );
  281. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  282. setData( model, '<table><tableRow><tableCell></tableCell></tableRow></table>' );
  283. expect( balloon.visibleView ).to.equal( toolbar );
  284. const lastView = new View();
  285. lastView.element = document.createElement( 'div' );
  286. balloon.add( {
  287. view: lastView,
  288. position: {
  289. target: document.body
  290. }
  291. } );
  292. expect( balloon.visibleView ).to.equal( lastView );
  293. editor.ui.fire( 'update' );
  294. expect( balloon.visibleView ).to.equal( lastView );
  295. } );
  296. it( 'should hide the toolbar on ui#update if the table is de–selected', () => {
  297. setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  298. expect( balloon.visibleView ).to.equal( toolbar );
  299. model.change( writer => {
  300. // Select the <paragraph>[...]</paragraph>
  301. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  302. } );
  303. expect( balloon.visibleView ).to.be.null;
  304. // Make sure successive change does not throw, e.g. attempting
  305. // to remove the toolbar twice.
  306. editor.ui.fire( 'update' );
  307. expect( balloon.visibleView ).to.be.null;
  308. } );
  309. } );
  310. } );
  311. } );
  312. // Plugin that adds fake_button to editor's component factory.
  313. class FakeButton extends Plugin {
  314. init() {
  315. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  316. const view = new ButtonView( locale );
  317. view.set( {
  318. label: 'fake button'
  319. } );
  320. return view;
  321. } );
  322. }
  323. }