8
0

attributecommand.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 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/model~Model#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 the attribute is set on the first node in the selection that allows this attribute.
  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 model = this.editor.model;
  53. const doc = model.document;
  54. this.value = this._getValueFromFirstAllowedNode();
  55. this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, this.attributeKey );
  56. }
  57. /**
  58. * Executes the command — applies the attribute to the selection or removes it from the selection.
  59. *
  60. * If the command is active (`value == true`), it will remove attributes. Otherwise, it will set attributes.
  61. *
  62. * The execution result differs, depending on the {@link module:engine/model/document~Document#selection}:
  63. *
  64. * * If the selection is on a range, the command applies the attribute to all nodes in that range
  65. * (if they are allowed to have this attribute by the {@link module:engine/model/schema~Schema schema}).
  66. * * If the selection is collapsed in a non-empty node, the command applies the attribute to the
  67. * {@link module:engine/model/document~Document#selection} itself (note that typed characters copy attributes from the selection).
  68. * * If the selection is collapsed in an empty node, the command applies the attribute to the parent node of the selection (note
  69. * that the selection inherits all attributes from a node if it is in an empty node).
  70. *
  71. * @fires execute
  72. * @param {Object} [options] Command options.
  73. * @param {Boolean} [options.forceValue] If set, it will force the command behavior. If `true`, the command will apply the attribute,
  74. * otherwise the command will remove the attribute.
  75. * If not set, the command will look for its current value to decide what it should do.
  76. */
  77. execute( options = {} ) {
  78. const model = this.editor.model;
  79. const doc = model.document;
  80. const selection = doc.selection;
  81. const value = ( options.forceValue === undefined ) ? !this.value : options.forceValue;
  82. model.change( writer => {
  83. if ( selection.isCollapsed ) {
  84. if ( value ) {
  85. writer.setSelectionAttribute( this.attributeKey, true );
  86. } else {
  87. writer.removeSelectionAttribute( this.attributeKey );
  88. }
  89. } else {
  90. const ranges = model.schema.getValidRanges( selection.getRanges(), this.attributeKey );
  91. for ( const range of ranges ) {
  92. if ( value ) {
  93. writer.setAttribute( this.attributeKey, value, range );
  94. } else {
  95. writer.removeAttribute( this.attributeKey, range );
  96. }
  97. }
  98. }
  99. } );
  100. }
  101. /**
  102. * Checks the attribute value of the first node in the selection that allows the attribute.
  103. * For the collapsed selection returns the selection attribute.
  104. *
  105. * @private
  106. * @returns {Boolean} The attribute value.
  107. */
  108. _getValueFromFirstAllowedNode() {
  109. const model = this.editor.model;
  110. const schema = model.schema;
  111. const selection = model.document.selection;
  112. if ( selection.isCollapsed ) {
  113. return selection.hasAttribute( this.attributeKey );
  114. }
  115. for ( const range of selection.getRanges() ) {
  116. for ( const item of range.getItems() ) {
  117. if ( schema.checkAttribute( item, this.attributeKey ) ) {
  118. return item.hasAttribute( this.attributeKey );
  119. }
  120. }
  121. }
  122. return false;
  123. }
  124. }