@ckeditor/ckeditor5-minimap 41.2.0 → 41.3.0-alpha.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/dist/index.js ADDED
@@ -0,0 +1,600 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ import { Plugin } from '@ckeditor/ckeditor5-core/dist/index.js';
6
+ import { toUnit, global, Rect, findClosestScrollableAncestor } from '@ckeditor/ckeditor5-utils/dist/index.js';
7
+ import { IframeView, View } from '@ckeditor/ckeditor5-ui/dist/index.js';
8
+ import { DomConverter, Renderer } from '@ckeditor/ckeditor5-engine/dist/index.js';
9
+
10
+ /**
11
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
12
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
13
+ */
14
+ /**
15
+ * @module minimap/minimapiframeview
16
+ */
17
+ const toPx$1 = toUnit('px');
18
+ /**
19
+ * The internal `<iframe>` view that hosts the minimap content.
20
+ *
21
+ * @internal
22
+ */
23
+ class MinimapIframeView extends IframeView {
24
+ /**
25
+ * Creates an instance of the internal minimap iframe.
26
+ */
27
+ constructor(locale, options) {
28
+ super(locale);
29
+ const bind = this.bindTemplate;
30
+ this.set('top', 0);
31
+ this.set('height', 0);
32
+ this._options = options;
33
+ this.extendTemplate({
34
+ attributes: {
35
+ class: [
36
+ 'ck-minimap__iframe'
37
+ ],
38
+ style: {
39
+ top: bind.to('top', top => toPx$1(top)),
40
+ height: bind.to('height', height => toPx$1(height))
41
+ }
42
+ }
43
+ });
44
+ }
45
+ /**
46
+ * @inheritDoc
47
+ */
48
+ render() {
49
+ return super.render().then(() => {
50
+ this._prepareDocument();
51
+ });
52
+ }
53
+ /**
54
+ * Sets the new height of the iframe.
55
+ */
56
+ setHeight(newHeight) {
57
+ this.height = newHeight;
58
+ }
59
+ /**
60
+ * Sets the top offset of the iframe to move it around vertically.
61
+ */
62
+ setTopOffset(newOffset) {
63
+ this.top = newOffset;
64
+ }
65
+ /**
66
+ * Sets the internal structure of the `<iframe>` readying it to display the
67
+ * minimap element.
68
+ */
69
+ _prepareDocument() {
70
+ const iframeDocument = this.element.contentWindow.document;
71
+ const domRootClone = iframeDocument.adoptNode(this._options.domRootClone);
72
+ const boxStyles = this._options.useSimplePreview ? `
73
+ .ck.ck-editor__editable_inline img {
74
+ filter: contrast( 0 );
75
+ }
76
+
77
+ p, li, a, figcaption, span {
78
+ background: hsl(0, 0%, 80%) !important;
79
+ color: hsl(0, 0%, 80%) !important;
80
+ }
81
+
82
+ h1, h2, h3, h4 {
83
+ background: hsl(0, 0%, 60%) !important;
84
+ color: hsl(0, 0%, 60%) !important;
85
+ }
86
+ ` : '';
87
+ const pageStyles = this._options.pageStyles.map(definition => {
88
+ if (typeof definition === 'string') {
89
+ return `<style>${definition}</style>`;
90
+ }
91
+ else {
92
+ return `<link rel="stylesheet" type="text/css" href="${definition.href}">`;
93
+ }
94
+ }).join('\n');
95
+ const html = `<!DOCTYPE html><html lang="en">
96
+ <head>
97
+ <meta charset="utf-8">
98
+ <meta name="viewport" content="width=device-width, initial-scale=1">
99
+ ${pageStyles}
100
+ <style>
101
+ html, body {
102
+ margin: 0 !important;
103
+ padding: 0 !important;
104
+ }
105
+
106
+ html {
107
+ overflow: hidden;
108
+ }
109
+
110
+ body {
111
+ transform: scale( ${this._options.scaleRatio} );
112
+ transform-origin: 0 0;
113
+ overflow: visible;
114
+ }
115
+
116
+ .ck.ck-editor__editable_inline {
117
+ margin: 0 !important;
118
+ border-color: transparent !important;
119
+ outline-color: transparent !important;
120
+ box-shadow: none !important;
121
+ }
122
+
123
+ .ck.ck-content {
124
+ background: white;
125
+ }
126
+
127
+ ${boxStyles}
128
+ </style>
129
+ </head>
130
+ <body class="${this._options.extraClasses || ''}"></body>
131
+ </html>`;
132
+ iframeDocument.open();
133
+ iframeDocument.write(html);
134
+ iframeDocument.close();
135
+ iframeDocument.body.appendChild(domRootClone);
136
+ }
137
+ }
138
+
139
+ /**
140
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
141
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
142
+ */
143
+ /**
144
+ * @module minimap/minimappositiontrackerview
145
+ */
146
+ const toPx = toUnit('px');
147
+ /**
148
+ * The position tracker visualizing the visible subset of the content. Displayed over the minimap.
149
+ *
150
+ * @internal
151
+ */
152
+ class MinimapPositionTrackerView extends View {
153
+ constructor(locale) {
154
+ super(locale);
155
+ const bind = this.bindTemplate;
156
+ this.set('height', 0);
157
+ this.set('top', 0);
158
+ this.set('scrollProgress', 0);
159
+ this.set('_isDragging', false);
160
+ this.setTemplate({
161
+ tag: 'div',
162
+ attributes: {
163
+ class: [
164
+ 'ck',
165
+ 'ck-minimap__position-tracker',
166
+ bind.if('_isDragging', 'ck-minimap__position-tracker_dragging')
167
+ ],
168
+ style: {
169
+ top: bind.to('top', top => toPx(top)),
170
+ height: bind.to('height', height => toPx(height))
171
+ },
172
+ 'data-progress': bind.to('scrollProgress')
173
+ },
174
+ on: {
175
+ mousedown: bind.to(() => {
176
+ this._isDragging = true;
177
+ })
178
+ }
179
+ });
180
+ }
181
+ /**
182
+ * @inheritDoc
183
+ */
184
+ render() {
185
+ super.render();
186
+ this.listenTo(global.document, 'mousemove', (evt, data) => {
187
+ if (!this._isDragging) {
188
+ return;
189
+ }
190
+ this.fire('drag', data.movementY);
191
+ }, { useCapture: true });
192
+ this.listenTo(global.document, 'mouseup', () => {
193
+ this._isDragging = false;
194
+ }, { useCapture: true });
195
+ }
196
+ /**
197
+ * Sets the new height of the tracker to visualize the subset of the content visible to the user.
198
+ */
199
+ setHeight(newHeight) {
200
+ this.height = newHeight;
201
+ }
202
+ /**
203
+ * Sets the top offset of the tracker to move it around vertically.
204
+ */
205
+ setTopOffset(newOffset) {
206
+ this.top = newOffset;
207
+ }
208
+ /**
209
+ * Sets the scroll progress (in %) to inform the user using a label when the tracker is being dragged.
210
+ */
211
+ setScrollProgress(newProgress) {
212
+ this.scrollProgress = newProgress;
213
+ }
214
+ }
215
+
216
+ /**
217
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
218
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
219
+ */
220
+ /**
221
+ * @module minimap/minimapview
222
+ */
223
+ /**
224
+ * The main view of the minimap. It renders the original content but scaled down with a tracker element
225
+ * visualizing the subset of the content visible to the user and allowing interactions (scrolling, dragging).
226
+ *
227
+ * @internal
228
+ */
229
+ class MinimapView extends View {
230
+ /**
231
+ * Creates an instance of the minimap view.
232
+ */
233
+ constructor({ locale, scaleRatio, pageStyles, extraClasses, useSimplePreview, domRootClone }) {
234
+ super(locale);
235
+ const bind = this.bindTemplate;
236
+ this._positionTrackerView = new MinimapPositionTrackerView(locale);
237
+ this._positionTrackerView.delegate('drag').to(this);
238
+ this._scaleRatio = scaleRatio;
239
+ this._minimapIframeView = new MinimapIframeView(locale, {
240
+ useSimplePreview,
241
+ pageStyles,
242
+ extraClasses,
243
+ scaleRatio,
244
+ domRootClone
245
+ });
246
+ this.setTemplate({
247
+ tag: 'div',
248
+ attributes: {
249
+ class: [
250
+ 'ck',
251
+ 'ck-minimap'
252
+ ]
253
+ },
254
+ children: [
255
+ this._positionTrackerView
256
+ ],
257
+ on: {
258
+ click: bind.to(this._handleMinimapClick.bind(this)),
259
+ wheel: bind.to(this._handleMinimapMouseWheel.bind(this))
260
+ }
261
+ });
262
+ }
263
+ /**
264
+ * @inheritDoc
265
+ */
266
+ destroy() {
267
+ this._minimapIframeView.destroy();
268
+ super.destroy();
269
+ }
270
+ /**
271
+ * Returns the DOM {@link module:utils/dom/rect~Rect} height of the minimap.
272
+ */
273
+ get height() {
274
+ return new Rect(this.element).height;
275
+ }
276
+ /**
277
+ * Returns the number of available space (pixels) the position tracker (visible subset of the content) can use to scroll vertically.
278
+ */
279
+ get scrollHeight() {
280
+ return Math.max(0, Math.min(this.height, this._minimapIframeView.height) - this._positionTrackerView.height);
281
+ }
282
+ /**
283
+ * @inheritDoc
284
+ */
285
+ render() {
286
+ super.render();
287
+ this._minimapIframeView.render();
288
+ this.element.appendChild(this._minimapIframeView.element);
289
+ }
290
+ /**
291
+ * Sets the new height of the minimap (in px) to respond to the changes in the original editing DOM root.
292
+ *
293
+ * **Note**:The provided value should be the `offsetHeight` of the original editing DOM root.
294
+ */
295
+ setContentHeight(newHeight) {
296
+ this._minimapIframeView.setHeight(newHeight * this._scaleRatio);
297
+ }
298
+ /**
299
+ * Sets the minimap scroll progress.
300
+ *
301
+ * The minimap scroll progress is linked to the original editing DOM root and its scrollable container (ancestor).
302
+ * Changing the progress will alter the vertical position of the minimap (and its position tracker) and give the user an accurate
303
+ * overview of the visible document.
304
+ *
305
+ * **Note**: The value should be between 0 and 1. 0 when the DOM root has not been scrolled, 1 when the
306
+ * scrolling has reached the end.
307
+ */
308
+ setScrollProgress(newScrollProgress) {
309
+ const iframeView = this._minimapIframeView;
310
+ const positionTrackerView = this._positionTrackerView;
311
+ // The scrolling should end when the bottom edge of the iframe touches the bottom edge of the minimap.
312
+ if (iframeView.height < this.height) {
313
+ iframeView.setTopOffset(0);
314
+ positionTrackerView.setTopOffset((iframeView.height - positionTrackerView.height) * newScrollProgress);
315
+ }
316
+ else {
317
+ const totalOffset = iframeView.height - this.height;
318
+ iframeView.setTopOffset(-totalOffset * newScrollProgress);
319
+ positionTrackerView.setTopOffset((this.height - positionTrackerView.height) * newScrollProgress);
320
+ }
321
+ positionTrackerView.setScrollProgress(Math.round(newScrollProgress * 100));
322
+ }
323
+ /**
324
+ * Sets the new height of the tracker (in px) to visualize the subset of the content visible to the user.
325
+ */
326
+ setPositionTrackerHeight(trackerHeight) {
327
+ this._positionTrackerView.setHeight(trackerHeight * this._scaleRatio);
328
+ }
329
+ /**
330
+ * @param data DOM event data
331
+ */
332
+ _handleMinimapClick(data) {
333
+ const positionTrackerView = this._positionTrackerView;
334
+ if (data.target === positionTrackerView.element) {
335
+ return;
336
+ }
337
+ const trackerViewRect = new Rect(positionTrackerView.element);
338
+ const diff = data.clientY - trackerViewRect.top - trackerViewRect.height / 2;
339
+ const percentage = diff / this._minimapIframeView.height;
340
+ this.fire('click', percentage);
341
+ }
342
+ /**
343
+ * @param data DOM event data
344
+ */
345
+ _handleMinimapMouseWheel(data) {
346
+ this.fire('drag', data.deltaY * this._scaleRatio);
347
+ }
348
+ }
349
+
350
+ /**
351
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
352
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
353
+ */
354
+ /* global CSSMediaRule */
355
+ /**
356
+ * @module minimap/utils
357
+ */
358
+ /**
359
+ * Clones the editing view DOM root by using a dedicated pair of {@link module:engine/view/renderer~Renderer} and
360
+ * {@link module:engine/view/domconverter~DomConverter}. The DOM root clone updates incrementally to stay in sync with the
361
+ * source root.
362
+ *
363
+ * @internal
364
+ * @param editor The editor instance the original editing root belongs to.
365
+ * @param rootName The name of the root to clone.
366
+ * @returns The editing root DOM clone element.
367
+ */
368
+ function cloneEditingViewDomRoot(editor, rootName) {
369
+ const viewDocument = editor.editing.view.document;
370
+ const viewRoot = viewDocument.getRoot(rootName);
371
+ const domConverter = new DomConverter(viewDocument);
372
+ const renderer = new Renderer(domConverter, viewDocument.selection);
373
+ const domRootClone = editor.editing.view.getDomRoot().cloneNode();
374
+ domConverter.bindElements(domRootClone, viewRoot);
375
+ renderer.markToSync('children', viewRoot);
376
+ renderer.markToSync('attributes', viewRoot);
377
+ viewRoot.on('change:children', (evt, node) => renderer.markToSync('children', node));
378
+ viewRoot.on('change:attributes', (evt, node) => renderer.markToSync('attributes', node));
379
+ viewRoot.on('change:text', (evt, node) => renderer.markToSync('text', node));
380
+ renderer.render();
381
+ editor.editing.view.on('render', () => renderer.render());
382
+ // TODO: Cleanup after destruction.
383
+ editor.on('destroy', () => {
384
+ domConverter.unbindDomElement(domRootClone);
385
+ });
386
+ return domRootClone;
387
+ }
388
+ /**
389
+ * Harvests all web page styles, for instance, to allow re-using them in an `<iframe>` preserving the look of the content.
390
+ *
391
+ * The returned data format is as follows:
392
+ *
393
+ * ```ts
394
+ * [
395
+ * 'p { color: red; ... } h2 { font-size: 2em; ... } ...',
396
+ * '.spacing { padding: 1em; ... }; ...',
397
+ * '...',
398
+ * { href: 'http://link.to.external.stylesheet' },
399
+ * { href: '...' }
400
+ * ]
401
+ * ```
402
+ *
403
+ * **Note**: For stylesheets with `href` different than window origin, an object is returned because
404
+ * accessing rules of these styles may cause CORS errors (depending on the configuration of the web page).
405
+ *
406
+ * @internal
407
+ */
408
+ function getPageStyles() {
409
+ return Array.from(global.document.styleSheets)
410
+ .map(styleSheet => {
411
+ // CORS
412
+ if (styleSheet.href && !styleSheet.href.startsWith(global.window.location.origin)) {
413
+ return { href: styleSheet.href };
414
+ }
415
+ return Array.from(styleSheet.cssRules)
416
+ .filter(rule => !(rule instanceof CSSMediaRule))
417
+ .map(rule => rule.cssText)
418
+ .join(' \n');
419
+ });
420
+ }
421
+ /**
422
+ * Gets dimensions rectangle according to passed DOM element. Returns whole window's size for `body` element.
423
+ *
424
+ * @internal
425
+ */
426
+ function getDomElementRect(domElement) {
427
+ return new Rect(domElement === global.document.body ? global.window : domElement);
428
+ }
429
+ /**
430
+ * Gets client height according to passed DOM element. Returns window's height for `body` element.
431
+ *
432
+ * @internal
433
+ */
434
+ function getClientHeight(domElement) {
435
+ return domElement === global.document.body ? global.window.innerHeight : domElement.clientHeight;
436
+ }
437
+ /**
438
+ * Returns the DOM element itself if it's not a `body` element, whole window otherwise.
439
+ *
440
+ * @internal
441
+ */
442
+ function getScrollable(domElement) {
443
+ return domElement === global.document.body ? global.window : domElement;
444
+ }
445
+
446
+ /**
447
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
448
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
449
+ */
450
+ /**
451
+ * @module minimap/minimap
452
+ */
453
+ /**
454
+ * The content minimap feature.
455
+ */
456
+ class Minimap extends Plugin {
457
+ /**
458
+ * @inheritDoc
459
+ */
460
+ static get pluginName() {
461
+ return 'Minimap';
462
+ }
463
+ /**
464
+ * @inheritDoc
465
+ */
466
+ init() {
467
+ const editor = this.editor;
468
+ this._minimapView = null;
469
+ this._scrollableRootAncestor = null;
470
+ this.listenTo(editor.ui, 'ready', this._onUiReady.bind(this));
471
+ }
472
+ /**
473
+ * @inheritDoc
474
+ */
475
+ destroy() {
476
+ super.destroy();
477
+ this._minimapView.destroy();
478
+ this._minimapView.element.remove();
479
+ }
480
+ /**
481
+ * Initializes the minimap view element and starts the layout synchronization
482
+ * on the editing view `render` event.
483
+ */
484
+ _onUiReady() {
485
+ const editor = this.editor;
486
+ // TODO: This will not work with the multi-root editor.
487
+ const editingRootElement = this._editingRootElement = editor.ui.getEditableElement();
488
+ this._scrollableRootAncestor = findClosestScrollableAncestor(editingRootElement);
489
+ // DOM root element is not yet attached to the document.
490
+ if (!editingRootElement.ownerDocument.body.contains(editingRootElement)) {
491
+ editor.ui.once('update', this._onUiReady.bind(this));
492
+ return;
493
+ }
494
+ this._initializeMinimapView();
495
+ this.listenTo(editor.editing.view, 'render', () => {
496
+ if (editor.state !== 'ready') {
497
+ return;
498
+ }
499
+ this._syncMinimapToEditingRootScrollPosition();
500
+ });
501
+ this._syncMinimapToEditingRootScrollPosition();
502
+ }
503
+ /**
504
+ * Initializes the minimap view and attaches listeners that make it responsive to the environment (document)
505
+ * but also allow the minimap to control the document (scroll position).
506
+ */
507
+ _initializeMinimapView() {
508
+ const editor = this.editor;
509
+ const locale = editor.locale;
510
+ const useSimplePreview = editor.config.get('minimap.useSimplePreview');
511
+ // TODO: Throw an error if there is no `minimap` in config.
512
+ const minimapContainerElement = editor.config.get('minimap.container');
513
+ const scrollableRootAncestor = this._scrollableRootAncestor;
514
+ // TODO: This should be dynamic, the root width could change as the viewport scales if not fixed unit.
515
+ const editingRootElementWidth = getDomElementRect(this._editingRootElement).width;
516
+ const minimapContainerWidth = getDomElementRect(minimapContainerElement).width;
517
+ const minimapScaleRatio = minimapContainerWidth / editingRootElementWidth;
518
+ const minimapView = this._minimapView = new MinimapView({
519
+ locale,
520
+ scaleRatio: minimapScaleRatio,
521
+ pageStyles: getPageStyles(),
522
+ extraClasses: editor.config.get('minimap.extraClasses'),
523
+ useSimplePreview,
524
+ domRootClone: cloneEditingViewDomRoot(editor)
525
+ });
526
+ minimapView.render();
527
+ // Scrollable ancestor scroll -> minimap position update.
528
+ minimapView.listenTo(global.document, 'scroll', (evt, data) => {
529
+ if (scrollableRootAncestor === global.document.body) {
530
+ if (data.target !== global.document) {
531
+ return;
532
+ }
533
+ }
534
+ else if (data.target !== scrollableRootAncestor) {
535
+ return;
536
+ }
537
+ this._syncMinimapToEditingRootScrollPosition();
538
+ }, { useCapture: true, usePassive: true });
539
+ // Viewport resize -> minimap position update.
540
+ minimapView.listenTo(global.window, 'resize', () => {
541
+ this._syncMinimapToEditingRootScrollPosition();
542
+ });
543
+ // Dragging the visible content area -> document (scrollable) position update.
544
+ minimapView.on('drag', (evt, movementY) => {
545
+ let movementYPercentage;
546
+ if (minimapView.scrollHeight === 0) {
547
+ movementYPercentage = 0;
548
+ }
549
+ else {
550
+ movementYPercentage = movementY / minimapView.scrollHeight;
551
+ }
552
+ const absoluteScrollProgress = movementYPercentage *
553
+ (scrollableRootAncestor.scrollHeight - getClientHeight(scrollableRootAncestor));
554
+ const scrollable = getScrollable(scrollableRootAncestor);
555
+ scrollable.scrollBy(0, Math.round(absoluteScrollProgress));
556
+ });
557
+ // Clicking the minimap -> center the document (scrollable) to the corresponding position.
558
+ minimapView.on('click', (evt, percentage) => {
559
+ const absoluteScrollProgress = percentage * scrollableRootAncestor.scrollHeight;
560
+ const scrollable = getScrollable(scrollableRootAncestor);
561
+ scrollable.scrollBy(0, Math.round(absoluteScrollProgress));
562
+ });
563
+ minimapContainerElement.appendChild(minimapView.element);
564
+ }
565
+ /**
566
+ * @private
567
+ */
568
+ _syncMinimapToEditingRootScrollPosition() {
569
+ const editingRootElement = this._editingRootElement;
570
+ const minimapView = this._minimapView;
571
+ minimapView.setContentHeight(editingRootElement.offsetHeight);
572
+ const editingRootRect = getDomElementRect(editingRootElement);
573
+ const scrollableRootAncestorRect = getDomElementRect(this._scrollableRootAncestor);
574
+ let scrollProgress;
575
+ // @if CK_DEBUG_MINIMAP // RectDrawer.clear();
576
+ // @if CK_DEBUG_MINIMAP // RectDrawer.draw( scrollableRootAncestorRect, { outlineColor: 'red' }, 'scrollableRootAncestor' );
577
+ // @if CK_DEBUG_MINIMAP // RectDrawer.draw( editingRootRect, { outlineColor: 'green' }, 'editingRoot' );
578
+ // The root is completely visible in the scrollable ancestor.
579
+ if (scrollableRootAncestorRect.contains(editingRootRect)) {
580
+ scrollProgress = 0;
581
+ }
582
+ else {
583
+ if (editingRootRect.top > scrollableRootAncestorRect.top) {
584
+ scrollProgress = 0;
585
+ }
586
+ else {
587
+ scrollProgress = (editingRootRect.top - scrollableRootAncestorRect.top) /
588
+ (scrollableRootAncestorRect.height - editingRootRect.height);
589
+ scrollProgress = Math.max(0, Math.min(scrollProgress, 1));
590
+ }
591
+ }
592
+ // The intersection helps to change the tracker height when there is a lot of padding around the root.
593
+ // Note: It is **essential** that the height is set first because the progress depends on the correct tracker height.
594
+ minimapView.setPositionTrackerHeight(scrollableRootAncestorRect.getIntersection(editingRootRect).height);
595
+ minimapView.setScrollProgress(scrollProgress);
596
+ }
597
+ }
598
+
599
+ export { Minimap };
600
+ //# sourceMappingURL=index.js.map