utils.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module image/widget/utils
  7. */
  8. const widgetSymbol = Symbol( 'isWidget' );
  9. const fakeSelectionLabelSymbol = Symbol( 'fakeSelectionLabel' );
  10. /**
  11. * CSS class added to each widget element.
  12. *
  13. * @const {String}
  14. */
  15. export const WIDGET_CLASS_NAME = 'ck-widget';
  16. /**
  17. * CSS class added to currently selected widget element.
  18. *
  19. * @const {String}
  20. */
  21. export const WIDGET_SELECTED_CLASS_NAME = 'ck-widget_selected';
  22. /**
  23. * Returns `true` if given {@link module:engine/view/element~Element} is a widget.
  24. *
  25. * @param {module:engine/view/element~Element} element
  26. * @returns {Boolean}
  27. */
  28. export function isWidget( element ) {
  29. return !!element.getCustomProperty( widgetSymbol );
  30. }
  31. /**
  32. * "Widgetizes" given {@link module:engine/view/element~Element}:
  33. * * sets `contenteditable` attribute to `true`,
  34. * * adds custom `getFillerOffset` method returning `null`,
  35. * * adds `ck-widget` CSS class,
  36. * * adds custom property allowing to recognize widget elements by using {@link ~isWidget}.
  37. *
  38. * @param {module:engine/view/element~Element} element
  39. * @returns {module:engine/view/element~Element} Returns same element.
  40. */
  41. export function widgetize( element ) {
  42. element.setAttribute( 'contenteditable', false );
  43. element.getFillerOffset = getFillerOffset;
  44. element.addClass( WIDGET_CLASS_NAME );
  45. element.setCustomProperty( widgetSymbol, true );
  46. return element;
  47. }
  48. /**
  49. * Sets fake selection label for given element.
  50. * It can be passed as a plain string or a function returning a string. Function will be called each time label is retrieved by
  51. * {module:image/widget/utils~getFakeSelectionLabel}.
  52. *
  53. * @param {module:engine/view/element~Element} element
  54. * @param {String|Function} labelOrCreator
  55. */
  56. export function setFakeSelectionLabel( element, labelOrCreator ) {
  57. element.setCustomProperty( fakeSelectionLabelSymbol, labelOrCreator );
  58. }
  59. /**
  60. * Returns fake selection label for provided element.
  61. *
  62. * @param {module:engine/view/element~Element} element
  63. * @return {String|undefined}
  64. */
  65. export function getFakeSelectionLabel( element ) {
  66. const labelCreator = element.getCustomProperty( fakeSelectionLabelSymbol );
  67. if ( !labelCreator ) {
  68. return undefined;
  69. }
  70. return typeof labelCreator == 'function' ? labelCreator() : labelCreator;
  71. }
  72. // Default filler offset function applied to all widget elements.
  73. //
  74. // @returns {null}
  75. function getFillerOffset() {
  76. return null;
  77. }