1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | import { Plugin } from 'ckeditor5/src/core';
|
11 | import ImageInsertPanelView from './ui/imageinsertpanelview';
|
12 | import { prepareIntegrations } from './utils';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | export default class ImageInsertUI extends Plugin {
|
26 | |
27 |
|
28 |
|
29 | static get pluginName() {
|
30 | return 'ImageInsertUI';
|
31 | }
|
32 |
|
33 | |
34 |
|
35 |
|
36 | init() {
|
37 | const editor = this.editor;
|
38 | const componentCreator = locale => {
|
39 | return this._createDropdownView( locale );
|
40 | };
|
41 |
|
42 |
|
43 | editor.ui.componentFactory.add( 'insertImage', componentCreator );
|
44 | editor.ui.componentFactory.add( 'imageInsert', componentCreator );
|
45 | }
|
46 |
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | _createDropdownView( locale ) {
|
56 | const editor = this.editor;
|
57 | const imageInsertView = new ImageInsertPanelView( locale, prepareIntegrations( editor ) );
|
58 | const command = editor.commands.get( 'uploadImage' );
|
59 |
|
60 | const dropdownView = imageInsertView.dropdownView;
|
61 | const splitButtonView = dropdownView.buttonView;
|
62 |
|
63 | splitButtonView.actionView = editor.ui.componentFactory.create( 'uploadImage' );
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | splitButtonView.actionView.extendTemplate( {
|
69 | attributes: {
|
70 | class: 'ck ck-button ck-splitbutton__action'
|
71 | }
|
72 | } );
|
73 |
|
74 | return this._setUpDropdown( dropdownView, imageInsertView, command );
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | _setUpDropdown( dropdownView, imageInsertView, command ) {
|
88 | const editor = this.editor;
|
89 | const t = editor.t;
|
90 | const insertButtonView = imageInsertView.insertButtonView;
|
91 | const insertImageViaUrlForm = imageInsertView.getIntegration( 'insertImageViaUrl' );
|
92 | const panelView = dropdownView.panelView;
|
93 | const imageUtils = this.editor.plugins.get( 'ImageUtils' );
|
94 |
|
95 | dropdownView.bind( 'isEnabled' ).to( command );
|
96 |
|
97 |
|
98 |
|
99 | dropdownView.buttonView.once( 'open', () => {
|
100 | panelView.children.add( imageInsertView );
|
101 | } );
|
102 |
|
103 | dropdownView.on( 'change:isOpen', () => {
|
104 | const selectedElement = editor.model.document.selection.getSelectedElement();
|
105 |
|
106 | if ( dropdownView.isOpen ) {
|
107 | imageInsertView.focus();
|
108 |
|
109 | if ( imageUtils.isImage( selectedElement ) ) {
|
110 | imageInsertView.imageURLInputValue = selectedElement.getAttribute( 'src' );
|
111 | insertButtonView.label = t( 'Update' );
|
112 | insertImageViaUrlForm.label = t( 'Update image URL' );
|
113 | } else {
|
114 | imageInsertView.imageURLInputValue = '';
|
115 | insertButtonView.label = t( 'Insert' );
|
116 | insertImageViaUrlForm.label = t( 'Insert image via URL' );
|
117 | }
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 | }, { priority: 'low' } );
|
123 |
|
124 | imageInsertView.delegate( 'submit', 'cancel' ).to( dropdownView );
|
125 | this.delegate( 'cancel' ).to( dropdownView );
|
126 |
|
127 | dropdownView.on( 'submit', () => {
|
128 | closePanel();
|
129 | onSubmit();
|
130 | } );
|
131 |
|
132 | dropdownView.on( 'cancel', () => {
|
133 | closePanel();
|
134 | } );
|
135 |
|
136 | function onSubmit() {
|
137 | const selectedElement = editor.model.document.selection.getSelectedElement();
|
138 |
|
139 | if ( imageUtils.isImage( selectedElement ) ) {
|
140 | editor.model.change( writer => {
|
141 | writer.setAttribute( 'src', imageInsertView.imageURLInputValue, selectedElement );
|
142 | writer.removeAttribute( 'srcset', selectedElement );
|
143 | writer.removeAttribute( 'sizes', selectedElement );
|
144 | } );
|
145 | } else {
|
146 | editor.execute( 'insertImage', { source: imageInsertView.imageURLInputValue } );
|
147 | }
|
148 | }
|
149 |
|
150 | function closePanel() {
|
151 | editor.editing.view.focus();
|
152 | dropdownView.isOpen = false;
|
153 | }
|
154 |
|
155 | return dropdownView;
|
156 | }
|
157 | }
|