toggleattributecommand.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @license Copyright (c) 2003-2016, 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~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. export default class ToggleAttributeCommand extends Command {
  19. /**
  20. * @see module:core/command/command~Command
  21. * @param {module:core/editor/editor~Editor} editor
  22. * @param {String} attributeKey Attribute that will be set by the command.
  23. */
  24. constructor( editor, attributeKey ) {
  25. super( editor );
  26. /**
  27. * Attribute that will be set by the command.
  28. *
  29. * @member {String}
  30. */
  31. this.attributeKey = attributeKey;
  32. /**
  33. * Flag indicating whether command is active. For collapsed selection it means that typed characters will have
  34. * the command's attribute set. For range selection it means that all nodes inside have the attribute applied.
  35. *
  36. * @observable
  37. * @member {Boolean} #value
  38. */
  39. this.set( 'value', false );
  40. this.listenTo( this.editor.document.selection, 'change:attribute', () => {
  41. this.value = this.editor.document.selection.hasAttribute( this.attributeKey );
  42. } );
  43. }
  44. /**
  45. * Checks if {@link module:engine/model/document~Document#schema} allows to create attribute in
  46. * {@link module:engine/model/document~Document#selection}.
  47. *
  48. * @private
  49. * @returns {Boolean}
  50. */
  51. _checkEnabled() {
  52. const document = this.editor.document;
  53. return isAttributeAllowedInSelection( this.attributeKey, document.selection, document.schema );
  54. }
  55. /**
  56. * Executes the command: adds or removes attributes to nodes or selection.
  57. *
  58. * If the command is active (`value == true`), it will remove attributes. Otherwise, it will set attributes.
  59. *
  60. * The execution result differs, depending on the {@link module:engine/model/document~Document#selection}:
  61. * * if selection is on a range, the command applies the attribute on all nodes in that ranges
  62. * (if they are allowed to have this attribute by the {@link module:engine/model/schema~Schema schema}),
  63. * * if selection is collapsed in non-empty node, the command applies attribute to the
  64. * {@link module:engine/model/document~Document#selection} itself (note that typed characters copy attributes from selection),
  65. * * if selection is collapsed in empty node, the command applies attribute to the parent node of selection (note
  66. * that selection inherits all attributes from a node if it is in empty node).
  67. *
  68. * If the command is disabled (`isEnabled == false`) when it is executed, nothing will happen.
  69. *
  70. * @private
  71. * @param {Object} [options] Options of command.
  72. * @param {Boolean} [options.forceValue] If set it will force command behavior. If `true`, command will apply attribute,
  73. * otherwise command will remove attribute. If not set, command will look for it's current value to decide what it should do.
  74. * @param {module:engine/model/batch~Batch} [options.batch] Batch to group undo steps.
  75. */
  76. _doExecute( options = {} ) {
  77. const document = this.editor.document;
  78. const selection = document.selection;
  79. const value = ( options.forceValue === undefined ) ? !this.value : options.forceValue;
  80. // If selection has non-collapsed ranges, we change attribute on nodes inside those ranges.
  81. document.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 = getSchemaValidRanges( this.attributeKey, selection.getRanges(), document.schema );
  90. // Keep it as one undo step.
  91. const batch = options.batch || document.batch();
  92. for ( let range of ranges ) {
  93. if ( value ) {
  94. batch.setAttribute( range, this.attributeKey, value );
  95. } else {
  96. batch.removeAttribute( range, this.attributeKey );
  97. }
  98. }
  99. }
  100. } );
  101. }
  102. }