mediaembedtoolbar.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document, console */
  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, sinon.match( ( { balloonClassName, view } ) => {
  53. return view === toolbar && balloonClassName === 'ck-toolbar-container';
  54. } ) );
  55. } );
  56. it( 'should set aria-label attribute', () => {
  57. toolbar.render();
  58. expect( toolbar.element.getAttribute( 'aria-label' ) ).to.equal( 'Media toolbar' );
  59. toolbar.destroy();
  60. } );
  61. } );
  62. describe( 'integration with the editor focus', () => {
  63. it( 'should show the toolbar when the editor gains focus and the media widget is selected', () => {
  64. editor.ui.focusTracker.isFocused = true;
  65. setData( editor.model, '[<media url=""></media>]' );
  66. editor.ui.focusTracker.isFocused = false;
  67. expect( balloon.visibleView ).to.be.null;
  68. editor.ui.focusTracker.isFocused = true;
  69. expect( balloon.visibleView ).to.equal( toolbar );
  70. } );
  71. it( 'should hide the toolbar when the editor loses focus and the media widget is selected', () => {
  72. editor.ui.focusTracker.isFocused = false;
  73. setData( editor.model, '[<media url=""></media>]' );
  74. editor.ui.focusTracker.isFocused = true;
  75. expect( balloon.visibleView ).to.equal( toolbar );
  76. editor.ui.focusTracker.isFocused = false;
  77. expect( balloon.visibleView ).to.be.null;
  78. } );
  79. } );
  80. describe( 'integration with the editor selection', () => {
  81. beforeEach( () => {
  82. editor.ui.focusTracker.isFocused = true;
  83. } );
  84. it( 'should show the toolbar on ui#update when the media widget is selected', () => {
  85. setData( editor.model, '<paragraph>[foo]</paragraph><media url=""></media>' );
  86. expect( balloon.visibleView ).to.be.null;
  87. editor.ui.fire( 'update' );
  88. expect( balloon.visibleView ).to.be.null;
  89. editor.model.change( writer => {
  90. // Select the [<media></media>]
  91. writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 'on' );
  92. } );
  93. expect( balloon.visibleView ).to.equal( toolbar );
  94. // Make sure successive change does not throw, e.g. attempting
  95. // to insert the toolbar twice.
  96. editor.ui.fire( 'update' );
  97. expect( balloon.visibleView ).to.equal( toolbar );
  98. } );
  99. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  100. setData( editor.model, '<media url=""></media>' );
  101. expect( balloon.visibleView ).to.equal( toolbar );
  102. const lastView = new View();
  103. lastView.element = document.createElement( 'div' );
  104. balloon.add( {
  105. view: lastView,
  106. position: {
  107. target: document.body
  108. }
  109. } );
  110. expect( balloon.visibleView ).to.equal( lastView );
  111. editor.ui.fire( 'update' );
  112. expect( balloon.visibleView ).to.equal( lastView );
  113. } );
  114. it( 'should hide the toolbar on ui#update if the media is de–selected', () => {
  115. setData( model, '<paragraph>foo</paragraph>[<media url=""></media>]' );
  116. expect( balloon.visibleView ).to.equal( toolbar );
  117. model.change( writer => {
  118. // Select the <paragraph>[...]</paragraph>
  119. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  120. } );
  121. expect( balloon.visibleView ).to.be.null;
  122. // Make sure successive change does not throw, e.g. attempting
  123. // to remove the toolbar twice.
  124. editor.ui.fire( 'update' );
  125. expect( balloon.visibleView ).to.be.null;
  126. } );
  127. } );
  128. } );
  129. describe( 'MediaEmbedToolbar - integration with BalloonEditor', () => {
  130. let clock, editor, balloonToolbar, element, widgetToolbarRepository, balloon, toolbar, model;
  131. testUtils.createSinonSandbox();
  132. beforeEach( () => {
  133. element = document.createElement( 'div' );
  134. document.body.appendChild( element );
  135. clock = testUtils.sinon.useFakeTimers();
  136. return BalloonEditor.create( element, {
  137. plugins: [ Paragraph, MediaEmbed, MediaEmbedToolbar, FakeButton, Bold ],
  138. balloonToolbar: [ 'bold' ],
  139. mediaEmbed: {
  140. toolbar: [ 'fake_button' ]
  141. }
  142. } ).then( _editor => {
  143. editor = _editor;
  144. model = editor.model;
  145. widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
  146. toolbar = widgetToolbarRepository._toolbarDefinitions.get( 'mediaEmbed' ).view;
  147. balloon = editor.plugins.get( 'ContextualBalloon' );
  148. balloonToolbar = editor.plugins.get( 'BalloonToolbar' );
  149. editor.ui.focusTracker.isFocused = true;
  150. } );
  151. } );
  152. afterEach( () => {
  153. return editor.destroy()
  154. .then( () => element.remove() );
  155. } );
  156. it( 'balloon toolbar should be hidden when media widget is selected', () => {
  157. setData( model, '<paragraph>[abc]</paragraph><media url=""></media>' );
  158. editor.editing.view.document.isFocused = true;
  159. expect( balloon.visibleView ).to.equal( null );
  160. model.change( writer => {
  161. // Select the [<media></media>]
  162. writer.setSelection( model.document.getRoot().getChild( 1 ), 'on' );
  163. } );
  164. expect( balloon.visibleView ).to.equal( toolbar );
  165. clock.tick( 200 );
  166. expect( balloon.visibleView ).to.equal( toolbar );
  167. } );
  168. it( 'balloon toolbar should be visible when media widget is not selected', () => {
  169. setData( model, '<paragraph>abc</paragraph>[<media url=""></media>]' );
  170. editor.editing.view.document.isFocused = true;
  171. expect( balloon.visibleView ).to.equal( toolbar );
  172. model.change( writer => {
  173. // Select the <paragraph>[abc]</paragraph>
  174. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  175. } );
  176. clock.tick( 200 );
  177. expect( balloon.visibleView ).to.equal( balloonToolbar.toolbarView );
  178. } );
  179. it( 'does not create the toolbar if its items are not specified', () => {
  180. const consoleWarnStub = sinon.stub( console, 'warn' );
  181. const element = document.createElement( 'div' );
  182. return BalloonEditor.create( element, {
  183. plugins: [ Paragraph, MediaEmbed, MediaEmbedToolbar, Bold ]
  184. } ).then( editor => {
  185. widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
  186. expect( widgetToolbarRepository._toolbarDefinitions.get( 'mediaEmbed' ) ).to.be.undefined;
  187. expect( consoleWarnStub.calledOnce ).to.equal( true );
  188. expect( consoleWarnStub.firstCall.args[ 0 ] ).to.match( /^widget-toolbar-no-items/ );
  189. element.remove();
  190. return editor.destroy();
  191. } );
  192. } );
  193. } );
  194. // Plugin that adds fake_button to editor's component factory.
  195. class FakeButton extends Plugin {
  196. init() {
  197. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  198. const view = new ButtonView( locale );
  199. view.set( {
  200. label: 'fake button'
  201. } );
  202. return view;
  203. } );
  204. }
  205. }