@mikuexe/annotator-effect 0.0.0-stage → 0.1.1
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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +263 -2
- package/dist/annotator.d.ts +5 -0
- package/dist/annotator.d.ts.map +1 -0
- package/dist/annotator.js +152 -0
- package/dist/annotator.js.map +1 -0
- package/dist/capture.d.ts +27 -0
- package/dist/capture.d.ts.map +1 -0
- package/dist/capture.js +186 -0
- package/dist/capture.js.map +1 -0
- package/dist/clipboard.d.ts +5 -0
- package/dist/clipboard.d.ts.map +1 -0
- package/dist/clipboard.js +35 -0
- package/dist/clipboard.js.map +1 -0
- package/dist/constants.d.ts +24 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +24 -0
- package/dist/constants.js.map +1 -0
- package/dist/format.d.ts +9 -0
- package/dist/format.d.ts.map +1 -0
- package/dist/format.js +109 -0
- package/dist/format.js.map +1 -0
- package/dist/hotkey.d.ts +13 -0
- package/dist/hotkey.d.ts.map +1 -0
- package/dist/hotkey.js +54 -0
- package/dist/hotkey.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/overlay/machine.d.ts +132 -0
- package/dist/overlay/machine.d.ts.map +1 -0
- package/dist/overlay/machine.js +236 -0
- package/dist/overlay/machine.js.map +1 -0
- package/dist/overlay/overlay.d.ts +26 -0
- package/dist/overlay/overlay.d.ts.map +1 -0
- package/dist/overlay/overlay.js +748 -0
- package/dist/overlay/overlay.js.map +1 -0
- package/dist/overlay/styles.d.ts +8 -0
- package/dist/overlay/styles.d.ts.map +1 -0
- package/dist/overlay/styles.js +386 -0
- package/dist/overlay/styles.js.map +1 -0
- package/dist/runtime/agent.d.ts +37 -0
- package/dist/runtime/agent.d.ts.map +1 -0
- package/dist/runtime/agent.js +142 -0
- package/dist/runtime/agent.js.map +1 -0
- package/dist/runtime/paths.d.ts +13 -0
- package/dist/runtime/paths.d.ts.map +1 -0
- package/dist/runtime/paths.js +64 -0
- package/dist/runtime/paths.js.map +1 -0
- package/dist/types.d.ts +106 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vite/analyze.d.ts +41 -0
- package/dist/vite/analyze.d.ts.map +1 -0
- package/dist/vite/analyze.js +324 -0
- package/dist/vite/analyze.js.map +1 -0
- package/dist/vite/ast.d.ts +38 -0
- package/dist/vite/ast.d.ts.map +1 -0
- package/dist/vite/ast.js +105 -0
- package/dist/vite/ast.js.map +1 -0
- package/dist/vite/index.d.ts +5 -0
- package/dist/vite/index.d.ts.map +1 -0
- package/dist/vite/index.js +3 -0
- package/dist/vite/index.js.map +1 -0
- package/dist/vite/plugin.d.ts +34 -0
- package/dist/vite/plugin.d.ts.map +1 -0
- package/dist/vite/plugin.js +181 -0
- package/dist/vite/plugin.js.map +1 -0
- package/dist/vite/scope.d.ts +39 -0
- package/dist/vite/scope.d.ts.map +1 -0
- package/dist/vite/scope.js +194 -0
- package/dist/vite/scope.js.map +1 -0
- package/dist/vite/tags.d.ts +5 -0
- package/dist/vite/tags.d.ts.map +1 -0
- package/dist/vite/tags.js +151 -0
- package/dist/vite/tags.js.map +1 -0
- package/dist/vite/transform.d.ts +39 -0
- package/dist/vite/transform.d.ts.map +1 -0
- package/dist/vite/transform.js +111 -0
- package/dist/vite/transform.js.map +1 -0
- package/package.json +104 -3
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
import { captureAnnotationTarget, createAnnotationId, parseSourceAttribute, } from "../capture.js";
|
|
2
|
+
import { copyTextToClipboard } from "../clipboard.js";
|
|
3
|
+
import { FRAME_TARGET_ATTRIBUTE, OVERLAY_ROOT_ATTRIBUTE, SOURCE_ATTRIBUTE, } from "../constants.js";
|
|
4
|
+
import { createAnnotationCollection, formatAnnotationCollection, formatLocation, getPageContext, } from "../format.js";
|
|
5
|
+
import { matchesHotkey } from "../hotkey.js";
|
|
6
|
+
import { readAgent } from "../runtime/agent.js";
|
|
7
|
+
import { acceptsPicks, initialOverlayState, isSessionActive, transition, } from "./machine.js";
|
|
8
|
+
import { overlayStyles } from "./styles.js";
|
|
9
|
+
// Input the page must not see while picking: the press and release around
|
|
10
|
+
// a click, and the gestures that open menus or select text.
|
|
11
|
+
const BLOCKED_INTERACTION_EVENTS = [
|
|
12
|
+
"pointerdown",
|
|
13
|
+
"pointerup",
|
|
14
|
+
"mousedown",
|
|
15
|
+
"mouseup",
|
|
16
|
+
"dblclick",
|
|
17
|
+
"auxclick",
|
|
18
|
+
"contextmenu",
|
|
19
|
+
"touchstart",
|
|
20
|
+
"touchend",
|
|
21
|
+
];
|
|
22
|
+
const TOAST_DURATION_MS = 3500;
|
|
23
|
+
export function mountOverlay(hostDocument, initialSettings, resolverFor) {
|
|
24
|
+
let settings = initialSettings;
|
|
25
|
+
let state = initialOverlayState;
|
|
26
|
+
let target = resolveTarget(settings.target, hostDocument);
|
|
27
|
+
let pickingWindow = null;
|
|
28
|
+
let frameForLoad = null;
|
|
29
|
+
let renderQueued = false;
|
|
30
|
+
let destroyed = false;
|
|
31
|
+
const hostWindow = hostDocument.defaultView ?? window;
|
|
32
|
+
const host = hostDocument.createElement("div");
|
|
33
|
+
host.setAttribute(OVERLAY_ROOT_ATTRIBUTE, "");
|
|
34
|
+
const shadow = host.attachShadow({ mode: "open" });
|
|
35
|
+
const style = hostDocument.createElement("style");
|
|
36
|
+
style.textContent = overlayStyles;
|
|
37
|
+
const view = createView(hostDocument);
|
|
38
|
+
shadow.append(style, view.root);
|
|
39
|
+
(hostDocument.body ?? hostDocument.documentElement).append(host);
|
|
40
|
+
const resolver = () => resolverFor(target.document?.defaultView ?? null);
|
|
41
|
+
const dispatch = (event) => {
|
|
42
|
+
const previous = state;
|
|
43
|
+
const result = transition(state, event);
|
|
44
|
+
if (!result.accepted || result.state === previous) {
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
state = result.state;
|
|
48
|
+
// Another top-layer tool, such as the DevTools panel, may have been
|
|
49
|
+
// raised since; each new session status brings the overlay back on top.
|
|
50
|
+
if (previous.status !== state.status && isSessionActive(state)) {
|
|
51
|
+
raise(host);
|
|
52
|
+
}
|
|
53
|
+
syncPickingListeners();
|
|
54
|
+
render();
|
|
55
|
+
if (previous.status !== "collecting" && state.status === "collecting") {
|
|
56
|
+
void collect(state.annotations);
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
};
|
|
60
|
+
const collect = async (annotations) => {
|
|
61
|
+
const payload = createAnnotationCollection(stripAnnotations(annotations), getPageContext(target.document));
|
|
62
|
+
const text = formatAnnotationCollection(payload, settings.output);
|
|
63
|
+
try {
|
|
64
|
+
await copyTextToClipboard(text, hostDocument);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
const reason = error instanceof Error ? error.message : "Clipboard copy failed.";
|
|
68
|
+
dispatch({ type: "FailedCollect", reason });
|
|
69
|
+
view.toast("error", "Copy failed", reason, TOAST_DURATION_MS);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
settings.onCollect?.(payload);
|
|
73
|
+
dispatch({ type: "SucceededCollect" });
|
|
74
|
+
view.toast("success", "Annotations copied", `${payload.annotations.length} copied to clipboard.`, TOAST_DURATION_MS);
|
|
75
|
+
};
|
|
76
|
+
const render = () => {
|
|
77
|
+
if (destroyed) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
host.setAttribute("data-position", settings.position);
|
|
81
|
+
view.render(state, settings, {
|
|
82
|
+
dispatch,
|
|
83
|
+
rectOf: (element, frame) => getRect(element, frame),
|
|
84
|
+
labelOf: (element) => hoverLabel(element, resolver()),
|
|
85
|
+
viewport: {
|
|
86
|
+
width: hostWindow.innerWidth,
|
|
87
|
+
height: hostWindow.innerHeight,
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
const scheduleRender = () => {
|
|
92
|
+
if (renderQueued || destroyed) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
renderQueued = true;
|
|
96
|
+
hostWindow.requestAnimationFrame(() => {
|
|
97
|
+
renderQueued = false;
|
|
98
|
+
render();
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
const pickableElement = (event) => {
|
|
102
|
+
const path = event.composedPath();
|
|
103
|
+
if (path.includes(host)) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
const first = path[0];
|
|
107
|
+
const activeDocument = target.document;
|
|
108
|
+
if (first?.nodeType !== 1 || !activeDocument) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const element = first;
|
|
112
|
+
return element === activeDocument.body ||
|
|
113
|
+
element === activeDocument.documentElement
|
|
114
|
+
? null
|
|
115
|
+
: element;
|
|
116
|
+
};
|
|
117
|
+
const onPointerOver = (event) => {
|
|
118
|
+
if (event.composedPath().includes(host)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const element = pickableElement(event);
|
|
122
|
+
const hovered = acceptsPicks(state) ? state.hovered : null;
|
|
123
|
+
if (hovered?.element === element) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
dispatch({
|
|
127
|
+
type: "Hovered",
|
|
128
|
+
target: element ? { element, frame: target.frame } : null,
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
const onPointerOut = (event) => {
|
|
132
|
+
if (event.relatedTarget === null) {
|
|
133
|
+
dispatch({ type: "Hovered", target: null });
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const suppressInteraction = (event) => {
|
|
137
|
+
if (!pickableElement(event)) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
event.preventDefault();
|
|
141
|
+
event.stopPropagation();
|
|
142
|
+
event.stopImmediatePropagation();
|
|
143
|
+
};
|
|
144
|
+
const onClick = (event) => {
|
|
145
|
+
const element = pickableElement(event);
|
|
146
|
+
if (!element) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
event.preventDefault();
|
|
150
|
+
event.stopPropagation();
|
|
151
|
+
event.stopImmediatePropagation();
|
|
152
|
+
const mouse = event;
|
|
153
|
+
const picked = {
|
|
154
|
+
element,
|
|
155
|
+
frame: target.frame,
|
|
156
|
+
data: captureAnnotationTarget(element, resolver()),
|
|
157
|
+
};
|
|
158
|
+
dispatch({
|
|
159
|
+
type: "Picked",
|
|
160
|
+
target: picked,
|
|
161
|
+
extend: mouse.metaKey || mouse.ctrlKey,
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
const onFrameLeave = () => dispatch({ type: "Hovered", target: null });
|
|
165
|
+
const onKeyDown = (event) => {
|
|
166
|
+
const keyboard = event;
|
|
167
|
+
if (matchesHotkey(keyboard, settings.hotkey)) {
|
|
168
|
+
keyboard.preventDefault();
|
|
169
|
+
dispatch({ type: "Toggled" });
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (keyboard.key === "Escape" && isSessionActive(state)) {
|
|
173
|
+
keyboard.preventDefault();
|
|
174
|
+
dispatch({ type: "PressedEscape" });
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
// A dialog or popover the page opens lands in the top layer above the
|
|
178
|
+
// overlay; raising again keeps outlines visible over it.
|
|
179
|
+
const onToggle = (event) => {
|
|
180
|
+
if (isSessionActive(state) &&
|
|
181
|
+
event.target !== host &&
|
|
182
|
+
event.newState === "open") {
|
|
183
|
+
raise(host);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
const syncPickingListeners = () => {
|
|
187
|
+
const wanted = acceptsPicks(state) && target.document
|
|
188
|
+
? target.document.defaultView
|
|
189
|
+
: null;
|
|
190
|
+
if (wanted === pickingWindow) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (pickingWindow) {
|
|
194
|
+
togglePickingListeners(pickingWindow, false);
|
|
195
|
+
}
|
|
196
|
+
pickingWindow = wanted;
|
|
197
|
+
if (pickingWindow) {
|
|
198
|
+
togglePickingListeners(pickingWindow, true);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
const togglePickingListeners = (targetWindow, on) => {
|
|
202
|
+
const method = on ? "addEventListener" : "removeEventListener";
|
|
203
|
+
targetWindow[method]("pointerover", onPointerOver, true);
|
|
204
|
+
targetWindow[method]("pointerout", onPointerOut, true);
|
|
205
|
+
targetWindow[method]("click", onClick, true);
|
|
206
|
+
BLOCKED_INTERACTION_EVENTS.forEach((type) => targetWindow[method](type, suppressInteraction, true));
|
|
207
|
+
targetWindow[method]("scroll", scheduleRender, true);
|
|
208
|
+
target.frame?.[method]("pointerleave", onFrameLeave);
|
|
209
|
+
if (targetWindow !== hostWindow) {
|
|
210
|
+
hostWindow[method]("scroll", scheduleRender, true);
|
|
211
|
+
}
|
|
212
|
+
hostWindow[method]("resize", scheduleRender);
|
|
213
|
+
};
|
|
214
|
+
const keyTargets = () => [hostDocument, target.document].filter((doc, index, all) => doc !== null && all.indexOf(doc) === index);
|
|
215
|
+
const attachDocumentListeners = () => {
|
|
216
|
+
keyTargets().forEach((doc) => doc.addEventListener("keydown", onKeyDown));
|
|
217
|
+
hostDocument.addEventListener("toggle", onToggle, true);
|
|
218
|
+
claimFrame();
|
|
219
|
+
};
|
|
220
|
+
const detachDocumentListeners = () => {
|
|
221
|
+
keyTargets().forEach((doc) => doc.removeEventListener("keydown", onKeyDown));
|
|
222
|
+
hostDocument.removeEventListener("toggle", onToggle, true);
|
|
223
|
+
frameForLoad?.removeEventListener("load", onFrameLoad);
|
|
224
|
+
frameForLoad = null;
|
|
225
|
+
};
|
|
226
|
+
// A framed page that runs the annotator would show its own overlay too.
|
|
227
|
+
// The frame is marked before it loads, and asked again once it has.
|
|
228
|
+
const claimFrame = () => {
|
|
229
|
+
const frame = target.frame;
|
|
230
|
+
if (!frame) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
frame.setAttribute(FRAME_TARGET_ATTRIBUTE, "");
|
|
234
|
+
suppressFrameOverlay(frame);
|
|
235
|
+
if (frameForLoad !== frame) {
|
|
236
|
+
frameForLoad?.removeEventListener("load", onFrameLoad);
|
|
237
|
+
frame.addEventListener("load", onFrameLoad);
|
|
238
|
+
frameForLoad = frame;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
function onFrameLoad() {
|
|
242
|
+
detachDocumentListeners();
|
|
243
|
+
if (pickingWindow) {
|
|
244
|
+
togglePickingListeners(pickingWindow, false);
|
|
245
|
+
pickingWindow = null;
|
|
246
|
+
}
|
|
247
|
+
target = resolveTarget(settings.target, hostDocument);
|
|
248
|
+
attachDocumentListeners();
|
|
249
|
+
syncPickingListeners();
|
|
250
|
+
render();
|
|
251
|
+
}
|
|
252
|
+
attachDocumentListeners();
|
|
253
|
+
render();
|
|
254
|
+
return {
|
|
255
|
+
host,
|
|
256
|
+
state: () => state,
|
|
257
|
+
dispatch,
|
|
258
|
+
update: (next) => {
|
|
259
|
+
const targetChanged = next.target !== settings.target;
|
|
260
|
+
detachDocumentListeners();
|
|
261
|
+
if (targetChanged && pickingWindow) {
|
|
262
|
+
togglePickingListeners(pickingWindow, false);
|
|
263
|
+
pickingWindow = null;
|
|
264
|
+
}
|
|
265
|
+
settings = next;
|
|
266
|
+
if (targetChanged) {
|
|
267
|
+
target = resolveTarget(settings.target, hostDocument);
|
|
268
|
+
}
|
|
269
|
+
attachDocumentListeners();
|
|
270
|
+
syncPickingListeners();
|
|
271
|
+
render();
|
|
272
|
+
},
|
|
273
|
+
destroy: () => {
|
|
274
|
+
if (destroyed) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
destroyed = true;
|
|
278
|
+
detachDocumentListeners();
|
|
279
|
+
if (pickingWindow) {
|
|
280
|
+
togglePickingListeners(pickingWindow, false);
|
|
281
|
+
pickingWindow = null;
|
|
282
|
+
}
|
|
283
|
+
view.dispose();
|
|
284
|
+
host.remove();
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function suppressFrameOverlay(frame) {
|
|
289
|
+
try {
|
|
290
|
+
readAgent(frame.contentWindow)?.suppressOverlay();
|
|
291
|
+
}
|
|
292
|
+
catch {
|
|
293
|
+
// A cross-origin frame throws on access; it can't be annotated anyway.
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function raise(host) {
|
|
297
|
+
if (typeof host.showPopover !== "function") {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
if (!host.hasAttribute("popover")) {
|
|
301
|
+
host.setAttribute("popover", "manual");
|
|
302
|
+
}
|
|
303
|
+
try {
|
|
304
|
+
if (host.matches(":popover-open")) {
|
|
305
|
+
host.hidePopover();
|
|
306
|
+
}
|
|
307
|
+
host.showPopover();
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
// Without top-layer support the fixed overlay still sits at the top z-index.
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function resolveTarget(target, hostDocument) {
|
|
314
|
+
const frameConstructor = hostDocument.defaultView?.HTMLIFrameElement;
|
|
315
|
+
if (frameConstructor && target instanceof frameConstructor) {
|
|
316
|
+
const frameDocument = contentDocumentOf(target);
|
|
317
|
+
if (!frameDocument) {
|
|
318
|
+
console.warn("@mikuexe/annotator-effect: the target iframe must be same-origin; its contentDocument is not accessible.");
|
|
319
|
+
}
|
|
320
|
+
return { document: frameDocument, frame: target };
|
|
321
|
+
}
|
|
322
|
+
if (target && "documentElement" in target) {
|
|
323
|
+
return { document: target, frame: null };
|
|
324
|
+
}
|
|
325
|
+
return { document: hostDocument, frame: null };
|
|
326
|
+
}
|
|
327
|
+
function contentDocumentOf(frame) {
|
|
328
|
+
try {
|
|
329
|
+
return frame.contentDocument;
|
|
330
|
+
}
|
|
331
|
+
catch {
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function getRect(element, frame) {
|
|
336
|
+
const rect = element.getBoundingClientRect();
|
|
337
|
+
const frameRect = frame?.getBoundingClientRect();
|
|
338
|
+
return {
|
|
339
|
+
top: rect.top + (frameRect?.top ?? 0),
|
|
340
|
+
left: rect.left + (frameRect?.left ?? 0),
|
|
341
|
+
width: rect.width,
|
|
342
|
+
height: rect.height,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
function stripAnnotations(annotations) {
|
|
346
|
+
return annotations.map((annotation) => ({
|
|
347
|
+
id: annotation.id,
|
|
348
|
+
note: annotation.note,
|
|
349
|
+
targets: annotation.targets.map((entry) => entry.data),
|
|
350
|
+
}));
|
|
351
|
+
}
|
|
352
|
+
function hoverLabel(element, resolver) {
|
|
353
|
+
let owner;
|
|
354
|
+
try {
|
|
355
|
+
owner = resolver?.ownerOf(element)?.component;
|
|
356
|
+
}
|
|
357
|
+
catch {
|
|
358
|
+
owner = undefined;
|
|
359
|
+
}
|
|
360
|
+
const site = parseSourceAttribute(element.getAttribute(SOURCE_ATTRIBUTE));
|
|
361
|
+
const where = site ? formatLocation(site) : "";
|
|
362
|
+
const name = owner ?? element.tagName.toLowerCase();
|
|
363
|
+
return where ? `${name} · ${where}` : name;
|
|
364
|
+
}
|
|
365
|
+
export function summarizeTarget(target) {
|
|
366
|
+
if (!target) {
|
|
367
|
+
return "Source unavailable";
|
|
368
|
+
}
|
|
369
|
+
const location = target.source ? formatLocation(target.source) : "";
|
|
370
|
+
const path = target.componentPath.join(" › ");
|
|
371
|
+
if (!location) {
|
|
372
|
+
return path
|
|
373
|
+
? `${path} · source unavailable`
|
|
374
|
+
: `${target.element.selector} · source unavailable`;
|
|
375
|
+
}
|
|
376
|
+
return path ? `${location} · ${path}` : location;
|
|
377
|
+
}
|
|
378
|
+
function createView(doc) {
|
|
379
|
+
const make = (tag, options = {}, ...children) => {
|
|
380
|
+
const node = doc.createElement(tag);
|
|
381
|
+
if (options.className) {
|
|
382
|
+
node.className = options.className;
|
|
383
|
+
}
|
|
384
|
+
if (options.text !== undefined) {
|
|
385
|
+
node.textContent = options.text;
|
|
386
|
+
}
|
|
387
|
+
for (const [name, value] of Object.entries(options.attrs ?? {})) {
|
|
388
|
+
node.setAttribute(name, value);
|
|
389
|
+
}
|
|
390
|
+
for (const [type, listener] of Object.entries(options.on ?? {})) {
|
|
391
|
+
node.addEventListener(type, listener);
|
|
392
|
+
}
|
|
393
|
+
for (const child of children) {
|
|
394
|
+
if (child) {
|
|
395
|
+
node.append(child);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return node;
|
|
399
|
+
};
|
|
400
|
+
const root = make("div", { attrs: { "aria-live": "polite" } });
|
|
401
|
+
const boxes = make("div");
|
|
402
|
+
const pins = make("div");
|
|
403
|
+
const preview = make("div");
|
|
404
|
+
const popover = make("div");
|
|
405
|
+
const panel = make("div");
|
|
406
|
+
const toastRegion = make("div");
|
|
407
|
+
const launcher = make("button", {
|
|
408
|
+
className: "launcher",
|
|
409
|
+
attrs: { type: "button", "data-annotator": "launcher" },
|
|
410
|
+
});
|
|
411
|
+
root.append(boxes, pins, preview, popover, panel, launcher, toastRegion);
|
|
412
|
+
let dispatchRef = () => {
|
|
413
|
+
throw new OverlayNotReadyError();
|
|
414
|
+
};
|
|
415
|
+
launcher.addEventListener("click", () => dispatchRef({ type: "Toggled" }));
|
|
416
|
+
const pinNodes = new Map();
|
|
417
|
+
let previewKey = null;
|
|
418
|
+
let panelKey = [];
|
|
419
|
+
let draftCache;
|
|
420
|
+
let toastTimer;
|
|
421
|
+
const renderBoxes = (state, context) => {
|
|
422
|
+
const nodes = [];
|
|
423
|
+
if (state.status === "drafting") {
|
|
424
|
+
for (const entry of state.draft.targets) {
|
|
425
|
+
if (entry.element.isConnected) {
|
|
426
|
+
nodes.push(box(context.rectOf(entry.element, entry.frame), "selected"));
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
if ("hovered" in state && state.hovered?.element.isConnected) {
|
|
431
|
+
const hovered = state.hovered;
|
|
432
|
+
const rect = context.rectOf(hovered.element, hovered.frame);
|
|
433
|
+
const node = box(rect, "hover");
|
|
434
|
+
node.append(make("span", {
|
|
435
|
+
className: "box-label",
|
|
436
|
+
text: context.labelOf(hovered.element),
|
|
437
|
+
attrs: rect.top < 22 ? { "data-below": "" } : {},
|
|
438
|
+
}));
|
|
439
|
+
nodes.push(node);
|
|
440
|
+
}
|
|
441
|
+
boxes.replaceChildren(...nodes);
|
|
442
|
+
};
|
|
443
|
+
const box = (rect, kind) => make("div", {
|
|
444
|
+
className: "box",
|
|
445
|
+
attrs: {
|
|
446
|
+
"data-kind": kind,
|
|
447
|
+
style: `top:${rect.top}px;left:${rect.left}px;width:${rect.width}px;height:${rect.height}px`,
|
|
448
|
+
},
|
|
449
|
+
});
|
|
450
|
+
const renderPins = (state, context) => {
|
|
451
|
+
const seen = new Set();
|
|
452
|
+
if (isSessionActive(state)) {
|
|
453
|
+
state.annotations.forEach((annotation, index) => {
|
|
454
|
+
annotation.targets.forEach((entry, targetIndex) => {
|
|
455
|
+
if (!entry.element.isConnected) {
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const key = `${annotation.id}:${targetIndex}`;
|
|
459
|
+
seen.add(key);
|
|
460
|
+
const rect = context.rectOf(entry.element, entry.frame);
|
|
461
|
+
const pin = pinNodes.get(key) ?? createPin(annotation.id);
|
|
462
|
+
pinNodes.set(key, pin);
|
|
463
|
+
pin.textContent = String(index + 1);
|
|
464
|
+
pin.title = annotation.note;
|
|
465
|
+
pin.setAttribute("aria-label", `Show annotation ${index + 1}`);
|
|
466
|
+
pin.setAttribute("aria-expanded", String(state.status === "picking" && state.previewId === annotation.id));
|
|
467
|
+
pin.style.top = `${Math.max(8, rect.top - 10)}px`;
|
|
468
|
+
pin.style.left = `${Math.max(8, rect.left - 10)}px`;
|
|
469
|
+
if (pin.parentNode !== pins) {
|
|
470
|
+
pins.append(pin);
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
for (const [key, pin] of pinNodes) {
|
|
476
|
+
if (!seen.has(key)) {
|
|
477
|
+
pin.remove();
|
|
478
|
+
pinNodes.delete(key);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
const createPin = (annotationId) => {
|
|
483
|
+
const show = () => dispatchRef({ type: "PreviewedAnnotation", id: annotationId });
|
|
484
|
+
return make("button", {
|
|
485
|
+
className: "pin",
|
|
486
|
+
attrs: {
|
|
487
|
+
type: "button",
|
|
488
|
+
"aria-haspopup": "dialog",
|
|
489
|
+
"data-annotator": "pin",
|
|
490
|
+
},
|
|
491
|
+
on: { click: show, focus: show, mouseover: show },
|
|
492
|
+
});
|
|
493
|
+
};
|
|
494
|
+
const renderPreview = (state, context) => {
|
|
495
|
+
const annotation = state.status === "picking" && state.previewId !== null
|
|
496
|
+
? state.annotations.find((entry) => entry.id === state.previewId)
|
|
497
|
+
: undefined;
|
|
498
|
+
if (!annotation || state.status !== "picking") {
|
|
499
|
+
previewKey = null;
|
|
500
|
+
preview.replaceChildren();
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
const index = state.annotations.indexOf(annotation) + 1;
|
|
504
|
+
const first = annotation.targets[0];
|
|
505
|
+
const rect = first
|
|
506
|
+
? context.rectOf(first.element, first.frame)
|
|
507
|
+
: { top: 8, left: 8, width: 0, height: 0 };
|
|
508
|
+
const top = Math.max(8, Math.min(context.viewport.height - 120, rect.top + rect.height + 8));
|
|
509
|
+
const left = Math.min(context.viewport.width - 268, Math.max(8, rect.left));
|
|
510
|
+
const position = `top:${top}px;left:${left}px`;
|
|
511
|
+
if (previewKey === annotation && preview.firstElementChild) {
|
|
512
|
+
preview.firstElementChild.setAttribute("style", position);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
previewKey = annotation;
|
|
516
|
+
const iconButton = (label, glyph, className, event) => make("button", {
|
|
517
|
+
className: `${className} icon`,
|
|
518
|
+
text: glyph,
|
|
519
|
+
attrs: { type: "button", "aria-label": label, title: label },
|
|
520
|
+
on: { click: () => dispatchRef(event) },
|
|
521
|
+
});
|
|
522
|
+
preview.replaceChildren(make("dialog", {
|
|
523
|
+
className: "preview",
|
|
524
|
+
attrs: {
|
|
525
|
+
open: "",
|
|
526
|
+
"aria-label": `Annotation ${index}`,
|
|
527
|
+
style: position,
|
|
528
|
+
"data-annotator": "preview",
|
|
529
|
+
},
|
|
530
|
+
}, make("div", { className: "preview-header" }, make("div", {
|
|
531
|
+
className: "preview-title",
|
|
532
|
+
text: `Annotation ${index}`,
|
|
533
|
+
}), make("div", { className: "preview-actions" }, iconButton(`Close annotation ${index}`, "×", "secondary", {
|
|
534
|
+
type: "ClosedPreview",
|
|
535
|
+
}), iconButton(`Edit annotation ${index}`, "✎", "secondary", {
|
|
536
|
+
type: "EditedAnnotation",
|
|
537
|
+
id: annotation.id,
|
|
538
|
+
}), iconButton(`Delete annotation ${index}`, "🗑", "danger", {
|
|
539
|
+
type: "DeletedAnnotation",
|
|
540
|
+
id: annotation.id,
|
|
541
|
+
}))), make("div", { className: "note", text: annotation.note }), make("div", {
|
|
542
|
+
className: "meta",
|
|
543
|
+
text: summarizeAnnotation(annotation),
|
|
544
|
+
})));
|
|
545
|
+
};
|
|
546
|
+
const renderPopover = (state, context) => {
|
|
547
|
+
if (state.status !== "drafting") {
|
|
548
|
+
draftCache = undefined;
|
|
549
|
+
popover.replaceChildren();
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
const { draft } = state;
|
|
553
|
+
const last = draft.targets[draft.targets.length - 1];
|
|
554
|
+
const rect = last
|
|
555
|
+
? context.rectOf(last.element, last.frame)
|
|
556
|
+
: { top: 8, left: 8, width: 0, height: 0 };
|
|
557
|
+
const top = Math.max(8, Math.min(context.viewport.height - 260, rect.top + rect.height + 8));
|
|
558
|
+
const left = Math.min(context.viewport.width - 340, Math.max(8, rect.left));
|
|
559
|
+
const position = `top:${top}px;left:${left}px`;
|
|
560
|
+
if (draftCache?.targets === draft.targets &&
|
|
561
|
+
draftCache.editingId === draft.editingId) {
|
|
562
|
+
draftCache.dialog.setAttribute("style", position);
|
|
563
|
+
if (draftCache.textarea.value !== draft.note) {
|
|
564
|
+
draftCache.textarea.value = draft.note;
|
|
565
|
+
}
|
|
566
|
+
draftCache.notice.textContent = state.notice ?? "";
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
const textarea = make("textarea", {
|
|
570
|
+
attrs: {
|
|
571
|
+
rows: "4",
|
|
572
|
+
placeholder: "What should change here?",
|
|
573
|
+
"aria-label": "Annotation note",
|
|
574
|
+
},
|
|
575
|
+
on: {
|
|
576
|
+
input: (event) => dispatchRef({
|
|
577
|
+
type: "EditedNote",
|
|
578
|
+
note: event.target.value,
|
|
579
|
+
}),
|
|
580
|
+
},
|
|
581
|
+
});
|
|
582
|
+
textarea.value = draft.note;
|
|
583
|
+
const notice = make("div", {
|
|
584
|
+
className: "notice",
|
|
585
|
+
text: state.notice ?? "",
|
|
586
|
+
});
|
|
587
|
+
const dialog = make("dialog", {
|
|
588
|
+
className: "popover",
|
|
589
|
+
attrs: {
|
|
590
|
+
open: "",
|
|
591
|
+
"aria-label": "Add source annotation",
|
|
592
|
+
style: position,
|
|
593
|
+
"data-annotator": "draft",
|
|
594
|
+
},
|
|
595
|
+
}, make("div", { className: "popover-title", text: "Annotation" }), make("div", {
|
|
596
|
+
className: "meta",
|
|
597
|
+
text: summarizeDraft(draft.targets),
|
|
598
|
+
}), textarea, make("div", { className: "actions" }, make("button", {
|
|
599
|
+
className: "secondary",
|
|
600
|
+
text: "Cancel",
|
|
601
|
+
attrs: { type: "button" },
|
|
602
|
+
on: { click: () => dispatchRef({ type: "CancelledDraft" }) },
|
|
603
|
+
}), make("button", {
|
|
604
|
+
className: "primary",
|
|
605
|
+
text: draft.editingId ? "Update note" : "Save note",
|
|
606
|
+
attrs: { type: "button", "data-annotator": "save" },
|
|
607
|
+
on: {
|
|
608
|
+
click: () => dispatchRef({ type: "SavedDraft", id: createAnnotationId() }),
|
|
609
|
+
},
|
|
610
|
+
})), notice);
|
|
611
|
+
popover.replaceChildren(dialog);
|
|
612
|
+
draftCache = {
|
|
613
|
+
targets: draft.targets,
|
|
614
|
+
editingId: draft.editingId,
|
|
615
|
+
dialog,
|
|
616
|
+
textarea,
|
|
617
|
+
notice,
|
|
618
|
+
};
|
|
619
|
+
textarea.focus();
|
|
620
|
+
};
|
|
621
|
+
const renderPanel = (state) => {
|
|
622
|
+
const key = isSessionActive(state)
|
|
623
|
+
? [state.status, state.annotations, state.notice]
|
|
624
|
+
: [state.status];
|
|
625
|
+
if (key.length === panelKey.length &&
|
|
626
|
+
key.every((part, i) => part === panelKey[i])) {
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
panelKey = key;
|
|
630
|
+
if (!isSessionActive(state)) {
|
|
631
|
+
panel.replaceChildren();
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
const collecting = state.status === "collecting";
|
|
635
|
+
const drafting = state.status === "drafting";
|
|
636
|
+
const list = state.annotations.length
|
|
637
|
+
? make("ol", { className: "list" }, ...state.annotations.map((annotation, index) => make("li", { className: "item" }, make("div", { className: "item-content" }, make("div", { className: "note", text: annotation.note }), make("div", {
|
|
638
|
+
className: "meta",
|
|
639
|
+
text: summarizeAnnotation(annotation),
|
|
640
|
+
})), make("div", { className: "item-actions" }, make("button", {
|
|
641
|
+
className: "link",
|
|
642
|
+
text: "Link element",
|
|
643
|
+
attrs: { type: "button" },
|
|
644
|
+
on: {
|
|
645
|
+
click: () => dispatchRef({
|
|
646
|
+
type: "StartedLinking",
|
|
647
|
+
id: annotation.id,
|
|
648
|
+
}),
|
|
649
|
+
},
|
|
650
|
+
}), make("button", {
|
|
651
|
+
className: "danger icon",
|
|
652
|
+
text: "🗑",
|
|
653
|
+
attrs: {
|
|
654
|
+
type: "button",
|
|
655
|
+
"aria-label": `Delete annotation ${index + 1}`,
|
|
656
|
+
title: `Delete annotation ${index + 1}`,
|
|
657
|
+
},
|
|
658
|
+
on: {
|
|
659
|
+
click: () => dispatchRef({
|
|
660
|
+
type: "DeletedAnnotation",
|
|
661
|
+
id: annotation.id,
|
|
662
|
+
}),
|
|
663
|
+
},
|
|
664
|
+
})))))
|
|
665
|
+
: make("p", {
|
|
666
|
+
className: "empty",
|
|
667
|
+
text: "Hover an element, click it, then add a note.",
|
|
668
|
+
});
|
|
669
|
+
const collect = make("button", {
|
|
670
|
+
className: "collect",
|
|
671
|
+
text: collecting ? "Copying…" : "Collect",
|
|
672
|
+
attrs: {
|
|
673
|
+
type: "button",
|
|
674
|
+
"data-annotator": "collect",
|
|
675
|
+
...(drafting ? { title: "Save or cancel the open note first." } : {}),
|
|
676
|
+
},
|
|
677
|
+
on: { click: () => dispatchRef({ type: "RequestedCollect" }) },
|
|
678
|
+
});
|
|
679
|
+
collect.disabled = collecting || drafting || state.annotations.length === 0;
|
|
680
|
+
panel.replaceChildren(make("section", {
|
|
681
|
+
className: "panel",
|
|
682
|
+
attrs: {
|
|
683
|
+
"aria-label": "Collected annotations",
|
|
684
|
+
"data-annotator": "panel",
|
|
685
|
+
},
|
|
686
|
+
}, make("div", { className: "panel-header" }, make("strong", { text: "Annotations" }), make("span", {
|
|
687
|
+
className: "badge",
|
|
688
|
+
text: String(state.annotations.length),
|
|
689
|
+
})), state.status === "linking" &&
|
|
690
|
+
make("p", {
|
|
691
|
+
className: "mode",
|
|
692
|
+
text: "Click another element to link it to this annotation.",
|
|
693
|
+
}), list, make("div", { className: "footer" }, make("button", {
|
|
694
|
+
className: "cancel",
|
|
695
|
+
text: "Cancel",
|
|
696
|
+
attrs: { type: "button" },
|
|
697
|
+
on: { click: () => dispatchRef({ type: "Cancelled" }) },
|
|
698
|
+
}), collect), state.notice !== null &&
|
|
699
|
+
make("div", { className: "notice", text: state.notice })));
|
|
700
|
+
};
|
|
701
|
+
return {
|
|
702
|
+
root,
|
|
703
|
+
render: (state, settings, context) => {
|
|
704
|
+
dispatchRef = context.dispatch;
|
|
705
|
+
const active = isSessionActive(state);
|
|
706
|
+
launcher.textContent = active ? "Annotating" : "Annotate";
|
|
707
|
+
launcher.setAttribute("aria-pressed", String(active));
|
|
708
|
+
launcher.title = `Toggle annotator (${settings.hotkey})`;
|
|
709
|
+
renderBoxes(state, context);
|
|
710
|
+
renderPins(state, context);
|
|
711
|
+
renderPreview(state, context);
|
|
712
|
+
renderPopover(state, context);
|
|
713
|
+
renderPanel(state);
|
|
714
|
+
},
|
|
715
|
+
toast: (tone, title, description, durationMs) => {
|
|
716
|
+
clearTimeout(toastTimer);
|
|
717
|
+
toastRegion.replaceChildren(make("div", {
|
|
718
|
+
className: "toast",
|
|
719
|
+
attrs: {
|
|
720
|
+
role: "status",
|
|
721
|
+
"data-tone": tone,
|
|
722
|
+
"data-annotator": "toast",
|
|
723
|
+
},
|
|
724
|
+
}, make("div", { className: "toast-title", text: title }), make("div", { className: "toast-description", text: description })));
|
|
725
|
+
toastTimer = setTimeout(() => toastRegion.replaceChildren(), durationMs);
|
|
726
|
+
},
|
|
727
|
+
dispose: () => clearTimeout(toastTimer),
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
function summarizeAnnotation(annotation) {
|
|
731
|
+
const first = summarizeTarget(annotation.targets[0]?.data);
|
|
732
|
+
return annotation.targets.length > 1
|
|
733
|
+
? `${first} · ${annotation.targets.length} linked elements`
|
|
734
|
+
: first;
|
|
735
|
+
}
|
|
736
|
+
function summarizeDraft(targets) {
|
|
737
|
+
if (targets.length === 1) {
|
|
738
|
+
return summarizeTarget(targets[0]?.data);
|
|
739
|
+
}
|
|
740
|
+
return `${targets.length} elements selected`;
|
|
741
|
+
}
|
|
742
|
+
export class OverlayNotReadyError extends Error {
|
|
743
|
+
name = "OverlayNotReadyError";
|
|
744
|
+
constructor() {
|
|
745
|
+
super("The overlay received an event before its first render.");
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
//# sourceMappingURL=overlay.js.map
|