imagetoolbar.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ImageToolbar from '../src/imagetoolbar';
  8. import Image from '../src/image';
  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 View from '@ckeditor/ckeditor5-ui/src/view';
  14. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. describe( 'ImageToolbar', () => {
  17. let editor, model, doc, toolbar, balloon, widgetToolbarRepository, editorElement;
  18. testUtils.createSinonSandbox();
  19. beforeEach( () => {
  20. editorElement = global.document.createElement( 'div' );
  21. global.document.body.appendChild( editorElement );
  22. return ClassicEditor
  23. .create( editorElement, {
  24. plugins: [ Paragraph, Image, ImageToolbar, FakeButton ],
  25. image: {
  26. toolbar: [ 'fake_button' ]
  27. }
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. model = newEditor.model;
  32. doc = model.document;
  33. widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
  34. toolbar = widgetToolbarRepository._toolbarDefinitions.get( 'image' ).view;
  35. balloon = editor.plugins.get( 'ContextualBalloon' );
  36. } );
  37. } );
  38. afterEach( () => {
  39. editorElement.remove();
  40. return editor.destroy();
  41. } );
  42. it( 'should be loaded', () => {
  43. expect( editor.plugins.get( ImageToolbar ) ).to.be.instanceOf( ImageToolbar );
  44. } );
  45. it( 'should not initialize if there is no configuration', () => {
  46. const consoleWarnStub = sinon.stub( console, 'warn' );
  47. const editorElement = global.document.createElement( 'div' );
  48. global.document.body.appendChild( editorElement );
  49. return ClassicEditor.create( editorElement, {
  50. plugins: [ ImageToolbar ]
  51. } )
  52. .then( editor => {
  53. expect( editor.plugins.get( ImageToolbar )._toolbar ).to.be.undefined;
  54. expect( consoleWarnStub.calledOnce ).to.equal( true );
  55. expect( consoleWarnStub.firstCall.args[ 0 ] ).to.match( /widget-toolbar-no-items/ );
  56. editorElement.remove();
  57. return editor.destroy();
  58. } );
  59. } );
  60. describe( 'toolbar', () => {
  61. it( 'should use the config.image.toolbar to create items', () => {
  62. expect( toolbar.items ).to.have.length( 1 );
  63. expect( toolbar.items.get( 0 ).label ).to.equal( 'fake button' );
  64. } );
  65. it( 'should set proper CSS classes', () => {
  66. const spy = sinon.spy( balloon, 'add' );
  67. editor.ui.focusTracker.isFocused = true;
  68. setData( model, '[<image src=""></image>]' );
  69. sinon.assert.calledWithMatch( spy, sinon.match( ( { balloonClassName, view } ) => {
  70. return view === toolbar && balloonClassName === 'ck-toolbar-container';
  71. } ) );
  72. } );
  73. it( 'should set aria-label attribute', () => {
  74. toolbar.render();
  75. expect( toolbar.element.getAttribute( 'aria-label' ) ).to.equal( 'Image toolbar' );
  76. toolbar.destroy();
  77. } );
  78. } );
  79. describe( 'integration with the editor focus', () => {
  80. it( 'should show the toolbar when the editor gains focus and the image is selected', () => {
  81. editor.ui.focusTracker.isFocused = true;
  82. setData( model, '[<image src=""></image>]' );
  83. editor.ui.focusTracker.isFocused = false;
  84. expect( balloon.visibleView ).to.be.null;
  85. editor.ui.focusTracker.isFocused = true;
  86. expect( balloon.visibleView ).to.equal( toolbar );
  87. } );
  88. it( 'should hide the toolbar when the editor loses focus and the image is selected', () => {
  89. editor.ui.focusTracker.isFocused = false;
  90. setData( model, '[<image src=""></image>]' );
  91. editor.ui.focusTracker.isFocused = true;
  92. expect( balloon.visibleView ).to.equal( toolbar );
  93. editor.ui.focusTracker.isFocused = false;
  94. expect( balloon.visibleView ).to.be.null;
  95. } );
  96. } );
  97. describe( 'integration with the editor selection', () => {
  98. beforeEach( () => {
  99. editor.ui.focusTracker.isFocused = true;
  100. } );
  101. it( 'should show the toolbar on ui#update when the image is selected', () => {
  102. setData( model, '<paragraph>[foo]</paragraph><image src=""></image>' );
  103. expect( balloon.visibleView ).to.be.null;
  104. editor.ui.fire( 'update' );
  105. expect( balloon.visibleView ).to.be.null;
  106. model.change( writer => {
  107. // Select the [<image></image>]
  108. writer.setSelection(
  109. writer.createRangeOn( doc.getRoot().getChild( 1 ) )
  110. );
  111. } );
  112. expect( balloon.visibleView ).to.equal( toolbar );
  113. // Make sure successive change does not throw, e.g. attempting
  114. // to insert the toolbar twice.
  115. editor.ui.fire( 'update' );
  116. expect( balloon.visibleView ).to.equal( toolbar );
  117. } );
  118. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  119. setData( model, '[<image src=""></image>]' );
  120. expect( balloon.visibleView ).to.equal( 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 ui#update if the image is de–selected', () => {
  134. setData( model, '<paragraph>foo</paragraph>[<image src=""></image>]' );
  135. expect( balloon.visibleView ).to.equal( toolbar );
  136. model.change( writer => {
  137. // Select the <paragraph>[...]</paragraph>
  138. writer.setSelection(
  139. writer.createRangeIn( doc.getRoot().getChild( 0 ) )
  140. );
  141. } );
  142. expect( balloon.visibleView ).to.be.null;
  143. // Make sure successive change does not throw, e.g. attempting
  144. // to remove the toolbar twice.
  145. editor.ui.fire( 'update' );
  146. expect( balloon.visibleView ).to.be.null;
  147. } );
  148. } );
  149. // Plugin that adds fake_button to editor's component factory.
  150. class FakeButton extends Plugin {
  151. init() {
  152. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  153. const view = new ButtonView( locale );
  154. view.set( {
  155. label: 'fake button'
  156. } );
  157. return view;
  158. } );
  159. }
  160. }
  161. } );