@ckeditor/ckeditor5-minimap 39.0.2 → 40.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.
@@ -1,137 +1,137 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- * @module minimap/minimapview
7
- */
8
- import { View } from 'ckeditor5/src/ui';
9
- import { Rect } from 'ckeditor5/src/utils';
10
- import MinimapIframeView from './minimapiframeview';
11
- import MinimapPositionTrackerView from './minimappositiontrackerview';
12
- /**
13
- * The main view of the minimap. It renders the original content but scaled down with a tracker element
14
- * visualizing the subset of the content visible to the user and allowing interactions (scrolling, dragging).
15
- *
16
- * @internal
17
- */
18
- export default class MinimapView extends View {
19
- /**
20
- * Creates an instance of the minimap view.
21
- */
22
- constructor({ locale, scaleRatio, pageStyles, extraClasses, useSimplePreview, domRootClone }) {
23
- super(locale);
24
- const bind = this.bindTemplate;
25
- this._positionTrackerView = new MinimapPositionTrackerView(locale);
26
- this._positionTrackerView.delegate('drag').to(this);
27
- this._scaleRatio = scaleRatio;
28
- this._minimapIframeView = new MinimapIframeView(locale, {
29
- useSimplePreview,
30
- pageStyles,
31
- extraClasses,
32
- scaleRatio,
33
- domRootClone
34
- });
35
- this.setTemplate({
36
- tag: 'div',
37
- attributes: {
38
- class: [
39
- 'ck',
40
- 'ck-minimap'
41
- ]
42
- },
43
- children: [
44
- this._positionTrackerView
45
- ],
46
- on: {
47
- click: bind.to(this._handleMinimapClick.bind(this)),
48
- wheel: bind.to(this._handleMinimapMouseWheel.bind(this))
49
- }
50
- });
51
- }
52
- /**
53
- * @inheritDoc
54
- */
55
- destroy() {
56
- this._minimapIframeView.destroy();
57
- super.destroy();
58
- }
59
- /**
60
- * Returns the DOM {@link module:utils/dom/rect~Rect} height of the minimap.
61
- */
62
- get height() {
63
- return new Rect(this.element).height;
64
- }
65
- /**
66
- * Returns the number of available space (pixels) the position tracker (visible subset of the content) can use to scroll vertically.
67
- */
68
- get scrollHeight() {
69
- return Math.max(0, Math.min(this.height, this._minimapIframeView.height) - this._positionTrackerView.height);
70
- }
71
- /**
72
- * @inheritDoc
73
- */
74
- render() {
75
- super.render();
76
- this._minimapIframeView.render();
77
- this.element.appendChild(this._minimapIframeView.element);
78
- }
79
- /**
80
- * Sets the new height of the minimap (in px) to respond to the changes in the original editing DOM root.
81
- *
82
- * **Note**:The provided value should be the `offsetHeight` of the original editing DOM root.
83
- */
84
- setContentHeight(newHeight) {
85
- this._minimapIframeView.setHeight(newHeight * this._scaleRatio);
86
- }
87
- /**
88
- * Sets the minimap scroll progress.
89
- *
90
- * The minimap scroll progress is linked to the original editing DOM root and its scrollable container (ancestor).
91
- * Changing the progress will alter the vertical position of the minimap (and its position tracker) and give the user an accurate
92
- * overview of the visible document.
93
- *
94
- * **Note**: The value should be between 0 and 1. 0 when the DOM root has not been scrolled, 1 when the
95
- * scrolling has reached the end.
96
- */
97
- setScrollProgress(newScrollProgress) {
98
- const iframeView = this._minimapIframeView;
99
- const positionTrackerView = this._positionTrackerView;
100
- // The scrolling should end when the bottom edge of the iframe touches the bottom edge of the minimap.
101
- if (iframeView.height < this.height) {
102
- iframeView.setTopOffset(0);
103
- positionTrackerView.setTopOffset((iframeView.height - positionTrackerView.height) * newScrollProgress);
104
- }
105
- else {
106
- const totalOffset = iframeView.height - this.height;
107
- iframeView.setTopOffset(-totalOffset * newScrollProgress);
108
- positionTrackerView.setTopOffset((this.height - positionTrackerView.height) * newScrollProgress);
109
- }
110
- positionTrackerView.setScrollProgress(Math.round(newScrollProgress * 100));
111
- }
112
- /**
113
- * Sets the new height of the tracker (in px) to visualize the subset of the content visible to the user.
114
- */
115
- setPositionTrackerHeight(trackerHeight) {
116
- this._positionTrackerView.setHeight(trackerHeight * this._scaleRatio);
117
- }
118
- /**
119
- * @param data DOM event data
120
- */
121
- _handleMinimapClick(data) {
122
- const positionTrackerView = this._positionTrackerView;
123
- if (data.target === positionTrackerView.element) {
124
- return;
125
- }
126
- const trackerViewRect = new Rect(positionTrackerView.element);
127
- const diff = data.clientY - trackerViewRect.top - trackerViewRect.height / 2;
128
- const percentage = diff / this._minimapIframeView.height;
129
- this.fire('click', percentage);
130
- }
131
- /**
132
- * @param data DOM event data
133
- */
134
- _handleMinimapMouseWheel(data) {
135
- this.fire('drag', data.deltaY * this._scaleRatio);
136
- }
137
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ * @module minimap/minimapview
7
+ */
8
+ import { View } from 'ckeditor5/src/ui';
9
+ import { Rect } from 'ckeditor5/src/utils';
10
+ import MinimapIframeView from './minimapiframeview';
11
+ import MinimapPositionTrackerView from './minimappositiontrackerview';
12
+ /**
13
+ * The main view of the minimap. It renders the original content but scaled down with a tracker element
14
+ * visualizing the subset of the content visible to the user and allowing interactions (scrolling, dragging).
15
+ *
16
+ * @internal
17
+ */
18
+ export default class MinimapView extends View {
19
+ /**
20
+ * Creates an instance of the minimap view.
21
+ */
22
+ constructor({ locale, scaleRatio, pageStyles, extraClasses, useSimplePreview, domRootClone }) {
23
+ super(locale);
24
+ const bind = this.bindTemplate;
25
+ this._positionTrackerView = new MinimapPositionTrackerView(locale);
26
+ this._positionTrackerView.delegate('drag').to(this);
27
+ this._scaleRatio = scaleRatio;
28
+ this._minimapIframeView = new MinimapIframeView(locale, {
29
+ useSimplePreview,
30
+ pageStyles,
31
+ extraClasses,
32
+ scaleRatio,
33
+ domRootClone
34
+ });
35
+ this.setTemplate({
36
+ tag: 'div',
37
+ attributes: {
38
+ class: [
39
+ 'ck',
40
+ 'ck-minimap'
41
+ ]
42
+ },
43
+ children: [
44
+ this._positionTrackerView
45
+ ],
46
+ on: {
47
+ click: bind.to(this._handleMinimapClick.bind(this)),
48
+ wheel: bind.to(this._handleMinimapMouseWheel.bind(this))
49
+ }
50
+ });
51
+ }
52
+ /**
53
+ * @inheritDoc
54
+ */
55
+ destroy() {
56
+ this._minimapIframeView.destroy();
57
+ super.destroy();
58
+ }
59
+ /**
60
+ * Returns the DOM {@link module:utils/dom/rect~Rect} height of the minimap.
61
+ */
62
+ get height() {
63
+ return new Rect(this.element).height;
64
+ }
65
+ /**
66
+ * Returns the number of available space (pixels) the position tracker (visible subset of the content) can use to scroll vertically.
67
+ */
68
+ get scrollHeight() {
69
+ return Math.max(0, Math.min(this.height, this._minimapIframeView.height) - this._positionTrackerView.height);
70
+ }
71
+ /**
72
+ * @inheritDoc
73
+ */
74
+ render() {
75
+ super.render();
76
+ this._minimapIframeView.render();
77
+ this.element.appendChild(this._minimapIframeView.element);
78
+ }
79
+ /**
80
+ * Sets the new height of the minimap (in px) to respond to the changes in the original editing DOM root.
81
+ *
82
+ * **Note**:The provided value should be the `offsetHeight` of the original editing DOM root.
83
+ */
84
+ setContentHeight(newHeight) {
85
+ this._minimapIframeView.setHeight(newHeight * this._scaleRatio);
86
+ }
87
+ /**
88
+ * Sets the minimap scroll progress.
89
+ *
90
+ * The minimap scroll progress is linked to the original editing DOM root and its scrollable container (ancestor).
91
+ * Changing the progress will alter the vertical position of the minimap (and its position tracker) and give the user an accurate
92
+ * overview of the visible document.
93
+ *
94
+ * **Note**: The value should be between 0 and 1. 0 when the DOM root has not been scrolled, 1 when the
95
+ * scrolling has reached the end.
96
+ */
97
+ setScrollProgress(newScrollProgress) {
98
+ const iframeView = this._minimapIframeView;
99
+ const positionTrackerView = this._positionTrackerView;
100
+ // The scrolling should end when the bottom edge of the iframe touches the bottom edge of the minimap.
101
+ if (iframeView.height < this.height) {
102
+ iframeView.setTopOffset(0);
103
+ positionTrackerView.setTopOffset((iframeView.height - positionTrackerView.height) * newScrollProgress);
104
+ }
105
+ else {
106
+ const totalOffset = iframeView.height - this.height;
107
+ iframeView.setTopOffset(-totalOffset * newScrollProgress);
108
+ positionTrackerView.setTopOffset((this.height - positionTrackerView.height) * newScrollProgress);
109
+ }
110
+ positionTrackerView.setScrollProgress(Math.round(newScrollProgress * 100));
111
+ }
112
+ /**
113
+ * Sets the new height of the tracker (in px) to visualize the subset of the content visible to the user.
114
+ */
115
+ setPositionTrackerHeight(trackerHeight) {
116
+ this._positionTrackerView.setHeight(trackerHeight * this._scaleRatio);
117
+ }
118
+ /**
119
+ * @param data DOM event data
120
+ */
121
+ _handleMinimapClick(data) {
122
+ const positionTrackerView = this._positionTrackerView;
123
+ if (data.target === positionTrackerView.element) {
124
+ return;
125
+ }
126
+ const trackerViewRect = new Rect(positionTrackerView.element);
127
+ const diff = data.clientY - trackerViewRect.top - trackerViewRect.height / 2;
128
+ const percentage = diff / this._minimapIframeView.height;
129
+ this.fire('click', percentage);
130
+ }
131
+ /**
132
+ * @param data DOM event data
133
+ */
134
+ _handleMinimapMouseWheel(data) {
135
+ this.fire('drag', data.deltaY * this._scaleRatio);
136
+ }
137
+ }
package/src/utils.d.ts CHANGED
@@ -1,61 +1,61 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- * @module minimap/utils
7
- */
8
- import { Rect } from 'ckeditor5/src/utils';
9
- import type { Editor } from 'ckeditor5/src/core';
10
- /**
11
- * Clones the editing view DOM root by using a dedicated pair of {@link module:engine/view/renderer~Renderer} and
12
- * {@link module:engine/view/domconverter~DomConverter}. The DOM root clone updates incrementally to stay in sync with the
13
- * source root.
14
- *
15
- * @internal
16
- * @param editor The editor instance the original editing root belongs to.
17
- * @param rootName The name of the root to clone.
18
- * @returns The editing root DOM clone element.
19
- */
20
- export declare function cloneEditingViewDomRoot(editor: Editor, rootName?: string): HTMLElement;
21
- /**
22
- * Harvests all web page styles, for instance, to allow re-using them in an `<iframe>` preserving the look of the content.
23
- *
24
- * The returned data format is as follows:
25
- *
26
- * ```ts
27
- * [
28
- * 'p { color: red; ... } h2 { font-size: 2em; ... } ...',
29
- * '.spacing { padding: 1em; ... }; ...',
30
- * '...',
31
- * { href: 'http://link.to.external.stylesheet' },
32
- * { href: '...' }
33
- * ]
34
- * ```
35
- *
36
- * **Note**: For stylesheets with `href` different than window origin, an object is returned because
37
- * accessing rules of these styles may cause CORS errors (depending on the configuration of the web page).
38
- *
39
- * @internal
40
- */
41
- export declare function getPageStyles(): Array<string | {
42
- href: string;
43
- }>;
44
- /**
45
- * Gets dimensions rectangle according to passed DOM element. Returns whole window's size for `body` element.
46
- *
47
- * @internal
48
- */
49
- export declare function getDomElementRect(domElement: HTMLElement): Rect;
50
- /**
51
- * Gets client height according to passed DOM element. Returns window's height for `body` element.
52
- *
53
- * @internal
54
- */
55
- export declare function getClientHeight(domElement: HTMLElement): number;
56
- /**
57
- * Returns the DOM element itself if it's not a `body` element, whole window otherwise.
58
- *
59
- * @internal
60
- */
61
- export declare function getScrollable(domElement: HTMLElement): Window | HTMLElement;
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ * @module minimap/utils
7
+ */
8
+ import { Rect } from 'ckeditor5/src/utils';
9
+ import type { Editor } from 'ckeditor5/src/core';
10
+ /**
11
+ * Clones the editing view DOM root by using a dedicated pair of {@link module:engine/view/renderer~Renderer} and
12
+ * {@link module:engine/view/domconverter~DomConverter}. The DOM root clone updates incrementally to stay in sync with the
13
+ * source root.
14
+ *
15
+ * @internal
16
+ * @param editor The editor instance the original editing root belongs to.
17
+ * @param rootName The name of the root to clone.
18
+ * @returns The editing root DOM clone element.
19
+ */
20
+ export declare function cloneEditingViewDomRoot(editor: Editor, rootName?: string): HTMLElement;
21
+ /**
22
+ * Harvests all web page styles, for instance, to allow re-using them in an `<iframe>` preserving the look of the content.
23
+ *
24
+ * The returned data format is as follows:
25
+ *
26
+ * ```ts
27
+ * [
28
+ * 'p { color: red; ... } h2 { font-size: 2em; ... } ...',
29
+ * '.spacing { padding: 1em; ... }; ...',
30
+ * '...',
31
+ * { href: 'http://link.to.external.stylesheet' },
32
+ * { href: '...' }
33
+ * ]
34
+ * ```
35
+ *
36
+ * **Note**: For stylesheets with `href` different than window origin, an object is returned because
37
+ * accessing rules of these styles may cause CORS errors (depending on the configuration of the web page).
38
+ *
39
+ * @internal
40
+ */
41
+ export declare function getPageStyles(): Array<string | {
42
+ href: string;
43
+ }>;
44
+ /**
45
+ * Gets dimensions rectangle according to passed DOM element. Returns whole window's size for `body` element.
46
+ *
47
+ * @internal
48
+ */
49
+ export declare function getDomElementRect(domElement: HTMLElement): Rect;
50
+ /**
51
+ * Gets client height according to passed DOM element. Returns window's height for `body` element.
52
+ *
53
+ * @internal
54
+ */
55
+ export declare function getClientHeight(domElement: HTMLElement): number;
56
+ /**
57
+ * Returns the DOM element itself if it's not a `body` element, whole window otherwise.
58
+ *
59
+ * @internal
60
+ */
61
+ export declare function getScrollable(domElement: HTMLElement): Window | HTMLElement;
package/src/utils.js CHANGED
@@ -1,97 +1,97 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- /* global CSSMediaRule */
6
- /**
7
- * @module minimap/utils
8
- */
9
- import { Rect, global } from 'ckeditor5/src/utils';
10
- import { DomConverter, Renderer } from 'ckeditor5/src/engine';
11
- /**
12
- * Clones the editing view DOM root by using a dedicated pair of {@link module:engine/view/renderer~Renderer} and
13
- * {@link module:engine/view/domconverter~DomConverter}. The DOM root clone updates incrementally to stay in sync with the
14
- * source root.
15
- *
16
- * @internal
17
- * @param editor The editor instance the original editing root belongs to.
18
- * @param rootName The name of the root to clone.
19
- * @returns The editing root DOM clone element.
20
- */
21
- export function cloneEditingViewDomRoot(editor, rootName) {
22
- const viewDocument = editor.editing.view.document;
23
- const viewRoot = viewDocument.getRoot(rootName);
24
- const domConverter = new DomConverter(viewDocument);
25
- const renderer = new Renderer(domConverter, viewDocument.selection);
26
- const domRootClone = editor.editing.view.getDomRoot().cloneNode();
27
- domConverter.bindElements(domRootClone, viewRoot);
28
- renderer.markToSync('children', viewRoot);
29
- renderer.markToSync('attributes', viewRoot);
30
- viewRoot.on('change:children', (evt, node) => renderer.markToSync('children', node));
31
- viewRoot.on('change:attributes', (evt, node) => renderer.markToSync('attributes', node));
32
- viewRoot.on('change:text', (evt, node) => renderer.markToSync('text', node));
33
- renderer.render();
34
- editor.editing.view.on('render', () => renderer.render());
35
- // TODO: Cleanup after destruction.
36
- editor.on('destroy', () => {
37
- domConverter.unbindDomElement(domRootClone);
38
- });
39
- return domRootClone;
40
- }
41
- /**
42
- * Harvests all web page styles, for instance, to allow re-using them in an `<iframe>` preserving the look of the content.
43
- *
44
- * The returned data format is as follows:
45
- *
46
- * ```ts
47
- * [
48
- * 'p { color: red; ... } h2 { font-size: 2em; ... } ...',
49
- * '.spacing { padding: 1em; ... }; ...',
50
- * '...',
51
- * { href: 'http://link.to.external.stylesheet' },
52
- * { href: '...' }
53
- * ]
54
- * ```
55
- *
56
- * **Note**: For stylesheets with `href` different than window origin, an object is returned because
57
- * accessing rules of these styles may cause CORS errors (depending on the configuration of the web page).
58
- *
59
- * @internal
60
- */
61
- export function getPageStyles() {
62
- return Array.from(global.document.styleSheets)
63
- .map(styleSheet => {
64
- // CORS
65
- if (styleSheet.href && !styleSheet.href.startsWith(global.window.location.origin)) {
66
- return { href: styleSheet.href };
67
- }
68
- return Array.from(styleSheet.cssRules)
69
- .filter(rule => !(rule instanceof CSSMediaRule))
70
- .map(rule => rule.cssText)
71
- .join(' \n');
72
- });
73
- }
74
- /**
75
- * Gets dimensions rectangle according to passed DOM element. Returns whole window's size for `body` element.
76
- *
77
- * @internal
78
- */
79
- export function getDomElementRect(domElement) {
80
- return new Rect(domElement === global.document.body ? global.window : domElement);
81
- }
82
- /**
83
- * Gets client height according to passed DOM element. Returns window's height for `body` element.
84
- *
85
- * @internal
86
- */
87
- export function getClientHeight(domElement) {
88
- return domElement === global.document.body ? global.window.innerHeight : domElement.clientHeight;
89
- }
90
- /**
91
- * Returns the DOM element itself if it's not a `body` element, whole window otherwise.
92
- *
93
- * @internal
94
- */
95
- export function getScrollable(domElement) {
96
- return domElement === global.document.body ? global.window : domElement;
97
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ /* global CSSMediaRule */
6
+ /**
7
+ * @module minimap/utils
8
+ */
9
+ import { Rect, global } from 'ckeditor5/src/utils';
10
+ import { DomConverter, Renderer } from 'ckeditor5/src/engine';
11
+ /**
12
+ * Clones the editing view DOM root by using a dedicated pair of {@link module:engine/view/renderer~Renderer} and
13
+ * {@link module:engine/view/domconverter~DomConverter}. The DOM root clone updates incrementally to stay in sync with the
14
+ * source root.
15
+ *
16
+ * @internal
17
+ * @param editor The editor instance the original editing root belongs to.
18
+ * @param rootName The name of the root to clone.
19
+ * @returns The editing root DOM clone element.
20
+ */
21
+ export function cloneEditingViewDomRoot(editor, rootName) {
22
+ const viewDocument = editor.editing.view.document;
23
+ const viewRoot = viewDocument.getRoot(rootName);
24
+ const domConverter = new DomConverter(viewDocument);
25
+ const renderer = new Renderer(domConverter, viewDocument.selection);
26
+ const domRootClone = editor.editing.view.getDomRoot().cloneNode();
27
+ domConverter.bindElements(domRootClone, viewRoot);
28
+ renderer.markToSync('children', viewRoot);
29
+ renderer.markToSync('attributes', viewRoot);
30
+ viewRoot.on('change:children', (evt, node) => renderer.markToSync('children', node));
31
+ viewRoot.on('change:attributes', (evt, node) => renderer.markToSync('attributes', node));
32
+ viewRoot.on('change:text', (evt, node) => renderer.markToSync('text', node));
33
+ renderer.render();
34
+ editor.editing.view.on('render', () => renderer.render());
35
+ // TODO: Cleanup after destruction.
36
+ editor.on('destroy', () => {
37
+ domConverter.unbindDomElement(domRootClone);
38
+ });
39
+ return domRootClone;
40
+ }
41
+ /**
42
+ * Harvests all web page styles, for instance, to allow re-using them in an `<iframe>` preserving the look of the content.
43
+ *
44
+ * The returned data format is as follows:
45
+ *
46
+ * ```ts
47
+ * [
48
+ * 'p { color: red; ... } h2 { font-size: 2em; ... } ...',
49
+ * '.spacing { padding: 1em; ... }; ...',
50
+ * '...',
51
+ * { href: 'http://link.to.external.stylesheet' },
52
+ * { href: '...' }
53
+ * ]
54
+ * ```
55
+ *
56
+ * **Note**: For stylesheets with `href` different than window origin, an object is returned because
57
+ * accessing rules of these styles may cause CORS errors (depending on the configuration of the web page).
58
+ *
59
+ * @internal
60
+ */
61
+ export function getPageStyles() {
62
+ return Array.from(global.document.styleSheets)
63
+ .map(styleSheet => {
64
+ // CORS
65
+ if (styleSheet.href && !styleSheet.href.startsWith(global.window.location.origin)) {
66
+ return { href: styleSheet.href };
67
+ }
68
+ return Array.from(styleSheet.cssRules)
69
+ .filter(rule => !(rule instanceof CSSMediaRule))
70
+ .map(rule => rule.cssText)
71
+ .join(' \n');
72
+ });
73
+ }
74
+ /**
75
+ * Gets dimensions rectangle according to passed DOM element. Returns whole window's size for `body` element.
76
+ *
77
+ * @internal
78
+ */
79
+ export function getDomElementRect(domElement) {
80
+ return new Rect(domElement === global.document.body ? global.window : domElement);
81
+ }
82
+ /**
83
+ * Gets client height according to passed DOM element. Returns window's height for `body` element.
84
+ *
85
+ * @internal
86
+ */
87
+ export function getClientHeight(domElement) {
88
+ return domElement === global.document.body ? global.window.innerHeight : domElement.clientHeight;
89
+ }
90
+ /**
91
+ * Returns the DOM element itself if it's not a `body` element, whole window otherwise.
92
+ *
93
+ * @internal
94
+ */
95
+ export function getScrollable(domElement) {
96
+ return domElement === global.document.body ? global.window : domElement;
97
+ }