UNPKG

2.05 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2022, 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/**
7 * @module image/imagecaption/imagecaptionui
8 */
9
10import { Plugin, icons } from 'ckeditor5/src/core';
11import { ButtonView } from 'ckeditor5/src/ui';
12import ImageCaptionUtils from './imagecaptionutils';
13
14/**
15 * The image caption UI plugin. It introduces the `'toggleImageCaption'` UI button.
16 *
17 * @extends module:core/plugin~Plugin
18 */
19export default class ImageCaptionUI extends Plugin {
20 /**
21 * @inheritDoc
22 */
23 static get requires() {
24 return [ ImageCaptionUtils ];
25 }
26
27 /**
28 * @inheritDoc
29 */
30 static get pluginName() {
31 return 'ImageCaptionUI';
32 }
33
34 /**
35 * @inheritDoc
36 */
37 init() {
38 const editor = this.editor;
39 const editingView = editor.editing.view;
40 const imageCaptionUtils = editor.plugins.get( 'ImageCaptionUtils' );
41 const t = editor.t;
42
43 editor.ui.componentFactory.add( 'toggleImageCaption', locale => {
44 const command = editor.commands.get( 'toggleImageCaption' );
45 const view = new ButtonView( locale );
46
47 view.set( {
48 icon: icons.caption,
49 tooltip: true,
50 isToggleable: true
51 } );
52
53 view.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
54 view.bind( 'label' ).to( command, 'value', value => value ? t( 'Toggle caption off' ) : t( 'Toggle caption on' ) );
55
56 this.listenTo( view, 'execute', () => {
57 editor.execute( 'toggleImageCaption', { focusCaptionOnShow: true } );
58
59 // Scroll to the selection and highlight the caption if the caption showed up.
60 const modelCaptionElement = imageCaptionUtils.getCaptionFromModelSelection( editor.model.document.selection );
61
62 if ( modelCaptionElement ) {
63 const figcaptionElement = editor.editing.mapper.toViewElement( modelCaptionElement );
64
65 editingView.scrollToTheSelection();
66
67 editingView.change( writer => {
68 writer.addClass( 'image__caption_highlighted', figcaptionElement );
69 } );
70 }
71 } );
72
73 return view;
74 } );
75 }
76}