imagetoolbar.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @license Copyright (c) 2003-2016, 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 not initialize if there is no configuration', () => {
  39. const editorElement = global.document.createElement( 'div' );
  40. global.document.body.appendChild( editorElement );
  41. return ClassicEditor.create( editorElement, {
  42. plugins: [ ImageToolbar ],
  43. } )
  44. .then( newEditor => {
  45. const viewBody = newEditor.ui.view.body;
  46. expect( getBalloonPanelView( viewBody ) ).to.be.undefined;
  47. newEditor.destroy();
  48. } );
  49. } );
  50. it( 'should add BalloonPanelView to view body', () => {
  51. expect( panel ).to.be.instanceOf( BalloonPanelView );
  52. } );
  53. it( 'should attach toolbar when image is selected', () => {
  54. const spy = sinon.spy( panel, 'attachTo' );
  55. setData( doc, '[<image src=""></image>]' );
  56. testPanelAttach( spy );
  57. } );
  58. it( 'should calculate panel position on scroll event', () => {
  59. setData( doc, '[<image src=""></image>]' );
  60. const spy = sinon.spy( panel, 'attachTo' );
  61. global.window.dispatchEvent( new Event( 'scroll' ) );
  62. testPanelAttach( spy );
  63. } );
  64. it( 'should calculate panel position on resize event', () => {
  65. setData( doc, '[<image src=""></image>]' );
  66. const spy = sinon.spy( panel, 'attachTo' );
  67. global.window.dispatchEvent( new Event( 'resize' ) );
  68. testPanelAttach( spy );
  69. } );
  70. it( 'should not calculate panel position on scroll if no image is selected', () => {
  71. setData( doc, '<image src=""></image>' );
  72. const spy = sinon.spy( panel, 'attachTo' );
  73. global.window.dispatchEvent( new Event( 'scroll' ) );
  74. sinon.assert.notCalled( spy );
  75. } );
  76. it( 'should not calculate panel position on resize if no image is selected', () => {
  77. setData( doc, '<image src=""></image>' );
  78. const spy = sinon.spy( panel, 'attachTo' );
  79. global.window.dispatchEvent( new Event( 'resize' ) );
  80. sinon.assert.notCalled( spy );
  81. } );
  82. it( 'should hide the panel when editor looses focus', () => {
  83. setData( doc, '[<image src=""></image>]' );
  84. editor.ui.focusTracker.isFocused = true;
  85. const spy = sinon.spy( panel, 'hide' );
  86. editor.ui.focusTracker.isFocused = false;
  87. sinon.assert.calledOnce( spy );
  88. } );
  89. // Returns BalloonPanelView from provided collection.
  90. function getBalloonPanelView( viewCollection ) {
  91. return viewCollection.find( item => item instanceof BalloonPanelView );
  92. }
  93. // Tests if panel.attachTo() was called correctly.
  94. function testPanelAttach( spy ) {
  95. const domRange = editor.editing.view.domConverter.viewRangeToDom( editingView.selection.getFirstRange() );
  96. sinon.assert.calledOnce( spy );
  97. const options = spy.firstCall.args[ 0 ];
  98. // Check if proper range was used.
  99. expect( options.target.startContainer ).to.equal( domRange.startContainer );
  100. expect( options.target.startOffset ).to.equal( domRange.startOffset );
  101. expect( options.target.endContainer ).to.equal( domRange.endContainer );
  102. expect( options.target.endOffset ).to.equal( domRange.endOffset );
  103. // Check if north/south calculation is correct.
  104. const [ north, south ] = options.positions;
  105. const targetRect = { top: 10, left: 20, width: 200, height: 100, bottom: 110, right: 220 };
  106. const balloonRect = { width: 50, height: 20 };
  107. const northPosition = north( targetRect, balloonRect );
  108. expect( northPosition.name ).to.equal( 'n' );
  109. expect( northPosition.top ).to.equal( targetRect.top - balloonRect.height - BalloonPanelView.arrowVerticalOffset );
  110. expect( northPosition.left ).to.equal( targetRect.left + targetRect.width / 2 - balloonRect.width / 2 );
  111. const southPosition = south( targetRect, balloonRect );
  112. expect( southPosition.name ).to.equal( 's' );
  113. expect( southPosition.top ).to.equal( targetRect.bottom + BalloonPanelView.arrowVerticalOffset );
  114. expect( southPosition.left ).to.equal( targetRect.left + targetRect.width / 2 - balloonRect.width / 2 );
  115. }
  116. // Plugin that adds fake_button to editor's component factory.
  117. class FakeButton extends Plugin {
  118. init() {
  119. this.editor.ui.componentFactory.add( 'fake_button', ( locale ) => {
  120. const view = new ButtonView( locale );
  121. view.set( {
  122. label: 'fake button'
  123. } );
  124. button = view;
  125. return view;
  126. } );
  127. }
  128. }
  129. } );