imageballoonpanel.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 '@ckeditor/ckeditor5-editor-classic/src/classic';
  7. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  8. import ImageBalloonPanel from '../../src/ui/imageballoonpanel';
  9. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/balloonpanel/balloonpanelview';
  10. import ImageEngine from '../../src/imageengine';
  11. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. describe( 'ImageBalloonPanel', () => {
  13. let editor, panel, document, editingView;
  14. beforeEach( () => {
  15. const editorElement = global.document.createElement( 'div' );
  16. global.document.body.appendChild( editorElement );
  17. return ClassicEditor.create( editorElement, {
  18. plugins: [ ImageEngine ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. panel = new ImageBalloonPanel( editor );
  23. document = editor.document;
  24. editingView = editor.editing.view;
  25. return editor.ui.view.body.add( panel );
  26. } );
  27. } );
  28. afterEach( () => {
  29. return editor.destroy();
  30. } );
  31. it( 'should add element to editor\'s focus tracker', () => {
  32. const spy = sinon.spy( editor.ui.focusTracker, 'add' );
  33. const panel = new ImageBalloonPanel( editor );
  34. sinon.assert.calledOnce( spy );
  35. sinon.assert.calledWithExactly( spy, panel.element );
  36. } );
  37. it( 'should detach panel when focus is removed', () => {
  38. const spy = sinon.spy( panel, 'detach' );
  39. editor.ui.focusTracker.isFocused = true;
  40. editor.ui.focusTracker.isFocused = false;
  41. sinon.assert.calledOnce( spy );
  42. } );
  43. it( 'should detach when image element is no longer selected', () => {
  44. const spy = sinon.spy( panel, 'detach' );
  45. setData( document, '[<image src=""></image>]' );
  46. panel.attach();
  47. document.enqueueChanges( () => {
  48. document.selection.removeAllRanges();
  49. } );
  50. sinon.assert.calledOnce( spy );
  51. } );
  52. it( 'should attach panel correctly', () => {
  53. const spy = sinon.spy( panel, 'attachTo' );
  54. panel.attach();
  55. testPanelAttach( spy );
  56. } );
  57. it( 'should calculate panel position on scroll event', () => {
  58. panel.attach();
  59. const spy = sinon.spy( panel, 'attachTo' );
  60. global.window.dispatchEvent( new Event( 'scroll' ) );
  61. testPanelAttach( spy );
  62. } );
  63. it( 'should calculate panel position on resize event event', () => {
  64. panel.attach();
  65. const spy = sinon.spy( panel, 'attachTo' );
  66. global.window.dispatchEvent( new Event( 'resize' ) );
  67. testPanelAttach( spy );
  68. } );
  69. it( 'should hide panel on detach', () => {
  70. panel.attach();
  71. const spy = sinon.spy( panel, 'hide' );
  72. panel.detach();
  73. sinon.assert.calledOnce( spy );
  74. } );
  75. it( 'should not reposition panel after detaching', () => {
  76. panel.attach();
  77. const spy = sinon.spy( panel, 'attachTo' );
  78. panel.detach();
  79. global.window.dispatchEvent( new Event( 'resize' ) );
  80. global.window.dispatchEvent( new Event( 'scroll' ) );
  81. sinon.assert.notCalled( spy );
  82. } );
  83. // Tests if panel.attachTo() was called correctly.
  84. function testPanelAttach( spy ) {
  85. const domRange = editor.editing.view.domConverter.viewRangeToDom( editingView.selection.getFirstRange() );
  86. sinon.assert.calledOnce( spy );
  87. const options = spy.firstCall.args[ 0 ];
  88. // Check if proper range was used.
  89. expect( options.target.startContainer ).to.equal( domRange.startContainer );
  90. expect( options.target.startOffset ).to.equal( domRange.startOffset );
  91. expect( options.target.endContainer ).to.equal( domRange.endContainer );
  92. expect( options.target.endOffset ).to.equal( domRange.endOffset );
  93. // Check if north/south calculation is correct.
  94. const [ north, south ] = options.positions;
  95. const targetRect = { top: 10, left: 20, width: 200, height: 100, bottom: 110, right: 220 };
  96. const balloonRect = { width: 50, height: 20 };
  97. const northPosition = north( targetRect, balloonRect );
  98. expect( northPosition.name ).to.equal( 'n' );
  99. expect( northPosition.top ).to.equal( targetRect.top - balloonRect.height - BalloonPanelView.arrowVerticalOffset );
  100. expect( northPosition.left ).to.equal( targetRect.left + targetRect.width / 2 - balloonRect.width / 2 );
  101. const southPosition = south( targetRect, balloonRect );
  102. expect( southPosition.name ).to.equal( 's' );
  103. expect( southPosition.top ).to.equal( targetRect.bottom + BalloonPanelView.arrowVerticalOffset );
  104. expect( southPosition.left ).to.equal( targetRect.left + targetRect.width / 2 - balloonRect.width / 2 );
  105. }
  106. } );