8
0

imagetoolbar.js 5.8 KB

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