mediaembedtoolbar.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import BalloonEditor from '@ckeditor/ckeditor5-editor-balloon/src/ballooneditor';
  8. import MediaEmbed from '../src/mediaembed';
  9. import MediaEmbedToolbar from '../src/mediaembedtoolbar';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import View from '@ckeditor/ckeditor5-ui/src/view';
  13. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  14. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  15. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. describe( 'MediaEmbedToolbar', () => {
  18. let editor, element, widgetToolbarRepository, balloon, toolbar, model;
  19. testUtils.createSinonSandbox();
  20. beforeEach( () => {
  21. element = document.createElement( 'div' );
  22. document.body.appendChild( element );
  23. return ClassicTestEditor.create( element, {
  24. plugins: [ Paragraph, MediaEmbed, MediaEmbedToolbar, FakeButton ],
  25. mediaEmbed: {
  26. toolbar: [ 'fake_button' ]
  27. }
  28. } ).then( _editor => {
  29. editor = _editor;
  30. model = editor.model;
  31. widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
  32. toolbar = widgetToolbarRepository._toolbarDefinitions.get( 'mediaEmbed' ).view;
  33. balloon = editor.plugins.get( 'ContextualBalloon' );
  34. } );
  35. } );
  36. afterEach( () => {
  37. return editor.destroy()
  38. .then( () => element.remove() );
  39. } );
  40. it( 'should be loaded', () => {
  41. expect( editor.plugins.get( MediaEmbedToolbar ) ).to.be.instanceOf( MediaEmbedToolbar );
  42. } );
  43. describe( 'toolbar', () => {
  44. it( 'should use the config.table.tableWidget to create items', () => {
  45. expect( toolbar.items ).to.have.length( 1 );
  46. expect( toolbar.items.get( 0 ).label ).to.equal( 'fake button' );
  47. } );
  48. it( 'should set proper CSS classes', () => {
  49. const spy = sinon.spy( balloon, 'add' );
  50. editor.ui.focusTracker.isFocused = true;
  51. setData( model, '[<media url=""></media>]' );
  52. sinon.assert.calledWithMatch( spy, {
  53. view: toolbar,
  54. balloonClassName: 'ck-toolbar-container'
  55. } );
  56. } );
  57. } );
  58. describe( 'integration with the editor focus', () => {
  59. it( 'should show the toolbar when the editor gains focus and the media widget is selected', () => {
  60. editor.ui.focusTracker.isFocused = true;
  61. setData( editor.model, '[<media url=""></media>]' );
  62. editor.ui.focusTracker.isFocused = false;
  63. expect( balloon.visibleView ).to.be.null;
  64. editor.ui.focusTracker.isFocused = true;
  65. expect( balloon.visibleView ).to.equal( toolbar );
  66. } );
  67. it( 'should hide the toolbar when the editor loses focus and the media widget is selected', () => {
  68. editor.ui.focusTracker.isFocused = false;
  69. setData( editor.model, '[<media url=""></media>]' );
  70. editor.ui.focusTracker.isFocused = true;
  71. expect( balloon.visibleView ).to.equal( toolbar );
  72. editor.ui.focusTracker.isFocused = false;
  73. expect( balloon.visibleView ).to.be.null;
  74. } );
  75. } );
  76. describe( 'integration with the editor selection', () => {
  77. beforeEach( () => {
  78. editor.ui.focusTracker.isFocused = true;
  79. } );
  80. it( 'should show the toolbar on ui#update when the media widget is selected', () => {
  81. setData( editor.model, '<paragraph>[foo]</paragraph><media url=""></media>' );
  82. expect( balloon.visibleView ).to.be.null;
  83. editor.ui.fire( 'update' );
  84. expect( balloon.visibleView ).to.be.null;
  85. editor.model.change( writer => {
  86. // Select the [<media></media>]
  87. writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 'on' );
  88. } );
  89. expect( balloon.visibleView ).to.equal( toolbar );
  90. // Make sure successive change does not throw, e.g. attempting
  91. // to insert the toolbar twice.
  92. editor.ui.fire( 'update' );
  93. expect( balloon.visibleView ).to.equal( toolbar );
  94. } );
  95. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  96. setData( editor.model, '<media url=""></media>' );
  97. expect( balloon.visibleView ).to.equal( toolbar );
  98. const lastView = new View();
  99. lastView.element = document.createElement( 'div' );
  100. balloon.add( {
  101. view: lastView,
  102. position: {
  103. target: document.body
  104. }
  105. } );
  106. expect( balloon.visibleView ).to.equal( lastView );
  107. editor.ui.fire( 'update' );
  108. expect( balloon.visibleView ).to.equal( lastView );
  109. } );
  110. it( 'should hide the toolbar on ui#update if the media is de–selected', () => {
  111. setData( model, '<paragraph>foo</paragraph>[<media url=""></media>]' );
  112. expect( balloon.visibleView ).to.equal( toolbar );
  113. model.change( writer => {
  114. // Select the <paragraph>[...]</paragraph>
  115. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  116. } );
  117. expect( balloon.visibleView ).to.be.null;
  118. // Make sure successive change does not throw, e.g. attempting
  119. // to remove the toolbar twice.
  120. editor.ui.fire( 'update' );
  121. expect( balloon.visibleView ).to.be.null;
  122. } );
  123. } );
  124. } );
  125. describe( 'MediaEmbedToolbar - integration with BalloonEditor', () => {
  126. let clock, editor, balloonToolbar, element, widgetToolbarRepository, balloon, toolbar, model;
  127. testUtils.createSinonSandbox();
  128. beforeEach( () => {
  129. element = document.createElement( 'div' );
  130. document.body.appendChild( element );
  131. clock = testUtils.sinon.useFakeTimers();
  132. return BalloonEditor.create( element, {
  133. plugins: [ Paragraph, MediaEmbed, MediaEmbedToolbar, FakeButton, Bold ],
  134. balloonToolbar: [ 'bold' ],
  135. media: {
  136. toolbar: [ 'fake_button' ]
  137. }
  138. } ).then( _editor => {
  139. editor = _editor;
  140. model = editor.model;
  141. widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
  142. toolbar = widgetToolbarRepository._toolbarDefinitions.get( 'mediaEmbed' ).view;
  143. balloon = editor.plugins.get( 'ContextualBalloon' );
  144. balloonToolbar = editor.plugins.get( 'BalloonToolbar' );
  145. } );
  146. } );
  147. beforeEach( () => {
  148. editor.ui.focusTracker.isFocused = true;
  149. } );
  150. afterEach( () => {
  151. return editor.destroy()
  152. .then( () => element.remove() );
  153. } );
  154. it( 'balloon toolbar should be hidden when media widget is selected', () => {
  155. setData( model, '<paragraph>[abc]</paragraph><media url=""></media>' );
  156. editor.editing.view.document.isFocused = true;
  157. expect( balloon.visibleView ).to.equal( null );
  158. model.change( writer => {
  159. // Select the [<media></media>]
  160. writer.setSelection( model.document.getRoot().getChild( 1 ), 'on' );
  161. } );
  162. expect( balloon.visibleView ).to.equal( toolbar );
  163. clock.tick( 200 );
  164. expect( balloon.visibleView ).to.equal( toolbar );
  165. } );
  166. it( 'balloon toolbar should be visible when media widget is not selected', () => {
  167. setData( model, '<paragraph>abc</paragraph>[<media url=""></media>]' );
  168. editor.editing.view.document.isFocused = true;
  169. expect( balloon.visibleView ).to.equal( toolbar );
  170. model.change( writer => {
  171. // Select the <paragraph>[abc]</paragraph>
  172. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  173. } );
  174. clock.tick( 200 );
  175. expect( balloon.visibleView ).to.equal( balloonToolbar.toolbarView );
  176. } );
  177. } );
  178. // Plugin that adds fake_button to editor's component factory.
  179. class FakeButton extends Plugin {
  180. init() {
  181. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  182. const view = new ButtonView( locale );
  183. view.set( {
  184. label: 'fake button'
  185. } );
  186. return view;
  187. } );
  188. }
  189. }