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 29
/
link.js
334 lines (281 loc) · 9.65 KB
/
link.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module link/link
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
import LinkEngine from './linkengine';
import LinkElement from './linkelement';
import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
import clickOutsideHandler from '@ckeditor/ckeditor5-ui/src/bindings/clickoutsidehandler';
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
import LinkFormView from './ui/linkformview';
import linkIcon from '../theme/icons/link.svg';
import unlinkIcon from '../theme/icons/unlink.svg';
import '../theme/theme.scss';
/**
* The link plugin. It introduces the Link and Unlink buttons and the <kbd>Ctrl+K</kbd> keystroke.
*
* It uses the {@link module:link/linkengine~LinkEngine link engine plugin} and the
* {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon contextual balloon plugin}.
*
* @extends module:core/plugin~Plugin
*/
export default class Link extends Plugin {
/**
* @inheritDoc
*/
static get requires() {
return [ LinkEngine, ContextualBalloon ];
}
/**
* @inheritDoc
*/
static get pluginName() {
return 'link/link';
}
/**
* @inheritDoc
*/
init() {
this.editor.editing.view.addObserver( ClickObserver );
/**
* The form view displayed inside of the balloon.
*
* @member {module:link/ui/linkformview~LinkFormView}
*/
this.formView = this._createForm();
/**
* The contextual balloon plugin instance.
*
* @private
* @member {module:ui/panel/balloon/contextualballoon~ContextualBalloon}
*/
this._balloon = this.editor.plugins.get( ContextualBalloon );
// Create toolbar buttons.
this._createToolbarLinkButton();
this._createToolbarUnlinkButton();
// Attach lifecycle actions to the the balloon.
this._attachActions();
}
/**
* Creates the {@link module:link/ui/linkformview~LinkFormView} instance.
*
* @private
* @returns {module:link/ui/linkformview~LinkFormView} Link form instance.
*/
_createForm() {
const editor = this.editor;
const formView = new LinkFormView( editor.locale );
formView.urlInputView.bind( 'value' ).to( editor.commands.get( 'link' ), 'value' );
// Execute link command after clicking on formView `Save` button.
this.listenTo( formView, 'submit', () => {
editor.execute( 'link', formView.urlInputView.inputView.element.value );
this._hidePanel( true );
} );
// Execute unlink command after clicking on formView `Unlink` button.
this.listenTo( formView, 'unlink', () => {
editor.execute( 'unlink' );
this._hidePanel( true );
} );
// Hide the panel after clicking on formView `Cancel` button.
this.listenTo( formView, 'cancel', () => this._hidePanel( true ) );
// Close the panel on esc key press when the form has focus.
formView.keystrokes.set( 'Esc', ( data, cancel ) => {
this._hidePanel( true );
cancel();
} );
return formView;
}
/**
* Creates a toolbar link button. Clicking this button will show
* {@link #_balloon} attached to the selection.
*
* @private
*/
_createToolbarLinkButton() {
const editor = this.editor;
const linkCommand = editor.commands.get( 'link' );
const t = editor.t;
// Handle `Ctrl+K` keystroke and show the panel.
editor.keystrokes.set( 'CTRL+K', () => this._showPanel( true ) );
editor.ui.componentFactory.add( 'link', ( locale ) => {
const button = new ButtonView( locale );
button.isEnabled = true;
button.label = t( 'Link' );
button.icon = linkIcon;
button.keystroke = 'CTRL+K';
button.tooltip = true;
// Bind button to the command.
button.bind( 'isEnabled' ).to( linkCommand, 'isEnabled' );
// Show the panel on button click.
this.listenTo( button, 'execute', () => this._showPanel( true ) );
return button;
} );
}
/**
* Creates a toolbar unlink button. Clicking this button will unlink
* the selected link.
*
* @private
*/
_createToolbarUnlinkButton() {
const editor = this.editor;
const t = editor.t;
const unlinkCommand = editor.commands.get( 'unlink' );
editor.ui.componentFactory.add( 'unlink', ( locale ) => {
const button = new ButtonView( locale );
button.isEnabled = false;
button.label = t( 'Unlink' );
button.icon = unlinkIcon;
button.tooltip = true;
// Bind button to the command.
button.bind( 'isEnabled' ).to( unlinkCommand, 'isEnabled' );
// Execute unlink command and hide panel, if open on button click.
this.listenTo( button, 'execute', () => editor.execute( 'unlink' ) );
return button;
} );
}
/**
* Attaches actions which control whether the balloon panel containing the
* {@link #formView} is visible or not.
*
* @private
*/
_attachActions() {
const viewDocument = this.editor.editing.view;
// Handle click on view document and show panel when selection is placed inside the link element.
// Keep panel open until selection will be inside the same link element.
this.listenTo( viewDocument, 'click', () => {
const viewSelection = viewDocument.selection;
const parentLink = this._getSelectedLinkElement();
// When collapsed selection is inside link element (link element is clicked).
if ( viewSelection.isCollapsed && parentLink ) {
// Then show panel but keep focus inside editor editable.
this._showPanel();
// Avoid duplication of the same listener.
this.stopListening( viewDocument, 'render' );
// Start listen to view document changes and close the panel when selection will be moved
// out of the actual link element.
this.listenTo( viewDocument, 'render', () => {
const currentParentLink = this._getSelectedLinkElement();
if ( !viewSelection.isCollapsed || parentLink !== currentParentLink ) {
this._hidePanel();
} else {
this._balloon.updatePosition();
}
} );
}
} );
// Focus the form if the balloon is visible and the Tab key has been pressed.
this.editor.keystrokes.set( 'Tab', ( data, cancel ) => {
if ( this._balloon.visibleView === this.formView && !this.formView.focusTracker.isFocused ) {
this.formView.focus();
cancel();
}
} );
// Close the panel on the Esc key press when the editable has focus and the balloon is visible.
this.editor.keystrokes.set( 'Esc', ( data, cancel ) => {
if ( this._balloon.visibleView === this.formView ) {
this._hidePanel();
cancel();
}
} );
// Close on click outside of balloon panel element.
clickOutsideHandler( {
emitter: this.formView,
activator: () => this._balloon.hasView( this.formView ),
contextElement: this._balloon.view.element,
callback: () => this._hidePanel()
} );
}
/**
* Adds the {@link #formView} to the {@link #_balloon}.
* When view is already added then try to focus it `focusInput` parameter is set as true.
*
* @protected
* @param {Boolean} [focusInput=false] When `true`, link form will be focused on panel show.
* @return {Promise} A promise resolved when the {@link #formView} {@link module:ui/view~View#init} is done.
*/
_showPanel( focusInput ) {
// https://github.com/ckeditor/ckeditor5-link/issues/53
this.formView.unlinkButtonView.isVisible = !!this._getSelectedLinkElement();
if ( this._balloon.hasView( this.formView ) ) {
// Check if formView should be focused and focus it if is visible.
if ( focusInput && this._balloon.visibleView === this.formView ) {
this.formView.urlInputView.select();
}
return Promise.resolve();
}
return this._balloon.add( {
view: this.formView,
position: this._getBalloonPositionData()
} ).then( () => {
if ( focusInput ) {
this.formView.urlInputView.select();
}
} );
}
/**
* Removes the {@link #formView} from the {@link #_balloon}.
*
* See {@link #_showPanel}.
*
* @protected
* @param {Boolean} [focusEditable=false] When `true`, editable focus will be restored on panel hide.
*/
_hidePanel( focusEditable ) {
if ( !this._balloon.hasView( this.formView ) ) {
return;
}
if ( focusEditable ) {
this.editor.editing.view.focus();
}
this.stopListening( this.editor.editing.view, 'render' );
this._balloon.remove( this.formView );
}
/**
* Returns positioning options for the {@link #_balloon}. They control the way balloon is attached
* to the target element or selection.
*
* If the selection is collapsed and inside a link element, then the panel will be attached to the
* entire link element. Otherwise, it will be attached to the selection.
*
* @private
* @returns {module:utils/dom/position~Options}
*/
_getBalloonPositionData() {
const viewDocument = this.editor.editing.view;
const targetLink = this._getSelectedLinkElement();
const target = targetLink ?
// When selection is inside link element, then attach panel to this element.
viewDocument.domConverter.getCorrespondingDomElement( targetLink )
:
// Otherwise attach panel to the selection.
viewDocument.domConverter.viewRangeToDom( viewDocument.selection.getFirstRange() );
return {
target,
limiter: viewDocument.domConverter.getCorrespondingDomElement( viewDocument.selection.editableElement )
};
}
/**
* Returns the {@link module:link/linkelement~LinkElement} at the first
* {@link module:engine/model/position~Position} of
* {@link module:engine/view/document~Document editing view's} selection or `null`
* if there's none.
*
* @private
* @returns {module:link/linkelement~LinkElement|null}
*/
_getSelectedLinkElement() {
return this.editor.editing.view
.selection
.getFirstPosition()
.parent
.getAncestors()
.find( ancestor => ancestor instanceof LinkElement );
}
}