1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { Plugin, icons } from 'ckeditor5/src/core.js';
|
9 | import { ButtonView } from 'ckeditor5/src/ui.js';
|
10 | import ImageCaptionUtils from './imagecaptionutils.js';
|
11 |
|
12 |
|
13 |
|
14 | export default class ImageCaptionUI extends Plugin {
|
15 | |
16 |
|
17 |
|
18 | static get requires() {
|
19 | return [ImageCaptionUtils];
|
20 | }
|
21 | |
22 |
|
23 |
|
24 | static get pluginName() {
|
25 | return 'ImageCaptionUI';
|
26 | }
|
27 | |
28 |
|
29 |
|
30 | init() {
|
31 | const editor = this.editor;
|
32 | const editingView = editor.editing.view;
|
33 | const imageCaptionUtils = editor.plugins.get('ImageCaptionUtils');
|
34 | const t = editor.t;
|
35 | editor.ui.componentFactory.add('toggleImageCaption', locale => {
|
36 | const command = editor.commands.get('toggleImageCaption');
|
37 | const view = new ButtonView(locale);
|
38 | view.set({
|
39 | icon: icons.caption,
|
40 | tooltip: true,
|
41 | isToggleable: true
|
42 | });
|
43 | view.bind('isOn', 'isEnabled').to(command, 'value', 'isEnabled');
|
44 | view.bind('label').to(command, 'value', value => value ? t('Toggle caption off') : t('Toggle caption on'));
|
45 | this.listenTo(view, 'execute', () => {
|
46 | editor.execute('toggleImageCaption', { focusCaptionOnShow: true });
|
47 |
|
48 | const modelCaptionElement = imageCaptionUtils.getCaptionFromModelSelection(editor.model.document.selection);
|
49 | if (modelCaptionElement) {
|
50 | const figcaptionElement = editor.editing.mapper.toViewElement(modelCaptionElement);
|
51 | editingView.scrollToTheSelection();
|
52 | editingView.change(writer => {
|
53 | writer.addClass('image__caption_highlighted', figcaptionElement);
|
54 | });
|
55 | }
|
56 | editor.editing.view.focus();
|
57 | });
|
58 | return view;
|
59 | });
|
60 | }
|
61 | }
|