@runic-artifex/vite-plugin-runic 0.19.0 → 0.20.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.
- package/README.md +532 -12
- package/dist/access.d.ts +46 -0
- package/dist/access.js +123 -0
- package/dist/calls.d.ts +83 -0
- package/dist/calls.js +140 -0
- package/dist/channel.d.ts +58 -1
- package/dist/channel.js +20 -1
- package/dist/client.js +210 -1
- package/dist/dev-session.d.ts +19 -2
- package/dist/dev-session.js +67 -46
- package/dist/diagnostics.d.ts +2 -0
- package/dist/diagnostics.js +2 -1
- package/dist/handlers.d.ts +140 -0
- package/dist/handlers.js +773 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +226 -11
- package/dist/inspect-data.d.ts +268 -0
- package/dist/inspect-data.js +442 -0
- package/dist/inspect-edit.d.ts +164 -0
- package/dist/inspect-edit.js +273 -0
- package/dist/inspect-editor.d.ts +34 -0
- package/dist/inspect-editor.js +282 -0
- package/dist/inspect-frameworks.d.ts +72 -0
- package/dist/inspect-frameworks.js +333 -0
- package/dist/inspect-handlers.d.ts +84 -0
- package/dist/inspect-handlers.js +331 -0
- package/dist/inspect.d.ts +42 -0
- package/dist/inspect.js +783 -0
- package/dist/otlp.d.ts +80 -0
- package/dist/otlp.js +254 -0
- package/dist/requests.d.ts +17 -0
- package/dist/requests.js +67 -0
- package/dist/sources.d.ts +135 -0
- package/dist/sources.js +343 -0
- package/dist/timeline.d.ts +12 -0
- package/dist/timeline.js +14 -0
- package/dist/translations-source.d.ts +36 -0
- package/dist/translations-source.js +109 -0
- package/package.json +12 -6
package/dist/inspect.js
ADDED
|
@@ -0,0 +1,783 @@
|
|
|
1
|
+
// Runic Inspect: the development client's select mode. Alt+Shift+X (or `toggle()`) turns it on; hovering outlines an
|
|
2
|
+
// element, and clicking it shows an overview of how it is connected: its framework component, the ViewModel members
|
|
3
|
+
// it binds with their values and C# members, the commands it calls with their C# handlers, its recent Bridge calls,
|
|
4
|
+
// and the texts its ViewModel sent, each location with an "open in editor" link. While it is on, the page's own
|
|
5
|
+
// handlers see no pointer event: a transparent layer takes them, and the window's capture phase stops them.
|
|
6
|
+
//
|
|
7
|
+
// What an element calls is found before any click in its component's source (handlers.ts and inspect-handlers.ts),
|
|
8
|
+
// and learned from the Bridge-call channel (`runic.views.calls`): its `start` report arrives in the caller's stack, so
|
|
9
|
+
// the DOM event being dispatched (`globalThis.event`) names the element. States come from the state inspector channel
|
|
10
|
+
// (`runic.views.inspector`), C# locations from the development source map, and every editor link opens through
|
|
11
|
+
// `/__runic/open`'s decision: a C# location by its source map id, a Translations message by its reference, a frontend
|
|
12
|
+
// file by path (only files Vite serves). The source map, the handler analysis, message sources and opening go over
|
|
13
|
+
// Vite's HMR connection when the client has it, so they reach the Vite server also from the .NET host's page under
|
|
14
|
+
// `dotnet runic dev`; else to `/__runic/sources`, `/__runic/handlers`, `/__runic-translations/message` and
|
|
15
|
+
// `/__runic/open` relative to the page.
|
|
16
|
+
//
|
|
17
|
+
// Text from generated `m.*()` calls carries Runic Translations' invisible message markers while select mode is on: turning
|
|
18
|
+
// it on calls `markers.set(true)` on the Translations text-change channel (`Symbol.for("runic.translations.text")`,
|
|
19
|
+
// vite-plugin-runic-translations 0.12 under `vite dev`), which renders the page again with a marker in front of every
|
|
20
|
+
// message's text; turning it off renders the plain text again. The overview decodes the markers of the selected
|
|
21
|
+
// element's text and attributes, and reads each message's source location from the Translations dev server's message API
|
|
22
|
+
// (`/__runic-translations/message`). Outside select mode the page's text has no markers.
|
|
23
|
+
//
|
|
24
|
+
// "Edit" on a message opens the translation editor (inspect-editor.ts): every locale's MF2 source, written back through
|
|
25
|
+
// the same API with the file hash it read. The page shows the saved text in place through the text-change channel, and
|
|
26
|
+
// the overview, which listens to that channel, describes the selected element again without losing the selection.
|
|
27
|
+
//
|
|
28
|
+
// From another machine (a page not on localhost), the data endpoints answer when the development server allows that
|
|
29
|
+
// machine (`runic({ allow })`, `RUNIC_DEV_ALLOW`); `/__runic/open` never does, so the overview says so.
|
|
30
|
+
//
|
|
31
|
+
// `globalThis[Symbol.for("runic.vite.inspect")]` is the API for agents and tests: `describe(element)` returns the
|
|
32
|
+
// overview as JSON-safe data. Everything here is part of the development client, which a build replaces with an empty
|
|
33
|
+
// module; it uses only the DOM, so every engine under `dotnet runic dev` runs it.
|
|
34
|
+
import { handlersPath, openPath, sourcesPath } from "./channel.js";
|
|
35
|
+
import { findProbes, matchBindings, probeFiles } from "./inspect-handlers.js";
|
|
36
|
+
import { assembleDescription, groupMessages, messageSource, normalizeText, stripMarkers, } from "./inspect-data.js";
|
|
37
|
+
import { findComponent, findComponentSync } from "./inspect-frameworks.js";
|
|
38
|
+
import { createMessageEditor, editorStyles } from "./inspect-editor.js";
|
|
39
|
+
import { hasEdits } from "./inspect-edit.js";
|
|
40
|
+
export const inspectKey = Symbol.for("runic.vite.inspect");
|
|
41
|
+
const callsKey = Symbol.for("runic.views.calls");
|
|
42
|
+
const inspectorKey = Symbol.for("runic.views.inspector");
|
|
43
|
+
const translatedTextKey = Symbol.for("runic.views.translated-text");
|
|
44
|
+
const translationsTextKey = Symbol.for("runic.translations.text");
|
|
45
|
+
const messagePath = "__runic-translations/message";
|
|
46
|
+
/** The attributes whose text the overview reads message markers from, besides the element's text. */
|
|
47
|
+
const messageAttributes = ["title", "aria-label", "aria-description", "aria-placeholder", "placeholder", "alt", "label"];
|
|
48
|
+
const tagName = "runic-inspect";
|
|
49
|
+
const interactiveSelector = "button, a[href], input, select, textarea, summary, [role=button], [role=link], " +
|
|
50
|
+
"[role=checkbox], [role=switch], [role=menuitem], [role=tab], [role=option], [contenteditable=true]";
|
|
51
|
+
const maximumCalls = 10;
|
|
52
|
+
const swallowed = ["pointerdown", "pointerup", "mousedown", "mouseup", "click", "dblclick", "auxclick", "contextmenu",
|
|
53
|
+
"touchstart", "touchend", "pointermove", "mousemove", "mouseover", "mouseout", "pointerover", "pointerout"];
|
|
54
|
+
const styles = `
|
|
55
|
+
:host { all: initial; }
|
|
56
|
+
* { box-sizing: border-box; }
|
|
57
|
+
.glass { position: fixed; inset: 0; pointer-events: auto; cursor: crosshair; background: transparent; }
|
|
58
|
+
.box { position: fixed; pointer-events: none; border: 2px solid #7c5cff; background: rgb(124 92 255 / 0.12);
|
|
59
|
+
border-radius: 2px; display: none; }
|
|
60
|
+
.tip, .bar { position: fixed; pointer-events: none; font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
61
|
+
color: #f4f2ff; background: #241f3d; border: 1px solid #4b3f8f; border-radius: 4px; padding: 2px 6px; white-space: nowrap; }
|
|
62
|
+
.tip { display: none; max-width: calc(100vw - 16px); overflow: hidden; text-overflow: ellipsis; }
|
|
63
|
+
.bar { left: 50%; top: 8px; transform: translateX(-50%); }
|
|
64
|
+
.card { position: fixed; right: 12px; bottom: 12px; width: min(420px, calc(100vw - 24px)); max-height: min(70vh, 640px);
|
|
65
|
+
overflow: auto; pointer-events: auto; font: 12px/1.45 ui-sans-serif, system-ui, sans-serif; color: #ecebf5;
|
|
66
|
+
background: #17152a; border: 1px solid #4b3f8f; border-radius: 8px; box-shadow: 0 8px 28px rgb(0 0 0 / 0.35); padding: 10px 12px; }
|
|
67
|
+
.card[hidden], .glass[hidden], .bar[hidden] { display: none; }
|
|
68
|
+
.card header { display: flex; gap: 8px; align-items: baseline; justify-content: space-between; margin-bottom: 6px; }
|
|
69
|
+
.card h2 { font: 600 13px/1.3 ui-sans-serif, system-ui, sans-serif; margin: 0; color: #fff; }
|
|
70
|
+
.card h3 { font: 600 11px/1.3 ui-sans-serif, system-ui, sans-serif; margin: 10px 0 4px; color: #b9b0ff; text-transform: uppercase; letter-spacing: 0.04em; }
|
|
71
|
+
.card ul { list-style: none; margin: 0; padding: 0; }
|
|
72
|
+
.card li { margin: 2px 0; overflow-wrap: anywhere; }
|
|
73
|
+
.card code { font: 11.5px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: #ffd98e; }
|
|
74
|
+
.card .muted { color: #a29fbd; }
|
|
75
|
+
.card .note { color: #c8c3e6; font-style: italic; }
|
|
76
|
+
.card a { color: #8fd3ff; text-decoration: underline; cursor: pointer; }
|
|
77
|
+
.card button.close { all: unset; cursor: pointer; color: #c8c3e6; padding: 0 4px; font-size: 14px; }
|
|
78
|
+
.card button.close:focus-visible, .card a:focus-visible { outline: 2px solid #8fd3ff; outline-offset: 1px; }
|
|
79
|
+
.card .overview { display: contents; }
|
|
80
|
+
` + editorStyles;
|
|
81
|
+
function text(value) {
|
|
82
|
+
if (typeof value === "string")
|
|
83
|
+
return JSON.stringify(value);
|
|
84
|
+
try {
|
|
85
|
+
return JSON.stringify(value) ?? String(value);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return String(value);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/** Whether a page's host name is this machine's loopback, as `/__runic/open` requires of its client. */
|
|
92
|
+
export function isLocalHostname(hostname) {
|
|
93
|
+
const name = hostname.toLowerCase().replace(/^\[|\]$/gu, "");
|
|
94
|
+
return name === "localhost" || name.endsWith(".localhost") || name === "::1" || /^127(?:\.\d{1,3}){3}$/u.test(name);
|
|
95
|
+
}
|
|
96
|
+
function shortFile(file, root) {
|
|
97
|
+
if (root !== undefined && file.startsWith(`${root.replace(/[\\/]+$/u, "")}/`))
|
|
98
|
+
return file.slice(root.replace(/[\\/]+$/u, "").length + 1);
|
|
99
|
+
const parts = file.split(/[\\/]/u);
|
|
100
|
+
return parts.length > 3 ? `…/${parts.slice(-3).join("/")}` : file;
|
|
101
|
+
}
|
|
102
|
+
/** Installs Runic Inspect on the page; returns a function that removes it (for HMR). */
|
|
103
|
+
export function installInspect(options = {}) {
|
|
104
|
+
const host = globalThis;
|
|
105
|
+
if (typeof document === "undefined" || typeof Element === "undefined")
|
|
106
|
+
return () => { };
|
|
107
|
+
const { base = "/", root, request } = options;
|
|
108
|
+
// A page of another machine: /__runic/open refuses it, so links show their location without opening it.
|
|
109
|
+
const remote = typeof location === "object" && typeof location.hostname === "string" && location.hostname !== "" && !isLocalHostname(location.hostname);
|
|
110
|
+
// The last refusals of the data endpoints (403), for the notes.
|
|
111
|
+
let handlersForbidden = false;
|
|
112
|
+
const learned = new WeakMap();
|
|
113
|
+
const elementCalls = new WeakMap();
|
|
114
|
+
const pendingCalls = new Map();
|
|
115
|
+
const states = new Map();
|
|
116
|
+
const contracts = new Map();
|
|
117
|
+
const modules = new Map();
|
|
118
|
+
// Learning: a call's start arrives in the stack of the code that made it, so the event being dispatched names the
|
|
119
|
+
// element. The event's target, not its currentTarget: React handles events on its root container.
|
|
120
|
+
function onCall(event) {
|
|
121
|
+
if (typeof event !== "object" || event === null || typeof event.id !== "number")
|
|
122
|
+
return;
|
|
123
|
+
if (event.phase === "start") {
|
|
124
|
+
if (typeof event.route !== "string" || typeof event.member !== "string")
|
|
125
|
+
return;
|
|
126
|
+
if (typeof event.contract === "string")
|
|
127
|
+
contracts.set(event.route, event.contract);
|
|
128
|
+
const dispatched = host.event;
|
|
129
|
+
const origin = dispatched?.composedPath?.()[0] ?? dispatched?.target;
|
|
130
|
+
if (!(origin instanceof Element) || overlay?.host.contains(origin))
|
|
131
|
+
return;
|
|
132
|
+
const element = bindingElement(origin);
|
|
133
|
+
const binding = { route: event.route, contract: String(event.contract ?? ""), kind: String(event.kind), member: event.member };
|
|
134
|
+
let bindings = learned.get(element);
|
|
135
|
+
if (bindings === undefined)
|
|
136
|
+
learned.set(element, bindings = new Map());
|
|
137
|
+
bindings.set(`${binding.route}\u0000${binding.kind}\u0000${binding.member}`, binding);
|
|
138
|
+
const call = {
|
|
139
|
+
id: event.id, kind: String(event.kind), request: String(event.request), route: event.route, member: event.member,
|
|
140
|
+
start: typeof event.start === "number" ? event.start : 0,
|
|
141
|
+
};
|
|
142
|
+
let calls = elementCalls.get(element);
|
|
143
|
+
if (calls === undefined)
|
|
144
|
+
elementCalls.set(element, calls = []);
|
|
145
|
+
calls.unshift(call);
|
|
146
|
+
if (calls.length > maximumCalls)
|
|
147
|
+
calls.length = maximumCalls;
|
|
148
|
+
pendingCalls.set(event.id, call);
|
|
149
|
+
if (pendingCalls.size > 200)
|
|
150
|
+
pendingCalls.delete(pendingCalls.keys().next().value);
|
|
151
|
+
}
|
|
152
|
+
else if (event.phase === "end") {
|
|
153
|
+
const call = pendingCalls.get(event.id);
|
|
154
|
+
if (call === undefined)
|
|
155
|
+
return;
|
|
156
|
+
pendingCalls.delete(event.id);
|
|
157
|
+
if (typeof event.duration === "number")
|
|
158
|
+
call.duration = event.duration;
|
|
159
|
+
if (typeof event.outcome === "string")
|
|
160
|
+
call.outcome = event.outcome;
|
|
161
|
+
if (typeof event.reason === "string")
|
|
162
|
+
call.reason = event.reason;
|
|
163
|
+
if (typeof event.revision === "number")
|
|
164
|
+
call.revision = event.revision;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function onInspector(event) {
|
|
168
|
+
if (typeof event !== "object" || event === null || typeof event.route !== "string")
|
|
169
|
+
return;
|
|
170
|
+
if (event.kind === "state")
|
|
171
|
+
states.set(event.route, event.state);
|
|
172
|
+
else if (event.kind === "closed")
|
|
173
|
+
states.delete(event.route);
|
|
174
|
+
}
|
|
175
|
+
const calls = (host[callsKey] ??= { listeners: new Set() });
|
|
176
|
+
calls.listeners.add(onCall);
|
|
177
|
+
calls.requested = true;
|
|
178
|
+
const inspector = (host[inspectorKey] ??= { listeners: new Set() });
|
|
179
|
+
inspector.listeners.add(onInspector);
|
|
180
|
+
inspector.requested = true;
|
|
181
|
+
function bindingElement(element) {
|
|
182
|
+
return element.closest(interactiveSelector) ?? element;
|
|
183
|
+
}
|
|
184
|
+
function fetchText(url) {
|
|
185
|
+
let pending = modules.get(url);
|
|
186
|
+
if (pending === undefined) {
|
|
187
|
+
pending = fetch(url, { credentials: "same-origin" })
|
|
188
|
+
.then((response) => response.ok ? response.text() : undefined)
|
|
189
|
+
.catch(() => undefined);
|
|
190
|
+
modules.set(url, pending);
|
|
191
|
+
if (modules.size > 100)
|
|
192
|
+
modules.delete(modules.keys().next().value);
|
|
193
|
+
}
|
|
194
|
+
return pending;
|
|
195
|
+
}
|
|
196
|
+
async function readSourceMap() {
|
|
197
|
+
try {
|
|
198
|
+
let status;
|
|
199
|
+
let map;
|
|
200
|
+
if (request !== undefined) {
|
|
201
|
+
const answer = await request("sources");
|
|
202
|
+
status = answer.status;
|
|
203
|
+
map = answer.body;
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
const response = await fetch(sourcesPath, { credentials: "same-origin", cache: "no-store" });
|
|
207
|
+
status = response.status;
|
|
208
|
+
map = response.ok ? await response.json() : undefined;
|
|
209
|
+
}
|
|
210
|
+
if (status === 404)
|
|
211
|
+
return { status: "missing" };
|
|
212
|
+
if (status === 403)
|
|
213
|
+
return { status: "forbidden" };
|
|
214
|
+
if (status !== 200)
|
|
215
|
+
return { status: "unavailable" };
|
|
216
|
+
return Array.isArray(map?.viewModels) && Array.isArray(map.members) ? { map } : { status: "unavailable" };
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
return { status: "unavailable" };
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
async function readAnalysis(file) {
|
|
223
|
+
try {
|
|
224
|
+
let analysis;
|
|
225
|
+
if (request !== undefined) {
|
|
226
|
+
const answer = await request("handlers", file);
|
|
227
|
+
handlersForbidden = answer.status === 403;
|
|
228
|
+
analysis = answer.status === 200 ? answer.body : undefined;
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
const response = await fetch(`${handlersPath}?file=${encodeURIComponent(file)}`, { credentials: "same-origin", cache: "no-store" });
|
|
232
|
+
handlersForbidden = response.status === 403;
|
|
233
|
+
if (!response.ok)
|
|
234
|
+
return undefined;
|
|
235
|
+
analysis = await response.json();
|
|
236
|
+
}
|
|
237
|
+
return analysis?.format === "runic-handlers/1" && Array.isArray(analysis.bindings) ? { ...analysis, requested: file } : undefined;
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
// The element's event bindings in its components' source (`/__runic/handlers`), before any click.
|
|
244
|
+
async function readSourceBindings(element) {
|
|
245
|
+
try {
|
|
246
|
+
const probes = await findProbes(element, { root, ng: host.ng, fetchText });
|
|
247
|
+
if (probes.length === 0)
|
|
248
|
+
return undefined;
|
|
249
|
+
const analyses = (await Promise.all(probeFiles(probes).slice(0, 6).map(readAnalysis)))
|
|
250
|
+
.filter((analysis) => analysis !== undefined);
|
|
251
|
+
return matchBindings(probes, analyses);
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
function resolveText(reference) {
|
|
258
|
+
const channel = host[translatedTextKey];
|
|
259
|
+
const translations = channel?.translations;
|
|
260
|
+
if (typeof translations?.resolve !== "function")
|
|
261
|
+
return undefined;
|
|
262
|
+
const windowLocale = channel?.windowLocale ?? null;
|
|
263
|
+
const locale = windowLocale !== null && translations.setLocale === undefined ? windowLocale : translations.getLocale?.();
|
|
264
|
+
if (typeof locale !== "string")
|
|
265
|
+
return undefined;
|
|
266
|
+
try {
|
|
267
|
+
const resolved = translations.resolve(reference, locale);
|
|
268
|
+
return typeof resolved === "string" ? { text: resolved, locale } : undefined;
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function translationMarkers() {
|
|
275
|
+
const markers = host[translationsTextKey]?.markers;
|
|
276
|
+
return typeof markers?.set === "function" && typeof markers.decode === "function" ? markers : undefined;
|
|
277
|
+
}
|
|
278
|
+
// The messages whose markers are in the element's text or its attributes, with their sources.
|
|
279
|
+
async function readMessages(element) {
|
|
280
|
+
const markers = translationMarkers();
|
|
281
|
+
if (markers === undefined)
|
|
282
|
+
return { messages: [], status: host[translationsTextKey] === undefined ? "none" : "unsupported" };
|
|
283
|
+
const found = [];
|
|
284
|
+
const decode = (text, where) => {
|
|
285
|
+
if (typeof text !== "string" || text.length === 0)
|
|
286
|
+
return;
|
|
287
|
+
try {
|
|
288
|
+
for (const message of markers.decode(text))
|
|
289
|
+
found.push({ message, in: where });
|
|
290
|
+
}
|
|
291
|
+
catch { /* a foreign channel */ }
|
|
292
|
+
};
|
|
293
|
+
decode(element.textContent, "text");
|
|
294
|
+
for (const name of messageAttributes)
|
|
295
|
+
decode(element.getAttribute(name), name);
|
|
296
|
+
if ((element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement) && element.type !== "password")
|
|
297
|
+
decode(element.value, "value");
|
|
298
|
+
const messages = groupMessages(found);
|
|
299
|
+
const status = markers.enabled ? "on" : "off";
|
|
300
|
+
if (messages.length === 0)
|
|
301
|
+
return { messages, status };
|
|
302
|
+
let unavailable = false, forbidden = false;
|
|
303
|
+
const replies = new Map();
|
|
304
|
+
const withSources = await Promise.all(messages.map(async (message) => {
|
|
305
|
+
const query = `key=${encodeURIComponent(message.key)}&catalog=${encodeURIComponent(message.catalog)}`;
|
|
306
|
+
let reply = replies.get(query);
|
|
307
|
+
if (reply === undefined) {
|
|
308
|
+
reply = request !== undefined
|
|
309
|
+
? request("message", { catalog: message.catalog, key: message.key })
|
|
310
|
+
.then((answer) => {
|
|
311
|
+
if (answer.status === 403)
|
|
312
|
+
forbidden = true;
|
|
313
|
+
return answer.status === 200 ? answer.body : undefined;
|
|
314
|
+
}).catch(() => undefined)
|
|
315
|
+
: fetch(`${base.endsWith("/") ? base : `${base}/`}${messagePath}?${query}`, { credentials: "same-origin", cache: "no-store" })
|
|
316
|
+
.then((response) => {
|
|
317
|
+
if (response.status === 403)
|
|
318
|
+
forbidden = true;
|
|
319
|
+
return response.ok ? response.json() : undefined;
|
|
320
|
+
})
|
|
321
|
+
.catch(() => undefined);
|
|
322
|
+
replies.set(query, reply);
|
|
323
|
+
}
|
|
324
|
+
const value = await reply;
|
|
325
|
+
if (value === undefined)
|
|
326
|
+
unavailable = true;
|
|
327
|
+
return { ...message, ...messageSource(value, message) };
|
|
328
|
+
}));
|
|
329
|
+
return { messages: withSources, status, ...(forbidden ? { sources: "forbidden" } : unavailable ? { sources: "unavailable" } : {}) };
|
|
330
|
+
}
|
|
331
|
+
async function describe(element) {
|
|
332
|
+
if (!(element instanceof Element))
|
|
333
|
+
throw new TypeError("describe needs an element.");
|
|
334
|
+
const targets = new Set([bindingElement(element)]);
|
|
335
|
+
if (element instanceof HTMLLabelElement && element.control)
|
|
336
|
+
targets.add(element.control);
|
|
337
|
+
const bindings = [];
|
|
338
|
+
const elementCallList = [];
|
|
339
|
+
for (const target of targets) {
|
|
340
|
+
bindings.push(...learned.get(target)?.values() ?? []);
|
|
341
|
+
elementCallList.push(...(elementCalls.get(target) ?? []).map((call) => ({ ...call })));
|
|
342
|
+
}
|
|
343
|
+
elementCallList.sort((a, b) => b.id - a.id);
|
|
344
|
+
// The text as the page shows it outside select mode: without message markers.
|
|
345
|
+
const ownText = stripMarkers(element.children.length === 0 ? element.textContent ?? ""
|
|
346
|
+
: [...element.childNodes].filter((node) => node.nodeType === 3).map((node) => node.textContent).join(" "));
|
|
347
|
+
const control = element;
|
|
348
|
+
const hasValue = (element instanceof HTMLInputElement && element.type !== "password" && element.type !== "checkbox" && element.type !== "radio")
|
|
349
|
+
|| element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement;
|
|
350
|
+
const [sourceMap, component, source, messages] = await Promise.all([
|
|
351
|
+
readSourceMap(),
|
|
352
|
+
findComponent(element, { root, ng: host.ng, fetchText }),
|
|
353
|
+
readSourceBindings(bindingElement(element)),
|
|
354
|
+
readMessages(element),
|
|
355
|
+
]);
|
|
356
|
+
const role = element.getAttribute("role");
|
|
357
|
+
return assembleDescription({
|
|
358
|
+
element: {
|
|
359
|
+
tag: element.tagName.toLowerCase(),
|
|
360
|
+
...(element.id === "" ? {} : { id: element.id }),
|
|
361
|
+
classes: [...element.classList].slice(0, 8),
|
|
362
|
+
...(role === null ? {} : { role }),
|
|
363
|
+
text: normalizeText(stripMarkers(element.textContent ?? "")),
|
|
364
|
+
ownText,
|
|
365
|
+
...(hasValue && typeof control.value === "string" ? { value: stripMarkers(control.value) } : {}),
|
|
366
|
+
interactive: element.matches(interactiveSelector) || element.closest(interactiveSelector) !== null,
|
|
367
|
+
},
|
|
368
|
+
learned: bindings,
|
|
369
|
+
calls: elementCallList,
|
|
370
|
+
states,
|
|
371
|
+
contracts,
|
|
372
|
+
sourceMap: sourceMap.map,
|
|
373
|
+
...(sourceMap.status === undefined ? {} : { sourceMapStatus: sourceMap.status }),
|
|
374
|
+
component,
|
|
375
|
+
source,
|
|
376
|
+
resolveText,
|
|
377
|
+
messages: messages.messages,
|
|
378
|
+
markerStatus: messages.status,
|
|
379
|
+
messageSourcesStatus: messages.sources,
|
|
380
|
+
handlersStatus: handlersForbidden ? "forbidden" : undefined,
|
|
381
|
+
remote,
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
async function open(location) {
|
|
385
|
+
try {
|
|
386
|
+
// One open path for every location: the endpoint validates each kind (sources.ts, resolveOpenLocation).
|
|
387
|
+
const target = location.editor === "runic" && location.id !== undefined ? { id: location.id }
|
|
388
|
+
: location.editor === "translations" && location.message !== undefined
|
|
389
|
+
? { message: { catalog: location.message.catalog, key: location.message.key, locale: location.message.locale } }
|
|
390
|
+
: { file: location.file, line: location.line, column: location.column };
|
|
391
|
+
if (request !== undefined)
|
|
392
|
+
return (await request("open", target)).status === 200;
|
|
393
|
+
const response = await fetch(openPath, {
|
|
394
|
+
method: "POST", credentials: "same-origin", headers: { "Content-Type": "application/json" },
|
|
395
|
+
body: JSON.stringify(target),
|
|
396
|
+
});
|
|
397
|
+
return response.ok;
|
|
398
|
+
}
|
|
399
|
+
catch {
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
let overlay;
|
|
404
|
+
/** The element the card describes. */
|
|
405
|
+
let selected;
|
|
406
|
+
/** The text-change channel the overview listens to, once there is one. */
|
|
407
|
+
let textChannel;
|
|
408
|
+
let refreshTimer;
|
|
409
|
+
let active = false;
|
|
410
|
+
let hovered;
|
|
411
|
+
let frame;
|
|
412
|
+
let selection = 0;
|
|
413
|
+
function createOverlay() {
|
|
414
|
+
const element = document.createElement(tagName);
|
|
415
|
+
element.setAttribute("style", "all: initial; position: fixed; inset: 0; pointer-events: none; z-index: 2147483647;");
|
|
416
|
+
const shadow = element.attachShadow({ mode: "open" });
|
|
417
|
+
const style = document.createElement("style");
|
|
418
|
+
style.textContent = styles;
|
|
419
|
+
const make = (className, tag = "div") => {
|
|
420
|
+
const part = document.createElement(tag);
|
|
421
|
+
part.className = className;
|
|
422
|
+
shadow.append(part);
|
|
423
|
+
return part;
|
|
424
|
+
};
|
|
425
|
+
shadow.append(style);
|
|
426
|
+
const glass = make("glass");
|
|
427
|
+
const box = make("box");
|
|
428
|
+
const tip = make("tip");
|
|
429
|
+
const bar = make("bar");
|
|
430
|
+
bar.textContent = "Runic Inspect: click an element · Esc or Alt+Shift+X to exit";
|
|
431
|
+
const card = make("card", "section");
|
|
432
|
+
// Only select mode shows the glass, which takes the page's pointer events, and the bar.
|
|
433
|
+
glass.hidden = bar.hidden = card.hidden = true;
|
|
434
|
+
card.setAttribute("aria-label", "Runic Inspect");
|
|
435
|
+
const overview = document.createElement("div");
|
|
436
|
+
overview.className = "overview";
|
|
437
|
+
const editor = createMessageEditor({
|
|
438
|
+
base,
|
|
439
|
+
location: link,
|
|
440
|
+
onSaved: scheduleRefresh,
|
|
441
|
+
onClose: (returnFocus) => {
|
|
442
|
+
if (!returnFocus)
|
|
443
|
+
return;
|
|
444
|
+
const opener = [...overview.querySelectorAll("button.edit")].find((button) => button.dataset["message"] === editing);
|
|
445
|
+
(opener ?? overview.querySelector("button.close"))?.focus();
|
|
446
|
+
},
|
|
447
|
+
});
|
|
448
|
+
card.append(overview, editor.root);
|
|
449
|
+
// Typing in the editor is not the page's: keys stop at the card instead of reaching the page's own handlers.
|
|
450
|
+
for (const type of ["keydown", "keyup", "keypress"])
|
|
451
|
+
card.addEventListener(type, (event) => event.stopPropagation());
|
|
452
|
+
document.documentElement.append(element);
|
|
453
|
+
return { host: element, glass, box, tip, bar, card, overview, editor };
|
|
454
|
+
}
|
|
455
|
+
/** The `catalog:key` the editor was opened on, to return the focus to its Edit button. */
|
|
456
|
+
let editing;
|
|
457
|
+
function openEditor(message) {
|
|
458
|
+
if (overlay === undefined)
|
|
459
|
+
return;
|
|
460
|
+
listenToText();
|
|
461
|
+
editing = `${message.catalog}:${message.key}`;
|
|
462
|
+
void overlay.editor.open({ catalog: message.catalog, key: message.key, locale: message.locale });
|
|
463
|
+
}
|
|
464
|
+
// The page's text changed (an in-place update after a save, an edit in an editor, a language switch, marker mode):
|
|
465
|
+
// once the frameworks rendered it, the editor reads its message again and the overview describes the same element.
|
|
466
|
+
function listenToText() {
|
|
467
|
+
const channel = host[translationsTextKey];
|
|
468
|
+
if (textChannel !== undefined || !(channel?.listeners instanceof Set))
|
|
469
|
+
return;
|
|
470
|
+
textChannel = channel;
|
|
471
|
+
textChannel.listeners.add(scheduleRefresh);
|
|
472
|
+
}
|
|
473
|
+
function scheduleRefresh() {
|
|
474
|
+
if (overlay === undefined || overlay.card.hidden || refreshTimer !== undefined)
|
|
475
|
+
return;
|
|
476
|
+
refreshTimer = setTimeout(() => {
|
|
477
|
+
refreshTimer = undefined;
|
|
478
|
+
void refresh();
|
|
479
|
+
}, 60);
|
|
480
|
+
}
|
|
481
|
+
async function refresh() {
|
|
482
|
+
if (overlay === undefined || overlay.card.hidden)
|
|
483
|
+
return;
|
|
484
|
+
void overlay.editor.refresh();
|
|
485
|
+
// Outside select mode the page has no markers, so describing it again would lose the messages the card lists.
|
|
486
|
+
if (selected === undefined || !selected.isConnected || translationMarkers()?.enabled !== true)
|
|
487
|
+
return;
|
|
488
|
+
const current = ++selection;
|
|
489
|
+
const description = await describe(selected);
|
|
490
|
+
if (current === selection && !overlay.card.hidden)
|
|
491
|
+
render(description);
|
|
492
|
+
}
|
|
493
|
+
function elementAt(x, y) {
|
|
494
|
+
const hostElement = overlay?.host;
|
|
495
|
+
let element = document.elementsFromPoint(x, y).find((candidate) => candidate !== hostElement && !hostElement?.contains(candidate));
|
|
496
|
+
// Into the page's own open shadow roots.
|
|
497
|
+
for (let depth = 0; element?.shadowRoot && depth < 10; depth++) {
|
|
498
|
+
const inner = element.shadowRoot.elementFromPoint(x, y);
|
|
499
|
+
if (inner === null || inner === element)
|
|
500
|
+
break;
|
|
501
|
+
element = inner;
|
|
502
|
+
}
|
|
503
|
+
return element;
|
|
504
|
+
}
|
|
505
|
+
function outline(element) {
|
|
506
|
+
if (overlay === undefined)
|
|
507
|
+
return;
|
|
508
|
+
const { box, tip } = overlay;
|
|
509
|
+
if (element === undefined) {
|
|
510
|
+
box.style.display = tip.style.display = "none";
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
const rect = element.getBoundingClientRect();
|
|
514
|
+
Object.assign(box.style, { display: "block", left: `${rect.left}px`, top: `${rect.top}px`, width: `${rect.width}px`, height: `${rect.height}px` });
|
|
515
|
+
const component = findComponentSync(element, root, host.ng)?.component;
|
|
516
|
+
const id = element.id === "" ? "" : `#${element.id}`;
|
|
517
|
+
tip.textContent = `${element.tagName.toLowerCase()}${id}${component === undefined ? "" : ` · ${component.name}`}`;
|
|
518
|
+
tip.style.display = "block";
|
|
519
|
+
const top = rect.top > 28 ? rect.top - 24 : Math.min(rect.bottom + 4, innerHeight - 24);
|
|
520
|
+
Object.assign(tip.style, { left: `${Math.max(4, Math.min(rect.left, innerWidth - 240))}px`, top: `${top}px` });
|
|
521
|
+
}
|
|
522
|
+
function link(location) {
|
|
523
|
+
if (remote) {
|
|
524
|
+
// /__runic/open answers pages of the development machine only; the location is still worth knowing.
|
|
525
|
+
const plain = document.createElement("span");
|
|
526
|
+
plain.className = "muted";
|
|
527
|
+
plain.textContent = `${shortFile(location.file, root)}:${location.line}`;
|
|
528
|
+
plain.title = `${location.file}:${location.line}:${location.column} (opens in the editor only from the development machine)`;
|
|
529
|
+
plain.dataset["location"] = location.editor;
|
|
530
|
+
return plain;
|
|
531
|
+
}
|
|
532
|
+
const anchor = document.createElement("a");
|
|
533
|
+
anchor.href = "#";
|
|
534
|
+
anchor.textContent = `${shortFile(location.file, root)}:${location.line}`;
|
|
535
|
+
anchor.title = `Open ${location.file}:${location.line}:${location.column} in the editor`;
|
|
536
|
+
anchor.dataset["open"] = location.editor;
|
|
537
|
+
anchor.addEventListener("click", (event) => {
|
|
538
|
+
event.preventDefault();
|
|
539
|
+
void open(location);
|
|
540
|
+
});
|
|
541
|
+
return anchor;
|
|
542
|
+
}
|
|
543
|
+
// An overview control as it is rendered again: the same kind, message and text find its counterpart.
|
|
544
|
+
function controlKey(element) {
|
|
545
|
+
const control = element;
|
|
546
|
+
return [control.tagName, control.className, control.dataset["message"] ?? "", control.dataset["open"] ?? "",
|
|
547
|
+
control.getAttribute("title") ?? "", control.textContent ?? ""].join("\u0000");
|
|
548
|
+
}
|
|
549
|
+
function render(description) {
|
|
550
|
+
const { host: overlayHost, card, overview, editor } = overlay;
|
|
551
|
+
// Rendering again replaces the overview's controls. A refresh (after a save, an edit elsewhere, a language switch)
|
|
552
|
+
// can arrive while the keyboard focus is on one of them, such as the Edit button Esc returned to: the focus moves
|
|
553
|
+
// to its counterpart instead of falling to the document.
|
|
554
|
+
const focused = overlayHost.shadowRoot?.activeElement;
|
|
555
|
+
const refocus = focused instanceof Element && overview.contains(focused) ? controlKey(focused) : undefined;
|
|
556
|
+
overview.replaceChildren();
|
|
557
|
+
const header = document.createElement("header");
|
|
558
|
+
const title = document.createElement("h2");
|
|
559
|
+
const { element } = description;
|
|
560
|
+
title.textContent = `<${element.tag}${element.id === undefined ? "" : `#${element.id}`}>${element.text === "" ? "" : ` ${element.text.slice(0, 40)}`}`;
|
|
561
|
+
const close = document.createElement("button");
|
|
562
|
+
close.className = "close";
|
|
563
|
+
close.textContent = "×";
|
|
564
|
+
close.title = "Close";
|
|
565
|
+
close.addEventListener("click", () => {
|
|
566
|
+
editor.close();
|
|
567
|
+
card.hidden = true;
|
|
568
|
+
});
|
|
569
|
+
header.append(title, close);
|
|
570
|
+
overview.append(header);
|
|
571
|
+
const section = (heading, items) => {
|
|
572
|
+
if (items.length === 0)
|
|
573
|
+
return;
|
|
574
|
+
const h3 = document.createElement("h3");
|
|
575
|
+
h3.textContent = heading;
|
|
576
|
+
const list = document.createElement("ul");
|
|
577
|
+
for (const parts of items) {
|
|
578
|
+
const item = document.createElement("li");
|
|
579
|
+
item.append(...parts);
|
|
580
|
+
list.append(item);
|
|
581
|
+
}
|
|
582
|
+
overview.append(h3, list);
|
|
583
|
+
};
|
|
584
|
+
const code = (value) => {
|
|
585
|
+
const element = document.createElement("code");
|
|
586
|
+
element.textContent = value;
|
|
587
|
+
return element;
|
|
588
|
+
};
|
|
589
|
+
const muted = (value) => {
|
|
590
|
+
const element = document.createElement("span");
|
|
591
|
+
element.className = "muted";
|
|
592
|
+
element.textContent = value;
|
|
593
|
+
return element;
|
|
594
|
+
};
|
|
595
|
+
const component = description.component;
|
|
596
|
+
if (component !== undefined) {
|
|
597
|
+
section("Component", [[
|
|
598
|
+
code(component.name), muted(` ${component.framework}`),
|
|
599
|
+
...(component.owners.length === 0 ? [] : [muted(` in ${component.owners.slice(0, 3).join(" › ")}`)]),
|
|
600
|
+
...(component.location === undefined ? [] : [" ", link(component.location)]),
|
|
601
|
+
]]);
|
|
602
|
+
}
|
|
603
|
+
section("Commands", description.commands.map((command) => [
|
|
604
|
+
code(`${command.member}()`), " → ", code(command.symbol ?? "?"),
|
|
605
|
+
...(command.location === undefined ? [] : [" ", link(command.location)]),
|
|
606
|
+
command.evidence === "source" ? muted(" · from the source, not called yet") : muted(` · ${command.calls} call${command.calls === 1 ? "" : "s"}`),
|
|
607
|
+
...(command.frontend === undefined ? [] : [muted(" · called at "), link(command.frontend)]),
|
|
608
|
+
...(command.canExecute === undefined ? [] : [muted(` · can run: ${text(command.canExecute)}`)]),
|
|
609
|
+
]));
|
|
610
|
+
section("ViewModel members", description.members.map((member) => [
|
|
611
|
+
code(member.member), muted(` ${member.kind}`), " = ", code(text(member.value)),
|
|
612
|
+
muted(member.evidence === "call" ? "" : member.evidence === "text" ? " · matches the text" : " · matches the value"),
|
|
613
|
+
...(member.symbol === undefined ? [] : [" → ", code(member.symbol)]),
|
|
614
|
+
...(member.location === undefined ? [] : [" ", link(member.location)]),
|
|
615
|
+
]));
|
|
616
|
+
section("ViewModels", description.viewModels.map((viewModel) => [
|
|
617
|
+
code(viewModel.name ?? viewModel.id ?? viewModel.route), muted(` route ${viewModel.route}`),
|
|
618
|
+
...(viewModel.id === undefined ? [] : [muted(` · ${viewModel.id}`)]),
|
|
619
|
+
...(viewModel.location === undefined ? [] : [" ", link(viewModel.location)]),
|
|
620
|
+
]));
|
|
621
|
+
section("Recent Bridge calls", description.calls.slice(0, 5).map((call) => [
|
|
622
|
+
code(`${call.kind} ${call.member}`), muted(` #${call.id}`),
|
|
623
|
+
` ${call.duration === undefined ? "running" : `${call.duration.toFixed(1)} ms`}`,
|
|
624
|
+
...(call.outcome === undefined ? [] : [` ${call.outcome}${call.reason === undefined ? "" : ` (${call.reason})`}`]),
|
|
625
|
+
...(call.revision === undefined ? [] : [muted(` · revision ${call.revision}`)]),
|
|
626
|
+
]));
|
|
627
|
+
section("Messages", description.messages.map((message) => [
|
|
628
|
+
code(`${message.catalog}:${message.key}`), message.locale === "" ? "" : muted(` [${message.locale}]`),
|
|
629
|
+
...(message.source === undefined ? [] : [` ${JSON.stringify(message.source.length > 80 ? `${message.source.slice(0, 79)}…` : message.source)}`]),
|
|
630
|
+
...(message.in.length === 1 && message.in[0] === "text" ? [] : [muted(` · in ${message.in.join(", ")}`)]),
|
|
631
|
+
...(message.location === undefined ? [] : [" ", link(message.location)]),
|
|
632
|
+
...(message.editable === true ? [editButton(message)] : []),
|
|
633
|
+
]));
|
|
634
|
+
section("Texts from the ViewModel", description.texts.map((entry) => [
|
|
635
|
+
code(`${entry.catalog}:${entry.key}`), entry.locale === undefined ? "" : muted(` [${entry.locale}]`),
|
|
636
|
+
` ${JSON.stringify(entry.text)}`, muted(` · ${entry.route}.${entry.path}`),
|
|
637
|
+
]));
|
|
638
|
+
for (const note of description.notes) {
|
|
639
|
+
const paragraph = document.createElement("p");
|
|
640
|
+
paragraph.className = "note";
|
|
641
|
+
paragraph.textContent = note;
|
|
642
|
+
overview.append(paragraph);
|
|
643
|
+
}
|
|
644
|
+
card.hidden = false;
|
|
645
|
+
if (refocus !== undefined) {
|
|
646
|
+
const counterpart = [...overview.querySelectorAll("button, a")].find((control) => controlKey(control) === refocus)
|
|
647
|
+
?? overview.querySelector("button.close");
|
|
648
|
+
counterpart?.focus({ preventScroll: true });
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
function editButton(message) {
|
|
652
|
+
const button = document.createElement("button");
|
|
653
|
+
button.type = "button";
|
|
654
|
+
button.className = "edit";
|
|
655
|
+
button.textContent = "Edit";
|
|
656
|
+
button.dataset["message"] = `${message.catalog}:${message.key}`;
|
|
657
|
+
button.setAttribute("aria-label", `Edit ${message.catalog}:${message.key}`);
|
|
658
|
+
button.addEventListener("click", () => openEditor(message));
|
|
659
|
+
return button;
|
|
660
|
+
}
|
|
661
|
+
async function select(element) {
|
|
662
|
+
overlay ??= createOverlay();
|
|
663
|
+
listenToText();
|
|
664
|
+
// Another element: an editor without unsaved text closes; one with unsaved text stays open.
|
|
665
|
+
if (element !== selected && overlay.editor.isOpen && overlay.editor.state !== undefined && !hasEdits(overlay.editor.state))
|
|
666
|
+
overlay.editor.close();
|
|
667
|
+
selected = element;
|
|
668
|
+
const current = ++selection;
|
|
669
|
+
const description = await describe(element);
|
|
670
|
+
if (current === selection)
|
|
671
|
+
render(description);
|
|
672
|
+
return description;
|
|
673
|
+
}
|
|
674
|
+
function setActive(on) {
|
|
675
|
+
if (on === active)
|
|
676
|
+
return active;
|
|
677
|
+
active = on;
|
|
678
|
+
// Message markers only while selecting: the page renders again with them, and again without them.
|
|
679
|
+
try {
|
|
680
|
+
translationMarkers()?.set(on);
|
|
681
|
+
}
|
|
682
|
+
catch { /* a foreign channel */ }
|
|
683
|
+
if (on)
|
|
684
|
+
overlay ??= createOverlay();
|
|
685
|
+
if (overlay !== undefined) {
|
|
686
|
+
overlay.glass.hidden = overlay.bar.hidden = !on;
|
|
687
|
+
if (!on)
|
|
688
|
+
outline(hovered = undefined);
|
|
689
|
+
}
|
|
690
|
+
return active;
|
|
691
|
+
}
|
|
692
|
+
function isOverlayUi(event) {
|
|
693
|
+
if (overlay === undefined)
|
|
694
|
+
return false;
|
|
695
|
+
const path = event.composedPath();
|
|
696
|
+
return path.includes(overlay.card);
|
|
697
|
+
}
|
|
698
|
+
// In select mode the glass takes every pointer event; the window's capture phase stops them before the page's
|
|
699
|
+
// handlers (document, window and React's root listeners) see them, and handles them here.
|
|
700
|
+
function onPointer(event) {
|
|
701
|
+
if (!active || isOverlayUi(event))
|
|
702
|
+
return;
|
|
703
|
+
event.preventDefault();
|
|
704
|
+
event.stopImmediatePropagation();
|
|
705
|
+
const pointer = event;
|
|
706
|
+
if (event.type === "pointermove" || event.type === "mousemove") {
|
|
707
|
+
const { clientX, clientY } = pointer;
|
|
708
|
+
if (frame !== undefined)
|
|
709
|
+
return;
|
|
710
|
+
frame = requestAnimationFrame(() => {
|
|
711
|
+
frame = undefined;
|
|
712
|
+
const element = elementAt(clientX, clientY);
|
|
713
|
+
if (element !== hovered)
|
|
714
|
+
outline(hovered = element);
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
else if (event.type === "pointerdown" || (event.type === "mousedown" && typeof PointerEvent === "undefined")) {
|
|
718
|
+
if (pointer.button !== 0)
|
|
719
|
+
return;
|
|
720
|
+
const element = elementAt(pointer.clientX, pointer.clientY);
|
|
721
|
+
if (element === undefined)
|
|
722
|
+
return;
|
|
723
|
+
outline(hovered = element);
|
|
724
|
+
void select(element);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
function onKey(event) {
|
|
728
|
+
const toggle = event.altKey && event.shiftKey && !event.ctrlKey && !event.metaKey
|
|
729
|
+
&& (event.code === "KeyX" || event.key === "X" || event.key === "x");
|
|
730
|
+
if (toggle) {
|
|
731
|
+
event.preventDefault();
|
|
732
|
+
event.stopImmediatePropagation();
|
|
733
|
+
setActive(!active);
|
|
734
|
+
}
|
|
735
|
+
else if (event.key === "Escape" && overlay?.editor.isOpen === true && event.composedPath().includes(overlay.editor.root)) {
|
|
736
|
+
// Esc in the editor closes the editor only, and the focus returns to its Edit button.
|
|
737
|
+
event.preventDefault();
|
|
738
|
+
event.stopImmediatePropagation();
|
|
739
|
+
overlay.editor.close(true);
|
|
740
|
+
}
|
|
741
|
+
else if (event.key === "Escape" && (active || overlay?.card.hidden === false)) {
|
|
742
|
+
event.preventDefault();
|
|
743
|
+
event.stopImmediatePropagation();
|
|
744
|
+
if (active)
|
|
745
|
+
setActive(false);
|
|
746
|
+
else
|
|
747
|
+
overlay.card.hidden = true;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
for (const type of swallowed)
|
|
751
|
+
addEventListener(type, onPointer, { capture: true, passive: false });
|
|
752
|
+
addEventListener("keydown", onKey, { capture: true });
|
|
753
|
+
const api = {
|
|
754
|
+
version: 1,
|
|
755
|
+
get active() { return active; },
|
|
756
|
+
toggle: (on) => setActive(on ?? !active),
|
|
757
|
+
describe,
|
|
758
|
+
select,
|
|
759
|
+
open,
|
|
760
|
+
};
|
|
761
|
+
host[inspectKey] = api;
|
|
762
|
+
return () => {
|
|
763
|
+
calls.listeners.delete(onCall);
|
|
764
|
+
inspector.listeners.delete(onInspector);
|
|
765
|
+
for (const type of swallowed)
|
|
766
|
+
removeEventListener(type, onPointer, { capture: true });
|
|
767
|
+
removeEventListener("keydown", onKey, { capture: true });
|
|
768
|
+
if (frame !== undefined)
|
|
769
|
+
cancelAnimationFrame(frame);
|
|
770
|
+
if (active)
|
|
771
|
+
try {
|
|
772
|
+
translationMarkers()?.set(false);
|
|
773
|
+
}
|
|
774
|
+
catch { /* a foreign channel */ }
|
|
775
|
+
textChannel?.listeners.delete(scheduleRefresh);
|
|
776
|
+
if (refreshTimer !== undefined)
|
|
777
|
+
clearTimeout(refreshTimer);
|
|
778
|
+
overlay?.host.remove();
|
|
779
|
+
overlay = undefined;
|
|
780
|
+
if (host[inspectKey] === api)
|
|
781
|
+
delete host[inspectKey];
|
|
782
|
+
};
|
|
783
|
+
}
|