tabletoolbar.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, window */
  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.toolbar 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. } );
  81. describe( 'integration with the editor focus', () => {
  82. it( 'should show the toolbar when the editor gains focus and the table is selected', () => {
  83. editor.ui.focusTracker.isFocused = true;
  84. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  85. editor.ui.focusTracker.isFocused = false;
  86. expect( balloon.visibleView ).to.be.null;
  87. editor.ui.focusTracker.isFocused = true;
  88. expect( balloon.visibleView ).to.equal( toolbar );
  89. } );
  90. it( 'should hide the toolbar when the editor loses focus and the table is selected', () => {
  91. editor.ui.focusTracker.isFocused = false;
  92. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  93. editor.ui.focusTracker.isFocused = true;
  94. expect( balloon.visibleView ).to.equal( toolbar );
  95. editor.ui.focusTracker.isFocused = false;
  96. expect( balloon.visibleView ).to.be.null;
  97. } );
  98. } );
  99. describe( 'integration with the editor selection (ui#update event)', () => {
  100. beforeEach( () => {
  101. editor.ui.focusTracker.isFocused = true;
  102. } );
  103. it( 'should not show the toolbar on ui#update when the table is selected', () => {
  104. setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  105. expect( balloon.visibleView ).to.be.null;
  106. } );
  107. it( 'should show the toolbar on ui#update when the table content is selected', () => {
  108. setData(
  109. model,
  110. '<paragraph>[foo]</paragraph><table><tableRow><tableCell><paragraph>bar</paragraph></tableCell></tableRow></table>'
  111. );
  112. expect( balloon.visibleView ).to.be.null;
  113. editor.ui.fire( 'update' );
  114. expect( balloon.visibleView ).to.be.null;
  115. model.change( writer => {
  116. // Select the <tableCell>[bar]</tableCell>
  117. writer.setSelection(
  118. writer.createRangeOn( doc.getRoot().getNodeByPath( [ 1, 0, 0, 0 ] ) )
  119. );
  120. } );
  121. expect( balloon.visibleView ).to.equal( toolbar );
  122. // Make sure successive change does not throw, e.g. attempting
  123. // to insert the toolbar twice.
  124. editor.ui.fire( 'update' );
  125. expect( balloon.visibleView ).to.equal( toolbar );
  126. } );
  127. it( 'should not show the toolbar on ui#update when the image inside table is selected', () => {
  128. setData(
  129. model,
  130. '<paragraph>[foo]</paragraph>' +
  131. '<table><tableRow><tableCell><paragraph>foo</paragraph><image src=""></image></tableCell></tableRow></table>'
  132. );
  133. expect( balloon.visibleView ).to.be.null;
  134. const imageToolbar = widgetToolbarRepository._toolbarDefinitions.get( 'image' ).view;
  135. model.change( writer => {
  136. // Select the <tableCell><paragraph></paragraph>[<image></image>]</tableCell>
  137. const nodeByPath = doc.getRoot().getNodeByPath( [ 1, 0, 0, 1 ] );
  138. writer.setSelection( nodeByPath, 'on' );
  139. } );
  140. expect( balloon.visibleView ).to.equal( imageToolbar );
  141. model.change( writer => {
  142. // Select the <tableCell><paragraph>[]</paragraph><image></image></tableCell>
  143. writer.setSelection(
  144. writer.createPositionAt( doc.getRoot().getNodeByPath( [ 1, 0, 0, 0 ] ), 0 )
  145. );
  146. } );
  147. expect( balloon.visibleView ).to.equal( toolbar );
  148. } );
  149. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  150. setData( model, '<table><tableRow><tableCell><paragraph>x[y]z</paragraph></tableCell></tableRow></table>' );
  151. expect( balloon.visibleView ).to.equal( toolbar );
  152. // Put anything on top of the ContextualBalloon stack above the table toolbar.
  153. const lastView = new View();
  154. lastView.element = document.createElement( 'div' );
  155. balloon.add( {
  156. view: lastView,
  157. position: {
  158. target: document.body
  159. }
  160. } );
  161. expect( balloon.visibleView ).to.equal( lastView );
  162. editor.ui.fire( 'update' );
  163. expect( balloon.visibleView ).to.equal( lastView );
  164. } );
  165. it( 'should hide the toolbar on render if the table is de–selected', () => {
  166. setData(
  167. model,
  168. '<paragraph>foo</paragraph><table><tableRow><tableCell><paragraph>[]</paragraph></tableCell></tableRow></table>'
  169. );
  170. expect( balloon.visibleView ).to.equal( toolbar );
  171. model.change( writer => {
  172. // Select the <paragraph>[...]</paragraph>
  173. writer.setSelection(
  174. writer.createRangeIn( doc.getRoot().getChild( 0 ) )
  175. );
  176. } );
  177. expect( balloon.visibleView ).to.be.null;
  178. // Make sure successive change does not throw, e.g. attempting
  179. // to remove the toolbar twice.
  180. editor.ui.fire( 'update' );
  181. expect( balloon.visibleView ).to.be.null;
  182. } );
  183. } );
  184. } );
  185. describe( 'deprecated toolbar', () => {
  186. let editor, editorElement, warnMock;
  187. it( 'table.toolbar should work as table.contentToolbar', () => {
  188. editorElement = global.document.createElement( 'div' );
  189. global.document.body.appendChild( editorElement );
  190. warnMock = sinon.stub( window.console, 'warn' );
  191. return ClassicTestEditor
  192. .create( editorElement, {
  193. plugins: [ Paragraph, Table, TableToolbar, FakeButton ],
  194. table: {
  195. toolbar: [ 'fake_button' ]
  196. }
  197. } )
  198. .then( newEditor => {
  199. editor = newEditor;
  200. const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  201. const toolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'tableContent' ).view;
  202. expect( toolbarView.items ).to.have.length( 1 );
  203. expect( toolbarView.items.get( 0 ).label ).to.equal( 'fake button' );
  204. sinon.assert.calledWith(
  205. warnMock,
  206. '`config.table.toolbar` is deprecated and will be removed in the next major release.' +
  207. ' Use `config.table.contentToolbar` instead.'
  208. );
  209. } );
  210. } );
  211. it( 'table.contentToolbar should be used if both toolbars options are provided', () => {
  212. editorElement = global.document.createElement( 'div' );
  213. global.document.body.appendChild( editorElement );
  214. warnMock = sinon.stub( window.console, 'warn' );
  215. return ClassicTestEditor
  216. .create( editorElement, {
  217. plugins: [ Paragraph, Table, TableToolbar, FakeButton, FooButton ],
  218. table: {
  219. toolbar: [ 'fake_button' ],
  220. contentToolbar: [ 'foo_button' ],
  221. }
  222. } )
  223. .then( newEditor => {
  224. editor = newEditor;
  225. const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  226. const toolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'tableContent' ).view;
  227. expect( toolbarView.items ).to.have.length( 1 );
  228. expect( toolbarView.items.get( 0 ).label ).to.equal( 'foo button' );
  229. } );
  230. } );
  231. afterEach( () => {
  232. editorElement.remove();
  233. return editor.destroy();
  234. } );
  235. } );
  236. describe( 'tableToolbar', () => {
  237. let editor, element, widgetToolbarRepository, balloon, toolbar, model;
  238. beforeEach( () => {
  239. element = document.createElement( 'div' );
  240. document.body.appendChild( element );
  241. return ClassicTestEditor.create( element, {
  242. plugins: [ Paragraph, Table, TableToolbar, FakeButton ],
  243. table: {
  244. tableToolbar: [ 'fake_button' ]
  245. }
  246. } ).then( _editor => {
  247. editor = _editor;
  248. widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  249. toolbar = widgetToolbarRepository._toolbarDefinitions.get( 'table' ).view;
  250. balloon = editor.plugins.get( 'ContextualBalloon' );
  251. model = editor.model;
  252. } );
  253. } );
  254. afterEach( () => {
  255. return editor.destroy()
  256. .then( () => element.remove() );
  257. } );
  258. describe( 'toolbar', () => {
  259. it( 'should not initialize if there is no configuration', () => {
  260. const editorElement = global.document.createElement( 'div' );
  261. global.document.body.appendChild( editorElement );
  262. return ClassicTestEditor.create( editorElement, {
  263. plugins: [ TableToolbar ]
  264. } )
  265. .then( editor => {
  266. const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  267. expect( widgetToolbarRepository._toolbarDefinitions.get( 'table' ) ).to.be.undefined;
  268. editorElement.remove();
  269. return editor.destroy();
  270. } );
  271. } );
  272. it( 'should use the config.table.tableWidget to create items', () => {
  273. expect( toolbar.items ).to.have.length( 1 );
  274. expect( toolbar.items.get( 0 ).label ).to.equal( 'fake button' );
  275. } );
  276. it( 'should set proper CSS classes', () => {
  277. const spy = sinon.spy( balloon, 'add' );
  278. editor.ui.focusTracker.isFocused = true;
  279. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  280. sinon.assert.calledWithMatch( spy, {
  281. view: toolbar,
  282. balloonClassName: 'ck-toolbar-container'
  283. } );
  284. } );
  285. } );
  286. describe( 'integration with the editor focus', () => {
  287. it( 'should show the toolbar when the editor gains focus and the table is selected', () => {
  288. editor.ui.focusTracker.isFocused = true;
  289. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  290. editor.ui.focusTracker.isFocused = false;
  291. expect( balloon.visibleView ).to.be.null;
  292. editor.ui.focusTracker.isFocused = true;
  293. expect( balloon.visibleView ).to.equal( toolbar );
  294. } );
  295. it( 'should hide the toolbar when the editor loses focus and the table is selected', () => {
  296. editor.ui.focusTracker.isFocused = false;
  297. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  298. editor.ui.focusTracker.isFocused = true;
  299. expect( balloon.visibleView ).to.equal( toolbar );
  300. editor.ui.focusTracker.isFocused = false;
  301. expect( balloon.visibleView ).to.be.null;
  302. } );
  303. } );
  304. describe( 'integration with the editor selection', () => {
  305. beforeEach( () => {
  306. editor.ui.focusTracker.isFocused = true;
  307. } );
  308. it( 'should show the toolbar on ui#update when the table widget is selected', () => {
  309. setData( editor.model, '<paragraph>[foo]</paragraph><table><tableRow><tableCell></tableCell></tableRow></table>' );
  310. expect( balloon.visibleView ).to.be.null;
  311. editor.ui.fire( 'update' );
  312. expect( balloon.visibleView ).to.be.null;
  313. editor.model.change( writer => {
  314. // Select the [<table></table>]
  315. writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 'on' );
  316. } );
  317. expect( balloon.visibleView ).to.equal( toolbar );
  318. // Make sure successive change does not throw, e.g. attempting
  319. // to insert the toolbar twice.
  320. editor.ui.fire( 'update' );
  321. expect( balloon.visibleView ).to.equal( toolbar );
  322. } );
  323. it( 'should not show the toolbar on ui#update when the selection is inside a table cell', () => {
  324. setData( editor.model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  325. expect( balloon.visibleView ).to.be.null;
  326. editor.ui.fire( 'update' );
  327. expect( balloon.visibleView ).to.be.null;
  328. } );
  329. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  330. setData( model, '<table><tableRow><tableCell></tableCell></tableRow></table>' );
  331. expect( balloon.visibleView ).to.equal( toolbar );
  332. const lastView = new View();
  333. lastView.element = document.createElement( 'div' );
  334. balloon.add( {
  335. view: lastView,
  336. position: {
  337. target: document.body
  338. }
  339. } );
  340. expect( balloon.visibleView ).to.equal( lastView );
  341. editor.ui.fire( 'update' );
  342. expect( balloon.visibleView ).to.equal( lastView );
  343. } );
  344. it( 'should hide the toolbar on ui#update if the table is de–selected', () => {
  345. setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  346. expect( balloon.visibleView ).to.equal( toolbar );
  347. model.change( writer => {
  348. // Select the <paragraph>[...]</paragraph>
  349. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  350. } );
  351. expect( balloon.visibleView ).to.be.null;
  352. // Make sure successive change does not throw, e.g. attempting
  353. // to remove the toolbar twice.
  354. editor.ui.fire( 'update' );
  355. expect( balloon.visibleView ).to.be.null;
  356. } );
  357. } );
  358. } );
  359. } );
  360. // Plugin that adds fake_button to editor's component factory.
  361. class FakeButton extends Plugin {
  362. init() {
  363. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  364. const view = new ButtonView( locale );
  365. view.set( {
  366. label: 'fake button'
  367. } );
  368. return view;
  369. } );
  370. }
  371. }
  372. // Plugin that adds foo_button to editor's component factory.
  373. class FooButton extends Plugin {
  374. init() {
  375. this.editor.ui.componentFactory.add( 'foo_button', locale => {
  376. const view = new ButtonView( locale );
  377. view.set( {
  378. label: 'foo button'
  379. } );
  380. return view;
  381. } );
  382. }
  383. }