8
0

widget.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. /**
  10. * Returns a table widget editing view element if one is selected.
  11. *
  12. * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
  13. * @returns {module:engine/view/element~Element|null}
  14. */
  15. export function getSelectedTableWidget( selection ) {
  16. const viewElement = selection.getSelectedElement();
  17. if ( viewElement && isTableWidget( viewElement ) ) {
  18. return viewElement;
  19. }
  20. return null;
  21. }
  22. /**
  23. * Returns a table widget editing view element if one is among the selection's ancestors.
  24. *
  25. * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
  26. * @returns {module:engine/view/element~Element|null}
  27. */
  28. export function getTableWidgetAncestor( selection ) {
  29. const parentTable = findAncestor( 'table', selection.getFirstPosition() );
  30. if ( parentTable && isTableWidget( parentTable.parent ) ) {
  31. return parentTable.parent;
  32. }
  33. return null;
  34. }
  35. // Checks if a given view element is a table widget.
  36. //
  37. // @param {module:engine/view/element~Element} viewElement
  38. // @returns {Boolean}
  39. function isTableWidget( viewElement ) {
  40. return !!viewElement.getCustomProperty( 'table' ) && isWidget( viewElement );
  41. }
  42. function findAncestor( parentName, positionOrElement ) {
  43. let parent = positionOrElement.parent;
  44. while ( parent ) {
  45. if ( parent.name === parentName ) {
  46. return parent;
  47. }
  48. parent = parent.parent;
  49. }
  50. }