8
0

tabletoolbar.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. } );
  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( 'tableToolbar', () => {
  186. let editor, element, widgetToolbarRepository, balloon, toolbar, model;
  187. beforeEach( () => {
  188. element = document.createElement( 'div' );
  189. document.body.appendChild( element );
  190. return ClassicTestEditor.create( element, {
  191. plugins: [ Paragraph, Table, TableToolbar, FakeButton ],
  192. table: {
  193. tableToolbar: [ 'fake_button' ]
  194. }
  195. } ).then( _editor => {
  196. editor = _editor;
  197. widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  198. toolbar = widgetToolbarRepository._toolbarDefinitions.get( 'table' ).view;
  199. balloon = editor.plugins.get( 'ContextualBalloon' );
  200. model = editor.model;
  201. } );
  202. } );
  203. afterEach( () => {
  204. return editor.destroy()
  205. .then( () => element.remove() );
  206. } );
  207. describe( 'toolbar', () => {
  208. it( 'should not initialize if there is no configuration', () => {
  209. const editorElement = global.document.createElement( 'div' );
  210. global.document.body.appendChild( editorElement );
  211. return ClassicTestEditor.create( editorElement, {
  212. plugins: [ TableToolbar ]
  213. } )
  214. .then( editor => {
  215. const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  216. expect( widgetToolbarRepository._toolbarDefinitions.get( 'table' ) ).to.be.undefined;
  217. editorElement.remove();
  218. return editor.destroy();
  219. } );
  220. } );
  221. it( 'should use the config.table.tableWidget to create items', () => {
  222. expect( toolbar.items ).to.have.length( 1 );
  223. expect( toolbar.items.get( 0 ).label ).to.equal( 'fake button' );
  224. } );
  225. it( 'should set proper CSS classes', () => {
  226. const spy = sinon.spy( balloon, 'add' );
  227. editor.ui.focusTracker.isFocused = true;
  228. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  229. sinon.assert.calledWithMatch( spy, {
  230. view: toolbar,
  231. balloonClassName: 'ck-toolbar-container'
  232. } );
  233. } );
  234. } );
  235. describe( 'integration with the editor focus', () => {
  236. it( 'should show the toolbar when the editor gains focus and the table is selected', () => {
  237. editor.ui.focusTracker.isFocused = true;
  238. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  239. editor.ui.focusTracker.isFocused = false;
  240. expect( balloon.visibleView ).to.be.null;
  241. editor.ui.focusTracker.isFocused = true;
  242. expect( balloon.visibleView ).to.equal( toolbar );
  243. } );
  244. it( 'should hide the toolbar when the editor loses focus and the table is selected', () => {
  245. editor.ui.focusTracker.isFocused = false;
  246. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  247. editor.ui.focusTracker.isFocused = true;
  248. expect( balloon.visibleView ).to.equal( toolbar );
  249. editor.ui.focusTracker.isFocused = false;
  250. expect( balloon.visibleView ).to.be.null;
  251. } );
  252. } );
  253. describe( 'integration with the editor selection', () => {
  254. beforeEach( () => {
  255. editor.ui.focusTracker.isFocused = true;
  256. } );
  257. it( 'should show the toolbar on ui#update when the table widget is selected', () => {
  258. setData( editor.model, '<paragraph>[foo]</paragraph><table><tableRow><tableCell></tableCell></tableRow></table>' );
  259. expect( balloon.visibleView ).to.be.null;
  260. editor.ui.fire( 'update' );
  261. expect( balloon.visibleView ).to.be.null;
  262. editor.model.change( writer => {
  263. // Select the [<table></table>]
  264. writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 'on' );
  265. } );
  266. expect( balloon.visibleView ).to.equal( toolbar );
  267. // Make sure successive change does not throw, e.g. attempting
  268. // to insert the toolbar twice.
  269. editor.ui.fire( 'update' );
  270. expect( balloon.visibleView ).to.equal( toolbar );
  271. } );
  272. it( 'should not show the toolbar on ui#update when the selection is inside a table cell', () => {
  273. setData( editor.model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  274. expect( balloon.visibleView ).to.be.null;
  275. editor.ui.fire( 'update' );
  276. expect( balloon.visibleView ).to.be.null;
  277. } );
  278. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  279. setData( model, '<table><tableRow><tableCell></tableCell></tableRow></table>' );
  280. expect( balloon.visibleView ).to.equal( toolbar );
  281. const lastView = new View();
  282. lastView.element = document.createElement( 'div' );
  283. balloon.add( {
  284. view: lastView,
  285. position: {
  286. target: document.body
  287. }
  288. } );
  289. expect( balloon.visibleView ).to.equal( lastView );
  290. editor.ui.fire( 'update' );
  291. expect( balloon.visibleView ).to.equal( lastView );
  292. } );
  293. it( 'should hide the toolbar on ui#update if the table is de–selected', () => {
  294. setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  295. expect( balloon.visibleView ).to.equal( toolbar );
  296. model.change( writer => {
  297. // Select the <paragraph>[...]</paragraph>
  298. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  299. } );
  300. expect( balloon.visibleView ).to.be.null;
  301. // Make sure successive change does not throw, e.g. attempting
  302. // to remove the toolbar twice.
  303. editor.ui.fire( 'update' );
  304. expect( balloon.visibleView ).to.be.null;
  305. } );
  306. } );
  307. } );
  308. } );
  309. // Plugin that adds fake_button to editor's component factory.
  310. class FakeButton extends Plugin {
  311. init() {
  312. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  313. const view = new ButtonView( locale );
  314. view.set( {
  315. label: 'fake button'
  316. } );
  317. return view;
  318. } );
  319. }
  320. }