Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
Add inertial panning (momentum) to zoomed lightbox images (#4138)
Peter Chinman757bb7c4b18c
diff --git a/js/app/packages/channel/Media/MediaViewerDialog.tsx b/js/app/packages/channel/Media/MediaViewerDialog.tsxindex 27f411f33a..44b66f4c6b 100644--- a/js/app/packages/channel/Media/MediaViewerDialog.tsx+++ b/js/app/packages/channel/Media/MediaViewerDialog.tsx@@ -74,10 +74,13 @@ function VideoViewerContent(props: {><Dialog.Content class="flex items-center justify-center bg-surface"><LightboxToolbar isVisible={true}>- <Dialog.CloseButton>- <Button variant="ghost" size="icon-md" label="Close">- <XIcon />- </Button>+ <Dialog.CloseButton+ as={Button}+ variant="ghost"+ size="icon-md"+ label="Close"+ >+ <XIcon /></Dialog.CloseButton></LightboxToolbar>diff --git a/js/app/packages/core/component/Lightbox.tsx b/js/app/packages/core/component/Lightbox.tsxdeleted file mode 100644index ddf8d4c932..0000000000--- a/js/app/packages/core/component/Lightbox.tsx+++ /dev/null@@ -1,665 +0,0 @@-import * as stackingContext from '@core/constant/stackingContext';-import { isMobile } from '@core/mobile/isMobile';-import { isTouchDevice } from '@core/mobile/isTouchDevice';-import { Dialog, useDialogContext } from '@kobalte/core/dialog';-import ChevronLeftIcon from '@phosphor/caret-left.svg';-import ChevronRightIcon from '@phosphor/caret-right.svg';-import ClipboardIcon from '@phosphor/clipboard.svg';-import DownloadIcon from '@phosphor/download-simple.svg';-import XIcon from '@phosphor/x.svg';-import Spinner from '@phosphor-icons/core/bold/spinner-gap-bold.svg?component-solid';-import { isIOS } from '@solid-primitives/platform';-import { Button, cn } from '@ui';-import {- type Accessor,- type Component,- createEffect,- createMemo,- createSignal,- type JSX,- onCleanup,- Show,- untrack,-} from 'solid-js';-import {- copyImageToClipboard,- downloadImage as downloadImageAction,-} from '../util/imageActions';-import { platformFetch } from '../util/platformFetch';--import { Zoompinch, type ZoompinchHandle } from './Zoompinch';--const SpinnerIcon: Component<JSX.SvgSVGAttributes<SVGSVGElement>> = (p) => (- <Spinner {...p} class="animate-spin" />-);--type LightboxProps = {- // Current image to display- src: Accessor<string | undefined>;- // Used for the download filename- imageId: Accessor<string>;- // Optional pre-fetched blob override (e.g. DSS images). Falls back to fetching `src`.- getBlob?: () => Promise<Blob | undefined>;- // Gallery navigation. Passing either enables swipe (mobile) + arrow key (desktop) support.- // Pass undefined for a direction when that navigation is unavailable (first/last image).- onPrevious?: () => void;- onNext?: () => void;- // "2/5" style indicator — rendered when provided- indexLabel?: Accessor<string>;- navigationHidden?: boolean;-};--export function Lightbox(props: LightboxProps) {- const dialogContext = useDialogContext();-- const [zoompinchHandle, setZoompinchHandle] = createSignal<- ZoompinchHandle | undefined- >();- let hideToolbarTimeout: ReturnType<typeof setTimeout> | undefined;-- const fetchBlob = async (): Promise<Blob | undefined> => {- if (props.getBlob) return props.getBlob();- const url = props.src();- if (!url) return undefined;- return (await platformFetch(url)).blob();- };-- // Pre-fetch the blob on iOS so it is already in memory when the user taps- // copy/download. This keeps navigator.share() close to synchronous with the- // gesture — the user-activation window expires if a network round-trip is- // needed. Desktop clipboard doesn't have this constraint.- const [cachedBlob, setCachedBlob] = createSignal<Blob | undefined>();- // True while the iOS pre-fetch is in flight. We surface this as a loading- // state on the copy/download buttons so the blob is guaranteed to be in- // memory before the user can tap. Without this, tapping download on a large- // image (whose pre-fetch hasn't finished yet) falls through to an awaited- // network fetch, which consumes the tap's user activation and makes- // navigator.share() silently no-op until a second tap.- const [isPrefetching, setIsPrefetching] = createSignal(false);- if (isIOS) {- createEffect(() => {- const currentSrc = props.src(); // re-fetch when navigating to a new image- let isStale = false;- onCleanup(() => {- isStale = true;- });-- setCachedBlob(undefined);- setIsPrefetching(true);- untrack(() => fetchBlob())- .then((blob) => {- if (isStale || props.src() !== currentSrc) return;- if (blob) setCachedBlob(blob);- })- .catch(() => {})- .finally(() => {- if (!isStale && props.src() === currentSrc) setIsPrefetching(false);- });- });- }- const fetchBlobCached = (): Promise<Blob | undefined> => {- const cached = cachedBlob();- return cached ? Promise.resolve(cached) : fetchBlob();- };-- const [isCopying, setIsCopying] = createSignal(false);- const [isDownloading, setIsDownloading] = createSignal(false);-- const copyToClipboard = async () => {- if (isCopying()) return;- setIsCopying(true);- try {- await copyImageToClipboard(fetchBlobCached, props.src() ?? '');- } finally {- setIsCopying(false);- }- };-- const downloadImage = async () => {- if (isDownloading()) return;- setIsDownloading(true);- try {- await downloadImageAction(fetchBlobCached, props.imageId());- } finally {- setIsDownloading(false);- }- };-- // Reactive cursor state — drives the cursor style on the Zoompinch wrapper.- const [isDragging, setIsDragging] = createSignal(false);- const [currentScale, setCurrentScale] = createSignal(1);-- // Zoom is split into card growth × engine scale. Each card axis grows with- // the zoom level independently until it hits the available area, so a- // capped axis starts cropping while the other keeps growing. The canvas- // keeps the image's aspect ratio, and the engine's model is "scale 1 =- // canvas contain-fits the wrapper", so the displayed image size is always- // base × zoom and exactly fills the card on every uncapped axis.- let containerEl: HTMLDivElement | undefined;- const [baseSize, setBaseSize] = createSignal<{ w: number; h: number }>();- const [zoom, setZoom] = createSignal(1);- let inApplyZoom = false;-- const availableArea = () => {- if (!containerEl) return undefined;- return {- w: containerEl.clientWidth,- // Mirror the img's sm:max-h-[80vh] cap on desktop- h: isMobile()- ? containerEl.clientHeight- : Math.min(containerEl.clientHeight, window.innerHeight * 0.8),- };- };-- const cardSizeFor = (base: { w: number; h: number }, z: number) => {- const avail = availableArea();- if (!avail) return { w: base.w * z, h: base.h * z };- return {- w: Math.min(base.w * z, avail.w),- h: Math.min(base.h * z, avail.h),- };- };-- const cardSize = createMemo(() => {- const base = baseSize();- return base ? cardSizeFor(base, zoom()) : undefined;- });-- // The engine's naturalScale: how far the base-aspect canvas is scaled to- // contain-fit the card. Engine scale carries the rest of the total zoom.- const containFactor = (- base: { w: number; h: number },- card: { w: number; h: number }- ) => Math.min(card.w / base.w, card.h / base.h);-- const totalZoom = createMemo(() => {- const base = baseSize();- const card = cardSize();- return base && card ? currentScale() * containFactor(base, card) : 1;- });-- // The unzoomed card size: contain-fit into the available area without- // upscaling, with the img's sm:min-w-50 floor on desktop.- const measureBase = (img: HTMLImageElement) => {- const avail = availableArea();- const nw = img.naturalWidth;- const nh = img.naturalHeight;- if (!avail || !nw || !nh) return undefined;- const fit = Math.min(avail.w / nw, avail.h / nh, 1);- return { w: Math.max(nw * fit, isMobile() ? 0 : 200), h: nh * fit };- };-- // The engine caches wrapper/canvas bounds via ResizeObservers, which fire- // after we resize the card. Refresh them synchronously so transforms applied- // in the same tick use the new geometry.- const syncEngineBounds = (handle: ZoompinchHandle) => {- const { engine, wrapperElement } = handle;- engine.wrapperBounds = wrapperElement.getBoundingClientRect();- const canvas = wrapperElement.querySelector(- '.canvas'- ) as HTMLElement | null;- if (canvas) {- // offsetWidth/Height: layout size, unaffected by the engine's transform- engine.canvasBounds = {- ...engine.canvasBounds,- width: canvas.offsetWidth,- height: canvas.offsetHeight,- };- }- };-- // The canvas-relative (0-1) content point currently under the wrapper center.- const centerCanvasRel = (- engine: ZoompinchHandle['engine']- ): [number, number] => {- const [cx, cy] = engine.normalizeClientCoords(- engine.wrapperInnerX + engine.wrapperInnerWidth / 2,- engine.wrapperInnerY + engine.wrapperInnerHeight / 2- );- return [cx / engine.canvasBounds.width, cy / engine.canvasBounds.height];- };-- const applyZoom = (- handle: ZoompinchHandle,- zoomLevel: number,- anchor?: [number, number]- ) => {- const { engine } = handle;- const base = baseSize();- const z = Math.max(1, zoomLevel);- const f = base ? containFactor(base, cardSizeFor(base, z)) : 1;- // Without an explicit anchor, keep whatever content point sits at the card- // center fixed across the resize — read it from the old geometry first.- const wrapperAnchor = anchor ?? ([0.5, 0.5] as [number, number]);- const canvasAnchor = anchor ?? centerCanvasRel(engine);- inApplyZoom = true;- setZoom(z);- syncEngineBounds(handle);- engine.applyTransform(z / f, wrapperAnchor, canvasAnchor);- inApplyZoom = false;- };-- // Continuous wheel / trackpad zoom. The engine computes the new scale (so the- // zoom feel is unchanged), but we discard its cursor-anchored translate and- // re-apply the zoom anchored at the *pre-gesture* view center via applyZoom.- // Center-anchoring is what keeps the image continuous across the point where- // an axis stops growing and starts cropping: while an axis is still growing- // there is no overflow on it, so the engine force-centers it and an- // off-center anchor's offset is suppressed — it would otherwise snap free the- // instant the axis caps, producing a visible jump. Non-zoom wheel events pan.- const handleWheel = (e: WheelEvent, engine: ZoompinchHandle['engine']) => {- const handle = zoompinchHandle();- const base = baseSize();- const card = cardSize();- if (e.ctrlKey && handle && base && card) {- e.preventDefault();- const anchor = centerCanvasRel(engine); // capture before the engine moves- // Let the engine derive the new scale, but suppress the rebalance pass —- // we re-apply the zoom ourselves below with the correct anchor.- inApplyZoom = true;- engine.handleWheel(e);- inApplyZoom = false;- applyZoom(handle, engine.scale * containFactor(base, card), anchor);- return;- }- // Pan. The engine's own wheel-pan multiplies the delta by 25 and only- // normalizes when |delta| >= 100, so trackpads and hi-res mice — whose- // deltas are small (often fractional) — lurch hundreds of px per event,- // snapping straight to the clamp edge. Pan 1:1 with the real pixel delta- // instead. (deltaMode: 0 = px, 1 = lines, 2 = pages.)- e.preventDefault();- const unit =- e.deltaMode === 1- ? 16- : e.deltaMode === 2- ? engine.wrapperBounds.height- : 1;- engine.setTranslateFromUserGesture(- engine.translateX - e.deltaX * unit,- engine.translateY - e.deltaY * unit- );- engine.update();- };-- // Rebalance after engine-driven zoom (wheel/pinch): move as much of the- // total zoom as possible into card growth, leaving the rest on the engine.- // Runs synchronously inside the engine's update event, before paint.- const rebalanceZoom = (engine: ZoompinchHandle['engine']) => {- if (inApplyZoom) return;- const handle = zoompinchHandle();- const base = baseSize();- const card = cardSize();- if (!handle || !base || !card) return;- const z = Math.max(1, engine.scale * containFactor(base, card));- const target = cardSizeFor(base, z);- if (- Math.abs(z / containFactor(base, target) - engine.scale) > 0.001 ||- Math.abs(target.w - card.w) > 0.5 ||- Math.abs(target.h - card.h) > 0.5- ) {- applyZoom(handle, z);- }- };-- const cursor = createMemo(() => {- if (isDragging() && currentScale() > 1.01) return 'grab';- if (totalZoom() > 1.01) return 'zoom-out';- return 'zoom-in';- });-- // Single-finger swipe state. On touch devices, when fully zoomed out, a- // single-finger drag is a swipe gesture: horizontal navigates the gallery,- // a downward swipe dismisses the lightbox. The axis is locked on the first- // clearly-directional movement so the two don't fight.- const SWIPE_DISMISS_DISTANCE = 100; // px of downward travel to dismiss- let swipeStartX = 0;- let swipeStartY = 0;- let swipeEndX = 0;- let swipeEndY = 0;- let swipeAxis: 'x' | 'y' | null = null;- let isSwiping = false;- let zoompinchHandlingTouch = false;-- const touchOnStart = (e: TouchEvent, engine: ZoompinchHandle['engine']) => {- const doSwipeDetection =- isTouchDevice() && e.touches.length === 1 && totalZoom() <= 1.01;- if (doSwipeDetection) {- swipeStartX = swipeEndX = e.touches[0].clientX;- swipeStartY = swipeEndY = e.touches[0].clientY;- swipeAxis = null;- isSwiping = false;- zoompinchHandlingTouch = false;- } else {- engine.handleTouchstart(e);- zoompinchHandlingTouch = true;- }- };-- const touchOnWindowMove = (- e: TouchEvent,- engine: ZoompinchHandle['engine']- ) => {- if (zoompinchHandlingTouch) {- engine.handleTouchmove(e);- return;- }- // Second finger appeared mid-gesture: switch to zoompinch- if (e.touches.length > 1) {- engine.handleTouchstart(e);- zoompinchHandlingTouch = true;- isSwiping = false;- return;- }- swipeEndX = e.touches[0].clientX;- swipeEndY = e.touches[0].clientY;- const dx = swipeEndX - swipeStartX;- const dy = swipeEndY - swipeStartY;- // Lock to the dominant axis once the gesture is clearly directional.- if (!swipeAxis && Math.hypot(dx, dy) > 10) {- swipeAxis = Math.abs(dx) > Math.abs(dy) ? 'x' : 'y';- }- // Downward-only on the y axis — an upward drag is left alone.- if (- (swipeAxis === 'x' && Math.abs(dx) > 30) ||- (swipeAxis === 'y' && dy > 30)- ) {- isSwiping = true;- }- if (isSwiping) e.preventDefault();- };-- const touchOnWindowEnd = (- e: TouchEvent,- engine: ZoompinchHandle['engine']- ) => {- if (zoompinchHandlingTouch) {- engine.handleTouchend(e);- zoompinchHandlingTouch = false;- return;- }- if (isSwiping && totalZoom() <= 1.01) {- if (swipeAxis === 'x') {- const diff = swipeStartX - swipeEndX;- if (Math.abs(diff) > 50) {- if (diff > 0) props.onNext?.();- else props.onPrevious?.();- }- } else if (- swipeAxis === 'y' &&- swipeEndY - swipeStartY > SWIPE_DISMISS_DISTANCE- ) {- dialogContext.close();- }- }- swipeAxis = null;- isSwiping = false;- swipeStartX = swipeStartY = swipeEndX = swipeEndY = 0;- zoompinchHandlingTouch = false;- };-- // Keyboard nav + toolbar fade — active while the Zoompinch handle is set- createEffect(() => {- const handle = zoompinchHandle();- if (!handle) return;- const { engine, wrapperElement: wrapper } = handle;-- const handleKeyDown = (e: KeyboardEvent) => {- if (e.key === 'Escape') {- dialogContext.close();- } else if (e.key === 'ArrowLeft') {- e.preventDefault();- props.onPrevious?.();- } else if (e.key === 'ArrowRight') {- e.preventDefault();- props.onNext?.();- }- };- window.addEventListener('keydown', handleKeyDown);-- if (!isMobile()) {- // Track dragging so click-to-zoom and cursor stay in sync- let isMouseDown = false;- let mouseDownX = 0;- let mouseDownY = 0;-- const handleMouseDown = (e: MouseEvent) => {- isMouseDown = true;- mouseDownX = e.clientX;- mouseDownY = e.clientY;- setIsDragging(false);- };- const handleWindowMouseMove = (e: MouseEvent) => {- if (!isMouseDown) return;- if (Math.hypot(e.clientX - mouseDownX, e.clientY - mouseDownY) > 5) {- setIsDragging(true);- }- };- const handleWindowMouseUp = () => {- isMouseDown = false;- // Delay reset so the click event (which fires after mouseup) can still- // read isDragging=true and suppress the zoom-out action.- setTimeout(() => setIsDragging(false), 0);- };-- // Click-to-zoom: zoom in at cursor position, or reset if already zoomed- const handleClick = (e: MouseEvent) => {- if (isDragging()) return;- const b = engine.wrapperBounds;- const relX = (e.clientX - b.x) / b.width;- const relY = (e.clientY - b.y) / b.height;- if (totalZoom() <= 1.01) {- applyZoom(handle, 2.5, [relX, relY]);- } else {- applyZoom(handle, 1);- }- };-- wrapper.addEventListener('mousedown', handleMouseDown);- window.addEventListener('mousemove', handleWindowMouseMove);- window.addEventListener('mouseup', handleWindowMouseUp);- wrapper.addEventListener('click', handleClick);-- onCleanup(() => {- wrapper.removeEventListener('mousedown', handleMouseDown);- window.removeEventListener('mousemove', handleWindowMouseMove);- window.removeEventListener('mouseup', handleWindowMouseUp);- wrapper.removeEventListener('click', handleClick);- });- }-- onCleanup(() => {- window.removeEventListener('keydown', handleKeyDown);- if (hideToolbarTimeout) clearTimeout(hideToolbarTimeout);- });- });-- // Reset zoom when navigating to a different image.- createEffect(() => {- props.src();- untrack(() => {- const handle = zoompinchHandle();- if (handle) applyZoom(handle, 1);- else setZoom(1);- });- });-- const navButtonClass =- 'absolute top-1/2 -translate-y-1/2 bg-surface backdrop-blur-sm rounded-lg border border-edge p-2 shadow-md hover:bg-surface transition-opacity duration-300 disabled:cursor-not-allowed disabled:opacity-50';-- const navVisible = () => true;-- return (- <div- ref={containerEl}- class="fixed inset-0 z-modal flex items-center justify-center"- style={{- 'margin-top': 'max(var(--safe-top), 0.5rem)',- 'margin-bottom': 'max(var(--safe-bottom), 1.5rem)',- 'margin-left': 'max(var(--safe-left), 0.5rem)',- 'margin-right': 'max(var(--safe-right), 0.5rem)',- }}- >- <Dialog.Content class="flex items-center justify-center bg-surface rounded-md overflow-hidden">- {/* Toolbar */}- <LightboxToolbar isVisible={true}>- <Button- variant="ghost"- size="icon-md"- onClick={copyToClipboard}- disabled={isCopying() || isPrefetching()}- label="Copy image"- >- {isCopying() ? <SpinnerIcon /> : <ClipboardIcon />}- </Button>- <Button- variant="ghost"- size="icon-md"- onClick={downloadImage}- disabled={isDownloading() || isPrefetching()}- label="Download image"- >- {isDownloading() ? <SpinnerIcon /> : <DownloadIcon />}- </Button>- <Dialog.CloseButton>- <Button variant="ghost" size="icon-md" label="Close">- <XIcon />- </Button>- </Dialog.CloseButton>- </LightboxToolbar>-- {/* Nav arrows — desktop only */}- <Show when={!isMobile()}>- <Show when={!props.navigationHidden}>- <button- class={cn(- navButtonClass,- 'left-4',- navVisible() ? 'opacity-100' : 'opacity-0 pointer-events-none'- )}- style={{ 'z-index': stackingContext.zModal + 1 }}- onClick={props.onPrevious}- disabled={!props.onPrevious}- aria-label="Previous image"- >- <ChevronLeftIcon class="size-5 text-ink" />- </button>-- <button- class={cn(- navButtonClass,- 'right-4',- navVisible() ? 'opacity-100' : 'opacity-0 pointer-events-none'- )}- style={{ 'z-index': stackingContext.zModal + 1 }}- onClick={props.onNext}- disabled={!props.onNext}- aria-label="Next image"- >- <ChevronRightIcon class="size-5 text-ink" />- </button>- </Show>- </Show>-- {/* Index indicator */}- <Show when={props.indexLabel}>- <div- class={cn(- 'absolute top-4 left-4 bg-surface backdrop-blur-sm rounded-lg border border-edge px-3 py-1.5 shadow-md transition-opacity duration-300',- navVisible() ? 'opacity-100' : 'opacity-0 pointer-events-none'- )}- style={{ 'z-index': stackingContext.zModal + 1 }}- >- <span class="text-sm text-ink font-medium">- {props.indexLabel?.()}- </span>- </div>- </Show>-- {/* Image */}- <div class="size-full flex items-center justify-center">- <Show- when={props.src()}- fallback={- <div class="flex flex-col items-center justify-center gap-2 size-15 border border-edge bg-surface">- <Spinner class="size-4 animate-spin" />- </div>- }- >- <Zoompinch- handleRef={setZoompinchHandle}- clampBounds- // Engine min below 1 so gestures can zoom out past the engine's- // own floor; rebalanceZoom transfers that into card shrinkage- // and enforces the real floor of total zoom = 1.- minScale={0.1}- onUpdate={(engine) => {- setCurrentScale(engine.scale);- rebalanceZoom(engine);- }}- onWheel={handleWheel}- touch={{- onStart: touchOnStart,- onWindowMove: touchOnWindowMove,- onWindowEnd: touchOnWindowEnd,- }}- class={cn('relative overflow-hidden', !cardSize() && 'size-full')}- style={{- cursor: cursor(),- ...(cardSize() && {- width: `${cardSize()!.w}px`,- height: `${cardSize()!.h}px`,- }),- }}- // Give the canvas the image's own aspect ratio so the engine's- // contain-fit and clamping track the image content, not the- // (possibly differently-shaped) card.- canvasStyle={- baseSize()- ? {- width: `${baseSize()!.w}px`,- height: `${baseSize()!.h}px`,- }- : undefined- }- >- <img- class="size-full sm:min-w-50 sm:max-h-[80vh] object-contain select-none"- style={{ '-webkit-touch-callout': 'none' }}- src={props.src()}- alt="preview"- onLoad={(e) => {- const base = measureBase(e.currentTarget);- if (!base) return;- setBaseSize(base);- const handle = zoompinchHandle();- if (handle) applyZoom(handle, 1);- else setZoom(1);- }}- />- </Zoompinch>- </Show>- </div>- </Dialog.Content>- </div>- );-}--type LightboxToolbarProps = {- isVisible: boolean;- children: JSX.Element;-};--export function LightboxToolbar(props: LightboxToolbarProps) {- return (- <div- class="absolute top-4 right-4 bg-surface backdrop-blur-sm rounded-lg border border-edge p-1 flex flex-row items-center gap-1 shadow-md transition-opacity duration-300"- classList={{- 'opacity-100': isMobile() || props.isVisible,- 'opacity-0 pointer-events-none': !isMobile() && !props.isVisible,- }}- style={{ 'z-index': stackingContext.zModal + 1 }}- >- {props.children}- </div>- );-}diff --git a/js/app/packages/core/component/Lightbox/Lightbox.tsx b/js/app/packages/core/component/Lightbox/Lightbox.tsxnew file mode 100644index 0000000000..e4525fc0ff--- /dev/null+++ b/js/app/packages/core/component/Lightbox/Lightbox.tsx@@ -0,0 +1,180 @@+import { Dialog, useDialogContext } from '@kobalte/core/dialog';+import ClipboardIcon from '@phosphor/clipboard.svg';+import DownloadIcon from '@phosphor/download-simple.svg';+import XIcon from '@phosphor/x.svg';+import Spinner from '@phosphor-icons/core/bold/spinner-gap-bold.svg?component-solid';+import { Button, cn } from '@ui';+import {+ type Accessor,+ type Component,+ createEffect,+ createSignal,+ type JSX,+ Show,+ untrack,+} from 'solid-js';+import { Zoompinch, type ZoompinchHandle } from '../Zoompinch';+import { createGestures } from './createGestures';+import { createImageActions } from './createImageActions';+import { createMomentum } from './createMomentum';+import { createZoomModel } from './createZoomModel';+import { LightboxChrome } from './LightboxChrome';+import { LightboxToolbar } from './LightboxToolbar';++const SpinnerIcon: Component<JSX.SvgSVGAttributes<SVGSVGElement>> = (p) => (+ <Spinner {...p} class="animate-spin" />+);++type LightboxProps = {+ // Current image to display+ src: Accessor<string | undefined>;+ // Used for the download filename+ imageId: Accessor<string>;+ // Optional pre-fetched blob override (e.g. DSS images). Falls back to fetching `src`.+ getBlob?: () => Promise<Blob | undefined>;+ // Gallery navigation. Passing either enables swipe (mobile) + arrow key (desktop) support.+ // Pass undefined for a direction when that navigation is unavailable (first/last image).+ onPrevious?: () => void;+ onNext?: () => void;+ // "2/5" style indicator — rendered when provided+ indexLabel?: Accessor<string>;+ navigationHidden?: boolean;+};++export function Lightbox(props: LightboxProps) {+ const dialogContext = useDialogContext();+ const [zoompinchHandle, setZoompinchHandle] = createSignal<+ ZoompinchHandle | undefined+ >();++ const images = createImageActions(props);+ const zoom = createZoomModel(zoompinchHandle);+ const momentum = createMomentum(zoompinchHandle);+ const gestures = createGestures({+ zoompinchHandle,+ totalZoom: zoom.totalZoom,+ currentScale: zoom.currentScale,+ applyZoom: zoom.applyZoom,+ momentum,+ onPrevious: () => props.onPrevious?.(),+ onNext: () => props.onNext?.(),+ onClose: () => dialogContext.close(),+ });++ // Reset zoom + kill any in-flight glide when navigating to a different image.+ createEffect(() => {+ props.src();+ untrack(() => {+ momentum.cancelMomentum();+ zoom.resetZoom();+ });+ });++ return (+ <div+ ref={zoom.setContainer}+ class="fixed inset-0 z-modal flex items-center justify-center"+ style={{+ 'margin-top': 'max(var(--safe-top), 0.5rem)',+ 'margin-bottom': 'max(var(--safe-bottom), 1.5rem)',+ 'margin-left': 'max(var(--safe-left), 0.5rem)',+ 'margin-right': 'max(var(--safe-right), 0.5rem)',+ }}+ >+ <Dialog.Content+ aria-label="Lightbox image viewer"+ class="flex items-center justify-center bg-surface rounded-md overflow-hidden"+ >+ <LightboxToolbar isVisible={true}>+ <Button+ variant="ghost"+ size="icon-md"+ onClick={images.copyToClipboard}+ disabled={images.isBusy() || images.isPrefetching()}+ label="Copy image"+ >+ {images.isCopying() ? <SpinnerIcon /> : <ClipboardIcon />}+ </Button>+ <Button+ variant="ghost"+ size="icon-md"+ onClick={images.downloadImage}+ disabled={images.isBusy() || images.isPrefetching()}+ label="Download image"+ >+ {images.isDownloading() ? <SpinnerIcon /> : <DownloadIcon />}+ </Button>+ <Dialog.CloseButton+ as={Button}+ variant="ghost"+ size="icon-md"+ label="Close"+ >+ <XIcon />+ </Dialog.CloseButton>+ </LightboxToolbar>++ <LightboxChrome+ onPrevious={props.onPrevious}+ onNext={props.onNext}+ navigationHidden={props.navigationHidden}+ indexLabel={props.indexLabel}+ />++ {/* Image */}+ <div class="size-full flex items-center justify-center">+ <Show+ when={props.src()}+ fallback={+ <div class="flex flex-col items-center justify-center gap-2 size-15 border border-edge bg-surface">+ <Spinner class="size-4 animate-spin" />+ </div>+ }+ >+ <Zoompinch+ handleRef={setZoompinchHandle}+ clampBounds+ // Engine min below 1 so gestures can zoom out past the engine's+ // own floor; rebalanceZoom transfers that into card shrinkage+ // and enforces the real floor of total zoom = 1.+ minScale={0.1}+ onUpdate={zoom.onEngineUpdate}+ onWheel={zoom.handleWheel}+ touch={gestures.touch}+ class={cn(+ 'relative overflow-hidden',+ !zoom.cardSize() && 'size-full'+ )}+ style={{+ cursor: gestures.cursor(),+ ...(zoom.cardSize() && {+ width: `${zoom.cardSize()!.w}px`,+ height: `${zoom.cardSize()!.h}px`,+ }),+ }}+ // Give the canvas the image's own aspect ratio so the engine's+ // contain-fit and clamping track the image content, not the+ // (possibly differently-shaped) card.+ canvasStyle={+ zoom.baseSize()+ ? {+ width: `${zoom.baseSize()!.w}px`,+ height: `${zoom.baseSize()!.h}px`,+ }+ : undefined+ }+ >+ <img+ class="size-full sm:min-w-50 sm:max-h-[80vh] object-contain select-none"+ style={{ '-webkit-touch-callout': 'none' }}+ src={props.src()}+ alt="preview"+ onLoad={(e) => zoom.handleImageLoad(e.currentTarget)}+ />+ </Zoompinch>+ </Show>+ </div>+ </Dialog.Content>+ </div>+ );+}diff --git a/js/app/packages/core/component/Lightbox/LightboxChrome.tsx b/js/app/packages/core/component/Lightbox/LightboxChrome.tsxnew file mode 100644index 0000000000..d038a621a6--- /dev/null+++ b/js/app/packages/core/component/Lightbox/LightboxChrome.tsx@@ -0,0 +1,62 @@+import * as stackingContext from '@core/constant/stackingContext';+import { isMobile } from '@core/mobile/isMobile';+import ChevronLeftIcon from '@phosphor/caret-left.svg';+import ChevronRightIcon from '@phosphor/caret-right.svg';+import { cn } from '@ui';+import { type Accessor, Show } from 'solid-js';++type LightboxChromeProps = {+ // Gallery navigation — a direction is disabled when its handler is omitted.+ onPrevious?: () => void;+ onNext?: () => void;+ navigationHidden?: boolean;+ // "2/5" style indicator — rendered when provided.+ indexLabel?: Accessor<string>;+};++const navButtonClass =+ 'absolute top-1/2 -translate-y-1/2 bg-surface backdrop-blur-sm rounded-lg border border-edge p-2 shadow-md hover:bg-surface transition-opacity duration-300 disabled:cursor-not-allowed disabled:opacity-50';++/** Desktop navigation arrows and the image-position indicator. */+export function LightboxChrome(props: LightboxChromeProps) {+ return (+ <>+ {/* Nav arrows — desktop only */}+ <Show when={!isMobile()}>+ <Show when={!props.navigationHidden}>+ <button+ class={cn(navButtonClass, 'left-4')}+ style={{ 'z-index': stackingContext.zModal + 1 }}+ onClick={props.onPrevious}+ disabled={!props.onPrevious}+ aria-label="Previous image"+ >+ <ChevronLeftIcon class="size-5 text-ink" />+ </button>++ <button+ class={cn(navButtonClass, 'right-4')}+ style={{ 'z-index': stackingContext.zModal + 1 }}+ onClick={props.onNext}+ disabled={!props.onNext}+ aria-label="Next image"+ >+ <ChevronRightIcon class="size-5 text-ink" />+ </button>+ </Show>+ </Show>++ {/* Index indicator */}+ <Show when={props.indexLabel}>+ <div+ class="absolute top-4 left-4 bg-surface backdrop-blur-sm rounded-lg border border-edge px-3 py-1.5 shadow-md transition-opacity duration-300"+ style={{ 'z-index': stackingContext.zModal + 1 }}+ >+ <span class="text-sm text-ink font-medium">+ {props.indexLabel?.()}+ </span>+ </div>+ </Show>+ </>+ );+}diff --git a/js/app/packages/core/component/Lightbox/LightboxToolbar.tsx b/js/app/packages/core/component/Lightbox/LightboxToolbar.tsxnew file mode 100644index 0000000000..b5cbe56e24--- /dev/null+++ b/js/app/packages/core/component/Lightbox/LightboxToolbar.tsx@@ -0,0 +1,25 @@+import * as stackingContext from '@core/constant/stackingContext';+import { isMobile } from '@core/mobile/isMobile';+import { cn } from '@ui';+import type { JSX } from 'solid-js';++type LightboxToolbarProps = {+ isVisible: boolean;+ children: JSX.Element;+};++export function LightboxToolbar(props: LightboxToolbarProps) {+ return (+ <div+ class={cn(+ 'absolute top-4 right-4 bg-surface backdrop-blur-sm rounded-lg border border-edge p-1 flex flex-row items-center gap-1 shadow-md transition-opacity duration-300',+ isMobile() || props.isVisible+ ? 'opacity-100'+ : 'opacity-0 pointer-events-none'+ )}+ style={{ 'z-index': stackingContext.zModal + 1 }}+ >+ {props.children}+ </div>+ );+}diff --git a/js/app/packages/core/component/Lightbox/createGestures.ts b/js/app/packages/core/component/Lightbox/createGestures.tsnew file mode 100644index 0000000000..47a33b47c9--- /dev/null+++ b/js/app/packages/core/component/Lightbox/createGestures.ts@@ -0,0 +1,235 @@+import { isMobile } from '@core/mobile/isMobile';+import { isTouchDevice } from '@core/mobile/isTouchDevice';+import {+ type Accessor,+ createEffect,+ createMemo,+ createSignal,+ onCleanup,+} from 'solid-js';+import type { ZoompinchHandle } from '../Zoompinch';+import type { createMomentum } from './createMomentum';++type GesturesInput = {+ zoompinchHandle: Accessor<ZoompinchHandle | undefined>;+ totalZoom: Accessor<number>;+ currentScale: Accessor<number>;+ applyZoom: (+ handle: ZoompinchHandle,+ zoomLevel: number,+ anchor?: [number, number]+ ) => void;+ momentum: ReturnType<typeof createMomentum>;+ onPrevious: () => void;+ onNext: () => void;+ onClose: () => void;+};++/**+ * Routes pointer input across the lightbox's three gesture systems: the+ * zoompinch engine (pan/pinch while zoomed in), single-finger swipes (gallery+ * nav + swipe-to-dismiss while zoomed out), and the inertial fling on release.+ * Also wires desktop keyboard nav and mouse drag / click-to-zoom. Returns the+ * reactive cursor and the touch handlers to hand to <Zoompinch>.+ */+export function createGestures(input: GesturesInput) {+ const {+ zoompinchHandle,+ totalZoom,+ currentScale,+ applyZoom,+ momentum,+ onPrevious,+ onNext,+ onClose,+ } = input;++ // Reactive cursor state — drives the cursor style on the Zoompinch wrapper.+ const [isDragging, setIsDragging] = createSignal(false);++ const cursor = createMemo(() => {+ if (isDragging() && currentScale() > 1.01) return 'grab';+ if (totalZoom() > 1.01) return 'zoom-out';+ return 'zoom-in';+ });++ // Single-finger swipe state. On touch devices, when fully zoomed out, a+ // single-finger drag is a swipe gesture: horizontal navigates the gallery,+ // a downward swipe dismisses the lightbox. The axis is locked on the first+ // clearly-directional movement so the two don't fight.+ const SWIPE_DISMISS_DISTANCE = 100; // px of downward travel to dismiss+ let swipeStartX = 0;+ let swipeStartY = 0;+ let swipeEndX = 0;+ let swipeEndY = 0;+ let swipeAxis: 'x' | 'y' | null = null;+ let isSwiping = false;+ let zoompinchHandlingTouch = false;++ const onStart = (e: TouchEvent, engine: ZoompinchHandle['engine']) => {+ // Any new touch catches an in-flight glide (tap-to-stop).+ momentum.cancelMomentum();+ const doSwipeDetection =+ isTouchDevice() && e.touches.length === 1 && totalZoom() <= 1.01;+ if (doSwipeDetection) {+ swipeStartX = swipeEndX = e.touches[0].clientX;+ swipeStartY = swipeEndY = e.touches[0].clientY;+ swipeAxis = null;+ isSwiping = false;+ zoompinchHandlingTouch = false;+ } else {+ engine.handleTouchstart(e);+ zoompinchHandlingTouch = true;+ momentum.resetMomentumTracking(e, engine);+ }+ };++ const onWindowMove = (e: TouchEvent, engine: ZoompinchHandle['engine']) => {+ if (zoompinchHandlingTouch) {+ engine.handleTouchmove(e);+ // Track velocity for single-finger pans only; a pinch shouldn't fling.+ if (e.touches.length === 1) momentum.sampleMomentum(e);+ else momentum.resetMomentumTracking(e, engine);+ return;+ }+ // Second finger appeared mid-gesture: switch to zoompinch+ if (e.touches.length > 1) {+ engine.handleTouchstart(e);+ zoompinchHandlingTouch = true;+ isSwiping = false;+ momentum.resetMomentumTracking(e, engine);+ return;+ }+ swipeEndX = e.touches[0].clientX;+ swipeEndY = e.touches[0].clientY;+ const dx = swipeEndX - swipeStartX;+ const dy = swipeEndY - swipeStartY;+ // Lock to the dominant axis once the gesture is clearly directional.+ if (!swipeAxis && Math.hypot(dx, dy) > 10) {+ swipeAxis = Math.abs(dx) > Math.abs(dy) ? 'x' : 'y';+ }+ // Downward-only on the y axis — an upward drag is left alone.+ if (+ (swipeAxis === 'x' && Math.abs(dx) > 30) ||+ (swipeAxis === 'y' && dy > 30)+ ) {+ isSwiping = true;+ }+ if (isSwiping) e.preventDefault();+ };++ const onWindowEnd = (e: TouchEvent, engine: ZoompinchHandle['engine']) => {+ if (zoompinchHandlingTouch) {+ engine.handleTouchend(e);+ // Fling only when the last finger lifts after a single-finger pan while+ // zoomed in — releasing one finger of a pinch leaves touches behind, and+ // there's nothing to pan when the image already fits.+ if (e.touches.length === 0) {+ zoompinchHandlingTouch = false;+ if (totalZoom() > 1.01) momentum.startMomentum(e, engine);+ } else {+ momentum.resetMomentumTracking(e, engine);+ }+ return;+ }+ if (isSwiping && totalZoom() <= 1.01) {+ if (swipeAxis === 'x') {+ const diff = swipeStartX - swipeEndX;+ if (Math.abs(diff) > 50) {+ if (diff > 0) onNext();+ else onPrevious();+ }+ } else if (+ swipeAxis === 'y' &&+ swipeEndY - swipeStartY > SWIPE_DISMISS_DISTANCE+ ) {+ onClose();+ }+ }+ swipeAxis = null;+ isSwiping = false;+ swipeStartX = swipeStartY = swipeEndX = swipeEndY = 0;+ zoompinchHandlingTouch = false;+ };++ // Keyboard nav + desktop mouse drag / click-to-zoom — active while the+ // Zoompinch handle is set.+ createEffect(() => {+ const handle = zoompinchHandle();+ if (!handle) return;+ const { engine, wrapperElement: wrapper } = handle;++ const handleKeyDown = (e: KeyboardEvent) => {+ if (e.key === 'Escape') {+ onClose();+ } else if (e.key === 'ArrowLeft') {+ e.preventDefault();+ onPrevious();+ } else if (e.key === 'ArrowRight') {+ e.preventDefault();+ onNext();+ }+ };+ window.addEventListener('keydown', handleKeyDown);++ if (!isMobile()) {+ // Track dragging so click-to-zoom and cursor stay in sync+ let isMouseDown = false;+ let mouseDownX = 0;+ let mouseDownY = 0;++ const handleMouseDown = (e: MouseEvent) => {+ isMouseDown = true;+ mouseDownX = e.clientX;+ mouseDownY = e.clientY;+ setIsDragging(false);+ };+ const handleWindowMouseMove = (e: MouseEvent) => {+ if (!isMouseDown) return;+ if (Math.hypot(e.clientX - mouseDownX, e.clientY - mouseDownY) > 5) {+ setIsDragging(true);+ }+ };+ const handleWindowMouseUp = () => {+ isMouseDown = false;+ // Delay reset so the click event (which fires after mouseup) can still+ // read isDragging=true and suppress the zoom-out action.+ setTimeout(() => setIsDragging(false), 0);+ };++ // Click-to-zoom: zoom in at cursor position, or reset if already zoomed+ const handleClick = (e: MouseEvent) => {+ if (isDragging()) return;+ const b = engine.wrapperBounds;+ const relX = (e.clientX - b.x) / b.width;+ const relY = (e.clientY - b.y) / b.height;+ if (totalZoom() <= 1.01) {+ applyZoom(handle, 2.5, [relX, relY]);+ } else {+ applyZoom(handle, 1);+ }+ };++ wrapper.addEventListener('mousedown', handleMouseDown);+ window.addEventListener('mousemove', handleWindowMouseMove);+ window.addEventListener('mouseup', handleWindowMouseUp);+ wrapper.addEventListener('click', handleClick);++ onCleanup(() => {+ wrapper.removeEventListener('mousedown', handleMouseDown);+ window.removeEventListener('mousemove', handleWindowMouseMove);+ window.removeEventListener('mouseup', handleWindowMouseUp);+ wrapper.removeEventListener('click', handleClick);+ });+ }++ onCleanup(() => {+ window.removeEventListener('keydown', handleKeyDown);+ });+ });++ return {+ cursor,+ touch: { onStart, onWindowMove, onWindowEnd },+ };+}diff --git a/js/app/packages/core/component/Lightbox/createImageActions.ts b/js/app/packages/core/component/Lightbox/createImageActions.tsnew file mode 100644index 0000000000..f916fdc6fe--- /dev/null+++ b/js/app/packages/core/component/Lightbox/createImageActions.ts@@ -0,0 +1,110 @@+import {+ copyImageToClipboard,+ downloadImage as downloadImageAction,+} from '@core/util/imageActions';+import { platformFetch } from '@core/util/platformFetch';+import { isIOS } from '@solid-primitives/platform';+import {+ type Accessor,+ createEffect,+ createSignal,+ onCleanup,+ untrack,+} from 'solid-js';++type ImageActionsInput = {+ // Current image to display.+ src: Accessor<string | undefined>;+ // Used for the download filename.+ imageId: Accessor<string>;+ // Optional pre-fetched blob override (e.g. DSS images). Falls back to fetching `src`.+ getBlob?: () => Promise<Blob | undefined>;+};++/**+ * Copy / download for the current image, with an iOS pre-fetch so the blob is+ * in memory before the user taps. Exposes loading flags so the buttons can show+ * a spinner and stay disabled until the share is guaranteed to be synchronous.+ */+export function createImageActions(input: ImageActionsInput) {+ const fetchBlob = async (): Promise<Blob | undefined> => {+ if (input.getBlob) return input.getBlob();+ const url = input.src();+ if (!url) return undefined;+ return (await platformFetch(url)).blob();+ };++ // Pre-fetch the blob on iOS so it is already in memory when the user taps+ // copy/download. This keeps navigator.share() close to synchronous with the+ // gesture — the user-activation window expires if a network round-trip is+ // needed. Desktop clipboard doesn't have this constraint.+ const [cachedBlob, setCachedBlob] = createSignal<Blob | undefined>();+ // True while the iOS pre-fetch is in flight. We surface this as a loading+ // state on the copy/download buttons so the blob is guaranteed to be in+ // memory before the user can tap. Without this, tapping download on a large+ // image (whose pre-fetch hasn't finished yet) falls through to an awaited+ // network fetch, which consumes the tap's user activation and makes+ // navigator.share() silently no-op until a second tap.+ const [isPrefetching, setIsPrefetching] = createSignal(false);+ if (isIOS) {+ createEffect(() => {+ const currentSrc = input.src(); // re-fetch when navigating to a new image+ let isStale = false;+ onCleanup(() => {+ isStale = true;+ });++ setCachedBlob(undefined);+ setIsPrefetching(true);+ untrack(() => fetchBlob())+ .then((blob) => {+ if (isStale || input.src() !== currentSrc) return;+ if (blob) setCachedBlob(blob);+ })+ .catch(() => {})+ .finally(() => {+ if (!isStale && input.src() === currentSrc) setIsPrefetching(false);+ });+ });+ }+ const fetchBlobCached = (): Promise<Blob | undefined> => {+ const cached = cachedBlob();+ return cached ? Promise.resolve(cached) : fetchBlob();+ };++ const [activeAction, setActiveAction] = createSignal<+ 'copy' | 'download' | undefined+ >();+ const isBusy = () => activeAction() != null;+ const isCopying = () => activeAction() === 'copy';+ const isDownloading = () => activeAction() === 'download';++ const copyToClipboard = async () => {+ if (isBusy()) return;+ setActiveAction('copy');+ try {+ await copyImageToClipboard(fetchBlobCached, input.src() ?? '');+ } finally {+ setActiveAction(undefined);+ }+ };++ const downloadImage = async () => {+ if (isBusy()) return;+ setActiveAction('download');+ try {+ await downloadImageAction(fetchBlobCached, input.imageId());+ } finally {+ setActiveAction(undefined);+ }+ };++ return {+ isPrefetching,+ isBusy,+ isCopying,+ isDownloading,+ copyToClipboard,+ downloadImage,+ };+}diff --git a/js/app/packages/core/component/Lightbox/createMomentum.ts b/js/app/packages/core/component/Lightbox/createMomentum.tsnew file mode 100644index 0000000000..6d0d5c532f--- /dev/null+++ b/js/app/packages/core/component/Lightbox/createMomentum.ts@@ -0,0 +1,178 @@+import { type Accessor, onCleanup } from 'solid-js';+import type { ZoompinchHandle } from '../Zoompinch';++/**+ * Inertial panning ("momentum"). After a single-finger pan while zoomed in,+ * releasing lets the image keep gliding in the release direction and ease to a+ * stop. Release velocity is sampled from a short window of touch positions,+ * including the final changedTouches point from touchend; the rAF loop then+ * replays that velocity through setTranslateFromUserGesture, so the engine still+ * clamps every frame and naturally kills motion at the bounds.+ */+export function createMomentum(+ zoompinchHandle: Accessor<ZoompinchHandle | undefined>+) {+ const MOMENTUM_FRAME_MS = 1000 / 60; // one 60fps frame, the friction base unit+ const MOMENTUM_MAX_FRAME_MS = 32; // ~2 frames; cap dt to avoid post-stall jumps+ const MOMENTUM_FRICTION = 0.86; // velocity retained per 60fps frame+ const MOMENTUM_MIN_SPEED = 0.015; // px/ms — below this the glide stops+ const MOMENTUM_MAX_SPEED = 2.75; // px/ms — clamp pathologically fast flicks+ const MOMENTUM_SAMPLE_WINDOW_MS = 160;+ const MOMENTUM_CLAMP_EPSILON_PX = 0.01; // sub-px move counts as "didn't move"+ let momentumVx = 0; // px/ms+ let momentumVy = 0;+ let momentumRaf: number | undefined;+ let momentumActiveTouchId: number | undefined;+ let momentumStartX = 0;+ let momentumStartY = 0;+ let momentumStartTranslateX = 0;+ let momentumStartTranslateY = 0;+ let momentumSamples: { x: number; y: number; t: number }[] = [];++ const cancelMomentum = () => {+ if (momentumRaf !== undefined) {+ cancelAnimationFrame(momentumRaf);+ momentumRaf = undefined;+ }+ };+ onCleanup(cancelMomentum);++ const eventTime = (e: TouchEvent) => e.timeStamp || performance.now();++ const getMomentumTouch = (touches: TouchList) => {+ if (momentumActiveTouchId === undefined) return touches[0];+ return (+ Array.from(touches).find(+ (touch) => touch.identifier === momentumActiveTouchId+ ) ?? touches[0]+ );+ };++ // Re-baseline velocity tracking when an engine-handled gesture starts or+ // changes touch count so the next single-finger move measures cleanly.+ const resetMomentumTracking = (+ e: TouchEvent,+ engine: ZoompinchHandle['engine']+ ) => {+ momentumVx = 0;+ momentumVy = 0;+ momentumActiveTouchId = undefined;+ const touch = e.touches.length === 1 ? e.touches[0] : undefined;+ const t = eventTime(e);+ if (!touch) {+ momentumSamples = [];+ return;+ }+ momentumActiveTouchId = touch.identifier;+ momentumStartX = touch.clientX;+ momentumStartY = touch.clientY;+ momentumStartTranslateX = engine.translateX;+ momentumStartTranslateY = engine.translateY;+ momentumSamples = [{ x: touch.clientX, y: touch.clientY, t }];+ };++ const sampleMomentumTouch = (touch: Touch | undefined, t: number) => {+ if (!touch) return;+ momentumSamples.push({ x: touch.clientX, y: touch.clientY, t });+ while (+ momentumSamples.length > 1 &&+ t - momentumSamples[0].t > MOMENTUM_SAMPLE_WINDOW_MS+ ) {+ momentumSamples.shift();+ }+ const first = momentumSamples[0];+ const last = momentumSamples[momentumSamples.length - 1];+ const dt = last.t - first.t;+ if (dt <= 0) {+ momentumVx = 0;+ momentumVy = 0;+ } else {+ momentumVx = (last.x - first.x) / dt;+ momentumVy = (last.y - first.y) / dt;+ }+ };++ // Fold the latest single-finger touch position into the release velocity.+ const sampleMomentum = (e: TouchEvent) => {+ sampleMomentumTouch(getMomentumTouch(e.touches), eventTime(e));+ };++ const startMomentum = (e: TouchEvent, engine: ZoompinchHandle['engine']) => {+ cancelMomentum();+ const activeTouchId = momentumActiveTouchId;+ if (activeTouchId === undefined || momentumSamples.length === 0) return;+ const releaseTime = eventTime(e);+ const releaseTouch = Array.from(e.changedTouches).find(+ (touch) => touch.identifier === activeTouchId+ );+ if (!releaseTouch) return;+ engine.setTranslateFromUserGesture(+ momentumStartTranslateX + releaseTouch.clientX - momentumStartX,+ momentumStartTranslateY + releaseTouch.clientY - momentumStartY+ );+ engine.update();+ sampleMomentumTouch(releaseTouch, releaseTime);+ const speed = Math.hypot(momentumVx, momentumVy);+ if (speed < MOMENTUM_MIN_SPEED) return;+ if (speed > MOMENTUM_MAX_SPEED) {+ const k = MOMENTUM_MAX_SPEED / speed;+ momentumVx *= k;+ momentumVy *= k;+ }++ let lastFrame = performance.now();+ const step = () => {+ // Bail if the engine went away (unmount / image swap) mid-glide.+ if (!zoompinchHandle()) {+ momentumRaf = undefined;+ return;+ }+ const now = performance.now();+ const dt = Math.min(now - lastFrame, MOMENTUM_MAX_FRAME_MS);+ lastFrame = now;++ const beforeX = engine.translateX;+ const beforeY = engine.translateY;+ const attemptedDx = Math.abs(momentumVx * dt);+ const attemptedDy = Math.abs(momentumVy * dt);+ engine.setTranslateFromUserGesture(+ engine.translateX + momentumVx * dt,+ engine.translateY + momentumVy * dt+ );+ engine.update();++ // An axis pinned at a clamp edge stops moving — drop its velocity so the+ // glide doesn't coast in place.+ if (+ attemptedDx > MOMENTUM_CLAMP_EPSILON_PX &&+ Math.abs(engine.translateX - beforeX) < MOMENTUM_CLAMP_EPSILON_PX+ ) {+ momentumVx = 0;+ }+ if (+ attemptedDy > MOMENTUM_CLAMP_EPSILON_PX &&+ Math.abs(engine.translateY - beforeY) < MOMENTUM_CLAMP_EPSILON_PX+ ) {+ momentumVy = 0;+ }++ const decay = MOMENTUM_FRICTION ** (dt / MOMENTUM_FRAME_MS);+ momentumVx *= decay;+ momentumVy *= decay;++ if (Math.hypot(momentumVx, momentumVy) < MOMENTUM_MIN_SPEED) {+ momentumRaf = undefined;+ return;+ }+ momentumRaf = requestAnimationFrame(step);+ };+ momentumRaf = requestAnimationFrame(step);+ };++ return {+ cancelMomentum,+ resetMomentumTracking,+ sampleMomentum,+ startMomentum,+ };+}diff --git a/js/app/packages/core/component/Lightbox/createZoomModel.ts b/js/app/packages/core/component/Lightbox/createZoomModel.tsnew file mode 100644index 0000000000..1c334a8513--- /dev/null+++ b/js/app/packages/core/component/Lightbox/createZoomModel.ts@@ -0,0 +1,224 @@+import { isMobile } from '@core/mobile/isMobile';+import { type Accessor, createMemo, createSignal } from 'solid-js';+import type { ZoompinchHandle } from '../Zoompinch';++type Size = { w: number; h: number };++/**+ * The lightbox's zoom model. Zoom is split into card growth × engine scale:+ * each card axis grows with the zoom level independently until it hits the+ * available area, so a capped axis starts cropping while the other keeps+ * growing. The canvas keeps the image's aspect ratio, and the engine's model is+ * "scale 1 = canvas contain-fits the wrapper", so the displayed image size is+ * always base × zoom and exactly fills the card on every uncapped axis.+ */+export function createZoomModel(+ zoompinchHandle: Accessor<ZoompinchHandle | undefined>+) {+ let containerEl: HTMLDivElement | undefined;+ const setContainer = (el: HTMLDivElement) => {+ containerEl = el;+ };++ const [baseSize, setBaseSize] = createSignal<Size>();+ const [zoom, setZoom] = createSignal(1);+ const [currentScale, setCurrentScale] = createSignal(1);+ // Suppresses the rebalance pass while we apply a zoom ourselves.+ let inApplyZoom = false;++ const availableArea = () => {+ if (!containerEl) return undefined;+ return {+ w: containerEl.clientWidth,+ // Mirror the img's sm:max-h-[80vh] cap on desktop+ h: isMobile()+ ? containerEl.clientHeight+ : Math.min(containerEl.clientHeight, window.innerHeight * 0.8),+ };+ };++ const cardSizeFor = (base: Size, z: number) => {+ const avail = availableArea();+ if (!avail) return { w: base.w * z, h: base.h * z };+ return {+ w: Math.min(base.w * z, avail.w),+ h: Math.min(base.h * z, avail.h),+ };+ };++ const cardSize = createMemo(() => {+ const base = baseSize();+ return base ? cardSizeFor(base, zoom()) : undefined;+ });++ // The engine's naturalScale: how far the base-aspect canvas is scaled to+ // contain-fit the card. Engine scale carries the rest of the total zoom.+ const containFactor = (base: Size, card: Size) =>+ Math.min(card.w / base.w, card.h / base.h);++ const totalZoom = createMemo(() => {+ const base = baseSize();+ const card = cardSize();+ return base && card ? currentScale() * containFactor(base, card) : 1;+ });++ // The unzoomed card size: contain-fit into the available area without+ // upscaling, with the img's sm:min-w-50 floor on desktop.+ const measureBase = (img: HTMLImageElement) => {+ const avail = availableArea();+ const nw = img.naturalWidth;+ const nh = img.naturalHeight;+ if (!avail || !nw || !nh) return undefined;+ const fit = Math.min(avail.w / nw, avail.h / nh, 1);+ const width = Math.max(nw * fit, isMobile() ? 0 : 200);+ const scale = width / nw;+ return { w: width, h: nh * scale };+ };++ // The engine caches wrapper/canvas bounds via ResizeObservers, which fire+ // after we resize the card. Refresh them synchronously so transforms applied+ // in the same tick use the new geometry.+ const syncEngineBounds = (handle: ZoompinchHandle) => {+ const { engine, wrapperElement } = handle;+ engine.wrapperBounds = wrapperElement.getBoundingClientRect();+ const canvas = wrapperElement.querySelector(+ '.canvas'+ ) as HTMLElement | null;+ if (canvas) {+ // offsetWidth/Height: layout size, unaffected by the engine's transform+ engine.canvasBounds = {+ ...engine.canvasBounds,+ width: canvas.offsetWidth,+ height: canvas.offsetHeight,+ };+ }+ };++ // The canvas-relative (0-1) content point currently under the wrapper center.+ const centerCanvasRel = (+ engine: ZoompinchHandle['engine']+ ): [number, number] => {+ const [cx, cy] = engine.normalizeClientCoords(+ engine.wrapperInnerX + engine.wrapperInnerWidth / 2,+ engine.wrapperInnerY + engine.wrapperInnerHeight / 2+ );+ return [cx / engine.canvasBounds.width, cy / engine.canvasBounds.height];+ };++ const applyZoom = (+ handle: ZoompinchHandle,+ zoomLevel: number,+ anchor?: [number, number]+ ) => {+ const { engine } = handle;+ const base = baseSize();+ const z = Math.max(1, zoomLevel);+ const f = base ? containFactor(base, cardSizeFor(base, z)) : 1;+ // Without an explicit anchor, keep whatever content point sits at the card+ // center fixed across the resize — read it from the old geometry first.+ const wrapperAnchor = anchor ?? ([0.5, 0.5] as [number, number]);+ const canvasAnchor = anchor ?? centerCanvasRel(engine);+ inApplyZoom = true;+ setZoom(z);+ syncEngineBounds(handle);+ engine.applyTransform(z / f, wrapperAnchor, canvasAnchor);+ inApplyZoom = false;+ };++ // Continuous wheel / trackpad zoom. The engine computes the new scale (so the+ // zoom feel is unchanged), but we discard its cursor-anchored translate and+ // re-apply the zoom anchored at the *pre-gesture* view center via applyZoom.+ // Center-anchoring is what keeps the image continuous across the point where+ // an axis stops growing and starts cropping: while an axis is still growing+ // there is no overflow on it, so the engine force-centers it and an+ // off-center anchor's offset is suppressed — it would otherwise snap free the+ // instant the axis caps, producing a visible jump. Non-zoom wheel events pan.+ const handleWheel = (e: WheelEvent, engine: ZoompinchHandle['engine']) => {+ const handle = zoompinchHandle();+ const base = baseSize();+ const card = cardSize();+ if (e.ctrlKey && handle && base && card) {+ e.preventDefault();+ const anchor = centerCanvasRel(engine); // capture before the engine moves+ // Let the engine derive the new scale, but suppress the rebalance pass —+ // we re-apply the zoom ourselves below with the correct anchor.+ inApplyZoom = true;+ engine.handleWheel(e);+ inApplyZoom = false;+ applyZoom(handle, engine.scale * containFactor(base, card), anchor);+ return;+ }+ // Pan. The engine's own wheel-pan multiplies the delta by 25 and only+ // normalizes when |delta| >= 100, so trackpads and hi-res mice — whose+ // deltas are small (often fractional) — lurch hundreds of px per event,+ // snapping straight to the clamp edge. Pan 1:1 with the real pixel delta+ // instead. (deltaMode: 0 = px, 1 = lines, 2 = pages.)+ e.preventDefault();+ const unit =+ e.deltaMode === 1+ ? 16+ : e.deltaMode === 2+ ? engine.wrapperBounds.height+ : 1;+ engine.setTranslateFromUserGesture(+ engine.translateX - e.deltaX * unit,+ engine.translateY - e.deltaY * unit+ );+ engine.update();+ };++ // Rebalance after engine-driven zoom (wheel/pinch): move as much of the+ // total zoom as possible into card growth, leaving the rest on the engine.+ // Runs synchronously inside the engine's update event, before paint.+ const rebalanceZoom = (engine: ZoompinchHandle['engine']) => {+ if (inApplyZoom) return;+ const handle = zoompinchHandle();+ const base = baseSize();+ const card = cardSize();+ if (!handle || !base || !card) return;+ const z = Math.max(1, engine.scale * containFactor(base, card));+ const target = cardSizeFor(base, z);+ if (+ Math.abs(z / containFactor(base, target) - engine.scale) > 0.001 ||+ Math.abs(target.w - card.w) > 0.5 ||+ Math.abs(target.h - card.h) > 0.5+ ) {+ applyZoom(handle, z);+ }+ };++ // Forwarded to Zoompinch's onUpdate — track the engine scale and rebalance.+ const onEngineUpdate = (engine: ZoompinchHandle['engine']) => {+ setCurrentScale(engine.scale);+ rebalanceZoom(engine);+ };++ // Reset to the zoom floor (1): apply it to the live handle, or just record+ // the level when there's no handle yet.+ const resetZoom = () => {+ const handle = zoompinchHandle();+ if (handle) applyZoom(handle, 1);+ else setZoom(1);+ };++ // Measure the freshly-loaded image and snap to the zoom floor.+ const handleImageLoad = (img: HTMLImageElement) => {+ const base = measureBase(img);+ if (!base) return;+ setBaseSize(base);+ resetZoom();+ };++ return {+ setContainer,+ baseSize,+ cardSize,+ totalZoom,+ currentScale,+ applyZoom,+ handleWheel,+ onEngineUpdate,+ handleImageLoad,+ resetZoom,+ };+}diff --git a/js/app/packages/core/component/Lightbox/index.ts b/js/app/packages/core/component/Lightbox/index.tsnew file mode 100644index 0000000000..3f48c7174a--- /dev/null+++ b/js/app/packages/core/component/Lightbox/index.ts@@ -0,0 +1,2 @@+export { Lightbox } from './Lightbox';+export { LightboxToolbar } from './LightboxToolbar';