fix(gui-plugins): drive the artifact viewer, image pan, video overlay and select preview through state machines (#61993)

This commit is contained in:
Luke Parker
2026-11-05 01:04:36 -06:00
committed by GitHub
Unverified
parent 491c8cd754
commit c605ac34f8
9 changed files with 1289 additions and 228 deletions
@@ -125,6 +125,32 @@ story("select fields come in the text input sizes and default to the inline trig
await expect(fields.nth(3)).toHaveCSS("box-shadow", "none")
})
story(
"a select previews the highlighted option, reverts it on Escape, and keeps the chosen one",
async ({ mount, page }) => {
const root = await mount("ui-select--highlight-preview")
const trigger = root.locator('[data-component="select-v2"]')
const preview = root.locator('[data-slot="select-preview"]')
await trigger.click()
await page.getByRole("option", { name: "Cherry", exact: true }).hover()
await expect(preview).toHaveText("Showing Cherry")
await page.keyboard.press("Escape")
await expect(page.getByRole("listbox")).toHaveCount(0)
await expect(preview).toHaveText("Showing Apple")
await trigger.click()
const banana = page.getByRole("option", { name: "Banana", exact: true })
await banana.hover()
await expect(preview).toHaveText("Showing Banana")
await banana.click()
await expect(page.getByRole("listbox")).toHaveCount(0)
await expect(trigger).toContainText("Banana")
// Choosing the previewed option must not run its undo afterwards.
await expect(preview).toHaveText("Showing Banana")
},
)
story("compact checkboxes use the design system control states and focus ring", async ({ mount, page }) => {
const root = await mount("ui-checkbox--compact")
+33 -1
View File
@@ -889,6 +889,7 @@ test("artifact actions copy and download content, switch source, and follow the
await context.grantPermissions(["clipboard-read", "clipboard-write"])
const source = '<svg xmlns="http://www.w3.org/2000/svg" width="120" height="80"><rect width="120" height="80"/></svg>'
const table = 'name,role\n"Jensen, Oliver",Researcher\nIsabella,Support'
const broken = '<svg xmlns="http://www.w3.org/2000/svg"><rect'
const notes = [
"---",
@@ -923,6 +924,8 @@ test("artifact actions copy and download content, switch source, and follow the
if (path === "notes.md") return { content: notes }
if (path === "broken.svg") return { content: broken }
return { type: "binary", content: "AAEC", encoding: "base64" }
},
pageMessages: () => ({
@@ -935,7 +938,7 @@ test("artifact actions copy and download content, switch source, and follow the
content: [
{
type: "text",
text: "---\ndescription: Chat heading\n---\n\n[Vector](vector.svg) [Table](data.csv) [Notes](notes.md) [Binary](sample.bin)",
text: "---\ndescription: Chat heading\n---\n\n[Vector](vector.svg) [Table](data.csv) [Notes](notes.md) [Binary](sample.bin) [Broken](broken.svg)",
},
],
time: { created: 1, completed: 2 },
@@ -1071,6 +1074,13 @@ test("artifact actions copy and download content, switch source, and follow the
await expect(page.getByRole("menuitem", { name: "Download", exact: true })).toHaveCount(0)
await page.getByRole("menuitem", { name: "Copy path", exact: true }).click()
await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe(`${workspace.directory}/sample.bin`)
// A text SVG that fails to decode shows the binary placeholder but can still show its source.
await page.getByRole("link", { name: "Broken", exact: true }).click()
await expect(toolbar.getByText("broken.svg", { exact: true })).toBeVisible()
await expect(panel.getByText("Couldn't load this file.", { exact: true })).toBeVisible()
await toolbar.getByRole("button", { name: "View source", exact: true }).click()
await expect(panel.locator("diffs-container")).toContainText(broken)
})
test("Markdown and DOCX sheets stay aligned when switching at wide and narrow preview widths", async ({ page }) => {
@@ -1320,6 +1330,28 @@ test("image gestures anchor zoom, pan, and reset independently of the pane", asy
await expect(stage).toHaveAttribute("data-zoom", "actual")
expect((await image.boundingBox())!.x).toBeCloseTo(scrolled.x + 30, 1)
expect((await image.boundingBox())!.y).toBeCloseTo(scrolled.y + 20, 1)
// A pan whose release the image never heard ends on the next move without the button, and pans nothing.
const touched = (await image.boundingBox())!
await page.mouse.down()
await expect(image).toHaveCSS("cursor", "grabbing")
await image.evaluate(
(element, point) =>
element.dispatchEvent(
new PointerEvent("pointermove", {
bubbles: true,
pointerId: 1,
pointerType: "mouse",
isPrimary: true,
buttons: 0,
clientX: point.x,
clientY: point.y,
}),
),
{ x: anchor.x - 100, y: anchor.y - 100 },
)
await expect(image).toHaveCSS("cursor", "grab")
expect(await image.boundingBox()).toEqual(touched)
await page.mouse.up()
// Keyboard-accessible fit restores the exact original geometry.
await fit.focus()
await page.keyboard.press("Enter")
@@ -0,0 +1,278 @@
import { describe, expect, test } from "bun:test"
import type { FileContent } from "@opencode/plugin/gui"
import {
createArtifactViewerState,
createVideoOverlayState,
INITIAL_IMAGE_STAGE_GESTURE_STATE,
selectArtifactInfo,
selectArtifactMode,
selectArtifactPreviewable,
selectArtifactReason,
selectArtifactShowingPreview,
selectArtifactStageKind,
selectStageClickSuppressed,
selectStageCursor,
selectVideoControlsVisible,
selectVideoLoading,
transitionArtifactViewer,
transitionImageStageGesture,
transitionVideoOverlay,
type ArtifactViewerEvent,
type ArtifactViewerState,
type ImageStageGestureEvent,
type ImageStageGestureState,
type ImageStageGestureTransition,
type VideoOverlayEvent,
type VideoOverlayState,
type VideoOverlayTransition,
} from "./artifact-view-machine"
const svg = (content: string): FileContent => ({ type: "text", content })
describe("artifact viewer", () => {
const run = (state: ArtifactViewerState, content: FileContent, ...events: ArtifactViewerEvent[]) =>
events.reduce((value, event) => transitionArtifactViewer(value, content, event), state)
test("a broken SVG keeps its source action and can still show its source", () => {
// Before: a failed decode turned the kind into binary, which removed "View source" for a text file.
const content = svg("<svg")
const failed = run(createArtifactViewerState(content), content, {
type: "decode.failed",
content,
reason: "Failed to load",
})
expect(selectArtifactStageKind(failed, "broken.svg", content)).toBe("binary")
expect(selectArtifactPreviewable("broken.svg", content)).toBe(true)
expect(selectArtifactReason(failed, content)).toBe("Failed to load")
const source = run(failed, content, { type: "mode.changed", mode: "source" })
expect(selectArtifactMode(source, content)).toBe("source")
expect(selectArtifactShowingPreview(source, "broken.svg", content)).toBe(false)
const back = run(source, content, { type: "mode.changed", mode: "preview" })
expect(selectArtifactShowingPreview(back, "broken.svg", content)).toBe(true)
expect(selectArtifactReason(back, content)).toBe("Failed to load")
expect(selectArtifactInfo(back, content)).toEqual({})
})
test("a failure drops reported facts, and facts after a failure change nothing", () => {
const content = svg("<svg/>")
const reported = run(createArtifactViewerState(content), content, {
type: "info.reported",
content,
info: { width: 2, height: 3 },
})
expect(selectArtifactInfo(reported, content)).toEqual({ width: 2, height: 3 })
const failed = run(reported, content, { type: "decode.failed", content })
expect(selectArtifactInfo(failed, content)).toEqual({})
expect(run(failed, content, { type: "info.reported", content, info: { width: 2 } })).toBe(failed)
expect(run(failed, content, { type: "decode.failed", content })).toBe(failed)
})
test("binary files offer no source", () => {
const content: FileContent = { type: "binary", content: "AAEC", encoding: "base64" }
expect(selectArtifactPreviewable("sample.bin", content)).toBe(false)
expect(selectArtifactShowingPreview(createArtifactViewerState(content), "sample.bin", content)).toBe(true)
expect(selectArtifactStageKind(createArtifactViewerState(content), "sample.bin", content)).toBe("binary")
})
test("results for an earlier content change nothing, and a reread starts from the preview", () => {
// Before: a late failure for the previous content was stamped onto the reread one.
const first = svg("<svg/>")
const second = svg("<svg></svg>")
const source = run(createArtifactViewerState(first), first, { type: "mode.changed", mode: "source" })
const failed = run(source, first, { type: "decode.failed", content: first })
expect(selectArtifactMode(failed, second)).toBe("preview")
expect(selectArtifactStageKind(failed, "a.svg", second)).toBe("svg")
const late = run(
failed,
second,
{ type: "decode.failed", content: first, reason: "Failed to load" },
{ type: "info.reported", content: first, info: { width: 1, height: 1 } },
)
expect(late.content).toBe(second)
expect(selectArtifactStageKind(late, "a.svg", second)).toBe("svg")
expect(selectArtifactInfo(late, second)).toEqual({})
})
})
describe("image stage gesture", () => {
const down = (pointerId = 1, zoomed = true): ImageStageGestureEvent => ({
type: "pointer.down",
pointerId,
button: 0,
primary: true,
zoomed,
})
const run = (state: ImageStageGestureState, ...events: ImageStageGestureEvent[]) =>
events.reduce<ImageStageGestureTransition>((step, event) => transitionImageStageGesture(step.state, event), {
state,
effects: [],
})
test("a moved pan swallows its click, even after the browser releases capture", () => {
const released = run(
INITIAL_IMAGE_STAGE_GESTURE_STATE,
down(),
{ type: "pointer.moved", pointerId: 1 },
{ type: "pointer.up", pointerId: 1 },
{ type: "pointer.cancel", pointerId: 1 },
)
expect(selectStageClickSuppressed(released.state)).toBe(true)
const clicked = run(released.state, { type: "click" })
expect(clicked.state).toEqual({ status: "idle" })
expect(selectStageClickSuppressed(clicked.state)).toBe(false)
})
test("a cancelled pan, or a touch pan with no click, does not swallow the next click", () => {
// Before: the moved flag outlived a pointercancel and ate the next real click.
const cancelled = run(
INITIAL_IMAGE_STAGE_GESTURE_STATE,
down(),
{ type: "pointer.moved", pointerId: 1 },
{ type: "pointer.cancel", pointerId: 1 },
)
expect(cancelled.effects).toEqual([{ type: "pointer.release", pointerId: 1 }])
expect(selectStageClickSuppressed(cancelled.state)).toBe(false)
const touched = run(
INITIAL_IMAGE_STAGE_GESTURE_STATE,
down(),
{ type: "pointer.moved", pointerId: 1 },
{ type: "pointer.up", pointerId: 1 },
down(2, false),
)
expect(touched.state).toEqual({ status: "idle" })
})
test("a press that never moved leaves clicks alone", () => {
const pressed = run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down(), { type: "pointer.up", pointerId: 1 })
expect(selectStageClickSuppressed(pressed.state)).toBe(false)
})
test("zooming back to fit ends the pan and releases the pointer", () => {
// Before: a Ctrl+wheel zoom out or Fit mid-drag left the drag active at fit.
const panning = run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down())
const fitted = run(panning.state, { type: "zoom.changed", zoomed: false })
expect(fitted.state).toEqual({ status: "idle" })
expect(fitted.effects).toEqual([{ type: "pointer.release", pointerId: 1 }])
expect(selectStageCursor(panning.state, true)).toBe("grabbing")
expect(selectStageCursor(fitted.state, true)).toBe("grab")
expect(run(panning.state, { type: "zoom.changed", zoomed: true }).state).toBe(panning.state)
})
test("only a primary press on a zoomed image pans, and a second pointer leaves it alone", () => {
expect(run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down(1, false)).state).toBe(INITIAL_IMAGE_STAGE_GESTURE_STATE)
const panning = run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down())
expect(panning.effects).toEqual([{ type: "pointer.capture", pointerId: 1 }])
expect(run(panning.state, down(2)).state).toBe(panning.state)
expect(run(panning.state, { type: "pointer.up", pointerId: 2 }).state).toBe(panning.state)
})
test("a pan that has moved keeps its state for further moves", () => {
const moved = run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down(), { type: "pointer.moved", pointerId: 1 })
expect(run(moved.state, { type: "pointer.moved", pointerId: 1 }).state).toBe(moved.state)
})
test("disposal releases a pan and ignores everything after", () => {
const disposed = run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down(), { type: "disposed" })
expect(disposed.effects).toEqual([{ type: "pointer.release", pointerId: 1 }])
expect(run(disposed.state, down()).state).toBe(disposed.state)
})
})
describe("video overlay", () => {
const run = (state: VideoOverlayState, ...events: VideoOverlayEvent[]) =>
events.reduce<VideoOverlayTransition>((step, event) => transitionVideoOverlay(step.state, event), {
state,
effects: [],
})
const paused = run(createVideoOverlayState(), { type: "media.loaded", paused: true }).state
test("loading hides the controls until the first frame", () => {
expect(selectVideoLoading(createVideoOverlayState())).toBe(true)
expect(selectVideoControlsVisible(createVideoOverlayState())).toBe(false)
expect(selectVideoLoading(paused)).toBe(false)
expect(selectVideoControlsVisible(paused)).toBe(true)
})
test("playing starts a fresh idle timer, so a timer armed while paused cannot hide the controls", () => {
// Before: a pointer move while paused armed a timer that hid the controls shortly after Play.
const moved = transitionVideoOverlay(paused, { type: "pointer.moved" })
expect(moved.state).toBe(paused)
expect(moved.effects).toEqual([])
const playing = run(paused, { type: "playback.changed", paused: false })
expect(playing.effects).toEqual([{ type: "idle.schedule", attempt: 1, delayMs: 2_500 }])
expect(selectVideoControlsVisible(playing.state)).toBe(true)
expect(run(playing.state, { type: "idle.elapsed", attempt: 0 }).state).toBe(playing.state)
const idle = run(playing.state, { type: "idle.elapsed", attempt: 1 })
expect(selectVideoControlsVisible(idle.state)).toBe(false)
expect(run(idle.state, { type: "pointer.moved" }).effects).toEqual([
{ type: "idle.schedule", attempt: 2, delayMs: 2_500 },
])
})
test("pausing or leaving clears the idle timer", () => {
const playing = run(paused, { type: "playback.changed", paused: false }).state
expect(run(playing, { type: "playback.changed", paused: true }).effects).toEqual([{ type: "idle.clear" }])
const left = run(playing, { type: "pointer.left" })
expect(left.effects).toEqual([{ type: "idle.clear" }])
expect(selectVideoControlsVisible(left.state)).toBe(false)
})
test("the controls stay hidden while the preview flies the video back", () => {
// Before: closing the preview showed the fullscreen button at once, over a stage the video had not returned to.
const expanded = run(paused, { type: "expand.requested", canExpand: true })
expect(expanded.effects).toEqual([{ type: "preview.open", attempt: 1 }])
expect(selectVideoControlsVisible(expanded.state)).toBe(false)
expect(run(expanded.state, { type: "expand.requested", canExpand: true }).state).toBe(expanded.state)
expect(run(expanded.state, { type: "pointer.moved" }).state).toBe(expanded.state)
const playing = run(expanded.state, { type: "playback.changed", paused: false })
expect(selectVideoControlsVisible(playing.state)).toBe(false)
expect(run(playing.state, { type: "preview.returned", attempt: 0 }).state).toBe(playing.state)
const back = run(playing.state, { type: "preview.returned", attempt: 1 })
expect(back.state).toEqual({ status: "playing-idle", issued: 1 })
expect(back.effects).toEqual([{ type: "preview.release", attempt: 1 }])
expect(run(back.state, { type: "preview.returned", attempt: 1 }).state).toBe(back.state)
})
test("a video without a frame, or still loading, does not expand", () => {
expect(run(paused, { type: "expand.requested", canExpand: false }).state).toBe(paused)
const loading = createVideoOverlayState()
expect(run(loading, { type: "expand.requested", canExpand: true }).state).toBe(loading)
})
test("unmounting closes an open preview and clears a running timer", () => {
// Before: a closed tab left its preview dialog open with no owner.
const expanded = run(paused, { type: "expand.requested", canExpand: true }).state
expect(run(expanded, { type: "disposed" }).effects).toEqual([{ type: "preview.close", attempt: 1 }])
const playing = run(paused, { type: "playback.changed", paused: false }).state
const disposed = run(playing, { type: "disposed" })
expect(disposed.effects).toEqual([{ type: "idle.clear" }])
expect(run(disposed.state, { type: "idle.elapsed", attempt: 1 }).state).toBe(disposed.state)
})
})
@@ -0,0 +1,395 @@
import type { FileContent } from "@opencode/plugin/gui"
import { artifactKind, type ArtifactKind } from "@opencode/util/artifact"
export type ArtifactMode = "preview" | "source"
/** Facts a viewer learns from the decoded media, shown in the toolbar. */
export type ArtifactInfo = {
readonly width?: number
readonly height?: number
readonly duration?: number
readonly rows?: number
readonly columns?: number
/** Facts a plugin's viewer reports, already localized. */
readonly details?: readonly string[]
}
/**
* The viewer of one loaded content: a reread file starts from the preview again. `undecodable` is a file its viewer
* could not decode; it shows the binary placeholder in preview and keeps the mode, so a broken SVG can still show its
* source.
*/
export type ArtifactViewerState =
| { readonly status: "preview" | "source"; readonly content: FileContent; readonly info: ArtifactInfo }
| {
readonly status: "undecodable"
readonly content: FileContent
readonly mode: ArtifactMode
/** Why the file shows as binary, when a viewer said. */
readonly reason?: string
}
/** Results name the content they decoded, so a late one from a file since reread changes nothing. */
export type ArtifactViewerEvent =
| { readonly type: "mode.changed"; readonly mode: ArtifactMode }
| { readonly type: "info.reported"; readonly content: FileContent; readonly info: ArtifactInfo }
| { readonly type: "decode.failed"; readonly content: FileContent; readonly reason?: string }
const EMPTY_INFO: ArtifactInfo = Object.freeze({})
/** Kinds that render a preview from their text and can toggle back to highlighted source. */
const previewableKinds: readonly ArtifactKind[] = ["svg", "html", "markdown", "mermaid", "table"]
export function createArtifactViewerState(content: FileContent): ArtifactViewerState {
return { status: "preview", content, info: EMPTY_INFO }
}
/** `content` is the one shown now; state kept for an earlier content reads as a fresh preview. */
export function transitionArtifactViewer(
state: ArtifactViewerState,
content: FileContent,
event: ArtifactViewerEvent,
): ArtifactViewerState {
const base = current(state, content)
if (event.type === "mode.changed") {
if (selectArtifactMode(base, content) === event.mode) return base
return base.status === "undecodable"
? { ...base, mode: event.mode }
: { status: event.mode, content, info: base.info }
}
if (event.content !== content) return base
if (event.type === "info.reported") {
if (base.status === "undecodable") return base
return { status: base.status, content, info: event.info }
}
if (base.status === "undecodable" && base.reason === event.reason) return base
// A file that fails also drops the facts its viewer reported, such as a page count.
return { status: "undecodable", content, mode: selectArtifactMode(base, content), reason: event.reason }
}
/** Text of a kind that previews from it can switch to its source, also once its preview failed to decode. */
export function selectArtifactPreviewable(path: string, content: FileContent) {
return content.type !== "binary" && previewableKinds.includes(artifactKind(path))
}
export function selectArtifactMode(state: ArtifactViewerState, content: FileContent): ArtifactMode {
const value = current(state, content)
return value.status === "undecodable" ? value.mode : value.status
}
export function selectArtifactShowingPreview(state: ArtifactViewerState, path: string, content: FileContent) {
return !selectArtifactPreviewable(path, content) || selectArtifactMode(state, content) === "preview"
}
/** The viewer the stage renders; `binary` is the placeholder. */
export function selectArtifactStageKind(
state: ArtifactViewerState,
path: string,
content: FileContent,
): ArtifactKind | "binary" {
if (content.type === "binary" && !content.mimeType) return "binary"
if (current(state, content).status === "undecodable") return "binary"
return artifactKind(path)
}
export function selectArtifactInfo(state: ArtifactViewerState, content: FileContent) {
const value = current(state, content)
return value.status === "undecodable" ? EMPTY_INFO : value.info
}
export function selectArtifactReason(state: ArtifactViewerState, content: FileContent) {
const value = current(state, content)
return value.status === "undecodable" ? value.reason : undefined
}
function current(state: ArtifactViewerState, content: FileContent) {
return state.content === content ? state : createArtifactViewerState(content)
}
/** How far a press moves before it pans rather than clicks. */
export const STAGE_DRAG_THRESHOLD_PX = 3
/**
* The press on a zoomed image. `suppress-click` follows a pan that moved: the click that ends it neither fits nor
* zooms. It lasts until that click or the next press, since a touch pan produces no click. Pan offsets live in the
* driver and paint straight to the image; once a pan has moved, its pointer moves dispatch nothing at all.
*/
export type ImageStageGestureState =
| { readonly status: "idle" }
| { readonly status: "panning"; readonly pointerId: number; readonly moved: boolean }
| { readonly status: "suppress-click" }
| { readonly status: "disposed" }
/** `zoomed` is whether the image shows above fit. */
export type ImageStageGestureEvent =
| {
readonly type: "pointer.down"
readonly pointerId: number
readonly button: number
readonly primary: boolean
readonly zoomed: boolean
}
/** The pan pointer moved past the threshold; the driver sends it once per pan. */
| { readonly type: "pointer.moved"; readonly pointerId: number }
| { readonly type: "pointer.up"; readonly pointerId: number }
/** `pointercancel`, `lostpointercapture`, or a mouse move with the primary button already up. */
| { readonly type: "pointer.cancel"; readonly pointerId: number }
/** Fit, a click, a pinch or a wheel zoom changed whether the image shows above fit. */
| { readonly type: "zoom.changed"; readonly zoomed: boolean }
| { readonly type: "click" }
| { readonly type: "disposed" }
export type ImageStageGestureEffect =
| { readonly type: "pointer.capture"; readonly pointerId: number }
| { readonly type: "pointer.release"; readonly pointerId: number }
export type ImageStageGestureTransition = {
readonly state: ImageStageGestureState
readonly effects: readonly ImageStageGestureEffect[]
}
const STAGE_IDLE: ImageStageGestureState = Object.freeze({ status: "idle" })
const STAGE_SUPPRESS_CLICK: ImageStageGestureState = Object.freeze({ status: "suppress-click" })
const STAGE_DISPOSED: ImageStageGestureState = Object.freeze({ status: "disposed" })
const NO_STAGE_EFFECTS: readonly ImageStageGestureEffect[] = Object.freeze([])
export const INITIAL_IMAGE_STAGE_GESTURE_STATE: ImageStageGestureState = STAGE_IDLE
export function transitionImageStageGesture(
state: ImageStageGestureState,
event: ImageStageGestureEvent,
): ImageStageGestureTransition {
if (state.status === "disposed") return unchangedStage(state)
const pan = state.status === "panning" ? state : undefined
if (event.type === "disposed") return { state: STAGE_DISPOSED, effects: releaseStage(pan) }
if (event.type === "pointer.down") {
// A second pointer leaves the pan alone; the same pointer pressing again means its release never arrived.
if (pan && pan.pointerId !== event.pointerId) return unchangedStage(state)
if (event.primary && event.button === 0 && event.zoomed) {
return {
state: { status: "panning", pointerId: event.pointerId, moved: false },
effects: [{ type: "pointer.capture", pointerId: event.pointerId }],
}
}
return state.status === "suppress-click" ? unchangedStage(STAGE_IDLE) : unchangedStage(state)
}
if (event.type === "pointer.moved") {
if (pan?.pointerId !== event.pointerId || pan.moved) return unchangedStage(state)
return { state: { status: "panning", pointerId: pan.pointerId, moved: true }, effects: NO_STAGE_EFFECTS }
}
if (event.type === "pointer.up") {
if (pan?.pointerId !== event.pointerId) return unchangedStage(state)
return unchangedStage(pan.moved ? STAGE_SUPPRESS_CLICK : STAGE_IDLE)
}
// The browser releases capture itself after pointerup; that lostpointercapture lands in suppress-click and changes
// nothing, so the click that follows is still swallowed.
if (event.type === "pointer.cancel") {
if (pan?.pointerId !== event.pointerId) return unchangedStage(state)
return { state: STAGE_IDLE, effects: releaseStage(pan) }
}
if (event.type === "zoom.changed") {
if (!pan || event.zoomed) return unchangedStage(state)
return { state: STAGE_IDLE, effects: releaseStage(pan) }
}
return state.status === "suppress-click" ? unchangedStage(STAGE_IDLE) : unchangedStage(state)
}
/** Whether the pan under way follows `pointerId`. */
export function selectStagePanPointer(state: ImageStageGestureState, pointerId: number) {
return state.status === "panning" && state.pointerId === pointerId
}
/** Whether the pan under way has passed the threshold, so each move pans. */
export function selectStagePanMoved(state: ImageStageGestureState) {
return state.status === "panning" && state.moved
}
/** Whether the next click ends a moved pan, and so neither fits nor zooms. */
export function selectStageClickSuppressed(state: ImageStageGestureState) {
return state.status === "suppress-click"
}
export function selectStageCursor(state: ImageStageGestureState, zoomed: boolean) {
if (!zoomed) return undefined
return state.status === "panning" ? "grabbing" : "grab"
}
function releaseStage(pan: Extract<ImageStageGestureState, { status: "panning" }> | undefined) {
if (!pan) return NO_STAGE_EFFECTS
return [{ type: "pointer.release", pointerId: pan.pointerId } as const]
}
function unchangedStage(state: ImageStageGestureState): ImageStageGestureTransition {
return { state, effects: NO_STAGE_EFFECTS }
}
/** How long playing video keeps its overlay controls after the pointer last moved. */
export const VIDEO_IDLE_MS = 2_500
/**
* The inline video's overlay controls. They show while paused, and while playing until the pointer rests. `expanded`
* lasts from the preview opening until the borrowed video is back in the stage (or the preview is gone), so the
* controls never float over an empty stage while it flies back. `issued` numbers each idle timer and preview: in
* `playing-active` it is the running timer, in `expanded` the open preview.
*/
export type VideoOverlayState =
| { readonly status: "loading"; readonly paused: boolean; readonly issued: number }
| { readonly status: "paused" | "playing-active" | "playing-idle"; readonly issued: number }
| { readonly status: "expanded"; readonly issued: number; readonly paused: boolean }
| { readonly status: "disposed" }
export type VideoOverlayEvent =
| { readonly type: "media.loaded"; readonly paused: boolean }
| { readonly type: "playback.changed"; readonly paused: boolean }
| { readonly type: "pointer.moved" }
| { readonly type: "pointer.left" }
| { readonly type: "idle.elapsed"; readonly attempt: number }
/** `canExpand`: the video has a frame and sits in the stage. */
| { readonly type: "expand.requested"; readonly canExpand: boolean }
/** The video is back in the stage, or the preview closed or was disposed without taking it. */
| { readonly type: "preview.returned"; readonly attempt: number }
| { readonly type: "disposed" }
export type VideoOverlayEffect =
| { readonly type: "idle.schedule"; readonly attempt: number; readonly delayMs: number }
| { readonly type: "idle.clear" }
| { readonly type: "preview.open"; readonly attempt: number }
/** Stops watching for the video's return; the preview closes on its own. */
| { readonly type: "preview.release"; readonly attempt: number }
/** The stage is going away: stop watching and close the preview. */
| { readonly type: "preview.close"; readonly attempt: number }
export type VideoOverlayTransition = {
readonly state: VideoOverlayState
readonly effects: readonly VideoOverlayEffect[]
}
const VIDEO_DISPOSED: VideoOverlayState = Object.freeze({ status: "disposed" })
export function createVideoOverlayState(): VideoOverlayState {
return { status: "loading", paused: true, issued: 0 }
}
export function transitionVideoOverlay(state: VideoOverlayState, event: VideoOverlayEvent): VideoOverlayTransition {
if (state.status === "disposed") return { state, effects: [] }
if (event.type === "disposed") {
if (state.status === "playing-active") return { state: VIDEO_DISPOSED, effects: [{ type: "idle.clear" }] }
if (state.status === "expanded") {
return { state: VIDEO_DISPOSED, effects: [{ type: "preview.close", attempt: state.issued }] }
}
return { state: VIDEO_DISPOSED, effects: [] }
}
if (event.type === "media.loaded") {
if (state.status !== "loading") return { state, effects: [] }
return event.paused ? { state: { status: "paused", issued: state.issued }, effects: [] } : active(state.issued)
}
if (event.type === "playback.changed") {
if (state.status === "loading" || state.status === "expanded") {
return state.paused === event.paused
? { state, effects: [] }
: { state: { ...state, paused: event.paused }, effects: [] }
}
if (event.paused === (state.status === "paused")) return { state, effects: [] }
if (!event.paused) return active(state.issued)
return {
state: { status: "paused", issued: state.issued },
effects: state.status === "playing-active" ? [{ type: "idle.clear" }] : [],
}
}
// Paused, loading or expanded video keeps its controls as they are, so pointer moves start no timers.
if (event.type === "pointer.moved") {
if (state.status !== "playing-active" && state.status !== "playing-idle") return { state, effects: [] }
return active(state.issued)
}
if (event.type === "pointer.left") {
if (state.status !== "playing-active") return { state, effects: [] }
return { state: { status: "playing-idle", issued: state.issued }, effects: [{ type: "idle.clear" }] }
}
if (event.type === "idle.elapsed") {
if (state.status !== "playing-active" || state.issued !== event.attempt) return { state, effects: [] }
return { state: { status: "playing-idle", issued: state.issued }, effects: [] }
}
if (event.type === "expand.requested") {
if (state.status === "loading" || state.status === "expanded" || !event.canExpand) return { state, effects: [] }
const attempt = state.issued + 1
return {
state: { status: "expanded", issued: attempt, paused: state.status === "paused" },
effects: [
...(state.status === "playing-active" ? [{ type: "idle.clear" } as const] : []),
{ type: "preview.open", attempt },
],
}
}
if (state.status !== "expanded" || state.issued !== event.attempt) return { state, effects: [] }
return {
state: { status: state.paused ? "paused" : "playing-idle", issued: state.issued },
effects: [{ type: "preview.release", attempt: state.issued }],
}
}
/** The loading indicator holds the stage until the first frame. */
export function selectVideoLoading(state: VideoOverlayState) {
return state.status === "loading"
}
export function selectVideoControlsVisible(state: VideoOverlayState) {
return state.status === "paused" || state.status === "playing-active"
}
function active(issued: number): VideoOverlayTransition {
const attempt = issued + 1
return {
state: { status: "playing-active", issued: attempt },
effects: [{ type: "idle.schedule", attempt, delayMs: VIDEO_IDLE_MS }],
}
}
+293 -200
View File
@@ -23,7 +23,7 @@ import { Loader } from "@opencode/ui/loader"
import { showToast } from "@opencode/ui/toast"
import { Markdown } from "@opencode/session-ui/markdown"
import { MarkdownProvider, useMarkdown } from "@opencode/session-ui/context/markdown"
import { artifactKind, type ArtifactKind } from "@opencode/util/artifact"
import { artifactKind } from "@opencode/util/artifact"
import { getDirectory, getFilename } from "@opencode/util/path"
import { createKeyed, usePlugin, type FileContent, type MountedSession, type SessionScreen } from "@opencode/plugin/gui"
import {
@@ -36,40 +36,43 @@ import {
type FrontmatterProperty,
} from "./artifact"
import { FileViewer } from "@opencode/plugin/gui/file"
import { uuid } from "@opencode/util/uuid"
import { ArtifactToolbar } from "./artifact-toolbar"
import {
createArtifactViewerState,
createVideoOverlayState,
INITIAL_IMAGE_STAGE_GESTURE_STATE,
selectArtifactInfo,
selectArtifactMode,
selectArtifactPreviewable,
selectArtifactReason,
selectArtifactShowingPreview,
selectArtifactStageKind,
selectStageClickSuppressed,
selectStageCursor,
selectStagePanMoved,
selectStagePanPointer,
selectVideoControlsVisible,
selectVideoLoading,
STAGE_DRAG_THRESHOLD_PX,
transitionArtifactViewer,
transitionImageStageGesture,
transitionVideoOverlay,
type ArtifactInfo,
type ArtifactViewerEvent,
type ImageStageGestureEvent,
type VideoOverlayEvent,
} from "./artifact-view-machine"
type ArtifactMode = "preview" | "source"
/** Facts a viewer learns from the decoded media, shown in the toolbar. */
type ArtifactInfo = {
width?: number
height?: number
duration?: number
rows?: number
columns?: number
/** Facts a plugin's viewer reports, already localized. */
details?: readonly string[]
}
type ViewerState = {
readonly mode: ArtifactMode
readonly info: ArtifactInfo
readonly undecodable: boolean
/** Why the file shows as binary, when a viewer said. */
readonly reason?: string
}
/** Results name the content they came from, so one from a file since reread changes nothing. */
type MediaProps = {
path: string
content: FileContent
onInfo: (info: ArtifactInfo) => void
onInfo: (content: FileContent, info: ArtifactInfo) => void
/** The browser could not decode the bytes; the host falls back to the binary placeholder. */
onError: () => void
onError: (content: FileContent) => void
}
/** Kinds that render a preview from their text and can toggle back to highlighted source. */
const previewableKinds: readonly ArtifactKind[] = ["svg", "html", "markdown", "mermaid", "table"]
/**
* Renders a loaded non-text file: media, documents, and data get a dedicated viewer with a toolbar;
* previewable text kinds can switch to `source`, which the host supplies (its code view).
@@ -84,32 +87,20 @@ export default function ArtifactView(props: {
}) {
const ctx = usePlugin()
const locale = ctx.locale
// Media the browser could not decode falls back to the binary placeholder.
const initial: ViewerState = { mode: "preview", info: {}, undecodable: false }
// The viewer state belongs to one loaded content: a reloaded file starts from the preview again.
const [saved, setSaved] = createSignal({ ...initial, content: props.content })
// The viewer state belongs to one loaded content: a reread file starts from the preview again.
const [viewerState, setViewerState] = createSignal(createArtifactViewerState(props.content))
const state = () => {
const value = saved()
const dispatchViewer = (event: ArtifactViewerEvent) => {
const prev = untrack(viewerState)
const content = untrack(() => props.content)
const next = transitionArtifactViewer(prev, content, event)
return value.content === props.content ? value : initial
if (next !== prev) setViewerState(next)
}
const change = (next: Partial<ViewerState>) => setSaved({ ...state(), ...next, content: props.content })
const kind = createMemo<ArtifactKind | "binary">(() => {
if (props.content.type === "binary" && !props.content.mimeType) return "binary"
if (state().undecodable) return "binary"
return artifactKind(props.path)
})
const previewable = createMemo(() => {
const value = kind()
return value !== "binary" && previewableKinds.includes(value)
})
// Media the browser could not decode falls back to the binary placeholder.
const kind = createMemo(() => selectArtifactStageKind(viewerState(), props.path, props.content))
const previewable = createMemo(() => selectArtifactPreviewable(props.path, props.content))
const table = createMemo(() =>
kind() === "table"
@@ -119,7 +110,10 @@ export default function ArtifactView(props: {
const meta = createMemo(() => {
const parsed = table()
const info: ArtifactInfo = parsed ? { rows: parsed.total, columns: parsed.columns } : state().info
const info: ArtifactInfo = parsed
? { rows: parsed.total, columns: parsed.columns }
: selectArtifactInfo(viewerState(), props.content)
return [
info.duration ? formatDuration(info.duration) : undefined,
@@ -131,10 +125,11 @@ export default function ArtifactView(props: {
].filter((item): item is string => !!item)
})
// A file that fails also drops the facts its viewer reported, such as a page count.
const fail = (reason?: string) => change({ undecodable: true, info: {}, reason })
const media = { onInfo: (info: ArtifactInfo) => change({ info }), onError: () => fail(ctx.t("view.loadFailed")) }
const media = {
onInfo: (content: FileContent, info: ArtifactInfo) => dispatchViewer({ type: "info.reported", content, info }),
onError: (content: FileContent) =>
dispatchViewer({ type: "decode.failed", content, reason: ctx.t("view.loadFailed") }),
}
const size = () => formatBytes(locale.locale(), contentBytes(props.content))
@@ -175,18 +170,22 @@ export default function ArtifactView(props: {
session={props.session}
screen={props.screen}
path={props.path}
text={
props.content.type !== "binary" && previewableKinds.includes(artifactKind(props.path))
? props.content.content
: undefined
}
mode={state().mode}
onModeChange={previewable() ? (mode) => change({ mode }) : undefined}
text={previewable() ? props.content.content : undefined}
mode={selectArtifactMode(viewerState(), props.content)}
onModeChange={previewable() ? (mode) => dispatchViewer({ type: "mode.changed", mode }) : undefined}
meta={meta()}
html={kind() === "html"}
/>
<Show when={!previewable() || state().mode === "preview"} fallback={props.source}>
<Switch fallback={<ArtifactBinary path={props.path} size={size()} reason={state().reason} />}>
<Show when={selectArtifactShowingPreview(viewerState(), props.path, props.content)} fallback={props.source}>
<Switch
fallback={
<ArtifactBinary
path={props.path}
size={size()}
reason={selectArtifactReason(viewerState(), props.content)}
/>
}
>
<Match when={kind() === "image" || kind() === "svg"}>
<ArtifactImage path={props.path} content={props.content} {...media} />
</Match>
@@ -212,8 +211,8 @@ export default function ArtifactView(props: {
path={props.path}
size={size()}
content={props.content}
onDetails={(details) => change({ info: { details } })}
onError={fail}
onDetails={(content, details) => dispatchViewer({ type: "info.reported", content, info: { details } })}
onError={(content, reason) => dispatchViewer({ type: "decode.failed", content, reason })}
/>
)}
</Match>
@@ -244,16 +243,24 @@ function formatDuration(seconds: number) {
return `${minutes}:${String(total % 60).padStart(2, "0")}`
}
function createMediaUrl(content: () => FileContent) {
return createMemo(() => {
const streamed = content().url
/**
* The URL media loads from, with the content it was made for. A reread that streams from the same URL keeps the
* source, so media already showing is not reloaded.
*/
function createMediaSource(content: () => FileContent) {
return createMemo(
() => {
const value = content()
if (streamed) return streamed
const value = blobUrlFromContent(content())
onCleanup(() => URL.revokeObjectURL(value))
if (value.url) return { content: value, src: value.url }
const src = blobUrlFromContent(value)
onCleanup(() => URL.revokeObjectURL(src))
return value
})
return { content: value, src }
},
undefined,
{ equals: (prev, next) => prev.src === next.src },
)
}
/** Holds the pane until the media's first frame, so a file still streaming never shows as blank. */
@@ -273,26 +280,25 @@ function MediaLoading() {
/** Images and SVG previews: each decoded image owns its viewport. */
function ArtifactImage(props: MediaProps) {
const url = createMediaUrl(() => props.content)
const source = createMediaSource(() => props.content)
return (
<Show when={url()} keyed>
{(src) => <ImageStage {...props} src={src} />}
<Show when={source()} keyed>
{(source) => <ImageStage {...props} content={source.content} src={source.src} />}
</Show>
)
}
function ImageStage(props: MediaProps & { src: string }) {
const [view, setView] = createStore({
width: 0,
height: 0,
imageWidth: 0,
imageHeight: 0,
zoom: 1,
x: 0,
y: 0,
dragging: false,
})
const [view, setView] = createStore({ width: 0, height: 0, imageWidth: 0, imageHeight: 0, zoom: 1 })
const [gesture, setGesture] = createSignal(INITIAL_IMAGE_STAGE_GESTURE_STATE)
// The pan offsets paint straight to the image, so a pointer move writes no reactive state.
let panX = 0
let panY = 0
// Where the pan's pointer pressed, then where it was on its last move.
const origin = { x: 0, y: 0 }
let stage: HTMLDivElement | undefined
let image: HTMLImageElement | undefined
const fit = () =>
Math.min(
@@ -304,38 +310,44 @@ function ImageStage(props: MediaProps & { src: string }) {
const minZoom = () => Math.min(0.25, 1 / fit())
const maxZoom = () => Math.max(8, 1 / fit())
const bound = (offset: number, image: number, zoom: number) => {
const bound = (offset: number, size: number) => {
// Keep the fitted footprint covered, including its letterboxing. Clamping to
// the stage edges instead would move the anchor during small pinch increments.
const limit = Math.max(0, (image * fit() * (zoom - 1)) / 2)
const limit = Math.max(0, (size * fit() * (view.zoom - 1)) / 2)
return Math.max(-limit, Math.min(limit, offset))
}
const x = () => bound(view.x, view.imageWidth, view.zoom)
const y = () => bound(view.y, view.imageHeight, view.zoom)
const reset = () => setView({ zoom: 1, x: 0, y: 0 })
const pan = (left: number, top: number) =>
setView({ x: bound(left, view.imageWidth, view.zoom), y: bound(top, view.imageHeight, view.zoom) })
let stage: HTMLDivElement | undefined
let drag: { id: number; clientX: number; clientY: number; x: number; y: number } | undefined
let dragged = false
const cursor = () => {
if (view.zoom <= 1) return undefined
return view.dragging ? "grabbing" : "grab"
const paint = () => {
if (image) image.style.transform = `translate(-50%, -50%) translate(${panX}px, ${panY}px) scale(${view.zoom})`
}
const endDrag = (event: PointerEvent) => {
if (event.pointerId !== drag?.id) return
drag = undefined
setView("dragging", false)
const pan = (left: number, top: number) => {
panX = bound(left, view.imageWidth)
panY = bound(top, view.imageHeight)
paint()
}
const dispatchGesture = (event: ImageStageGestureEvent) => {
const prev = untrack(gesture)
const next = transitionImageStageGesture(prev, event)
if (next.state !== prev) setGesture(next.state)
next.effects.forEach((effect) => {
if (effect.type === "pointer.capture") return image?.setPointerCapture(effect.pointerId)
if (image?.hasPointerCapture(effect.pointerId)) image.releasePointerCapture(effect.pointerId)
})
}
const setZoom = (zoom: number, left: number, top: number) => {
setView("zoom", zoom)
pan(left, top)
dispatchGesture({ type: "zoom.changed", zoomed: zoom > 1 })
}
const reset = () => setZoom(1, 0, 0)
const zoomAt = (zoom: number, clientX: number, clientY: number) => {
if (!stage) return
@@ -343,11 +355,7 @@ function ImageStage(props: MediaProps & { src: string }) {
const next = Math.max(minZoom(), Math.min(maxZoom(), zoom))
const ratio = next / view.zoom
const anchor = { x: clientX - rect.left - rect.width / 2, y: clientY - rect.top - rect.height / 2 }
setView({
zoom: next,
x: bound(anchor.x - (anchor.x - x()) * ratio, view.imageWidth, next),
y: bound(anchor.y - (anchor.y - y()) * ratio, view.imageHeight, next),
})
setZoom(next, anchor.x - (anchor.x - panX) * ratio, anchor.y - (anchor.y - panY) * ratio)
}
const zoomBy = (factor: number) => {
@@ -357,16 +365,20 @@ function ImageStage(props: MediaProps & { src: string }) {
zoomAt(view.zoom * factor, rect.left + rect.width / 2, rect.top + rect.height / 2)
}
const cancel = (event: PointerEvent) => dispatchGesture({ type: "pointer.cancel", pointerId: event.pointerId })
createResizeObserver(
() => stage,
() => {
if (!stage) return
setView({ width: stage.clientWidth, height: stage.clientHeight })
setView({ x: x(), y: y() })
pan(panX, panY)
},
)
onCleanup(() => dispatchGesture({ type: "disposed" }))
return (
<div
ref={stage}
@@ -390,10 +402,14 @@ function ImageStage(props: MediaProps & { src: string }) {
}
// Use the OS's trackpad momentum directly, with no second animation loop.
pan(x() - event.deltaX * unit, y() - event.deltaY * unit)
pan(panX - event.deltaX * unit, panY - event.deltaY * unit)
}}
>
<img
ref={(element) => {
image = element
paint()
}}
data-slot="artifact-media"
src={props.src}
alt={getFilename(props.path)}
@@ -404,40 +420,54 @@ function ImageStage(props: MediaProps & { src: string }) {
height: view.imageHeight ? `${view.imageHeight * fit()}px` : undefined,
"max-width": "none",
"max-height": "none",
transform: `translate(-50%, -50%) translate(${x()}px, ${y()}px) scale(${view.zoom})`,
"touch-action": view.zoom > 1 ? "none" : "auto",
cursor: cursor(),
cursor: selectStageCursor(gesture(), view.zoom > 1),
}}
onPointerDown={(event) => {
if (!event.isPrimary || event.button !== 0) return
dispatchGesture({
type: "pointer.down",
pointerId: event.pointerId,
button: event.button,
primary: event.isPrimary,
zoomed: view.zoom > 1,
})
dragged = false
if (view.zoom <= 1) return
drag = { id: event.pointerId, clientX: event.clientX, clientY: event.clientY, x: x(), y: y() }
event.currentTarget.setPointerCapture(event.pointerId)
setView("dragging", true)
if (!selectStagePanPointer(gesture(), event.pointerId)) return
origin.x = event.clientX
origin.y = event.clientY
}}
onPointerMove={(event) => {
if (!drag || drag.id !== event.pointerId) return
const current = gesture()
const dx = event.clientX - drag.clientX
const dy = event.clientY - drag.clientY
if (!selectStagePanPointer(current, event.pointerId)) return
if (!dragged && Math.hypot(dx, dy) < 3) return
// The button came up where this element could not hear it.
if (event.pointerType === "mouse" && (event.buttons & 1) === 0) return cancel(event)
const dx = event.clientX - origin.x
const dy = event.clientY - origin.y
dragged = true
pan(drag.x + dx, drag.y + dy)
if (dx === 0 && dy === 0) return
// Only the move that crosses the threshold changes the gesture; the rest dispatch and allocate nothing.
if (!selectStagePanMoved(current)) {
if (Math.hypot(dx, dy) < STAGE_DRAG_THRESHOLD_PX) return
dispatchGesture({ type: "pointer.moved", pointerId: event.pointerId })
}
// Each move pans by its own delta, so reversing at a clamp wall moves the image at once.
origin.x = event.clientX
origin.y = event.clientY
pan(panX + dx, panY + dy)
}}
onPointerUp={endDrag}
onPointerCancel={endDrag}
onLostPointerCapture={endDrag}
onError={() => props.onError()}
onPointerUp={(event) => dispatchGesture({ type: "pointer.up", pointerId: event.pointerId })}
onPointerCancel={cancel}
onLostPointerCapture={cancel}
onError={() => props.onError(props.content)}
onLoad={(event) => {
const image = event.currentTarget
const width = image.naturalWidth || image.width || image.clientWidth || 300
const height = image.naturalHeight || image.height || image.clientHeight || 150
const element = event.currentTarget
const width = element.naturalWidth || element.width || element.clientWidth || 300
const height = element.naturalHeight || element.height || element.clientHeight || 150
setView({
imageWidth: width,
@@ -445,14 +475,15 @@ function ImageStage(props: MediaProps & { src: string }) {
width: stage?.clientWidth ?? 0,
height: stage?.clientHeight ?? 0,
})
props.onInfo({ width, height })
pan(panX, panY)
props.onInfo(props.content, { width, height })
}}
onClick={(event) => {
if (dragged) {
dragged = false
// The click that ends a moved pan neither fits nor zooms.
const suppressed = selectStageClickSuppressed(gesture())
dispatchGesture({ type: "click" })
return
}
if (suppressed) return
if (view.zoom !== 1) return reset()
@@ -475,44 +506,90 @@ function ImageStage(props: MediaProps & { src: string }) {
}
function ArtifactVideo(props: MediaProps) {
const source = createMediaSource(() => props.content)
return (
<Show when={source()} keyed>
{(source) => <VideoStage {...props} content={source.content} src={source.src} />}
</Show>
)
}
function VideoStage(props: MediaProps & { src: string }) {
const ctx = usePlugin()
const dialog = useDialog()
const url = createMediaUrl(() => props.content)
const [loadedUrl, setLoadedUrl] = createSignal<string>()
const [paused, setPaused] = createSignal(true)
const [active, setActive] = createSignal(false)
const [open, setOpen] = createSignal(false)
const [overlay, setOverlay] = createSignal(createVideoOverlayState())
const [box, setBox] = createStore({ left: 0, top: 0, ready: false })
let stage: HTMLDivElement | undefined
let frame: HTMLDivElement | undefined
let video: HTMLVideoElement | undefined
let idleTimer: ReturnType<typeof setTimeout> | undefined
let popoverFrame: number | undefined
let preview: { attempt: number; id: string; observer: MutationObserver } | undefined
const clearIdle = () => {
if (idleTimer !== undefined) clearTimeout(idleTimer)
idleTimer = undefined
const dispatch = (event: VideoOverlayEvent) => {
const prev = untrack(overlay)
const next = transitionVideoOverlay(prev, event)
if (next.state !== prev) setOverlay(next.state)
next.effects.forEach((effect) => {
if (effect.type === "idle.schedule") {
clearTimeout(idleTimer)
idleTimer = setTimeout(() => dispatch({ type: "idle.elapsed", attempt: effect.attempt }), effect.delayMs)
return
}
if (effect.type === "idle.clear") {
clearTimeout(idleTimer)
idleTimer = undefined
return
}
if (effect.type === "preview.open") return open(effect.attempt)
if (preview?.attempt !== effect.attempt) return
preview.observer.disconnect()
if (effect.type === "preview.close") dialog.close(preview.id)
preview = undefined
})
}
const wake = () => {
setActive(true)
clearIdle()
idleTimer = setTimeout(() => setActive(false), 2500)
}
onCleanup(clearIdle)
// The preview fills the window and borrows this <video> element, so playback carries on as it flies out and back.
const expand = () => {
// The overlay controls stay hidden until the video is back in the stage, or the preview is gone without it.
const open = (attempt: number) => {
const el = video
if (!el?.videoWidth || open() || !stage?.contains(el)) return
setOpen(true)
if (!el || !frame) return dispatch({ type: "preview.returned", attempt })
const back = () => {
if (stage?.contains(el)) dispatch({ type: "preview.returned", attempt })
}
// The preview hands the video back into this frame; nothing else in it changes while the preview is open.
const observer = new MutationObserver(back)
const id = uuid()
observer.observe(frame, { childList: true })
preview = { attempt, id, observer }
dialog.show(
() => <ImagePreview src={el.currentSrc} alt={getFilename(props.path)} video={el} />,
() => setOpen(false),
() => {
onCleanup(() => dispatch({ type: "preview.returned", attempt }))
return <ImagePreview src={el.currentSrc} alt={getFilename(props.path)} video={el} />
},
// A preview that closes before it borrowed the video, or never mounts, leaves it in place.
back,
id,
)
}
const expand = () =>
dispatch({ type: "expand.requested", canExpand: !!video?.videoWidth && !!stage?.contains(video) })
const measure = () => {
if (!video || video.offsetWidth === 0 || video.offsetHeight === 0) return
@@ -530,7 +607,14 @@ function ArtifactVideo(props: MediaProps) {
() => measure(),
)
const controlsVisible = () => !open() && (paused() || active())
onCleanup(() => {
if (popoverFrame !== undefined) {
cancelAnimationFrame(popoverFrame)
video?.removeAttribute("popover")
}
dispatch({ type: "disposed" })
})
return (
<div
@@ -538,13 +622,10 @@ function ArtifactVideo(props: MediaProps) {
data-slot="artifact-stage"
data-zoom="fit"
class="relative min-h-0 flex-1 overflow-hidden"
onPointerMove={wake}
onPointerLeave={() => {
clearIdle()
setActive(false)
}}
onPointerMove={() => dispatch({ type: "pointer.moved" })}
onPointerLeave={() => dispatch({ type: "pointer.left" })}
>
<div class="absolute inset-0 flex items-center justify-center p-6">
<div ref={frame} class="absolute inset-0 flex items-center justify-center p-6">
<video
ref={video}
data-slot="artifact-media"
@@ -552,8 +633,8 @@ function ArtifactVideo(props: MediaProps) {
controls
preload="metadata"
playsinline
onError={() => props.onError()}
src={url()}
onError={() => props.onError(props.content)}
src={props.src}
onClick={(event) => {
// Chromium's controls answer the second click of a double click with a native fullscreen request, which
// covers the preview the double click opens. Opting out with controlslist="nofullscreen" would also end
@@ -565,21 +646,25 @@ function ArtifactVideo(props: MediaProps) {
if (event.detail < 2 || document.fullscreenElement === el) return
el.setAttribute("popover", "manual")
el.showPopover?.()
requestAnimationFrame(() => el.removeAttribute("popover"))
if (popoverFrame !== undefined) cancelAnimationFrame(popoverFrame)
popoverFrame = requestAnimationFrame(() => {
popoverFrame = undefined
el.removeAttribute("popover")
})
}}
onDblClick={expand}
onPlay={() => setPaused(false)}
onPause={() => setPaused(true)}
onEnded={() => setPaused(true)}
onPlay={() => dispatch({ type: "playback.changed", paused: false })}
onPause={() => dispatch({ type: "playback.changed", paused: true })}
onEnded={() => dispatch({ type: "playback.changed", paused: true })}
onLoadedMetadata={(event) => {
const el = event.currentTarget
setPaused(el.paused)
props.onInfo({ width: el.videoWidth, height: el.videoHeight, duration: el.duration })
props.onInfo(props.content, { width: el.videoWidth, height: el.videoHeight, duration: el.duration })
measure()
}}
onLoadedData={() => setLoadedUrl(url())}
onLoadedData={(event) => dispatch({ type: "media.loaded", paused: event.currentTarget.paused })}
/>
<Show when={loadedUrl() !== url()}>
<Show when={selectVideoLoading(overlay())}>
<MediaLoading />
</Show>
<button
@@ -591,8 +676,8 @@ function ArtifactVideo(props: MediaProps) {
style={box.ready ? { left: `${box.left}px`, top: `${box.top}px` } : { right: "72px", bottom: "48px" }}
classList={{
"absolute z-10 flex size-12 items-center justify-center transition-opacity focus-visible:opacity-100 focus-visible:outline-none": true,
"opacity-100": controlsVisible(),
"pointer-events-none opacity-0": !controlsVisible(),
"opacity-100": selectVideoControlsVisible(overlay()),
"pointer-events-none opacity-0": !selectVideoControlsVisible(overlay()),
}}
>
<span class="flex size-9 items-center justify-center rounded-full transition-colors">
@@ -605,7 +690,7 @@ function ArtifactVideo(props: MediaProps) {
}
function ArtifactAudio(props: MediaProps) {
const url = createMediaUrl(() => props.content)
const source = createMediaSource(() => props.content)
return (
<div data-slot="artifact-stage" class="relative min-h-0 flex-1 overflow-auto">
@@ -617,11 +702,12 @@ function ArtifactAudio(props: MediaProps) {
<div class="max-w-full truncate text-14-medium text-text-strong">{getFilename(props.path)}</div>
<audio
class="w-full"
onError={() => props.onError()}
// Setting a new src drops the old one's queued events, so these always describe the current source.
onError={() => props.onError(source().content)}
controls
preload="metadata"
src={url()}
onLoadedMetadata={(event) => props.onInfo({ duration: event.currentTarget.duration })}
src={source().src}
onLoadedMetadata={(event) => props.onInfo(source().content, { duration: event.currentTarget.duration })}
/>
</div>
</div>
@@ -630,9 +716,9 @@ function ArtifactAudio(props: MediaProps) {
}
function ArtifactFrame(props: { path: string; content: FileContent; kind: "pdf" | "html" }) {
const url = createMediaUrl(() => props.content)
const source = createMediaSource(() => props.content)
// PDF Open Parameters: start with the thumbnail pane closed and the page fitted to the pane width.
const src = () => (props.kind === "pdf" ? `${url()}#navpanes=0&view=FitH` : url())
const src = () => (props.kind === "pdf" ? `${source().src}#navpanes=0&view=FitH` : source().src)
return (
<iframe
@@ -811,12 +897,12 @@ const specimenSizes = [12, 16, 24, 40, 64]
function ArtifactFont(props: { path: string; content: FileContent }) {
const ctx = usePlugin()
const url = createMediaUrl(() => props.content)
const source = createMediaSource(() => props.content)
const family = createMemo(() => `artifact-${Math.random().toString(36).slice(2)}`)
// The document's font set holds the face while it shows.
createKeyed(url, (source) => {
const face = new FontFace(family(), `url(${source})`)
createKeyed(source, (value) => {
const face = new FontFace(family(), `url(${value.src})`)
document.fonts.add(face)
void face.load().catch(() => undefined)
@@ -880,14 +966,21 @@ function ArtifactViewer(props: {
path: string
size: string
content: FileContent
onDetails: (details: readonly string[]) => void
onError: (reason?: string) => void
onDetails: (content: FileContent, details: readonly string[]) => void
onError: (content: FileContent, reason?: string) => void
}) {
// Checked together, so the check only ever reads bytes freshly decoded, never ones the viewer has moved away.
// Checked together, so the check only ever reads bytes freshly decoded, never ones the viewer has moved away. The
// callbacks name the content these bytes came from, so a viewer that answers after a reread changes nothing.
const decoded = createMemo(() => {
const bytes = bytesFromContent(props.content)
const content = props.content
const bytes = bytesFromContent(content)
return { bytes, problem: props.viewer.problem?.(bytes) }
return {
bytes,
problem: props.viewer.problem?.(bytes),
onDetails: (details: readonly string[]) => props.onDetails(content, details),
onError: (reason?: string) => props.onError(content, reason),
}
})
return (
@@ -899,7 +992,7 @@ function ArtifactViewer(props: {
// Taking the error makes Solid call this once per error, untracked, rather than render it as a reactive child.
fallback={(_) => {
// Fails the file as a rejection does: its details leave the toolbar, and a reloaded file mounts a new viewer.
props.onError()
untrack(decoded).onError()
return <ArtifactBinary path={props.path} size={props.size} />
}}
@@ -907,8 +1000,8 @@ function ArtifactViewer(props: {
<Dynamic
component={props.viewer.View}
bytes={decoded().bytes}
onDetails={props.onDetails}
onError={props.onError}
onDetails={decoded().onDetails}
onError={decoded().onError}
/>
</ErrorBoundary>
</Show>
@@ -0,0 +1,105 @@
import { describe, expect, test } from "bun:test"
import {
createSelectHighlightState,
transitionSelectHighlight,
type SelectHighlightEvent,
type SelectHighlightState,
type SelectHighlightTransition,
} from "./machine"
const OPEN: SelectHighlightEvent = { type: "open.changed", open: true }
const CLOSE: SelectHighlightEvent = { type: "open.changed", open: false }
const highlight = (key: string): SelectHighlightEvent => ({ type: "highlight.changed", key, previewable: true })
const select = (key: string | undefined, closing = true): SelectHighlightEvent => ({
type: "option.selected",
key,
closing,
})
function run(state: SelectHighlightState, ...events: SelectHighlightEvent[]) {
const steps: SelectHighlightTransition[] = []
events.forEach((event) => steps.push(transitionSelectHighlight(steps.at(-1)?.state ?? state, event)))
return steps
}
const effects = (steps: SelectHighlightTransition[]) => steps.map((step) => step.effects.map((effect) => effect.type))
describe("select highlight preview", () => {
test("choosing the previewed option keeps its preview through the close", () => {
// Before: onChange ran the highlighted option's undo after onSelect had committed it.
const steps = run(createSelectHighlightState(false), OPEN, highlight("b"), select("b"), CLOSE)
expect(steps[1]!.effects).toEqual([{ type: "preview.apply", key: "b" }])
expect(effects(steps).slice(2)).toEqual([[], []])
expect(steps.at(-1)!.state).toEqual({ status: "closed" })
})
test("choosing another option, or clearing, reverts the preview before the caller commits", () => {
for (const key of ["b", undefined]) {
const steps = run(createSelectHighlightState(false), OPEN, highlight("a"), select(key), CLOSE)
expect(effects(steps).slice(2)).toEqual([["preview.revert"], []])
expect(steps.at(-1)!.state).toEqual({ status: "closed" })
}
})
test("items that highlight while the menu is closing or closed preview nothing, and a reopen previews again", () => {
// Before: a focus or pointer event during the exit animation applied a preview nothing ever reverted.
const committed = run(createSelectHighlightState(false), OPEN, highlight("a"), select("a")).at(-1)!.state
const late = transitionSelectHighlight(committed, highlight("c"))
expect(late.state).toBe(committed)
expect(late.effects).toEqual([])
const closed = transitionSelectHighlight(committed, CLOSE).state
expect(transitionSelectHighlight(closed, highlight("c")).state).toBe(closed)
const steps = run(closed, OPEN, highlight("c"), CLOSE)
expect(steps[1]!.effects).toEqual([{ type: "preview.apply", key: "c" }])
expect(effects(steps)[2]).toEqual(["preview.revert"])
})
test("a controlled close reverts the preview", () => {
// Before: a parent setting open={false} fired no onOpenChange, so the preview stayed applied while closed.
const steps = run(createSelectHighlightState(true), highlight("a"), CLOSE)
expect(effects(steps)).toEqual([["preview.apply"], ["preview.revert"]])
})
test("moving between options reverts the previous preview first, and the same option does nothing", () => {
const steps = run(createSelectHighlightState(true), highlight("a"), highlight("a"), highlight("b"))
expect(steps[1]!.state).toBe(steps[0]!.state)
expect(steps[2]!.effects).toEqual([{ type: "preview.revert" }, { type: "preview.apply", key: "b" }])
})
test("a select without onHighlight previews nothing and drops a preview it had", () => {
const off: SelectHighlightEvent = { type: "highlight.changed", key: "a", previewable: false }
const idle = createSelectHighlightState(true)
expect(transitionSelectHighlight(idle, off).state).toBe(idle)
expect(effects(run(idle, highlight("b"), off))[1]).toEqual(["preview.revert"])
})
test("a choice that keeps the menu open keeps previewing", () => {
const steps = run(createSelectHighlightState(true), highlight("a"), select("a", false), highlight("b"))
expect(steps[1]!.state).toEqual({ status: "idle" })
expect(steps[1]!.effects).toEqual([])
expect(steps[2]!.effects).toEqual([{ type: "preview.apply", key: "b" }])
})
test("disposal reverts an active preview and ignores everything after", () => {
const steps = run(createSelectHighlightState(true), highlight("a"), { type: "disposed" })
expect(effects(steps)[1]).toEqual(["preview.revert"])
const disposed = steps[1]!.state
expect(transitionSelectHighlight(disposed, OPEN).state).toBe(disposed)
expect(transitionSelectHighlight(disposed, highlight("b")).effects).toEqual([])
expect(transitionSelectHighlight(createSelectHighlightState(false), { type: "disposed" }).effects).toEqual([])
})
})
+81
View File
@@ -0,0 +1,81 @@
/**
* The preview a `<Select>` shows for its highlighted option (`onHighlight`, which may return an undo). Unlike a one-shot
* palette, a select opens and closes repeatedly: choosing the previewed option keeps its preview, choosing another
* reverts it first, and closing without a choice reverts it. Nothing previews while the menu is closed or closing.
*/
export type SelectHighlightState =
| { readonly status: "closed" }
| { readonly status: "idle" }
| { readonly status: "previewing"; readonly key: string }
| { readonly status: "committed" }
| { readonly status: "disposed" }
export type SelectHighlightEvent =
| { readonly type: "open.changed"; readonly open: boolean }
| { readonly type: "highlight.changed"; readonly key: string; readonly previewable: boolean }
/** `closing` is whether the menu closes on this choice (`closeOnSelection`). */
| { readonly type: "option.selected"; readonly key: string | undefined; readonly closing: boolean }
| { readonly type: "disposed" }
export type SelectHighlightEffect =
| { readonly type: "preview.apply"; readonly key: string }
| { readonly type: "preview.revert" }
export type SelectHighlightTransition = {
readonly state: SelectHighlightState
readonly effects: readonly SelectHighlightEffect[]
}
const CLOSED: SelectHighlightState = Object.freeze({ status: "closed" })
const IDLE: SelectHighlightState = Object.freeze({ status: "idle" })
const COMMITTED: SelectHighlightState = Object.freeze({ status: "committed" })
const DISPOSED: SelectHighlightState = Object.freeze({ status: "disposed" })
const NONE: readonly SelectHighlightEffect[] = Object.freeze([])
const REVERT: readonly SelectHighlightEffect[] = Object.freeze([{ type: "preview.revert" }])
export function createSelectHighlightState(open: boolean): SelectHighlightState {
return open ? IDLE : CLOSED
}
export function transitionSelectHighlight(
state: SelectHighlightState,
event: SelectHighlightEvent,
): SelectHighlightTransition {
if (state.status === "disposed") return { state, effects: NONE }
const previewing = state.status === "previewing"
if (event.type === "disposed") return { state: DISPOSED, effects: previewing ? REVERT : NONE }
if (event.type === "open.changed") {
if (event.open)
return { state: state.status === "closed" || state.status === "committed" ? IDLE : state, effects: NONE }
return { state: CLOSED, effects: previewing ? REVERT : NONE }
}
if (event.type === "option.selected") {
// The chosen option's preview is the committed value; any other preview reverts before the caller commits.
const effects = previewing && (event.key === undefined || state.key !== event.key) ? REVERT : NONE
if (!event.closing) return { state: IDLE, effects }
return { state: state.status === "closed" ? CLOSED : COMMITTED, effects }
}
// A closed or closing menu still delivers focus and pointer events to its items (forceMount, exit animation).
if (state.status === "closed" || state.status === "committed") return { state, effects: NONE }
if (!event.previewable) return previewing ? { state: IDLE, effects: REVERT } : { state, effects: NONE }
if (previewing && state.key === event.key) return { state, effects: NONE }
return {
state: { status: "previewing", key: event.key },
effects: [...(previewing ? REVERT : NONE), { type: "preview.apply", key: event.key }],
}
}
@@ -165,6 +165,35 @@ export const Surfaces = {
),
}
export const HighlightPreview = {
render: () => {
const [current, setCurrent] = createSignal("Apple")
const [shown, setShown] = createSignal("Apple")
return (
<div style={{ display: "grid", gap: "12px", width: "280px" }}>
<Select
appearance="base"
options={fruits}
current={current()}
onHighlight={(value) => {
const previous = shown()
setShown(value)
return () => setShown(previous)
}}
onSelect={(value) => {
if (!value) return
setCurrent(value)
setShown(value)
}}
/>
<output data-slot="select-preview">Showing {shown()}</output>
</div>
)
},
}
export const WithField = {
parameters: { frameHeight: "500px" },
render: (args) => {
+49 -27
View File
@@ -10,8 +10,19 @@ import {
Trigger,
Value,
} from "@kobalte/core/select"
import { Show, createMemo, onCleanup, splitProps, type ComponentProps, type JSX } from "solid-js"
import {
Show,
createComputed,
createMemo,
on,
onCleanup,
splitProps,
untrack,
type ComponentProps,
type JSX,
} from "solid-js"
import { MenuScroll } from "../../components/menu-scroll"
import { createSelectHighlightState, transitionSelectHighlight, type SelectHighlightEvent } from "./machine"
import "./select.css"
function groupOptions<T>(options: T[], groupBy?: (x: T) => string): { category: string; options: T[] }[] {
@@ -80,8 +91,6 @@ export type SelectProps<T> = Omit<
contentClass?: string
}
type HighlightState = { key?: string; cleanup?: void | (() => void) }
export function Select<T>(props: SelectProps<T>) {
const [local, others] = splitProps(props, [
"class",
@@ -111,36 +120,46 @@ export function Select<T>(props: SelectProps<T>) {
"fitViewport",
])
const state: HighlightState = {}
let highlight = createSelectHighlightState(untrack(() => others.open ?? others.defaultOpen ?? false))
// The highlighted item and the undo its preview returned; the machine decides when each runs.
let highlighted: T | undefined
let revert: void | (() => void)
let scroller: HTMLDivElement | undefined
const stop = () => {
state.cleanup?.()
state.cleanup = undefined
state.key = undefined
const dispatch = (event: SelectHighlightEvent) => {
const next = transitionSelectHighlight(highlight, event)
highlight = next.state
next.effects.forEach((effect) => {
if (effect.type === "preview.revert") {
const undo = revert
revert = undefined
return undo?.()
}
revert = highlighted === undefined ? undefined : local.onHighlight?.(highlighted)
})
}
const keyFor = (item: T) => (local.value ? local.value(item) : String(item))
const move = (item: T | undefined) => {
if (!local.onHighlight) return
if (!item) {
stop()
return
}
const key = keyFor(item)
if (state.key === key) return
state.cleanup?.()
state.cleanup = local.onHighlight(item)
state.key = key
const move = (item: T) => {
highlighted = item
dispatch({ type: "highlight.changed", key: keyFor(item), previewable: !!local.onHighlight })
}
onCleanup(stop)
// A controlled menu closed by its owner does not call onOpenChange.
createComputed(
on(
() => others.open,
(open) => {
if (open !== undefined) dispatch({ type: "open.changed", open })
},
),
)
onCleanup(() => dispatch({ type: "disposed" }))
const grouped = createMemo(() => groupOptions(local.options, local.groupBy))
@@ -192,13 +211,16 @@ export function Select<T>(props: SelectProps<T>) {
)}
onChange={(next) => {
const v = next == null ? null : Array.isArray(next) ? (next[0] ?? null) : next
dispatch({
type: "option.selected",
key: v === null ? undefined : keyFor(v),
closing: others.closeOnSelection ?? true,
})
local.onSelect?.(v)
stop()
}}
onOpenChange={(open) => {
if (others.open === undefined) dispatch({ type: "open.changed", open })
local.onOpenChange?.(open)
if (!open) stop()
}}
>
<Trigger