-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
minimapiframeview.ts
168 lines (141 loc) · 3.67 KB
/
minimapiframeview.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
/**
* @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 minimap/minimapiframeview
*/
import { IframeView } from 'ckeditor5/src/ui.js';
import { toUnit, type Locale } from 'ckeditor5/src/utils.js';
import type { MinimapViewOptions } from './minimapview.js';
const toPx = toUnit( 'px' );
/**
* The internal `<iframe>` view that hosts the minimap content.
*
* @internal
*/
export default class MinimapIframeView extends IframeView {
/**
* The CSS `top` used to scroll the minimap.
*
* @readonly
*/
declare public top: number;
/**
* The CSS `height` of the iframe.
*
* @readonly
*/
declare public height: number;
/**
* Cached view constructor options for re-use in other methods.
*/
private readonly _options: MinimapViewOptions;
/**
* Creates an instance of the internal minimap iframe.
*/
constructor( locale: Locale, options: MinimapViewOptions ) {
super( locale );
const bind = this.bindTemplate;
this.set( 'top', 0 );
this.set( 'height', 0 );
this._options = options;
this.extendTemplate( {
attributes: {
tabindex: -1,
'aria-hidden': 'true',
class: [
'ck-minimap__iframe'
],
style: {
top: bind.to( 'top', top => toPx( top ) ),
height: bind.to( 'height', height => toPx( height ) )
}
}
} );
}
/**
* @inheritDoc
*/
public override render(): Promise<unknown> {
return super.render().then( () => {
this._prepareDocument();
} );
}
/**
* Sets the new height of the iframe.
*/
public setHeight( newHeight: number ): void {
this.height = newHeight;
}
/**
* Sets the top offset of the iframe to move it around vertically.
*/
public setTopOffset( newOffset: number ): void {
this.top = newOffset;
}
/**
* Sets the internal structure of the `<iframe>` readying it to display the
* minimap element.
*/
private _prepareDocument(): void {
const iframeDocument = this.element!.contentWindow!.document;
const domRootClone = iframeDocument.adoptNode( this._options.domRootClone );
const boxStyles = this._options.useSimplePreview ? `
.ck.ck-editor__editable_inline img {
filter: contrast( 0 );
}
p, li, a, figcaption, span {
background: hsl(0, 0%, 80%) !important;
color: hsl(0, 0%, 80%) !important;
}
h1, h2, h3, h4 {
background: hsl(0, 0%, 60%) !important;
color: hsl(0, 0%, 60%) !important;
}
` : '';
const pageStyles = this._options.pageStyles.map( definition => {
if ( typeof definition === 'string' ) {
return `<style>${ definition }</style>`;
} else {
return `<link rel="stylesheet" type="text/css" href="${ definition.href }">`;
}
} ).join( '\n' );
const html = `<!DOCTYPE html><html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
${ pageStyles }
<style>
html, body {
margin: 0 !important;
padding: 0 !important;
}
html {
overflow: hidden;
}
body {
transform: scale( ${ this._options.scaleRatio } );
transform-origin: 0 0;
overflow: visible;
}
.ck.ck-editor__editable_inline {
margin: 0 !important;
border-color: transparent !important;
outline-color: transparent !important;
box-shadow: none !important;
}
.ck.ck-content {
background: white;
}
${ boxStyles }
</style>
</head>
<body class="${ this._options.extraClasses || '' }"></body>
</html>`;
iframeDocument.open();
iframeDocument.write( html );
iframeDocument.close();
iframeDocument.body.appendChild( domRootClone );
}
}