highlightcommand.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module highlight/highlightcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  10. /**
  11. * The highlight command. It is used by the {@link module:highlight/highlightediting~HighlightEditing highlight feature}
  12. * to apply the text highlighting.
  13. *
  14. * editor.execute( 'highlight', { value: 'greenMarker' } );
  15. *
  16. * **Note**: Executing the command without the value removes the attribute from the model. If the selection is collapsed
  17. * inside a text with the highlight attribute, the command will remove the attribute from the entire range
  18. * of that text.
  19. *
  20. * @extends module:core/command~Command
  21. */
  22. export default class HighlightCommand extends Command {
  23. /**
  24. * @inheritDoc
  25. */
  26. refresh() {
  27. const model = this.editor.model;
  28. const doc = model.document;
  29. /**
  30. * A value indicating whether the command is active. If the selection has some highlight attribute,
  31. * it corresponds to the value of that attribute.
  32. *
  33. * @observable
  34. * @readonly
  35. * @member {undefined|String} module:highlight/highlightcommand~HighlightCommand#value
  36. */
  37. this.value = doc.selection.getAttribute( 'highlight' );
  38. this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'highlight' );
  39. }
  40. /**
  41. * Executes the command.
  42. *
  43. * @protected
  44. * @param {Object} [options] Options for the executed command.
  45. * @param {String} [options.value] a value to apply.
  46. *
  47. * @fires execute
  48. */
  49. execute( options = {} ) {
  50. const model = this.editor.model;
  51. const document = model.document;
  52. const selection = document.selection;
  53. const highlighter = options.value;
  54. model.change( writer => {
  55. const ranges = model.schema.getValidRanges( selection.getRanges(), 'highlight' );
  56. if ( selection.isCollapsed ) {
  57. const position = selection.getFirstPosition();
  58. // When selection is inside text with `highlight` attribute.
  59. if ( selection.hasAttribute( 'highlight' ) ) {
  60. // Find the full highlighted range.
  61. const isSameHighlight = value => {
  62. return value.item.hasAttribute( 'highlight' ) && value.item.getAttribute( 'highlight' ) === this.value;
  63. };
  64. const highlightStart = position.getLastMatchingPosition( isSameHighlight, { direction: 'backward' } );
  65. const highlightEnd = position.getLastMatchingPosition( isSameHighlight );
  66. const highlightRange = new Range( highlightStart, highlightEnd );
  67. // Then depending on current value...
  68. if ( !highlighter || this.value === highlighter ) {
  69. // ...remove attribute when passing highlighter different then current or executing "eraser".
  70. writer.removeAttribute( 'highlight', highlightRange );
  71. writer.removeSelectionAttribute( 'highlight' );
  72. } else {
  73. // ...update `highlight` value.
  74. writer.setAttribute( 'highlight', highlighter, highlightRange );
  75. writer.setSelectionAttribute( 'highlight', highlighter );
  76. }
  77. } else if ( highlighter ) {
  78. writer.setSelectionAttribute( 'highlight', highlighter );
  79. }
  80. } else {
  81. for ( const range of ranges ) {
  82. if ( highlighter ) {
  83. writer.setAttribute( 'highlight', highlighter, range );
  84. } else {
  85. writer.removeAttribute( 'highlight', range );
  86. }
  87. }
  88. }
  89. } );
  90. }
  91. }