UNPKG

1.18 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2023, 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 { isWidget } from 'ckeditor5/src/widget';
6/**
7 * Returns a table widget editing view element if one is selected.
8 */
9export function getSelectedTableWidget(selection) {
10 const viewElement = selection.getSelectedElement();
11 if (viewElement && isTableWidget(viewElement)) {
12 return viewElement;
13 }
14 return null;
15}
16/**
17 * Returns a table widget editing view element if one is among the selection's ancestors.
18 */
19export function getTableWidgetAncestor(selection) {
20 const selectionPosition = selection.getFirstPosition();
21 if (!selectionPosition) {
22 return null;
23 }
24 let parent = selectionPosition.parent;
25 while (parent) {
26 if (parent.is('element') && isTableWidget(parent)) {
27 return parent;
28 }
29 parent = parent.parent;
30 }
31 return null;
32}
33/**
34 * Checks if a given view element is a table widget.
35 */
36function isTableWidget(viewElement) {
37 return !!viewElement.getCustomProperty('table') && isWidget(viewElement);
38}