tabletoolbar.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import ClassicEditor 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. describe( 'TableToolbar', () => {
  17. let editor, model, doc, editingView, plugin, toolbar, balloon, editorElement;
  18. beforeEach( () => {
  19. editorElement = global.document.createElement( 'div' );
  20. global.document.body.appendChild( editorElement );
  21. return ClassicEditor
  22. .create( editorElement, {
  23. plugins: [ Paragraph, Table, TableToolbar, FakeButton ],
  24. table: {
  25. toolbar: [ 'fake_button' ]
  26. }
  27. } )
  28. .then( newEditor => {
  29. editor = newEditor;
  30. model = newEditor.model;
  31. doc = model.document;
  32. plugin = editor.plugins.get( TableToolbar );
  33. toolbar = plugin._toolbar;
  34. editingView = editor.editing.view;
  35. balloon = editor.plugins.get( 'ContextualBalloon' );
  36. } );
  37. } );
  38. afterEach( () => {
  39. editorElement.remove();
  40. return editor.destroy();
  41. } );
  42. it( 'should be loaded', () => {
  43. expect( editor.plugins.get( TableToolbar ) ).to.be.instanceOf( TableToolbar );
  44. } );
  45. it( 'should not initialize if there is no configuration', () => {
  46. const editorElement = global.document.createElement( 'div' );
  47. global.document.body.appendChild( editorElement );
  48. return ClassicEditor.create( editorElement, {
  49. plugins: [ TableToolbar ]
  50. } )
  51. .then( editor => {
  52. expect( editor.plugins.get( TableToolbar )._toolbar ).to.be.undefined;
  53. editorElement.remove();
  54. return editor.destroy();
  55. } );
  56. } );
  57. describe( 'toolbar', () => {
  58. it( 'should use the config.table.toolbar to create items', () => {
  59. expect( toolbar.items ).to.have.length( 1 );
  60. expect( toolbar.items.get( 0 ).label ).to.equal( 'fake button' );
  61. } );
  62. it( 'should set proper CSS classes', () => {
  63. const spy = sinon.spy( balloon, 'add' );
  64. editor.ui.focusTracker.isFocused = true;
  65. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  66. // "Flush" throttled update event.
  67. editor.ui.fire( 'update' );
  68. sinon.assert.calledWithMatch( spy, {
  69. view: toolbar,
  70. balloonClassName: 'ck-toolbar-container'
  71. } );
  72. } );
  73. } );
  74. describe( 'integration with the editor focus', () => {
  75. it( 'should show the toolbar when the editor gains focus and the table is selected', () => {
  76. editor.ui.focusTracker.isFocused = true;
  77. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  78. editor.ui.focusTracker.isFocused = false;
  79. expect( balloon.visibleView ).to.be.null;
  80. editor.ui.focusTracker.isFocused = true;
  81. expect( balloon.visibleView ).to.equal( toolbar );
  82. } );
  83. it( 'should hide the toolbar when the editor loses focus and the table is selected', () => {
  84. editor.ui.focusTracker.isFocused = false;
  85. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  86. editor.ui.focusTracker.isFocused = true;
  87. expect( balloon.visibleView ).to.equal( toolbar );
  88. editor.ui.focusTracker.isFocused = false;
  89. expect( balloon.visibleView ).to.be.null;
  90. } );
  91. } );
  92. describe( 'integration with the editor selection (ui#update event)', () => {
  93. beforeEach( () => {
  94. editor.ui.focusTracker.isFocused = true;
  95. } );
  96. it( 'should not show the toolbar on ui#update when the table is selected', () => {
  97. setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  98. // "Flush" throttled update event.
  99. editor.ui.fire( 'update' );
  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( model, '<paragraph>[foo]</paragraph><table><tableRow><tableCell></tableCell></tableRow></table>' );
  104. expect( balloon.visibleView ).to.be.null;
  105. editor.ui.fire( 'update' );
  106. expect( balloon.visibleView ).to.be.null;
  107. model.change( writer => {
  108. // Select the [<tableCell></tableCell>]
  109. writer.setSelection(
  110. Range.createOn( doc.getRoot().getChild( 1 ).getChild( 0 ).getChild( 0 ) )
  111. );
  112. } );
  113. // "Flush" throttled update event.
  114. editor.ui.fire( 'update' );
  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>x[y]z</tableCell></tableRow></table>' );
  123. // "Flush" throttled update event.
  124. editor.ui.fire( 'update' );
  125. expect( balloon.visibleView ).to.equal( toolbar );
  126. // Put anything on top of the ContextualBalloon stack above the table toolbar.
  127. const lastView = new View();
  128. lastView.element = document.createElement( 'div' );
  129. balloon.add( {
  130. view: lastView,
  131. position: {
  132. target: document.body
  133. }
  134. } );
  135. expect( balloon.visibleView ).to.equal( lastView );
  136. editor.ui.fire( 'update' );
  137. expect( balloon.visibleView ).to.equal( lastView );
  138. } );
  139. it( 'should hide the toolbar on render if the table is de–selected', () => {
  140. setData( model, '<paragraph>foo</paragraph><table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  141. // "Flush" throttled update event.
  142. editor.ui.fire( 'update' );
  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. // "Flush" throttled update event.
  151. editor.ui.fire( 'update' );
  152. expect( balloon.visibleView ).to.be.null;
  153. // Make sure successive change does not throw, e.g. attempting
  154. // to remove the toolbar twice.
  155. editor.ui.fire( 'update' );
  156. expect( balloon.visibleView ).to.be.null;
  157. } );
  158. } );
  159. // Plugin that adds fake_button to editor's component factory.
  160. class FakeButton extends Plugin {
  161. init() {
  162. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  163. const view = new ButtonView( locale );
  164. view.set( {
  165. label: 'fake button'
  166. } );
  167. return view;
  168. } );
  169. }
  170. }
  171. } );