@ckeditor/ckeditor5-widget 35.2.0 → 35.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,47 +2,24 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /* global DOMParser */
7
-
8
6
  /**
9
7
  * @module widget/widgettypearound
10
8
  */
11
-
12
9
  import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
13
10
  import Template from '@ckeditor/ckeditor5-ui/src/template';
14
11
  import Enter from '@ckeditor/ckeditor5-enter/src/enter';
15
12
  import Delete from '@ckeditor/ckeditor5-typing/src/delete';
16
- import {
17
- isForwardArrowKeyCode,
18
- keyCodes
19
- } from '@ckeditor/ckeditor5-utils/src/keyboard';
20
-
21
- import {
22
- isTypeAroundWidget,
23
- getClosestTypeAroundDomButton,
24
- getTypeAroundButtonPosition,
25
- getClosestWidgetViewElement,
26
- getTypeAroundFakeCaretPosition,
27
- TYPE_AROUND_SELECTION_ATTRIBUTE
28
- } from './utils';
29
-
30
- import {
31
- isNonTypingKeystroke
32
- } from '@ckeditor/ckeditor5-typing/src/utils/injectunsafekeystrokeshandling';
33
-
13
+ import { isForwardArrowKeyCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
14
+ import { isTypeAroundWidget, getClosestTypeAroundDomButton, getTypeAroundButtonPosition, getClosestWidgetViewElement, getTypeAroundFakeCaretPosition, TYPE_AROUND_SELECTION_ATTRIBUTE } from './utils';
34
15
  import { isWidget } from '../utils';
35
-
36
16
  import returnIcon from '../../theme/icons/return-arrow.svg';
37
17
  import '../../theme/widgettypearound.css';
38
-
39
- const POSSIBLE_INSERTION_POSITIONS = [ 'before', 'after' ];
40
-
18
+ import env from '@ckeditor/ckeditor5-utils/src/env';
19
+ const POSSIBLE_INSERTION_POSITIONS = ['before', 'after'];
41
20
  // Do the SVG parsing once and then clone the result <svg> DOM element for each new button.
42
- const RETURN_ARROW_ICON_ELEMENT = new DOMParser().parseFromString( returnIcon, 'image/svg+xml' ).firstChild;
43
-
21
+ const RETURN_ARROW_ICON_ELEMENT = new DOMParser().parseFromString(returnIcon, 'image/svg+xml').firstChild;
44
22
  const PLUGIN_DISABLED_EDITING_ROOT_CLASS = 'ck-widget__type-around_disabled';
45
-
46
23
  /**
47
24
  * A plugin that allows users to type around widgets where normally it is impossible to place the caret due
48
25
  * to limitations of web browsers. These "tight spots" occur, for instance, before (or after) a widget being
@@ -56,858 +33,756 @@ const PLUGIN_DISABLED_EDITING_ROOT_CLASS = 'ck-widget__type-around_disabled';
56
33
  * @extends module:core/plugin~Plugin
57
34
  */
58
35
  export default class WidgetTypeAround extends Plugin {
59
- /**
60
- * @inheritDoc
61
- */
62
- static get pluginName() {
63
- return 'WidgetTypeAround';
64
- }
65
-
66
- /**
67
- * @inheritDoc
68
- */
69
- static get requires() {
70
- return [ Enter, Delete ];
71
- }
72
-
73
- /**
74
- * @inheritDoc
75
- */
76
- constructor( editor ) {
77
- super( editor );
78
-
79
- /**
80
- * A reference to the model widget element that has the fake caret active
81
- * on either side of it. It is later used to remove CSS classes associated with the fake caret
82
- * when the widget no longer needs it.
83
- *
84
- * @private
85
- * @member {module:engine/model/element~Element|null}
86
- */
87
- this._currentFakeCaretModelElement = null;
88
- }
89
-
90
- /**
91
- * @inheritDoc
92
- */
93
- init() {
94
- const editor = this.editor;
95
- const editingView = editor.editing.view;
96
-
97
- // Set a CSS class on the view editing root when the plugin is disabled so all the buttons
98
- // and lines visually disappear. All the interactions are disabled in individual plugin methods.
99
- this.on( 'change:isEnabled', ( evt, data, isEnabled ) => {
100
- editingView.change( writer => {
101
- for ( const root of editingView.document.roots ) {
102
- if ( isEnabled ) {
103
- writer.removeClass( PLUGIN_DISABLED_EDITING_ROOT_CLASS, root );
104
- } else {
105
- writer.addClass( PLUGIN_DISABLED_EDITING_ROOT_CLASS, root );
106
- }
107
- }
108
- } );
109
-
110
- if ( !isEnabled ) {
111
- editor.model.change( writer => {
112
- writer.removeSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE );
113
- } );
114
- }
115
- } );
116
-
117
- this._enableTypeAroundUIInjection();
118
- this._enableInsertingParagraphsOnButtonClick();
119
- this._enableInsertingParagraphsOnEnterKeypress();
120
- this._enableInsertingParagraphsOnTypingKeystroke();
121
- this._enableTypeAroundFakeCaretActivationUsingKeyboardArrows();
122
- this._enableDeleteIntegration();
123
- this._enableInsertContentIntegration();
124
- this._enableInsertObjectIntegration();
125
- this._enableDeleteContentIntegration();
126
- }
127
-
128
- /**
129
- * @inheritDoc
130
- */
131
- destroy() {
132
- this._currentFakeCaretModelElement = null;
133
- }
134
-
135
- /**
136
- * Inserts a new paragraph next to a widget element with the selection anchored in it.
137
- *
138
- * **Note**: This method is heavily user-oriented and will both focus the editing view and scroll
139
- * the viewport to the selection in the inserted paragraph.
140
- *
141
- * @protected
142
- * @param {module:engine/model/element~Element} widgetModelElement The model widget element next to which a paragraph is inserted.
143
- * @param {'before'|'after'} position The position where the paragraph is inserted. Either `'before'` or `'after'` the widget.
144
- */
145
- _insertParagraph( widgetModelElement, position ) {
146
- const editor = this.editor;
147
- const editingView = editor.editing.view;
148
-
149
- const attributesToCopy = editor.model.schema.getAttributesWithProperty( widgetModelElement, 'copyOnReplace', true );
150
-
151
- editor.execute( 'insertParagraph', {
152
- position: editor.model.createPositionAt( widgetModelElement, position ),
153
- attributes: attributesToCopy
154
- } );
155
-
156
- editingView.focus();
157
- editingView.scrollToTheSelection();
158
- }
159
-
160
- /**
161
- * A wrapper for the {@link module:utils/emittermixin~EmitterMixin#listenTo} method that executes the callbacks only
162
- * when the plugin {@link #isEnabled is enabled}.
163
- *
164
- * @private
165
- * @param {module:utils/emittermixin~Emitter} emitter The object that fires the event.
166
- * @param {String} event The name of the event.
167
- * @param {Function} callback The function to be called on event.
168
- * @param {Object} [options={}] Additional options.
169
- * @param {module:utils/priorities~PriorityString|Number} [options.priority='normal'] The priority of this event callback. The higher
170
- * the priority value the sooner the callback will be fired. Events having the same priority are called in the
171
- * order they were added.
172
- */
173
- _listenToIfEnabled( emitter, event, callback, options ) {
174
- this.listenTo( emitter, event, ( ...args ) => {
175
- // Do not respond if the plugin is disabled.
176
- if ( this.isEnabled ) {
177
- callback( ...args );
178
- }
179
- }, options );
180
- }
181
-
182
- /**
183
- * Similar to {@link #_insertParagraph}, this method inserts a paragraph except that it
184
- * does not expect a position. Instead, it performs the insertion next to a selected widget
185
- * according to the `widget-type-around` model selection attribute value (fake caret position).
186
- *
187
- * Because this method requires the `widget-type-around` attribute to be set,
188
- * the insertion can only happen when the widget's fake caret is active (e.g. activated
189
- * using the keyboard).
190
- *
191
- * @private
192
- * @returns {Boolean} Returns `true` when the paragraph was inserted (the attribute was present) and `false` otherwise.
193
- */
194
- _insertParagraphAccordingToFakeCaretPosition() {
195
- const editor = this.editor;
196
- const model = editor.model;
197
- const modelSelection = model.document.selection;
198
- const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition( modelSelection );
199
-
200
- if ( !typeAroundFakeCaretPosition ) {
201
- return false;
202
- }
203
-
204
- const selectedModelElement = modelSelection.getSelectedElement();
205
-
206
- this._insertParagraph( selectedModelElement, typeAroundFakeCaretPosition );
207
-
208
- return true;
209
- }
210
-
211
- /**
212
- * Creates a listener in the editing conversion pipeline that injects the widget type around
213
- * UI into every single widget instance created in the editor.
214
- *
215
- * The UI is delivered as a {@link module:engine/view/uielement~UIElement}
216
- * wrapper which renders DOM buttons that users can use to insert paragraphs.
217
- *
218
- * @private
219
- */
220
- _enableTypeAroundUIInjection() {
221
- const editor = this.editor;
222
- const schema = editor.model.schema;
223
- const t = editor.locale.t;
224
- const buttonTitles = {
225
- before: t( 'Insert paragraph before block' ),
226
- after: t( 'Insert paragraph after block' )
227
- };
228
-
229
- editor.editing.downcastDispatcher.on( 'insert', ( evt, data, conversionApi ) => {
230
- const viewElement = conversionApi.mapper.toViewElement( data.item );
231
-
232
- // Filter out non-widgets and inline widgets.
233
- if ( isTypeAroundWidget( viewElement, data.item, schema ) ) {
234
- injectUIIntoWidget( conversionApi.writer, buttonTitles, viewElement );
235
- }
236
- }, { priority: 'low' } );
237
- }
238
-
239
- /**
240
- * Brings support for the fake caret that appears when either:
241
- *
242
- * * the selection moves to a widget from a position next to it using arrow keys,
243
- * * the arrow key is pressed when the widget is already selected.
244
- *
245
- * The fake caret lets the user know that they can start typing or just press
246
- * <kbd>Enter</kbd> to insert a paragraph at the position next to a widget as suggested by the fake caret.
247
- *
248
- * The fake caret disappears when the user changes the selection or the editor
249
- * gets blurred.
250
- *
251
- * The whole idea is as follows:
252
- *
253
- * 1. A user does one of the 2 scenarios described at the beginning.
254
- * 2. The "keydown" listener is executed and the decision is made whether to show or hide the fake caret.
255
- * 3. If it should show up, the `widget-type-around` model selection attribute is set indicating
256
- * on which side of the widget it should appear.
257
- * 4. The selection dispatcher reacts to the selection attribute and sets CSS classes responsible for the
258
- * fake caret on the view widget.
259
- * 5. If the fake caret should disappear, the selection attribute is removed and the dispatcher
260
- * does the CSS class clean-up in the view.
261
- * 6. Additionally, `change:range` and `FocusTracker#isFocused` listeners also remove the selection
262
- * attribute (the former also removes widget CSS classes).
263
- *
264
- * @private
265
- */
266
- _enableTypeAroundFakeCaretActivationUsingKeyboardArrows() {
267
- const editor = this.editor;
268
- const model = editor.model;
269
- const modelSelection = model.document.selection;
270
- const schema = model.schema;
271
- const editingView = editor.editing.view;
272
-
273
- // This is the main listener responsible for the fake caret.
274
- // Note: The priority must precede the default Widget class keydown handler ("high").
275
- this._listenToIfEnabled( editingView.document, 'arrowKey', ( evt, domEventData ) => {
276
- this._handleArrowKeyPress( evt, domEventData );
277
- }, { context: [ isWidget, '$text' ], priority: 'high' } );
278
-
279
- // This listener makes sure the widget type around selection attribute will be gone from the model
280
- // selection as soon as the model range changes. This attribute only makes sense when a widget is selected
281
- // (and the "fake horizontal caret" is visible) so whenever the range changes (e.g. selection moved somewhere else),
282
- // let's get rid of the attribute so that the selection downcast dispatcher isn't even bothered.
283
- this._listenToIfEnabled( modelSelection, 'change:range', ( evt, data ) => {
284
- // Do not reset the selection attribute when the change was indirect.
285
- if ( !data.directChange ) {
286
- return;
287
- }
288
-
289
- // Get rid of the widget type around attribute of the selection on every change:range.
290
- // If the range changes, it means for sure, the user is no longer in the active ("fake horizontal caret") mode.
291
- editor.model.change( writer => {
292
- writer.removeSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE );
293
- } );
294
- } );
295
-
296
- // Get rid of the widget type around attribute of the selection on every document change
297
- // that makes widget not selected any more (i.e. widget was removed).
298
- this._listenToIfEnabled( model.document, 'change:data', () => {
299
- const selectedModelElement = modelSelection.getSelectedElement();
300
-
301
- if ( selectedModelElement ) {
302
- const selectedViewElement = editor.editing.mapper.toViewElement( selectedModelElement );
303
-
304
- if ( isTypeAroundWidget( selectedViewElement, selectedModelElement, schema ) ) {
305
- return;
306
- }
307
- }
308
-
309
- editor.model.change( writer => {
310
- writer.removeSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE );
311
- } );
312
- } );
313
-
314
- // React to changes of the model selection attribute made by the arrow keys listener.
315
- // If the block widget is selected and the attribute changes, downcast the attribute to special
316
- // CSS classes associated with the active ("fake horizontal caret") mode of the widget.
317
- this._listenToIfEnabled( editor.editing.downcastDispatcher, 'selection', ( evt, data, conversionApi ) => {
318
- const writer = conversionApi.writer;
319
-
320
- if ( this._currentFakeCaretModelElement ) {
321
- const selectedViewElement = conversionApi.mapper.toViewElement( this._currentFakeCaretModelElement );
322
-
323
- if ( selectedViewElement ) {
324
- // Get rid of CSS classes associated with the active ("fake horizontal caret") mode from the view widget.
325
- writer.removeClass( POSSIBLE_INSERTION_POSITIONS.map( positionToWidgetCssClass ), selectedViewElement );
326
-
327
- this._currentFakeCaretModelElement = null;
328
- }
329
- }
330
-
331
- const selectedModelElement = data.selection.getSelectedElement();
332
-
333
- if ( !selectedModelElement ) {
334
- return;
335
- }
336
-
337
- const selectedViewElement = conversionApi.mapper.toViewElement( selectedModelElement );
338
-
339
- if ( !isTypeAroundWidget( selectedViewElement, selectedModelElement, schema ) ) {
340
- return;
341
- }
342
-
343
- const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition( data.selection );
344
-
345
- if ( !typeAroundFakeCaretPosition ) {
346
- return;
347
- }
348
-
349
- writer.addClass( positionToWidgetCssClass( typeAroundFakeCaretPosition ), selectedViewElement );
350
-
351
- // Remember the view widget that got the "fake-caret" CSS class. This class should be removed ASAP when the
352
- // selection changes
353
- this._currentFakeCaretModelElement = selectedModelElement;
354
- } );
355
-
356
- this._listenToIfEnabled( editor.ui.focusTracker, 'change:isFocused', ( evt, name, isFocused ) => {
357
- if ( !isFocused ) {
358
- editor.model.change( writer => {
359
- writer.removeSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE );
360
- } );
361
- }
362
- } );
363
-
364
- function positionToWidgetCssClass( position ) {
365
- return `ck-widget_type-around_show-fake-caret_${ position }`;
366
- }
367
- }
368
-
369
- /**
370
- * A listener executed on each "keydown" in the view document, a part of
371
- * {@link #_enableTypeAroundFakeCaretActivationUsingKeyboardArrows}.
372
- *
373
- * It decides whether the arrow keypress should activate the fake caret or not (also whether it should
374
- * be deactivated).
375
- *
376
- * The fake caret activation is done by setting the `widget-type-around` model selection attribute
377
- * in this listener, and stopping and preventing the event that would normally be handled by the widget
378
- * plugin that is responsible for the regular keyboard navigation near/across all widgets (that
379
- * includes inline widgets, which are ignored by the widget type around plugin).
380
- *
381
- * @private
382
- */
383
- _handleArrowKeyPress( evt, domEventData ) {
384
- const editor = this.editor;
385
- const model = editor.model;
386
- const modelSelection = model.document.selection;
387
- const schema = model.schema;
388
- const editingView = editor.editing.view;
389
-
390
- const keyCode = domEventData.keyCode;
391
- const isForward = isForwardArrowKeyCode( keyCode, editor.locale.contentLanguageDirection );
392
- const selectedViewElement = editingView.document.selection.getSelectedElement();
393
- const selectedModelElement = editor.editing.mapper.toModelElement( selectedViewElement );
394
- let shouldStopAndPreventDefault;
395
-
396
- // Handle keyboard navigation when a type-around-compatible widget is currently selected.
397
- if ( isTypeAroundWidget( selectedViewElement, selectedModelElement, schema ) ) {
398
- shouldStopAndPreventDefault = this._handleArrowKeyPressOnSelectedWidget( isForward );
399
- }
400
- // Handle keyboard arrow navigation when the selection is next to a type-around-compatible widget
401
- // and the widget is about to be selected.
402
- else if ( modelSelection.isCollapsed ) {
403
- shouldStopAndPreventDefault = this._handleArrowKeyPressWhenSelectionNextToAWidget( isForward );
404
- }
405
- // Handle collapsing a non-collapsed selection that is wider than on a single widget.
406
- else if ( !domEventData.shiftKey ) {
407
- shouldStopAndPreventDefault = this._handleArrowKeyPressWhenNonCollapsedSelection( isForward );
408
- }
409
-
410
- if ( shouldStopAndPreventDefault ) {
411
- domEventData.preventDefault();
412
- evt.stop();
413
- }
414
- }
415
-
416
- /**
417
- * Handles the keyboard navigation on "keydown" when a widget is currently selected and activates or deactivates
418
- * the fake caret for that widget, depending on the current value of the `widget-type-around` model
419
- * selection attribute and the direction of the pressed arrow key.
420
- *
421
- * @private
422
- * @param {Boolean} isForward `true` when the pressed arrow key was responsible for the forward model selection movement
423
- * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
424
- * @returns {Boolean} Returns `true` when the keypress was handled and no other keydown listener of the editor should
425
- * process the event any further. Returns `false` otherwise.
426
- */
427
- _handleArrowKeyPressOnSelectedWidget( isForward ) {
428
- const editor = this.editor;
429
- const model = editor.model;
430
- const modelSelection = model.document.selection;
431
- const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition( modelSelection );
432
-
433
- return model.change( writer => {
434
- // If the fake caret is displayed...
435
- if ( typeAroundFakeCaretPosition ) {
436
- const isLeavingWidget = typeAroundFakeCaretPosition === ( isForward ? 'after' : 'before' );
437
-
438
- // If the keyboard arrow works against the value of the selection attribute...
439
- // then remove the selection attribute but prevent default DOM actions
440
- // and do not let the Widget plugin listener move the selection. This brings
441
- // the widget back to the state, for instance, like if was selected using the mouse.
442
- //
443
- // **Note**: If leaving the widget when the fake caret is active, then the default
444
- // Widget handler will change the selection and, in turn, this will automatically discard
445
- // the selection attribute.
446
- if ( !isLeavingWidget ) {
447
- writer.removeSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE );
448
-
449
- return true;
450
- }
451
- }
452
- // If the fake caret wasn't displayed, let's set it now according to the direction of the arrow
453
- // key press. This also means we cannot let the Widget plugin listener move the selection.
454
- else {
455
- writer.setSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE, isForward ? 'after' : 'before' );
456
-
457
- return true;
458
- }
459
-
460
- return false;
461
- } );
462
- }
463
-
464
- /**
465
- * Handles the keyboard navigation on "keydown" when **no** widget is selected but the selection is **directly** next
466
- * to one and upon the fake caret should become active for this widget upon arrow keypress
467
- * (AKA entering/selecting the widget).
468
- *
469
- * **Note**: This code mirrors the implementation from the widget plugin but also adds the selection attribute.
470
- * Unfortunately, there is no safe way to let the widget plugin do the selection part first and then just set the
471
- * selection attribute here in the widget type around plugin. This is why this code must duplicate some from the widget plugin.
472
- *
473
- * @private
474
- * @param {Boolean} isForward `true` when the pressed arrow key was responsible for the forward model selection movement
475
- * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
476
- * @returns {Boolean} Returns `true` when the keypress was handled and no other keydown listener of the editor should
477
- * process the event any further. Returns `false` otherwise.
478
- */
479
- _handleArrowKeyPressWhenSelectionNextToAWidget( isForward ) {
480
- const editor = this.editor;
481
- const model = editor.model;
482
- const schema = model.schema;
483
- const widgetPlugin = editor.plugins.get( 'Widget' );
484
-
485
- // This is the widget the selection is about to be set on.
486
- const modelElementNextToSelection = widgetPlugin._getObjectElementNextToSelection( isForward );
487
- const viewElementNextToSelection = editor.editing.mapper.toViewElement( modelElementNextToSelection );
488
-
489
- if ( isTypeAroundWidget( viewElementNextToSelection, modelElementNextToSelection, schema ) ) {
490
- model.change( writer => {
491
- widgetPlugin._setSelectionOverElement( modelElementNextToSelection );
492
- writer.setSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE, isForward ? 'before' : 'after' );
493
- } );
494
-
495
- // The change() block above does the same job as the Widget plugin. The event can
496
- // be safely canceled.
497
- return true;
498
- }
499
-
500
- return false;
501
- }
502
-
503
- /**
504
- * Handles the keyboard navigation on "keydown" when a widget is currently selected (together with some other content)
505
- * and the widget is the first or last element in the selection. It activates or deactivates the fake caret for that widget.
506
- *
507
- * @private
508
- * @param {Boolean} isForward `true` when the pressed arrow key was responsible for the forward model selection movement
509
- * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
510
- * @returns {Boolean} Returns `true` when the keypress was handled and no other keydown listener of the editor should
511
- * process the event any further. Returns `false` otherwise.
512
- */
513
- _handleArrowKeyPressWhenNonCollapsedSelection( isForward ) {
514
- const editor = this.editor;
515
- const model = editor.model;
516
- const schema = model.schema;
517
- const mapper = editor.editing.mapper;
518
- const modelSelection = model.document.selection;
519
-
520
- const selectedModelNode = isForward ?
521
- modelSelection.getLastPosition().nodeBefore :
522
- modelSelection.getFirstPosition().nodeAfter;
523
-
524
- const selectedViewNode = mapper.toViewElement( selectedModelNode );
525
-
526
- // There is a widget at the collapse position so collapse the selection to the fake caret on it.
527
- if ( isTypeAroundWidget( selectedViewNode, selectedModelNode, schema ) ) {
528
- model.change( writer => {
529
- writer.setSelection( selectedModelNode, 'on' );
530
- writer.setSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE, isForward ? 'after' : 'before' );
531
- } );
532
-
533
- return true;
534
- }
535
-
536
- return false;
537
- }
538
-
539
- /**
540
- * Registers a `mousedown` listener for the view document which intercepts events
541
- * coming from the widget type around UI, which happens when a user clicks one of the buttons
542
- * that insert a paragraph next to a widget.
543
- *
544
- * @private
545
- */
546
- _enableInsertingParagraphsOnButtonClick() {
547
- const editor = this.editor;
548
- const editingView = editor.editing.view;
549
-
550
- this._listenToIfEnabled( editingView.document, 'mousedown', ( evt, domEventData ) => {
551
- const button = getClosestTypeAroundDomButton( domEventData.domTarget );
552
-
553
- if ( !button ) {
554
- return;
555
- }
556
-
557
- const buttonPosition = getTypeAroundButtonPosition( button );
558
- const widgetViewElement = getClosestWidgetViewElement( button, editingView.domConverter );
559
- const widgetModelElement = editor.editing.mapper.toModelElement( widgetViewElement );
560
-
561
- this._insertParagraph( widgetModelElement, buttonPosition );
562
-
563
- domEventData.preventDefault();
564
- evt.stop();
565
- } );
566
- }
567
-
568
- /**
569
- * Creates the <kbd>Enter</kbd> key listener on the view document that allows the user to insert a paragraph
570
- * near the widget when either:
571
- *
572
- * * The fake caret was first activated using the arrow keys,
573
- * * The entire widget is selected in the model.
574
- *
575
- * In the first case, the new paragraph is inserted according to the `widget-type-around` selection
576
- * attribute (see {@link #_handleArrowKeyPress}).
577
- *
578
- * In the second case, the new paragraph is inserted based on whether a soft (<kbd>Shift</kbd>+<kbd>Enter</kbd>) keystroke
579
- * was pressed or not.
580
- *
581
- * @private
582
- */
583
- _enableInsertingParagraphsOnEnterKeypress() {
584
- const editor = this.editor;
585
- const selection = editor.model.document.selection;
586
- const editingView = editor.editing.view;
587
-
588
- this._listenToIfEnabled( editingView.document, 'enter', ( evt, domEventData ) => {
589
- // This event could be triggered from inside the widget but we are interested
590
- // only when the widget is selected itself.
591
- if ( evt.eventPhase != 'atTarget' ) {
592
- return;
593
- }
594
-
595
- const selectedModelElement = selection.getSelectedElement();
596
- const selectedViewElement = editor.editing.mapper.toViewElement( selectedModelElement );
597
-
598
- const schema = editor.model.schema;
599
- let wasHandled;
600
-
601
- // First check if the widget is selected and there's a type around selection attribute associated
602
- // with the fake caret that would tell where to insert a new paragraph.
603
- if ( this._insertParagraphAccordingToFakeCaretPosition() ) {
604
- wasHandled = true;
605
- }
606
- // Then, if there is no selection attribute associated with the fake caret, check if the widget
607
- // simply is selected and create a new paragraph according to the keystroke (Shift+)Enter.
608
- else if ( isTypeAroundWidget( selectedViewElement, selectedModelElement, schema ) ) {
609
- this._insertParagraph( selectedModelElement, domEventData.isSoft ? 'before' : 'after' );
610
-
611
- wasHandled = true;
612
- }
613
-
614
- if ( wasHandled ) {
615
- domEventData.preventDefault();
616
- evt.stop();
617
- }
618
- }, { context: isWidget } );
619
- }
620
-
621
- /**
622
- * Similar to the {@link #_enableInsertingParagraphsOnEnterKeypress}, it allows the user
623
- * to insert a paragraph next to a widget when the fake caret was activated using arrow
624
- * keys but it responds to typing keystrokes instead of <kbd>Enter</kbd>.
625
- *
626
- * "Typing keystrokes" are keystrokes that insert new content into the document,
627
- * for instance, letters ("a") or numbers ("4"). The "keydown" listener enabled by this method
628
- * will insert a new paragraph according to the `widget-type-around` model selection attribute
629
- * as the user simply starts typing, which creates the impression that the fake caret
630
- * behaves like a real one rendered by the browser (AKA your text appears where the caret was).
631
- *
632
- * **Note**: At the moment this listener creates 2 undo steps: one for the `insertParagraph` command
633
- * and another one for actual typing. It is not a disaster but this may need to be fixed
634
- * sooner or later.
635
- *
636
- * Learn more in {@link module:typing/utils/injectunsafekeystrokeshandling}.
637
- *
638
- * @private
639
- */
640
- _enableInsertingParagraphsOnTypingKeystroke() {
641
- const editor = this.editor;
642
- const editingView = editor.editing.view;
643
- const keyCodesHandledSomewhereElse = [
644
- keyCodes.enter,
645
- keyCodes.delete,
646
- keyCodes.backspace
647
- ];
648
-
649
- // Note: The priority must precede the default observers.
650
- this._listenToIfEnabled( editingView.document, 'keydown', ( evt, domEventData ) => {
651
- // Don't handle enter/backspace/delete here. They are handled in dedicated listeners.
652
- if ( !keyCodesHandledSomewhereElse.includes( domEventData.keyCode ) && !isNonTypingKeystroke( domEventData ) ) {
653
- this._insertParagraphAccordingToFakeCaretPosition();
654
- }
655
- }, { priority: 'high' } );
656
- }
657
-
658
- /**
659
- * It creates a "delete" event listener on the view document to handle cases when the <kbd>Delete</kbd> or <kbd>Backspace</kbd>
660
- * is pressed and the fake caret is currently active.
661
- *
662
- * The fake caret should create an illusion of a real browser caret so that when it appears before or after
663
- * a widget, pressing <kbd>Delete</kbd> or <kbd>Backspace</kbd> should remove a widget or delete the content
664
- * before or after a widget (depending on the content surrounding the widget).
665
- *
666
- * @private
667
- */
668
- _enableDeleteIntegration() {
669
- const editor = this.editor;
670
- const editingView = editor.editing.view;
671
- const model = editor.model;
672
- const schema = model.schema;
673
-
674
- this._listenToIfEnabled( editingView.document, 'delete', ( evt, domEventData ) => {
675
- // This event could be triggered from inside the widget but we are interested
676
- // only when the widget is selected itself.
677
- if ( evt.eventPhase != 'atTarget' ) {
678
- return;
679
- }
680
-
681
- const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition( model.document.selection );
682
-
683
- // This listener handles only these cases when the fake caret is active.
684
- if ( !typeAroundFakeCaretPosition ) {
685
- return;
686
- }
687
-
688
- const direction = domEventData.direction;
689
- const selectedModelWidget = model.document.selection.getSelectedElement();
690
-
691
- const isFakeCaretBefore = typeAroundFakeCaretPosition === 'before';
692
- const isDeleteForward = direction == 'forward';
693
- const shouldDeleteEntireWidget = isFakeCaretBefore === isDeleteForward;
694
-
695
- if ( shouldDeleteEntireWidget ) {
696
- editor.execute( 'delete', {
697
- selection: model.createSelection( selectedModelWidget, 'on' )
698
- } );
699
- } else {
700
- const range = schema.getNearestSelectionRange(
701
- model.createPositionAt( selectedModelWidget, typeAroundFakeCaretPosition ),
702
- direction
703
- );
704
-
705
- // If there is somewhere to move selection to, then there will be something to delete.
706
- if ( range ) {
707
- // If the range is NOT collapsed, then we know that the range contains an object (see getNearestSelectionRange() docs).
708
- if ( !range.isCollapsed ) {
709
- model.change( writer => {
710
- writer.setSelection( range );
711
- editor.execute( isDeleteForward ? 'deleteForward' : 'delete' );
712
- } );
713
- } else {
714
- const probe = model.createSelection( range.start );
715
- model.modifySelection( probe, { direction } );
716
-
717
- // If the range is collapsed, let's see if a non-collapsed range exists that can could be deleted.
718
- // If such range exists, use the editor command because it it safe for collaboration (it merges where it can).
719
- if ( !probe.focus.isEqual( range.start ) ) {
720
- model.change( writer => {
721
- writer.setSelection( range );
722
- editor.execute( isDeleteForward ? 'deleteForward' : 'delete' );
723
- } );
724
- }
725
- // If there is no non-collapsed range to be deleted then we are sure that there is an empty element
726
- // next to a widget that should be removed. "delete" and "deleteForward" commands cannot get rid of it
727
- // so calling Model#deleteContent here manually.
728
- else {
729
- const deepestEmptyRangeAncestor = getDeepestEmptyElementAncestor( schema, range.start.parent );
730
-
731
- model.deleteContent( model.createSelection( deepestEmptyRangeAncestor, 'on' ), {
732
- doNotAutoparagraph: true
733
- } );
734
- }
735
- }
736
- }
737
- }
738
-
739
- // If some content was deleted, don't let the handler from the Widget plugin kick in.
740
- // If nothing was deleted, then the default handler will have nothing to do anyway.
741
- domEventData.preventDefault();
742
- evt.stop();
743
- }, { context: isWidget } );
744
- }
745
-
746
- /**
747
- * Attaches the {@link module:engine/model/model~Model#event:insertContent} event listener that, for instance, allows the user to paste
748
- * content near a widget when the fake caret is first activated using the arrow keys.
749
- *
750
- * The content is inserted according to the `widget-type-around` selection attribute (see {@link #_handleArrowKeyPress}).
751
- *
752
- * @private
753
- */
754
- _enableInsertContentIntegration() {
755
- const editor = this.editor;
756
- const model = this.editor.model;
757
- const documentSelection = model.document.selection;
758
-
759
- this._listenToIfEnabled( editor.model, 'insertContent', ( evt, [ content, selectable ] ) => {
760
- if ( selectable && !selectable.is( 'documentSelection' ) ) {
761
- return;
762
- }
763
-
764
- const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition( documentSelection );
765
-
766
- if ( !typeAroundFakeCaretPosition ) {
767
- return;
768
- }
769
-
770
- evt.stop();
771
-
772
- return model.change( writer => {
773
- const selectedElement = documentSelection.getSelectedElement();
774
- const position = model.createPositionAt( selectedElement, typeAroundFakeCaretPosition );
775
- const selection = writer.createSelection( position );
776
-
777
- const result = model.insertContent( content, selection );
778
-
779
- writer.setSelection( selection );
780
-
781
- return result;
782
- } );
783
- }, { priority: 'high' } );
784
- }
785
-
786
- /**
787
- * Attaches the {@link module:engine/model/model~Model#event:insertObject} event listener that modifies the
788
- * `options.findOptimalPosition`parameter to position of fake caret in relation to selected element
789
- * to reflect user's intent of desired insertion position.
790
- *
791
- * The object is inserted according to the `widget-type-around` selection attribute (see {@link #_handleArrowKeyPress}).
792
- *
793
- * @private
794
- */
795
- _enableInsertObjectIntegration() {
796
- const editor = this.editor;
797
- const model = this.editor.model;
798
- const documentSelection = model.document.selection;
799
-
800
- this._listenToIfEnabled( editor.model, 'insertObject', ( evt, args ) => {
801
- const [ , selectable, , options = {} ] = args;
802
-
803
- if ( selectable && !selectable.is( 'documentSelection' ) ) {
804
- return;
805
- }
806
-
807
- const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition( documentSelection );
808
-
809
- if ( !typeAroundFakeCaretPosition ) {
810
- return;
811
- }
812
-
813
- options.findOptimalPosition = typeAroundFakeCaretPosition;
814
- args[ 3 ] = options;
815
- }, { priority: 'high' } );
816
- }
817
-
818
- /**
819
- * Attaches the {@link module:engine/model/model~Model#event:deleteContent} event listener to block the event when the fake
820
- * caret is active.
821
- *
822
- * This is required for cases that trigger {@link module:engine/model/model~Model#deleteContent `model.deleteContent()`}
823
- * before calling {@link module:engine/model/model~Model#insertContent `model.insertContent()`} like, for instance,
824
- * plain text pasting.
825
- *
826
- * @private
827
- */
828
- _enableDeleteContentIntegration() {
829
- const editor = this.editor;
830
- const model = this.editor.model;
831
- const documentSelection = model.document.selection;
832
-
833
- this._listenToIfEnabled( editor.model, 'deleteContent', ( evt, [ selection ] ) => {
834
- if ( selection && !selection.is( 'documentSelection' ) ) {
835
- return;
836
- }
837
-
838
- const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition( documentSelection );
839
-
840
- // Disable removing the selection content while pasting plain text.
841
- if ( typeAroundFakeCaretPosition ) {
842
- evt.stop();
843
- }
844
- }, { priority: 'high' } );
845
- }
36
+ /**
37
+ * @inheritDoc
38
+ */
39
+ constructor(editor) {
40
+ super(editor);
41
+ /**
42
+ * A reference to the model widget element that has the fake caret active
43
+ * on either side of it. It is later used to remove CSS classes associated with the fake caret
44
+ * when the widget no longer needs it.
45
+ *
46
+ * @private
47
+ * @member {module:engine/model/element~Element|null}
48
+ */
49
+ this._currentFakeCaretModelElement = null;
50
+ }
51
+ /**
52
+ * @inheritDoc
53
+ */
54
+ static get pluginName() {
55
+ return 'WidgetTypeAround';
56
+ }
57
+ /**
58
+ * @inheritDoc
59
+ */
60
+ static get requires() {
61
+ return [Enter, Delete];
62
+ }
63
+ /**
64
+ * @inheritDoc
65
+ */
66
+ init() {
67
+ const editor = this.editor;
68
+ const editingView = editor.editing.view;
69
+ // Set a CSS class on the view editing root when the plugin is disabled so all the buttons
70
+ // and lines visually disappear. All the interactions are disabled in individual plugin methods.
71
+ this.on('change:isEnabled', (evt, data, isEnabled) => {
72
+ editingView.change(writer => {
73
+ for (const root of editingView.document.roots) {
74
+ if (isEnabled) {
75
+ writer.removeClass(PLUGIN_DISABLED_EDITING_ROOT_CLASS, root);
76
+ }
77
+ else {
78
+ writer.addClass(PLUGIN_DISABLED_EDITING_ROOT_CLASS, root);
79
+ }
80
+ }
81
+ });
82
+ if (!isEnabled) {
83
+ editor.model.change(writer => {
84
+ writer.removeSelectionAttribute(TYPE_AROUND_SELECTION_ATTRIBUTE);
85
+ });
86
+ }
87
+ });
88
+ this._enableTypeAroundUIInjection();
89
+ this._enableInsertingParagraphsOnButtonClick();
90
+ this._enableInsertingParagraphsOnEnterKeypress();
91
+ this._enableInsertingParagraphsOnTypingKeystroke();
92
+ this._enableTypeAroundFakeCaretActivationUsingKeyboardArrows();
93
+ this._enableDeleteIntegration();
94
+ this._enableInsertContentIntegration();
95
+ this._enableInsertObjectIntegration();
96
+ this._enableDeleteContentIntegration();
97
+ }
98
+ /**
99
+ * @inheritDoc
100
+ */
101
+ destroy() {
102
+ super.destroy();
103
+ this._currentFakeCaretModelElement = null;
104
+ }
105
+ /**
106
+ * Inserts a new paragraph next to a widget element with the selection anchored in it.
107
+ *
108
+ * **Note**: This method is heavily user-oriented and will both focus the editing view and scroll
109
+ * the viewport to the selection in the inserted paragraph.
110
+ *
111
+ * @protected
112
+ * @param {module:engine/model/element~Element} widgetModelElement The model widget element next to which a paragraph is inserted.
113
+ * @param {'before'|'after'} position The position where the paragraph is inserted. Either `'before'` or `'after'` the widget.
114
+ */
115
+ _insertParagraph(widgetModelElement, position) {
116
+ const editor = this.editor;
117
+ const editingView = editor.editing.view;
118
+ const attributesToCopy = editor.model.schema.getAttributesWithProperty(widgetModelElement, 'copyOnReplace', true);
119
+ editor.execute('insertParagraph', {
120
+ position: editor.model.createPositionAt(widgetModelElement, position),
121
+ attributes: attributesToCopy
122
+ });
123
+ editingView.focus();
124
+ editingView.scrollToTheSelection();
125
+ }
126
+ /**
127
+ * A wrapper for the {@link module:utils/emittermixin~EmitterMixin#listenTo} method that executes the callbacks only
128
+ * when the plugin {@link #isEnabled is enabled}.
129
+ *
130
+ * @private
131
+ * @param {module:utils/emittermixin~Emitter} emitter The object that fires the event.
132
+ * @param {String} event The name of the event.
133
+ * @param {Function} callback The function to be called on event.
134
+ * @param {Object} [options={}] Additional options.
135
+ * @param {module:utils/priorities~PriorityString|Number} [options.priority='normal'] The priority of this event callback. The higher
136
+ * the priority value the sooner the callback will be fired. Events having the same priority are called in the
137
+ * order they were added.
138
+ */
139
+ _listenToIfEnabled(emitter, event, callback, options) {
140
+ this.listenTo(emitter, event, (...args) => {
141
+ // Do not respond if the plugin is disabled.
142
+ if (this.isEnabled) {
143
+ callback(...args);
144
+ }
145
+ }, options);
146
+ }
147
+ /**
148
+ * Similar to {@link #_insertParagraph}, this method inserts a paragraph except that it
149
+ * does not expect a position. Instead, it performs the insertion next to a selected widget
150
+ * according to the `widget-type-around` model selection attribute value (fake caret position).
151
+ *
152
+ * Because this method requires the `widget-type-around` attribute to be set,
153
+ * the insertion can only happen when the widget's fake caret is active (e.g. activated
154
+ * using the keyboard).
155
+ *
156
+ * @private
157
+ * @returns {Boolean} Returns `true` when the paragraph was inserted (the attribute was present) and `false` otherwise.
158
+ */
159
+ _insertParagraphAccordingToFakeCaretPosition() {
160
+ const editor = this.editor;
161
+ const model = editor.model;
162
+ const modelSelection = model.document.selection;
163
+ const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition(modelSelection);
164
+ if (!typeAroundFakeCaretPosition) {
165
+ return false;
166
+ }
167
+ // @if CK_DEBUG_TYPING // if ( window.logCKETyping ) {
168
+ // @if CK_DEBUG_TYPING // console.info( '%c[WidgetTypeAround]%c Fake caret -> insert paragraph',
169
+ // @if CK_DEBUG_TYPING // 'font-weight: bold; color: green', ''
170
+ // @if CK_DEBUG_TYPING // );
171
+ // @if CK_DEBUG_TYPING // }
172
+ const selectedModelElement = modelSelection.getSelectedElement();
173
+ this._insertParagraph(selectedModelElement, typeAroundFakeCaretPosition);
174
+ return true;
175
+ }
176
+ /**
177
+ * Creates a listener in the editing conversion pipeline that injects the widget type around
178
+ * UI into every single widget instance created in the editor.
179
+ *
180
+ * The UI is delivered as a {@link module:engine/view/uielement~UIElement}
181
+ * wrapper which renders DOM buttons that users can use to insert paragraphs.
182
+ *
183
+ * @private
184
+ */
185
+ _enableTypeAroundUIInjection() {
186
+ const editor = this.editor;
187
+ const schema = editor.model.schema;
188
+ const t = editor.locale.t;
189
+ const buttonTitles = {
190
+ before: t('Insert paragraph before block'),
191
+ after: t('Insert paragraph after block')
192
+ };
193
+ editor.editing.downcastDispatcher.on('insert', (evt, data, conversionApi) => {
194
+ const viewElement = conversionApi.mapper.toViewElement(data.item);
195
+ // Filter out non-widgets and inline widgets.
196
+ if (isTypeAroundWidget(viewElement, data.item, schema)) {
197
+ injectUIIntoWidget(conversionApi.writer, buttonTitles, viewElement);
198
+ }
199
+ }, { priority: 'low' });
200
+ }
201
+ /**
202
+ * Brings support for the fake caret that appears when either:
203
+ *
204
+ * * the selection moves to a widget from a position next to it using arrow keys,
205
+ * * the arrow key is pressed when the widget is already selected.
206
+ *
207
+ * The fake caret lets the user know that they can start typing or just press
208
+ * <kbd>Enter</kbd> to insert a paragraph at the position next to a widget as suggested by the fake caret.
209
+ *
210
+ * The fake caret disappears when the user changes the selection or the editor
211
+ * gets blurred.
212
+ *
213
+ * The whole idea is as follows:
214
+ *
215
+ * 1. A user does one of the 2 scenarios described at the beginning.
216
+ * 2. The "keydown" listener is executed and the decision is made whether to show or hide the fake caret.
217
+ * 3. If it should show up, the `widget-type-around` model selection attribute is set indicating
218
+ * on which side of the widget it should appear.
219
+ * 4. The selection dispatcher reacts to the selection attribute and sets CSS classes responsible for the
220
+ * fake caret on the view widget.
221
+ * 5. If the fake caret should disappear, the selection attribute is removed and the dispatcher
222
+ * does the CSS class clean-up in the view.
223
+ * 6. Additionally, `change:range` and `FocusTracker#isFocused` listeners also remove the selection
224
+ * attribute (the former also removes widget CSS classes).
225
+ *
226
+ * @private
227
+ */
228
+ _enableTypeAroundFakeCaretActivationUsingKeyboardArrows() {
229
+ const editor = this.editor;
230
+ const model = editor.model;
231
+ const modelSelection = model.document.selection;
232
+ const schema = model.schema;
233
+ const editingView = editor.editing.view;
234
+ // This is the main listener responsible for the fake caret.
235
+ // Note: The priority must precede the default Widget class keydown handler ("high").
236
+ this._listenToIfEnabled(editingView.document, 'arrowKey', (evt, domEventData) => {
237
+ this._handleArrowKeyPress(evt, domEventData);
238
+ }, { context: [isWidget, '$text'], priority: 'high' });
239
+ // This listener makes sure the widget type around selection attribute will be gone from the model
240
+ // selection as soon as the model range changes. This attribute only makes sense when a widget is selected
241
+ // (and the "fake horizontal caret" is visible) so whenever the range changes (e.g. selection moved somewhere else),
242
+ // let's get rid of the attribute so that the selection downcast dispatcher isn't even bothered.
243
+ this._listenToIfEnabled(modelSelection, 'change:range', (evt, data) => {
244
+ // Do not reset the selection attribute when the change was indirect.
245
+ if (!data.directChange) {
246
+ return;
247
+ }
248
+ // Get rid of the widget type around attribute of the selection on every change:range.
249
+ // If the range changes, it means for sure, the user is no longer in the active ("fake horizontal caret") mode.
250
+ editor.model.change(writer => {
251
+ writer.removeSelectionAttribute(TYPE_AROUND_SELECTION_ATTRIBUTE);
252
+ });
253
+ });
254
+ // Get rid of the widget type around attribute of the selection on every document change
255
+ // that makes widget not selected any more (i.e. widget was removed).
256
+ this._listenToIfEnabled(model.document, 'change:data', () => {
257
+ const selectedModelElement = modelSelection.getSelectedElement();
258
+ if (selectedModelElement) {
259
+ const selectedViewElement = editor.editing.mapper.toViewElement(selectedModelElement);
260
+ if (isTypeAroundWidget(selectedViewElement, selectedModelElement, schema)) {
261
+ return;
262
+ }
263
+ }
264
+ editor.model.change(writer => {
265
+ writer.removeSelectionAttribute(TYPE_AROUND_SELECTION_ATTRIBUTE);
266
+ });
267
+ });
268
+ // React to changes of the model selection attribute made by the arrow keys listener.
269
+ // If the block widget is selected and the attribute changes, downcast the attribute to special
270
+ // CSS classes associated with the active ("fake horizontal caret") mode of the widget.
271
+ this._listenToIfEnabled(editor.editing.downcastDispatcher, 'selection', (evt, data, conversionApi) => {
272
+ const writer = conversionApi.writer;
273
+ if (this._currentFakeCaretModelElement) {
274
+ const selectedViewElement = conversionApi.mapper.toViewElement(this._currentFakeCaretModelElement);
275
+ if (selectedViewElement) {
276
+ // Get rid of CSS classes associated with the active ("fake horizontal caret") mode from the view widget.
277
+ writer.removeClass(POSSIBLE_INSERTION_POSITIONS.map(positionToWidgetCssClass), selectedViewElement);
278
+ this._currentFakeCaretModelElement = null;
279
+ }
280
+ }
281
+ const selectedModelElement = data.selection.getSelectedElement();
282
+ if (!selectedModelElement) {
283
+ return;
284
+ }
285
+ const selectedViewElement = conversionApi.mapper.toViewElement(selectedModelElement);
286
+ if (!isTypeAroundWidget(selectedViewElement, selectedModelElement, schema)) {
287
+ return;
288
+ }
289
+ const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition(data.selection);
290
+ if (!typeAroundFakeCaretPosition) {
291
+ return;
292
+ }
293
+ writer.addClass(positionToWidgetCssClass(typeAroundFakeCaretPosition), selectedViewElement);
294
+ // Remember the view widget that got the "fake-caret" CSS class. This class should be removed ASAP when the
295
+ // selection changes
296
+ this._currentFakeCaretModelElement = selectedModelElement;
297
+ });
298
+ this._listenToIfEnabled(editor.ui.focusTracker, 'change:isFocused', (evt, name, isFocused) => {
299
+ if (!isFocused) {
300
+ editor.model.change(writer => {
301
+ writer.removeSelectionAttribute(TYPE_AROUND_SELECTION_ATTRIBUTE);
302
+ });
303
+ }
304
+ });
305
+ function positionToWidgetCssClass(position) {
306
+ return `ck-widget_type-around_show-fake-caret_${position}`;
307
+ }
308
+ }
309
+ /**
310
+ * A listener executed on each "keydown" in the view document, a part of
311
+ * {@link #_enableTypeAroundFakeCaretActivationUsingKeyboardArrows}.
312
+ *
313
+ * It decides whether the arrow keypress should activate the fake caret or not (also whether it should
314
+ * be deactivated).
315
+ *
316
+ * The fake caret activation is done by setting the `widget-type-around` model selection attribute
317
+ * in this listener, and stopping and preventing the event that would normally be handled by the widget
318
+ * plugin that is responsible for the regular keyboard navigation near/across all widgets (that
319
+ * includes inline widgets, which are ignored by the widget type around plugin).
320
+ *
321
+ * @private
322
+ */
323
+ _handleArrowKeyPress(evt, domEventData) {
324
+ const editor = this.editor;
325
+ const model = editor.model;
326
+ const modelSelection = model.document.selection;
327
+ const schema = model.schema;
328
+ const editingView = editor.editing.view;
329
+ const keyCode = domEventData.keyCode;
330
+ const isForward = isForwardArrowKeyCode(keyCode, editor.locale.contentLanguageDirection);
331
+ const selectedViewElement = editingView.document.selection.getSelectedElement();
332
+ const selectedModelElement = editor.editing.mapper.toModelElement(selectedViewElement);
333
+ let shouldStopAndPreventDefault;
334
+ // Handle keyboard navigation when a type-around-compatible widget is currently selected.
335
+ if (isTypeAroundWidget(selectedViewElement, selectedModelElement, schema)) {
336
+ shouldStopAndPreventDefault = this._handleArrowKeyPressOnSelectedWidget(isForward);
337
+ }
338
+ // Handle keyboard arrow navigation when the selection is next to a type-around-compatible widget
339
+ // and the widget is about to be selected.
340
+ else if (modelSelection.isCollapsed) {
341
+ shouldStopAndPreventDefault = this._handleArrowKeyPressWhenSelectionNextToAWidget(isForward);
342
+ }
343
+ // Handle collapsing a non-collapsed selection that is wider than on a single widget.
344
+ else if (!domEventData.shiftKey) {
345
+ shouldStopAndPreventDefault = this._handleArrowKeyPressWhenNonCollapsedSelection(isForward);
346
+ }
347
+ if (shouldStopAndPreventDefault) {
348
+ domEventData.preventDefault();
349
+ evt.stop();
350
+ }
351
+ }
352
+ /**
353
+ * Handles the keyboard navigation on "keydown" when a widget is currently selected and activates or deactivates
354
+ * the fake caret for that widget, depending on the current value of the `widget-type-around` model
355
+ * selection attribute and the direction of the pressed arrow key.
356
+ *
357
+ * @private
358
+ * @param {Boolean} isForward `true` when the pressed arrow key was responsible for the forward model selection movement
359
+ * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
360
+ * @returns {Boolean} Returns `true` when the keypress was handled and no other keydown listener of the editor should
361
+ * process the event any further. Returns `false` otherwise.
362
+ */
363
+ _handleArrowKeyPressOnSelectedWidget(isForward) {
364
+ const editor = this.editor;
365
+ const model = editor.model;
366
+ const modelSelection = model.document.selection;
367
+ const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition(modelSelection);
368
+ return model.change(writer => {
369
+ // If the fake caret is displayed...
370
+ if (typeAroundFakeCaretPosition) {
371
+ const isLeavingWidget = typeAroundFakeCaretPosition === (isForward ? 'after' : 'before');
372
+ // If the keyboard arrow works against the value of the selection attribute...
373
+ // then remove the selection attribute but prevent default DOM actions
374
+ // and do not let the Widget plugin listener move the selection. This brings
375
+ // the widget back to the state, for instance, like if was selected using the mouse.
376
+ //
377
+ // **Note**: If leaving the widget when the fake caret is active, then the default
378
+ // Widget handler will change the selection and, in turn, this will automatically discard
379
+ // the selection attribute.
380
+ if (!isLeavingWidget) {
381
+ writer.removeSelectionAttribute(TYPE_AROUND_SELECTION_ATTRIBUTE);
382
+ return true;
383
+ }
384
+ }
385
+ // If the fake caret wasn't displayed, let's set it now according to the direction of the arrow
386
+ // key press. This also means we cannot let the Widget plugin listener move the selection.
387
+ else {
388
+ writer.setSelectionAttribute(TYPE_AROUND_SELECTION_ATTRIBUTE, isForward ? 'after' : 'before');
389
+ return true;
390
+ }
391
+ return false;
392
+ });
393
+ }
394
+ /**
395
+ * Handles the keyboard navigation on "keydown" when **no** widget is selected but the selection is **directly** next
396
+ * to one and upon the fake caret should become active for this widget upon arrow keypress
397
+ * (AKA entering/selecting the widget).
398
+ *
399
+ * **Note**: This code mirrors the implementation from the widget plugin but also adds the selection attribute.
400
+ * Unfortunately, there is no safe way to let the widget plugin do the selection part first and then just set the
401
+ * selection attribute here in the widget type around plugin. This is why this code must duplicate some from the widget plugin.
402
+ *
403
+ * @private
404
+ * @param {Boolean} isForward `true` when the pressed arrow key was responsible for the forward model selection movement
405
+ * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
406
+ * @returns {Boolean} Returns `true` when the keypress was handled and no other keydown listener of the editor should
407
+ * process the event any further. Returns `false` otherwise.
408
+ */
409
+ _handleArrowKeyPressWhenSelectionNextToAWidget(isForward) {
410
+ const editor = this.editor;
411
+ const model = editor.model;
412
+ const schema = model.schema;
413
+ const widgetPlugin = editor.plugins.get('Widget');
414
+ // This is the widget the selection is about to be set on.
415
+ const modelElementNextToSelection = widgetPlugin._getObjectElementNextToSelection(isForward);
416
+ const viewElementNextToSelection = editor.editing.mapper.toViewElement(modelElementNextToSelection);
417
+ if (isTypeAroundWidget(viewElementNextToSelection, modelElementNextToSelection, schema)) {
418
+ model.change(writer => {
419
+ widgetPlugin._setSelectionOverElement(modelElementNextToSelection);
420
+ writer.setSelectionAttribute(TYPE_AROUND_SELECTION_ATTRIBUTE, isForward ? 'before' : 'after');
421
+ });
422
+ // The change() block above does the same job as the Widget plugin. The event can
423
+ // be safely canceled.
424
+ return true;
425
+ }
426
+ return false;
427
+ }
428
+ /**
429
+ * Handles the keyboard navigation on "keydown" when a widget is currently selected (together with some other content)
430
+ * and the widget is the first or last element in the selection. It activates or deactivates the fake caret for that widget.
431
+ *
432
+ * @private
433
+ * @param {Boolean} isForward `true` when the pressed arrow key was responsible for the forward model selection movement
434
+ * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
435
+ * @returns {Boolean} Returns `true` when the keypress was handled and no other keydown listener of the editor should
436
+ * process the event any further. Returns `false` otherwise.
437
+ */
438
+ _handleArrowKeyPressWhenNonCollapsedSelection(isForward) {
439
+ const editor = this.editor;
440
+ const model = editor.model;
441
+ const schema = model.schema;
442
+ const mapper = editor.editing.mapper;
443
+ const modelSelection = model.document.selection;
444
+ const selectedModelNode = isForward ?
445
+ modelSelection.getLastPosition().nodeBefore :
446
+ modelSelection.getFirstPosition().nodeAfter;
447
+ const selectedViewNode = mapper.toViewElement(selectedModelNode);
448
+ // There is a widget at the collapse position so collapse the selection to the fake caret on it.
449
+ if (isTypeAroundWidget(selectedViewNode, selectedModelNode, schema)) {
450
+ model.change(writer => {
451
+ writer.setSelection(selectedModelNode, 'on');
452
+ writer.setSelectionAttribute(TYPE_AROUND_SELECTION_ATTRIBUTE, isForward ? 'after' : 'before');
453
+ });
454
+ return true;
455
+ }
456
+ return false;
457
+ }
458
+ /**
459
+ * Registers a `mousedown` listener for the view document which intercepts events
460
+ * coming from the widget type around UI, which happens when a user clicks one of the buttons
461
+ * that insert a paragraph next to a widget.
462
+ *
463
+ * @private
464
+ */
465
+ _enableInsertingParagraphsOnButtonClick() {
466
+ const editor = this.editor;
467
+ const editingView = editor.editing.view;
468
+ this._listenToIfEnabled(editingView.document, 'mousedown', (evt, domEventData) => {
469
+ const button = getClosestTypeAroundDomButton(domEventData.domTarget);
470
+ if (!button) {
471
+ return;
472
+ }
473
+ const buttonPosition = getTypeAroundButtonPosition(button);
474
+ const widgetViewElement = getClosestWidgetViewElement(button, editingView.domConverter);
475
+ const widgetModelElement = editor.editing.mapper.toModelElement(widgetViewElement);
476
+ this._insertParagraph(widgetModelElement, buttonPosition);
477
+ domEventData.preventDefault();
478
+ evt.stop();
479
+ });
480
+ }
481
+ /**
482
+ * Creates the <kbd>Enter</kbd> key listener on the view document that allows the user to insert a paragraph
483
+ * near the widget when either:
484
+ *
485
+ * * The fake caret was first activated using the arrow keys,
486
+ * * The entire widget is selected in the model.
487
+ *
488
+ * In the first case, the new paragraph is inserted according to the `widget-type-around` selection
489
+ * attribute (see {@link #_handleArrowKeyPress}).
490
+ *
491
+ * In the second case, the new paragraph is inserted based on whether a soft (<kbd>Shift</kbd>+<kbd>Enter</kbd>) keystroke
492
+ * was pressed or not.
493
+ *
494
+ * @private
495
+ */
496
+ _enableInsertingParagraphsOnEnterKeypress() {
497
+ const editor = this.editor;
498
+ const selection = editor.model.document.selection;
499
+ const editingView = editor.editing.view;
500
+ this._listenToIfEnabled(editingView.document, 'enter', (evt, domEventData) => {
501
+ // This event could be triggered from inside the widget but we are interested
502
+ // only when the widget is selected itself.
503
+ if (evt.eventPhase != 'atTarget') {
504
+ return;
505
+ }
506
+ const selectedModelElement = selection.getSelectedElement();
507
+ const selectedViewElement = editor.editing.mapper.toViewElement(selectedModelElement);
508
+ const schema = editor.model.schema;
509
+ let wasHandled;
510
+ // First check if the widget is selected and there's a type around selection attribute associated
511
+ // with the fake caret that would tell where to insert a new paragraph.
512
+ if (this._insertParagraphAccordingToFakeCaretPosition()) {
513
+ wasHandled = true;
514
+ }
515
+ // Then, if there is no selection attribute associated with the fake caret, check if the widget
516
+ // simply is selected and create a new paragraph according to the keystroke (Shift+)Enter.
517
+ else if (isTypeAroundWidget(selectedViewElement, selectedModelElement, schema)) {
518
+ this._insertParagraph(selectedModelElement, domEventData.isSoft ? 'before' : 'after');
519
+ wasHandled = true;
520
+ }
521
+ if (wasHandled) {
522
+ domEventData.preventDefault();
523
+ evt.stop();
524
+ }
525
+ }, { context: isWidget });
526
+ }
527
+ /**
528
+ * Similar to the {@link #_enableInsertingParagraphsOnEnterKeypress}, it allows the user
529
+ * to insert a paragraph next to a widget when the fake caret was activated using arrow
530
+ * keys but it responds to typing instead of <kbd>Enter</kbd>.
531
+ *
532
+ * Listener enabled by this method will insert a new paragraph according to the `widget-type-around`
533
+ * model selection attribute as the user simply starts typing, which creates the impression that the fake caret
534
+ * behaves like a real one rendered by the browser (AKA your text appears where the caret was).
535
+ *
536
+ * **Note**: At the moment this listener creates 2 undo steps: one for the `insertParagraph` command
537
+ * and another one for actual typing. It is not a disaster but this may need to be fixed
538
+ * sooner or later.
539
+ *
540
+ * @private
541
+ */
542
+ _enableInsertingParagraphsOnTypingKeystroke() {
543
+ const editor = this.editor;
544
+ const viewDocument = editor.editing.view.document;
545
+ // Note: The priority must precede the default Input plugin insertText handler.
546
+ this._listenToIfEnabled(viewDocument, 'insertText', (evt, data) => {
547
+ if (this._insertParagraphAccordingToFakeCaretPosition()) {
548
+ // The view selection in the event data contains the widget. If the new paragraph
549
+ // was inserted, modify the view selection passed along with the insertText event
550
+ // so the default event handler in the Input plugin starts typing inside the paragraph.
551
+ // Otherwise, the typing would be over the widget.
552
+ data.selection = viewDocument.selection;
553
+ }
554
+ }, { priority: 'high' });
555
+ if (env.isAndroid) {
556
+ // On Android with English keyboard, the composition starts just by putting caret
557
+ // at the word end or by selecting a table column. This is not a real composition started.
558
+ // Trigger delete content on first composition key pressed.
559
+ this._listenToIfEnabled(viewDocument, 'keydown', (evt, data) => {
560
+ if (data.keyCode == 229) {
561
+ this._insertParagraphAccordingToFakeCaretPosition();
562
+ }
563
+ });
564
+ }
565
+ else {
566
+ // Note: The priority must precede the default Input plugin compositionstart handler (to call it before delete content).
567
+ this._listenToIfEnabled(viewDocument, 'compositionstart', () => {
568
+ this._insertParagraphAccordingToFakeCaretPosition();
569
+ }, { priority: 'high' });
570
+ }
571
+ }
572
+ /**
573
+ * It creates a "delete" event listener on the view document to handle cases when the <kbd>Delete</kbd> or <kbd>Backspace</kbd>
574
+ * is pressed and the fake caret is currently active.
575
+ *
576
+ * The fake caret should create an illusion of a real browser caret so that when it appears before or after
577
+ * a widget, pressing <kbd>Delete</kbd> or <kbd>Backspace</kbd> should remove a widget or delete the content
578
+ * before or after a widget (depending on the content surrounding the widget).
579
+ *
580
+ * @private
581
+ */
582
+ _enableDeleteIntegration() {
583
+ const editor = this.editor;
584
+ const editingView = editor.editing.view;
585
+ const model = editor.model;
586
+ const schema = model.schema;
587
+ this._listenToIfEnabled(editingView.document, 'delete', (evt, domEventData) => {
588
+ // This event could be triggered from inside the widget but we are interested
589
+ // only when the widget is selected itself.
590
+ if (evt.eventPhase != 'atTarget') {
591
+ return;
592
+ }
593
+ const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition(model.document.selection);
594
+ // This listener handles only these cases when the fake caret is active.
595
+ if (!typeAroundFakeCaretPosition) {
596
+ return;
597
+ }
598
+ const direction = domEventData.direction;
599
+ const selectedModelWidget = model.document.selection.getSelectedElement();
600
+ const isFakeCaretBefore = typeAroundFakeCaretPosition === 'before';
601
+ const isDeleteForward = direction == 'forward';
602
+ const shouldDeleteEntireWidget = isFakeCaretBefore === isDeleteForward;
603
+ if (shouldDeleteEntireWidget) {
604
+ editor.execute('delete', {
605
+ selection: model.createSelection(selectedModelWidget, 'on')
606
+ });
607
+ }
608
+ else {
609
+ const range = schema.getNearestSelectionRange(model.createPositionAt(selectedModelWidget, typeAroundFakeCaretPosition), direction);
610
+ // If there is somewhere to move selection to, then there will be something to delete.
611
+ if (range) {
612
+ // If the range is NOT collapsed, then we know that the range contains an object (see getNearestSelectionRange() docs).
613
+ if (!range.isCollapsed) {
614
+ model.change(writer => {
615
+ writer.setSelection(range);
616
+ editor.execute(isDeleteForward ? 'deleteForward' : 'delete');
617
+ });
618
+ }
619
+ else {
620
+ const probe = model.createSelection(range.start);
621
+ model.modifySelection(probe, { direction });
622
+ // If the range is collapsed, let's see if a non-collapsed range exists that can could be deleted.
623
+ // If such range exists, use the editor command because it it safe for collaboration (it merges where it can).
624
+ if (!probe.focus.isEqual(range.start)) {
625
+ model.change(writer => {
626
+ writer.setSelection(range);
627
+ editor.execute(isDeleteForward ? 'deleteForward' : 'delete');
628
+ });
629
+ }
630
+ // If there is no non-collapsed range to be deleted then we are sure that there is an empty element
631
+ // next to a widget that should be removed. "delete" and "deleteForward" commands cannot get rid of it
632
+ // so calling Model#deleteContent here manually.
633
+ else {
634
+ const deepestEmptyRangeAncestor = getDeepestEmptyElementAncestor(schema, range.start.parent);
635
+ model.deleteContent(model.createSelection(deepestEmptyRangeAncestor, 'on'), {
636
+ doNotAutoparagraph: true
637
+ });
638
+ }
639
+ }
640
+ }
641
+ }
642
+ // If some content was deleted, don't let the handler from the Widget plugin kick in.
643
+ // If nothing was deleted, then the default handler will have nothing to do anyway.
644
+ domEventData.preventDefault();
645
+ evt.stop();
646
+ }, { context: isWidget });
647
+ }
648
+ /**
649
+ * Attaches the {@link module:engine/model/model~Model#event:insertContent} event listener that, for instance, allows the user to paste
650
+ * content near a widget when the fake caret is first activated using the arrow keys.
651
+ *
652
+ * The content is inserted according to the `widget-type-around` selection attribute (see {@link #_handleArrowKeyPress}).
653
+ *
654
+ * @private
655
+ */
656
+ _enableInsertContentIntegration() {
657
+ const editor = this.editor;
658
+ const model = this.editor.model;
659
+ const documentSelection = model.document.selection;
660
+ this._listenToIfEnabled(editor.model, 'insertContent', (evt, [content, selectable]) => {
661
+ if (selectable && !selectable.is('documentSelection')) {
662
+ return;
663
+ }
664
+ const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition(documentSelection);
665
+ if (!typeAroundFakeCaretPosition) {
666
+ return;
667
+ }
668
+ evt.stop();
669
+ return model.change(writer => {
670
+ const selectedElement = documentSelection.getSelectedElement();
671
+ const position = model.createPositionAt(selectedElement, typeAroundFakeCaretPosition);
672
+ const selection = writer.createSelection(position);
673
+ const result = model.insertContent(content, selection);
674
+ writer.setSelection(selection);
675
+ return result;
676
+ });
677
+ }, { priority: 'high' });
678
+ }
679
+ /**
680
+ * Attaches the {@link module:engine/model/model~Model#event:insertObject} event listener that modifies the
681
+ * `options.findOptimalPosition`parameter to position of fake caret in relation to selected element
682
+ * to reflect user's intent of desired insertion position.
683
+ *
684
+ * The object is inserted according to the `widget-type-around` selection attribute (see {@link #_handleArrowKeyPress}).
685
+ *
686
+ * @private
687
+ */
688
+ _enableInsertObjectIntegration() {
689
+ const editor = this.editor;
690
+ const model = this.editor.model;
691
+ const documentSelection = model.document.selection;
692
+ this._listenToIfEnabled(editor.model, 'insertObject', (evt, args) => {
693
+ const [, selectable, , options = {}] = args;
694
+ if (selectable && !selectable.is('documentSelection')) {
695
+ return;
696
+ }
697
+ const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition(documentSelection);
698
+ if (!typeAroundFakeCaretPosition) {
699
+ return;
700
+ }
701
+ options.findOptimalPosition = typeAroundFakeCaretPosition;
702
+ args[3] = options;
703
+ }, { priority: 'high' });
704
+ }
705
+ /**
706
+ * Attaches the {@link module:engine/model/model~Model#event:deleteContent} event listener to block the event when the fake
707
+ * caret is active.
708
+ *
709
+ * This is required for cases that trigger {@link module:engine/model/model~Model#deleteContent `model.deleteContent()`}
710
+ * before calling {@link module:engine/model/model~Model#insertContent `model.insertContent()`} like, for instance,
711
+ * plain text pasting.
712
+ *
713
+ * @private
714
+ */
715
+ _enableDeleteContentIntegration() {
716
+ const editor = this.editor;
717
+ const model = this.editor.model;
718
+ const documentSelection = model.document.selection;
719
+ this._listenToIfEnabled(editor.model, 'deleteContent', (evt, [selection]) => {
720
+ if (selection && !selection.is('documentSelection')) {
721
+ return;
722
+ }
723
+ const typeAroundFakeCaretPosition = getTypeAroundFakeCaretPosition(documentSelection);
724
+ // Disable removing the selection content while pasting plain text.
725
+ if (typeAroundFakeCaretPosition) {
726
+ evt.stop();
727
+ }
728
+ }, { priority: 'high' });
729
+ }
846
730
  }
