UNPKG

1.87 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5import { BalloonPanelView } from 'ckeditor5/src/ui.js';
6/**
7 * A helper utility that positions the
8 * {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon contextual balloon} instance
9 * with respect to the image in the editor content, if one is selected.
10 *
11 * @param editor The editor instance.
12 */
13export function repositionContextualBalloon(editor) {
14 const balloon = editor.plugins.get('ContextualBalloon');
15 const imageUtils = editor.plugins.get('ImageUtils');
16 if (imageUtils.getClosestSelectedImageWidget(editor.editing.view.document.selection)) {
17 const position = getBalloonPositionData(editor);
18 balloon.updatePosition(position);
19 }
20}
21/**
22 * Returns the positioning options that control the geometry of the
23 * {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon contextual balloon} with respect
24 * to the selected element in the editor content.
25 *
26 * @param editor The editor instance.
27 */
28export function getBalloonPositionData(editor) {
29 const editingView = editor.editing.view;
30 const defaultPositions = BalloonPanelView.defaultPositions;
31 const imageUtils = editor.plugins.get('ImageUtils');
32 return {
33 target: editingView.domConverter.mapViewToDom(imageUtils.getClosestSelectedImageWidget(editingView.document.selection)),
34 positions: [
35 defaultPositions.northArrowSouth,
36 defaultPositions.northArrowSouthWest,
37 defaultPositions.northArrowSouthEast,
38 defaultPositions.southArrowNorth,
39 defaultPositions.southArrowNorthWest,
40 defaultPositions.southArrowNorthEast,
41 defaultPositions.viewportStickyNorth
42 ]
43 };
44}