@ckeditor/ckeditor5-utils 34.2.0 → 35.0.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.
- package/CHANGELOG.md +324 -0
- package/LICENSE.md +1 -1
- package/package.json +19 -8
- package/src/areconnectedthroughproperties.js +0 -92
- package/src/ckeditorerror.js +0 -217
- package/src/collection.js +0 -785
- package/src/comparearrays.js +0 -51
- package/src/config.js +0 -246
- package/src/count.js +0 -26
- package/src/diff.js +0 -138
- package/src/difftochanges.js +0 -86
- package/src/dom/createelement.js +0 -49
- package/src/dom/emittermixin.js +0 -341
- package/src/dom/getancestors.js +0 -31
- package/src/dom/getborderwidths.js +0 -27
- package/src/dom/getcommonancestor.js +0 -31
- package/src/dom/getdatafromelement.js +0 -24
- package/src/dom/getpositionedancestor.js +0 -28
- package/src/dom/global.js +0 -26
- package/src/dom/indexof.js +0 -25
- package/src/dom/insertat.js +0 -19
- package/src/dom/iscomment.js +0 -20
- package/src/dom/isnode.js +0 -26
- package/src/dom/isrange.js +0 -18
- package/src/dom/istext.js +0 -18
- package/src/dom/isvisible.js +0 -25
- package/src/dom/iswindow.js +0 -30
- package/src/dom/position.js +0 -518
- package/src/dom/rect.js +0 -443
- package/src/dom/remove.js +0 -21
- package/src/dom/resizeobserver.js +0 -378
- package/src/dom/scroll.js +0 -302
- package/src/dom/setdatainelement.js +0 -24
- package/src/dom/tounit.js +0 -27
- package/src/elementreplacer.js +0 -57
- package/src/emittermixin.js +0 -719
- package/src/env.js +0 -190
- package/src/eventinfo.js +0 -79
- package/src/fastdiff.js +0 -261
- package/src/first.js +0 -24
- package/src/focustracker.js +0 -157
- package/src/index.js +0 -45
- package/src/inserttopriorityarray.js +0 -42
- package/src/isiterable.js +0 -18
- package/src/keyboard.js +0 -301
- package/src/keystrokehandler.js +0 -130
- package/src/language.js +0 -26
- package/src/locale.js +0 -176
- package/src/mapsequal.js +0 -32
- package/src/mix.js +0 -47
- package/src/nth.js +0 -31
- package/src/objecttomap.js +0 -29
- package/src/observablemixin.js +0 -908
- package/src/priorities.js +0 -44
- package/src/spy.js +0 -25
- package/src/toarray.js +0 -18
- package/src/tomap.js +0 -29
- package/src/translation-service.js +0 -216
- package/src/uid.js +0 -59
- package/src/unicode.js +0 -106
- package/src/version.js +0 -157
|
@@ -1,378 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @module utils/dom/resizeobserver
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/* globals setTimeout, clearTimeout */
|
|
11
|
-
|
|
12
|
-
import mix from '../mix';
|
|
13
|
-
import global from './global';
|
|
14
|
-
import Rect from './rect';
|
|
15
|
-
import DomEmitterMixin from './emittermixin';
|
|
16
|
-
|
|
17
|
-
const RESIZE_CHECK_INTERVAL = 100;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* A helper class which instances allow performing custom actions when native DOM elements are resized.
|
|
21
|
-
*
|
|
22
|
-
* const editableElement = editor.editing.view.getDomRoot();
|
|
23
|
-
*
|
|
24
|
-
* const observer = new ResizeObserver( editableElement, entry => {
|
|
25
|
-
* console.log( 'The editable element has been resized in DOM.' );
|
|
26
|
-
* console.log( entry.target ); // -> editableElement
|
|
27
|
-
* console.log( entry.contentRect.width ); // -> e.g. '423px'
|
|
28
|
-
* } );
|
|
29
|
-
*
|
|
30
|
-
* By default, it uses the [native DOM resize observer](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)
|
|
31
|
-
* under the hood and in browsers that do not support the native API yet, a polyfilled observer is
|
|
32
|
-
* used instead.
|
|
33
|
-
*/
|
|
34
|
-
export default class ResizeObserver {
|
|
35
|
-
/**
|
|
36
|
-
* Creates an instance of the `ResizeObserver` class.
|
|
37
|
-
*
|
|
38
|
-
* @param {HTMLElement} element A DOM element that is to be observed for resizing. Note that
|
|
39
|
-
* the element must be visible (i.e. not detached from DOM) for the observer to work.
|
|
40
|
-
* @param {Function} callback A function called when the observed element was resized. It passes
|
|
41
|
-
* the [`ResizeObserverEntry`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry)
|
|
42
|
-
* object with information about the resize event.
|
|
43
|
-
*/
|
|
44
|
-
constructor( element, callback ) {
|
|
45
|
-
// **Note**: For the maximum performance, this class ensures only a single instance of the native
|
|
46
|
-
// (or polyfilled) observer is used no matter how many instances of this class were created.
|
|
47
|
-
if ( !ResizeObserver._observerInstance ) {
|
|
48
|
-
ResizeObserver._createObserver();
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The element observer by this observer.
|
|
53
|
-
*
|
|
54
|
-
* @readonly
|
|
55
|
-
* @private
|
|
56
|
-
* @member {HTMLElement}
|
|
57
|
-
*/
|
|
58
|
-
this._element = element;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* The callback executed each time {@link #_element} is resized.
|
|
62
|
-
*
|
|
63
|
-
* @readonly
|
|
64
|
-
* @private
|
|
65
|
-
* @member {Function}
|
|
66
|
-
*/
|
|
67
|
-
this._callback = callback;
|
|
68
|
-
|
|
69
|
-
ResizeObserver._addElementCallback( element, callback );
|
|
70
|
-
ResizeObserver._observerInstance.observe( element );
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Destroys the observer which disables the `callback` passed to the {@link #constructor}.
|
|
75
|
-
*/
|
|
76
|
-
destroy() {
|
|
77
|
-
ResizeObserver._deleteElementCallback( this._element, this._callback );
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Registers a new resize callback for the DOM element.
|
|
82
|
-
*
|
|
83
|
-
* @private
|
|
84
|
-
* @static
|
|
85
|
-
* @param {HTMLElement} element
|
|
86
|
-
* @param {Function} callback
|
|
87
|
-
*/
|
|
88
|
-
static _addElementCallback( element, callback ) {
|
|
89
|
-
if ( !ResizeObserver._elementCallbacks ) {
|
|
90
|
-
ResizeObserver._elementCallbacks = new Map();
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
let callbacks = ResizeObserver._elementCallbacks.get( element );
|
|
94
|
-
|
|
95
|
-
if ( !callbacks ) {
|
|
96
|
-
callbacks = new Set();
|
|
97
|
-
ResizeObserver._elementCallbacks.set( element, callbacks );
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
callbacks.add( callback );
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Removes a resize callback from the DOM element. If no callbacks are left
|
|
105
|
-
* for the element, it removes the element from the native observer.
|
|
106
|
-
*
|
|
107
|
-
* @private
|
|
108
|
-
* @static
|
|
109
|
-
* @param {HTMLElement} element
|
|
110
|
-
* @param {Function} callback
|
|
111
|
-
*/
|
|
112
|
-
static _deleteElementCallback( element, callback ) {
|
|
113
|
-
const callbacks = ResizeObserver._getElementCallbacks( element );
|
|
114
|
-
|
|
115
|
-
// Remove the element callback. Check if exist first in case someone
|
|
116
|
-
// called destroy() twice.
|
|
117
|
-
if ( callbacks ) {
|
|
118
|
-
callbacks.delete( callback );
|
|
119
|
-
|
|
120
|
-
// If no callbacks left for the element, also remove the element.
|
|
121
|
-
if ( !callbacks.size ) {
|
|
122
|
-
ResizeObserver._elementCallbacks.delete( element );
|
|
123
|
-
ResizeObserver._observerInstance.unobserve( element );
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if ( ResizeObserver._elementCallbacks && !ResizeObserver._elementCallbacks.size ) {
|
|
128
|
-
ResizeObserver._observerInstance = null;
|
|
129
|
-
ResizeObserver._elementCallbacks = null;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Returns are registered resize callbacks for the DOM element.
|
|
135
|
-
*
|
|
136
|
-
* @private
|
|
137
|
-
* @static
|
|
138
|
-
* @param {HTMLElement} element
|
|
139
|
-
* @returns {Set.<HTMLElement>|null}
|
|
140
|
-
*/
|
|
141
|
-
static _getElementCallbacks( element ) {
|
|
142
|
-
if ( !ResizeObserver._elementCallbacks ) {
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return ResizeObserver._elementCallbacks.get( element );
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Creates the single native observer shared across all `ResizeObserver` instances.
|
|
151
|
-
* If the browser does not support the native API, it creates a polyfill.
|
|
152
|
-
*
|
|
153
|
-
* @private
|
|
154
|
-
* @static
|
|
155
|
-
*/
|
|
156
|
-
static _createObserver() {
|
|
157
|
-
let ObserverConstructor;
|
|
158
|
-
|
|
159
|
-
// TODO: One day, the `ResizeObserver` API will be supported in all modern web browsers.
|
|
160
|
-
// When it happens, this module will no longer make sense and should be removed and
|
|
161
|
-
// the native implementation should be used across the project to save bytes.
|
|
162
|
-
// Check out https://caniuse.com/#feat=resizeobserver.
|
|
163
|
-
if ( typeof global.window.ResizeObserver === 'function' ) {
|
|
164
|
-
ObserverConstructor = global.window.ResizeObserver;
|
|
165
|
-
} else {
|
|
166
|
-
ObserverConstructor = ResizeObserverPolyfill;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
ResizeObserver._observerInstance = new ObserverConstructor( entries => {
|
|
170
|
-
for ( const entry of entries ) {
|
|
171
|
-
const callbacks = ResizeObserver._getElementCallbacks( entry.target );
|
|
172
|
-
|
|
173
|
-
if ( callbacks ) {
|
|
174
|
-
for ( const callback of callbacks ) {
|
|
175
|
-
callback( entry );
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
} );
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* The single native observer instance (or polyfill in browsers that do not support the API)
|
|
185
|
-
* shared across all {@link module:utils/dom/resizeobserver~ResizeObserver} instances.
|
|
186
|
-
*
|
|
187
|
-
* @static
|
|
188
|
-
* @protected
|
|
189
|
-
* @readonly
|
|
190
|
-
* @property {Object|null} module:utils/dom/resizeobserver~ResizeObserver#_observerInstance
|
|
191
|
-
*/
|
|
192
|
-
ResizeObserver._observerInstance = null;
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* A mapping of native DOM elements and their callbacks shared across all
|
|
196
|
-
* {@link module:utils/dom/resizeobserver~ResizeObserver} instances.
|
|
197
|
-
*
|
|
198
|
-
* @static
|
|
199
|
-
* @private
|
|
200
|
-
* @readonly
|
|
201
|
-
* @property {Map.<HTMLElement,Set>|null} module:utils/dom/resizeobserver~ResizeObserver#_elementCallbacks
|
|
202
|
-
*/
|
|
203
|
-
ResizeObserver._elementCallbacks = null;
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* A polyfill class for the native [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver).
|
|
207
|
-
*
|
|
208
|
-
* @private
|
|
209
|
-
* @mixes module:utils/domemittermixin~DomEmitterMixin
|
|
210
|
-
*/
|
|
211
|
-
class ResizeObserverPolyfill {
|
|
212
|
-
/**
|
|
213
|
-
* Creates an instance of the {@link module:utils/dom/resizeobserver~ResizeObserverPolyfill} class.
|
|
214
|
-
*
|
|
215
|
-
* It synchronously reacts to resize of the window to check if observed elements' geometry changed.
|
|
216
|
-
*
|
|
217
|
-
* Additionally, the polyfilled observer uses a timeout to check if observed elements' geometry has changed
|
|
218
|
-
* in some other way (dynamic layouts, scrollbars showing up, etc.), so its response can also be asynchronous.
|
|
219
|
-
*
|
|
220
|
-
* @param {Function} callback A function called when any observed element was resized. Refer to the
|
|
221
|
-
* native [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) API to
|
|
222
|
-
* learn more.
|
|
223
|
-
*/
|
|
224
|
-
constructor( callback ) {
|
|
225
|
-
/**
|
|
226
|
-
* A function called when any observed {@link #_elements element} was resized.
|
|
227
|
-
*
|
|
228
|
-
* @readonly
|
|
229
|
-
* @protected
|
|
230
|
-
* @member {Function}
|
|
231
|
-
*/
|
|
232
|
-
this._callback = callback;
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* DOM elements currently observed by the observer instance.
|
|
236
|
-
*
|
|
237
|
-
* @readonly
|
|
238
|
-
* @protected
|
|
239
|
-
* @member {Set}
|
|
240
|
-
*/
|
|
241
|
-
this._elements = new Set();
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Cached DOM {@link #_elements elements} bounding rects to compare to upon the next check.
|
|
245
|
-
*
|
|
246
|
-
* @readonly
|
|
247
|
-
* @protected
|
|
248
|
-
* @member {Map.<HTMLElement,module:utils/dom/rect~Rect>}
|
|
249
|
-
*/
|
|
250
|
-
this._previousRects = new Map();
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* An UID of the current timeout upon which the observed elements rects
|
|
254
|
-
* will be compared to the {@link #_previousRects previous rects} from the past.
|
|
255
|
-
*
|
|
256
|
-
* @readonly
|
|
257
|
-
* @protected
|
|
258
|
-
* @member {Map.<HTMLElement,module:utils/dom/rect~Rect>}
|
|
259
|
-
*/
|
|
260
|
-
this._periodicCheckTimeout = null;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Starts observing a DOM element.
|
|
265
|
-
*
|
|
266
|
-
* Learn more in the
|
|
267
|
-
* [native method documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/observe).
|
|
268
|
-
*
|
|
269
|
-
* @param {HTMLElement} element
|
|
270
|
-
*/
|
|
271
|
-
observe( element ) {
|
|
272
|
-
this._elements.add( element );
|
|
273
|
-
|
|
274
|
-
this._checkElementRectsAndExecuteCallback();
|
|
275
|
-
|
|
276
|
-
if ( this._elements.size === 1 ) {
|
|
277
|
-
this._startPeriodicCheck();
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Stops observing a DOM element.
|
|
283
|
-
*
|
|
284
|
-
* Learn more in the
|
|
285
|
-
* [native method documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/unobserve).
|
|
286
|
-
*
|
|
287
|
-
* @param {HTMLElement} element
|
|
288
|
-
*/
|
|
289
|
-
unobserve( element ) {
|
|
290
|
-
this._elements.delete( element );
|
|
291
|
-
this._previousRects.delete( element );
|
|
292
|
-
|
|
293
|
-
if ( !this._elements.size ) {
|
|
294
|
-
this._stopPeriodicCheck();
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* When called, the observer calls the {@link #_callback resize callback} for all observed
|
|
300
|
-
* {@link #_elements elements} but also starts checking periodically for changes in the elements' geometry.
|
|
301
|
-
* If some are detected, {@link #_callback resize callback} is called for relevant elements that were resized.
|
|
302
|
-
*
|
|
303
|
-
* @protected
|
|
304
|
-
*/
|
|
305
|
-
_startPeriodicCheck() {
|
|
306
|
-
const periodicCheck = () => {
|
|
307
|
-
this._checkElementRectsAndExecuteCallback();
|
|
308
|
-
this._periodicCheckTimeout = setTimeout( periodicCheck, RESIZE_CHECK_INTERVAL );
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
this.listenTo( global.window, 'resize', () => {
|
|
312
|
-
this._checkElementRectsAndExecuteCallback();
|
|
313
|
-
} );
|
|
314
|
-
|
|
315
|
-
this._periodicCheckTimeout = setTimeout( periodicCheck, RESIZE_CHECK_INTERVAL );
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Stops checking for changes in all observed {@link #_elements elements} geometry.
|
|
320
|
-
*
|
|
321
|
-
* @protected
|
|
322
|
-
*/
|
|
323
|
-
_stopPeriodicCheck() {
|
|
324
|
-
clearTimeout( this._periodicCheckTimeout );
|
|
325
|
-
this.stopListening();
|
|
326
|
-
this._previousRects.clear();
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Checks if the geometry of any of the {@link #_elements element} has changed. If so, executes
|
|
331
|
-
* the {@link #_callback resize callback} with element geometry data.
|
|
332
|
-
*
|
|
333
|
-
* @protected
|
|
334
|
-
*/
|
|
335
|
-
_checkElementRectsAndExecuteCallback() {
|
|
336
|
-
const entries = [];
|
|
337
|
-
|
|
338
|
-
for ( const element of this._elements ) {
|
|
339
|
-
if ( this._hasRectChanged( element ) ) {
|
|
340
|
-
entries.push( {
|
|
341
|
-
target: element,
|
|
342
|
-
contentRect: this._previousRects.get( element )
|
|
343
|
-
} );
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
if ( entries.length ) {
|
|
348
|
-
this._callback( entries );
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* Compares the DOM element geometry to the {@link #_previousRects cached geometry} from the past.
|
|
354
|
-
* Returns `true` if geometry has changed or the element is checked for the first time.
|
|
355
|
-
*
|
|
356
|
-
* @protected
|
|
357
|
-
* @param {HTMLElement} element
|
|
358
|
-
* @returns {Boolean}
|
|
359
|
-
*/
|
|
360
|
-
_hasRectChanged( element ) {
|
|
361
|
-
if ( !element.ownerDocument.body.contains( element ) ) {
|
|
362
|
-
return false;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
const currentRect = new Rect( element );
|
|
366
|
-
const previousRect = this._previousRects.get( element );
|
|
367
|
-
|
|
368
|
-
// The first check should always yield true despite no Previous rect to compare to.
|
|
369
|
-
// The native ResizeObserver does that and... that makes sense. Sort of.
|
|
370
|
-
const hasChanged = !previousRect || !previousRect.isEqual( currentRect );
|
|
371
|
-
|
|
372
|
-
this._previousRects.set( element, currentRect );
|
|
373
|
-
|
|
374
|
-
return hasChanged;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
mix( ResizeObserverPolyfill, DomEmitterMixin );
|
package/src/dom/scroll.js
DELETED
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @module utils/dom/scroll
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import isRange from './isrange';
|
|
11
|
-
import Rect from './rect';
|
|
12
|
-
import isText from './istext';
|
|
13
|
-
|
|
14
|
-
const utils = {};
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Makes any page `HTMLElement` or `Range` (`target`) visible inside the browser viewport.
|
|
18
|
-
* This helper will scroll all `target` ancestors and the web browser viewport to reveal the target to
|
|
19
|
-
* the user. If the `target` is already visible, nothing will happen.
|
|
20
|
-
*
|
|
21
|
-
* @param {HTMLElement|Range} options.target A target, which supposed to become visible to the user.
|
|
22
|
-
* @param {Number} [options.viewportOffset] An offset from the edge of the viewport (in pixels)
|
|
23
|
-
* the `target` will be moved by when the viewport is scrolled. It enhances the user experience
|
|
24
|
-
* by keeping the `target` some distance from the edge of the viewport and thus making it easier to
|
|
25
|
-
* read or edit by the user.
|
|
26
|
-
*/
|
|
27
|
-
export function scrollViewportToShowTarget( { target, viewportOffset = 0 } ) {
|
|
28
|
-
const targetWindow = getWindow( target );
|
|
29
|
-
let currentWindow = targetWindow;
|
|
30
|
-
let currentFrame = null;
|
|
31
|
-
|
|
32
|
-
// Iterate over all windows, starting from target's parent window up to window#top.
|
|
33
|
-
while ( currentWindow ) {
|
|
34
|
-
let firstAncestorToScroll;
|
|
35
|
-
|
|
36
|
-
// Let's scroll target's ancestors first to reveal it. Then, once the ancestor scrolls
|
|
37
|
-
// settled down, the algorithm can eventually scroll the viewport of the current window.
|
|
38
|
-
//
|
|
39
|
-
// Note: If the current window is target's **original** window (e.g. the first one),
|
|
40
|
-
// start scrolling the closest parent of the target. If not, scroll the closest parent
|
|
41
|
-
// of an iframe that resides in the current window.
|
|
42
|
-
if ( currentWindow == targetWindow ) {
|
|
43
|
-
firstAncestorToScroll = getParentElement( target );
|
|
44
|
-
} else {
|
|
45
|
-
firstAncestorToScroll = getParentElement( currentFrame );
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Scroll the target's ancestors first. Once done, scrolling the viewport is easy.
|
|
49
|
-
scrollAncestorsToShowRect( firstAncestorToScroll, () => {
|
|
50
|
-
// Note: If the target does not belong to the current window **directly**,
|
|
51
|
-
// i.e. it resides in an iframe belonging to the window, obtain the target's rect
|
|
52
|
-
// in the coordinates of the current window. By default, a Rect returns geometry
|
|
53
|
-
// relative to the current window's viewport. To make it work in a parent window,
|
|
54
|
-
// it must be shifted.
|
|
55
|
-
return getRectRelativeToWindow( target, currentWindow );
|
|
56
|
-
} );
|
|
57
|
-
|
|
58
|
-
// Obtain the rect of the target after it has been scrolled within its ancestors.
|
|
59
|
-
// It's time to scroll the viewport.
|
|
60
|
-
const targetRect = getRectRelativeToWindow( target, currentWindow );
|
|
61
|
-
|
|
62
|
-
scrollWindowToShowRect( currentWindow, targetRect, viewportOffset );
|
|
63
|
-
|
|
64
|
-
if ( currentWindow.parent != currentWindow ) {
|
|
65
|
-
// Keep the reference to the <iframe> element the "previous current window" was
|
|
66
|
-
// rendered within. It will be useful to re–calculate the rect of the target
|
|
67
|
-
// in the parent window's relative geometry. The target's rect must be shifted
|
|
68
|
-
// by it's iframe's position.
|
|
69
|
-
currentFrame = currentWindow.frameElement;
|
|
70
|
-
currentWindow = currentWindow.parent;
|
|
71
|
-
|
|
72
|
-
// If the current window has some parent but frameElement is inaccessible, then they have
|
|
73
|
-
// different domains/ports and, due to security reasons, accessing and scrolling
|
|
74
|
-
// the parent window won't be possible.
|
|
75
|
-
// See https://github.com/ckeditor/ckeditor5/issues/930.
|
|
76
|
-
if ( !currentFrame ) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
} else {
|
|
80
|
-
currentWindow = null;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Makes any page `HTMLElement` or `Range` (target) visible within its scrollable ancestors,
|
|
87
|
-
* e.g. if they have `overflow: scroll` CSS style.
|
|
88
|
-
*
|
|
89
|
-
* @param {HTMLElement|Range} target A target, which supposed to become visible to the user.
|
|
90
|
-
*/
|
|
91
|
-
export function scrollAncestorsToShowTarget( target ) {
|
|
92
|
-
const targetParent = getParentElement( target );
|
|
93
|
-
|
|
94
|
-
scrollAncestorsToShowRect( targetParent, () => {
|
|
95
|
-
return new Rect( target );
|
|
96
|
-
} );
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// TODO: Using a property value shorthand in the top of the file
|
|
100
|
-
// causes JSDoc to throw errors. See https://github.com/cksource/docs-builder/issues/75.
|
|
101
|
-
Object.assign( utils, {
|
|
102
|
-
scrollViewportToShowTarget,
|
|
103
|
-
scrollAncestorsToShowTarget
|
|
104
|
-
} );
|
|
105
|
-
|
|
106
|
-
// Makes a given rect visible within its parent window.
|
|
107
|
-
//
|
|
108
|
-
// Note: Avoid the situation where the caret is still in the viewport, but totally
|
|
109
|
-
// at the edge of it. In such situation, if it moved beyond the viewport in the next
|
|
110
|
-
// action e.g. after paste, the scrolling would move it to the viewportOffset level
|
|
111
|
-
// and it all would look like the caret visually moved up/down:
|
|
112
|
-
//
|
|
113
|
-
// 1.
|
|
114
|
-
// | foo[]
|
|
115
|
-
// | <--- N px of space below the caret
|
|
116
|
-
// +---------------------------------...
|
|
117
|
-
//
|
|
118
|
-
// 2. *paste*
|
|
119
|
-
// 3.
|
|
120
|
-
// |
|
|
121
|
-
// |
|
|
122
|
-
// +-foo-----------------------------...
|
|
123
|
-
// bar[] <--- caret below viewport, scrolling...
|
|
124
|
-
//
|
|
125
|
-
// 4. *scrolling*
|
|
126
|
-
// 5.
|
|
127
|
-
// |
|
|
128
|
-
// | foo
|
|
129
|
-
// | bar[] <--- caret precisely at the edge
|
|
130
|
-
// +---------------------------------...
|
|
131
|
-
//
|
|
132
|
-
// To prevent this, this method checks the rects moved by the viewportOffset to cover
|
|
133
|
-
// the upper/lower edge of the viewport. It makes sure if the action repeats, there's
|
|
134
|
-
// no twitching – it's a purely visual improvement:
|
|
135
|
-
//
|
|
136
|
-
// 5. (after fix)
|
|
137
|
-
// |
|
|
138
|
-
// | foo
|
|
139
|
-
// | bar[]
|
|
140
|
-
// | <--- N px of space below the caret
|
|
141
|
-
// +---------------------------------...
|
|
142
|
-
//
|
|
143
|
-
// @private
|
|
144
|
-
// @param {Window} window A window which is scrolled to reveal the rect.
|
|
145
|
-
// @param {module:utils/dom/rect~Rect} rect A rect which is to be revealed.
|
|
146
|
-
// @param {Number} viewportOffset See scrollViewportToShowTarget.
|
|
147
|
-
function scrollWindowToShowRect( window, rect, viewportOffset ) {
|
|
148
|
-
const targetShiftedDownRect = rect.clone().moveBy( 0, viewportOffset );
|
|
149
|
-
const targetShiftedUpRect = rect.clone().moveBy( 0, -viewportOffset );
|
|
150
|
-
const viewportRect = new Rect( window ).excludeScrollbarsAndBorders();
|
|
151
|
-
|
|
152
|
-
const rects = [ targetShiftedUpRect, targetShiftedDownRect ];
|
|
153
|
-
|
|
154
|
-
if ( !rects.every( rect => viewportRect.contains( rect ) ) ) {
|
|
155
|
-
let { scrollX, scrollY } = window;
|
|
156
|
-
|
|
157
|
-
if ( isAbove( targetShiftedUpRect, viewportRect ) ) {
|
|
158
|
-
scrollY -= viewportRect.top - rect.top + viewportOffset;
|
|
159
|
-
} else if ( isBelow( targetShiftedDownRect, viewportRect ) ) {
|
|
160
|
-
scrollY += rect.bottom - viewportRect.bottom + viewportOffset;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// TODO: Web browsers scroll natively to place the target in the middle
|
|
164
|
-
// of the viewport. It's not a very popular case, though.
|
|
165
|
-
if ( isLeftOf( rect, viewportRect ) ) {
|
|
166
|
-
scrollX -= viewportRect.left - rect.left + viewportOffset;
|
|
167
|
-
} else if ( isRightOf( rect, viewportRect ) ) {
|
|
168
|
-
scrollX += rect.right - viewportRect.right + viewportOffset;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
window.scrollTo( scrollX, scrollY );
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// Recursively scrolls element ancestors to visually reveal a rect.
|
|
176
|
-
//
|
|
177
|
-
// @private
|
|
178
|
-
// @param {HTMLElement} A parent The first ancestors to start scrolling.
|
|
179
|
-
// @param {Function} getRect A function which returns the Rect, which is to be revealed.
|
|
180
|
-
function scrollAncestorsToShowRect( parent, getRect ) {
|
|
181
|
-
const parentWindow = getWindow( parent );
|
|
182
|
-
let parentRect, targetRect;
|
|
183
|
-
|
|
184
|
-
while ( parent != parentWindow.document.body ) {
|
|
185
|
-
targetRect = getRect();
|
|
186
|
-
parentRect = new Rect( parent ).excludeScrollbarsAndBorders();
|
|
187
|
-
|
|
188
|
-
if ( !parentRect.contains( targetRect ) ) {
|
|
189
|
-
if ( isAbove( targetRect, parentRect ) ) {
|
|
190
|
-
parent.scrollTop -= parentRect.top - targetRect.top;
|
|
191
|
-
} else if ( isBelow( targetRect, parentRect ) ) {
|
|
192
|
-
parent.scrollTop += targetRect.bottom - parentRect.bottom;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if ( isLeftOf( targetRect, parentRect ) ) {
|
|
196
|
-
parent.scrollLeft -= parentRect.left - targetRect.left;
|
|
197
|
-
} else if ( isRightOf( targetRect, parentRect ) ) {
|
|
198
|
-
parent.scrollLeft += targetRect.right - parentRect.right;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
parent = parent.parentNode;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// Determines if a given `Rect` extends beyond the bottom edge of the second `Rect`.
|
|
207
|
-
//
|
|
208
|
-
// @private
|
|
209
|
-
// @param {module:utils/dom/rect~Rect} firstRect
|
|
210
|
-
// @param {module:utils/dom/rect~Rect} secondRect
|
|
211
|
-
function isBelow( firstRect, secondRect ) {
|
|
212
|
-
return firstRect.bottom > secondRect.bottom;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// Determines if a given `Rect` extends beyond the top edge of the second `Rect`.
|
|
216
|
-
//
|
|
217
|
-
// @private
|
|
218
|
-
// @param {module:utils/dom/rect~Rect} firstRect
|
|
219
|
-
// @param {module:utils/dom/rect~Rect} secondRect
|
|
220
|
-
function isAbove( firstRect, secondRect ) {
|
|
221
|
-
return firstRect.top < secondRect.top;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// Determines if a given `Rect` extends beyond the left edge of the second `Rect`.
|
|
225
|
-
//
|
|
226
|
-
// @private
|
|
227
|
-
// @param {module:utils/dom/rect~Rect} firstRect
|
|
228
|
-
// @param {module:utils/dom/rect~Rect} secondRect
|
|
229
|
-
function isLeftOf( firstRect, secondRect ) {
|
|
230
|
-
return firstRect.left < secondRect.left;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// Determines if a given `Rect` extends beyond the right edge of the second `Rect`.
|
|
234
|
-
//
|
|
235
|
-
// @private
|
|
236
|
-
// @param {module:utils/dom/rect~Rect} firstRect
|
|
237
|
-
// @param {module:utils/dom/rect~Rect} secondRect
|
|
238
|
-
function isRightOf( firstRect, secondRect ) {
|
|
239
|
-
return firstRect.right > secondRect.right;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Returns the closest window of an element or range.
|
|
243
|
-
//
|
|
244
|
-
// @private
|
|
245
|
-
// @param {HTMLElement|Range} firstRect
|
|
246
|
-
// @returns {Window}
|
|
247
|
-
function getWindow( elementOrRange ) {
|
|
248
|
-
if ( isRange( elementOrRange ) ) {
|
|
249
|
-
return elementOrRange.startContainer.ownerDocument.defaultView;
|
|
250
|
-
} else {
|
|
251
|
-
return elementOrRange.ownerDocument.defaultView;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// Returns the closest parent of an element or DOM range.
|
|
256
|
-
//
|
|
257
|
-
// @private
|
|
258
|
-
// @param {HTMLElement|Range} firstRect
|
|
259
|
-
// @returns {HTMLelement}
|
|
260
|
-
function getParentElement( elementOrRange ) {
|
|
261
|
-
if ( isRange( elementOrRange ) ) {
|
|
262
|
-
let parent = elementOrRange.commonAncestorContainer;
|
|
263
|
-
|
|
264
|
-
// If a Range is attached to the Text, use the closest element ancestor.
|
|
265
|
-
if ( isText( parent ) ) {
|
|
266
|
-
parent = parent.parentNode;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
return parent;
|
|
270
|
-
} else {
|
|
271
|
-
return elementOrRange.parentNode;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// Returns the rect of an element or range residing in an iframe.
|
|
276
|
-
// The result rect is relative to the geometry of the passed window instance.
|
|
277
|
-
//
|
|
278
|
-
// @private
|
|
279
|
-
// @param {HTMLElement|Range} target Element or range which rect should be returned.
|
|
280
|
-
// @param {Window} relativeWindow A window the rect should be relative to.
|
|
281
|
-
// @returns {module:utils/dom/rect~Rect}
|
|
282
|
-
function getRectRelativeToWindow( target, relativeWindow ) {
|
|
283
|
-
const targetWindow = getWindow( target );
|
|
284
|
-
const rect = new Rect( target );
|
|
285
|
-
|
|
286
|
-
if ( targetWindow === relativeWindow ) {
|
|
287
|
-
return rect;
|
|
288
|
-
} else {
|
|
289
|
-
let currentWindow = targetWindow;
|
|
290
|
-
|
|
291
|
-
while ( currentWindow != relativeWindow ) {
|
|
292
|
-
const frame = currentWindow.frameElement;
|
|
293
|
-
const frameRect = new Rect( frame ).excludeScrollbarsAndBorders();
|
|
294
|
-
|
|
295
|
-
rect.moveBy( frameRect.left, frameRect.top );
|
|
296
|
-
|
|
297
|
-
currentWindow = currentWindow.parent;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
return rect;
|
|
302
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @module utils/dom/setdatainelement
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/* globals HTMLTextAreaElement */
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Sets data in a given element.
|
|
14
|
-
*
|
|
15
|
-
* @param {HTMLElement} el The element in which the data will be set.
|
|
16
|
-
* @param {String} data The data string.
|
|
17
|
-
*/
|
|
18
|
-
export default function setDataInElement( el, data ) {
|
|
19
|
-
if ( el instanceof HTMLTextAreaElement ) {
|
|
20
|
-
el.value = data;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
el.innerHTML = data;
|
|
24
|
-
}
|