8
0

imagetoolbar.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  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 env from '@ckeditor/ckeditor5-utils/src/env';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. describe( 'ImageToolbar', () => {
  18. let editor, model, doc, toolbar, balloon, widgetToolbarRepository, editorElement;
  19. testUtils.createSinonSandbox();
  20. beforeEach( () => {
  21. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  22. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  23. editorElement = global.document.createElement( 'div' );
  24. global.document.body.appendChild( editorElement );
  25. return ClassicEditor
  26. .create( editorElement, {
  27. plugins: [ Paragraph, Image, ImageToolbar, FakeButton ],
  28. image: {
  29. toolbar: [ 'fake_button' ]
  30. }
  31. } )
  32. .then( newEditor => {
  33. editor = newEditor;
  34. model = newEditor.model;
  35. doc = model.document;
  36. widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
  37. toolbar = widgetToolbarRepository._toolbarDefinitions.get( 'image' ).view;
  38. balloon = editor.plugins.get( 'ContextualBalloon' );
  39. } );
  40. } );
  41. afterEach( () => {
  42. editorElement.remove();
  43. return editor.destroy();
  44. } );
  45. it( 'should be loaded', () => {
  46. expect( editor.plugins.get( ImageToolbar ) ).to.be.instanceOf( ImageToolbar );
  47. } );
  48. it( 'should not initialize if there is no configuration', () => {
  49. const editorElement = global.document.createElement( 'div' );
  50. global.document.body.appendChild( editorElement );
  51. return ClassicEditor.create( editorElement, {
  52. plugins: [ ImageToolbar ],
  53. } )
  54. .then( editor => {
  55. expect( editor.plugins.get( ImageToolbar )._toolbar ).to.be.undefined;
  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, {
  70. view: toolbar,
  71. balloonClassName: 'ck-toolbar-container'
  72. } );
  73. } );
  74. } );
  75. describe( 'integration with the editor focus', () => {
  76. it( 'should show the toolbar when the editor gains focus and the image is selected', () => {
  77. editor.ui.focusTracker.isFocused = true;
  78. setData( model, '[<image src=""></image>]' );
  79. editor.ui.focusTracker.isFocused = false;
  80. expect( balloon.visibleView ).to.be.null;
  81. editor.ui.focusTracker.isFocused = true;
  82. expect( balloon.visibleView ).to.equal( toolbar );
  83. } );
  84. it( 'should hide the toolbar when the editor loses focus and the image is selected', () => {
  85. editor.ui.focusTracker.isFocused = false;
  86. setData( model, '[<image src=""></image>]' );
  87. editor.ui.focusTracker.isFocused = true;
  88. expect( balloon.visibleView ).to.equal( toolbar );
  89. editor.ui.focusTracker.isFocused = false;
  90. expect( balloon.visibleView ).to.be.null;
  91. } );
  92. } );
  93. describe( 'integration with the editor selection', () => {
  94. beforeEach( () => {
  95. editor.ui.focusTracker.isFocused = true;
  96. } );
  97. it( 'should show the toolbar on ui#update when the image is selected', () => {
  98. setData( model, '<paragraph>[foo]</paragraph><image src=""></image>' );
  99. expect( balloon.visibleView ).to.be.null;
  100. editor.ui.fire( 'update' );
  101. expect( balloon.visibleView ).to.be.null;
  102. model.change( writer => {
  103. // Select the [<image></image>]
  104. writer.setSelection(
  105. writer.createRangeOn( doc.getRoot().getChild( 1 ) )
  106. );
  107. } );
  108. expect( balloon.visibleView ).to.equal( toolbar );
  109. // Make sure successive change does not throw, e.g. attempting
  110. // to insert the toolbar twice.
  111. editor.ui.fire( 'update' );
  112. expect( balloon.visibleView ).to.equal( toolbar );
  113. } );
  114. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  115. setData( model, '[<image src=""></image>]' );
  116. expect( balloon.visibleView ).to.equal( toolbar );
  117. const lastView = new View();
  118. lastView.element = document.createElement( 'div' );
  119. balloon.add( {
  120. view: lastView,
  121. position: {
  122. target: document.body
  123. }
  124. } );
  125. expect( balloon.visibleView ).to.equal( lastView );
  126. editor.ui.fire( 'update' );
  127. expect( balloon.visibleView ).to.equal( lastView );
  128. } );
  129. it( 'should hide the toolbar on ui#update if the image is de–selected', () => {
  130. setData( model, '<paragraph>foo</paragraph>[<image src=""></image>]' );
  131. expect( balloon.visibleView ).to.equal( toolbar );
  132. model.change( writer => {
  133. // Select the <paragraph>[...]</paragraph>
  134. writer.setSelection(
  135. writer.createRangeIn( doc.getRoot().getChild( 0 ) )
  136. );
  137. } );
  138. expect( balloon.visibleView ).to.be.null;
  139. // Make sure successive change does not throw, e.g. attempting
  140. // to remove the toolbar twice.
  141. editor.ui.fire( 'update' );
  142. expect( balloon.visibleView ).to.be.null;
  143. } );
  144. } );
  145. // Plugin that adds fake_button to editor's component factory.
  146. class FakeButton extends Plugin {
  147. init() {
  148. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  149. const view = new ButtonView( locale );
  150. view.set( {
  151. label: 'fake button'
  152. } );
  153. return view;
  154. } );
  155. }
  156. }
  157. } );