toggleattributecommand.js 4.1 KB

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