8
0

imagetextalternativecommand.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 image/imagetextalternative/imagetextalternativecommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { isImage } from '../image/utils';
  10. /**
  11. * The image text alternative command. It is used to change the `alt` attribute of `<image>` elements.
  12. *
  13. * @extends module:core/command~Command
  14. */
  15. export default class ImageTextAlternativeCommand extends Command {
  16. /**
  17. * The command value: `false` if there is no `alt` attribute, otherwise the value of the `alt` attribute.
  18. *
  19. * @readonly
  20. * @observable
  21. * @member {String|Boolean} #value
  22. */
  23. /**
  24. * @inheritDoc
  25. */
  26. refresh() {
  27. const element = this.editor.model.document.selection.getSelectedElement();
  28. this.isEnabled = isImage( element );
  29. if ( isImage( element ) && element.hasAttribute( 'alt' ) ) {
  30. this.value = element.getAttribute( 'alt' );
  31. } else {
  32. this.value = false;
  33. }
  34. }
  35. /**
  36. * Executes the command.
  37. *
  38. * @fires execute
  39. * @param {Object} options
  40. * @param {String} options.newValue The new value of the `alt` attribute to set.
  41. */
  42. execute( options ) {
  43. const model = this.editor.model;
  44. const imageElement = model.document.selection.getSelectedElement();
  45. model.change( writer => {
  46. writer.setAttribute( 'alt', options.newValue, imageElement );
  47. } );
  48. }
  49. }