toggleattributecommand.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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~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. // Refresh the value and state of the command on #changesDone to make sure that
  41. // the correct state of the command is set initially.
  42. // https://github.com/ckeditor/ckeditor5-core/issues/50
  43. this.listenTo( editor.document, 'changesDone', () => {
  44. this.refreshValue();
  45. this.refreshState();
  46. } );
  47. }
  48. /**
  49. * Updates command's {@link #value value} based on current selection.
  50. */
  51. refreshValue() {
  52. this.value = this.editor.document.selection.hasAttribute( this.attributeKey );
  53. }
  54. /**
  55. * Checks if {@link module:engine/model/document~Document#schema} allows to create attribute in
  56. * {@link module:engine/model/document~Document#selection}.
  57. *
  58. * @private
  59. * @returns {Boolean}
  60. */
  61. _checkEnabled() {
  62. const document = this.editor.document;
  63. return isAttributeAllowedInSelection( this.attributeKey, document.selection, document.schema );
  64. }
  65. /**
  66. * Executes the command: adds or removes attributes to nodes or selection.
  67. *
  68. * If the command is active (`value == true`), it will remove attributes. Otherwise, it will set attributes.
  69. *
  70. * The execution result differs, depending on the {@link module:engine/model/document~Document#selection}:
  71. * * if selection is on a range, the command applies the attribute on all nodes in that ranges
  72. * (if they are allowed to have this attribute by the {@link module:engine/model/schema~Schema schema}),
  73. * * if selection is collapsed in non-empty node, the command applies attribute to the
  74. * {@link module:engine/model/document~Document#selection} itself (note that typed characters copy attributes from selection),
  75. * * if selection is collapsed in empty node, the command applies attribute to the parent node of selection (note
  76. * that selection inherits all attributes from a node if it is in empty node).
  77. *
  78. * If the command is disabled (`isEnabled == false`) when it is executed, nothing will happen.
  79. *
  80. * @private
  81. * @param {Object} [options] Options of command.
  82. * @param {Boolean} [options.forceValue] If set it will force command behavior. If `true`, command will apply attribute,
  83. * otherwise command will remove attribute. If not set, command will look for it's current value to decide what it should do.
  84. * @param {module:engine/model/batch~Batch} [options.batch] Batch to group undo steps.
  85. */
  86. _doExecute( options = {} ) {
  87. const document = this.editor.document;
  88. const selection = document.selection;
  89. const value = ( options.forceValue === undefined ) ? !this.value : options.forceValue;
  90. // If selection has non-collapsed ranges, we change attribute on nodes inside those ranges.
  91. document.enqueueChanges( () => {
  92. if ( selection.isCollapsed ) {
  93. if ( value ) {
  94. selection.setAttribute( this.attributeKey, true );
  95. } else {
  96. selection.removeAttribute( this.attributeKey );
  97. }
  98. } else {
  99. const ranges = getSchemaValidRanges( this.attributeKey, selection.getRanges(), document.schema );
  100. // Keep it as one undo step.
  101. const batch = options.batch || document.batch();
  102. for ( let range of ranges ) {
  103. if ( value ) {
  104. batch.setAttribute( range, this.attributeKey, value );
  105. } else {
  106. batch.removeAttribute( range, this.attributeKey );
  107. }
  108. }
  109. }
  110. } );
  111. }
  112. }