tabletoolbar.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, 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. balloon = editor.plugins.get( 'ContextualBalloon' );
  35. } );
  36. } );
  37. afterEach( () => {
  38. editorElement.remove();
  39. return editor.destroy();
  40. } );
  41. it( 'should be loaded', () => {
  42. expect( editor.plugins.get( TableToolbar ) ).to.be.instanceOf( TableToolbar );
  43. } );
  44. it( 'should not initialize if there is no configuration', () => {
  45. const editorElement = global.document.createElement( 'div' );
  46. global.document.body.appendChild( editorElement );
  47. return ClassicEditor.create( editorElement, {
  48. plugins: [ TableToolbar ]
  49. } )
  50. .then( editor => {
  51. expect( editor.plugins.get( TableToolbar )._toolbar ).to.be.undefined;
  52. editorElement.remove();
  53. return editor.destroy();
  54. } );
  55. } );
  56. describe( 'toolbar', () => {
  57. it( 'should use the config.table.toolbar to create items', () => {
  58. expect( toolbar.items ).to.have.length( 1 );
  59. expect( toolbar.items.get( 0 ).label ).to.equal( 'fake button' );
  60. } );
  61. it( 'should set proper CSS classes', () => {
  62. const spy = sinon.spy( balloon, 'add' );
  63. editor.ui.focusTracker.isFocused = true;
  64. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  65. sinon.assert.calledWithMatch( spy, {
  66. view: toolbar,
  67. balloonClassName: 'ck-toolbar-container'
  68. } );
  69. } );
  70. } );
  71. describe( 'integration with the editor focus', () => {
  72. it( 'should show the toolbar when the editor gains focus and the table is selected', () => {
  73. editor.ui.focusTracker.isFocused = true;
  74. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  75. editor.ui.focusTracker.isFocused = false;
  76. expect( balloon.visibleView ).to.be.null;
  77. editor.ui.focusTracker.isFocused = true;
  78. expect( balloon.visibleView ).to.equal( toolbar );
  79. } );
  80. it( 'should hide the toolbar when the editor loses focus and the table is selected', () => {
  81. editor.ui.focusTracker.isFocused = false;
  82. setData( model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  83. editor.ui.focusTracker.isFocused = true;
  84. expect( balloon.visibleView ).to.equal( toolbar );
  85. editor.ui.focusTracker.isFocused = false;
  86. expect( balloon.visibleView ).to.be.null;
  87. } );
  88. } );
  89. describe( 'integration with the editor selection (ui#update event)', () => {
  90. beforeEach( () => {
  91. editor.ui.focusTracker.isFocused = true;
  92. } );
  93. it( 'should not show the toolbar on ui#update when the table is selected', () => {
  94. setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  95. expect( balloon.visibleView ).to.be.null;
  96. } );
  97. it( 'should show the toolbar on ui#update when the table content is selected', () => {
  98. setData(
  99. model,
  100. '<paragraph>[foo]</paragraph><table><tableRow><tableCell><paragraph>bar</paragraph></tableCell></tableRow></table>'
  101. );
  102. expect( balloon.visibleView ).to.be.null;
  103. editor.ui.fire( 'update' );
  104. expect( balloon.visibleView ).to.be.null;
  105. model.change( writer => {
  106. // Select the <tableCell>[bar]</tableCell>
  107. writer.setSelection(
  108. Range.createOn( doc.getRoot().getChild( 1 ).getChild( 0 ).getChild( 0 ).getChild( 0 ) )
  109. );
  110. } );
  111. expect( balloon.visibleView ).to.equal( toolbar );
  112. // Make sure successive change does not throw, e.g. attempting
  113. // to insert the toolbar twice.
  114. editor.ui.fire( 'update' );
  115. expect( balloon.visibleView ).to.equal( toolbar );
  116. } );
  117. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  118. setData( model, '<table><tableRow><tableCell><paragraph>x[y]z</paragraph></tableCell></tableRow></table>' );
  119. expect( balloon.visibleView ).to.equal( toolbar );
  120. // Put anything on top of the ContextualBalloon stack above the table toolbar.
  121. const lastView = new View();
  122. lastView.element = document.createElement( 'div' );
  123. balloon.add( {
  124. view: lastView,
  125. position: {
  126. target: document.body
  127. }
  128. } );
  129. expect( balloon.visibleView ).to.equal( lastView );
  130. editor.ui.fire( 'update' );
  131. expect( balloon.visibleView ).to.equal( lastView );
  132. } );
  133. it( 'should hide the toolbar on render if the table is de–selected', () => {
  134. setData(
  135. model,
  136. '<paragraph>foo</paragraph><table><tableRow><tableCell><paragraph>[]</paragraph></tableCell></tableRow></table>'
  137. );
  138. expect( balloon.visibleView ).to.equal( toolbar );
  139. model.change( writer => {
  140. // Select the <paragraph>[...]</paragraph>
  141. writer.setSelection(
  142. Range.createIn( doc.getRoot().getChild( 0 ) )
  143. );
  144. } );
  145. expect( balloon.visibleView ).to.be.null;
  146. // Make sure successive change does not throw, e.g. attempting
  147. // to remove the toolbar twice.
  148. editor.ui.fire( 'update' );
  149. expect( balloon.visibleView ).to.be.null;
  150. } );
  151. } );
  152. // Plugin that adds fake_button to editor's component factory.
  153. class FakeButton extends Plugin {
  154. init() {
  155. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  156. const view = new ButtonView( locale );
  157. view.set( {
  158. label: 'fake button'
  159. } );
  160. return view;
  161. } );
  162. }
  163. }
  164. } );