The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics globalDate:off globalTimers:off - This isolated Electron preload does not run inside an Effect runtime. |
| 2 | import { ipcRenderer } from "electron"; |
| 3 | import { getElementContext } from "react-grab/primitives"; |
| 4 | import { matchesKeybindingShortcut } from "@t3tools/shared/keybindingMatching"; |
| 5 | import type { |
| 6 | DesktopPreviewAnnotationTheme, |
| 7 | DesktopPreviewAnnotationVoiceConfig, |
| 8 | DesktopPreviewAnnotationVoiceEvent, |
| 9 | DesktopPreviewAnnotationVoiceState, |
| 10 | PickedElementPayload, |
| 11 | PickedElementStackFrame, |
| 12 | PreviewAnnotationPayload, |
| 13 | PreviewAnnotationPoint, |
| 14 | PreviewAnnotationRect, |
| 15 | PreviewAnnotationRegionTarget, |
| 16 | PreviewAnnotationStrokeTarget, |
| 17 | PreviewAnnotationStyleChange, |
| 18 | PreviewAnnotationSubmission, |
| 19 | } from "@t3tools/contracts"; |
| 20 | |
| 21 | import { createAnnotationIdSource } from "./AnnotationIds.ts"; |
| 22 | import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts"; |
| 23 | import { |
| 24 | addStrokePoint, |
| 25 | openStrokePath, |
| 26 | startStroke, |
| 27 | strokeBounds, |
| 28 | type StrokeGeometry, |
| 29 | } from "./AnnotationStroke.ts"; |
| 30 | import { previewAnnotationStyles } from "./AnnotationStyles.generated.ts"; |
| 31 | import { installRecordingCursor } from "./RecordingCursor.ts"; |
| 32 | import { DEFAULT_RECORDING_INPUT_OPTIONS } from "./RecordingInput.ts"; |
| 33 | import { |
| 34 | ANNOTATION_CAPTURED_CHANNEL, |
| 35 | ANNOTATION_SEND_ENABLED_CHANNEL, |
| 36 | ANNOTATION_THEME_CHANNEL, |
| 37 | ANNOTATION_VOICE_EVENT_CHANNEL, |
| 38 | ANNOTATION_VOICE_STATE_CHANNEL, |
| 39 | CANCEL_PICK_CHANNEL, |
| 40 | ELEMENT_PICKED_CHANNEL, |
| 41 | HUMAN_INPUT_CHANNEL, |
| 42 | POINTER_DOWN_HOST_CHANNEL, |
| 43 | PAGE_UNHANDLED_SHORTCUT_CHANNEL, |
| 44 | RECORDING_CURSOR_CHANNEL, |
| 45 | RECORDING_POINTER_CHANNEL, |
| 46 | RECORDING_KEY_CHANNEL, |
| 47 | RECORDING_INPUT_CHANNEL, |
| 48 | RECORDING_CONTROLLER_CHANNEL, |
| 49 | START_PICK_CHANNEL, |
| 50 | THUMB_PRESS_CHANNEL, |
| 51 | } from "./GuestProtocol.ts"; |
| 52 | const OVERLAY_ATTRIBUTE = "data-t3code-annotation-ui"; |
| 53 | const INPUT_LAYER_ATTRIBUTE = "data-t3code-annotation-input"; |
| 54 | const Z_INDEX_OVERLAY = 2147483646; |
| 55 | const PRIMARY = "var(--t3-primary)"; |
| 56 | const PRIMARY_FILL = "color-mix(in srgb, var(--t3-primary) 10%, transparent)"; |
| 57 | const MAX_MARQUEE_ELEMENTS = 20; |
| 58 | const ELEMENT_CONTEXT_TIMEOUT_MS = 5_000; |
| 59 | const CONTENT_LAYER_Z_INDEX = 1; |
| 60 | const CHROME_LAYER_Z_INDEX = 10; |
| 61 | // Page dialogs (Radix, react-remove-scroll) dismiss, refocus or block scrolling on document events |
| 62 | // that shadow retargeting attributes to the overlay host. |
| 63 | const PAGE_ISOLATED_EVENTS = [ |
| 64 | "pointerdown", |
| 65 | "focusin", |
| 66 | "focusout", |
| 67 | "wheel", |
| 68 | "touchstart", |
| 69 | "touchmove", |
| 70 | ] as const; |
| 71 | |
| 72 | let recordingCursor: ReturnType<typeof installRecordingCursor> | null = null; |
| 73 | ipcRenderer.on( |
| 74 | RECORDING_CURSOR_CHANNEL, |
| 75 | (_event, active: unknown, inputOptions: unknown, controller: unknown) => { |
| 76 | if (active === true) { |
| 77 | const options = |
| 78 | typeof inputOptions === "object" && inputOptions !== null |
| 79 | ? { |
| 80 | showKeyPresses: |
| 81 | "showKeyPresses" in inputOptions && inputOptions.showKeyPresses === true, |
| 82 | showMousePresses: |
| 83 | "showMousePresses" in inputOptions && inputOptions.showMousePresses === true, |
| 84 | } |
| 85 | : DEFAULT_RECORDING_INPUT_OPTIONS; |
| 86 | recordingCursor ??= installRecordingCursor(document, window, options, (input) => |
| 87 | ipcRenderer.send(RECORDING_INPUT_CHANNEL, input), |
| 88 | ); |
| 89 | recordingCursor.setTheme(annotationTheme); |
| 90 | if (controller === "agent" || controller === "human" || controller === "none") |
| 91 | recordingCursor.setController(controller); |
| 92 | } else { |
| 93 | recordingCursor?.dispose(); |
| 94 | recordingCursor = null; |
| 95 | } |
| 96 | }, |
| 97 | ); |
| 98 | ipcRenderer.on(RECORDING_CONTROLLER_CHANNEL, (_event, controller: unknown, point: unknown) => { |
| 99 | const humanPoint = |
| 100 | typeof point === "object" && |
| 101 | point !== null && |
| 102 | "x" in point && |
| 103 | typeof point.x === "number" && |
| 104 | Number.isFinite(point.x) && |
| 105 | "y" in point && |
| 106 | typeof point.y === "number" && |
| 107 | Number.isFinite(point.y) |
| 108 | ? { x: point.x, y: point.y } |
| 109 | : undefined; |
| 110 | if (controller === "agent" || controller === "human" || controller === "none") |
| 111 | recordingCursor?.setController(controller, humanPoint); |
| 112 | }); |
| 113 | ipcRenderer.on(RECORDING_KEY_CHANNEL, (_event, input: unknown) => { |
| 114 | if ( |
| 115 | typeof input !== "object" || |
| 116 | input === null || |
| 117 | !("key" in input) || |
| 118 | typeof input.key !== "string" |
| 119 | ) |
| 120 | return; |
| 121 | recordingCursor?.keyPress({ |
| 122 | key: input.key, |
| 123 | metaKey: "metaKey" in input && input.metaKey === true, |
| 124 | ctrlKey: "ctrlKey" in input && input.ctrlKey === true, |
| 125 | altKey: "altKey" in input && input.altKey === true, |
| 126 | shiftKey: "shiftKey" in input && input.shiftKey === true, |
| 127 | }); |
| 128 | }); |
| 129 | ipcRenderer.on(RECORDING_POINTER_CHANNEL, (_event, point: unknown) => { |
| 130 | if ( |
| 131 | typeof point === "object" && |
| 132 | point !== null && |
| 133 | "x" in point && |
| 134 | typeof point.x === "number" && |
| 135 | Number.isFinite(point.x) && |
| 136 | "y" in point && |
| 137 | typeof point.y === "number" && |
| 138 | Number.isFinite(point.y) |
| 139 | ) |
| 140 | recordingCursor?.move( |
| 141 | { x: point.x, y: point.y }, |
| 142 | "phase" in point && point.phase === "click" ? "click" : "move", |
| 143 | ); |
| 144 | }); |
| 145 | |
| 146 | type AnnotationTool = "select" | "marquee" | "draw" | "erase"; |
| 147 | |
| 148 | interface SelectedElement { |
| 149 | id: string; |
| 150 | element: Element; |
| 151 | outline: HTMLDivElement; |
| 152 | label: HTMLDivElement; |
| 153 | baselineStyles: Map<string, string>; |
| 154 | // Last rect measured while connected; a detached node reports 0x0. |
| 155 | rect: PreviewAnnotationRect; |
| 156 | } |
| 157 | |
| 158 | interface AnnotationSession { |
| 159 | teardown: (notifyMain: boolean) => void; |
| 160 | applyTheme: (theme: DesktopPreviewAnnotationTheme) => void; |
| 161 | setSendEnabled: (enabled: boolean) => void; |
| 162 | handleOverlayKey: (event: KeyboardEvent) => void; |
| 163 | } |
| 164 | |
| 165 | let activeSession: AnnotationSession | null = null; |
| 166 | let annotationTheme: DesktopPreviewAnnotationTheme | null = null; |
| 167 | |
| 168 | const applyAnnotationTheme = ( |
| 169 | host: HTMLElement, |
| 170 | theme: DesktopPreviewAnnotationTheme | null, |
| 171 | ): void => { |
| 172 | if (!theme) return; |
| 173 | host.style.colorScheme = theme.colorScheme; |
| 174 | const variables = { |
| 175 | "--t3-radius": theme.radius, |
| 176 | "--t3-background": theme.background, |
| 177 | "--t3-foreground": theme.foreground, |
| 178 | "--t3-popover": theme.popover, |
| 179 | "--t3-popover-foreground": theme.popoverForeground, |
| 180 | "--t3-primary": theme.primary, |
| 181 | "--t3-primary-foreground": theme.primaryForeground, |
| 182 | "--t3-muted": theme.muted, |
| 183 | "--t3-muted-foreground": theme.mutedForeground, |
| 184 | "--t3-accent": theme.accent, |
| 185 | "--t3-accent-foreground": theme.accentForeground, |
| 186 | "--t3-border": theme.border, |
| 187 | "--t3-input": theme.input, |
| 188 | "--t3-ring": theme.ring, |
| 189 | "--t3-font-sans": theme.fontSans, |
| 190 | "--t3-font-mono": theme.fontMono, |
| 191 | }; |
| 192 | for (const [name, value] of Object.entries(variables)) { |
| 193 | host.style.setProperty(name, value); |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | const reportHumanPointerInput = (event: PointerEvent): void => { |
| 198 | if (!event.isTrusted) return; |
| 199 | // The guest does not always take focus on a click, so the host's webview |
| 200 | // `focus` event can't be relied on to dismiss open menus and popovers. |
| 201 | ipcRenderer.sendToHost(POINTER_DOWN_HOST_CHANNEL); |
| 202 | ipcRenderer.send(HUMAN_INPUT_CHANNEL, { |
| 203 | kind: "pointer", |
| 204 | x: event.clientX, |
| 205 | y: event.clientY, |
| 206 | button: event.button, |
| 207 | }); |
| 208 | }; |
| 209 | |
| 210 | const reportHumanKeyInput = (event: KeyboardEvent): void => { |
| 211 | if (!event.isTrusted) return; |
| 212 | ipcRenderer.send(HUMAN_INPUT_CHANNEL, { |
| 213 | kind: "key", |
| 214 | key: event.key, |
| 215 | code: event.code, |
| 216 | }); |
| 217 | }; |
| 218 | |
| 219 | window.addEventListener("pointerdown", reportHumanPointerInput, true); |
| 220 | window.addEventListener("keydown", reportHumanKeyInput, true); |
| 221 | |
| 222 | // Capture runs before a page can stop the event; the task delay lets every page |
| 223 | // listener call preventDefault before the host is told the key went unhandled. |
| 224 | const reportUnhandledShortcut = (event: KeyboardEvent): void => { |
| 225 | if (!event.isTrusted || event.repeat || event.isComposing) return; |
| 226 | if (!event.metaKey && !event.ctrlKey && !event.altKey && !/^F\d{1,2}$/u.test(event.key)) return; |
| 227 | setTimeout(() => { |
| 228 | if (event.defaultPrevented) return; |
| 229 | ipcRenderer.send(PAGE_UNHANDLED_SHORTCUT_CHANNEL, { |
| 230 | key: event.key, |
| 231 | code: event.code, |
| 232 | metaKey: event.metaKey, |
| 233 | ctrlKey: event.ctrlKey, |
| 234 | shiftKey: event.shiftKey, |
| 235 | altKey: event.altKey, |
| 236 | }); |
| 237 | }, 0); |
| 238 | }; |
| 239 | |
| 240 | window.addEventListener("keydown", reportUnhandledShortcut, true); |
| 241 | |
| 242 | // Registered before page scripts so their capture listeners never see keys typed into the |
| 243 | // overlay; the closed shadow root retargets those to the host, hiding that they edit text. |
| 244 | const isolateOverlayKey = (event: KeyboardEvent): void => { |
| 245 | if (!activeSession || !(event.target instanceof Element) || !isAnnotationNode(event.target)) |
| 246 | return; |
| 247 | activeSession.handleOverlayKey(event); |
| 248 | event.stopImmediatePropagation(); |
| 249 | }; |
| 250 | |
| 251 | for (const type of ["keydown", "keypress", "keyup"] as const) |
| 252 | window.addEventListener(type, isolateOverlayKey, true); |
| 253 | |
| 254 | let replayedThumbPress: { readonly x: number; readonly y: number } | null = null; |
| 255 | ipcRenderer.on( |
| 256 | THUMB_PRESS_CHANNEL, |
| 257 | (_event, phase: unknown, x: unknown, y: unknown, button: unknown) => { |
| 258 | if (phase !== "down" && phase !== "up") return; |
| 259 | if (typeof x !== "number" || typeof y !== "number" || typeof button !== "number") return; |
| 260 | // Main reports presses inside a frame in that frame's coordinates. |
| 261 | const inFrame = document.querySelector("iframe:hover, frame:hover, object:hover, embed:hover"); |
| 262 | if (phase === "up") { |
| 263 | const pressed = replayedThumbPress; |
| 264 | replayedThumbPress = null; |
| 265 | // A replayed press is always released, at its own point if the release lands in a frame. |
| 266 | if (pressed) recordingCursor?.press(inFrame ? pressed : { x, y }, "up"); |
| 267 | return; |
| 268 | } |
| 269 | if (inFrame) return; |
| 270 | replayedThumbPress = { x, y }; |
| 271 | ipcRenderer.sendToHost(POINTER_DOWN_HOST_CHANNEL); |
| 272 | ipcRenderer.send(HUMAN_INPUT_CHANNEL, { kind: "pointer", x, y, button }); |
| 273 | recordingCursor?.press({ x, y }, "down"); |
| 274 | }, |
| 275 | ); |
| 276 | |
| 277 | const nextId = createAnnotationIdSource(); |
| 278 | |
| 279 | const rectFromDomRect = (rect: DOMRect): PreviewAnnotationRect => ({ |
| 280 | x: rect.left, |
| 281 | y: rect.top, |
| 282 | width: rect.width, |
| 283 | height: rect.height, |
| 284 | }); |
| 285 | |
| 286 | const normalizeRect = ( |
| 287 | startX: number, |
| 288 | startY: number, |
| 289 | endX: number, |
| 290 | endY: number, |
| 291 | ): PreviewAnnotationRect => ({ |
| 292 | x: Math.min(startX, endX), |
| 293 | y: Math.min(startY, endY), |
| 294 | width: Math.abs(endX - startX), |
| 295 | height: Math.abs(endY - startY), |
| 296 | }); |
| 297 | |
| 298 | const isUsableRect = (rect: PreviewAnnotationRect): boolean => rect.width >= 3 && rect.height >= 3; |
| 299 | |
| 300 | function unionRects( |
| 301 | rects: ReadonlyArray<PreviewAnnotationRect>, |
| 302 | padding = 20, |
| 303 | ): PreviewAnnotationRect | null { |
| 304 | if (rects.length === 0) return null; |
| 305 | const left = Math.min(...rects.map((rect) => rect.x)); |
| 306 | const top = Math.min(...rects.map((rect) => rect.y)); |
| 307 | const right = Math.max(...rects.map((rect) => rect.x + rect.width)); |
| 308 | const bottom = Math.max(...rects.map((rect) => rect.y + rect.height)); |
| 309 | // Main clips to the captured viewport; clipping here would lose offscreen bounds. |
| 310 | return { |
| 311 | x: left - padding, |
| 312 | y: top - padding, |
| 313 | width: right - left + padding * 2, |
| 314 | height: bottom - top + padding * 2, |
| 315 | }; |
| 316 | } |
| 317 | |
| 318 | function isAnnotationNode(element: Element): boolean { |
| 319 | return element instanceof Element && element.closest(`[${OVERLAY_ATTRIBUTE}]`) !== null; |
| 320 | } |
| 321 | |
| 322 | function pickFromPoint(clientX: number, clientY: number): Element | null { |
| 323 | for (const candidate of document.elementsFromPoint(clientX, clientY)) { |
| 324 | if (!(candidate instanceof Element)) continue; |
| 325 | if (isAnnotationNode(candidate) || candidate.hasAttribute(INPUT_LAYER_ATTRIBUTE)) continue; |
| 326 | if (candidate === document.documentElement || candidate === document.body) continue; |
| 327 | return candidate; |
| 328 | } |
| 329 | return null; |
| 330 | } |
| 331 | |
| 332 | function describeRawElement(element: Element): string { |
| 333 | const tag = element.tagName.toLowerCase(); |
| 334 | const id = element.id ? `#${element.id}` : ""; |
| 335 | const classes = |
| 336 | element instanceof HTMLElement && typeof element.className === "string" |
| 337 | ? element.className |
| 338 | .trim() |
| 339 | .split(/\s+/) |
| 340 | .filter(Boolean) |
| 341 | .slice(0, 2) |
| 342 | .map((name) => `.${name}`) |
| 343 | .join("") |
| 344 | : ""; |
| 345 | return `${tag}${id}${classes}`; |
| 346 | } |
| 347 | |
| 348 | function createBox(color: string, fill: string): HTMLDivElement { |
| 349 | const node = document.createElement("div"); |
| 350 | node.setAttribute(OVERLAY_ATTRIBUTE, ""); |
| 351 | node.style.cssText = [ |
| 352 | "position:fixed", |
| 353 | "pointer-events:none", |
| 354 | `border:2px solid ${color}`, |
| 355 | `background:${fill}`, |
| 356 | "border-radius:3px", |
| 357 | "box-sizing:border-box", |
| 358 | "display:none", |
| 359 | `z-index:${CONTENT_LAYER_Z_INDEX}`, |
| 360 | ].join(";"); |
| 361 | return node; |
| 362 | } |
| 363 | |
| 364 | function positionBox(node: HTMLElement, rect: PreviewAnnotationRect): void { |
| 365 | if (rect.width <= 0 || rect.height <= 0) { |
| 366 | node.style.display = "none"; |
| 367 | return; |
| 368 | } |
| 369 | node.style.display = "block"; |
| 370 | node.style.transform = `translate(${rect.x}px, ${rect.y}px)`; |
| 371 | node.style.width = `${rect.width}px`; |
| 372 | node.style.height = `${rect.height}px`; |
| 373 | } |
| 374 | |
| 375 | function visibleElementRect(element: Element): PreviewAnnotationRect { |
| 376 | const rect = element.getBoundingClientRect(); |
| 377 | let left = Math.max(0, rect.left); |
| 378 | let top = Math.max(0, rect.top); |
| 379 | let right = Math.min(document.documentElement.clientWidth, rect.right); |
| 380 | let bottom = Math.min(document.documentElement.clientHeight, rect.bottom); |
| 381 | if (getComputedStyle(element).position !== "fixed") { |
| 382 | for (let ancestor = element.parentElement; ancestor; ancestor = ancestor.parentElement) { |
| 383 | if (ancestor === document.body || ancestor === document.documentElement) break; |
| 384 | const style = getComputedStyle(ancestor); |
| 385 | const bounds = ancestor.getBoundingClientRect(); |
| 386 | const clipLeft = bounds.left + ancestor.clientLeft; |
| 387 | const clipTop = bounds.top + ancestor.clientTop; |
| 388 | if (style.overflowX !== "visible") { |
| 389 | left = Math.max(left, clipLeft); |
| 390 | right = Math.min(right, clipLeft + ancestor.clientWidth); |
| 391 | } |
| 392 | if (style.overflowY !== "visible") { |
| 393 | top = Math.max(top, clipTop); |
| 394 | bottom = Math.min(bottom, clipTop + ancestor.clientHeight); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | return { x: left, y: top, width: Math.max(0, right - left), height: Math.max(0, bottom - top) }; |
| 399 | } |
| 400 | |
| 401 | function createLabel(): HTMLDivElement { |
| 402 | const label = document.createElement("div"); |
| 403 | label.setAttribute(OVERLAY_ATTRIBUTE, ""); |
| 404 | label.className = |
| 405 | "fixed z-1 max-w-70 overflow-hidden rounded-md bg-primary px-2 py-1 font-sans text-xs font-semibold text-primary-foreground shadow-md"; |
| 406 | label.style.cssText = [ |
| 407 | "position:fixed", |
| 408 | "pointer-events:none", |
| 409 | "white-space:nowrap", |
| 410 | "text-overflow:ellipsis", |
| 411 | `z-index:${CONTENT_LAYER_Z_INDEX}`, |
| 412 | ].join(";"); |
| 413 | return label; |
| 414 | } |
| 415 | |
| 416 | function updateSelectedVisual(target: SelectedElement): void { |
| 417 | if (!target.element.isConnected) { |
| 418 | target.outline.style.display = "none"; |
| 419 | target.label.style.display = "none"; |
| 420 | return; |
| 421 | } |
| 422 | target.rect = rectFromDomRect(target.element.getBoundingClientRect()); |
| 423 | const rect = visibleElementRect(target.element); |
| 424 | positionBox(target.outline, rect); |
| 425 | if (rect.width === 0 || rect.height === 0) { |
| 426 | target.label.style.display = "none"; |
| 427 | return; |
| 428 | } |
| 429 | target.label.textContent = describeRawElement(target.element); |
| 430 | target.label.style.display = "block"; |
| 431 | target.label.style.transform = `translate(${Math.max(4, rect.x)}px, ${Math.max(4, rect.y - 22)}px)`; |
| 432 | } |
| 433 | |
| 434 | function toStackFrame(frame: { |
| 435 | functionName?: string; |
| 436 | fileName?: string; |
| 437 | lineNumber?: number; |
| 438 | columnNumber?: number; |
| 439 | }): PickedElementStackFrame { |
| 440 | return { |
| 441 | functionName: frame.functionName ?? null, |
| 442 | fileName: frame.fileName ?? null, |
| 443 | lineNumber: frame.lineNumber ?? null, |
| 444 | columnNumber: frame.columnNumber ?? null, |
| 445 | }; |
| 446 | } |
| 447 | |
| 448 | /** React-internals lookups can remain pending forever; bound them so submit cannot stall the overlay. */ |
| 449 | function withCaptureTimeout<A>(promise: Promise<A>, millis: number): Promise<A | null> { |
| 450 | let timer: ReturnType<typeof setTimeout> | undefined; |
| 451 | return Promise.race([ |
| 452 | promise, |
| 453 | new Promise<null>((resolve) => { |
| 454 | timer = setTimeout(() => resolve(null), millis); |
| 455 | }), |
| 456 | ]).finally(() => clearTimeout(timer)); |
| 457 | } |
| 458 | |
| 459 | const HTML_PREVIEW_MAX_CHARS = 500; |
| 460 | |
| 461 | /** Preserve the tag, HTML, and crop rect when React context lookup fails or stalls. */ |
| 462 | async function captureElement(element: Element): Promise<PickedElementPayload> { |
| 463 | const base = { |
| 464 | pageUrl: location.href, |
| 465 | pageTitle: document.title?.trim() || null, |
| 466 | tagName: element.tagName.toLowerCase(), |
| 467 | pickedAt: new Date().toISOString(), |
| 468 | }; |
| 469 | try { |
| 470 | const context = await withCaptureTimeout( |
| 471 | Promise.resolve(getElementContext(element)), |
| 472 | ELEMENT_CONTEXT_TIMEOUT_MS, |
| 473 | ); |
| 474 | if (context) { |
| 475 | const stack = (context.stack ?? []).map(toStackFrame); |
| 476 | return { |
| 477 | ...base, |
| 478 | selector: context.selector, |
| 479 | htmlPreview: context.htmlPreview ?? "", |
| 480 | componentName: context.componentName, |
| 481 | source: stack[0] ?? null, |
| 482 | stack, |
| 483 | styles: context.styles ?? "", |
| 484 | }; |
| 485 | } |
| 486 | } catch {} |
| 487 | return { |
| 488 | ...base, |
| 489 | selector: null, |
| 490 | htmlPreview: element.outerHTML.slice(0, HTML_PREVIEW_MAX_CHARS), |
| 491 | componentName: null, |
| 492 | source: null, |
| 493 | stack: [], |
| 494 | styles: "", |
| 495 | }; |
| 496 | } |
| 497 | |
| 498 | function createButton(label: string, title: string): HTMLButtonElement { |
| 499 | const button = document.createElement("button"); |
| 500 | button.type = "button"; |
| 501 | button.textContent = label; |
| 502 | button.title = title; |
| 503 | button.className = |
| 504 | "inline-flex h-7 cursor-pointer items-center justify-center rounded-md border border-transparent px-2 font-sans text-xs font-medium text-foreground outline-none hover:bg-accent disabled:pointer-events-none disabled:opacity-60"; |
| 505 | return button; |
| 506 | } |
| 507 | |
| 508 | function styleControl(input: HTMLInputElement | HTMLSelectElement): void { |
| 509 | input.setAttribute("aria-label", input.getAttribute("aria-label") ?? "Style value"); |
| 510 | input.className = |
| 511 | "h-7 min-w-0 w-full appearance-none rounded-md border border-input bg-background px-2 font-mono text-xs text-foreground shadow-xs outline-none"; |
| 512 | } |
| 513 | |
| 514 | function createUnitControl(input: HTMLInputElement): HTMLElement { |
| 515 | const wrapper = document.createElement("div"); |
| 516 | wrapper.style.cssText = "position:relative;min-width:0"; |
| 517 | const unit = document.createElement("span"); |
| 518 | unit.textContent = input.dataset.unit ?? ""; |
| 519 | unit.className = |
| 520 | "pointer-events-none absolute top-1/2 right-2 -translate-y-1/2 font-mono text-xs text-muted-foreground"; |
| 521 | wrapper.append(input, unit); |
| 522 | return wrapper; |
| 523 | } |
| 524 | |
| 525 | function createField( |
| 526 | labelText: string, |
| 527 | input: HTMLInputElement | HTMLSelectElement, |
| 528 | ): HTMLLabelElement { |
| 529 | const label = document.createElement("label"); |
| 530 | label.className = |
| 531 | "grid min-h-7 grid-cols-[82px_minmax(0,1fr)] items-center gap-2 font-sans text-xs font-medium text-muted-foreground"; |
| 532 | const text = document.createElement("span"); |
| 533 | text.textContent = labelText; |
| 534 | styleControl(input); |
| 535 | label.append( |
| 536 | text, |
| 537 | input instanceof HTMLInputElement && input.dataset.unit ? createUnitControl(input) : input, |
| 538 | ); |
| 539 | return label; |
| 540 | } |
| 541 | |
| 542 | function createStyleSection(): HTMLElement { |
| 543 | const section = document.createElement("section"); |
| 544 | section.className = "grid gap-1 border-t border-border py-2"; |
| 545 | return section; |
| 546 | } |
| 547 | |
| 548 | function createUnitInput(unit: string, placeholder = "0"): HTMLInputElement { |
| 549 | const input = document.createElement("input"); |
| 550 | input.type = "number"; |
| 551 | input.placeholder = placeholder; |
| 552 | input.style.paddingRight = "30px"; |
| 553 | input.dataset.unit = unit; |
| 554 | return input; |
| 555 | } |
| 556 | |
| 557 | function startAnnotation( |
| 558 | sendEnabled: boolean, |
| 559 | voice: DesktopPreviewAnnotationVoiceConfig | undefined, |
| 560 | ): void { |
| 561 | activeSession?.teardown(false); |
| 562 | let finished = false; |
| 563 | const host = document.createElement("div"); |
| 564 | host.setAttribute(OVERLAY_ATTRIBUTE, ""); |
| 565 | host.setAttribute("dir", "ltr"); |
| 566 | host.popover = "manual"; |
| 567 | // Resets the UA popover box (fit-content size, auto margin, border, padding, Canvas background). |
| 568 | host.style.cssText = `position:fixed;inset:0;width:auto;height:auto;margin:0;border:0;padding:0;overflow:visible;background:transparent;color:inherit;z-index:${Z_INDEX_OVERLAY};pointer-events:none`; |
| 569 | applyAnnotationTheme(host, annotationTheme); |
| 570 | const shadowRoot = host.attachShadow({ mode: "closed" }); |
| 571 | const themeStyle = document.createElement("style"); |
| 572 | themeStyle.textContent = previewAnnotationStyles; |
| 573 | shadowRoot.appendChild(themeStyle); |
| 574 | |
| 575 | const root = document.createElement("div"); |
| 576 | root.setAttribute(OVERLAY_ATTRIBUTE, ""); |
| 577 | root.setAttribute("dir", "ltr"); |
| 578 | root.className = "fixed inset-0 font-sans text-foreground"; |
| 579 | root.style.cssText = "pointer-events:none"; |
| 580 | const cursorStyle = document.createElement("style"); |
| 581 | cursorStyle.setAttribute(OVERLAY_ATTRIBUTE, ""); |
| 582 | cursorStyle.textContent = `[${OVERLAY_ATTRIBUTE}]::backdrop { display:none !important; } html[data-t3code-annotation-tool] body, html[data-t3code-annotation-tool] body * { cursor: crosshair !important; } [${OVERLAY_ATTRIBUTE}], [${OVERLAY_ATTRIBUTE}] * { cursor: default !important; } [${OVERLAY_ATTRIBUTE}] input[type=number]::-webkit-inner-spin-button, [${OVERLAY_ATTRIBUTE}] input[type=number]::-webkit-outer-spin-button { appearance:none; margin:0; }`; |
| 583 | document.documentElement.appendChild(cursorStyle); |
| 584 | shadowRoot.appendChild(root); |
| 585 | |
| 586 | const hoverOutline = createBox(PRIMARY, PRIMARY_FILL); |
| 587 | const marqueeBox = createBox(PRIMARY, PRIMARY_FILL); |
| 588 | root.append(hoverOutline, marqueeBox); |
| 589 | |
| 590 | const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); |
| 591 | svg.setAttribute(OVERLAY_ATTRIBUTE, ""); |
| 592 | svg.setAttribute("width", "100%"); |
| 593 | svg.setAttribute("height", "100%"); |
| 594 | svg.setAttribute("viewBox", `0 0 ${window.innerWidth} ${window.innerHeight}`); |
| 595 | svg.style.cssText = "position:fixed;inset:0;overflow:visible;pointer-events:none"; |
| 596 | svg.style.zIndex = String(CONTENT_LAYER_Z_INDEX); |
| 597 | root.appendChild(svg); |
| 598 | |
| 599 | const toolbar = document.createElement("div"); |
| 600 | toolbar.setAttribute(OVERLAY_ATTRIBUTE, ""); |
| 601 | toolbar.className = |
| 602 | "pointer-events-auto fixed top-2.5 left-1/2 flex -translate-x-1/2 gap-0.5 rounded-lg border border-border bg-popover/95 p-1 text-popover-foreground shadow-lg backdrop-blur-xl"; |
| 603 | toolbar.style.zIndex = String(CHROME_LAYER_Z_INDEX); |
| 604 | root.appendChild(toolbar); |
| 605 | |
| 606 | const editor = document.createElement("div"); |
| 607 | editor.setAttribute(OVERLAY_ATTRIBUTE, ""); |
| 608 | editor.className = |
| 609 | "pointer-events-auto fixed hidden max-h-[calc(100vh-16px)] w-[min(360px,calc(100vw-16px))] flex-col overflow-hidden rounded-xl border border-border bg-popover/96 text-popover-foreground shadow-2xl backdrop-blur-xl"; |
| 610 | editor.style.zIndex = String(CHROME_LAYER_Z_INDEX); |
| 611 | root.appendChild(editor); |
| 612 | |
| 613 | const composerRow = document.createElement("div"); |
| 614 | composerRow.className = "flex items-start gap-2 p-2"; |
| 615 | |
| 616 | const adjust = createButton("", "Expand annotation editor"); |
| 617 | adjust.setAttribute("aria-label", "Expand annotation editor"); |
| 618 | adjust.setAttribute("aria-expanded", "false"); |
| 619 | adjust.className += |
| 620 | " h-8 w-8 shrink-0 bg-muted p-0 text-muted-foreground hover:bg-accent hover:text-accent-foreground"; |
| 621 | adjust.innerHTML = |
| 622 | '<svg viewBox="0 0 20 20" width="15" height="15" aria-hidden="true"><path d="M4 5h12M4 10h12M4 15h12M7 3v4M13 8v4M9 13v4" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/></svg>'; |
| 623 | composerRow.appendChild(adjust); |
| 624 | |
| 625 | const comment = document.createElement("textarea"); |
| 626 | comment.placeholder = "Describe the change…"; |
| 627 | comment.setAttribute("dir", "auto"); |
| 628 | comment.rows = 1; |
| 629 | comment.className = |
| 630 | "min-h-8 max-h-24 min-w-0 flex-1 resize-none overflow-y-hidden border-0 border-b border-b-transparent bg-transparent px-0 py-1.5 font-sans text-sm leading-5 text-foreground outline-none ring-0 placeholder:text-muted-foreground focus:border-b-primary focus:outline-none focus:ring-0"; |
| 631 | composerRow.appendChild(comment); |
| 632 | |
| 633 | // Recording happens in the application renderer, which owns the microphone consent. |
| 634 | // This guest only draws the controls, forwards trusted gestures, and mirrors state. |
| 635 | let voicePhase: DesktopPreviewAnnotationVoiceState["phase"] = "idle"; |
| 636 | let voiceSettings = false; |
| 637 | let voiceBusy = false; |
| 638 | let voiceAvailable = false; |
| 639 | let voiceKeyCode: string | null = null; |
| 640 | let pendingVoiceSubmission: PreviewAnnotationSubmission | null = null; |
| 641 | let voiceSubmissionReady = false; |
| 642 | const voiceButton = createButton("", "Start voice input"); |
| 643 | voiceButton.innerHTML = |
| 644 | '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 19v3"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><rect x="9" y="2" width="6" height="13" rx="3"/></svg>'; |
| 645 | voiceButton.className += " h-8 w-8 shrink-0 p-0 text-muted-foreground"; |
| 646 | voiceButton.hidden = true; |
| 647 | voiceButton.setAttribute("aria-label", "Start voice input"); |
| 648 | composerRow.appendChild(voiceButton); |
| 649 | const voiceRow = document.createElement("div"); |
| 650 | voiceRow.className = "flex min-w-0 items-center justify-end gap-1 px-2 pb-2"; |
| 651 | voiceRow.hidden = true; |
| 652 | const voicePill = document.createElement("div"); |
| 653 | const voiceCancel = createButton("", "Cancel voice input"); |
| 654 | voiceCancel.innerHTML = |
| 655 | '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><path d="m18 6-12 12M6 6l12 12" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>'; |
| 656 | voiceCancel.className += " h-7 w-7 shrink-0 rounded-full p-0"; |
| 657 | const voiceFinish = createButton("", "Finish voice input"); |
| 658 | voiceFinish.className += |
| 659 | " h-7 w-7 shrink-0 rounded-full border-primary bg-primary p-0 text-primary-foreground hover:bg-primary/90"; |
| 660 | const voiceSkip = createButton("Skip", "Skip transcript cleanup"); |
| 661 | voiceSkip.className += " h-7 shrink-0 rounded-full px-2.5"; |
| 662 | // createButton's rounded-md sorts after rounded-full in the generated CSS, so it would win. |
| 663 | for (const button of [voiceCancel, voiceFinish, voiceSkip]) button.classList.remove("rounded-md"); |
| 664 | const voiceStatus = document.createElement("div"); |
| 665 | voiceStatus.className = "flex h-7 min-w-0 flex-1 items-center gap-2"; |
| 666 | voiceStatus.setAttribute("role", "status"); |
| 667 | const voiceStatusText = document.createElement("span"); |
| 668 | voiceStatusText.className = "min-w-0 flex-1 truncate text-right text-sm text-muted-foreground"; |
| 669 | const waveform = document.createElement("div"); |
| 670 | waveform.className = |
| 671 | "flex h-5 min-w-0 flex-1 items-center justify-between gap-px overflow-hidden"; |
| 672 | waveform.setAttribute("aria-hidden", "true"); |
| 673 | const waveformLevels = Array<number>(24).fill(0); |
| 674 | const waveformBars = waveformLevels.map(() => { |
| 675 | const bar = document.createElement("span"); |
| 676 | bar.className = "h-full w-0.5 shrink-0 origin-center rounded-full bg-primary"; |
| 677 | bar.style.transform = "scaleY(0.08)"; |
| 678 | bar.style.opacity = "0.25"; |
| 679 | waveform.appendChild(bar); |
| 680 | return bar; |
| 681 | }); |
| 682 | const voiceElapsed = document.createElement("span"); |
| 683 | voiceElapsed.className = "shrink-0 text-xs tabular-nums text-muted-foreground"; |
| 684 | let recordingStartedAt = 0; |
| 685 | let elapsedTimer: ReturnType<typeof setInterval> | null = null; |
| 686 | const updateElapsed = () => { |
| 687 | const seconds = Math.floor((Date.now() - recordingStartedAt) / 1_000); |
| 688 | voiceElapsed.textContent = `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`; |
| 689 | }; |
| 690 | voiceStatus.append(voiceStatusText, waveform, voiceElapsed); |
| 691 | voicePill.append(voiceCancel, voiceStatus, voiceSkip, voiceFinish); |
| 692 | voiceRow.appendChild(voicePill); |
| 693 | const voicePreview = document.createElement("p"); |
| 694 | voicePreview.className = "px-2 pb-2 text-sm text-muted-foreground"; |
| 695 | voicePreview.setAttribute("role", "status"); |
| 696 | voicePreview.hidden = true; |
| 697 | const sendVoice = ( |
| 698 | action: DesktopPreviewAnnotationVoiceEvent["action"], |
| 699 | keyboard?: KeyboardEvent, |
| 700 | ) => { |
| 701 | if (!voice || finished) return; |
| 702 | const event: DesktopPreviewAnnotationVoiceEvent = { |
| 703 | sessionId: voice.sessionId, |
| 704 | text: comment.value, |
| 705 | cursor: comment.selectionStart, |
| 706 | selectionEnd: comment.selectionEnd, |
| 707 | action, |
| 708 | ...(keyboard |
| 709 | ? { |
| 710 | keyboard: { |
| 711 | type: keyboard.type === "keyup" ? ("keyup" as const) : ("keydown" as const), |
| 712 | key: keyboard.key, |
| 713 | code: keyboard.code, |
| 714 | metaKey: keyboard.metaKey, |
| 715 | ctrlKey: keyboard.ctrlKey, |
| 716 | shiftKey: keyboard.shiftKey, |
| 717 | altKey: keyboard.altKey, |
| 718 | repeat: keyboard.repeat, |
| 719 | }, |
| 720 | } |
| 721 | : {}), |
| 722 | }; |
| 723 | ipcRenderer.send(ANNOTATION_VOICE_EVENT_CHANNEL, event); |
| 724 | }; |
| 725 | const isDictationChord = (event: KeyboardEvent) => |
| 726 | voice !== undefined && |
| 727 | voice.shortcuts.some((shortcut) => |
| 728 | matchesKeybindingShortcut(event, shortcut, navigator.platform), |
| 729 | ); |
| 730 | for (const button of [voiceButton, voiceFinish, voiceCancel, voiceSkip]) { |
| 731 | button.addEventListener("pointerdown", (event) => event.preventDefault()); |
| 732 | } |
| 733 | // Page scripts cannot reach the closed shadow root, and a synthetic click never counts. |
| 734 | voiceButton.addEventListener("click", (event) => { |
| 735 | if (!event.isTrusted) return; |
| 736 | if (voiceSettings) { |
| 737 | sendVoice("settings"); |
| 738 | return; |
| 739 | } |
| 740 | if (!voiceAvailable || (voiceBusy && voicePhase !== "recording")) return; |
| 741 | sendVoice(voicePhase === "recording" ? "stop" : "start"); |
| 742 | }); |
| 743 | voiceFinish.addEventListener("click", (event) => { |
| 744 | if (event.isTrusted) sendVoice("stop"); |
| 745 | }); |
| 746 | voiceCancel.addEventListener("click", (event) => { |
| 747 | if (!event.isTrusted) return; |
| 748 | pendingVoiceSubmission = null; |
| 749 | sendVoice("cancel"); |
| 750 | }); |
| 751 | voiceSkip.addEventListener("click", (event) => { |
| 752 | if (event.isTrusted) sendVoice("skip"); |
| 753 | }); |
| 754 | const onVoiceState = ( |
| 755 | _event: Electron.IpcRendererEvent, |
| 756 | state: DesktopPreviewAnnotationVoiceState, |
| 757 | ) => { |
| 758 | if (finished || state.sessionId !== voice?.sessionId) return; |
| 759 | const phaseChanged = voicePhase !== state.phase; |
| 760 | const submissionChanged = voiceBusy !== state.blocksSubmission; |
| 761 | const previewChanged = voicePreview.textContent !== (state.preview ?? ""); |
| 762 | voicePhase = state.phase; |
| 763 | voiceSettings = state.errorAction === "settings"; |
| 764 | voiceBusy = state.blocksSubmission; |
| 765 | voiceAvailable = state.available; |
| 766 | comment.readOnly = state.freezesEditor; |
| 767 | if (state.draft) { |
| 768 | voiceSubmissionReady = pendingVoiceSubmission !== null; |
| 769 | comment.value = state.draft.text; |
| 770 | comment.setSelectionRange(state.draft.cursor, state.draft.cursor); |
| 771 | resizeComment(); |
| 772 | } |
| 773 | const recording = state.phase === "recording"; |
| 774 | const active = state.phase !== "idle" && state.phase !== "error"; |
| 775 | const wasActive = voicePill.parentElement === composerRow; |
| 776 | voiceButton.hidden = !state.available || active; |
| 777 | voiceButton.disabled = voiceBusy; |
| 778 | const label = voiceSettings ? "Open voice settings" : "Start voice input"; |
| 779 | voiceButton.title = |
| 780 | !voiceSettings && state.shortcutLabel ? `${label} (${state.shortcutLabel})` : label; |
| 781 | voiceButton.setAttribute("aria-label", label); |
| 782 | // An active dictation takes the textarea's place; it takes focus once (sr-only) so Enter still attaches. |
| 783 | voiceRow.hidden = state.phase === "idle" || active; |
| 784 | comment.classList.toggle("sr-only", active); |
| 785 | if (active && !wasActive) comment.focus({ preventScroll: true }); |
| 786 | submit.hidden = active; |
| 787 | // Re-inserting on every level update would blur a focused Finish or Cancel. |
| 788 | if (active && voicePill.parentElement !== composerRow) comment.after(voicePill); |
| 789 | else if (!active && voicePill.parentElement !== voiceRow) voiceRow.appendChild(voicePill); |
| 790 | voicePill.className = active |
| 791 | ? "flex h-8 min-w-0 flex-1 items-center gap-2" |
| 792 | : "flex min-w-0 items-center gap-1"; |
| 793 | // A draft that arrived while the textarea was sr-only was measured at 1px wide. |
| 794 | if (wasActive && !active) resizeComment(); |
| 795 | else if (phaseChanged) queueEditorLayout(); |
| 796 | voiceCancel.title = |
| 797 | state.phase === "error" ? "Dismiss voice input error" : "Cancel voice input"; |
| 798 | voiceCancel.setAttribute("aria-label", voiceCancel.title); |
| 799 | voiceStatus.setAttribute("aria-label", state.status ?? "Voice input"); |
| 800 | voiceStatus.setAttribute("aria-live", recording ? "off" : "polite"); |
| 801 | voiceStatusText.textContent = state.status; |
| 802 | voiceStatusText.style.color = state.phase === "error" ? "var(--t3-destructive)" : ""; |
| 803 | voiceStatusText.hidden = recording; |
| 804 | waveform.hidden = !recording; |
| 805 | voiceElapsed.hidden = !recording; |
| 806 | if (recording && elapsedTimer === null) { |
| 807 | recordingStartedAt = Date.now(); |
| 808 | waveformLevels.fill(0); |
| 809 | updateElapsed(); |
| 810 | elapsedTimer = setInterval(updateElapsed, 250); |
| 811 | } else if (!recording && elapsedTimer !== null) { |
| 812 | clearInterval(elapsedTimer); |
| 813 | elapsedTimer = null; |
| 814 | } |
| 815 | if (recording) { |
| 816 | waveformLevels.copyWithin(0, 1); |
| 817 | waveformLevels[waveformLevels.length - 1] = state.level; |
| 818 | const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches; |
| 819 | waveformBars.forEach((bar, index) => { |
| 820 | const level = waveformLevels[index] ?? 0; |
| 821 | bar.style.opacity = String(0.25 + level * 0.75); |
| 822 | bar.style.transform = `scaleY(${reducedMotion ? 0.35 : Math.max(0.08, level)})`; |
| 823 | }); |
| 824 | } |
| 825 | voiceSkip.hidden = state.phase !== "post-processing"; |
| 826 | voiceFinish.hidden = !active || state.phase === "post-processing"; |
| 827 | voiceFinish.disabled = !recording; |
| 828 | voiceFinish.title = recording ? "Finish voice input" : (state.status ?? "Voice input is busy"); |
| 829 | if (recording && state.shortcutLabel) voiceFinish.title += ` (${state.shortcutLabel})`; |
| 830 | voiceFinish.setAttribute("aria-label", voiceFinish.title); |
| 831 | if (phaseChanged) |
| 832 | voiceFinish.innerHTML = recording |
| 833 | ? '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><path d="m5 12 4 4L19 6" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>' |
| 834 | : '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><path d="M12 2a10 10 0 1 0 10 10" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>'; |
| 835 | if (!active) voicePill.append(voiceStatus, voiceCancel); |
| 836 | else if (phaseChanged) voicePill.append(voiceCancel, voiceStatus, voiceSkip, voiceFinish); |
| 837 | voicePreview.textContent = state.preview; |
| 838 | voicePreview.hidden = !state.preview; |
| 839 | if (phaseChanged || submissionChanged || previewChanged || state.draft) updateStatus(); |
| 840 | if (pendingVoiceSubmission !== null) { |
| 841 | if (state.phase === "recording") sendVoice("stop"); |
| 842 | if (state.phase === "error") pendingVoiceSubmission = null; |
| 843 | if (state.phase === "idle") { |
| 844 | const submission = pendingVoiceSubmission; |
| 845 | pendingVoiceSubmission = null; |
| 846 | if (voiceSubmissionReady && submission !== null) submitAnnotation(submission); |
| 847 | voiceSubmissionReady = false; |
| 848 | } |
| 849 | } |
| 850 | }; |
| 851 | |
| 852 | const dragHandle = document.createElement("button"); |
| 853 | dragHandle.type = "button"; |
| 854 | dragHandle.textContent = "⠿"; |
| 855 | dragHandle.title = "Drag annotation editor"; |
| 856 | dragHandle.className = |
| 857 | "hidden h-8 w-6 shrink-0 cursor-grab select-none border-0 bg-transparent p-0 font-sans text-lg font-bold leading-5 text-muted-foreground"; |
| 858 | composerRow.appendChild(dragHandle); |
| 859 | |
| 860 | const submit = createButton("Attach", "Attach annotation and screenshot (Enter)"); |
| 861 | const updateSendHint = () => { |
| 862 | submit.title = sendEnabled |
| 863 | ? "Attach annotation and screenshot (Enter). Send with Cmd/Ctrl+Enter." |
| 864 | : "Attach annotation and screenshot (Enter)"; |
| 865 | }; |
| 866 | updateSendHint(); |
| 867 | submit.className += |
| 868 | " h-8 shrink-0 border-primary bg-primary px-3 text-primary-foreground shadow-sm hover:bg-primary/90"; |
| 869 | composerRow.appendChild(submit); |
| 870 | editor.append(composerRow, voicePreview, voiceRow); |
| 871 | |
| 872 | const stylePanel = document.createElement("div"); |
| 873 | stylePanel.className = |
| 874 | "hidden max-h-[min(176px,calc(100vh-180px))] overflow-auto border-t border-border bg-muted/40 px-3"; |
| 875 | editor.appendChild(stylePanel); |
| 876 | |
| 877 | const selected = new Map<Element, SelectedElement>(); |
| 878 | const regions: PreviewAnnotationRegionTarget[] = []; |
| 879 | const strokes: PreviewAnnotationStrokeTarget[] = []; |
| 880 | const styleChanges = new Map<string, PreviewAnnotationStyleChange>(); |
| 881 | const toolButtons = new Map<AnnotationTool, HTMLButtonElement>(); |
| 882 | // Chromium draws a PDF in its own viewer frame, so pointer and key input over it |
| 883 | // never reaches this document. A transparent layer above the viewer catches the |
| 884 | // pointer and takes focus for Escape and the tool keys. A PDF has no elements to |
| 885 | // select, so it starts on Region. |
| 886 | const isPdf = document.contentType === "application/pdf"; |
| 887 | const inputLayer = isPdf ? document.createElement("div") : null; |
| 888 | if (inputLayer) { |
| 889 | inputLayer.setAttribute(INPUT_LAYER_ATTRIBUTE, ""); |
| 890 | inputLayer.tabIndex = -1; |
| 891 | inputLayer.style.cssText = `position:fixed;inset:0;z-index:${Z_INDEX_OVERLAY - 1};cursor:crosshair;outline:none`; |
| 892 | } |
| 893 | let tool: AnnotationTool = isPdf ? "marquee" : "select"; |
| 894 | let dragStart: PreviewAnnotationPoint | null = null; |
| 895 | let activeStroke: { |
| 896 | target: PreviewAnnotationStrokeTarget; |
| 897 | geometry: StrokeGeometry; |
| 898 | group: SVGGElement; |
| 899 | path: SVGPathElement; |
| 900 | } | null = null; |
| 901 | let pendingCapture = false; |
| 902 | let editorExpanded = false; |
| 903 | let editorWasShown = false; |
| 904 | let editorPosition: { left: number; top: number } | null = null; |
| 905 | let editorDrag: { pointerId: number; offsetX: number; offsetY: number } | null = null; |
| 906 | let editorLayoutFrame: number | null = null; |
| 907 | |
| 908 | const resizeComment = (): void => { |
| 909 | const maxHeight = 96; |
| 910 | comment.style.height = "auto"; |
| 911 | const nextHeight = Math.min(comment.scrollHeight, maxHeight); |
| 912 | comment.style.height = `${nextHeight}px`; |
| 913 | comment.style.overflowY = comment.scrollHeight > maxHeight ? "auto" : "hidden"; |
| 914 | queueEditorLayout(); |
| 915 | }; |
| 916 | comment.addEventListener("input", () => { |
| 917 | resizeComment(); |
| 918 | sendVoice("sync"); |
| 919 | }); |
| 920 | |
| 921 | const updateStatus = (): void => { |
| 922 | const hasTargets = selected.size > 0 || regions.length > 0 || strokes.length > 0; |
| 923 | if (!hasTargets && voiceBusy) { |
| 924 | pendingVoiceSubmission = null; |
| 925 | sendVoice("cancel"); |
| 926 | } |
| 927 | editor.style.display = hasTargets ? "flex" : "none"; |
| 928 | submit.disabled = !hasTargets || voicePhase === "preparing" || pendingCapture; |
| 929 | submit.style.opacity = hasTargets ? "1" : "0.45"; |
| 930 | adjust.disabled = !hasTargets; |
| 931 | stylePanel.style.display = editorExpanded && selected.size > 0 ? "grid" : "none"; |
| 932 | queueEditorLayout(); |
| 933 | if (hasTargets && !editorWasShown) { |
| 934 | editorWasShown = true; |
| 935 | window.setTimeout(() => comment.focus({ preventScroll: true }), 0); |
| 936 | } |
| 937 | }; |
| 938 | |
| 939 | const refreshToolButtons = (): void => { |
| 940 | for (const [candidate, button] of toolButtons) { |
| 941 | const active = candidate === tool; |
| 942 | button.classList.toggle("bg-primary/10", active); |
| 943 | button.classList.toggle("text-primary", active); |
| 944 | button.classList.toggle("text-foreground", !active); |
| 945 | } |
| 946 | if (tool !== "select") hoverOutline.style.display = "none"; |
| 947 | if (tool !== "marquee") marqueeBox.style.display = "none"; |
| 948 | document.documentElement.setAttribute("data-t3code-annotation-tool", tool); |
| 949 | }; |
| 950 | |
| 951 | const removeSelected = (target: SelectedElement): void => { |
| 952 | if (target.element instanceof HTMLElement || target.element instanceof SVGElement) { |
| 953 | for (const [property, baseline] of target.baselineStyles) { |
| 954 | if (baseline) target.element.style.setProperty(property, baseline); |
| 955 | else target.element.style.removeProperty(property); |
| 956 | } |
| 957 | } |
| 958 | selected.delete(target.element); |
| 959 | target.outline.remove(); |
| 960 | target.label.remove(); |
| 961 | for (const [key, change] of styleChanges) { |
| 962 | if (change.targetId === target.id) styleChanges.delete(key); |
| 963 | } |
| 964 | }; |
| 965 | |
| 966 | const addSelected = (element: Element): void => { |
| 967 | if (selected.has(element)) return; |
| 968 | const target: SelectedElement = { |
| 969 | id: nextId("element"), |
| 970 | element, |
| 971 | outline: createBox(PRIMARY, PRIMARY_FILL), |
| 972 | label: createLabel(), |
| 973 | baselineStyles: new Map(), |
| 974 | rect: rectFromDomRect(element.getBoundingClientRect()), |
| 975 | }; |
| 976 | selected.set(element, target); |
| 977 | root.append(target.outline, target.label); |
| 978 | updateSelectedVisual(target); |
| 979 | if (editorExpanded) { |
| 980 | stylePanel.style.display = "grid"; |
| 981 | syncStyleControls(); |
| 982 | } |
| 983 | }; |
| 984 | |
| 985 | const toggleSelected = (element: Element, additive: boolean): void => { |
| 986 | const existing = selected.get(element); |
| 987 | if (existing) { |
| 988 | removeSelected(existing); |
| 989 | } else { |
| 990 | if (!additive) { |
| 991 | for (const target of Array.from(selected.values())) removeSelected(target); |
| 992 | } |
| 993 | addSelected(element); |
| 994 | } |
| 995 | updateStatus(); |
| 996 | }; |
| 997 | |
| 998 | const setStyleForSelected = (property: string, value: string): void => { |
| 999 | for (const target of selected.values()) { |
| 1000 | if (!(target.element instanceof HTMLElement || target.element instanceof SVGElement)) |
| 1001 | continue; |
| 1002 | if (!target.baselineStyles.has(property)) { |
| 1003 | target.baselineStyles.set(property, target.element.style.getPropertyValue(property)); |
| 1004 | } |
| 1005 | const key = `${target.id}:${property}`; |
| 1006 | const previousValue = |
| 1007 | styleChanges.get(key)?.previousValue ?? |
| 1008 | getComputedStyle(target.element).getPropertyValue(property).trim(); |
| 1009 | target.element.style.setProperty(property, value, "important"); |
| 1010 | styleChanges.set(key, { |
| 1011 | targetId: target.id, |
| 1012 | selector: null, |
| 1013 | property, |
| 1014 | previousValue, |
| 1015 | value, |
| 1016 | }); |
| 1017 | updateSelectedVisual(target); |
| 1018 | } |
| 1019 | }; |
| 1020 | |
| 1021 | const textSection = createStyleSection(); |
| 1022 | const colorsSection = createStyleSection(); |
| 1023 | const bordersSection = createStyleSection(); |
| 1024 | const sizingSection = createStyleSection(); |
| 1025 | stylePanel.append(textSection, colorsSection, bordersSection, sizingSection); |
| 1026 | |
| 1027 | const fontFamily = document.createElement("select"); |
| 1028 | for (const value of ["inherit", "system-ui", "sans-serif", "serif", "monospace"]) { |
| 1029 | const option = document.createElement("option"); |
| 1030 | option.value = value; |
| 1031 | option.textContent = value; |
| 1032 | fontFamily.appendChild(option); |
| 1033 | } |
| 1034 | fontFamily.addEventListener("change", () => setStyleForSelected("font-family", fontFamily.value)); |
| 1035 | textSection.appendChild(createField("Font", fontFamily)); |
| 1036 | |
| 1037 | const fontSize = createUnitInput("px", "16"); |
| 1038 | fontSize.min = "1"; |
| 1039 | fontSize.max = "300"; |
| 1040 | fontSize.addEventListener("input", () => { |
| 1041 | if (fontSize.value) setStyleForSelected("font-size", `${fontSize.value}px`); |
| 1042 | }); |
| 1043 | textSection.appendChild(createField("Font size", fontSize)); |
| 1044 | |
| 1045 | const fontWeight = document.createElement("select"); |
| 1046 | for (const value of ["300", "400", "500", "600", "700", "800", "900"]) { |
| 1047 | const option = document.createElement("option"); |
| 1048 | option.value = value; |
| 1049 | option.textContent = value; |
| 1050 | fontWeight.appendChild(option); |
| 1051 | } |
| 1052 | fontWeight.addEventListener("change", () => setStyleForSelected("font-weight", fontWeight.value)); |
| 1053 | textSection.appendChild(createField("Font weight", fontWeight)); |
| 1054 | |
| 1055 | const lineHeight = document.createElement("input"); |
| 1056 | lineHeight.type = "text"; |
| 1057 | lineHeight.placeholder = "normal / 1.4"; |
| 1058 | lineHeight.addEventListener("change", () => { |
| 1059 | if (lineHeight.value.trim()) setStyleForSelected("line-height", lineHeight.value.trim()); |
| 1060 | }); |
| 1061 | textSection.appendChild(createField("Line height", lineHeight)); |
| 1062 | |
| 1063 | const createColorRow = ( |
| 1064 | labelText: string, |
| 1065 | property: string, |
| 1066 | section: HTMLElement, |
| 1067 | ): { row: HTMLLabelElement; color: HTMLInputElement; text: HTMLInputElement } => { |
| 1068 | const row = document.createElement("label"); |
| 1069 | row.className = |
| 1070 | "grid min-h-7 grid-cols-[82px_minmax(0,1fr)] items-center gap-2 font-sans text-xs font-medium text-muted-foreground"; |
| 1071 | const label = document.createElement("span"); |
| 1072 | label.textContent = labelText; |
| 1073 | const control = document.createElement("div"); |
| 1074 | control.className = |
| 1075 | "grid h-7 grid-cols-[22px_minmax(0,1fr)] items-center gap-1 rounded-md border border-input bg-background px-1 shadow-xs"; |
| 1076 | const color = document.createElement("input"); |
| 1077 | color.type = "color"; |
| 1078 | color.setAttribute("aria-label", labelText); |
| 1079 | color.style.cssText = |
| 1080 | "width:20px;height:20px;padding:0;border:0;border-radius:5px;overflow:hidden;background:transparent;cursor:pointer"; |
| 1081 | const text = document.createElement("input"); |
| 1082 | text.type = "text"; |
| 1083 | text.setAttribute("aria-label", `${labelText} value`); |
| 1084 | text.className = |
| 1085 | "min-w-0 w-full border-0 bg-transparent font-mono text-xs text-foreground outline-none"; |
| 1086 | color.addEventListener("input", () => { |
| 1087 | text.value = color.value; |
| 1088 | setStyleForSelected(property, color.value); |
| 1089 | }); |
| 1090 | text.addEventListener("change", () => { |
| 1091 | const value = text.value.trim(); |
| 1092 | if (!value) return; |
| 1093 | setStyleForSelected(property, value); |
| 1094 | if (/^#[0-9a-f]{6}$/i.test(value)) color.value = value; |
| 1095 | }); |
| 1096 | control.append(color, text); |
| 1097 | row.append(label, control); |
| 1098 | section.appendChild(row); |
| 1099 | return { row, color, text }; |
| 1100 | }; |
| 1101 | |
| 1102 | const textColor = createColorRow("Text color", "color", colorsSection); |
| 1103 | const backgroundColor = createColorRow("Background", "background-color", colorsSection); |
| 1104 | |
| 1105 | const opacity = document.createElement("input"); |
| 1106 | opacity.type = "range"; |
| 1107 | opacity.min = "0"; |
| 1108 | opacity.max = "1"; |
| 1109 | opacity.step = "0.05"; |
| 1110 | opacity.value = "1"; |
| 1111 | opacity.style.accentColor = PRIMARY; |
| 1112 | opacity.addEventListener("input", () => setStyleForSelected("opacity", opacity.value)); |
| 1113 | colorsSection.appendChild(createField("Opacity", opacity)); |
| 1114 | |
| 1115 | const radius = createUnitInput("px", "0"); |
| 1116 | radius.min = "0"; |
| 1117 | radius.max = "300"; |
| 1118 | radius.addEventListener("input", () => { |
| 1119 | if (radius.value) setStyleForSelected("border-radius", `${radius.value}px`); |
| 1120 | }); |
| 1121 | bordersSection.appendChild(createField("Radius", radius)); |
| 1122 | |
| 1123 | const borderColor = createColorRow("Border color", "border-color", bordersSection); |
| 1124 | |
| 1125 | const borderWidth = createUnitInput("px", "0"); |
| 1126 | borderWidth.min = "0"; |
| 1127 | borderWidth.max = "100"; |
| 1128 | borderWidth.addEventListener("input", () => { |
| 1129 | if (borderWidth.value) { |
| 1130 | setStyleForSelected("border-style", "solid"); |
| 1131 | setStyleForSelected("border-width", `${borderWidth.value}px`); |
| 1132 | } |
| 1133 | }); |
| 1134 | bordersSection.appendChild(createField("Border width", borderWidth)); |
| 1135 | |
| 1136 | const dimensions = document.createElement("div"); |
| 1137 | dimensions.style.cssText = |
| 1138 | "display:grid;grid-template-columns:82px minmax(0,1fr);gap:8px;align-items:center"; |
| 1139 | const dimensionLabel = document.createElement("div"); |
| 1140 | dimensionLabel.className = "grid gap-2 font-sans text-xs font-medium text-muted-foreground"; |
| 1141 | dimensionLabel.innerHTML = "<span>Width</span><span>Height</span>"; |
| 1142 | const dimensionControls = document.createElement("div"); |
| 1143 | dimensionControls.style.cssText = "position:relative;display:grid;gap:3px;padding-left:22px"; |
| 1144 | const widthInput = createUnitInput("px", "auto"); |
| 1145 | const heightInput = createUnitInput("px", "auto"); |
| 1146 | styleControl(widthInput); |
| 1147 | styleControl(heightInput); |
| 1148 | const aspectLock = createButton("", "Lock aspect ratio"); |
| 1149 | aspectLock.setAttribute("aria-pressed", "true"); |
| 1150 | aspectLock.style.cssText += |
| 1151 | ";position:absolute;left:0;top:50%;transform:translateY(-50%);width:18px;height:38px;padding:0"; |
| 1152 | aspectLock.className += " bg-primary/10 text-primary"; |
| 1153 | dimensionControls.append( |
| 1154 | createUnitControl(widthInput), |
| 1155 | createUnitControl(heightInput), |
| 1156 | aspectLock, |
| 1157 | ); |
| 1158 | dimensions.append(dimensionLabel, dimensionControls); |
| 1159 | sizingSection.appendChild(dimensions); |
| 1160 | |
| 1161 | let aspectLocked = true; |
| 1162 | let aspectRatio = 1; |
| 1163 | const refreshAspectButton = (): void => { |
| 1164 | aspectLock.innerHTML = aspectLocked |
| 1165 | ? '<svg viewBox="0 0 20 20" width="14" height="14" aria-hidden="true"><path d="M8 6.5 9.5 5A3.5 3.5 0 0 1 14.5 10l-1.5 1.5M12 13.5 10.5 15A3.5 3.5 0 0 1 5.5 10L7 8.5M7.5 12.5l5-5" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/></svg>' |
| 1166 | : '<svg viewBox="0 0 20 20" width="14" height="14" aria-hidden="true"><path d="m6 6 8 8M8 6.5 9.5 5A3.5 3.5 0 0 1 14 9M12 13.5 10.5 15A3.5 3.5 0 0 1 6 11" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/></svg>'; |
| 1167 | aspectLock.setAttribute("aria-pressed", String(aspectLocked)); |
| 1168 | aspectLock.classList.toggle("bg-primary/10", aspectLocked); |
| 1169 | aspectLock.classList.toggle("text-primary", aspectLocked); |
| 1170 | aspectLock.classList.toggle("bg-muted", !aspectLocked); |
| 1171 | aspectLock.classList.toggle("text-muted-foreground", !aspectLocked); |
| 1172 | }; |
| 1173 | aspectLock.addEventListener("click", () => { |
| 1174 | aspectLocked = !aspectLocked; |
| 1175 | refreshAspectButton(); |
| 1176 | }); |
| 1177 | widthInput.addEventListener("input", () => { |
| 1178 | const width = Number(widthInput.value); |
| 1179 | if (!Number.isFinite(width) || width <= 0) return; |
| 1180 | setStyleForSelected("width", `${width}px`); |
| 1181 | if (aspectLocked && aspectRatio > 0) { |
| 1182 | const height = Math.max(1, Math.round(width / aspectRatio)); |
| 1183 | heightInput.value = String(height); |
| 1184 | setStyleForSelected("height", `${height}px`); |
| 1185 | } |
| 1186 | }); |
| 1187 | heightInput.addEventListener("input", () => { |
| 1188 | const height = Number(heightInput.value); |
| 1189 | if (!Number.isFinite(height) || height <= 0) return; |
| 1190 | setStyleForSelected("height", `${height}px`); |
| 1191 | if (aspectLocked && aspectRatio > 0) { |
| 1192 | const width = Math.max(1, Math.round(height * aspectRatio)); |
| 1193 | widthInput.value = String(width); |
| 1194 | setStyleForSelected("width", `${width}px`); |
| 1195 | } |
| 1196 | }); |
| 1197 | refreshAspectButton(); |
| 1198 | |
| 1199 | const addSpacingField = ( |
| 1200 | label: string, |
| 1201 | property: string, |
| 1202 | placeholder: string, |
| 1203 | ): HTMLInputElement => { |
| 1204 | const input = document.createElement("input"); |
| 1205 | input.type = "text"; |
| 1206 | input.placeholder = placeholder; |
| 1207 | input.addEventListener("change", () => { |
| 1208 | if (input.value.trim()) setStyleForSelected(property, input.value.trim()); |
| 1209 | }); |
| 1210 | sizingSection.appendChild(createField(label, input)); |
| 1211 | return input; |
| 1212 | }; |
| 1213 | const padding = addSpacingField("Padding", "padding", "0 0 0 0"); |
| 1214 | const margin = addSpacingField("Margin", "margin", "0 0 0 0"); |
| 1215 | const gap = addSpacingField("Gap", "gap", "0px"); |
| 1216 | |
| 1217 | const syncStyleControls = (): void => { |
| 1218 | const first = selected.values().next().value as SelectedElement | undefined; |
| 1219 | if (!first) return; |
| 1220 | const computed = getComputedStyle(first.element); |
| 1221 | const rect = first.element.getBoundingClientRect(); |
| 1222 | aspectRatio = rect.height > 0 ? rect.width / rect.height : 1; |
| 1223 | widthInput.value = String(Math.round(rect.width)); |
| 1224 | heightInput.value = String(Math.round(rect.height)); |
| 1225 | fontSize.value = String(Math.round(Number.parseFloat(computed.fontSize) || 16)); |
| 1226 | fontWeight.value = computed.fontWeight.match(/^[0-9]+$/) ? computed.fontWeight : "400"; |
| 1227 | lineHeight.value = computed.lineHeight; |
| 1228 | fontFamily.value = Array.from(fontFamily.options).some( |
| 1229 | (option) => option.value === computed.fontFamily, |
| 1230 | ) |
| 1231 | ? computed.fontFamily |
| 1232 | : "inherit"; |
| 1233 | textColor.text.value = computed.color; |
| 1234 | backgroundColor.text.value = computed.backgroundColor; |
| 1235 | borderColor.text.value = computed.borderColor; |
| 1236 | opacity.value = computed.opacity; |
| 1237 | radius.value = String(Math.round(Number.parseFloat(computed.borderRadius) || 0)); |
| 1238 | borderWidth.value = String(Math.round(Number.parseFloat(computed.borderWidth) || 0)); |
| 1239 | padding.value = computed.padding; |
| 1240 | margin.value = computed.margin; |
| 1241 | gap.value = computed.gap === "normal" ? "0px" : computed.gap; |
| 1242 | }; |
| 1243 | |
| 1244 | const tools: ReadonlyArray<[AnnotationTool, string, string]> = [ |
| 1245 | ["select", "Select", "Select elements (V)"], |
| 1246 | ["marquee", "Region", "Draw a region or marquee-select elements (R)"], |
| 1247 | ["draw", "Draw", "Draw freehand (D)"], |
| 1248 | ["erase", "Erase", "Remove an annotation target (E)"], |
| 1249 | ]; |
| 1250 | for (const [candidate, label, title] of tools) { |
| 1251 | const button = createButton(label, title); |
| 1252 | button.className += " h-8 px-2.5 text-sm"; |
| 1253 | button.addEventListener("click", () => { |
| 1254 | tool = candidate; |
| 1255 | refreshToolButtons(); |
| 1256 | }); |
| 1257 | toolButtons.set(candidate, button); |
| 1258 | toolbar.appendChild(button); |
| 1259 | } |
| 1260 | |
| 1261 | const clampEditorPosition = (left: number, top: number): { left: number; top: number } => { |
| 1262 | const margin = 8; |
| 1263 | const rect = editor.getBoundingClientRect(); |
| 1264 | return { |
| 1265 | left: Math.min( |
| 1266 | Math.max(margin, left), |
| 1267 | Math.max(margin, window.innerWidth - rect.width - margin), |
| 1268 | ), |
| 1269 | top: Math.min( |
| 1270 | Math.max(margin, top), |
| 1271 | Math.max(margin, window.innerHeight - rect.height - margin), |
| 1272 | ), |
| 1273 | }; |
| 1274 | }; |
| 1275 | |
| 1276 | const applyEditorPosition = (position: { left: number; top: number }): void => { |
| 1277 | const clamped = clampEditorPosition(position.left, position.top); |
| 1278 | editor.style.left = `${clamped.left}px`; |
| 1279 | editor.style.top = `${clamped.top}px`; |
| 1280 | editor.style.right = "auto"; |
| 1281 | editor.style.bottom = "auto"; |
| 1282 | if (editorExpanded) editorPosition = clamped; |
| 1283 | }; |
| 1284 | |
| 1285 | const getAnnotationBounds = (): PreviewAnnotationRect | null => |
| 1286 | unionRects( |
| 1287 | [ |
| 1288 | ...Array.from(selected.values(), (target) => visibleElementRect(target.element)).filter( |
| 1289 | (rect) => rect.width > 0 && rect.height > 0, |
| 1290 | ), |
| 1291 | ...regions.map((region) => region.rect), |
| 1292 | ...strokes.map((stroke) => stroke.bounds), |
| 1293 | ], |
| 1294 | 0, |
| 1295 | ); |
| 1296 | |
| 1297 | const positionCompactEditor = (): void => { |
| 1298 | const bounds = getAnnotationBounds(); |
| 1299 | if (!bounds) return; |
| 1300 | const editorRect = editor.getBoundingClientRect(); |
| 1301 | const gap = 8; |
| 1302 | const candidates = [ |
| 1303 | { left: bounds.x + bounds.width + gap, top: bounds.y }, |
| 1304 | { left: bounds.x - editorRect.width - gap, top: bounds.y }, |
| 1305 | { |
| 1306 | left: bounds.x + bounds.width - editorRect.width, |
| 1307 | top: bounds.y + bounds.height + gap, |
| 1308 | }, |
| 1309 | { |
| 1310 | left: bounds.x + bounds.width - editorRect.width, |
| 1311 | top: bounds.y - editorRect.height - gap, |
| 1312 | }, |
| 1313 | ]; |
| 1314 | const overflow = (position: { left: number; top: number }): number => |
| 1315 | Math.max(0, -position.left) + |
| 1316 | Math.max(0, -position.top) + |
| 1317 | Math.max(0, position.left + editorRect.width - window.innerWidth) + |
| 1318 | Math.max(0, position.top + editorRect.height - window.innerHeight); |
| 1319 | const best = candidates.reduce((current, candidate) => |
| 1320 | overflow(candidate) < overflow(current) ? candidate : current, |
| 1321 | ); |
| 1322 | applyEditorPosition(best); |
| 1323 | }; |
| 1324 | |
| 1325 | function queueEditorLayout(): void { |
| 1326 | if (editorLayoutFrame !== null) window.cancelAnimationFrame(editorLayoutFrame); |
| 1327 | editorLayoutFrame = window.requestAnimationFrame(() => { |
| 1328 | editorLayoutFrame = null; |
| 1329 | if (editor.style.display === "none") return; |
| 1330 | if (editorExpanded && editorPosition) applyEditorPosition(editorPosition); |
| 1331 | else positionCompactEditor(); |
| 1332 | }); |
| 1333 | } |
| 1334 | |
| 1335 | adjust.addEventListener("click", () => { |
| 1336 | if (selected.size === 0) return; |
| 1337 | if (!editorExpanded) { |
| 1338 | const rect = editor.getBoundingClientRect(); |
| 1339 | editorExpanded = true; |
| 1340 | editorPosition = { left: rect.left, top: rect.top }; |
| 1341 | stylePanel.style.display = selected.size > 0 ? "grid" : "none"; |
| 1342 | dragHandle.style.display = "block"; |
| 1343 | adjust.setAttribute("aria-expanded", "true"); |
| 1344 | adjust.title = "Collapse annotation editor"; |
| 1345 | adjust.setAttribute("aria-label", "Collapse annotation editor"); |
| 1346 | if (selected.size > 0) syncStyleControls(); |
| 1347 | } else { |
| 1348 | editorExpanded = false; |
| 1349 | editorPosition = null; |
| 1350 | stylePanel.style.display = "none"; |
| 1351 | dragHandle.style.display = "none"; |
| 1352 | adjust.setAttribute("aria-expanded", "false"); |
| 1353 | adjust.title = "Expand annotation editor"; |
| 1354 | adjust.setAttribute("aria-label", "Expand annotation editor"); |
| 1355 | } |
| 1356 | queueEditorLayout(); |
| 1357 | }); |
| 1358 | |
| 1359 | const onEditorPointerDown = (event: PointerEvent): void => { |
| 1360 | if (event.button !== 0 || !editorExpanded) return; |
| 1361 | const rect = editor.getBoundingClientRect(); |
| 1362 | editorDrag = { |
| 1363 | pointerId: event.pointerId, |
| 1364 | offsetX: event.clientX - rect.left, |
| 1365 | offsetY: event.clientY - rect.top, |
| 1366 | }; |
| 1367 | dragHandle.setPointerCapture(event.pointerId); |
| 1368 | dragHandle.style.cursor = "grabbing"; |
| 1369 | event.preventDefault(); |
| 1370 | event.stopPropagation(); |
| 1371 | }; |
| 1372 | |
| 1373 | const onEditorPointerMove = (event: PointerEvent): void => { |
| 1374 | if (!editorDrag || editorDrag.pointerId !== event.pointerId) return; |
| 1375 | applyEditorPosition({ |
| 1376 | left: event.clientX - editorDrag.offsetX, |
| 1377 | top: event.clientY - editorDrag.offsetY, |
| 1378 | }); |
| 1379 | event.preventDefault(); |
| 1380 | event.stopPropagation(); |
| 1381 | }; |
| 1382 | |
| 1383 | const onEditorPointerUp = (event: PointerEvent): void => { |
| 1384 | if (!editorDrag || editorDrag.pointerId !== event.pointerId) return; |
| 1385 | editorDrag = null; |
| 1386 | dragHandle.style.cursor = "grab"; |
| 1387 | if (dragHandle.hasPointerCapture(event.pointerId)) |
| 1388 | dragHandle.releasePointerCapture(event.pointerId); |
| 1389 | event.preventDefault(); |
| 1390 | event.stopPropagation(); |
| 1391 | }; |
| 1392 | dragHandle.addEventListener("pointerdown", onEditorPointerDown); |
| 1393 | dragHandle.addEventListener("pointermove", onEditorPointerMove); |
| 1394 | dragHandle.addEventListener("pointerup", onEditorPointerUp); |
| 1395 | dragHandle.addEventListener("pointercancel", onEditorPointerUp); |
| 1396 | |
| 1397 | const repaint = (): void => { |
| 1398 | for (const target of selected.values()) updateSelectedVisual(target); |
| 1399 | queueEditorLayout(); |
| 1400 | }; |
| 1401 | const onScroll = (): void => { |
| 1402 | // A detached element's last rect no longer matches what is on screen once the page scrolls. |
| 1403 | for (const target of selected.values()) { |
| 1404 | if (!target.element.isConnected) target.rect = { x: 0, y: 0, width: 0, height: 0 }; |
| 1405 | } |
| 1406 | repaint(); |
| 1407 | }; |
| 1408 | // A scrollbar appearing narrows the viewport without a window resize. |
| 1409 | const rootResizeObserver = new ResizeObserver(repaint); |
| 1410 | |
| 1411 | const removeTargetAtPoint = (x: number, y: number): boolean => { |
| 1412 | for (const target of Array.from(selected.values()).toReversed()) { |
| 1413 | const rect = target.element.getBoundingClientRect(); |
| 1414 | if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) { |
| 1415 | removeSelected(target); |
| 1416 | updateStatus(); |
| 1417 | return true; |
| 1418 | } |
| 1419 | } |
| 1420 | const regionIndex = regions.findIndex( |
| 1421 | (region) => |
| 1422 | x >= region.rect.x && |
| 1423 | x <= region.rect.x + region.rect.width && |
| 1424 | y >= region.rect.y && |
| 1425 | y <= region.rect.y + region.rect.height, |
| 1426 | ); |
| 1427 | if (regionIndex >= 0) { |
| 1428 | const [removed] = regions.splice(regionIndex, 1); |
| 1429 | root.querySelector(`[data-region-id="${removed?.id}"]`)?.remove(); |
| 1430 | updateStatus(); |
| 1431 | return true; |
| 1432 | } |
| 1433 | const strokeIndex = strokes.findIndex( |
| 1434 | (stroke) => |
| 1435 | x >= stroke.bounds.x && |
| 1436 | x <= stroke.bounds.x + stroke.bounds.width && |
| 1437 | y >= stroke.bounds.y && |
| 1438 | y <= stroke.bounds.y + stroke.bounds.height, |
| 1439 | ); |
| 1440 | if (strokeIndex >= 0) { |
| 1441 | const [removed] = strokes.splice(strokeIndex, 1); |
| 1442 | svg.querySelector(`[data-stroke-id="${removed?.id}"]`)?.remove(); |
| 1443 | updateStatus(); |
| 1444 | return true; |
| 1445 | } |
| 1446 | return false; |
| 1447 | }; |
| 1448 | |
| 1449 | const selectElementsInRect = (rect: PreviewAnnotationRect): number => { |
| 1450 | const candidates = Array.from(document.querySelectorAll("body *")) |
| 1451 | .filter((element) => !isAnnotationNode(element)) |
| 1452 | .map((element) => ({ element, rect: element.getBoundingClientRect() })) |
| 1453 | .filter(({ rect: candidate }) => { |
| 1454 | if (candidate.width < 2 || candidate.height < 2) return false; |
| 1455 | return !( |
| 1456 | candidate.right < rect.x || |
| 1457 | candidate.left > rect.x + rect.width || |
| 1458 | candidate.bottom < rect.y || |
| 1459 | candidate.top > rect.y + rect.height |
| 1460 | ); |
| 1461 | }) |
| 1462 | .filter(({ element, rect: candidate }) => { |
| 1463 | const centerX = candidate.left + candidate.width / 2; |
| 1464 | const centerY = candidate.top + candidate.height / 2; |
| 1465 | return ( |
| 1466 | centerX >= rect.x && |
| 1467 | centerX <= rect.x + rect.width && |
| 1468 | centerY >= rect.y && |
| 1469 | centerY <= rect.y + rect.height && |
| 1470 | (element.children.length === 0 || |
| 1471 | element instanceof HTMLButtonElement || |
| 1472 | element instanceof HTMLAnchorElement || |
| 1473 | element.getAttribute("role") === "button") |
| 1474 | ); |
| 1475 | }) |
| 1476 | .sort( |
| 1477 | (left, right) => left.rect.width * left.rect.height - right.rect.width * right.rect.height, |
| 1478 | ) |
| 1479 | .slice(0, MAX_MARQUEE_ELEMENTS); |
| 1480 | for (const candidate of candidates) addSelected(candidate.element); |
| 1481 | return candidates.length; |
| 1482 | }; |
| 1483 | |
| 1484 | const clearHoverOutline = (): void => { |
| 1485 | hoverOutline.style.display = "none"; |
| 1486 | }; |
| 1487 | |
| 1488 | const onPointerMove = (event: PointerEvent): void => { |
| 1489 | if (isAnnotationNode(event.target as Element)) { |
| 1490 | clearHoverOutline(); |
| 1491 | return; |
| 1492 | } |
| 1493 | if (tool === "select" && dragStart === null) { |
| 1494 | const target = pickFromPoint(event.clientX, event.clientY); |
| 1495 | if (target) positionBox(hoverOutline, visibleElementRect(target)); |
| 1496 | else clearHoverOutline(); |
| 1497 | return; |
| 1498 | } |
| 1499 | clearHoverOutline(); |
| 1500 | if (tool === "marquee" && dragStart) { |
| 1501 | positionBox( |
| 1502 | marqueeBox, |
| 1503 | normalizeRect(dragStart.x, dragStart.y, event.clientX, event.clientY), |
| 1504 | ); |
| 1505 | return; |
| 1506 | } |
| 1507 | if (tool === "draw" && activeStroke) { |
| 1508 | const closedPath = addStrokePoint(activeStroke.geometry, { |
| 1509 | x: event.clientX, |
| 1510 | y: event.clientY, |
| 1511 | }); |
| 1512 | activeStroke.target.bounds = strokeBounds(activeStroke.geometry, activeStroke.target.width); |
| 1513 | if (closedPath !== null) { |
| 1514 | activeStroke.path.setAttribute("d", closedPath); |
| 1515 | activeStroke.path = activeStroke.group.appendChild( |
| 1516 | document.createElementNS("http://www.w3.org/2000/svg", "path"), |
| 1517 | ); |
| 1518 | } |
| 1519 | activeStroke.path.setAttribute("d", openStrokePath(activeStroke.geometry)); |
| 1520 | } |
| 1521 | }; |
| 1522 | |
| 1523 | const onPointerDown = (event: PointerEvent): void => { |
| 1524 | if (event.button !== 0 || isAnnotationNode(event.target as Element)) return; |
| 1525 | event.preventDefault(); |
| 1526 | event.stopPropagation(); |
| 1527 | if (tool === "select") { |
| 1528 | const target = pickFromPoint(event.clientX, event.clientY); |
| 1529 | if (target) toggleSelected(target, event.shiftKey); |
| 1530 | return; |
| 1531 | } |
| 1532 | if (tool === "erase") { |
| 1533 | removeTargetAtPoint(event.clientX, event.clientY); |
| 1534 | return; |
| 1535 | } |
| 1536 | dragStart = { x: event.clientX, y: event.clientY }; |
| 1537 | if (tool === "draw") { |
| 1538 | const geometry = startStroke(dragStart); |
| 1539 | const stroke: PreviewAnnotationStrokeTarget = { |
| 1540 | id: nextId("stroke"), |
| 1541 | color: annotationTheme?.primary ?? "#2563eb", |
| 1542 | width: 4, |
| 1543 | points: geometry.points, |
| 1544 | bounds: { x: dragStart.x, y: dragStart.y, width: 1, height: 1 }, |
| 1545 | }; |
| 1546 | const group = document.createElementNS("http://www.w3.org/2000/svg", "g"); |
| 1547 | group.setAttribute(OVERLAY_ATTRIBUTE, ""); |
| 1548 | group.setAttribute("data-stroke-id", stroke.id); |
| 1549 | group.setAttribute("fill", "none"); |
| 1550 | group.setAttribute("stroke", stroke.color); |
| 1551 | group.setAttribute("stroke-width", String(stroke.width)); |
| 1552 | group.setAttribute("stroke-linecap", "round"); |
| 1553 | group.setAttribute("stroke-linejoin", "round"); |
| 1554 | const path = group.appendChild( |
| 1555 | document.createElementNS("http://www.w3.org/2000/svg", "path"), |
| 1556 | ); |
| 1557 | svg.appendChild(group); |
| 1558 | activeStroke = { target: stroke, geometry, group, path }; |
| 1559 | } |
| 1560 | }; |
| 1561 | |
| 1562 | const onPointerUp = (event: PointerEvent): void => { |
| 1563 | if (!dragStart) return; |
| 1564 | event.preventDefault(); |
| 1565 | event.stopPropagation(); |
| 1566 | if (tool === "marquee") { |
| 1567 | const rect = normalizeRect(dragStart.x, dragStart.y, event.clientX, event.clientY); |
| 1568 | marqueeBox.style.display = "none"; |
| 1569 | if (isUsableRect(rect)) { |
| 1570 | const found = selectElementsInRect(rect); |
| 1571 | if (found === 0) { |
| 1572 | const region: PreviewAnnotationRegionTarget = { id: nextId("region"), rect }; |
| 1573 | regions.push(region); |
| 1574 | const regionBox = createBox( |
| 1575 | PRIMARY, |
| 1576 | "color-mix(in srgb, var(--t3-primary) 6%, transparent)", |
| 1577 | ); |
| 1578 | regionBox.setAttribute("data-region-id", region.id); |
| 1579 | positionBox(regionBox, rect); |
| 1580 | root.appendChild(regionBox); |
| 1581 | } |
| 1582 | } |
| 1583 | } else if (tool === "draw" && activeStroke) { |
| 1584 | if (activeStroke.target.points.length > 1) strokes.push(activeStroke.target); |
| 1585 | else activeStroke.group.remove(); |
| 1586 | activeStroke = null; |
| 1587 | } |
| 1588 | dragStart = null; |
| 1589 | updateStatus(); |
| 1590 | }; |
| 1591 | |
| 1592 | const onClick = (event: MouseEvent): void => { |
| 1593 | if (isAnnotationNode(event.target as Element)) return; |
| 1594 | event.preventDefault(); |
| 1595 | event.stopPropagation(); |
| 1596 | }; |
| 1597 | |
| 1598 | const stopAtOverlay = (event: Event): void => event.stopPropagation(); |
| 1599 | // Focus moving from the page into the overlay targets the page element, so the host never sees it. |
| 1600 | const onFocusOutToOverlay = (event: FocusEvent): void => { |
| 1601 | if (event.relatedTarget instanceof Element && isAnnotationNode(event.relatedTarget)) |
| 1602 | event.stopPropagation(); |
| 1603 | }; |
| 1604 | |
| 1605 | // Page popovers and modal dialogs sit in the top layer above any z-index, and a modal makes |
| 1606 | // everything outside it inert, so the overlay is shown last and lives inside the topmost modal. |
| 1607 | // Modals in the order they opened, since DOM order does not say which one blocks the rest. |
| 1608 | const openedModals: Element[] = []; |
| 1609 | const raiseOverlay = (): void => { |
| 1610 | const focused = shadowRoot.activeElement; |
| 1611 | const parent = |
| 1612 | document.activeElement?.closest("dialog:modal") ?? |
| 1613 | openedModals.findLast((dialog) => dialog.matches(":modal")) ?? |
| 1614 | Array.from(document.querySelectorAll("dialog:modal")).at(-1) ?? |
| 1615 | document.documentElement; |
| 1616 | if (host.parentElement !== parent) parent.appendChild(host); |
| 1617 | if (host.matches(":popover-open")) host.hidePopover(); |
| 1618 | host.showPopover(); |
| 1619 | if (focused instanceof HTMLElement && !focused.matches(":focus")) |
| 1620 | focused.focus({ preventScroll: true }); |
| 1621 | }; |
| 1622 | const onPageToggle = (event: Event): void => { |
| 1623 | const target = event.target; |
| 1624 | if (target === host) return; |
| 1625 | if (target instanceof HTMLDialogElement) { |
| 1626 | const index = openedModals.indexOf(target); |
| 1627 | if (index !== -1) openedModals.splice(index, 1); |
| 1628 | if (target.matches(":modal")) openedModals.push(target); |
| 1629 | } |
| 1630 | raiseOverlay(); |
| 1631 | }; |
| 1632 | // Removing an open dialog hides the overlay inside it without a toggle event. |
| 1633 | const detachObserver = new MutationObserver(() => { |
| 1634 | if (!host.isConnected || !host.matches(":popover-open")) raiseOverlay(); |
| 1635 | }); |
| 1636 | |
| 1637 | const onPointerOut = (event: PointerEvent): void => { |
| 1638 | if (event.relatedTarget === null) clearHoverOutline(); |
| 1639 | }; |
| 1640 | |
| 1641 | const onWindowFocus = (): void => { |
| 1642 | if (editor.style.display !== "none") sendVoice("focus"); |
| 1643 | }; |
| 1644 | const onWindowBlur = (): void => { |
| 1645 | sendVoice("blur"); |
| 1646 | clearHoverOutline(); |
| 1647 | }; |
| 1648 | |
| 1649 | const restoreStyles = (): void => { |
| 1650 | for (const target of selected.values()) { |
| 1651 | if (!(target.element instanceof HTMLElement || target.element instanceof SVGElement)) |
| 1652 | continue; |
| 1653 | for (const [property, baseline] of target.baselineStyles) { |
| 1654 | if (baseline) target.element.style.setProperty(property, baseline); |
| 1655 | else target.element.style.removeProperty(property); |
| 1656 | } |
| 1657 | } |
| 1658 | }; |
| 1659 | |
| 1660 | const teardown = (notifyMain: boolean): void => { |
| 1661 | if (finished) return; |
| 1662 | finished = true; |
| 1663 | rootResizeObserver.disconnect(); |
| 1664 | restoreStyles(); |
| 1665 | window.removeEventListener("pointermove", onPointerMove, true); |
| 1666 | window.removeEventListener("pointerdown", onPointerDown, true); |
| 1667 | window.removeEventListener("pointerup", onPointerUp, true); |
| 1668 | window.removeEventListener("pointerout", onPointerOut, true); |
| 1669 | window.removeEventListener("click", onClick, true); |
| 1670 | window.removeEventListener("blur", onWindowBlur); |
| 1671 | window.removeEventListener("focus", onWindowFocus); |
| 1672 | window.removeEventListener("keydown", onKeyDown, true); |
| 1673 | window.removeEventListener("keyup", onVoiceKeyUp, true); |
| 1674 | window.removeEventListener("scroll", onScroll, true); |
| 1675 | window.removeEventListener("resize", repaint); |
| 1676 | dragHandle.removeEventListener("pointerdown", onEditorPointerDown); |
| 1677 | dragHandle.removeEventListener("pointermove", onEditorPointerMove); |
| 1678 | dragHandle.removeEventListener("pointerup", onEditorPointerUp); |
| 1679 | dragHandle.removeEventListener("pointercancel", onEditorPointerUp); |
| 1680 | for (const type of PAGE_ISOLATED_EVENTS) host.removeEventListener(type, stopAtOverlay); |
| 1681 | window.removeEventListener("toggle", onPageToggle, true); |
| 1682 | detachObserver.disconnect(); |
| 1683 | document.documentElement.removeEventListener("focusout", onFocusOutToOverlay); |
| 1684 | if (editorLayoutFrame !== null) window.cancelAnimationFrame(editorLayoutFrame); |
| 1685 | ipcRenderer.off(CANCEL_PICK_CHANNEL, onCancel); |
| 1686 | ipcRenderer.off(ANNOTATION_CAPTURED_CHANNEL, onCaptured); |
| 1687 | ipcRenderer.off(ANNOTATION_VOICE_STATE_CHANNEL, onVoiceState); |
| 1688 | if (elapsedTimer !== null) clearInterval(elapsedTimer); |
| 1689 | document.documentElement.removeAttribute("data-t3code-annotation-tool"); |
| 1690 | cursorStyle.remove(); |
| 1691 | inputLayer?.remove(); |
| 1692 | host.remove(); |
| 1693 | activeSession = null; |
| 1694 | if (notifyMain) ipcRenderer.send(ELEMENT_PICKED_CHANNEL, null); |
| 1695 | }; |
| 1696 | |
| 1697 | const onCancel = (): void => teardown(false); |
| 1698 | const onCaptured = (): void => teardown(false); |
| 1699 | const onVoiceKeyUp = (event: KeyboardEvent): void => { |
| 1700 | if (!event.isTrusted || (event.code || event.key) !== voiceKeyCode) return; |
| 1701 | voiceKeyCode = null; |
| 1702 | event.preventDefault(); |
| 1703 | event.stopImmediatePropagation(); |
| 1704 | sendVoice("key", event); |
| 1705 | }; |
| 1706 | const onKeyDown = (event: KeyboardEvent): void => { |
| 1707 | // Only the keyboard drives dictation; a page cannot synthesize the chord or Escape. |
| 1708 | if (voice && event.isTrusted && !event.isComposing && event.keyCode !== 229) { |
| 1709 | if (event.key === "Escape" && voiceBusy) { |
| 1710 | event.preventDefault(); |
| 1711 | event.stopImmediatePropagation(); |
| 1712 | voiceKeyCode = null; |
| 1713 | pendingVoiceSubmission = null; |
| 1714 | sendVoice("key", event); |
| 1715 | return; |
| 1716 | } |
| 1717 | if (voiceAvailable && editor.style.display !== "none" && isDictationChord(event)) { |
| 1718 | event.preventDefault(); |
| 1719 | event.stopImmediatePropagation(); |
| 1720 | voiceKeyCode = event.code || event.key; |
| 1721 | sendVoice("key", event); |
| 1722 | return; |
| 1723 | } |
| 1724 | } |
| 1725 | if (event.key === "Escape" && !event.isComposing) { |
| 1726 | event.preventDefault(); |
| 1727 | event.stopPropagation(); |
| 1728 | teardown(true); |
| 1729 | return; |
| 1730 | } |
| 1731 | if (shadowRoot.activeElement?.matches("input, textarea, select")) return; |
| 1732 | if (event.key === "v") tool = "select"; |
| 1733 | else if (event.key === "r") tool = "marquee"; |
| 1734 | else if (event.key === "d") tool = "draw"; |
| 1735 | else if (event.key === "e") tool = "erase"; |
| 1736 | else return; |
| 1737 | refreshToolButtons(); |
| 1738 | }; |
| 1739 | |
| 1740 | const submitAnnotation = (submission: PreviewAnnotationSubmission): void => { |
| 1741 | if (submission === "send" && !sendEnabled) return; |
| 1742 | if (voicePhase === "preparing") return; |
| 1743 | // Submitting while dictating finishes the recording and submits once the text lands. |
| 1744 | if (voiceBusy) { |
| 1745 | if (pendingVoiceSubmission === null) { |
| 1746 | pendingVoiceSubmission = submission; |
| 1747 | voiceSubmissionReady = false; |
| 1748 | if (voicePhase === "recording") sendVoice("stop"); |
| 1749 | } |
| 1750 | return; |
| 1751 | } |
| 1752 | if (pendingCapture || (selected.size === 0 && regions.length === 0 && strokes.length === 0)) |
| 1753 | return; |
| 1754 | pendingCapture = true; |
| 1755 | submit.disabled = true; |
| 1756 | submit.textContent = "Capturing…"; |
| 1757 | // Snapshot the submitted comment before context lookup lets the user edit it. |
| 1758 | const submittedComment = comment.value.trim(); |
| 1759 | const submittedRegions = [...regions]; |
| 1760 | const submittedStrokes = [...strokes]; |
| 1761 | const submittedStyleChanges = Array.from(styleChanges.values(), (change) => ({ ...change })); |
| 1762 | void Promise.all( |
| 1763 | Array.from(selected.values()).map(async (target) => { |
| 1764 | const element = await captureElement(target.element); |
| 1765 | for (const change of submittedStyleChanges) { |
| 1766 | if (change.targetId === target.id && element.selector !== null) { |
| 1767 | change.selector = element.selector; |
| 1768 | } |
| 1769 | } |
| 1770 | return { |
| 1771 | id: target.id, |
| 1772 | element, |
| 1773 | rect: target.element.isConnected |
| 1774 | ? rectFromDomRect(target.element.getBoundingClientRect()) |
| 1775 | : target.rect, |
| 1776 | }; |
| 1777 | }), |
| 1778 | ) |
| 1779 | .then((elements) => { |
| 1780 | // A late submit must not reach a replacement pick's listener. |
| 1781 | if (finished) return; |
| 1782 | const annotation: PreviewAnnotationPayload = { |
| 1783 | id: nextId("annotation"), |
| 1784 | pageUrl: location.href, |
| 1785 | pageTitle: document.title?.trim() || null, |
| 1786 | comment: submittedComment, |
| 1787 | elements, |
| 1788 | regions: submittedRegions, |
| 1789 | strokes: submittedStrokes, |
| 1790 | styleChanges: submittedStyleChanges, |
| 1791 | screenshot: null, |
| 1792 | createdAt: new Date().toISOString(), |
| 1793 | }; |
| 1794 | editor.style.display = "none"; |
| 1795 | toolbar.style.display = "none"; |
| 1796 | hoverOutline.style.display = "none"; |
| 1797 | const screenshotRect = unionRects([ |
| 1798 | ...elements.map((target) => target.rect), |
| 1799 | ...submittedRegions.map((region) => region.rect), |
| 1800 | ...submittedStrokes.map((stroke) => stroke.bounds), |
| 1801 | ]); |
| 1802 | ipcRenderer.send( |
| 1803 | ELEMENT_PICKED_CHANNEL, |
| 1804 | annotation, |
| 1805 | screenshotRect, |
| 1806 | submission === "send" && !sendEnabled ? "attach" : submission, |
| 1807 | // Main crops a viewport capture, whose pixels are CSS px × this. |
| 1808 | window.devicePixelRatio, |
| 1809 | ); |
| 1810 | }) |
| 1811 | .catch(() => { |
| 1812 | teardown(true); |
| 1813 | }); |
| 1814 | }; |
| 1815 | submit.addEventListener("click", (event) => { |
| 1816 | if (event.isTrusted) submitAnnotation("attach"); |
| 1817 | }); |
| 1818 | const handleOverlayKey = (event: KeyboardEvent): void => { |
| 1819 | if (event.type === "keyup") onVoiceKeyUp(event); |
| 1820 | if (event.type !== "keydown") return; |
| 1821 | onKeyDown(event); |
| 1822 | if (event.cancelBubble) return; |
| 1823 | const submission = |
| 1824 | shadowRoot.activeElement === comment ? resolveAnnotationSubmission(event, sendEnabled) : null; |
| 1825 | if (!submission) return; |
| 1826 | event.preventDefault(); |
| 1827 | submitAnnotation(submission); |
| 1828 | }; |
| 1829 | |
| 1830 | window.addEventListener("pointermove", onPointerMove, { capture: true, passive: false }); |
| 1831 | window.addEventListener("pointerdown", onPointerDown, { capture: true, passive: false }); |
| 1832 | window.addEventListener("pointerup", onPointerUp, { capture: true, passive: false }); |
| 1833 | window.addEventListener("pointerout", onPointerOut, { capture: true, passive: true }); |
| 1834 | window.addEventListener("click", onClick, { capture: true, passive: false }); |
| 1835 | window.addEventListener("blur", onWindowBlur); |
| 1836 | window.addEventListener("focus", onWindowFocus); |
| 1837 | window.addEventListener("keydown", onKeyDown, { capture: true }); |
| 1838 | window.addEventListener("keyup", onVoiceKeyUp, { capture: true }); |
| 1839 | window.addEventListener("scroll", onScroll, { capture: true, passive: true }); |
| 1840 | window.addEventListener("resize", repaint, { passive: true }); |
| 1841 | // Bubble phase, so the overlay's own handlers still see these events. |
| 1842 | for (const type of PAGE_ISOLATED_EVENTS) |
| 1843 | host.addEventListener(type, stopAtOverlay, { passive: true }); |
| 1844 | document.documentElement.addEventListener("focusout", onFocusOutToOverlay); |
| 1845 | ipcRenderer.on(CANCEL_PICK_CHANNEL, onCancel); |
| 1846 | ipcRenderer.on(ANNOTATION_CAPTURED_CHANNEL, onCaptured); |
| 1847 | if (voice) ipcRenderer.on(ANNOTATION_VOICE_STATE_CHANNEL, onVoiceState); |
| 1848 | if (inputLayer) { |
| 1849 | document.documentElement.appendChild(inputLayer); |
| 1850 | inputLayer.focus({ preventScroll: true }); |
| 1851 | } |
| 1852 | raiseOverlay(); |
| 1853 | window.addEventListener("toggle", onPageToggle, true); |
| 1854 | detachObserver.observe(document.documentElement, { childList: true, subtree: true }); |
| 1855 | rootResizeObserver.observe(document.documentElement); |
| 1856 | refreshToolButtons(); |
| 1857 | updateStatus(); |
| 1858 | sendVoice(document.hasFocus() ? "focus" : "sync"); |
| 1859 | activeSession = { |
| 1860 | teardown, |
| 1861 | applyTheme: (theme) => applyAnnotationTheme(host, theme), |
| 1862 | setSendEnabled: (enabled) => { |
| 1863 | sendEnabled = enabled; |
| 1864 | updateSendHint(); |
| 1865 | }, |
| 1866 | handleOverlayKey, |
| 1867 | }; |
| 1868 | } |
| 1869 | |
| 1870 | ipcRenderer.on( |
| 1871 | START_PICK_CHANNEL, |
| 1872 | ( |
| 1873 | _event, |
| 1874 | theme: DesktopPreviewAnnotationTheme | undefined, |
| 1875 | sendEnabled?: boolean, |
| 1876 | voice?: DesktopPreviewAnnotationVoiceConfig, |
| 1877 | ) => { |
| 1878 | if (theme) annotationTheme = theme; |
| 1879 | startAnnotation(sendEnabled === true, voice); |
| 1880 | }, |
| 1881 | ); |
| 1882 | ipcRenderer.on(ANNOTATION_SEND_ENABLED_CHANNEL, (_event, enabled: boolean) => { |
| 1883 | activeSession?.setSendEnabled(enabled === true); |
| 1884 | }); |
| 1885 | ipcRenderer.on(ANNOTATION_THEME_CHANNEL, (_event, theme: DesktopPreviewAnnotationTheme) => { |
| 1886 | annotationTheme = theme; |
| 1887 | recordingCursor?.setTheme(theme); |
| 1888 | activeSession?.applyTheme(theme); |
| 1889 | }); |
| 1890 | ipcRenderer.on(CANCEL_PICK_CHANNEL, () => activeSession?.teardown(false)); |