-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
utils.ts
60 lines (52 loc) · 2.08 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module image/image/ui/utils
*/
import type { PositionOptions } from 'ckeditor5/src/utils.js';
import type { Editor } from 'ckeditor5/src/core.js';
import { BalloonPanelView, type ContextualBalloon } from 'ckeditor5/src/ui.js';
import type ImageUtils from '../../imageutils.js';
/**
* A helper utility that positions the
* {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon contextual balloon} instance
* with respect to the image in the editor content, if one is selected.
*
* @param editor The editor instance.
*/
export function repositionContextualBalloon( editor: Editor ): void {
const balloon: ContextualBalloon = editor.plugins.get( 'ContextualBalloon' );
const imageUtils: ImageUtils = editor.plugins.get( 'ImageUtils' );
if ( imageUtils.getClosestSelectedImageWidget( editor.editing.view.document.selection ) ) {
const position = getBalloonPositionData( editor );
balloon.updatePosition( position );
}
}
/**
* Returns the positioning options that control the geometry of the
* {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon contextual balloon} with respect
* to the selected element in the editor content.
*
* @param editor The editor instance.
*/
export function getBalloonPositionData( editor: Editor ): Partial<PositionOptions> {
const editingView = editor.editing.view;
const defaultPositions = BalloonPanelView.defaultPositions;
const imageUtils: ImageUtils = editor.plugins.get( 'ImageUtils' );
return {
target: editingView.domConverter.mapViewToDom(
imageUtils.getClosestSelectedImageWidget( editingView.document.selection )!
) as HTMLElement,
positions: [
defaultPositions.northArrowSouth,
defaultPositions.northArrowSouthWest,
defaultPositions.northArrowSouthEast,
defaultPositions.southArrowNorth,
defaultPositions.southArrowNorthWest,
defaultPositions.southArrowNorthEast,
defaultPositions.viewportStickyNorth
]
};
}