-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
imagetextalternativecommand.ts
59 lines (51 loc) · 1.72 KB
/
imagetextalternativecommand.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module image/imagetextalternative/imagetextalternativecommand
*/
import { Command } from 'ckeditor5/src/core.js';
import type ImageUtils from '../imageutils.js';
/**
* The image text alternative command. It is used to change the `alt` attribute of `<imageBlock>` and `<imageInline>` model elements.
*/
export default class ImageTextAlternativeCommand extends Command {
/**
* The command value: `false` if there is no `alt` attribute, otherwise the value of the `alt` attribute.
*
* @readonly
* @observable
*/
declare public value: string | false;
/**
* @inheritDoc
*/
public override refresh(): void {
const editor = this.editor;
const imageUtils: ImageUtils = editor.plugins.get( 'ImageUtils' );
const element = imageUtils.getClosestSelectedImageElement( this.editor.model.document.selection )!;
this.isEnabled = !!element;
if ( this.isEnabled && element.hasAttribute( 'alt' ) ) {
this.value = element.getAttribute( 'alt' ) as string | false;
} else {
this.value = false;
}
}
/**
* Executes the command.
*
* @fires execute
* @param options
* @param options.newValue The new value of the `alt` attribute to set.
*/
public override execute( options: { newValue: string } ): void {
const editor = this.editor;
const imageUtils: ImageUtils = editor.plugins.get( 'ImageUtils' );
const model = editor.model;
const imageElement = imageUtils.getClosestSelectedImageElement( model.document.selection );
model.change( writer => {
writer.setAttribute( 'alt', options.newValue, imageElement! );
} );
}
}