manualdecorator.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 link/utils
  7. */
  8. import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
  9. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  10. /**
  11. * Helper class that stores manual decorators with observable {@link module:link/utils~ManualDecorator#value}
  12. * to support integration with the UI state. An instance of this class is a model with the state of individual manual decorators.
  13. * These decorators are kept as collections in {@link module:link/linkcommand~LinkCommand#manualDecorators}.
  14. *
  15. * @mixes module:utils/observablemixin~ObservableMixin
  16. */
  17. export default class ManualDecorator {
  18. /**
  19. * Creates a new instance of {@link module:link/utils~ManualDecorator}.
  20. *
  21. * @param {Object} config
  22. * @param {String} config.id The name of the attribute used in the model that represents a given manual decorator.
  23. * For example: `'linkIsExternal'`.
  24. * @param {String} config.label The label used in the user interface to toggle the manual decorator.
  25. * @param {Object} config.attributes A set of attributes added to output data when the decorator is active for a specific link.
  26. * Attributes should keep the format of attributes defined in {@link module:engine/view/elementdefinition~ElementDefinition}.
  27. * @param {Boolean} [config.defaultValue] Controls whether the decorator is "on" by default.
  28. */
  29. constructor( { id, label, attributes, defaultValue } ) {
  30. /**
  31. * An ID of a manual decorator which is the name of the attribute in the model, for example: 'linkManualDecorator0'.
  32. *
  33. * @type {String}
  34. */
  35. this.id = id;
  36. /**
  37. * The value of the current manual decorator. It reflects its state from the UI.
  38. *
  39. * @observable
  40. * @member {Boolean} module:link/utils~ManualDecorator#value
  41. */
  42. this.set( 'value' );
  43. /**
  44. * The default value of manual decorator.
  45. *
  46. * @type {Boolean}
  47. */
  48. this.defaultValue = defaultValue;
  49. /**
  50. * The label used in the user interface to toggle the manual decorator.
  51. *
  52. * @type {String}
  53. */
  54. this.label = label;
  55. /**
  56. * A set of attributes added to downcasted data when the decorator is activated for a specific link.
  57. * Attributes should be added in a form of attributes defined in {@link module:engine/view/elementdefinition~ElementDefinition}.
  58. *
  59. * @type {Object}
  60. */
  61. this.attributes = attributes;
  62. }
  63. }
  64. mix( ManualDecorator, ObservableMixin );