@sixb/app 0.2.3 → 0.2.4
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/dist/agent/activity-label.d.ts.map +1 -1
- package/dist/agent/agent-chat.d.ts.map +1 -1
- package/dist/agent/background/background-tool-row.d.ts.map +1 -1
- package/dist/agent/background/interpret.d.ts +1 -1
- package/dist/agent/background/interpret.d.ts.map +1 -1
- package/dist/agent/components/conversation-panel.d.ts +4 -1
- package/dist/agent/components/conversation-panel.d.ts.map +1 -1
- package/dist/agent/components/message-parts.d.ts.map +1 -1
- package/dist/agent/components/transcript.d.ts +5 -0
- package/dist/agent/components/transcript.d.ts.map +1 -1
- package/dist/agent/delegation/agent-exchange.d.ts +16 -0
- package/dist/agent/delegation/agent-exchange.d.ts.map +1 -0
- package/dist/agent/delegation/child-card.d.ts +3 -3
- package/dist/agent/delegation/child-card.d.ts.map +1 -1
- package/dist/agent/delegation/child-parts.d.ts +1 -10
- package/dist/agent/delegation/child-parts.d.ts.map +1 -1
- package/dist/agent/delegation/child-work.d.ts.map +1 -1
- package/dist/agent/delegation/interpret.d.ts +4 -10
- package/dist/agent/delegation/interpret.d.ts.map +1 -1
- package/dist/agent/file-edits/file-edit-tool-view.d.ts +10 -0
- package/dist/agent/file-edits/file-edit-tool-view.d.ts.map +1 -0
- package/dist/agent/file-edits/interpret.d.ts +25 -0
- package/dist/agent/file-edits/interpret.d.ts.map +1 -0
- package/dist/agent/hooks/use-agent-conversation.d.ts +6 -0
- package/dist/agent/hooks/use-agent-conversation.d.ts.map +1 -1
- package/dist/agent/i18n/en.d.ts +46 -5
- package/dist/agent/i18n/en.d.ts.map +1 -1
- package/dist/agent/i18n/fr.d.ts.map +1 -1
- package/dist/agent/i18n/index.d.ts +76 -8
- package/dist/agent/i18n/index.d.ts.map +1 -1
- package/dist/agent/message-agent/interpret.d.ts +2 -2
- package/dist/agent/message-agent/interpret.d.ts.map +1 -1
- package/dist/agent/publish-artifact/interpret.d.ts +1 -1
- package/dist/agent/publish-artifact/interpret.d.ts.map +1 -1
- package/dist/agent/questions/interpret.d.ts +31 -0
- package/dist/agent/questions/interpret.d.ts.map +1 -0
- package/dist/agent/questions/question-card.d.ts +16 -0
- package/dist/agent/questions/question-card.d.ts.map +1 -0
- package/dist/agent/questions/question-composer.d.ts +15 -0
- package/dist/agent/questions/question-composer.d.ts.map +1 -0
- package/dist/agent/read/interpret.d.ts +8 -1
- package/dist/agent/read/interpret.d.ts.map +1 -1
- package/dist/agent/read/read-tool-view.d.ts.map +1 -1
- package/dist/agent/tool-views/built-in.d.ts.map +1 -1
- package/dist/agent/waits.d.ts +14 -0
- package/dist/agent/waits.d.ts.map +1 -0
- package/dist/artifacts/edit-session.d.ts +11 -1
- package/dist/artifacts/edit-session.d.ts.map +1 -1
- package/dist/artifacts/frame.d.ts.map +1 -1
- package/dist/artifacts/messages.d.ts +52 -0
- package/dist/artifacts/messages.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/board-editor.d.ts +11 -4
- package/dist/artifacts/renderers/canvas/board-editor.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/board.d.ts +14 -2
- package/dist/artifacts/renderers/canvas/board.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/canvas.d.ts +4 -3
- package/dist/artifacts/renderers/canvas/canvas.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/controls.d.ts +15 -4
- package/dist/artifacts/renderers/canvas/controls.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/focus.d.ts +19 -5
- package/dist/artifacts/renderers/canvas/focus.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/gestures.d.ts +46 -0
- package/dist/artifacts/renderers/canvas/gestures.d.ts.map +1 -0
- package/dist/artifacts/renderers/canvas/page-resize.d.ts +21 -0
- package/dist/artifacts/renderers/canvas/page-resize.d.ts.map +1 -0
- package/dist/artifacts/renderers/canvas/page-size-controls.d.ts +21 -0
- package/dist/artifacts/renderers/canvas/page-size-controls.d.ts.map +1 -0
- package/dist/artifacts/renderers/canvas/page-size.d.ts +93 -0
- package/dist/artifacts/renderers/canvas/page-size.d.ts.map +1 -0
- package/dist/artifacts/renderers/canvas/page-zoom.d.ts +64 -0
- package/dist/artifacts/renderers/canvas/page-zoom.d.ts.map +1 -0
- package/dist/artifacts/renderers/canvas/shown-size.d.ts +26 -0
- package/dist/artifacts/renderers/canvas/shown-size.d.ts.map +1 -0
- package/dist/{chunk-kes4ygmz.js → chunk-970s5nkn.js} +1 -1
- package/dist/{chunk-001c4gnk.js → chunk-kymx9y5e.js} +3117 -2363
- package/dist/{chunk-001c4gnk.js.map → chunk-kymx9y5e.js.map} +36 -29
- package/dist/{chunk-fwb9mxwm.js → chunk-pxd07kvx.js} +1 -1
- package/dist/{chunk-5crvhjp2.js → chunk-yrcm1pbg.js} +1504 -233
- package/dist/chunk-yrcm1pbg.js.map +34 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +3 -3
- package/dist/pages.js +2 -2
- package/package.json +2 -2
- package/src/agent/activity-label.ts +6 -1
- package/src/agent/agent-chat.tsx +1 -0
- package/src/agent/background/background-tool-row.tsx +24 -10
- package/src/agent/background/interpret.ts +60 -51
- package/src/agent/components/conversation-panel.tsx +27 -8
- package/src/agent/components/message-parts.tsx +23 -5
- package/src/agent/components/transcript.tsx +45 -27
- package/src/agent/delegation/agent-exchange.tsx +53 -0
- package/src/agent/delegation/child-card.tsx +6 -40
- package/src/agent/delegation/child-parts.tsx +1 -84
- package/src/agent/delegation/child-work.tsx +19 -5
- package/src/agent/delegation/interpret.ts +33 -54
- package/src/agent/delegation/model.ts +1 -1
- package/src/agent/file-edits/file-edit-tool-view.tsx +126 -0
- package/src/agent/file-edits/interpret.ts +120 -0
- package/src/agent/hooks/use-agent-conversation.ts +20 -0
- package/src/agent/i18n/en.ts +47 -5
- package/src/agent/i18n/fr.ts +39 -4
- package/src/agent/message-agent/interpret.ts +4 -3
- package/src/agent/publish-artifact/interpret.ts +14 -5
- package/src/agent/publish-artifact/publish-artifact-tool-view.tsx +2 -2
- package/src/agent/questions/interpret.ts +90 -0
- package/src/agent/questions/question-card.tsx +57 -0
- package/src/agent/questions/question-composer.tsx +257 -0
- package/src/agent/read/interpret.ts +34 -1
- package/src/agent/read/read-tool-view.tsx +20 -4
- package/src/agent/tool-views/built-in.tsx +19 -8
- package/src/agent/waits.ts +50 -0
- package/src/artifacts/artifacts.css +36 -0
- package/src/artifacts/edit-session.tsx +15 -1
- package/src/artifacts/frame.tsx +3 -2
- package/src/artifacts/messages.ts +54 -0
- package/src/artifacts/renderers/canvas/board-editor.tsx +37 -15
- package/src/artifacts/renderers/canvas/board.tsx +70 -8
- package/src/artifacts/renderers/canvas/canvas.tsx +66 -15
- package/src/artifacts/renderers/canvas/controls.tsx +64 -30
- package/src/artifacts/renderers/canvas/focus.tsx +200 -56
- package/src/artifacts/renderers/canvas/gestures.ts +265 -0
- package/src/artifacts/renderers/canvas/page-resize.tsx +542 -0
- package/src/artifacts/renderers/canvas/page-size-controls.tsx +193 -0
- package/src/artifacts/renderers/canvas/page-size.ts +218 -0
- package/src/artifacts/renderers/canvas/page-zoom.ts +280 -0
- package/src/artifacts/renderers/canvas/shown-size.ts +105 -0
- package/src/artifacts/viewer.tsx +15 -1
- package/src/index.ts +2 -0
- package/dist/chunk-5crvhjp2.js.map +0 -28
- /package/dist/{chunk-kes4ygmz.js.map → chunk-970s5nkn.js.map} +0 -0
- /package/dist/{chunk-fwb9mxwm.js.map → chunk-pxd07kvx.js.map} +0 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DropdownMenu,
|
|
3
|
+
DropdownMenuContent,
|
|
4
|
+
DropdownMenuItem,
|
|
5
|
+
DropdownMenuLabel,
|
|
6
|
+
DropdownMenuSeparator,
|
|
7
|
+
DropdownMenuTrigger,
|
|
8
|
+
} from "@sixb/ui/components"
|
|
9
|
+
import { cn } from "@sixb/ui/lib/utils"
|
|
10
|
+
import { Check, Proportions, RotateCcw } from "lucide-react"
|
|
11
|
+
import { useRef } from "react"
|
|
12
|
+
import { useArtifactMessages } from "../../messages"
|
|
13
|
+
import { type PageSize, SCREENS, type SizeName, type SizeSuggestions, sameSize } from "./page-size"
|
|
14
|
+
|
|
15
|
+
/** The sizes the menu offers: the board's, the full page, and the common screens. */
|
|
16
|
+
const MENU: readonly SizeName[] = ["board", "full", "phone", "tablet", "desktop"]
|
|
17
|
+
|
|
18
|
+
/** A size the menu offers, as it lays the page out from the size shown now. */
|
|
19
|
+
function menuSize(
|
|
20
|
+
name: SizeName,
|
|
21
|
+
shown: PageSize,
|
|
22
|
+
board: PageSize,
|
|
23
|
+
suggestions: SizeSuggestions
|
|
24
|
+
): PageSize | null {
|
|
25
|
+
if (name === "board") return board
|
|
26
|
+
if (name === "full") {
|
|
27
|
+
const full = suggestions.heights.find((each) => each.name === "full")?.h
|
|
28
|
+
return full === undefined ? null : { w: shown.w, h: full }
|
|
29
|
+
}
|
|
30
|
+
const screen = SCREENS.find((each) => each.name === name)
|
|
31
|
+
return screen && "h" in screen ? { w: screen.w, h: screen.h } : null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The page's size in the opened board's header. Its menu offers what the handles snap to, for
|
|
36
|
+
* those who would rather click, and is the way on a phone. Shown at another size than the
|
|
37
|
+
* board's, it says which, and beside it, on a wide header, or in its menu, offers the way back
|
|
38
|
+
* and, to whoever may change the canvas, Keep this size.
|
|
39
|
+
*/
|
|
40
|
+
export function SizeControls({
|
|
41
|
+
shown,
|
|
42
|
+
board,
|
|
43
|
+
suggestions,
|
|
44
|
+
kept,
|
|
45
|
+
onPick,
|
|
46
|
+
onReset,
|
|
47
|
+
onKeep,
|
|
48
|
+
}: {
|
|
49
|
+
readonly shown: PageSize
|
|
50
|
+
readonly board: PageSize
|
|
51
|
+
readonly suggestions: SizeSuggestions
|
|
52
|
+
/** A size just kept as the board's, said for a moment. */
|
|
53
|
+
readonly kept: PageSize | null
|
|
54
|
+
/** A size picked from the menu, by what it is. */
|
|
55
|
+
readonly onPick: (size: PageSize, name: SizeName) => void
|
|
56
|
+
/** Back to the board's own size. */
|
|
57
|
+
readonly onReset: () => void
|
|
58
|
+
/** Keeps the size shown as the board's; absent for whoever may not change the canvas. */
|
|
59
|
+
readonly onKeep?: (() => void) | undefined
|
|
60
|
+
}) {
|
|
61
|
+
const messages = useArtifactMessages().canvas.size
|
|
62
|
+
const resized = !sameSize(shown, board)
|
|
63
|
+
// Picked with the pointer, the menu gives the focus back to nothing: given back to its
|
|
64
|
+
// trigger, the browser would ring it as if the keyboard had.
|
|
65
|
+
const pointer = useRef(false)
|
|
66
|
+
const sizes = MENU.map((name) => ({ name, size: menuSize(name, shown, board, suggestions) }))
|
|
67
|
+
// The first that is the size shown: the board's own before a screen of the same size.
|
|
68
|
+
const current = sizes.find(({ size }) => size !== null && sameSize(size, shown))?.name
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<div className="flex shrink-0 items-center gap-1">
|
|
72
|
+
{kept ? (
|
|
73
|
+
<span
|
|
74
|
+
role="status"
|
|
75
|
+
className="flex items-center gap-1.5 px-2 text-xs font-medium text-foreground motion-safe:animate-in motion-safe:fade-in"
|
|
76
|
+
>
|
|
77
|
+
<Check className="size-3.5 text-primary" aria-hidden="true" />
|
|
78
|
+
<span className="@max-3xl/canvas:sr-only">{messages.kept(kept.w, kept.h)}</span>
|
|
79
|
+
</span>
|
|
80
|
+
) : null}
|
|
81
|
+
|
|
82
|
+
<DropdownMenu modal={false}>
|
|
83
|
+
<DropdownMenuTrigger
|
|
84
|
+
onPointerDown={() => {
|
|
85
|
+
pointer.current = true
|
|
86
|
+
}}
|
|
87
|
+
onKeyDown={() => {
|
|
88
|
+
pointer.current = false
|
|
89
|
+
}}
|
|
90
|
+
aria-label={
|
|
91
|
+
resized ? `${messages.menu}, ${messages.dimensions(shown.w, shown.h)}` : messages.menu
|
|
92
|
+
}
|
|
93
|
+
title={resized ? messages.yoursOnly : messages.menu}
|
|
94
|
+
className={cn(
|
|
95
|
+
"flex h-8 shrink-0 items-center gap-1.5 rounded-lg px-2 text-sm font-medium outline-none hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring data-[state=open]:bg-muted",
|
|
96
|
+
resized
|
|
97
|
+
? "bg-primary/10 text-primary hover:bg-primary/15 data-[state=open]:bg-primary/15"
|
|
98
|
+
: "text-muted-foreground hover:text-foreground data-[state=open]:text-foreground"
|
|
99
|
+
)}
|
|
100
|
+
>
|
|
101
|
+
<Proportions className="size-4" aria-hidden="true" />
|
|
102
|
+
{resized ? (
|
|
103
|
+
<span className="text-xs tabular-nums max-sm:hidden">
|
|
104
|
+
{messages.dimensions(shown.w, shown.h)}
|
|
105
|
+
</span>
|
|
106
|
+
) : (
|
|
107
|
+
<span className="max-md:hidden">{messages.label}</span>
|
|
108
|
+
)}
|
|
109
|
+
</DropdownMenuTrigger>
|
|
110
|
+
|
|
111
|
+
<DropdownMenuContent
|
|
112
|
+
align="end"
|
|
113
|
+
sideOffset={6}
|
|
114
|
+
className="w-[min(17rem,calc(100vw-2rem))] p-1.5"
|
|
115
|
+
onPointerDown={() => {
|
|
116
|
+
pointer.current = true
|
|
117
|
+
}}
|
|
118
|
+
onKeyDown={() => {
|
|
119
|
+
pointer.current = false
|
|
120
|
+
}}
|
|
121
|
+
onCloseAutoFocus={(event) => {
|
|
122
|
+
if (pointer.current) event.preventDefault()
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
<DropdownMenuLabel className="px-2 pt-1 pb-1.5 text-[11px] font-medium tracking-wide text-muted-foreground uppercase">
|
|
126
|
+
{messages.menu}
|
|
127
|
+
</DropdownMenuLabel>
|
|
128
|
+
|
|
129
|
+
{sizes.map(({ name, size }) => (
|
|
130
|
+
<DropdownMenuItem
|
|
131
|
+
key={name}
|
|
132
|
+
disabled={size === null}
|
|
133
|
+
onSelect={() => {
|
|
134
|
+
if (name === "board") onReset()
|
|
135
|
+
else if (size) onPick(size, name)
|
|
136
|
+
}}
|
|
137
|
+
className="gap-2 rounded-md py-1.5"
|
|
138
|
+
>
|
|
139
|
+
<Check
|
|
140
|
+
className={cn("size-3.5", name === current ? "text-foreground" : "invisible")}
|
|
141
|
+
aria-hidden="true"
|
|
142
|
+
/>
|
|
143
|
+
<span className="flex-1 text-[13px]">{messages.names[name]}</span>
|
|
144
|
+
<span className="text-xs text-muted-foreground tabular-nums">
|
|
145
|
+
{size ? messages.dimensions(size.w, size.h) : messages.measuring}
|
|
146
|
+
</span>
|
|
147
|
+
</DropdownMenuItem>
|
|
148
|
+
))}
|
|
149
|
+
|
|
150
|
+
{resized ? (
|
|
151
|
+
<>
|
|
152
|
+
<DropdownMenuSeparator className="my-1" />
|
|
153
|
+
<p className="px-2 pt-0.5 pb-1 text-xs text-muted-foreground">
|
|
154
|
+
{onKeep ? messages.yoursUntilKept : messages.yoursOnly}
|
|
155
|
+
</p>
|
|
156
|
+
{onKeep ? (
|
|
157
|
+
<DropdownMenuItem
|
|
158
|
+
onSelect={onKeep}
|
|
159
|
+
className="rounded-md py-1.5 pl-[30px] text-[13px] font-medium text-primary focus:text-primary"
|
|
160
|
+
>
|
|
161
|
+
{messages.keep}
|
|
162
|
+
</DropdownMenuItem>
|
|
163
|
+
) : null}
|
|
164
|
+
</>
|
|
165
|
+
) : null}
|
|
166
|
+
</DropdownMenuContent>
|
|
167
|
+
</DropdownMenu>
|
|
168
|
+
|
|
169
|
+
{resized ? (
|
|
170
|
+
<span className="hidden items-center gap-1 @3xl/canvas:flex">
|
|
171
|
+
<button
|
|
172
|
+
type="button"
|
|
173
|
+
aria-label={messages.reset}
|
|
174
|
+
title={messages.reset}
|
|
175
|
+
onClick={onReset}
|
|
176
|
+
className="flex size-8 items-center justify-center rounded-lg text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring"
|
|
177
|
+
>
|
|
178
|
+
<RotateCcw className="size-3.5" aria-hidden="true" />
|
|
179
|
+
</button>
|
|
180
|
+
{onKeep ? (
|
|
181
|
+
<button
|
|
182
|
+
type="button"
|
|
183
|
+
onClick={onKeep}
|
|
184
|
+
className="h-7 shrink-0 rounded-lg bg-primary px-2.5 text-xs font-medium whitespace-nowrap text-primary-foreground outline-none transition-colors hover:bg-primary/90 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1"
|
|
185
|
+
>
|
|
186
|
+
{messages.keep}
|
|
187
|
+
</button>
|
|
188
|
+
) : null}
|
|
189
|
+
</span>
|
|
190
|
+
) : null}
|
|
191
|
+
</div>
|
|
192
|
+
)
|
|
193
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { ARTIFACT_LIMITS } from "@sixb/core/artifacts"
|
|
2
|
+
|
|
3
|
+
// An opened board's page as the reader looks at it: the size it is laid out at, which they may
|
|
4
|
+
// change for themselves, the sizes it snaps to, and how far it is zoomed.
|
|
5
|
+
|
|
6
|
+
/** A page's size as it is laid out, in its own pixels. */
|
|
7
|
+
export interface PageSize {
|
|
8
|
+
readonly w: number
|
|
9
|
+
readonly h: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** What a suggested size is: the board's own, the page's full height, or a common screen. */
|
|
13
|
+
export type SizeName = "board" | "full" | "phone" | "tablet" | "laptop" | "desktop"
|
|
14
|
+
|
|
15
|
+
/** A size the page snaps to: a width, a height, or both. */
|
|
16
|
+
export interface SizeSuggestion {
|
|
17
|
+
readonly name: SizeName
|
|
18
|
+
readonly w?: number
|
|
19
|
+
readonly h?: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The sizes a resize snaps to, by the edge it moves. */
|
|
23
|
+
export interface SizeSuggestions {
|
|
24
|
+
readonly widths: readonly SizeSuggestion[]
|
|
25
|
+
readonly heights: readonly SizeSuggestion[]
|
|
26
|
+
/** Both at once, for the corner: the board's size and the common screens'. */
|
|
27
|
+
readonly sizes: readonly SizeSuggestion[]
|
|
28
|
+
/** The least the page is laid out at: {@link PAGE_MIN}, or the board's own size when less. */
|
|
29
|
+
readonly least: PageSize
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The edges a resize moves. */
|
|
33
|
+
export interface ResizedEdges {
|
|
34
|
+
readonly w: boolean
|
|
35
|
+
readonly h: boolean
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The common screens' widths a page is laid out at, and their sizes as a whole. */
|
|
39
|
+
export const SCREENS = [
|
|
40
|
+
{ name: "phone", w: 390, h: 844 },
|
|
41
|
+
{ name: "tablet", w: 768, h: 1024 },
|
|
42
|
+
{ name: "laptop", w: 1280 },
|
|
43
|
+
{ name: "desktop", w: 1440, h: 900 },
|
|
44
|
+
] as const satisfies readonly SizeSuggestion[]
|
|
45
|
+
|
|
46
|
+
/** Whether a size is a common screen's: its width carries to the other boards. */
|
|
47
|
+
export function isScreen(name: SizeName): boolean {
|
|
48
|
+
return SCREENS.some((screen) => screen.name === name)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The least a page is laid out at, which still reads as a page, and the most, the largest board a
|
|
53
|
+
* canvas holds.
|
|
54
|
+
*/
|
|
55
|
+
export const PAGE_MIN: PageSize = { w: 240, h: 160 }
|
|
56
|
+
export const PAGE_MAX = ARTIFACT_LIMITS.maxCanvasItemSize
|
|
57
|
+
|
|
58
|
+
/** The grid a resize moves along, the canvas's own. */
|
|
59
|
+
const GRID = 8
|
|
60
|
+
|
|
61
|
+
/** How near, in pixels on screen, a suggested size pulls the edge being dragged to it. */
|
|
62
|
+
export const SNAP_REACH = 8
|
|
63
|
+
|
|
64
|
+
/** The zoom an opened board ranges over, and how much a step moves it, as the canvas's do. */
|
|
65
|
+
export const ZOOM_MIN = 0.1
|
|
66
|
+
export const ZOOM_MAX = 2.5
|
|
67
|
+
const ZOOM_STEP = 1.2
|
|
68
|
+
|
|
69
|
+
/** A size kept within what a page is laid out at, in whole pixels. */
|
|
70
|
+
export function clampSize(size: PageSize, least: PageSize = PAGE_MIN): PageSize {
|
|
71
|
+
return {
|
|
72
|
+
w: Math.round(Math.min(PAGE_MAX, Math.max(least.w, size.w))),
|
|
73
|
+
h: Math.round(Math.min(PAGE_MAX, Math.max(least.h, size.h))),
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* What a page snaps to: the board's own size, its full height once the page reports it, and the
|
|
79
|
+
* common screens. A size the board's own already names is not named twice.
|
|
80
|
+
*/
|
|
81
|
+
export function sizeSuggestions(board: PageSize, content: number | null): SizeSuggestions {
|
|
82
|
+
const least = { w: Math.min(PAGE_MIN.w, board.w), h: Math.min(PAGE_MIN.h, board.h) }
|
|
83
|
+
const full =
|
|
84
|
+
content === null
|
|
85
|
+
? []
|
|
86
|
+
: [{ name: "full" as const, h: clampSize({ ...board, h: content }, least).h }]
|
|
87
|
+
const widths = distinct([{ name: "board", w: board.w }, ...SCREENS], (each) => each.w)
|
|
88
|
+
const heights = distinct([{ name: "board", h: board.h }, ...full], (each) => each.h)
|
|
89
|
+
const sizes = distinct(
|
|
90
|
+
[{ name: "board", w: board.w, h: board.h }, ...SCREENS.filter((screen) => "h" in screen)],
|
|
91
|
+
(each) => `${each.w}×${each.h}`
|
|
92
|
+
)
|
|
93
|
+
return { widths, heights, sizes, least }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function distinct(
|
|
97
|
+
suggestions: readonly SizeSuggestion[],
|
|
98
|
+
key: (suggestion: SizeSuggestion) => unknown
|
|
99
|
+
): SizeSuggestion[] {
|
|
100
|
+
const seen = new Set<unknown>()
|
|
101
|
+
return suggestions.filter((suggestion) => {
|
|
102
|
+
const value = key(suggestion)
|
|
103
|
+
if (seen.has(value)) return false
|
|
104
|
+
seen.add(value)
|
|
105
|
+
return true
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Where a resize leaves the page, and the suggestions it snapped to. */
|
|
110
|
+
export interface SnappedSize {
|
|
111
|
+
readonly size: PageSize
|
|
112
|
+
readonly w: SizeSuggestion | null
|
|
113
|
+
readonly h: SizeSuggestion | null
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Where a resize puts the page: each moved edge on the nearest suggestion within `reach`, in the
|
|
118
|
+
* page's pixels, else on the grid; the corner on a whole suggested size when both edges are
|
|
119
|
+
* near one. `free`, as with Alt held, it goes where the pointer is.
|
|
120
|
+
*/
|
|
121
|
+
export function snapSize(
|
|
122
|
+
raw: PageSize,
|
|
123
|
+
edges: ResizedEdges,
|
|
124
|
+
suggestions: SizeSuggestions,
|
|
125
|
+
reach: number,
|
|
126
|
+
free = false
|
|
127
|
+
): SnappedSize {
|
|
128
|
+
const { least } = suggestions
|
|
129
|
+
if (free) return { size: clampSize(raw, least), w: null, h: null }
|
|
130
|
+
|
|
131
|
+
if (edges.w && edges.h) {
|
|
132
|
+
const size = nearest(suggestions.sizes, (each) =>
|
|
133
|
+
Math.abs((each.w ?? 0) - raw.w) <= reach && Math.abs((each.h ?? 0) - raw.h) <= reach
|
|
134
|
+
? Math.hypot((each.w ?? 0) - raw.w, (each.h ?? 0) - raw.h)
|
|
135
|
+
: null
|
|
136
|
+
)
|
|
137
|
+
if (size?.w !== undefined && size.h !== undefined) {
|
|
138
|
+
return { size: clampSize({ w: size.w, h: size.h }, least), w: size, h: size }
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const w = edges.w ? nearestOn(suggestions.widths, "w", raw.w, reach) : null
|
|
143
|
+
const h = edges.h ? nearestOn(suggestions.heights, "h", raw.h, reach) : null
|
|
144
|
+
const size = clampSize(
|
|
145
|
+
{
|
|
146
|
+
w: edges.w ? (w?.w ?? onGrid(raw.w)) : raw.w,
|
|
147
|
+
h: edges.h ? (h?.h ?? onGrid(raw.h)) : raw.h,
|
|
148
|
+
},
|
|
149
|
+
least
|
|
150
|
+
)
|
|
151
|
+
return { size, w, h }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function nearestOn(
|
|
155
|
+
suggestions: readonly SizeSuggestion[],
|
|
156
|
+
axis: "w" | "h",
|
|
157
|
+
value: number,
|
|
158
|
+
reach: number
|
|
159
|
+
): SizeSuggestion | null {
|
|
160
|
+
return nearest(suggestions, (each) => {
|
|
161
|
+
const at = each[axis]
|
|
162
|
+
return at !== undefined && Math.abs(at - value) <= reach ? Math.abs(at - value) : null
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The suggestion at the least distance; null when none is within reach. */
|
|
167
|
+
function nearest(
|
|
168
|
+
suggestions: readonly SizeSuggestion[],
|
|
169
|
+
distance: (suggestion: SizeSuggestion) => number | null
|
|
170
|
+
): SizeSuggestion | null {
|
|
171
|
+
let best: SizeSuggestion | null = null
|
|
172
|
+
let least = Number.POSITIVE_INFINITY
|
|
173
|
+
for (const suggestion of suggestions) {
|
|
174
|
+
const away = distance(suggestion)
|
|
175
|
+
if (away === null || away >= least) continue
|
|
176
|
+
best = suggestion
|
|
177
|
+
least = away
|
|
178
|
+
}
|
|
179
|
+
return best
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function onGrid(value: number): number {
|
|
183
|
+
return Math.round(value / GRID) * GRID
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The room an opened board is fitted into, on screen. */
|
|
187
|
+
export interface Room {
|
|
188
|
+
readonly width: number
|
|
189
|
+
readonly height: number
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The zoom a page opens at. One taller than wide reads like a document: fitted to the room's
|
|
194
|
+
* width and scrolled, never enlarged past its own size, unless it nearly fits whole, which it
|
|
195
|
+
* then does. Any other opens whole. `whole` always fits it whole.
|
|
196
|
+
*/
|
|
197
|
+
export function fitScale(page: PageSize, room: Room, how: "open" | "whole"): number {
|
|
198
|
+
const whole = clampZoom(Math.min(room.width / page.w, room.height / page.h))
|
|
199
|
+
if (how === "whole" || page.h <= page.w) return whole
|
|
200
|
+
|
|
201
|
+
const width = clampZoom(Math.min(room.width / page.w, Math.max(whole, 1)))
|
|
202
|
+
// A few pixels of scrolling read as a page cut off: it shows whole instead.
|
|
203
|
+
return whole >= width * 0.8 ? whole : width
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** One step of ⌘+ or ⌘−, or of the zoom bar's buttons: a fifth more, or less. */
|
|
207
|
+
export function zoomStep(scale: number, direction: "in" | "out"): number {
|
|
208
|
+
return clampZoom(direction === "in" ? scale * ZOOM_STEP : scale / ZOOM_STEP)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function clampZoom(scale: number): number {
|
|
212
|
+
return Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, scale))
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Whether two sizes are one. */
|
|
216
|
+
export function sameSize(left: PageSize, right: PageSize): boolean {
|
|
217
|
+
return left.w === right.w && left.h === right.h
|
|
218
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react"
|
|
2
|
+
import { clampZoom, fitScale, type PageSize, type Room, zoomStep } from "./page-size"
|
|
3
|
+
|
|
4
|
+
/** How an opened board is zoomed: fitted, as it opens or whole, or at a zoom the reader chose. */
|
|
5
|
+
type Zoom = { readonly fit: "open" | "whole" } | { readonly scale: number }
|
|
6
|
+
|
|
7
|
+
const OPEN: Zoom = { fit: "open" }
|
|
8
|
+
|
|
9
|
+
/** How long the page takes to glide back to the middle once a resize ends. */
|
|
10
|
+
const SETTLE_MS = 220
|
|
11
|
+
|
|
12
|
+
/** A point on the page, in its own pixels. */
|
|
13
|
+
export interface PagePoint {
|
|
14
|
+
readonly x: number
|
|
15
|
+
readonly y: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Where the page sits in the stage that scrolls over it, and how large that stage's content is. */
|
|
19
|
+
export interface PagePlacement {
|
|
20
|
+
readonly left: number
|
|
21
|
+
readonly top: number
|
|
22
|
+
readonly width: number
|
|
23
|
+
readonly height: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Where the page sits: centered while it fits the view, else from the padding, the content as
|
|
28
|
+
* large as it takes. `held`, its top left stays where a resize found it.
|
|
29
|
+
*/
|
|
30
|
+
export function pagePlacement(
|
|
31
|
+
view: Room,
|
|
32
|
+
box: Room,
|
|
33
|
+
pad: StagePadding,
|
|
34
|
+
held: { readonly left: number; readonly top: number } | null = null
|
|
35
|
+
): PagePlacement {
|
|
36
|
+
const left = held?.left ?? Math.max(pad.side, (view.width - box.width) / 2)
|
|
37
|
+
const top =
|
|
38
|
+
held?.top ?? Math.max(pad.top, pad.top + (view.height - pad.top - pad.bottom - box.height) / 2)
|
|
39
|
+
return {
|
|
40
|
+
left,
|
|
41
|
+
top,
|
|
42
|
+
width: Math.max(view.width, left + box.width + pad.side),
|
|
43
|
+
height: Math.max(view.height, top + box.height + pad.bottom),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The room kept around the page. */
|
|
48
|
+
export interface StagePadding {
|
|
49
|
+
readonly side: number
|
|
50
|
+
readonly top: number
|
|
51
|
+
readonly bottom: number
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The room kept around the page: less on a phone, where its foot stays clear of the zoom bar,
|
|
56
|
+
* which would cover its bottom handle.
|
|
57
|
+
*/
|
|
58
|
+
export function stagePadding(view: Room): StagePadding {
|
|
59
|
+
return view.width < 640 ? { side: 12, top: 12, bottom: 64 } : { side: 24, top: 24, bottom: 24 }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* An opened board's zoom, and where its page sits in the stage scrolled over it. It opens fitted
|
|
64
|
+
* as {@link fitScale} says, and stays fitted as the room changes, until the reader zooms. A zoom
|
|
65
|
+
* keeps the point it is made around where it was on screen: the pointer's, or the view's middle.
|
|
66
|
+
* Each board opens fitted again.
|
|
67
|
+
*/
|
|
68
|
+
export function usePageZoom({
|
|
69
|
+
id,
|
|
70
|
+
page,
|
|
71
|
+
view,
|
|
72
|
+
stage,
|
|
73
|
+
}: {
|
|
74
|
+
/** The board shown: another opens fitted. */
|
|
75
|
+
readonly id: string
|
|
76
|
+
readonly page: PageSize
|
|
77
|
+
/** The stage's size on screen, once measured. */
|
|
78
|
+
readonly view: Room | null
|
|
79
|
+
/** The element that scrolls over the page. */
|
|
80
|
+
readonly stage: HTMLElement | null
|
|
81
|
+
}) {
|
|
82
|
+
const [state, setState] = useState<{ readonly id: string; readonly zoom: Zoom }>({
|
|
83
|
+
id,
|
|
84
|
+
zoom: OPEN,
|
|
85
|
+
})
|
|
86
|
+
const zoom = state.id === id ? state.zoom : OPEN
|
|
87
|
+
const [held, setHeld] = useState<{ readonly left: number; readonly top: number } | null>(null)
|
|
88
|
+
// A resize just ended: the page glides back to the middle rather than jumping there.
|
|
89
|
+
const [settling, setSettling] = useState(false)
|
|
90
|
+
|
|
91
|
+
const pad = view ? stagePadding(view) : null
|
|
92
|
+
const room =
|
|
93
|
+
view && pad
|
|
94
|
+
? {
|
|
95
|
+
width: Math.max(1, view.width - pad.side * 2),
|
|
96
|
+
height: Math.max(1, view.height - pad.top - pad.bottom),
|
|
97
|
+
}
|
|
98
|
+
: null
|
|
99
|
+
const scale = !room ? 0 : "scale" in zoom ? zoom.scale : fitScale(page, room, zoom.fit)
|
|
100
|
+
const box = { width: page.w * scale, height: page.h * scale }
|
|
101
|
+
const placement = view && pad ? pagePlacement(view, box, pad, held) : null
|
|
102
|
+
|
|
103
|
+
// A zoom made and not drawn yet: the page point it is made around, and where on the view that
|
|
104
|
+
// point stays. Zooms made before it is drawn, as a pinch's are, go on from it.
|
|
105
|
+
const anchor = useRef<{ x: number; y: number; vx: number; vy: number; scale: number } | null>(
|
|
106
|
+
null
|
|
107
|
+
)
|
|
108
|
+
const latest = useRef({ id, scale, placement, view })
|
|
109
|
+
latest.current = { id, scale, placement, view }
|
|
110
|
+
|
|
111
|
+
useLayoutEffect(() => {
|
|
112
|
+
const point = anchor.current
|
|
113
|
+
if (!point || !stage || !placement) return
|
|
114
|
+
|
|
115
|
+
anchor.current = null
|
|
116
|
+
stage.scrollLeft = placement.left + point.x * scale - point.vx
|
|
117
|
+
stage.scrollTop = placement.top + point.y * scale - point.vy
|
|
118
|
+
}, [stage, placement, scale])
|
|
119
|
+
|
|
120
|
+
// Another board, or a fit, starts at the page's top.
|
|
121
|
+
const fitted = "fit" in zoom ? zoom.fit : null
|
|
122
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: another board is one more start.
|
|
123
|
+
useLayoutEffect(() => {
|
|
124
|
+
if (fitted === null || !stage) return
|
|
125
|
+
stage.scrollTop = 0
|
|
126
|
+
stage.scrollLeft = 0
|
|
127
|
+
}, [stage, fitted, id])
|
|
128
|
+
|
|
129
|
+
/** Zooms to `next`, around a point of the page, or the view's middle. */
|
|
130
|
+
const zoomTo = useCallback(
|
|
131
|
+
(next: number, at: PagePoint | null = null) => {
|
|
132
|
+
const { id, scale, placement, view } = latest.current
|
|
133
|
+
if (!stage || !placement || !view || scale === 0) return
|
|
134
|
+
|
|
135
|
+
const pending = anchor.current
|
|
136
|
+
const from = pending?.scale ?? scale
|
|
137
|
+
const target = clampZoom(next)
|
|
138
|
+
if (Math.abs(target - from) < 1e-4) return
|
|
139
|
+
|
|
140
|
+
if (!pending) {
|
|
141
|
+
const vx = at ? placement.left + at.x * scale - stage.scrollLeft : view.width / 2
|
|
142
|
+
const vy = at ? placement.top + at.y * scale - stage.scrollTop : view.height / 2
|
|
143
|
+
anchor.current = {
|
|
144
|
+
x: (stage.scrollLeft + vx - placement.left) / scale,
|
|
145
|
+
y: (stage.scrollTop + vy - placement.top) / scale,
|
|
146
|
+
vx,
|
|
147
|
+
vy,
|
|
148
|
+
scale: target,
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
anchor.current = { ...pending, scale: target }
|
|
152
|
+
}
|
|
153
|
+
setState({ id, zoom: { scale: target } })
|
|
154
|
+
},
|
|
155
|
+
[stage]
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
/** The zoom now, counting one made and not drawn yet. */
|
|
159
|
+
const current = useCallback(() => anchor.current?.scale ?? latest.current.scale, [])
|
|
160
|
+
|
|
161
|
+
const zoomBy = useCallback(
|
|
162
|
+
(factor: number, at: PagePoint | null = null) => zoomTo(current() * factor, at),
|
|
163
|
+
[zoomTo, current]
|
|
164
|
+
)
|
|
165
|
+
const step = useCallback(
|
|
166
|
+
(direction: "in" | "out") => zoomTo(zoomStep(current(), direction)),
|
|
167
|
+
[zoomTo, current]
|
|
168
|
+
)
|
|
169
|
+
const fit = useCallback((how: "open" | "whole" = "whole") => {
|
|
170
|
+
anchor.current = null
|
|
171
|
+
setState({ id: latest.current.id, zoom: { fit: how } })
|
|
172
|
+
}, [])
|
|
173
|
+
|
|
174
|
+
/** A double-click: the page at its own size around the pointer, or, at it, the whole page. */
|
|
175
|
+
const toggle = useCallback(
|
|
176
|
+
(at: PagePoint) => {
|
|
177
|
+
if (Math.abs(current() - 1) < 0.01) fit("whole")
|
|
178
|
+
else zoomTo(1, at)
|
|
179
|
+
},
|
|
180
|
+
[fit, zoomTo, current]
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
/** A point on screen as a point of the page. */
|
|
184
|
+
const pageAt = useCallback(
|
|
185
|
+
(clientX: number, clientY: number): PagePoint | null => {
|
|
186
|
+
const { scale, placement } = latest.current
|
|
187
|
+
if (!stage || !placement || scale === 0) return null
|
|
188
|
+
|
|
189
|
+
const rect = stage.getBoundingClientRect()
|
|
190
|
+
return {
|
|
191
|
+
x: (stage.scrollLeft + clientX - rect.left - placement.left) / scale,
|
|
192
|
+
y: (stage.scrollTop + clientY - rect.top - placement.top) / scale,
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
[stage]
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
/** A resize starts: the zoom, and the page's top left, stay as they are until it ends. */
|
|
199
|
+
const hold = useCallback(() => {
|
|
200
|
+
const { id, scale, placement } = latest.current
|
|
201
|
+
if (!placement) return
|
|
202
|
+
setState({ id, zoom: { scale } })
|
|
203
|
+
setHeld({ left: placement.left, top: placement.top })
|
|
204
|
+
}, [])
|
|
205
|
+
const release = useCallback(() => {
|
|
206
|
+
setHeld(null)
|
|
207
|
+
setSettling(true)
|
|
208
|
+
}, [])
|
|
209
|
+
|
|
210
|
+
useEffect(() => {
|
|
211
|
+
if (!settling) return
|
|
212
|
+
const timer = window.setTimeout(() => setSettling(false), SETTLE_MS)
|
|
213
|
+
return () => window.clearTimeout(timer)
|
|
214
|
+
}, [settling])
|
|
215
|
+
|
|
216
|
+
// ⌘ or Ctrl with the wheel, or a trackpad's pinch, over the stage around the page zooms it, as
|
|
217
|
+
// the frame says the reader does over the page itself; two fingers pinch on a touch screen.
|
|
218
|
+
useEffect(() => {
|
|
219
|
+
if (!stage) return
|
|
220
|
+
const mac = /Mac|iPhone|iPad/.test(navigator.platform)
|
|
221
|
+
|
|
222
|
+
const onWheel = (event: WheelEvent) => {
|
|
223
|
+
if (!(event.ctrlKey || event.metaKey)) return
|
|
224
|
+
event.preventDefault()
|
|
225
|
+
|
|
226
|
+
const unit = event.deltaMode === 1 ? 0.05 : event.deltaMode ? 1 : 0.002
|
|
227
|
+
const factor = 2 ** (-event.deltaY * unit * (event.ctrlKey && mac ? 10 : 1))
|
|
228
|
+
zoomBy(factor, pageAt(event.clientX, event.clientY))
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let spread = 0
|
|
232
|
+
const spreadOf = (touches: TouchList) => {
|
|
233
|
+
const [one, two] = [touches[0], touches[1]]
|
|
234
|
+
return one && two ? Math.hypot(one.clientX - two.clientX, one.clientY - two.clientY) : 0
|
|
235
|
+
}
|
|
236
|
+
const onTouchStart = (event: TouchEvent) => {
|
|
237
|
+
spread = event.touches.length === 2 ? spreadOf(event.touches) : 0
|
|
238
|
+
}
|
|
239
|
+
const onTouchMove = (event: TouchEvent) => {
|
|
240
|
+
const [one, two] = [event.touches[0], event.touches[1]]
|
|
241
|
+
if (!one || !two || event.touches.length !== 2 || spread === 0) return
|
|
242
|
+
event.preventDefault()
|
|
243
|
+
|
|
244
|
+
const next = spreadOf(event.touches)
|
|
245
|
+
zoomBy(
|
|
246
|
+
next / spread,
|
|
247
|
+
pageAt((one.clientX + two.clientX) / 2, (one.clientY + two.clientY) / 2)
|
|
248
|
+
)
|
|
249
|
+
spread = next
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
stage.addEventListener("wheel", onWheel, { passive: false })
|
|
253
|
+
stage.addEventListener("touchstart", onTouchStart, { passive: true })
|
|
254
|
+
stage.addEventListener("touchmove", onTouchMove, { passive: false })
|
|
255
|
+
return () => {
|
|
256
|
+
stage.removeEventListener("wheel", onWheel)
|
|
257
|
+
stage.removeEventListener("touchstart", onTouchStart)
|
|
258
|
+
stage.removeEventListener("touchmove", onTouchMove)
|
|
259
|
+
}
|
|
260
|
+
}, [stage, zoomBy, pageAt])
|
|
261
|
+
|
|
262
|
+
return {
|
|
263
|
+
scale,
|
|
264
|
+
placement,
|
|
265
|
+
/** Whether a resize holds the page where it is. */
|
|
266
|
+
held: held !== null,
|
|
267
|
+
/** Whether the page glides back to where it sits, as a resize ends. */
|
|
268
|
+
settling,
|
|
269
|
+
zoomTo,
|
|
270
|
+
zoomBy,
|
|
271
|
+
step,
|
|
272
|
+
fit,
|
|
273
|
+
toggle,
|
|
274
|
+
pageAt,
|
|
275
|
+
hold,
|
|
276
|
+
release,
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export type PageZoom = ReturnType<typeof usePageZoom>
|