8
0

attributecommand.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module basic-styles/attributecommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. /**
  10. * An extension of the base {@link module:core/command~Command} class, which provides utilities for a command
  11. * that toggles a single attribute on a text or an element.
  12. *
  13. * `AttributeCommand` uses {@link module:engine/model/document~Document#selection}
  14. * to decide which nodes (if any) should be changed, and applies or removes the attribute from them.
  15. *
  16. * The command checks the {@link module:engine/model/document~Document#schema} to decide if it can be enabled
  17. * for the current selection and to which nodes the attribute can be applied.
  18. *
  19. * @extends module:core/command~Command
  20. */
  21. export default class AttributeCommand extends Command {
  22. /**
  23. * @param {module:core/editor/editor~Editor} editor
  24. * @param {String} attributeKey Attribute that will be set by the command.
  25. */
  26. constructor( editor, attributeKey ) {
  27. super( editor );
  28. /**
  29. * The attribute that will be set by the command.
  30. *
  31. * @readonly
  32. * @member {String}
  33. */
  34. this.attributeKey = attributeKey;
  35. /**
  36. * Flag indicating whether the command is active. The command is active when the
  37. * {@link module:engine/model/selection~Selection#hasAttribute selection has the attribute} which means that:
  38. *
  39. * * If the selection is not empty – That it starts in a text (or another node) which has the attribute set.
  40. * * If the selection is empty – That the selection has the attribute itself (which means that newly typed
  41. * text will have this attribute, too).
  42. *
  43. * @observable
  44. * @readonly
  45. * @member {Boolean} #value
  46. */
  47. }
  48. /**
  49. * Updates the command's {@link #value} and {@link #isEnabled} based on the current selection.
  50. */
  51. refresh() {
  52. const doc = this.editor.document;
  53. this.value = doc.selection.hasAttribute( this.attributeKey );
  54. this.isEnabled = doc.schema.checkAttributeInSelection( doc.selection, this.attributeKey );
  55. }
  56. /**
  57. * Executes the command — applies the attribute to the selection or removes it from the selection.
  58. *
  59. * If the command is active (`value == true`), it will remove attributes. Otherwise, it will set attributes.
  60. *
  61. * The execution result differs, depending on the {@link module:engine/model/document~Document#selection}:
  62. *
  63. * * If the selection is on a range, the command applies the attribute to all nodes in that range
  64. * (if they are allowed to have this attribute by the {@link module:engine/model/schema~Schema schema}).
  65. * * If the selection is collapsed in a non-empty node, the command applies the attribute to the
  66. * {@link module:engine/model/document~Document#selection} itself (note that typed characters copy attributes from the selection).
  67. * * If the selection is collapsed in an empty node, the command applies the attribute to the parent node of the selection (note
  68. * that the selection inherits all attributes from a node if it is in an empty node).
  69. *
  70. * @fires execute
  71. * @param {Object} [options] Command options.
  72. * @param {Boolean} [options.forceValue] If set, it will force the command behavior. If `true`, the command will apply the attribute,
  73. * otherwise the command will remove the attribute.
  74. * If not set, the command will look for its current value to decide what it should do.
  75. * @param {module:engine/model/batch~Batch} [options.batch] A batch to group undo steps.
  76. */
  77. execute( options = {} ) {
  78. const doc = this.editor.document;
  79. const selection = doc.selection;
  80. const value = ( options.forceValue === undefined ) ? !this.value : options.forceValue;
  81. doc.enqueueChanges( () => {
  82. if ( selection.isCollapsed ) {
  83. if ( value ) {
  84. selection.setAttribute( this.attributeKey, true );
  85. } else {
  86. selection.removeAttribute( this.attributeKey );
  87. }
  88. } else {
  89. const ranges = doc.schema.getValidRanges( selection.getRanges(), this.attributeKey );
  90. const batch = options.batch || doc.batch();
  91. for ( const range of ranges ) {
  92. if ( value ) {
  93. batch.setAttribute( range, this.attributeKey, value );
  94. } else {
  95. batch.removeAttribute( range, this.attributeKey );
  96. }
  97. }
  98. }
  99. } );
  100. }
  101. }