8
0

mediaembedtoolbar.js 8.4 KB

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