8
0

toggleattributecommand.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Command from './command.js';
  6. import getSchemaValidRanges from './helpers/getschemavalidranges.js';
  7. import isAttributeAllowedInSelection from './helpers/isattributeallowedinselection.js';
  8. /**
  9. * An extension of basic {@link core.command.Command} class, which provides utilities for a command that toggle a single
  10. * attribute on a text or element with value `true`. ToggleAttributeCommand uses {@link engine.model.Document#selection}
  11. * to decide which nodes (if any) should be changed, and applies or removes attributes from them.
  12. * See {@link engine.view.Converter#execute} for more.
  13. *
  14. * The command checks {@link engine.model.Document#schema} to decide if it should be enabled.
  15. * See {@link engine.view.Converter#checkSchema} for more.
  16. *
  17. * @memberOf core.command
  18. */
  19. export default class ToggleAttributeCommand extends Command {
  20. /**
  21. * @see core.command.Command
  22. * @param {core.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. * @member {String} core.command.ToggleAttributeCommand#attributeKey
  31. */
  32. this.attributeKey = attributeKey;
  33. /**
  34. * Flag indicating whether command is active. For collapsed selection it means that typed characters will have
  35. * the command's attribute set. For range selection it means that all nodes inside have the attribute applied.
  36. *
  37. * @observable
  38. * @member {Boolean} core.command.ToggleAttributeCommand#value
  39. */
  40. this.set( 'value', false );
  41. this.listenTo( this.editor.document.selection, 'change:attribute', () => {
  42. this.value = this.editor.document.selection.hasAttribute( this.attributeKey );
  43. } );
  44. }
  45. /**
  46. * Checks if {@link engine.model.Document#schema} allows to create attribute in {@link engine.model.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 engine.model.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 engine.model.Schema schema}),
  63. * * if selection is collapsed in non-empty node, the command applies attribute to the {@link engine.model.Document#selection}
  64. * 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 {engine.model.Batch} [options.batch] Batch to group undo steps.
  75. * @param {Array.<engine.model.Range>} [options.ranges] The list of ranges to apply command to.
  76. */
  77. _doExecute( options = {} ) {
  78. const { forceValue, batch, ranges } = options;
  79. const document = this.editor.document;
  80. const selection = document.selection;
  81. const value = ( forceValue === undefined ) ? !this.value : forceValue;
  82. // If selection has non-collapsed ranges, we change attribute on nodes inside those ranges.
  83. document.enqueueChanges( () => {
  84. if ( !ranges && selection.isCollapsed ) {
  85. if ( value ) {
  86. selection.setAttribute( this.attributeKey, true );
  87. } else {
  88. selection.removeAttribute( this.attributeKey );
  89. }
  90. } else {
  91. const workRanges = getSchemaValidRanges( this.attributeKey, ranges || selection.getRanges(), document.schema );
  92. // Keep it as one undo step.
  93. const workBatch = batch || document.batch();
  94. for ( let range of workRanges ) {
  95. if ( value ) {
  96. workBatch.setAttribute( range, this.attributeKey, value );
  97. } else {
  98. workBatch.removeAttribute( range, this.attributeKey );
  99. }
  100. }
  101. }
  102. } );
  103. }
  104. }