Skip to content

Commit 9684f8f

Browse files
feat(ui): expand video attachments in the media overlay (#132131)
* feat(ui): expand video attachments in media overlay * fix(ui): preserve video overlay playback ownership * fix(ui): satisfy video overlay CI boundaries
1 parent a920626 commit 9684f8f

14 files changed

Lines changed: 285 additions & 119 deletions

‎ui/src/components/image-lightbox.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,47 @@ describe("openclaw-image-lightbox", () => {
9292
expect(root?.querySelector<HTMLButtonElement>(".close")?.hasAttribute("autofocus")).toBe(true);
9393
});
9494

95+
it("renders video in the shared overlay without image zoom controls", async () => {
96+
render(
97+
html`<openclaw-image-lightbox
98+
mediaKind="video"
99+
src="https://example.com/demo.mp4?playback=1"
100+
originalSrc="https://example.com/demo.mp4"
101+
.imageTitle=${"Demo clip"}
102+
></openclaw-image-lightbox>`,
103+
container,
104+
);
105+
const modal = container.querySelector("openclaw-image-lightbox");
106+
if (!modal) {
107+
throw new Error("missing media lightbox");
108+
}
109+
await modal.updateComplete;
110+
111+
const video = modal.shadowRoot?.querySelector<HTMLVideoElement>("video");
112+
expect(video?.src).toBe("https://example.com/demo.mp4?playback=1");
113+
expect(video?.controls).toBe(true);
114+
expect(video?.autoplay).toBe(true);
115+
expect(modal.shadowRoot?.querySelector("img, .zoom-controls")).toBeNull();
116+
expect(
117+
modal.shadowRoot?.querySelector<HTMLButtonElement>(".close")?.getAttribute("aria-label"),
118+
).toBe("Close video preview");
119+
await vi.waitFor(() =>
120+
expect(modal.shadowRoot?.querySelector<HTMLAnchorElement>(".open-original")?.href).toBe(
121+
"https://example.com/demo.mp4",
122+
),
123+
);
124+
const openOriginal = modal.shadowRoot?.querySelector<HTMLAnchorElement>(".open-original");
125+
video?.focus();
126+
video?.dispatchEvent(
127+
new KeyboardEvent("keydown", { key: "Tab", bubbles: true, composed: true }),
128+
);
129+
expect(modal.shadowRoot?.activeElement).toBe(openOriginal);
130+
openOriginal?.dispatchEvent(
131+
new KeyboardEvent("keydown", { key: "Tab", shiftKey: true, bubbles: true, composed: true }),
132+
);
133+
expect(modal.shadowRoot?.activeElement).toBe(video);
134+
});
135+
95136
it("accepts parameters on safe raster MIME types", async () => {
96137
fetchImage.mockResolvedValueOnce({
97138
blob: async () => new Blob(["png"], { type: "image/png;charset=utf-8" }),

‎ui/src/components/image-lightbox.ts‎

Lines changed: 78 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import { icons } from "./icons.ts";
77
import "./modal-dialog.ts";
88

99
export type ImageLightboxItem = {
10+
kind?: "image" | "video";
1011
src: string;
12+
originalSrc?: string;
1113
title: string;
1214
release?: () => void;
1315
};
@@ -33,11 +35,13 @@ function dataUrlMimeType(source: string): string | undefined {
3335
}
3436

3537
class OpenClawImageLightbox extends OpenClawLitElement {
38+
@property() mediaKind: "image" | "video" = "image";
3639
@property() src = "";
40+
@property() originalSrc = "";
3741
@property({ attribute: false }) imageTitle = "";
3842
@query(".stage") private stage?: HTMLDivElement;
3943
@query(".image") private image?: HTMLImageElement;
40-
@queryAll(".action") private actions!: NodeListOf<HTMLElement>;
44+
@queryAll(".action, video[controls]") private focusables!: NodeListOf<HTMLElement>;
4145
@state() private openOriginalUrl = "";
4246
@state() private scale = 1;
4347
@state() private imageReady = false;
@@ -182,19 +186,28 @@ class OpenClawImageLightbox extends OpenClawLitElement {
182186
overflow: hidden;
183187
}
184188
185-
.image {
189+
.image,
190+
.video {
186191
display: block;
187192
min-width: 0;
188193
min-height: 0;
189194
max-width: 100%;
190195
max-height: 100%;
191-
width: auto;
192196
height: auto;
193197
object-fit: contain;
198+
}
199+
200+
.image {
201+
width: auto;
194202
cursor: zoom-in;
195203
-webkit-user-drag: none;
196204
}
197205
206+
.video {
207+
width: min(1280px, 100%);
208+
background: var(--media-bg);
209+
}
210+
198211
.image.zoomed {
199212
cursor: grab;
200213
}
@@ -306,21 +319,28 @@ class OpenClawImageLightbox extends OpenClawLitElement {
306319
}
307320

308321
protected override updated(changed: PropertyValues<this>) {
309-
if (changed.has("src")) {
322+
if (changed.has("src") || changed.has("originalSrc") || changed.has("mediaKind")) {
310323
this.destroyPanzoom();
311324
this.scale = 1;
312-
this.imageReady = false;
313325
void this.resolveOriginalUrl();
314326
}
315327
}
316328

317329
override render() {
318330
const title = this.imageTitle.trim() || t("chat.imageLightbox.untitled");
331+
const dialogLabel =
332+
this.mediaKind === "video"
333+
? t("chat.mediaPlayer.videoPreview", { title })
334+
: t("chat.imageLightbox.label", { title });
335+
const closeLabel =
336+
this.mediaKind === "video"
337+
? t("chat.mediaPlayer.closeVideoPreview")
338+
: t("chat.imageLightbox.close");
319339
const canZoom = this.imageReady && this.panzoom !== undefined;
320340
return html`
321341
<openclaw-modal-dialog
322342
class="mobile-edge-to-edge viewport-edge-to-edge"
323-
label=${t("chat.imageLightbox.label", { title })}
343+
label=${dialogLabel}
324344
@modal-cancel=${this.emitClose}
325345
@keydown=${this.handleKeydown}
326346
>
@@ -350,7 +370,7 @@ class OpenClawImageLightbox extends OpenClawLitElement {
350370
class="action close"
351371
type="button"
352372
autofocus
353-
aria-label=${t("chat.imageLightbox.close")}
373+
aria-label=${closeLabel}
354374
@click=${this.emitClose}
355375
>
356376
${icons.x}
@@ -364,44 +384,56 @@ class OpenClawImageLightbox extends OpenClawLitElement {
364384
@pointercancel=${this.resetBackdropPointer}
365385
@dblclick=${this.handleDoubleClick}
366386
>
367-
<img
368-
class=${this.scale > 1 ? "image zoomed" : "image"}
369-
src=${this.src}
370-
alt=${title}
371-
@load=${this.handleImageLoad}
372-
@error=${this.handleImageError}
373-
@dragstart=${(event: DragEvent) => event.preventDefault()}
374-
/>
375-
</div>
376-
<div class="zoom-controls">
377-
<button
378-
class="action zoom-control"
379-
type="button"
380-
aria-label=${t("chat.imageLightbox.zoomOut")}
381-
?disabled=${!canZoom || this.scale <= 1}
382-
@click=${this.zoomOut}
383-
>
384-
−
385-
</button>
386-
<button
387-
class="action zoom-control zoom-level"
388-
type="button"
389-
aria-label=${t("chat.imageLightbox.resetZoom")}
390-
?disabled=${!canZoom || this.scale === 1}
391-
@click=${this.resetZoom}
392-
>
393-
${Math.round(this.scale * 100)}%
394-
</button>
395-
<button
396-
class="action zoom-control"
397-
type="button"
398-
aria-label=${t("chat.imageLightbox.zoomIn")}
399-
?disabled=${!canZoom || this.scale >= MAX_SCALE}
400-
@click=${this.zoomIn}
401-
>
402-
+
403-
</button>
387+
${this.mediaKind === "video"
388+
? html`<video
389+
class="video"
390+
src=${this.src}
391+
aria-label=${title}
392+
controls
393+
autoplay
394+
playsinline
395+
tabindex="0"
396+
></video>`
397+
: html`<img
398+
class=${this.scale > 1 ? "image zoomed" : "image"}
399+
src=${this.src}
400+
alt=${title}
401+
@load=${this.handleImageLoad}
402+
@error=${this.handleImageError}
403+
@dragstart=${(event: DragEvent) => event.preventDefault()}
404+
/>`}
404405
</div>
406+
${this.mediaKind === "image"
407+
? html`<div class="zoom-controls">
408+
<button
409+
class="action zoom-control"
410+
type="button"
411+
aria-label=${t("chat.imageLightbox.zoomOut")}
412+
?disabled=${!canZoom || this.scale <= 1}
413+
@click=${this.zoomOut}
414+
>
415+
−
416+
</button>
417+
<button
418+
class="action zoom-control zoom-level"
419+
type="button"
420+
aria-label=${t("chat.imageLightbox.resetZoom")}
421+
?disabled=${!canZoom || this.scale === 1}
422+
@click=${this.resetZoom}
423+
>
424+
${Math.round(this.scale * 100)}%
425+
</button>
426+
<button
427+
class="action zoom-control"
428+
type="button"
429+
aria-label=${t("chat.imageLightbox.zoomIn")}
430+
?disabled=${!canZoom || this.scale >= MAX_SCALE}
431+
@click=${this.zoomIn}
432+
>
433+
+
434+
</button>
435+
</div>`
436+
: nothing}
405437
</section>
406438
</openclaw-modal-dialog>
407439
`;
@@ -550,7 +582,7 @@ class OpenClawImageLightbox extends OpenClawLitElement {
550582
private async resolveOriginalUrl() {
551583
const request = ++this.originalUrlRequest;
552584
this.revokeOriginalBlobUrl();
553-
const source = this.src.trim();
585+
const source = (this.originalSrc || this.src).trim();
554586
if (!source) {
555587
this.openOriginalUrl = "";
556588
return;
@@ -609,7 +641,7 @@ class OpenClawImageLightbox extends OpenClawLitElement {
609641
if (event.key !== "Tab") {
610642
return;
611643
}
612-
const actions = [...this.actions].filter(
644+
const actions = [...this.focusables].filter(
613645
(action) => !(action instanceof HTMLButtonElement && action.disabled),
614646
);
615647
const first = actions[0];

‎ui/src/i18n/locales/en.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5917,6 +5917,9 @@ export const en: TranslationMap = {
59175917
volume: "Volume",
59185918
download: "Download {filename}",
59195919
preparing: "Preparing playback…",
5920+
openVideo: "Expand {filename} in the media overlay",
5921+
videoPreview: "Video preview: {title}",
5922+
closeVideoPreview: "Close video preview",
59205923
},
59215924
modelControls: {
59225925
default: "Default",

‎ui/src/pages/chat/chat-state-page.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { handleSendChat } from "./chat-send-submit.ts";
3030
import { OFFLINE_QUEUE_STORAGE_ERROR } from "./chat-send-support.ts";
3131
import { retireChatModelSelectionOwnership } from "./chat-session.ts";
3232
import type { ChatPageHost } from "./chat-state-host.ts";
33+
import { safeMediaAttachmentHref } from "./components/chat-attachment-href.ts";
3334
import {
3435
handleChatDraftChange,
3536
handleChatInputHistoryKey,
@@ -447,14 +448,22 @@ export function createPageState(
447448
item.release?.();
448449
return;
449450
}
450-
const safeSrc = resolveSafeExternalUrl(item.src, window.location.href, {
451-
allowDataImage: true,
452-
});
453-
if (!safeSrc) {
451+
const video = item.kind === "video";
452+
const resolveSrc = (src: string) =>
453+
video
454+
? safeMediaAttachmentHref(src, "video")
455+
: resolveSafeExternalUrl(src, window.location.href, { allowDataImage: true });
456+
const safeSrc = resolveSrc(item.src);
457+
const safeOriginalSrc = item.originalSrc ? resolveSrc(item.originalSrc) : undefined;
458+
if (!safeSrc || (item.originalSrc && !safeOriginalSrc)) {
454459
item.release?.();
455460
return;
456461
}
457-
state.imageLightbox = { ...item, src: safeSrc };
462+
state.imageLightbox = {
463+
...item,
464+
src: safeSrc,
465+
...(safeOriginalSrc ? { originalSrc: safeOriginalSrc } : {}),
466+
};
458467
renderLifecycle.invalidate();
459468
};
460469
state.handleCloseImage = () => {

‎ui/src/pages/chat/chat-state.test.ts‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ReactiveController, ReactiveControllerHost } from "lit";
1+
import { render, type ReactiveController, type ReactiveControllerHost } from "lit";
22
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
33
import { createDeferred } from "../../../../test/helpers/promise.js";
44
import type { GatewayBrowserClient } from "../../api/gateway.ts";
@@ -25,6 +25,7 @@ import {
2525
} from "./chat-state-refresh.ts";
2626
import { resolveChatAvatarUrl, selectedChatSessionRow } from "./chat-state-route.ts";
2727
import { buildChatItems } from "./chat-thread-build.ts";
28+
import { renderAssistantAttachments } from "./components/chat-message-attachments.ts";
2829
import { getChatSessionProjection, reduceChatSessionProjection } from "./history-merge.ts";
2930
import { scheduleControlUiAfterPaint } from "./performance.ts";
3031
import { applySessionMessagePayload } from "./session-message-apply.ts";
@@ -3109,6 +3110,53 @@ describe("session pull request refresh", () => {
31093110
});
31103111

31113112
describe("image lightbox lifecycle", () => {
3113+
it("accepts only matching base64 video at the page boundary", () => {
3114+
const context = {
3115+
agents: { state: { agentsList: null }, ensureList: vi.fn(async () => null) },
3116+
agentSelection: { state: { selectedId: "main" } },
3117+
basePath: "",
3118+
config: {
3119+
current: {
3120+
allowExternalEmbedUrls: false,
3121+
assistantIdentity: { name: "Assistant" },
3122+
embedSandboxMode: "scripts",
3123+
localMediaPreviewRoots: [],
3124+
},
3125+
},
3126+
initialUserMessage: createInitialUserMessageHandoff(),
3127+
sessions: {},
3128+
} as unknown as ApplicationContext;
3129+
const state = createPageState(
3130+
context,
3131+
{ invalidate: vi.fn(), afterCommit: () => () => {} },
3132+
{ querySelector: () => null },
3133+
);
3134+
3135+
const source = "data:video/mp4;base64,AAAA";
3136+
const container = document.body.appendChild(document.createElement("div"));
3137+
render(
3138+
renderAssistantAttachments(
3139+
[
3140+
{
3141+
type: "attachment",
3142+
attachment: { kind: "video", label: "Clip", mimeType: "video/mp4", url: source },
3143+
},
3144+
],
3145+
{ onOpenImage: state.handleOpenImage },
3146+
),
3147+
container,
3148+
);
3149+
const player = container.querySelector("openclaw-chat-video-player") as HTMLElement & {
3150+
onExpand: (src: string) => void;
3151+
};
3152+
player.onExpand(source);
3153+
expect(state.imageLightbox?.src).toBe("data:video/mp4;base64,AAAA");
3154+
3155+
state.handleOpenImage({ kind: "video", src: "data:audio/mp3;base64,AAAA", title: "Audio" });
3156+
expect(state.imageLightbox).toBeNull();
3157+
container.remove();
3158+
});
3159+
31123160
it("invalidates immediately when beginning a deferred image open", () => {
31133161
const invalidate = vi.fn();
31143162
const context = {

‎ui/src/pages/chat/components/chat-attachment-card.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type AttachmentCardHeaderOptions = {
2020
mimeType?: string;
2121
sizeBytes?: number;
2222
downloadHref?: string;
23+
expandLabel?: string;
2324
onExpand?: () => void;
2425
visualMode?: AttachmentFileVisualMode;
2526
voiceNote?: boolean;
@@ -95,6 +96,8 @@ export function renderAttachmentCardHeader(options: AttachmentCardHeaderOptions)
9596
const metadata = [typeLabel, formattedSize].filter(Boolean).join(" · ");
9697
const downloadTitle = t("chat.mediaPlayer.download", { filename: options.label });
9798
const hasOpenAction = options.onExpand !== undefined;
99+
const expandLabel =
100+
options.expandLabel ?? t("chat.attachments.expand", { filename: options.label });
98101
const downloadClass = `chat-assistant-attachment-card__action chat-assistant-attachment-card__download chat-assistant-attachment-card__download--ghost ${
99102
hasOpenAction ? "chat-assistant-attachment-card__download--secondary" : ""
100103
}`;
@@ -151,8 +154,8 @@ export function renderAttachmentCardHeader(options: AttachmentCardHeaderOptions)
151154
class="chat-assistant-attachment-card__action ${compactPreview
152155
? "chat-assistant-attachment-card__expand--icon"
153156
: "chat-assistant-attachment-card__action--labeled"} chat-assistant-attachment-card__expand"
154-
aria-label=${t("chat.attachments.expand", { filename: options.label })}
155-
title=${t("chat.attachments.expand", { filename: options.label })}
157+
aria-label=${expandLabel}
158+
title=${expandLabel}
156159
@click=${options.onExpand}
157160
>
158161
${compactPreview

0 commit comments

Comments
 (0)