UNPKG

2.36 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2024, 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/imagecaption/imagecaptionui
7 */
8import { Plugin, icons } from 'ckeditor5/src/core.js';
9import { ButtonView } from 'ckeditor5/src/ui.js';
10import ImageCaptionUtils from './imagecaptionutils.js';
11/**
12 * The image caption UI plugin. It introduces the `'toggleImageCaption'` UI button.
13 */
14export default class ImageCaptionUI extends Plugin {
15 /**
16 * @inheritDoc
17 */
18 static get requires() {
19 return [ImageCaptionUtils];
20 }
21 /**
22 * @inheritDoc
23 */
24 static get pluginName() {
25 return 'ImageCaptionUI';
26 }
27 /**
28 * @inheritDoc
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 // Scroll to the selection and highlight the caption if the caption showed up.
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}