imagetextalternativecommand.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 on `<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.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. * @param {module:engine/model/batch~Batch} [options.batch] A batch to collect all the change steps. A new batch will be
  42. * created if this option is not set.
  43. */
  44. execute( options ) {
  45. const doc = this.editor.document;
  46. const imageElement = doc.selection.getSelectedElement();
  47. doc.enqueueChanges( () => {
  48. const batch = options.batch || doc.batch();
  49. batch.setAttribute( 'alt', options.newValue, imageElement );
  50. } );
  51. }
  52. }