8
0

tabletoolbar.js 14 KB

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