widget.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module table/utils/ui/widget
  7. */
  8. import { isWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  9. import { findAncestor } from '../common';
  10. /**
  11. * Returns a table widget editing view element if one is selected.
  12. *
  13. * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
  14. * @returns {module:engine/view/element~Element|null}
  15. */
  16. export function getSelectedTableWidget( selection ) {
  17. const viewElement = selection.getSelectedElement();
  18. if ( viewElement && isTableWidget( viewElement ) ) {
  19. return viewElement;
  20. }
  21. return null;
  22. }
  23. /**
  24. * Returns a table widget editing view element if one is among the selection's ancestors.
  25. *
  26. * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
  27. * @returns {module:engine/view/element~Element|null}
  28. */
  29. export function getTableWidgetAncestor( selection ) {
  30. const parentTable = findAncestor( 'table', selection.getFirstPosition() );
  31. if ( parentTable && isTableWidget( parentTable.parent ) ) {
  32. return parentTable.parent;
  33. }
  34. return null;
  35. }
  36. // Checks if a given view element is a table widget.
  37. //
  38. // @param {module:engine/view/element~Element} viewElement
  39. // @returns {Boolean}
  40. function isTableWidget( viewElement ) {
  41. return !!viewElement.getCustomProperty( 'table' ) && isWidget( viewElement );
  42. }