UNPKG

1.53 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. 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 */
8import { Command } from 'ckeditor5/src/core';
9/**
10 * The image text alternative command. It is used to change the `alt` attribute of `<imageBlock>` and `<imageInline>` model elements.
11 */
12export default class ImageTextAlternativeCommand extends Command {
13 /**
14 * @inheritDoc
15 */
16 refresh() {
17 const editor = this.editor;
18 const imageUtils = editor.plugins.get('ImageUtils');
19 const element = imageUtils.getClosestSelectedImageElement(this.editor.model.document.selection);
20 this.isEnabled = !!element;
21 if (this.isEnabled && element.hasAttribute('alt')) {
22 this.value = element.getAttribute('alt');
23 }
24 else {
25 this.value = false;
26 }
27 }
28 /**
29 * Executes the command.
30 *
31 * @fires execute
32 * @param options
33 * @param options.newValue The new value of the `alt` attribute to set.
34 */
35 execute(options) {
36 const editor = this.editor;
37 const imageUtils = editor.plugins.get('ImageUtils');
38 const model = editor.model;
39 const imageElement = imageUtils.getClosestSelectedImageElement(model.document.selection);
40 model.change(writer => {
41 writer.setAttribute('alt', options.newValue, imageElement);
42 });
43 }
44}