mediaembedtoolbar.js 7.4 KB

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