8
0

imagetoolbar.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global Event */
  6. import ClassicEditor from 'ckeditor5-editor-classic/src/classic';
  7. import ImageToolbar from 'ckeditor5-image/src/imagetoolbar';
  8. import Image from 'ckeditor5-image/src/image';
  9. import global from 'ckeditor5-utils/src/dom/global';
  10. import BalloonPanelView from 'ckeditor5-ui/src/balloonpanel/balloonpanelview';
  11. import Plugin from 'ckeditor5-core/src/plugin';
  12. import ButtonView from 'ckeditor5-ui/src/button/buttonview';
  13. import { setData } from 'ckeditor5-engine/src/dev-utils/model';
  14. describe( 'ImageToolbar', () => {
  15. let editor, button, editingView, doc, panel;
  16. beforeEach( () => {
  17. const editorElement = global.document.createElement( 'div' );
  18. global.document.body.appendChild( editorElement );
  19. return ClassicEditor.create( editorElement, {
  20. plugins: [ Image, ImageToolbar, FakeButton ],
  21. image: {
  22. toolbar: [ 'fake_button' ]
  23. }
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. editingView = editor.editing.view;
  28. doc = editor.document;
  29. panel = getBalloonPanelView( editor.ui.view.body );
  30. } );
  31. } );
  32. afterEach( () => {
  33. return editor.destroy();
  34. } );
  35. it( 'should be loaded', () => {
  36. expect( editor.plugins.get( ImageToolbar ) ).to.be.instanceOf( ImageToolbar );
  37. } );
  38. it( 'should initialize image.defaultToolbar to an empty array', () => {
  39. expect( editor.config.get( 'image.defaultToolbar' ) ).to.eql( [] );
  40. } );
  41. it( 'should not initialize if there is no configuration', () => {
  42. const editorElement = global.document.createElement( 'div' );
  43. global.document.body.appendChild( editorElement );
  44. return ClassicEditor.create( editorElement, {
  45. plugins: [ ImageToolbar ],
  46. } )
  47. .then( newEditor => {
  48. const viewBody = newEditor.ui.view.body;
  49. expect( getBalloonPanelView( viewBody ) ).to.be.undefined;
  50. newEditor.destroy();
  51. } );
  52. } );
  53. it( 'should allow other plugins to alter default config', () => {
  54. const editorElement = global.document.createElement( 'div' );
  55. global.document.body.appendChild( editorElement );
  56. return ClassicEditor.create( editorElement, {
  57. plugins: [ ImageToolbar, FakeButton, AlterDefaultConfig ]
  58. } )
  59. .then( newEditor => {
  60. const panel = getBalloonPanelView( newEditor.ui.view.body );
  61. const toolbar = panel.content.get( 0 );
  62. const button = toolbar.items.get( 0 );
  63. expect( newEditor.config.get( 'image.defaultToolbar' ) ).to.eql( [ 'fake_button' ] );
  64. expect( button.label ).to.equal( 'fake button' );
  65. newEditor.destroy();
  66. } );
  67. } );
  68. it( 'should add BalloonPanelView to view body', () => {
  69. expect( panel ).to.be.instanceOf( BalloonPanelView );
  70. } );
  71. it( 'should attach toolbar when image is selected', () => {
  72. const spy = sinon.spy( panel, 'attachTo' );
  73. setData( doc, '[<image src=""></image>]' );
  74. testPanelAttach( spy );
  75. } );
  76. it( 'should calculate panel position on scroll event', () => {
  77. setData( doc, '[<image src=""></image>]' );
  78. const spy = sinon.spy( panel, 'attachTo' );
  79. global.window.dispatchEvent( new Event( 'scroll' ) );
  80. testPanelAttach( spy );
  81. } );
  82. it( 'should calculate panel position on resize event', () => {
  83. setData( doc, '[<image src=""></image>]' );
  84. const spy = sinon.spy( panel, 'attachTo' );
  85. global.window.dispatchEvent( new Event( 'resize' ) );
  86. testPanelAttach( spy );
  87. } );
  88. it( 'should not calculate panel position on scroll if no image is selected', () => {
  89. setData( doc, '<image src=""></image>' );
  90. const spy = sinon.spy( panel, 'attachTo' );
  91. global.window.dispatchEvent( new Event( 'scroll' ) );
  92. sinon.assert.notCalled( spy );
  93. } );
  94. it( 'should not calculate panel position on resize if no image is selected', () => {
  95. setData( doc, '<image src=""></image>' );
  96. const spy = sinon.spy( panel, 'attachTo' );
  97. global.window.dispatchEvent( new Event( 'resize' ) );
  98. sinon.assert.notCalled( spy );
  99. } );
  100. it( 'should hide the panel when editor looses focus', () => {
  101. setData( doc, '[<image src=""></image>]' );
  102. editor.ui.focusTracker.isFocused = true;
  103. const spy = sinon.spy( panel, 'hide' );
  104. editor.ui.focusTracker.isFocused = false;
  105. sinon.assert.calledOnce( spy );
  106. } );
  107. // Returns BalloonPanelView from provided collection.
  108. function getBalloonPanelView( viewCollection ) {
  109. return viewCollection.find( item => item instanceof BalloonPanelView );
  110. }
  111. // Tests if panel.attachTo() was called correctly.
  112. function testPanelAttach( spy ) {
  113. const domRange = editor.editing.view.domConverter.viewRangeToDom( editingView.selection.getFirstRange() );
  114. sinon.assert.calledOnce( spy );
  115. const options = spy.firstCall.args[ 0 ];
  116. // Check if proper range was used.
  117. expect( options.target.startContainer ).to.equal( domRange.startContainer );
  118. expect( options.target.startOffset ).to.equal( domRange.startOffset );
  119. expect( options.target.endContainer ).to.equal( domRange.endContainer );
  120. expect( options.target.endOffset ).to.equal( domRange.endOffset );
  121. // Check if north/south calculation is correct.
  122. const [ north, south ] = options.positions;
  123. const targetRect = { top: 10, left: 20, width: 200, height: 100, bottom: 110, right: 220 };
  124. const balloonRect = { width: 50, height: 20 };
  125. const northPosition = north( targetRect, balloonRect );
  126. expect( northPosition.name ).to.equal( 'n' );
  127. expect( northPosition.top ).to.equal( targetRect.top - balloonRect.height - BalloonPanelView.arrowVerticalOffset );
  128. expect( northPosition.left ).to.equal( targetRect.left + targetRect.width / 2 - balloonRect.width / 2 );
  129. const southPosition = south( targetRect, balloonRect );
  130. expect( southPosition.name ).to.equal( 's' );
  131. expect( southPosition.top ).to.equal( targetRect.bottom + BalloonPanelView.arrowVerticalOffset );
  132. expect( southPosition.left ).to.equal( targetRect.left + targetRect.width / 2 - balloonRect.width / 2 );
  133. }
  134. // Plugin that adds fake_button to editor's component factory.
  135. class FakeButton extends Plugin {
  136. init() {
  137. this.editor.ui.componentFactory.add( 'fake_button', ( locale ) => {
  138. const view = new ButtonView( locale );
  139. view.set( {
  140. label: 'fake button'
  141. } );
  142. button = view;
  143. return view;
  144. } );
  145. }
  146. }
  147. class AlterDefaultConfig extends Plugin {
  148. init() {
  149. const defaultImageToolbarConfig = this.editor.config.get( 'image.defaultToolbar' );
  150. if ( defaultImageToolbarConfig ) {
  151. defaultImageToolbarConfig.push( 'fake_button' );
  152. }
  153. }
  154. }
  155. } );