UNPKG

4.51 kBPlain TextView Raw
1// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// @ts-ignore Importing CSS is handled in Rollup.
6import commonStyle from './common.css'; // eslint-disable-line rulesdir/es_modules_import
7import {adoptStyleSheet} from './common.js';
8import {gridStyle} from './highlight_grid_common.js';
9import {DistancesOverlay} from './tool_distances.js';
10// @ts-ignore Importing CSS is handled in Rollup.
11import highlightGridStyle from './tool_grid.css'; // eslint-disable-line rulesdir/es_modules_import
12// @ts-ignore Importing CSS is handled in Rollup.
13import highlightStyle from './tool_highlight.css'; // eslint-disable-line rulesdir/es_modules_import
14import {HighlightOverlay} from './tool_highlight.js';
15// @ts-ignore Importing CSS is handled in Rollup.
16import pausedStyle from './tool_paused.css'; // eslint-disable-line rulesdir/es_modules_import
17import type {PausedToolMessage} from './tool_paused.js';
18import {PausedOverlay} from './tool_paused.js';
19import type {PersistentToolMessage} from './tool_persistent.js';
20import {PersistentOverlay} from './tool_persistent.js';
21// @ts-ignore Importing CSS is handled in Rollup.
22import screenshotStyle from './tool_screenshot.css'; // eslint-disable-line rulesdir/es_modules_import
23import {ScreenshotOverlay} from './tool_screenshot.js';
24import type {ScreenshotToolMessage} from './tool_screenshot.js';
25// @ts-ignore Importing CSS is handled in Rollup.
26import sourceOrderStyle from './tool_source_order.css'; // eslint-disable-line rulesdir/es_modules_import
27import {SourceOrderOverlay} from './tool_source_order.js';
28import {ViewportSizeOverlay} from './tool_viewport_size.js';
29
30declare global {
31 // eslint-disable-next-line @typescript-eslint/no-unused-vars
32 interface Window {
33 // eslint-disable-next-line @typescript-eslint/naming-convention
34 InspectorOverlayHost: {send(data: PausedToolMessage|PersistentToolMessage|ScreenshotToolMessage|string): void};
35 }
36}
37
38adoptStyleSheet(commonStyle);
39
40const gridStyleSheet = new CSSStyleSheet();
41gridStyleSheet.replaceSync(gridStyle);
42
43const highlightOverlay = new HighlightOverlay(window, [highlightStyle, gridStyleSheet]);
44const persistentOverlay = new PersistentOverlay(window, [highlightGridStyle, gridStyleSheet]);
45const distancesOverlay = new DistancesOverlay(window);
46const pausedOverlay = new PausedOverlay(window, pausedStyle);
47const screenshotOverlay = new ScreenshotOverlay(window, screenshotStyle);
48const sourceOrderOverlay = new SourceOrderOverlay(window, sourceOrderStyle);
49const viewportSizeOverlay = new ViewportSizeOverlay(window);
50
51interface Overlays {
52 distances: DistancesOverlay;
53 highlight: HighlightOverlay;
54 persistent: PersistentOverlay;
55 paused: PausedOverlay;
56 screenshot: ScreenshotOverlay;
57 sourceOrder: SourceOrderOverlay;
58 viewportSize: ViewportSizeOverlay;
59}
60
61type PlatformName = string;
62
63// Key in this object is the name the backend refers to a particular overlay by.
64const 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
74let currentOverlay: Overlays[keyof Overlays];
75let platformName: PlatformName;
76
77type MessageLookup = {
78 'setOverlay': keyof Overlays,
79 'setPlatform': PlatformName,
80 drawingFinished: '',
81};
82
83const 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 // TODO: setPlatform invokes install() for compatibility with the backend.
93 // The call to install() can be removed from setPlatform() after the backend is updated.
94 if (!currentOverlay.installed) {
95 currentOverlay.install();
96 }
97 } else if (functionName === 'setPlatform') {
98 platformName = message[1];
99 } else if (functionName === 'drawingFinished') {
100 // TODO The logic needs to be added here once the backend starts sending this event.
101 } else {
102 currentOverlay.dispatch(message);
103 }
104};
105
106// Window has an additional dispatch function added, so retype as unknown first
107(window as unknown as {dispatch: Function}).dispatch = dispatch;