-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
imagecaptionui.ts
77 lines (62 loc) · 2.17 KB
/
imagecaptionui.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* @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/imagecaption/imagecaptionui
*/
import { Plugin, icons } from 'ckeditor5/src/core.js';
import { ButtonView } from 'ckeditor5/src/ui.js';
import ImageCaptionUtils from './imagecaptionutils.js';
import type ToggleImageCaptionCommand from './toggleimagecaptioncommand.js';
/**
* The image caption UI plugin. It introduces the `'toggleImageCaption'` UI button.
*/
export default class ImageCaptionUI extends Plugin {
/**
* @inheritDoc
*/
public static get requires() {
return [ ImageCaptionUtils ] as const;
}
/**
* @inheritDoc
*/
public static get pluginName() {
return 'ImageCaptionUI' as const;
}
/**
* @inheritDoc
*/
public init(): void {
const editor = this.editor;
const editingView = editor.editing.view;
const imageCaptionUtils: ImageCaptionUtils = editor.plugins.get( 'ImageCaptionUtils' );
const t = editor.t;
editor.ui.componentFactory.add( 'toggleImageCaption', locale => {
const command: ToggleImageCaptionCommand = editor.commands.get( 'toggleImageCaption' )!;
const view = new ButtonView( locale );
view.set( {
icon: icons.caption,
tooltip: true,
isToggleable: true
} );
view.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
view.bind( 'label' ).to( command, 'value', value => value ? t( 'Toggle caption off' ) : t( 'Toggle caption on' ) );
this.listenTo( view, 'execute', () => {
editor.execute( 'toggleImageCaption', { focusCaptionOnShow: true } );
// Scroll to the selection and highlight the caption if the caption showed up.
const modelCaptionElement = imageCaptionUtils.getCaptionFromModelSelection( editor.model.document.selection );
if ( modelCaptionElement ) {
const figcaptionElement = editor.editing.mapper.toViewElement( modelCaptionElement );
editingView.scrollToTheSelection();
editingView.change( writer => {
writer.addClass( 'image__caption_highlighted', figcaptionElement! );
} );
}
editor.editing.view.focus();
} );
return view;
} );
}
}