1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | import { Plugin, icons } from 'ckeditor5/src/core';
|
11 | import { ButtonView } from 'ckeditor5/src/ui';
|
12 | import ImageCaptionUtils from './imagecaptionutils';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export default class ImageCaptionUI extends Plugin {
|
20 | |
21 |
|
22 |
|
23 | static get requires() {
|
24 | return [ ImageCaptionUtils ];
|
25 | }
|
26 |
|
27 | |
28 |
|
29 |
|
30 | static get pluginName() {
|
31 | return 'ImageCaptionUI';
|
32 | }
|
33 |
|
34 | |
35 |
|
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 |
|
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 | }
|