tabletoolbar.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. import TableSelection from '../src/tableselection';
  17. describe( 'TableToolbar', () => {
  18. let editor, model, doc, plugin, toolbar, balloon, editorElement;
  19. beforeEach( () => {
  20. editorElement = global.document.createElement( 'div' );
  21. global.document.body.appendChild( editorElement );
  22. return ClassicEditor
  23. .create( editorElement, {
  24. plugins: [ Paragraph, Table, TableSelection, TableToolbar, FakeButton ],
  25. table: {
  26. toolbar: [ 'fake_button' ]
  27. }
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. model = newEditor.model;
  32. doc = model.document;
  33. plugin = editor.plugins.get( TableToolbar );
  34. toolbar = plugin._toolbar;
  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. sinon.assert.calledWithMatch( spy, {
  67. view: toolbar,
  68. balloonClassName: 'ck-toolbar-container'
  69. } );
  70. } );
  71. } );
  72. describe( 'integration with the editor focus', () => {
  73. it( 'should show the toolbar when the editor gains focus and the table is selected', () => {
  74. editor.ui.focusTracker.isFocused = true;
  75. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  76. editor.ui.focusTracker.isFocused = false;
  77. expect( balloon.visibleView ).to.be.null;
  78. editor.ui.focusTracker.isFocused = true;
  79. expect( balloon.visibleView ).to.equal( toolbar );
  80. } );
  81. it( 'should hide the toolbar when the editor loses focus and the table is selected', () => {
  82. editor.ui.focusTracker.isFocused = false;
  83. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  84. editor.ui.focusTracker.isFocused = true;
  85. expect( balloon.visibleView ).to.equal( toolbar );
  86. editor.ui.focusTracker.isFocused = false;
  87. expect( balloon.visibleView ).to.be.null;
  88. } );
  89. } );
  90. describe( 'integration with the editor selection (ui#update event)', () => {
  91. beforeEach( () => {
  92. editor.ui.focusTracker.isFocused = true;
  93. } );
  94. it( 'should not show the toolbar on ui#update when the table is selected', () => {
  95. setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  96. expect( balloon.visibleView ).to.be.null;
  97. } );
  98. it( 'should show the toolbar on ui#update when the table content is selected', () => {
  99. setData(
  100. model,
  101. '<paragraph>[foo]</paragraph><table><tableRow><tableCell><paragraph>bar</paragraph></tableCell></tableRow></table>'
  102. );
  103. expect( balloon.visibleView ).to.be.null;
  104. editor.ui.fire( 'update' );
  105. expect( balloon.visibleView ).to.be.null;
  106. model.change( writer => {
  107. // Select the <tableCell>[bar]</tableCell>
  108. writer.setSelection(
  109. Range.createOn( doc.getRoot().getChild( 1 ).getChild( 0 ).getChild( 0 ).getChild( 0 ) )
  110. );
  111. } );
  112. expect( balloon.visibleView ).to.equal( toolbar );
  113. // Make sure successive change does not throw, e.g. attempting
  114. // to insert the toolbar twice.
  115. editor.ui.fire( 'update' );
  116. expect( balloon.visibleView ).to.equal( toolbar );
  117. } );
  118. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  119. setData( model, '<table><tableRow><tableCell><paragraph>x[y]z</paragraph></tableCell></tableRow></table>' );
  120. expect( balloon.visibleView ).to.equal( toolbar );
  121. // Put anything on top of the ContextualBalloon stack above the table toolbar.
  122. const lastView = new View();
  123. lastView.element = document.createElement( 'div' );
  124. balloon.add( {
  125. view: lastView,
  126. position: {
  127. target: document.body
  128. }
  129. } );
  130. expect( balloon.visibleView ).to.equal( lastView );
  131. editor.ui.fire( 'update' );
  132. expect( balloon.visibleView ).to.equal( lastView );
  133. } );
  134. it( 'should hide the toolbar on render if the table is de–selected', () => {
  135. setData(
  136. model,
  137. '<paragraph>foo</paragraph><table><tableRow><tableCell><paragraph>[]</paragraph></tableCell></tableRow></table>'
  138. );
  139. expect( balloon.visibleView ).to.equal( toolbar );
  140. model.change( writer => {
  141. // Select the <paragraph>[...]</paragraph>
  142. writer.setSelection(
  143. Range.createIn( doc.getRoot().getChild( 0 ) )
  144. );
  145. } );
  146. expect( balloon.visibleView ).to.be.null;
  147. // Make sure successive change does not throw, e.g. attempting
  148. // to remove the toolbar twice.
  149. editor.ui.fire( 'update' );
  150. expect( balloon.visibleView ).to.be.null;
  151. } );
  152. } );
  153. // Plugin that adds fake_button to editor's component factory.
  154. class FakeButton extends Plugin {
  155. init() {
  156. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  157. const view = new ButtonView( locale );
  158. view.set( {
  159. label: 'fake button'
  160. } );
  161. return view;
  162. } );
  163. }
  164. }
  165. } );