847
-
848
731
  // Injects the type around UI into a view widget instance.
849
732
  //
850
733
  // @param {module:engine/view/downcastwriter~DowncastWriter} viewWriter
851
734
  // @param {Object.<String,String>} buttonTitles
852
735
  // @param {module:engine/view/element~Element} widgetViewElement
853
- function injectUIIntoWidget( viewWriter, buttonTitles, widgetViewElement ) {
854
- const typeAroundWrapper = viewWriter.createUIElement( 'div', {
855
- class: 'ck ck-reset_all ck-widget__type-around'
856
- }, function( domDocument ) {
857
- const wrapperDomElement = this.toDomElement( domDocument );
858
-
859
- injectButtons( wrapperDomElement, buttonTitles );
860
- injectFakeCaret( wrapperDomElement );
861
-
862
- return wrapperDomElement;
863
- } );
864
-
865
- // Inject the type around wrapper into the widget's wrapper.
866
- viewWriter.insert( viewWriter.createPositionAt( widgetViewElement, 'end' ), typeAroundWrapper );
736
+ function injectUIIntoWidget(viewWriter, buttonTitles, widgetViewElement) {
737
+ const typeAroundWrapper = viewWriter.createUIElement('div', {
738
+ class: 'ck ck-reset_all ck-widget__type-around'
739
+ }, function (domDocument) {
740
+ const wrapperDomElement = this.toDomElement(domDocument);
741
+ injectButtons(wrapperDomElement, buttonTitles);
742
+ injectFakeCaret(wrapperDomElement);
743
+ return wrapperDomElement;
744
+ });
745
+ // Inject the type around wrapper into the widget's wrapper.
746
+ viewWriter.insert(viewWriter.createPositionAt(widgetViewElement, 'end'), typeAroundWrapper);
867
747
  }
868
-
869
748
  // FYI: Not using the IconView class because each instance would need to be destroyed to avoid memory leaks
870
749
  // and it's pretty hard to figure out when a view (widget) is gone for good so it's cheaper to use raw
871
750
  // <svg> here.
872
751
  //
873
752
  // @param {HTMLElement} wrapperDomElement
874
753
  // @param {Object.<String,String>} buttonTitles
875
- function injectButtons( wrapperDomElement, buttonTitles ) {
876
- for ( const position of POSSIBLE_INSERTION_POSITIONS ) {
877
- const buttonTemplate = new Template( {
878
- tag: 'div',
879
- attributes: {
880
- class: [
881
- 'ck',
882
- 'ck-widget__type-around__button',
883
- `ck-widget__type-around__button_${ position }`
884
- ],
885
- title: buttonTitles[ position ]
886
- },
887
- children: [
888
- wrapperDomElement.ownerDocument.importNode( RETURN_ARROW_ICON_ELEMENT, true )
889
- ]
890
- } );
891
-
892
- wrapperDomElement.appendChild( buttonTemplate.render() );
893
- }
754
+ function injectButtons(wrapperDomElement, buttonTitles) {
755
+ for (const position of POSSIBLE_INSERTION_POSITIONS) {
756
+ const buttonTemplate = new Template({
757
+ tag: 'div',
758
+ attributes: {
759
+ class: [
760
+ 'ck',
761
+ 'ck-widget__type-around__button',
762
+ `ck-widget__type-around__button_${position}`
763
+ ],
764
+ title: buttonTitles[position]
765
+ },
766
+ children: [
767
+ wrapperDomElement.ownerDocument.importNode(RETURN_ARROW_ICON_ELEMENT, true)
768
+ ]
769
+ });
770
+ wrapperDomElement.appendChild(buttonTemplate.render());
771
+ }
894
772
  }
895
-
896
773
  // @param {HTMLElement} wrapperDomElement
897
- function injectFakeCaret( wrapperDomElement ) {
898
- const caretTemplate = new Template( {
899
- tag: 'div',
900
- attributes: {
901
- class: [
902
- 'ck',
903
- 'ck-widget__type-around__fake-caret'
904
- ]
905
- }
906
- } );
907
-
908
- wrapperDomElement.appendChild( caretTemplate.render() );
774
+ function injectFakeCaret(wrapperDomElement) {
775
+ const caretTemplate = new Template({
776
+ tag: 'div',
777
+ attributes: {
778
+ class: [
779
+ 'ck',
780
+ 'ck-widget__type-around__fake-caret'
781
+ ]
782
+ }
783
+ });
784
+ wrapperDomElement.appendChild(caretTemplate.render());
909
785
  }
910
-
911
786
  // Returns the ancestor of an element closest to the root which is empty. For instance,
912
787
  // for `<baz>`:
913
788
  //
@@ -918,16 +793,13 @@ function injectFakeCaret( wrapperDomElement ) {
918
793
  // @param {module:engine/model/schema~Schema} schema
919
794
  // @param {module:engine/model/element~Element} element
920
795
  // @returns {module:engine/model/element~Element|null}
921
- function getDeepestEmptyElementAncestor( schema, element ) {
922
- let deepestEmptyAncestor = element;
923
-
924
- for ( const ancestor of element.getAncestors( { parentFirst: true } ) ) {
925
- if ( ancestor.childCount > 1 || schema.isLimit( ancestor ) ) {
926
- break;
927
- }
928
-
929
- deepestEmptyAncestor = ancestor;
930
- }
931
-
932
- return deepestEmptyAncestor;
796
+ function getDeepestEmptyElementAncestor(schema, element) {
797
+ let deepestEmptyAncestor = element;
798
+ for (const ancestor of element.getAncestors({ parentFirst: true })) {
799
+ if (ancestor.childCount > 1 || schema.isLimit(ancestor)) {
800
+ break;
801
+ }
802
+ deepestEmptyAncestor = ancestor;
803
+ }
804
+ return deepestEmptyAncestor;
933
805
  }