imageballoonpanelview.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/imageballoonpanelview';
  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 after init', () => {
  32. const spy = sinon.spy( editor.ui.focusTracker, 'add' );
  33. const panel = new ImageBalloonPanel( editor );
  34. sinon.assert.notCalled( spy );
  35. panel.init();
  36. sinon.assert.calledOnce( spy );
  37. sinon.assert.calledWithExactly( spy, panel.element );
  38. } );
  39. it( 'should detach panel when focus is removed', () => {
  40. const spy = sinon.spy( panel, 'detach' );
  41. editor.ui.focusTracker.isFocused = true;
  42. editor.ui.focusTracker.isFocused = false;
  43. sinon.assert.calledOnce( spy );
  44. } );
  45. it( 'should detach when image element is no longer selected', () => {
  46. const spy = sinon.spy( panel, 'detach' );
  47. setData( document, '[<image src=""></image>]' );
  48. panel.attach();
  49. document.enqueueChanges( () => {
  50. document.selection.removeAllRanges();
  51. } );
  52. sinon.assert.calledOnce( spy );
  53. } );
  54. it( 'should attach panel correctly', () => {
  55. const spy = sinon.spy( panel, 'attachTo' );
  56. panel.attach();
  57. testPanelAttach( spy );
  58. } );
  59. it( 'should calculate panel position on scroll event', () => {
  60. panel.attach();
  61. const spy = sinon.spy( panel, 'attachTo' );
  62. global.window.dispatchEvent( new Event( 'scroll' ) );
  63. testPanelAttach( spy );
  64. } );
  65. it( 'should calculate panel position on resize event event', () => {
  66. panel.attach();
  67. const spy = sinon.spy( panel, 'attachTo' );
  68. global.window.dispatchEvent( new Event( 'resize' ) );
  69. testPanelAttach( spy );
  70. } );
  71. it( 'should hide panel on detach', () => {
  72. panel.attach();
  73. const spy = sinon.spy( panel, 'hide' );
  74. panel.detach();
  75. sinon.assert.calledOnce( spy );
  76. } );
  77. it( 'should not reposition panel after detaching', () => {
  78. panel.attach();
  79. const spy = sinon.spy( panel, 'attachTo' );
  80. panel.detach();
  81. global.window.dispatchEvent( new Event( 'resize' ) );
  82. global.window.dispatchEvent( new Event( 'scroll' ) );
  83. sinon.assert.notCalled( spy );
  84. } );
  85. // Tests if panel.attachTo() was called correctly.
  86. function testPanelAttach( spy ) {
  87. const domRange = editor.editing.view.domConverter.viewRangeToDom( editingView.selection.getFirstRange() );
  88. sinon.assert.calledOnce( spy );
  89. const options = spy.firstCall.args[ 0 ];
  90. // Check if proper range was used.
  91. expect( options.target.startContainer ).to.equal( domRange.startContainer );
  92. expect( options.target.startOffset ).to.equal( domRange.startOffset );
  93. expect( options.target.endContainer ).to.equal( domRange.endContainer );
  94. expect( options.target.endOffset ).to.equal( domRange.endOffset );
  95. // Check if north/south calculation is correct.
  96. const [ north, south ] = options.positions;
  97. const targetRect = { top: 10, left: 20, width: 200, height: 100, bottom: 110, right: 220 };
  98. const balloonRect = { width: 50, height: 20 };
  99. const northPosition = north( targetRect, balloonRect );
  100. expect( northPosition.name ).to.equal( 'n' );
  101. expect( northPosition.top ).to.equal( targetRect.top - balloonRect.height - BalloonPanelView.arrowVerticalOffset );
  102. expect( northPosition.left ).to.equal( targetRect.left + targetRect.width / 2 - balloonRect.width / 2 );
  103. const southPosition = south( targetRect, balloonRect );
  104. expect( southPosition.name ).to.equal( 's' );
  105. expect( southPosition.top ).to.equal( targetRect.bottom + BalloonPanelView.arrowVerticalOffset );
  106. expect( southPosition.left ).to.equal( targetRect.left + targetRect.width / 2 - balloonRect.width / 2 );
  107. }
  108. } );