-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
widget.ts
68 lines (52 loc) · 1.79 KB
/
widget.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
61
62
63
64
65
66
67
68
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module table/utils/ui/widget
*/
import type { ViewDocumentFragment, ViewDocumentSelection, ViewElement, ViewNode } from 'ckeditor5/src/engine';
import { isWidget } from 'ckeditor5/src/widget';
/**
* Depending on the position of the selection either return the selected table or the table higher in the hierarchy.
*/
export function getSelectionAffectedTableWidget( selection: ViewDocumentSelection ): ViewElement | null {
const selectedTable = getSelectedTableWidget( selection );
if ( selectedTable ) {
return selectedTable;
}
return getTableWidgetAncestor( selection );
}
/**
* Returns a table widget editing view element if one is selected.
*/
export function getSelectedTableWidget( selection: ViewDocumentSelection ): ViewElement | null {
const viewElement = selection.getSelectedElement();
if ( viewElement && isTableWidget( viewElement ) ) {
return viewElement;
}
return null;
}
/**
* Returns a table widget editing view element if one is among the selection's ancestors.
*/
export function getTableWidgetAncestor( selection: ViewDocumentSelection ): ViewElement | null {
const selectionPosition = selection.getFirstPosition();
if ( !selectionPosition ) {
return null;
}
let parent: ViewNode | ViewDocumentFragment | null = selectionPosition.parent;
while ( parent ) {
if ( parent.is( 'element' ) && isTableWidget( parent ) ) {
return parent;
}
parent = parent.parent;
}
return null;
}
/**
* Checks if a given view element is a table widget.
*/
function isTableWidget( viewElement: ViewElement ): boolean {
return !!viewElement.getCustomProperty( 'table' ) && isWidget( viewElement );
}