utils.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module table/utils
  7. */
  8. import { toWidget, isWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  9. import { getParentTable } from './commands/utils';
  10. const tableSymbol = Symbol( 'isTable' );
  11. /**
  12. * Converts a given {@link module:engine/view/element~Element} to a table widget:
  13. * * Adds a {@link module:engine/view/element~Element#_setCustomProperty custom property} allowing to recognize the table widget element.
  14. * * Calls the {@link module:widget/utils~toWidget} function with the proper element's label creator.
  15. *
  16. * @param {module:engine/view/element~Element} viewElement
  17. * @param {module:engine/view/downcastwriter~DowncastWriter} writer An instance of the view writer.
  18. * @param {String} label The element's label. It will be concatenated with the table `alt` attribute if one is present.
  19. * @returns {module:engine/view/element~Element}
  20. */
  21. export function toTableWidget( viewElement, writer ) {
  22. writer.setCustomProperty( tableSymbol, true, viewElement );
  23. return toWidget( viewElement, writer, { hasSelectionHandler: true } );
  24. }
  25. /**
  26. * Checks if a given view element is a table widget.
  27. *
  28. * @param {module:engine/view/element~Element} viewElement
  29. * @returns {Boolean}
  30. */
  31. export function isTableWidget( viewElement ) {
  32. return !!viewElement.getCustomProperty( tableSymbol ) && isWidget( viewElement );
  33. }
  34. /**
  35. * Checks if a table widget is the only selected element.
  36. *
  37. * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
  38. * @returns {Boolean}
  39. */
  40. export function isTableWidgetSelected( selection ) {
  41. const viewElement = selection.getSelectedElement();
  42. return !!( viewElement && isTableWidget( viewElement ) );
  43. }
  44. /**
  45. * Checks if a table widget content is selected.
  46. *
  47. * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
  48. * @returns {Boolean}
  49. */
  50. export function isTableContentSelected( selection ) {
  51. const parentTable = getParentTable( selection.getFirstPosition() );
  52. return !!( parentTable && isTableWidget( parentTable.parent ) );
  53. }