@zyro-sec/ui 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +29 -0
- package/bunfig.toml +2 -0
- package/package.json +21 -40
- package/perf/motion.bench.ts +14 -0
- package/perf/motion.ts +16 -0
- package/script/build-oc2-v2-overrides.ts +38 -0
- package/script/colors.txt +288 -0
- package/script/generate-spinner.ts +121 -0
- package/script/pack.ts +5 -0
- package/script/tailwind.ts +24 -0
- package/src/actions/button/button.stories.tsx +158 -0
- package/src/actions/icon-button/icon-button.stories.tsx +122 -0
- package/src/actions/split-button/split-button.stories.tsx +59 -0
- package/src/assets/favicon/apple-touch-icon-v3.png +0 -0
- package/src/assets/favicon/apple-touch-icon.png +0 -0
- package/src/assets/favicon/favicon-96x96-v3.png +0 -0
- package/src/assets/favicon/favicon-96x96.png +0 -0
- package/src/assets/favicon/favicon-v3.ico +0 -0
- package/src/assets/favicon/favicon-v3.svg +7 -0
- package/src/assets/favicon/favicon.ico +0 -0
- package/src/assets/favicon/favicon.svg +7 -0
- package/src/assets/favicon/site.webmanifest +24 -0
- package/src/assets/favicon/web-app-manifest-192x192.png +0 -0
- package/src/assets/favicon/web-app-manifest-512x512.png +0 -0
- package/src/assets/icons/provider/302ai.svg +7 -0
- package/src/assets/icons/provider/abacus.svg +14 -0
- package/src/assets/icons/provider/aihubmix.svg +3 -0
- package/src/assets/icons/provider/alibaba-cn.svg +3 -0
- package/src/assets/icons/provider/alibaba-coding-plan-cn.svg +3 -0
- package/src/assets/icons/provider/alibaba-coding-plan.svg +3 -0
- package/src/assets/icons/provider/alibaba.svg +3 -0
- package/src/assets/icons/provider/amazon-bedrock.svg +3 -0
- package/src/assets/icons/provider/anthropic.svg +3 -0
- package/src/assets/icons/provider/azure-cognitive-services.svg +24 -0
- package/src/assets/icons/provider/azure.svg +3 -0
- package/src/assets/icons/provider/bailing.svg +3 -0
- package/src/assets/icons/provider/baseten.svg +3 -0
- package/src/assets/icons/provider/berget.svg +3 -0
- package/src/assets/icons/provider/cerebras.svg +4 -0
- package/src/assets/icons/provider/chutes.svg +24 -0
- package/src/assets/icons/provider/clarifai.svg +24 -0
- package/src/assets/icons/provider/claude-agent.svg +3 -0
- package/src/assets/icons/provider/cloudferro-sherlock.svg +5 -0
- package/src/assets/icons/provider/cloudflare-ai-gateway.svg +4 -0
- package/src/assets/icons/provider/cloudflare-workers-ai.svg +4 -0
- package/src/assets/icons/provider/cohere.svg +5 -0
- package/src/assets/icons/provider/cortecs.svg +24 -0
- package/src/assets/icons/provider/deepinfra.svg +4 -0
- package/src/assets/icons/provider/deepseek.svg +3 -0
- package/src/assets/icons/provider/digitalocean.svg +6 -0
- package/src/assets/icons/provider/dinference.svg +1 -0
- package/src/assets/icons/provider/drun.svg +8 -0
- package/src/assets/icons/provider/evroc.svg +3 -0
- package/src/assets/icons/provider/fastrouter.svg +3 -0
- package/src/assets/icons/provider/fireworks-ai.svg +3 -0
- package/src/assets/icons/provider/firmware.svg +18 -0
- package/src/assets/icons/provider/friendli.svg +3 -0
- package/src/assets/icons/provider/github-copilot.svg +3 -0
- package/src/assets/icons/provider/github-models.svg +3 -0
- package/src/assets/icons/provider/gitlab.svg +3 -0
- package/src/assets/icons/provider/google-vertex-anthropic.svg +24 -0
- package/src/assets/icons/provider/google-vertex.svg +10 -0
- package/src/assets/icons/provider/google.svg +3 -0
- package/src/assets/icons/provider/groq.svg +3 -0
- package/src/assets/icons/provider/helicone.svg +6 -0
- package/src/assets/icons/provider/huggingface.svg +3 -0
- package/src/assets/icons/provider/iflowcn.svg +3 -0
- package/src/assets/icons/provider/inception.svg +4 -0
- package/src/assets/icons/provider/inference.svg +3 -0
- package/src/assets/icons/provider/io-net.svg +5 -0
- package/src/assets/icons/provider/jiekou.svg +4 -0
- package/src/assets/icons/provider/kilo.svg +4 -0
- package/src/assets/icons/provider/kimi-for-coding.svg +3 -0
- package/src/assets/icons/provider/kuae-cloud-coding-plan.svg +3 -0
- package/src/assets/icons/provider/llama.svg +3 -0
- package/src/assets/icons/provider/lmstudio.svg +24 -0
- package/src/assets/icons/provider/lucidquery.svg +3 -0
- package/src/assets/icons/provider/meganova.svg +3 -0
- package/src/assets/icons/provider/meituan.svg +6 -0
- package/src/assets/icons/provider/meta.svg +3 -0
- package/src/assets/icons/provider/minimax-cn-coding-plan.svg +24 -0
- package/src/assets/icons/provider/minimax-cn.svg +3 -0
- package/src/assets/icons/provider/minimax-coding-plan.svg +24 -0
- package/src/assets/icons/provider/minimax.svg +3 -0
- package/src/assets/icons/provider/mistral.svg +3 -0
- package/src/assets/icons/provider/moark.svg +3 -0
- package/src/assets/icons/provider/modelscope.svg +5 -0
- package/src/assets/icons/provider/moonshotai-cn.svg +3 -0
- package/src/assets/icons/provider/moonshotai.svg +3 -0
- package/src/assets/icons/provider/morph.svg +24 -0
- package/src/assets/icons/provider/nano-gpt.svg +3 -0
- package/src/assets/icons/provider/nebius.svg +4 -0
- package/src/assets/icons/provider/nova.svg +3 -0
- package/src/assets/icons/provider/novita-ai.svg +10 -0
- package/src/assets/icons/provider/nvidia.svg +3 -0
- package/src/assets/icons/provider/ollama-cloud.svg +7 -0
- package/src/assets/icons/provider/openai.svg +3 -0
- package/src/assets/icons/provider/opencode-go.svg +3 -0
- package/src/assets/icons/provider/opencode.svg +3 -0
- package/src/assets/icons/provider/openrouter.svg +3 -0
- package/src/assets/icons/provider/ovhcloud.svg +3 -0
- package/src/assets/icons/provider/perplexity-agent.svg +3 -0
- package/src/assets/icons/provider/perplexity.svg +3 -0
- package/src/assets/icons/provider/poe.svg +7 -0
- package/src/assets/icons/provider/privatemode-ai.svg +5 -0
- package/src/assets/icons/provider/qihang-ai.svg +9 -0
- package/src/assets/icons/provider/qiniu-ai.svg +7 -0
- package/src/assets/icons/provider/reflection.svg +3 -0
- package/src/assets/icons/provider/requesty.svg +24 -0
- package/src/assets/icons/provider/sap-ai-core.svg +24 -0
- package/src/assets/icons/provider/scaleway.svg +5 -0
- package/src/assets/icons/provider/siliconflow-cn.svg +3 -0
- package/src/assets/icons/provider/siliconflow.svg +3 -0
- package/src/assets/icons/provider/stackit.svg +4 -0
- package/src/assets/icons/provider/stepfun.svg +16 -0
- package/src/assets/icons/provider/submodel.svg +3 -0
- package/src/assets/icons/provider/synthetic.svg +24 -0
- package/src/assets/icons/provider/tencent-coding-plan.svg +5 -0
- package/src/assets/icons/provider/tencent.svg +3 -0
- package/src/assets/icons/provider/thinkingmachines.svg +3 -0
- package/src/assets/icons/provider/togetherai.svg +4 -0
- package/src/assets/icons/provider/upstage.svg +24 -0
- package/src/assets/icons/provider/v0.svg +3 -0
- package/src/assets/icons/provider/venice.svg +4 -0
- package/src/assets/icons/provider/vercel.svg +3 -0
- package/src/assets/icons/provider/vivgrid.svg +4 -0
- package/src/assets/icons/provider/vultr.svg +6 -0
- package/src/assets/icons/provider/wandb.svg +24 -0
- package/src/assets/icons/provider/xai.svg +3 -0
- package/src/assets/icons/provider/xiaomi.svg +3 -0
- package/src/assets/icons/provider/zai-coding-plan.svg +3 -0
- package/src/assets/icons/provider/zai.svg +3 -0
- package/src/assets/icons/provider/zenmux.svg +4 -0
- package/src/assets/icons/provider/zhipuai-coding-plan.svg +3 -0
- package/src/assets/icons/provider/zhipuai.svg +3 -0
- package/src/assets/images/social-share-black.png +0 -0
- package/src/assets/images/social-share-zen.png +0 -0
- package/src/assets/images/social-share.png +0 -0
- package/src/components/animated-number-machine.test.ts +140 -0
- package/src/components/animated-number.stories.tsx +35 -0
- package/src/components/app-icon.stories.tsx +73 -0
- package/src/components/card.stories.tsx +101 -0
- package/src/components/collapsible.stories.tsx +76 -0
- package/src/components/context-menu.stories.tsx +150 -0
- package/src/components/diagram-preview.stories.tsx +62 -0
- package/src/components/dock-surface.stories.tsx +26 -0
- package/src/components/document-surface.stories.tsx +42 -0
- package/src/components/error-details.stories.tsx +72 -0
- package/src/components/favicon.stories.tsx +49 -0
- package/src/components/file-icon-machine.test.ts +52 -0
- package/src/components/file-icon.stories.tsx +93 -0
- package/src/components/file-icons/sprite.svg +11707 -0
- package/src/components/font.stories.tsx +48 -0
- package/src/components/hover-card.stories.tsx +79 -0
- package/src/components/image-flight-machine.test.ts +184 -0
- package/src/components/image-flight.test.ts +173 -0
- package/src/components/image-preview.stories.tsx +392 -0
- package/src/components/image-zoom-machine.test.ts +154 -0
- package/src/components/image-zoom.test.ts +96 -0
- package/src/components/list.stories.tsx +221 -0
- package/src/components/logo.stories.tsx +62 -0
- package/src/components/menu-scroll.stories.tsx +73 -0
- package/src/components/popover-machine.test.ts +150 -0
- package/src/components/popover.stories.tsx +84 -0
- package/src/components/preview-controls.stories.tsx +40 -0
- package/src/components/progress.stories.tsx +69 -0
- package/src/components/provider-icon.stories.tsx +75 -0
- package/src/components/resize-handle-machine.test.ts +164 -0
- package/src/components/resize-handle.stories.tsx +81 -0
- package/src/components/rolling-number.stories.tsx +23 -0
- package/src/components/scroll-view-machine.test.ts +141 -0
- package/src/components/scroll-view.stories.tsx +68 -0
- package/src/components/scroll-view.test.ts +114 -0
- package/src/components/spinner-radar.test.ts +61 -0
- package/src/components/spinner.stories.tsx +40 -0
- package/src/components/sticky-accordion-header.stories.tsx +64 -0
- package/src/components/text-field-machine.test.ts +99 -0
- package/src/components/text-field.stories.tsx +132 -0
- package/src/components/text-reveal-machine.test.ts +157 -0
- package/src/components/text-reveal.stories.tsx +363 -0
- package/src/components/text-strikethrough.stories.tsx +296 -0
- package/src/components/thinking-heading.stories.tsx +900 -0
- package/src/components/truncated-text.stories.tsx +37 -0
- package/src/components/typewriter-machine.test.ts +177 -0
- package/src/components/typewriter.stories.tsx +51 -0
- package/src/context/dialog-machine.test.ts +295 -0
- package/src/context/i18n.test.ts +89 -0
- package/src/context/marked-base.test.ts +75 -0
- package/src/context/marked-parser.test.ts +167 -0
- package/src/context/marked-regression.test.ts +12 -0
- package/src/data-display/accordion/accordion.stories.tsx +76 -0
- package/src/data-display/avatar/avatar.stories.tsx +48 -0
- package/src/data-display/badge/badge.stories.tsx +75 -0
- package/src/data-display/diff-changes/diff-changes.stories.tsx +63 -0
- package/src/data-display/keybind/keybind.stories.tsx +77 -0
- package/src/data-display/line-comment/line-comment.stories.tsx +288 -0
- package/src/data-display/line-comment/machine.test.ts +385 -0
- package/src/data-display/project-avatar/animation-machine.test.ts +134 -0
- package/src/data-display/project-avatar/generated.test.ts +50 -0
- package/src/data-display/project-avatar/procedural.test.ts +16 -0
- package/src/data-display/project-avatar/project-avatar.stories.tsx +102 -0
- package/src/data-display/project-avatar/project-avatar.test.ts +21 -0
- package/src/data-display/project-avatar/project-icon-styles.stories.tsx +772 -0
- package/src/feedback/activity-ring/activity-ring.stories.tsx +20 -0
- package/src/feedback/loader/loader.stories.tsx +44 -0
- package/src/feedback/progress-circle/progress-circle.stories.tsx +56 -0
- package/src/feedback/toast/description.test.ts +20 -0
- package/src/feedback/toast/machine.test.ts +106 -0
- package/src/feedback/toast/toast.stories.tsx +186 -0
- package/src/forms/checkbox/checkbox.stories.tsx +104 -0
- package/src/forms/field/field.stories.tsx +129 -0
- package/src/forms/inline-input/inline-input.stories.tsx +37 -0
- package/src/forms/radio/radio.stories.tsx +145 -0
- package/src/forms/redirect-paste/redirect-paste.stories.tsx +101 -0
- package/src/forms/search-list/navigation.test.ts +40 -0
- package/src/forms/search-list/search-list.stories.tsx +108 -0
- package/src/forms/select/machine.test.ts +105 -0
- package/src/forms/select/select.stories.tsx +208 -0
- package/src/forms/switch/switch.stories.tsx +97 -0
- package/src/forms/text-input/text-input.stories.tsx +220 -0
- package/src/forms/textarea/textarea.stories.tsx +137 -0
- package/src/forms/typeahead/machine.test.ts +143 -0
- package/src/forms/typeahead/typeahead.stories.tsx +113 -0
- package/src/hooks/auto-scroll-machine.test.ts +212 -0
- package/src/hooks/list-machine.test.ts +263 -0
- package/src/hooks/spin-phase.test.ts +32 -0
- package/src/hooks/use-filtered-list.test.ts +41 -0
- package/src/icons/icon/icon.stories.tsx +131 -0
- package/src/layout/divider/divider.stories.tsx +38 -0
- package/src/motion/README.md +113 -0
- package/src/motion/core.test.ts +328 -0
- package/src/motion-tuning-features.test.ts +129 -0
- package/src/motion-tuning.test.ts +68 -0
- package/src/navigation/menu/machine.test.ts +85 -0
- package/src/navigation/menu/menu.stories.tsx +328 -0
- package/src/navigation/menu/submenu-machine.test.ts +71 -0
- package/src/navigation/segmented-control/segmented-control.stories.tsx +115 -0
- package/src/navigation/segmented-control/segmented-control.test.ts +123 -0
- package/src/navigation/tab-state-indicator.stories.tsx +24 -0
- package/src/navigation/tabs/tabs.stories.tsx +193 -0
- package/src/overlays/dialog/dialog.stories.tsx +365 -0
- package/src/overlays/mcp-sign-in/machine.test.ts +215 -0
- package/src/overlays/tooltip/machine.test.ts +214 -0
- package/src/overlays/tooltip/tooltip.stories.tsx +155 -0
- package/src/styles/blue-tokens.stories.tsx +215 -0
- package/src/theme/desktop-theme.schema.json +168 -0
- package/src/theme/machine.test.ts +498 -0
- package/src/theme/v2/resolve.test.ts +120 -0
- package/src/typography/slash/slash.stories.tsx +26 -0
- package/src/typography/text-shimmer/machine.test.ts +117 -0
- package/src/typography/text-shimmer/text-shimmer.stories.tsx +64 -0
- package/src/typography/wordmark/wordmark.stories.tsx +19 -0
- package/test/solid-jsx.ts +20 -0
- package/tsconfig.build.json +14 -0
- package/tsconfig.json +23 -0
- package/vite.config.ts +61 -0
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import { onCleanup, onMount } from "solid-js"
|
|
3
|
+
import * as mod from "./image-preview"
|
|
4
|
+
import { Button } from "@zyro-sec/ui/button"
|
|
5
|
+
import { useDialog } from "../context/dialog"
|
|
6
|
+
|
|
7
|
+
const docs = `### Overview
|
|
8
|
+
Image preview content intended to render inside the dialog stack.
|
|
9
|
+
|
|
10
|
+
Use for full-size image inspection; keep images optimized.
|
|
11
|
+
|
|
12
|
+
### API
|
|
13
|
+
- Required: \`src\`.
|
|
14
|
+
- Optional: \`alt\` text, \`origin\`, the image on the page that the preview grows out of and shrinks back into, and
|
|
15
|
+
\`video\`, a video on the page that the preview borrows and shows instead, still playing.
|
|
16
|
+
|
|
17
|
+
### Variants and states
|
|
18
|
+
- Single layout: the image fills the window without a panel, with zoom controls and a close action in one strip under it.
|
|
19
|
+
|
|
20
|
+
### Behavior
|
|
21
|
+
- Intended to be used via \`useDialog().show\`.
|
|
22
|
+
- With an \`origin\`, the image flies from its place on the page to the preview while the page dims, and back on close.
|
|
23
|
+
- Pinch or Cmd/Ctrl + wheel zooms at the cursor; wheel and drag pan while zoomed.
|
|
24
|
+
- Double-click toggles between fit and 100%; Cmd/Ctrl + =, - and 0 zoom in, out and to fit, as do 0 and a first Escape.
|
|
25
|
+
- A video zooms inside its own frame, under its controls; Space plays and pauses it.
|
|
26
|
+
- A click on the backdrop around the image closes it, unless it ends a pan.
|
|
27
|
+
|
|
28
|
+
### Accessibility
|
|
29
|
+
- Uses localized aria-label for close button.
|
|
30
|
+
|
|
31
|
+
### Theming/tokens
|
|
32
|
+
- Uses \`data-component="image-preview"\` and slot attributes.
|
|
33
|
+
|
|
34
|
+
`
|
|
35
|
+
|
|
36
|
+
export default {
|
|
37
|
+
title: "UI/ImagePreview",
|
|
38
|
+
id: "components-image-preview",
|
|
39
|
+
component: mod.ImagePreview,
|
|
40
|
+
tags: ["autodocs"],
|
|
41
|
+
parameters: {
|
|
42
|
+
// Opening the actual preview installs its window-wide pinch and zoom handlers.
|
|
43
|
+
gridPreview: () => <img src={poster(0.2)} alt="Garden open day poster" />,
|
|
44
|
+
docs: {
|
|
45
|
+
description: {
|
|
46
|
+
component: docs,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const Basic = {
|
|
53
|
+
render: () => {
|
|
54
|
+
const dialog = useDialog()
|
|
55
|
+
const src = poster()
|
|
56
|
+
|
|
57
|
+
const open = () => dialog.show(() => <mod.ImagePreview src={src} alt="Preview" />)
|
|
58
|
+
|
|
59
|
+
onMount(open)
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<Button variant="neutral" onClick={open}>
|
|
63
|
+
Open image preview
|
|
64
|
+
</Button>
|
|
65
|
+
)
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// A large, offline, screenshot-style bitmap with small text that is only legible once zoomed in.
|
|
70
|
+
export const Screenshot = {
|
|
71
|
+
render: () => {
|
|
72
|
+
const dialog = useDialog()
|
|
73
|
+
const src = screenshot()
|
|
74
|
+
|
|
75
|
+
const open = () => dialog.show(() => <mod.ImagePreview src={src} alt="Editor screenshot" />)
|
|
76
|
+
|
|
77
|
+
onMount(open)
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<Button variant="neutral" onClick={open}>
|
|
81
|
+
Open screenshot preview
|
|
82
|
+
</Button>
|
|
83
|
+
)
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Images in a message open from where they sit and close back into place: a full-width image and a cropped thumbnail.
|
|
88
|
+
export const FromMessage = {
|
|
89
|
+
render: () => {
|
|
90
|
+
const dialog = useDialog()
|
|
91
|
+
const src = poster()
|
|
92
|
+
|
|
93
|
+
const open = (event: MouseEvent & { currentTarget: HTMLImageElement }) => {
|
|
94
|
+
const origin = event.currentTarget
|
|
95
|
+
dialog.show(() => <mod.ImagePreview src={src} alt={origin.alt} origin={origin} />)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<div style={{ width: "640px", margin: "48px auto", display: "flex", "flex-direction": "column", gap: "16px" }}>
|
|
100
|
+
<div style={{ display: "flex", "justify-content": "flex-end" }}>
|
|
101
|
+
<img
|
|
102
|
+
data-story-image="thumbnail"
|
|
103
|
+
src={src}
|
|
104
|
+
alt="Poster thumbnail"
|
|
105
|
+
onClick={open}
|
|
106
|
+
style={{ width: "58px", height: "46px", "object-fit": "cover", "border-radius": "6px", cursor: "pointer" }}
|
|
107
|
+
/>
|
|
108
|
+
</div>
|
|
109
|
+
<p>Here is the poster for the garden open day, with the new colours.</p>
|
|
110
|
+
<img
|
|
111
|
+
data-story-image="inline"
|
|
112
|
+
src={src}
|
|
113
|
+
alt="Garden open day poster"
|
|
114
|
+
onClick={open}
|
|
115
|
+
style={{ width: "100%", "border-radius": "8px", cursor: "zoom-in" }}
|
|
116
|
+
/>
|
|
117
|
+
<p>The hills use the brand greens, and the date sits on the sun.</p>
|
|
118
|
+
</div>
|
|
119
|
+
)
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// A video in a pane opens into the preview still playing, and goes back when it closes.
|
|
124
|
+
export const FromVideo = {
|
|
125
|
+
render: () => {
|
|
126
|
+
const dialog = useDialog()
|
|
127
|
+
let video
|
|
128
|
+
|
|
129
|
+
onMount(() => {
|
|
130
|
+
const canvas = document.createElement("canvas")
|
|
131
|
+
canvas.width = 640
|
|
132
|
+
canvas.height = 400
|
|
133
|
+
const ctx = canvas.getContext("2d")
|
|
134
|
+
|
|
135
|
+
let frame: number
|
|
136
|
+
|
|
137
|
+
const draw = (time) => {
|
|
138
|
+
ctx.fillStyle = "#1f2a44"
|
|
139
|
+
ctx.fillRect(0, 0, 640, 400)
|
|
140
|
+
ctx.fillStyle = "#ffd27a"
|
|
141
|
+
ctx.beginPath()
|
|
142
|
+
ctx.arc(320 + 200 * Math.sin(time / 700), 200, 60, 0, Math.PI * 2)
|
|
143
|
+
ctx.fill()
|
|
144
|
+
frame = requestAnimationFrame(draw)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
frame = requestAnimationFrame(draw)
|
|
148
|
+
const stream = canvas.captureStream(30)
|
|
149
|
+
|
|
150
|
+
video.srcObject = stream
|
|
151
|
+
onCleanup(() => {
|
|
152
|
+
cancelAnimationFrame(frame)
|
|
153
|
+
stream.getTracks().forEach((track) => track.stop())
|
|
154
|
+
})
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
const open = () => dialog.show(() => <mod.ImagePreview src="" alt="Orchard board tour" video={video} />)
|
|
158
|
+
|
|
159
|
+
return (
|
|
160
|
+
<div
|
|
161
|
+
data-story-pane
|
|
162
|
+
style={{ width: "640px", margin: "48px auto", display: "flex", "justify-content": "center" }}
|
|
163
|
+
>
|
|
164
|
+
<video
|
|
165
|
+
ref={video}
|
|
166
|
+
data-story-video
|
|
167
|
+
muted
|
|
168
|
+
autoplay
|
|
169
|
+
playsinline
|
|
170
|
+
controls
|
|
171
|
+
onDblClick={open}
|
|
172
|
+
style={{ width: "100%", "border-radius": "8px", background: "black" }}
|
|
173
|
+
/>
|
|
174
|
+
</div>
|
|
175
|
+
)
|
|
176
|
+
},
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// A bright, offline poster, so the image reads clearly against the dimmed page while it moves.
|
|
180
|
+
function poster(scale = 1) {
|
|
181
|
+
const canvas = document.createElement("canvas")
|
|
182
|
+
canvas.width = 1600 * scale
|
|
183
|
+
canvas.height = 1000 * scale
|
|
184
|
+
const ctx = canvas.getContext("2d")
|
|
185
|
+
ctx.scale(scale, scale)
|
|
186
|
+
const sky = ctx.createLinearGradient(0, 0, 0, 1000)
|
|
187
|
+
sky.addColorStop(0, "#ff8a5b")
|
|
188
|
+
sky.addColorStop(1, "#ffd27a")
|
|
189
|
+
ctx.fillStyle = sky
|
|
190
|
+
ctx.fillRect(0, 0, 1600, 1000)
|
|
191
|
+
ctx.fillStyle = "#fff4d6"
|
|
192
|
+
ctx.beginPath()
|
|
193
|
+
ctx.arc(1120, 360, 170, 0, Math.PI * 2)
|
|
194
|
+
ctx.fill()
|
|
195
|
+
;[
|
|
196
|
+
["#3f9d5a", 700, 0.0],
|
|
197
|
+
["#2f7d48", 790, 1.7],
|
|
198
|
+
["#1f5c36", 880, 3.1],
|
|
199
|
+
].forEach(([color, base, phase]) => {
|
|
200
|
+
ctx.fillStyle = color
|
|
201
|
+
ctx.beginPath()
|
|
202
|
+
ctx.moveTo(0, 1000)
|
|
203
|
+
|
|
204
|
+
for (let x = 0; x <= 1600; x += 20) ctx.lineTo(x, base - 70 * Math.sin(x / 260 + phase))
|
|
205
|
+
ctx.lineTo(1600, 1000)
|
|
206
|
+
ctx.fill()
|
|
207
|
+
})
|
|
208
|
+
ctx.fillStyle = "#1f2a44"
|
|
209
|
+
ctx.font = "800 120px -apple-system, system-ui, sans-serif"
|
|
210
|
+
ctx.fillText("Garden open day", 110, 230)
|
|
211
|
+
ctx.font = "600 52px -apple-system, system-ui, sans-serif"
|
|
212
|
+
ctx.fillText("Lumen Garden · 12 April", 116, 310)
|
|
213
|
+
ctx.fillStyle = "#c2410c"
|
|
214
|
+
ctx.font = "800 64px -apple-system, system-ui, sans-serif"
|
|
215
|
+
ctx.textAlign = "center"
|
|
216
|
+
ctx.fillText("12.4", 1120, 382)
|
|
217
|
+
|
|
218
|
+
return canvas.toDataURL("image/png")
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function screenshot() {
|
|
222
|
+
const canvas = document.createElement("canvas")
|
|
223
|
+
canvas.width = 2880
|
|
224
|
+
canvas.height = 1800
|
|
225
|
+
const ctx = canvas.getContext("2d")
|
|
226
|
+
ctx.scale(2, 2)
|
|
227
|
+
|
|
228
|
+
ctx.fillStyle = "#1d1e22"
|
|
229
|
+
ctx.fillRect(0, 0, 1440, 900)
|
|
230
|
+
ctx.fillStyle = "#2a2b31"
|
|
231
|
+
ctx.fillRect(0, 0, 1440, 38)
|
|
232
|
+
;["#ff5f57", "#febc2e", "#28c840"].forEach((color, index) => {
|
|
233
|
+
ctx.fillStyle = color
|
|
234
|
+
ctx.beginPath()
|
|
235
|
+
ctx.arc(20 + index * 20, 19, 6, 0, Math.PI * 2)
|
|
236
|
+
ctx.fill()
|
|
237
|
+
})
|
|
238
|
+
ctx.fillStyle = "#a9abb3"
|
|
239
|
+
ctx.font = "13px -apple-system, system-ui, sans-serif"
|
|
240
|
+
ctx.textAlign = "center"
|
|
241
|
+
ctx.fillText("orchard.ts — lumen-garden", 720, 24)
|
|
242
|
+
ctx.textAlign = "left"
|
|
243
|
+
|
|
244
|
+
ctx.fillStyle = "#23242a"
|
|
245
|
+
ctx.fillRect(0, 38, 240, 836)
|
|
246
|
+
ctx.fillStyle = "#7d808a"
|
|
247
|
+
ctx.font = "600 10px -apple-system, system-ui, sans-serif"
|
|
248
|
+
ctx.fillText("EXPLORER", 16, 64)
|
|
249
|
+
;[
|
|
250
|
+
"src",
|
|
251
|
+
" orchard.ts",
|
|
252
|
+
" greenhouse.ts",
|
|
253
|
+
" sprinkler.ts",
|
|
254
|
+
" seeds",
|
|
255
|
+
" tomato.json",
|
|
256
|
+
" basil.json",
|
|
257
|
+
" marigold.json",
|
|
258
|
+
"test",
|
|
259
|
+
" orchard.test.ts",
|
|
260
|
+
"README.md",
|
|
261
|
+
"package.json",
|
|
262
|
+
].forEach((name, index) => {
|
|
263
|
+
if (index === 1) {
|
|
264
|
+
ctx.fillStyle = "#33353d"
|
|
265
|
+
ctx.fillRect(0, 76 + index * 24, 240, 24)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
ctx.fillStyle = index === 1 ? "#e6e7eb" : "#a9abb3"
|
|
269
|
+
ctx.font = "12px -apple-system, system-ui, sans-serif"
|
|
270
|
+
ctx.fillText(name.trimStart(), 20 + (name.length - name.trimStart().length) * 6, 92 + index * 24)
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
const code = [
|
|
274
|
+
[
|
|
275
|
+
["#c792ea", "import "],
|
|
276
|
+
["#e6e7eb", "{ Sprinkler } "],
|
|
277
|
+
["#c792ea", "from "],
|
|
278
|
+
["#c3e88d", '"./sprinkler"'],
|
|
279
|
+
],
|
|
280
|
+
[
|
|
281
|
+
["#c792ea", "import "],
|
|
282
|
+
["#e6e7eb", "{ Greenhouse } "],
|
|
283
|
+
["#c792ea", "from "],
|
|
284
|
+
["#c3e88d", '"./greenhouse"'],
|
|
285
|
+
],
|
|
286
|
+
[],
|
|
287
|
+
[["#5c6370", "// Waters each bed once the soil sensor drops below its threshold."]],
|
|
288
|
+
[
|
|
289
|
+
["#c792ea", "export function "],
|
|
290
|
+
["#82aaff", "tend"],
|
|
291
|
+
["#e6e7eb", "(garden: Greenhouse, now: Date) {"],
|
|
292
|
+
],
|
|
293
|
+
[
|
|
294
|
+
["#c792ea", " const "],
|
|
295
|
+
["#e6e7eb", "dry = garden.beds."],
|
|
296
|
+
["#82aaff", "filter"],
|
|
297
|
+
["#e6e7eb", "((bed) => bed.moisture < bed.threshold)"],
|
|
298
|
+
],
|
|
299
|
+
[
|
|
300
|
+
["#c792ea", " if "],
|
|
301
|
+
["#e6e7eb", "(dry.length === "],
|
|
302
|
+
["#f78c6c", "0"],
|
|
303
|
+
["#e6e7eb", ") "],
|
|
304
|
+
["#c792ea", "return"],
|
|
305
|
+
],
|
|
306
|
+
[],
|
|
307
|
+
[
|
|
308
|
+
["#c792ea", " for "],
|
|
309
|
+
["#e6e7eb", "("],
|
|
310
|
+
["#c792ea", "const "],
|
|
311
|
+
["#e6e7eb", "bed "],
|
|
312
|
+
["#c792ea", "of "],
|
|
313
|
+
["#e6e7eb", "dry) {"],
|
|
314
|
+
],
|
|
315
|
+
[
|
|
316
|
+
["#c792ea", " const "],
|
|
317
|
+
["#e6e7eb", "minutes = Math."],
|
|
318
|
+
["#82aaff", "ceil"],
|
|
319
|
+
["#e6e7eb", "((bed.threshold - bed.moisture) * "],
|
|
320
|
+
["#f78c6c", "4"],
|
|
321
|
+
["#e6e7eb", ")"],
|
|
322
|
+
],
|
|
323
|
+
[
|
|
324
|
+
["#e6e7eb", " Sprinkler."],
|
|
325
|
+
["#82aaff", "open"],
|
|
326
|
+
["#e6e7eb", "(bed.valve, { minutes, startedAt: now })"],
|
|
327
|
+
],
|
|
328
|
+
[
|
|
329
|
+
["#e6e7eb", " garden.log."],
|
|
330
|
+
["#82aaff", "push"],
|
|
331
|
+
["#e6e7eb", "(`watered ${bed.name} for ${minutes}m`)"],
|
|
332
|
+
],
|
|
333
|
+
[["#e6e7eb", " }"]],
|
|
334
|
+
[],
|
|
335
|
+
[
|
|
336
|
+
["#c792ea", " return "],
|
|
337
|
+
["#e6e7eb", "dry."],
|
|
338
|
+
["#82aaff", "map"],
|
|
339
|
+
["#e6e7eb", "((bed) => bed.name)"],
|
|
340
|
+
],
|
|
341
|
+
[["#e6e7eb", "}"]],
|
|
342
|
+
]
|
|
343
|
+
|
|
344
|
+
ctx.font = "13px Menlo, ui-monospace, monospace"
|
|
345
|
+
code.forEach((line, index) => {
|
|
346
|
+
const y = 76 + index * 22
|
|
347
|
+
ctx.fillStyle = "#4b4e57"
|
|
348
|
+
ctx.textAlign = "right"
|
|
349
|
+
ctx.fillText(String(index + 1), 296, y)
|
|
350
|
+
ctx.textAlign = "left"
|
|
351
|
+
line.reduce((x, [color, text]) => {
|
|
352
|
+
ctx.fillStyle = color
|
|
353
|
+
ctx.fillText(text, x, y)
|
|
354
|
+
|
|
355
|
+
return x + ctx.measureText(text).width
|
|
356
|
+
}, 320)
|
|
357
|
+
})
|
|
358
|
+
|
|
359
|
+
ctx.fillStyle = "#26272d"
|
|
360
|
+
ctx.fillRect(1080, 38, 360, 836)
|
|
361
|
+
ctx.fillStyle = "#e6e7eb"
|
|
362
|
+
ctx.font = "600 12px -apple-system, system-ui, sans-serif"
|
|
363
|
+
ctx.fillText("Soil moisture", 1100, 70)
|
|
364
|
+
;[0.62, 0.31, 0.48, 0.27, 0.71, 0.55].forEach((value, index) => {
|
|
365
|
+
const y = 92 + index * 34
|
|
366
|
+
ctx.fillStyle = "#a9abb3"
|
|
367
|
+
ctx.font = "11px -apple-system, system-ui, sans-serif"
|
|
368
|
+
ctx.fillText(`Bed ${String.fromCharCode(65 + index)}`, 1100, y + 12)
|
|
369
|
+
ctx.fillStyle = "#33353d"
|
|
370
|
+
ctx.fillRect(1160, y + 3, 220, 10)
|
|
371
|
+
ctx.fillStyle = value < 0.35 ? "#f78c6c" : "#7fd88f"
|
|
372
|
+
ctx.fillRect(1160, y + 3, 220 * value, 10)
|
|
373
|
+
ctx.fillStyle = "#7d808a"
|
|
374
|
+
ctx.font = "8px -apple-system, system-ui, sans-serif"
|
|
375
|
+
ctx.fillText(`${Math.round(value * 100)}% · threshold 35%`, 1386 - 64, y + 26)
|
|
376
|
+
})
|
|
377
|
+
ctx.fillStyle = "#7d808a"
|
|
378
|
+
ctx.font = "7px -apple-system, system-ui, sans-serif"
|
|
379
|
+
;[
|
|
380
|
+
"Sensor readings refresh every 90 seconds.",
|
|
381
|
+
"Beds below their threshold are watered on the next tick.",
|
|
382
|
+
"Valve 4 was serviced on 12 March; calibration offset +0.02.",
|
|
383
|
+
].forEach((text, index) => ctx.fillText(text, 1100, 330 + index * 11))
|
|
384
|
+
|
|
385
|
+
ctx.fillStyle = "#3b5bdb"
|
|
386
|
+
ctx.fillRect(0, 874, 1440, 26)
|
|
387
|
+
ctx.fillStyle = "#ffffff"
|
|
388
|
+
ctx.font = "11px -apple-system, system-ui, sans-serif"
|
|
389
|
+
ctx.fillText("main · 0 problems · Ln 6, Col 42 · UTF-8 · TypeScript", 12, 891)
|
|
390
|
+
|
|
391
|
+
return canvas.toDataURL("image/png")
|
|
392
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import {
|
|
3
|
+
INITIAL_PREVIEW_ZOOM_STATE,
|
|
4
|
+
selectDragging,
|
|
5
|
+
selectPanPointer,
|
|
6
|
+
transitionPreviewZoom,
|
|
7
|
+
type PreviewZoomEffect,
|
|
8
|
+
type PreviewZoomEvent,
|
|
9
|
+
type PreviewZoomState,
|
|
10
|
+
} from "./image-zoom-machine"
|
|
11
|
+
|
|
12
|
+
function replay(events: PreviewZoomEvent[], from = INITIAL_PREVIEW_ZOOM_STATE) {
|
|
13
|
+
return events.reduce<{ state: PreviewZoomState; effects: PreviewZoomEffect[] }>(
|
|
14
|
+
(result, event) => {
|
|
15
|
+
const next = transitionPreviewZoom(result.state, event)
|
|
16
|
+
|
|
17
|
+
return { state: next.state, effects: [...result.effects, ...next.effects] }
|
|
18
|
+
},
|
|
19
|
+
{ state: from, effects: [] },
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function down(target: "viewport" | "video" | "content", options: { zoomed?: boolean; onBackdrop?: boolean } = {}) {
|
|
24
|
+
return {
|
|
25
|
+
type: "pointer.down",
|
|
26
|
+
pointerId: 1,
|
|
27
|
+
button: 0,
|
|
28
|
+
target,
|
|
29
|
+
onBackdrop: options.onBackdrop ?? false,
|
|
30
|
+
zoomed: options.zoomed ?? true,
|
|
31
|
+
} satisfies PreviewZoomEvent
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const moved = { type: "pointer.moved", pointerId: 1 } satisfies PreviewZoomEvent
|
|
35
|
+
|
|
36
|
+
const up = { type: "pointer.up", pointerId: 1 } satisfies PreviewZoomEvent
|
|
37
|
+
|
|
38
|
+
describe("image preview zoom gesture", () => {
|
|
39
|
+
// Bug: nothing ended a pan whose pointer capture was lost without a pointerup, so plain hover with the same mouse
|
|
40
|
+
// pointer kept panning the image under a grabbing cursor. The driver sends `pointer.cancel` on lostpointercapture
|
|
41
|
+
// and on a move with the primary button up.
|
|
42
|
+
test("a pan that loses its pointer ends", () => {
|
|
43
|
+
const panning = replay([down("viewport"), moved])
|
|
44
|
+
|
|
45
|
+
expect(selectDragging(panning.state)).toBe(true)
|
|
46
|
+
expect(panning.effects).toEqual([{ type: "pointer.capture", target: "viewport", pointerId: 1 }])
|
|
47
|
+
|
|
48
|
+
const ended = transitionPreviewZoom(panning.state, { type: "pointer.cancel", pointerId: 1 })
|
|
49
|
+
expect(selectDragging(ended.state)).toBe(false)
|
|
50
|
+
expect(selectPanPointer(ended.state, 1)).toBe(false)
|
|
51
|
+
expect(ended.effects).toEqual([{ type: "pointer.release", target: "viewport", pointerId: 1 }])
|
|
52
|
+
expect(transitionPreviewZoom(ended.state, moved).state).toBe(ended.state)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
// Bug: Escape or 0 during a press glided back to fit and left the pan, and its grabbing cursor, on a fitted image.
|
|
56
|
+
test("an eased zoom takes over from a pan", () => {
|
|
57
|
+
const glide = replay([down("viewport"), moved, { type: "glide.start" }])
|
|
58
|
+
|
|
59
|
+
expect(glide.state).toEqual({ status: "gliding", gesture: { type: "idle" } })
|
|
60
|
+
expect(glide.effects.slice(1)).toEqual([
|
|
61
|
+
{ type: "pointer.release", target: "viewport", pointerId: 1 },
|
|
62
|
+
{ type: "glide.loop.start" },
|
|
63
|
+
])
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test("a press does not stop a glide, the hand moving does, and a pan ends once the image is back at fit", () => {
|
|
67
|
+
const pressed = replay([{ type: "glide.start" }, down("viewport")])
|
|
68
|
+
|
|
69
|
+
expect(pressed.state.status).toBe("gliding")
|
|
70
|
+
expect(selectDragging(pressed.state)).toBe(true)
|
|
71
|
+
|
|
72
|
+
const followed = transitionPreviewZoom(pressed.state, { type: "follow", zoomed: true })
|
|
73
|
+
expect(followed.state.status).toBe("still")
|
|
74
|
+
expect(followed.effects).toEqual([{ type: "glide.loop.stop" }])
|
|
75
|
+
|
|
76
|
+
const settled = transitionPreviewZoom(pressed.state, { type: "glide.settled", zoomed: false })
|
|
77
|
+
expect(settled.state).toEqual({ status: "still", gesture: { type: "idle" } })
|
|
78
|
+
expect(settled.effects).toEqual([
|
|
79
|
+
{ type: "glide.loop.stop" },
|
|
80
|
+
{ type: "pointer.release", target: "viewport", pointerId: 1 },
|
|
81
|
+
])
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
// Bug: the "panned" flag outlived its gesture, so a later click on the video's controls was swallowed.
|
|
85
|
+
test("only the release of a moved video pan swallows its click", () => {
|
|
86
|
+
expect(replay([down("video"), moved, up]).effects.at(-1)).toEqual({ type: "video.suppress-click" })
|
|
87
|
+
expect(replay([down("video"), up]).effects).toEqual([{ type: "pointer.capture", target: "video", pointerId: 1 }])
|
|
88
|
+
expect(replay([down("video"), moved, { type: "pointer.cancel", pointerId: 1 }]).effects.at(-1)).toEqual({
|
|
89
|
+
type: "pointer.release",
|
|
90
|
+
target: "video",
|
|
91
|
+
pointerId: 1,
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test.each([
|
|
96
|
+
["an unzoomed press on the backdrop closes", [down("viewport", { zoomed: false, onBackdrop: true })], true],
|
|
97
|
+
["a press on the band around the body closes", [down("content", { onBackdrop: true })], true],
|
|
98
|
+
["a zoomed press on the backdrop that stays put closes", [down("viewport", { onBackdrop: true }), up], true],
|
|
99
|
+
[
|
|
100
|
+
"a zoomed press on the backdrop that stays put, then loses capture, closes",
|
|
101
|
+
[down("viewport", { onBackdrop: true }), up, { type: "pointer.cancel", pointerId: 1 } satisfies PreviewZoomEvent],
|
|
102
|
+
true,
|
|
103
|
+
],
|
|
104
|
+
["a zoomed press on the image, whose captured click lands on the backdrop, stays", [down("viewport"), up], false],
|
|
105
|
+
["a zoomed press on the backdrop that pans stays", [down("viewport", { onBackdrop: true }), moved, up], false],
|
|
106
|
+
["a press on a control stays", [down("content")], false],
|
|
107
|
+
])("on its click, %s", (_, events, closes) => {
|
|
108
|
+
const pressed = replay(events)
|
|
109
|
+
const clicked = transitionPreviewZoom(pressed.state, { type: "backdrop.click", onBackdrop: true })
|
|
110
|
+
|
|
111
|
+
expect(clicked.effects).toEqual(closes ? [{ type: "dialog.close" }] : [])
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test("a secondary button or a second pointer leaves the pan alone; the same pointer again starts over", () => {
|
|
115
|
+
const panning = replay([down("viewport"), moved]).state
|
|
116
|
+
|
|
117
|
+
expect(transitionPreviewZoom(panning, { ...down("viewport"), pointerId: 2 }).state).toBe(panning)
|
|
118
|
+
expect(transitionPreviewZoom(panning, { type: "pointer.up", pointerId: 2 }).state).toBe(panning)
|
|
119
|
+
expect(transitionPreviewZoom(INITIAL_PREVIEW_ZOOM_STATE, { ...down("viewport"), button: 2 }).state).toBe(
|
|
120
|
+
INITIAL_PREVIEW_ZOOM_STATE,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
const again = transitionPreviewZoom(panning, down("viewport"))
|
|
124
|
+
expect(again.state).toEqual({
|
|
125
|
+
status: "still",
|
|
126
|
+
gesture: { type: "panning", pointerId: 1, target: "viewport", onBackdrop: false, moved: false },
|
|
127
|
+
})
|
|
128
|
+
expect(again.effects).toEqual([{ type: "pointer.capture", target: "viewport", pointerId: 1 }])
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test("moves that change nothing answer with the same transition, so a pan allocates nothing", () => {
|
|
132
|
+
const panning = replay([down("viewport"), moved]).state
|
|
133
|
+
const first = transitionPreviewZoom(panning, moved)
|
|
134
|
+
|
|
135
|
+
expect(first.state).toBe(panning)
|
|
136
|
+
expect(transitionPreviewZoom(panning, moved)).toBe(first)
|
|
137
|
+
expect(transitionPreviewZoom(panning, { type: "follow", zoomed: true })).toBe(first)
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
test("disposal releases the pan and stops the glide, then ignores everything", () => {
|
|
141
|
+
const busy: PreviewZoomState = {
|
|
142
|
+
status: "gliding",
|
|
143
|
+
gesture: { type: "panning", pointerId: 1, target: "video", onBackdrop: false, moved: false },
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const disposed = transitionPreviewZoom(busy, { type: "disposed" })
|
|
147
|
+
|
|
148
|
+
expect(disposed.effects).toEqual([
|
|
149
|
+
{ type: "pointer.release", target: "video", pointerId: 1 },
|
|
150
|
+
{ type: "glide.loop.stop" },
|
|
151
|
+
])
|
|
152
|
+
expect(transitionPreviewZoom(disposed.state, { type: "glide.start" }).effects).toEqual([])
|
|
153
|
+
})
|
|
154
|
+
})
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import { clampView, detailScale, FIT_VIEW, maxScale, panBy, viewBox, wheelZoomFactor, zoomAt } from "./image-zoom"
|
|
3
|
+
|
|
4
|
+
// A 1000×500 image fitted into a 1000×600 viewport at half its natural size.
|
|
5
|
+
const bounds = { width: 1000, height: 500, viewportWidth: 1000, viewportHeight: 600, maxScale: maxScale(0.5) }
|
|
6
|
+
|
|
7
|
+
describe("zoomAt", () => {
|
|
8
|
+
test("keeps the image point under the cursor in place", () => {
|
|
9
|
+
const anchor = { x: 300, y: -100 }
|
|
10
|
+
const view = zoomAt(FIT_VIEW, 2, anchor, bounds)
|
|
11
|
+
// The fitted point under the anchor sits at (anchor - offset) / scale in image space.
|
|
12
|
+
const before = { x: (anchor.x - FIT_VIEW.x) / FIT_VIEW.scale, y: (anchor.y - FIT_VIEW.y) / FIT_VIEW.scale }
|
|
13
|
+
|
|
14
|
+
expect(view).toEqual({ scale: 2, x: -300, y: 100 })
|
|
15
|
+
expect({ x: before.x * view.scale + view.x, y: before.y * view.scale + view.y }).toEqual(anchor)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test("zooms about the center without moving it", () => {
|
|
19
|
+
expect(zoomAt({ scale: 2, x: 120, y: -40 }, 4, { x: 0, y: 0 }, bounds)).toEqual({ scale: 4, x: 240, y: -80 })
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test("never shrinks below fit or grows past the limit", () => {
|
|
23
|
+
expect(zoomAt({ scale: 3, x: 400, y: 200 }, 0.2, { x: 100, y: 100 }, bounds)).toEqual(FIT_VIEW)
|
|
24
|
+
expect(zoomAt(FIT_VIEW, 100, { x: 0, y: 0 }, bounds).scale).toBe(16)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test("pulls an anchor near the edge back so no empty space shows", () => {
|
|
28
|
+
expect(zoomAt(FIT_VIEW, 2, { x: 500, y: 0 }, bounds)).toEqual({ scale: 2, x: -500, y: 0 })
|
|
29
|
+
expect(zoomAt(FIT_VIEW, 1.1, { x: 500, y: 300 }, bounds)).toEqual({ scale: 1.1, x: -50, y: 0 })
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
describe("panBy", () => {
|
|
34
|
+
test("moves freely inside the overflow", () => {
|
|
35
|
+
expect(panBy({ scale: 2, x: 0, y: 0 }, { x: 120, y: -80 }, bounds)).toEqual({ scale: 2, x: 120, y: -80 })
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test("stops at each edge of the zoomed image", () => {
|
|
39
|
+
// At 2x the image is 2000×1000, so it can travel 500px horizontally and 200px vertically each way.
|
|
40
|
+
expect(panBy({ scale: 2, x: 450, y: 150 }, { x: 900, y: 900 }, bounds)).toEqual({ scale: 2, x: 500, y: 200 })
|
|
41
|
+
expect(panBy({ scale: 2, x: 0, y: 0 }, { x: -900, y: -900 }, bounds)).toEqual({ scale: 2, x: -500, y: -200 })
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test("keeps an axis that still fits centered", () => {
|
|
45
|
+
expect(panBy(FIT_VIEW, { x: 40, y: 40 }, bounds)).toEqual(FIT_VIEW)
|
|
46
|
+
expect(panBy({ scale: 1.1, x: 0, y: 0 }, { x: 0, y: 40 }, bounds)).toEqual({ scale: 1.1, x: 0, y: 0 })
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
describe("clampView", () => {
|
|
51
|
+
test("re-centers a pan that a smaller zoom or larger viewport no longer allows", () => {
|
|
52
|
+
expect(clampView({ scale: 2, x: 500, y: 200 }, { ...bounds, viewportWidth: 1600, viewportHeight: 1200 })).toEqual({
|
|
53
|
+
scale: 2,
|
|
54
|
+
x: 200,
|
|
55
|
+
y: 0,
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
describe("detailScale", () => {
|
|
61
|
+
test("opens a downscaled image at 100%", () => {
|
|
62
|
+
expect(detailScale(0.25)).toBe(4)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test("doubles an image already shown near its natural size", () => {
|
|
66
|
+
expect(detailScale(1)).toBe(2)
|
|
67
|
+
expect(detailScale(0.8)).toBe(2)
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
describe("wheelZoomFactor", () => {
|
|
72
|
+
test("zooms in for upward deltas and out for downward ones by the same ratio", () => {
|
|
73
|
+
expect(wheelZoomFactor(-10)).toBeGreaterThan(1)
|
|
74
|
+
expect(wheelZoomFactor(-10) * wheelZoomFactor(10)).toBeCloseTo(1)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test("bounds a coarse mouse-wheel notch", () => {
|
|
78
|
+
expect(wheelZoomFactor(-400)).toBe(wheelZoomFactor(-100))
|
|
79
|
+
expect(wheelZoomFactor(-400)).toBeLessThan(1.5)
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
describe("viewBox", () => {
|
|
84
|
+
// An 800×450 video fitted at half its natural 1600×900.
|
|
85
|
+
const size = { width: 800, height: 450 }
|
|
86
|
+
const natural = { width: 1600, height: 900 }
|
|
87
|
+
|
|
88
|
+
test("shows the whole frame at fit", () => {
|
|
89
|
+
expect(viewBox(1, 0, 0, size, natural)).toBe("none")
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test("shows the middle half at 2x, and more of the left once panned right", () => {
|
|
93
|
+
expect(viewBox(2, 0, 0, size, natural)).toBe("inset(225px 400px 225px 400px)")
|
|
94
|
+
expect(viewBox(2, 200, 0, size, natural)).toBe("inset(225px 600px 225px 200px)")
|
|
95
|
+
})
|
|
96
|
+
})
|