1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import commonStyle from './common.css';
|
7 | import {adoptStyleSheet} from './common.js';
|
8 | import {gridStyle} from './highlight_grid_common.js';
|
9 | import {DistancesOverlay} from './tool_distances.js';
|
10 |
|
11 | import highlightGridStyle from './tool_grid.css';
|
12 |
|
13 | import highlightStyle from './tool_highlight.css';
|
14 | import {HighlightOverlay} from './tool_highlight.js';
|
15 |
|
16 | import pausedStyle from './tool_paused.css';
|
17 | import type {PausedToolMessage} from './tool_paused.js';
|
18 | import {PausedOverlay} from './tool_paused.js';
|
19 | import type {PersistentToolMessage} from './tool_persistent.js';
|
20 | import {PersistentOverlay} from './tool_persistent.js';
|
21 |
|
22 | import screenshotStyle from './tool_screenshot.css';
|
23 | import {ScreenshotOverlay} from './tool_screenshot.js';
|
24 | import type {ScreenshotToolMessage} from './tool_screenshot.js';
|
25 |
|
26 | import sourceOrderStyle from './tool_source_order.css';
|
27 | import {SourceOrderOverlay} from './tool_source_order.js';
|
28 | import {ViewportSizeOverlay} from './tool_viewport_size.js';
|
29 |
|
30 | declare global {
|
31 |
|
32 | interface Window {
|
33 |
|
34 | InspectorOverlayHost: {send(data: PausedToolMessage|PersistentToolMessage|ScreenshotToolMessage|string): void};
|
35 | }
|
36 | }
|
37 |
|
38 | adoptStyleSheet(commonStyle);
|
39 |
|
40 | const gridStyleSheet = new CSSStyleSheet();
|
41 | gridStyleSheet.replaceSync(gridStyle);
|
42 |
|
43 | const highlightOverlay = new HighlightOverlay(window, [highlightStyle, gridStyleSheet]);
|
44 | const persistentOverlay = new PersistentOverlay(window, [highlightGridStyle, gridStyleSheet]);
|
45 | const distancesOverlay = new DistancesOverlay(window);
|
46 | const pausedOverlay = new PausedOverlay(window, pausedStyle);
|
47 | const screenshotOverlay = new ScreenshotOverlay(window, screenshotStyle);
|
48 | const sourceOrderOverlay = new SourceOrderOverlay(window, sourceOrderStyle);
|
49 | const viewportSizeOverlay = new ViewportSizeOverlay(window);
|
50 |
|
51 | interface Overlays {
|
52 | distances: DistancesOverlay;
|
53 | highlight: HighlightOverlay;
|
54 | persistent: PersistentOverlay;
|
55 | paused: PausedOverlay;
|
56 | screenshot: ScreenshotOverlay;
|
57 | sourceOrder: SourceOrderOverlay;
|
58 | viewportSize: ViewportSizeOverlay;
|
59 | }
|
60 |
|
61 | type PlatformName = string;
|
62 |
|
63 |
|
64 | const overlays: Overlays = {
|
65 | distances: distancesOverlay,
|
66 | highlight: highlightOverlay,
|
67 | persistent: persistentOverlay,
|
68 | paused: pausedOverlay,
|
69 | screenshot: screenshotOverlay,
|
70 | sourceOrder: sourceOrderOverlay,
|
71 | viewportSize: viewportSizeOverlay,
|
72 | };
|
73 |
|
74 | let currentOverlay: Overlays[keyof Overlays];
|
75 | let platformName: PlatformName;
|
76 |
|
77 | type MessageLookup = {
|
78 | 'setOverlay': keyof Overlays,
|
79 | 'setPlatform': PlatformName,
|
80 | drawingFinished: '',
|
81 | };
|
82 |
|
83 | const dispatch = <K extends keyof MessageLookup>(message: [a: K, b: MessageLookup[K]]) => {
|
84 | const functionName = message[0];
|
85 | if (functionName === 'setOverlay') {
|
86 | const overlayName = message[1] as keyof Overlays;
|
87 | if (currentOverlay) {
|
88 | currentOverlay.uninstall();
|
89 | }
|
90 | currentOverlay = overlays[overlayName];
|
91 | currentOverlay.setPlatform(platformName);
|
92 |
|
93 |
|
94 | if (!currentOverlay.installed) {
|
95 | currentOverlay.install();
|
96 | }
|
97 | } else if (functionName === 'setPlatform') {
|
98 | platformName = message[1];
|
99 | } else if (functionName === 'drawingFinished') {
|
100 |
|
101 | } else {
|
102 | currentOverlay.dispatch(message);
|
103 | }
|
104 | };
|
105 |
|
106 |
|
107 | (window as unknown as {dispatch: Function}).dispatch = dispatch;
|