utils.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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
  20. .create( editorElement, {
  21. plugins: [ Image, Paragraph, ContextualBalloon ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. doc = editor.document;
  26. editingView = editor.editing.view;
  27. balloon = editor.plugins.get( 'ContextualBalloon' );
  28. } );
  29. } );
  30. afterEach( () => {
  31. editorElement.remove();
  32. return editor.destroy();
  33. } );
  34. describe( 'repositionContextualBalloon', () => {
  35. it( 'should re-position the ContextualBalloon when the image is selected', () => {
  36. const spy = sinon.spy( balloon, 'updatePosition' );
  37. const defaultPositions = BalloonPanelView.defaultPositions;
  38. const view = new View();
  39. view.element = global.document.createElement( 'div' );
  40. balloon.add( {
  41. view,
  42. position: {
  43. target: global.document.body
  44. }
  45. } );
  46. setData( doc, '[<image src=""></image>]' );
  47. repositionContextualBalloon( editor );
  48. sinon.assert.calledWithExactly( spy, {
  49. target: editingView.domConverter.viewToDom( editingView.selection.getSelectedElement() ),
  50. positions: [
  51. defaultPositions.northArrowSouth,
  52. defaultPositions.southArrowNorth
  53. ]
  54. } );
  55. } );
  56. it( 'should not engage with no image is selected', () => {
  57. const spy = sinon.spy( balloon, 'updatePosition' );
  58. setData( doc, '<paragraph>foo</paragraph>' );
  59. repositionContextualBalloon( editor );
  60. sinon.assert.notCalled( spy );
  61. } );
  62. } );
  63. describe( 'getBalloonPositionData', () => {
  64. it( 'returns the position data', () => {
  65. const defaultPositions = BalloonPanelView.defaultPositions;
  66. setData( doc, '[<image src=""></image>]' );
  67. const data = getBalloonPositionData( editor );
  68. expect( data ).to.deep.equal( {
  69. target: editingView.domConverter.viewToDom( editingView.selection.getSelectedElement() ),
  70. positions: [
  71. defaultPositions.northArrowSouth,
  72. defaultPositions.southArrowNorth
  73. ]
  74. } );
  75. } );
  76. } );
  77. } );