highlightcommand.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 value = 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 `linkHref` attribute.
  56. if ( selection.hasAttribute( 'highlight' ) ) {
  57. const highlightStart = position.getLastMatchingPosition(
  58. val => val.item.hasAttribute( 'highlight' ) && val.item.getAttribute( 'highlight' ) === value,
  59. { direction: 'backward' }
  60. );
  61. const highlightEnd = position.getLastMatchingPosition(
  62. val => val.item.hasAttribute( 'highlight' ) && val.item.getAttribute( 'highlight' ) === value
  63. );
  64. const highlightRange = new Range( highlightStart, highlightEnd );
  65. // Then update `highlight` value.
  66. writer.setAttribute( 'highlight', value, highlightRange );
  67. // Create new range wrapping changed link.
  68. selection.setRanges( [ highlightRange ] );
  69. } else {
  70. // TODO
  71. }
  72. } else {
  73. for ( const range of ranges ) {
  74. if ( value ) {
  75. writer.setAttribute( 'highlight', value, range );
  76. } else {
  77. writer.removeAttribute( 'highlight', range );
  78. }
  79. }
  80. }
  81. } );
  82. }
  83. }