tabletoolbar.js 14 KB

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