utils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  6. import Image from '../../../src/image';
  7. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import View from '@ckeditor/ckeditor5-ui/src/view';
  10. import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
  11. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  12. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. import { repositionContextualBalloon, getBalloonPositionData } from '../../../src/image/ui/utils';
  14. describe( 'Utils', () => {
  15. let editor, doc, editingView, balloon, editorElement;
  16. beforeEach( () => {
  17. editorElement = global.document.createElement( 'div' );
  18. global.document.body.appendChild( editorElement );
  19. return ClassicEditor.create( editorElement, {
  20. plugins: [ Image, Paragraph, ContextualBalloon ]
  21. } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. doc = editor.document;
  25. editingView = editor.editing.view;
  26. balloon = editor.plugins.get( 'ContextualBalloon' );
  27. } );
  28. } );
  29. afterEach( () => {
  30. editorElement.remove();
  31. return editor.destroy();
  32. } );
  33. describe( 'repositionContextualBalloon', () => {
  34. it( 'should re-position the ContextualBalloon when the image is selected', () => {
  35. const spy = sinon.spy( balloon, 'updatePosition' );
  36. const defaultPositions = BalloonPanelView.defaultPositions;
  37. const view = new View();
  38. view.element = global.document.createElement( 'div' );
  39. balloon.add( {
  40. view,
  41. position: {
  42. target: global.document.body
  43. }
  44. } );
  45. setData( doc, '[<image src=""></image>]' );
  46. repositionContextualBalloon( editor );
  47. sinon.assert.calledWithExactly( spy, {
  48. target: editingView.domConverter.viewToDom( editingView.selection.getSelectedElement() ),
  49. positions: [
  50. defaultPositions.northArrowSouth,
  51. defaultPositions.southArrowNorth
  52. ]
  53. } );
  54. } );
  55. it( 'should not engage with no image is selected', () => {
  56. const spy = sinon.spy( balloon, 'updatePosition' );
  57. setData( doc, '<paragraph>foo</paragraph>' );
  58. repositionContextualBalloon( editor );
  59. sinon.assert.notCalled( spy );
  60. } );
  61. } );
  62. describe( 'getBalloonPositionData', () => {
  63. it( 'returns the position data', () => {
  64. const defaultPositions = BalloonPanelView.defaultPositions;
  65. setData( doc, '[<image src=""></image>]' );
  66. const data = getBalloonPositionData( editor );
  67. expect( data ).to.deep.equal( {
  68. target: editingView.domConverter.viewToDom( editingView.selection.getSelectedElement() ),
  69. positions: [
  70. defaultPositions.northArrowSouth,
  71. defaultPositions.southArrowNorth
  72. ]
  73. } );
  74. } );
  75. } );
  76. } );