![]()
{
+ 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 (
+
+ {(source) => }
+
+ )
+}
+
+function VideoStage(props: MediaProps & { src: string }) {
const ctx = usePlugin()
const dialog = useDialog()
- const url = createMediaUrl(() => props.content)
- const [loadedUrl, setLoadedUrl] = createSignal
()
- 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 | 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