imagetoolbar.js 5.8 KB

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