8
0

imagetoolbar.js 5.7 KB

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