-
Notifications
You must be signed in to change notification settings - Fork 493
/
Copy pathcommon.ts
234 lines (202 loc) · 6.17 KB
/
common.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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
export type PathCommands = Array<string|number>;
export interface Quad {
p1: Position;
p2: Position;
p3: Position;
p4: Position;
}
export interface Position {
x: number;
y: number;
}
export interface Bounds {
minX: number;
maxX: number;
minY: number;
maxY: number;
width?: number;
height?: number;
allPoints: Position[];
}
export interface AreaBounds {
name: string;
bounds: {allPoints: Position[]};
}
interface ViewportSize {
width: number;
height: number;
}
export interface ResetData {
viewportSize: ViewportSize;
viewportSizeForMediaQueries?: ViewportSize;
deviceScaleFactor: number;
pageScaleFactor: number;
pageZoomFactor: number;
emulationScaleFactor: number;
scrollX: number;
scrollY: number;
}
// Overlay class should be used to implement various tools and provide
// access to globals via the window object it receives in the constructor.
// Old logic is kept temporarily while the tools are being migrated.
export class Overlay {
protected viewportSize = {width: 800, height: 600};
protected viewportSizeForMediaQueries?: ViewportSize;
protected deviceScaleFactor = 1;
protected emulationScaleFactor = 1;
protected pageScaleFactor = 1;
protected pageZoomFactor = 1;
protected scrollX = 0;
protected scrollY = 0;
protected style: CSSStyleSheet[];
protected canvas?: HTMLCanvasElement;
protected canvasWidth = 0;
protected canvasHeight = 0;
protected platform?: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
private _window?: Window;
// eslint-disable-next-line @typescript-eslint/naming-convention
private _document?: Document;
// eslint-disable-next-line @typescript-eslint/naming-convention
private _context?: CanvasRenderingContext2D|null;
// eslint-disable-next-line @typescript-eslint/naming-convention
private _installed = false;
constructor(window: Window, style: CSSStyleSheet[] = []) {
this._window = window;
this._document = window.document;
if (!Array.isArray(style)) {
style = [style];
}
this.style = style;
}
setCanvas(canvas: HTMLCanvasElement) {
this.canvas = canvas;
this._context = canvas.getContext('2d');
}
install() {
for (const style of this.style) {
adoptStyleSheet(style);
}
this._installed = true;
}
uninstall() {
for (const style of this.style) {
document.adoptedStyleSheets = document.adoptedStyleSheets.filter(s => s !== style);
}
this._installed = false;
}
reset(resetData?: ResetData) {
if (resetData) {
this.viewportSize = resetData.viewportSize;
this.viewportSizeForMediaQueries = resetData.viewportSizeForMediaQueries;
this.deviceScaleFactor = resetData.deviceScaleFactor;
this.pageScaleFactor = resetData.pageScaleFactor;
this.pageZoomFactor = resetData.pageZoomFactor;
this.emulationScaleFactor = resetData.emulationScaleFactor;
this.scrollX = Math.round(resetData.scrollX);
this.scrollY = Math.round(resetData.scrollY);
}
this.resetCanvas();
}
resetCanvas() {
if (!this.canvas || !this._context) {
return;
}
this.canvas.width = this.deviceScaleFactor * this.viewportSize.width;
this.canvas.height = this.deviceScaleFactor * this.viewportSize.height;
this.canvas.style.width = this.viewportSize.width + 'px';
this.canvas.style.height = this.viewportSize.height + 'px';
this._context.scale(this.deviceScaleFactor, this.deviceScaleFactor);
this.canvasWidth = this.viewportSize.width;
this.canvasHeight = this.viewportSize.height;
}
setPlatform(platform: string) {
this.platform = platform;
this.document.body.classList.add('platform-' + platform);
if (!this._installed) {
this.install();
}
}
dispatch(message: unknown[]) {
const functionName = message.shift() as string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(this as any)[functionName].apply(this, message);
}
eventHasCtrlOrMeta(event: KeyboardEvent) {
return this.platform === 'mac' ? (event.metaKey && !event.ctrlKey) : (event.ctrlKey && !event.metaKey);
}
get context(): CanvasRenderingContext2D {
if (!this._context) {
throw new Error('Context object is missing');
}
return this._context;
}
get document(): Document {
if (!this._document) {
throw new Error('Document object is missing');
}
return this._document;
}
get window(): Window {
if (!this._window) {
throw new Error('Window object is missing');
}
return this._window;
}
get installed(): boolean {
return this._installed;
}
}
export function log(text: string) {
let element = document.getElementById('log');
if (!element) {
element = createChild(document.body, 'div');
element.id = 'log';
}
createChild(element, 'div').textContent = text;
}
export function createChild(parent: HTMLElement, tagName: string, className?: string): HTMLElement {
const element = createElement(tagName, className);
element.addEventListener('click', function(e: Event) {
e.stopPropagation();
}, false);
parent.appendChild(element);
return element;
}
export function createTextChild(parent: HTMLElement, text: string): Text {
const element = document.createTextNode(text);
parent.appendChild(element);
return element;
}
export function createElement(tagName: string, className?: string) {
const element = document.createElement(tagName);
if (className) {
element.className = className;
}
return element;
}
export function ellipsify(str: string, maxLength: number) {
if (str.length <= maxLength) {
return String(str);
}
return str.substr(0, maxLength - 1) + '\u2026';
}
export function constrainNumber(num: number, min: number, max: number): number {
if (num < min) {
num = min;
} else if (num > max) {
num = max;
}
return num;
}
declare global {
interface Document {
adoptedStyleSheets: CSSStyleSheet[];
}
}
export function adoptStyleSheet(styleSheet: CSSStyleSheet) {
document.adoptedStyleSheets = [...document.adoptedStyleSheets, styleSheet];
}