utils.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module image/image/ui/utils
  7. */
  8. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  9. import { isImageWidgetSelected } from '../utils';
  10. /**
  11. * A helper utility which positions the
  12. * {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon} instance
  13. * with respect to the image in the editor content, if one is selected.
  14. *
  15. * @param {module:core/editor/editor~Editor} editor The editor instance.
  16. */
  17. export function repositionContextualBalloon( editor ) {
  18. const balloon = editor.plugins.get( 'ContextualBalloon' );
  19. if ( isImageWidgetSelected( editor.editing.view.document.selection ) ) {
  20. const position = getBalloonPositionData( editor );
  21. balloon.updatePosition( position );
  22. }
  23. }
  24. /**
  25. * Returns the positioning options that control the geometry of the
  26. * {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon}, with respect
  27. * to the selected element in the editor content.
  28. *
  29. * @param {module:core/editor/editor~Editor} editor The editor instance.
  30. * @returns {module:utils/dom/position~Options}
  31. */
  32. export function getBalloonPositionData( editor ) {
  33. const editingView = editor.editing.view;
  34. const defaultPositions = BalloonPanelView.defaultPositions;
  35. return {
  36. target: editingView.domConverter.viewToDom( editingView.document.selection.getSelectedElement() ),
  37. positions: [
  38. defaultPositions.northArrowSouth,
  39. defaultPositions.northArrowSouthWest,
  40. defaultPositions.northArrowSouthEast,
  41. defaultPositions.southArrowNorth,
  42. defaultPositions.southArrowNorthWest,
  43. defaultPositions.southArrowNorthEast
  44. ]
  45. };
  46. }