8
0

tabletoolbar.js 14 KB

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