highlightcommand.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 text highlighting.
  13. *
  14. * @extends module:core/command~Command
  15. */
  16. export default class HighlightCommand extends Command {
  17. /**
  18. * @inheritDoc
  19. */
  20. refresh() {
  21. const model = this.editor.model;
  22. const doc = model.document;
  23. /**
  24. * A flag indicating whether the command is active, which means that the selection has highlight attribute set.
  25. *
  26. * @observable
  27. * @readonly
  28. * @member {undefined|String} module:highlight/highlightcommand~HighlightCommand#value
  29. */
  30. this.value = doc.selection.getAttribute( 'highlight' );
  31. this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'highlight' );
  32. }
  33. /**
  34. * Executes the command.
  35. *
  36. * @protected
  37. * @param {Object} [options] Options for the executed command.
  38. * @param {String} [options.value] a value to apply.
  39. *
  40. * @fires execute
  41. */
  42. execute( options = {} ) {
  43. const model = this.editor.model;
  44. const document = model.document;
  45. const selection = document.selection;
  46. // Do not apply highlight on collapsed selection when not inside existing highlight.
  47. if ( selection.isCollapsed && !this.value ) {
  48. return;
  49. }
  50. const highlighter = options.value;
  51. model.change( writer => {
  52. const ranges = model.schema.getValidRanges( selection.getRanges(), 'highlight' );
  53. if ( selection.isCollapsed ) {
  54. const position = selection.getFirstPosition();
  55. // When selection is inside text with `highlight` attribute.
  56. if ( selection.hasAttribute( 'highlight' ) ) {
  57. // Find the full highlighted range.
  58. const isSameHighlight = value => {
  59. return value.item.hasAttribute( 'highlight' ) && value.item.getAttribute( 'highlight' ) === this.value;
  60. };
  61. const highlightStart = position.getLastMatchingPosition( isSameHighlight, { direction: 'backward' } );
  62. const highlightEnd = position.getLastMatchingPosition( isSameHighlight );
  63. const highlightRange = new Range( highlightStart, highlightEnd );
  64. // Then depending on current value...
  65. if ( !highlighter || this.value === highlighter ) {
  66. // ...remove attribute when passing highlighter different then current or executing "eraser".
  67. writer.removeAttribute( 'highlight', highlightRange );
  68. } else {
  69. // ...update `highlight` value.
  70. writer.setAttribute( 'highlight', highlighter, highlightRange );
  71. // And create new range wrapping changed highlighter.
  72. selection.setRanges( [ highlightRange ] );
  73. }
  74. } else {
  75. // TODO
  76. }
  77. } else {
  78. for ( const range of ranges ) {
  79. if ( highlighter ) {
  80. writer.setAttribute( 'highlight', highlighter, range );
  81. } else {
  82. writer.removeAttribute( 'highlight', range );
  83. }
  84. }
  85. }
  86. } );
  87. }
  88. }