This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
imagetextalternativeui.js
214 lines (181 loc) · 5.67 KB
/
imagetextalternativeui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module image/imagetextalternative/imagetextalternativeui
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
import clickOutsideHandler from '@ckeditor/ckeditor5-ui/src/bindings/clickoutsidehandler';
import TextAlternativeFormView from './ui/textalternativeformview';
import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
import textAlternativeIcon from '@ckeditor/ckeditor5-core/theme/icons/low-vision.svg';
import { repositionContextualBalloon, getBalloonPositionData } from '../image/ui/utils';
import { getSelectedImageWidget } from '../image/utils';
/**
* The image text alternative UI plugin.
*
* The plugin uses the {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon}.
*
* @extends module:core/plugin~Plugin
*/
export default class ImageTextAlternativeUI extends Plugin {
/**
* @inheritDoc
*/
static get requires() {
return [ ContextualBalloon ];
}
/**
* @inheritDoc
*/
static get pluginName() {
return 'ImageTextAlternativeUI';
}
/**
* @inheritDoc
*/
init() {
this._createButton();
this._createForm();
}
/**
* @inheritDoc
*/
destroy() {
super.destroy();
// Destroy created UI components as they are not automatically destroyed (see ckeditor5#1341).
this._form.destroy();
}
/**
* Creates a button showing the balloon panel for changing the image text alternative and
* registers it in the editor {@link module:ui/componentfactory~ComponentFactory ComponentFactory}.
*
* @private
*/
_createButton() {
const editor = this.editor;
const t = editor.t;
editor.ui.componentFactory.add( 'imageTextAlternative', locale => {
const command = editor.commands.get( 'imageTextAlternative' );
const view = new ButtonView( locale );
view.set( {
label: t( 'Change image text alternative' ),
icon: textAlternativeIcon,
tooltip: true
} );
view.bind( 'isEnabled' ).to( command, 'isEnabled' );
this.listenTo( view, 'execute', () => this._showForm() );
return view;
} );
}
/**
* Creates the {@link module:image/imagetextalternative/ui/textalternativeformview~TextAlternativeFormView}
* form.
*
* @private
*/
_createForm() {
const editor = this.editor;
const view = editor.editing.view;
const viewDocument = view.document;
/**
* The contextual balloon plugin instance.
*
* @private
* @member {module:ui/panel/balloon/contextualballoon~ContextualBalloon}
*/
this._balloon = this.editor.plugins.get( 'ContextualBalloon' );
/**
* A form containing a textarea and buttons, used to change the `alt` text value.
*
* @member {module:image/imagetextalternative/ui/textalternativeformview~TextAlternativeFormView}
*/
this._form = new TextAlternativeFormView( editor.locale );
// Render the form so its #element is available for clickOutsideHandler.
this._form.render();
this.listenTo( this._form, 'submit', () => {
editor.execute( 'imageTextAlternative', {
newValue: this._form.labeledInput.inputView.element.value
} );
this._hideForm( true );
} );
this.listenTo( this._form, 'cancel', () => {
this._hideForm( true );
} );
// Close the form on Esc key press.
this._form.keystrokes.set( 'Esc', ( data, cancel ) => {
this._hideForm( true );
cancel();
} );
// Reposition the balloon or hide the form if an image widget is no longer selected.
this.listenTo( editor.ui, 'update', () => {
if ( !getSelectedImageWidget( viewDocument.selection ) ) {
this._hideForm( true );
} else if ( this._isVisible ) {
repositionContextualBalloon( editor );
}
} );
// Close on click outside of balloon panel element.
clickOutsideHandler( {
emitter: this._form,
activator: () => this._isVisible,
contextElements: [ this._form.element ],
callback: () => this._hideForm()
} );
}
/**
* Shows the {@link #_form} in the {@link #_balloon}.
*
* @private
*/
_showForm() {
if ( this._isVisible ) {
return;
}
const editor = this.editor;
const command = editor.commands.get( 'imageTextAlternative' );
const labeledInput = this._form.labeledInput;
if ( !this._balloon.hasView( this._form ) ) {
this._balloon.add( {
view: this._form,
position: getBalloonPositionData( editor )
} );
}
// Make sure that each time the panel shows up, the field remains in sync with the value of
// the command. If the user typed in the input, then canceled the balloon (`labeledInput#value`
// stays unaltered) and re-opened it without changing the value of the command, they would see the
// old value instead of the actual value of the command.
// https://github.com/ckeditor/ckeditor5-image/issues/114
labeledInput.value = labeledInput.inputView.element.value = command.value || '';
this._form.labeledInput.select();
}
/**
* Removes the {@link #_form} from the {@link #_balloon}.
*
* @param {Boolean} [focusEditable=false] Controls whether the editing view is focused afterwards.
* @private
*/
_hideForm( focusEditable ) {
if ( !this._isVisible ) {
return;
}
// Blur the input element before removing it from DOM to prevent issues in some browsers.
// See https://github.com/ckeditor/ckeditor5/issues/1501.
this._form.saveButtonView.focus();
this._balloon.remove( this._form );
if ( focusEditable ) {
this.editor.editing.view.focus();
}
}
/**
* Returns `true` when the {@link #_form} is the visible view in the {@link #_balloon}.
*
* @private
* @type {Boolean}
*/
get _isVisible() {
return this._balloon.visibleView == this._form;
}
}