8
0

unlinkcommand.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module link/unlinkcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import findLinkRange from './findlinkrange';
  10. import first from '@ckeditor/ckeditor5-utils/src/first';
  11. /**
  12. * The unlink command. It is used by the {@link module:link/link~Link link plugin}.
  13. *
  14. * @extends module:core/command~Command
  15. */
  16. export default class UnlinkCommand extends Command {
  17. /**
  18. * @inheritDoc
  19. */
  20. refresh() {
  21. const model = this.editor.model;
  22. const doc = model.document;
  23. const selectedElement = first( doc.selection.getSelectedBlocks() );
  24. // A check for the `LinkImage` plugin. If the selection contains an image element, get values from the element.
  25. // Currently the selection reads attributes from text nodes only. See #7429 and #7465.
  26. if ( selectedElement && selectedElement.is( 'image' ) && model.schema.checkAttribute( 'image', 'linkHref' ) ) {
  27. this.isEnabled = model.schema.checkAttribute( selectedElement, 'linkHref' );
  28. } else {
  29. this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'linkHref' );
  30. }
  31. }
  32. /**
  33. * Executes the command.
  34. *
  35. * When the selection is collapsed, it removes the `linkHref` attribute from each node with the same `linkHref` attribute value.
  36. * When the selection is non-collapsed, it removes the `linkHref` attribute from each node in selected ranges.
  37. *
  38. * # Decorators
  39. *
  40. * If {@link module:link/link~LinkConfig#decorators `config.link.decorators`} is specified,
  41. * all configured decorators are removed together with the `linkHref` attribute.
  42. *
  43. * @fires execute
  44. */
  45. execute() {
  46. const editor = this.editor;
  47. const model = this.editor.model;
  48. const selection = model.document.selection;
  49. const linkCommand = editor.commands.get( 'link' );
  50. model.change( writer => {
  51. // Get ranges to unlink.
  52. const rangesToUnlink = selection.isCollapsed ?
  53. [ findLinkRange( selection.getFirstPosition(), selection.getAttribute( 'linkHref' ), model ) ] : selection.getRanges();
  54. // Remove `linkHref` attribute from specified ranges.
  55. for ( const range of rangesToUnlink ) {
  56. writer.removeAttribute( 'linkHref', range );
  57. // If there are registered custom attributes, then remove them during unlink.
  58. if ( linkCommand ) {
  59. for ( const manualDecorator of linkCommand.manualDecorators ) {
  60. writer.removeAttribute( manualDecorator.id, range );
  61. }
  62. }
  63. }
  64. } );
  65. }
  66. }