/
stickypanelview.ts
392 lines (338 loc) · 12.1 KB
/
stickypanelview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module ui/panel/sticky/stickypanelview
*/
import View from '../../view.js';
import Template from '../../template.js';
import type ViewCollection from '../../viewcollection.js';
import {
type Locale,
type ObservableChangeEvent,
global,
toUnit,
Rect
} from '@ckeditor/ckeditor5-utils';
// @if CK_DEBUG_STICKYPANEL // const {
// @if CK_DEBUG_STICKYPANEL // default: RectDrawer,
// @if CK_DEBUG_STICKYPANEL // diagonalStylesBlack
// @if CK_DEBUG_STICKYPANEL // } = require( '@ckeditor/ckeditor5-utils/tests/_utils/rectdrawer' );
import '../../../theme/components/panel/stickypanel.css';
const toPx = toUnit( 'px' );
/**
* The sticky panel view class.
*/
export default class StickyPanelView extends View {
/**
* Collection of the child views which creates balloon panel contents.
*/
public readonly content: ViewCollection;
/**
* Controls whether the sticky panel should be active.
*
* @readonly
* @observable
*/
declare public isActive: boolean;
/**
* Controls whether the sticky panel is in the "sticky" state.
*
* @readonly
* @observable
*/
declare public isSticky: boolean;
/**
* The limiter element for the sticky panel instance. Its bounding rect limits
* the "stickyness" of the panel, i.e. when the panel reaches the bottom
* edge of the limiter, it becomes sticky to that edge and does not float
* off the limiter. It is mandatory for the panel to work properly and once
* set, it cannot be changed.
*
* @readonly
* @observable
*/
declare public limiterElement: HTMLElement | null;
/**
* The offset from the bottom edge of {@link #limiterElement}
* which stops the panel from stickying any further to prevent limiter's content
* from being completely covered.
*
* @readonly
* @observable
* @default 50
*/
declare public limiterBottomOffset: number;
/**
* The offset from the top edge of the web browser's viewport which makes the
* panel become sticky. The default value is `0`, which means the panel becomes
* sticky when it's upper edge touches the top of the page viewport.
*
* This attribute is useful when the web page has UI elements positioned to the top
* either using `position: fixed` or `position: sticky`, which would cover the
* sticky panel or vice–versa (depending on the `z-index` hierarchy).
*
* Bound to {@link module:ui/editorui/editorui~EditorUI#viewportOffset `EditorUI#viewportOffset`}.
*
* If {@link module:core/editor/editorconfig~EditorConfig#ui `EditorConfig#ui.viewportOffset.top`} is defined, then
* it will override the default value.
*
* @observable
* @default 0
*/
declare public viewportTopOffset: number;
/**
* The panel which accepts children into {@link #content} collection.
* Also an element which is positioned when {@link #isSticky}.
*/
public contentPanelElement: HTMLElement;
/**
* Controls the `margin-left` CSS style of the panel.
*
* @private
* @readonly
* @observable
*/
declare public _marginLeft: string | null;
/**
* Set `true` if the sticky panel reached the bottom edge of the
* {@link #limiterElement}.
*
* @private
* @readonly
* @observable
*/
declare public _isStickyToTheBottomOfLimiter: boolean;
/**
* The `top` CSS position of the panel when it is sticky to the top of the viewport or scrollable
* ancestors of the {@link #limiterElement}.
*
* @private
* @readonly
* @observable
*/
declare public _stickyTopOffset: number | null;
/**
* The `bottom` CSS position of the panel when it is sticky to the bottom of the {@link #limiterElement}.
*
* @private
* @readonly
* @observable
*/
declare public _stickyBottomOffset: number | null;
/**
* A dummy element which visually fills the space as long as the
* actual panel is sticky. It prevents flickering of the UI.
*/
private _contentPanelPlaceholder: HTMLElement;
/**
* @inheritDoc
*/
constructor( locale?: Locale ) {
super( locale );
const bind = this.bindTemplate;
this.set( 'isActive', false );
this.set( 'isSticky', false );
this.set( 'limiterElement', null );
this.set( 'limiterBottomOffset', 50 );
this.set( 'viewportTopOffset', 0 );
this.set( '_marginLeft', null );
this.set( '_isStickyToTheBottomOfLimiter', false );
this.set( '_stickyTopOffset', null );
this.set( '_stickyBottomOffset', null );
this.content = this.createCollection();
this._contentPanelPlaceholder = new Template( {
tag: 'div',
attributes: {
class: [
'ck',
'ck-sticky-panel__placeholder'
],
style: {
display: bind.to( 'isSticky', isSticky => isSticky ? 'block' : 'none' ),
height: bind.to( 'isSticky', isSticky => {
return isSticky ? toPx( this._contentPanelRect.height ) : null;
} )
}
}
} ).render() as HTMLElement;
this.contentPanelElement = new Template( {
tag: 'div',
attributes: {
class: [
'ck',
'ck-sticky-panel__content',
// Toggle class of the panel when "sticky" state changes in the view.
bind.if( 'isSticky', 'ck-sticky-panel__content_sticky' ),
bind.if( '_isStickyToTheBottomOfLimiter', 'ck-sticky-panel__content_sticky_bottom-limit' )
],
style: {
width: bind.to( 'isSticky', isSticky => {
return isSticky ? toPx( this._contentPanelPlaceholder.getBoundingClientRect().width ) : null;
} ),
top: bind.to( '_stickyTopOffset', value => value ? toPx( value ) : value ),
bottom: bind.to( '_stickyBottomOffset', value => value ? toPx( value ) : value ),
marginLeft: bind.to( '_marginLeft' )
}
},
children: this.content
} ).render() as HTMLElement;
this.setTemplate( {
tag: 'div',
attributes: {
class: [
'ck',
'ck-sticky-panel'
]
},
children: [
this._contentPanelPlaceholder,
this.contentPanelElement
]
} );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
// Check if the panel should go into the sticky state immediately.
this.checkIfShouldBeSticky();
// Update sticky state of the panel as the window and ancestors are being scrolled.
this.listenTo( global.document, 'scroll', () => {
this.checkIfShouldBeSticky();
}, { useCapture: true } );
// Synchronize with `model.isActive` because sticking an inactive panel is pointless.
this.listenTo<ObservableChangeEvent>( this, 'change:isActive', () => {
this.checkIfShouldBeSticky();
} );
}
/**
* Analyzes the environment to decide whether the panel should be sticky or not.
* Then handles the positioning of the panel.
*/
public checkIfShouldBeSticky(): void {
// @if CK_DEBUG_STICKYPANEL // RectDrawer.clear();
if ( !this.limiterElement || !this.isActive ) {
this._unstick();
return;
}
const limiterRect = new Rect( this.limiterElement );
let visibleLimiterRect = limiterRect.getVisible();
if ( visibleLimiterRect ) {
const windowRect = new Rect( global.window );
windowRect.top += this.viewportTopOffset;
windowRect.height -= this.viewportTopOffset;
visibleLimiterRect = visibleLimiterRect.getIntersection( windowRect );
}
// @if CK_DEBUG_STICKYPANEL // if ( visibleLimiterRect ) {
// @if CK_DEBUG_STICKYPANEL // RectDrawer.draw( visibleLimiterRect,
// @if CK_DEBUG_STICKYPANEL // { outlineWidth: '3px', opacity: '.8', outlineColor: 'red', outlineOffset: '-3px' },
// @if CK_DEBUG_STICKYPANEL // 'Visible anc'
// @if CK_DEBUG_STICKYPANEL // );
// @if CK_DEBUG_STICKYPANEL // }
// @if CK_DEBUG_STICKYPANEL //
// @if CK_DEBUG_STICKYPANEL // RectDrawer.draw( limiterRect,
// @if CK_DEBUG_STICKYPANEL // { outlineWidth: '3px', opacity: '.8', outlineColor: 'green', outlineOffset: '-3px' },
// @if CK_DEBUG_STICKYPANEL // 'Limiter'
// @if CK_DEBUG_STICKYPANEL // );
// Stick the panel only if
// * the limiter's ancestors are intersecting with each other so that some of their rects are visible,
// * and the limiter's top edge is above the visible ancestors' top edge.
if ( visibleLimiterRect && limiterRect.top < visibleLimiterRect.top ) {
// @if CK_DEBUG_STICKYPANEL // RectDrawer.draw( visibleLimiterRect,
// @if CK_DEBUG_STICKYPANEL // { outlineWidth: '3px', opacity: '.8', outlineColor: 'fuchsia', outlineOffset: '-3px',
// @if CK_DEBUG_STICKYPANEL // backgroundColor: 'rgba(255, 0, 255, .3)' },
// @if CK_DEBUG_STICKYPANEL // 'Visible limiter'
// @if CK_DEBUG_STICKYPANEL // );
const visibleLimiterTop = visibleLimiterRect.top;
// Check if there's a change the panel can be sticky to the bottom of the limiter.
if ( visibleLimiterTop + this._contentPanelRect.height + this.limiterBottomOffset > visibleLimiterRect.bottom ) {
const stickyBottomOffset = Math.max( limiterRect.bottom - visibleLimiterRect.bottom, 0 ) + this.limiterBottomOffset;
// @if CK_DEBUG_STICKYPANEL // const stickyBottomOffsetRect = new Rect( {
// @if CK_DEBUG_STICKYPANEL // top: limiterRect.bottom - stickyBottomOffset, left: 0, right: 2000,
// @if CK_DEBUG_STICKYPANEL // bottom: limiterRect.bottom - stickyBottomOffset, width: 2000, height: 1
// @if CK_DEBUG_STICKYPANEL // } );
// @if CK_DEBUG_STICKYPANEL // RectDrawer.draw( stickyBottomOffsetRect,
// @if CK_DEBUG_STICKYPANEL // { outlineWidth: '1px', opacity: '.8', outlineColor: 'black' },
// @if CK_DEBUG_STICKYPANEL // 'Sticky bottom offset'
// @if CK_DEBUG_STICKYPANEL // );
// Check if sticking the panel to the bottom of the limiter does not cause it to suddenly
// move upwards if there's not enough space for it.
if ( limiterRect.bottom - stickyBottomOffset > limiterRect.top + this._contentPanelRect.height ) {
this._stickToBottomOfLimiter( stickyBottomOffset );
} else {
this._unstick();
}
} else {
if ( this._contentPanelRect.height + this.limiterBottomOffset < limiterRect.height ) {
this._stickToTopOfAncestors( visibleLimiterTop );
} else {
this._unstick();
}
}
} else {
this._unstick();
}
// @if CK_DEBUG_STICKYPANEL // console.clear();
// @if CK_DEBUG_STICKYPANEL // console.log( 'isSticky', this.isSticky );
// @if CK_DEBUG_STICKYPANEL // console.log( '_isStickyToTheBottomOfLimiter', this._isStickyToTheBottomOfLimiter );
// @if CK_DEBUG_STICKYPANEL // console.log( '_stickyTopOffset', this._stickyTopOffset );
// @if CK_DEBUG_STICKYPANEL // console.log( '_stickyBottomOffset', this._stickyBottomOffset );
// @if CK_DEBUG_STICKYPANEL // if ( visibleLimiterRect ) {
// @if CK_DEBUG_STICKYPANEL // RectDrawer.draw( visibleLimiterRect,
// @if CK_DEBUG_STICKYPANEL // { ...diagonalStylesBlack,
// @if CK_DEBUG_STICKYPANEL // outlineWidth: '3px', opacity: '.8', outlineColor: 'orange', outlineOffset: '-3px',
// @if CK_DEBUG_STICKYPANEL // backgroundColor: 'rgba(0, 0, 255, .2)' },
// @if CK_DEBUG_STICKYPANEL // 'visibleLimiterRect'
// @if CK_DEBUG_STICKYPANEL // );
// @if CK_DEBUG_STICKYPANEL // }
}
/**
* Sticks the panel at the given CSS `top` offset.
*
* @private
* @param topOffset
*/
private _stickToTopOfAncestors( topOffset: number ) {
this.isSticky = true;
this._isStickyToTheBottomOfLimiter = false;
this._stickyTopOffset = topOffset;
this._stickyBottomOffset = null;
this._marginLeft = toPx( -global.window.scrollX );
}
/**
* Sticks the panel at the bottom of the limiter with a given CSS `bottom` offset.
*
* @private
* @param stickyBottomOffset
*/
private _stickToBottomOfLimiter( stickyBottomOffset: number ) {
this.isSticky = true;
this._isStickyToTheBottomOfLimiter = true;
this._stickyTopOffset = null;
this._stickyBottomOffset = stickyBottomOffset;
this._marginLeft = toPx( -global.window.scrollX );
}
/**
* Unsticks the panel putting it back to its original position.
*
* @private
*/
private _unstick() {
this.isSticky = false;
this._isStickyToTheBottomOfLimiter = false;
this._stickyTopOffset = null;
this._stickyBottomOffset = null;
this._marginLeft = null;
}
/**
* Returns the bounding rect of the {@link #contentPanelElement}.
*
* @private
*/
private get _contentPanelRect(): Rect {
return new Rect( this.contentPanelElement );
}
}