utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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, 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. 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( editor.model, '[<image src=""></image>]' );
  46. repositionContextualBalloon( editor );
  47. sinon.assert.calledWithExactly( spy, {
  48. target: editingView.domConverter.viewToDom( editingView.document.selection.getSelectedElement() ),
  49. positions: [
  50. defaultPositions.northArrowSouth,
  51. defaultPositions.northArrowSouthWest,
  52. defaultPositions.northArrowSouthEast,
  53. defaultPositions.southArrowNorth,
  54. defaultPositions.southArrowNorthWest,
  55. defaultPositions.southArrowNorthEast
  56. ]
  57. } );
  58. } );
  59. it( 'should not engage with no image is selected', () => {
  60. const spy = sinon.spy( balloon, 'updatePosition' );
  61. setData( editor.model, '<paragraph>foo</paragraph>' );
  62. repositionContextualBalloon( editor );
  63. sinon.assert.notCalled( spy );
  64. } );
  65. } );
  66. describe( 'getBalloonPositionData', () => {
  67. it( 'returns the position data', () => {
  68. const defaultPositions = BalloonPanelView.defaultPositions;
  69. setData( editor.model, '[<image src=""></image>]' );
  70. const data = getBalloonPositionData( editor );
  71. expect( data ).to.deep.equal( {
  72. target: editingView.domConverter.viewToDom( editingView.document.selection.getSelectedElement() ),
  73. positions: [
  74. defaultPositions.northArrowSouth,
  75. defaultPositions.northArrowSouthWest,
  76. defaultPositions.northArrowSouthEast,
  77. defaultPositions.southArrowNorth,
  78. defaultPositions.southArrowNorthWest,
  79. defaultPositions.southArrowNorthEast
  80. ]
  81. } );
  82. } );
  83. } );
  84. } );