feat(app): editable sheet preview, edit wrappers, cleaning preview and ids
This commit is contained in:
1 parent
2f824a947c
commit
173be5949a
10 files changed
+663
-13
No files matched your search
@@ -2,6 +2,7 @@ import {
|
||||
CORE_UNAVAILABLE,
|
||||
closedRefusal,
|
||||
parseOutcome,
|
||||
type CoreFailure,
|
||||
type CoreOutcome,
|
||||
type CoreRuntime,
|
||||
type GluePreviewSheet,
|
||||
@@ -19,6 +20,24 @@ export interface PreviewDrawing {
|
||||
bytes: Uint8Array
|
||||
}
|
||||
|
||||
/** What `accept` answers about the candidate state: kept, or a refusal the caller shows. */
|
||||
export type Acceptance<R> = { accepted: true } | { accepted: false; refusal: R }
|
||||
|
||||
/**
|
||||
* `replaceDocument`'s answer. Only `ok: true` tells the caller to bump its handle counter;
|
||||
* a `core` failure carries the closed kind, a `refused` one the caller's own refusal.
|
||||
*/
|
||||
export type ReplaceOutcome<R> =
|
||||
| { ok: true; state: PreviewState }
|
||||
| { ok: false; reason: 'core'; failure: CoreFailure }
|
||||
| { ok: false; reason: 'refused'; refusal: R }
|
||||
|
||||
/** A trap or a stopped module: no handle to roll back, the rebuild uses the kept input. */
|
||||
const TRAPPED_KINDS: ReadonlySet<string> = new Set([
|
||||
'internal',
|
||||
'core_unavailable',
|
||||
])
|
||||
|
||||
/**
|
||||
* A stored version loaded into the core (`PreviewSheet`, RUST.md §12): the
|
||||
* core checks each drawing's sha256 against its row, the fingerprint, the caps
|
||||
@@ -27,17 +46,74 @@ export interface PreviewDrawing {
|
||||
* the module: after a trap and a recovery it is rebuilt from the kept input,
|
||||
* a handle of a dropped instance is never called. `free` releases it when the
|
||||
* page leaves.
|
||||
*
|
||||
* A draft's handle is editable: `replaceDocument` and `addDrawing` keep a new
|
||||
* text or drawing only once the core accepted it, so a rebuild after a trap
|
||||
* starts from the last accepted text and drawings (a text that trapped is
|
||||
* never kept, LRN-058).
|
||||
*/
|
||||
export class CataloguePreview {
|
||||
private handle: GluePreviewSheet | null = null
|
||||
private handleGeneration = -1
|
||||
private freed = false
|
||||
private readonly drawings = new Map<string, PreviewDrawing>()
|
||||
|
||||
constructor(
|
||||
private readonly runtime: CoreRuntime,
|
||||
private readonly input: PreviewInput,
|
||||
private readonly drawings: readonly PreviewDrawing[],
|
||||
) {}
|
||||
private input: PreviewInput,
|
||||
drawings: readonly PreviewDrawing[],
|
||||
) {
|
||||
for (const drawing of drawings) this.drawings.set(drawing.assetId, drawing)
|
||||
}
|
||||
|
||||
/** The last accepted document text (what a rebuild would load). */
|
||||
get document(): string {
|
||||
return this.input.document
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the draft's document. The new text becomes the kept input only when
|
||||
* `replace_document`, `preview.state` and `accept(state)` all succeed; otherwise the
|
||||
* handle returns to the previous text and the kept input and drawings stay as they were.
|
||||
*/
|
||||
replaceDocument<R = never>(
|
||||
text: string,
|
||||
accept?: (state: PreviewState) => Acceptance<R>,
|
||||
): ReplaceOutcome<R> {
|
||||
const previous = this.input.document
|
||||
const handle = this.current()
|
||||
if (!handle.ok) return { ok: false, reason: 'core', failure: handle }
|
||||
const replaced = this.runtime.invoke(() =>
|
||||
parseOutcome<null>(handle.value.replace_document(text)),
|
||||
)
|
||||
if (!replaced.ok) return { ok: false, reason: 'core', failure: replaced }
|
||||
const outcome = this.judge(accept)
|
||||
if (outcome.ok) this.input = { ...this.input, document: text }
|
||||
else if (
|
||||
outcome.reason === 'refused' ||
|
||||
!TRAPPED_KINDS.has(outcome.failure.kind)
|
||||
) {
|
||||
this.rollback(previous)
|
||||
}
|
||||
return outcome
|
||||
}
|
||||
|
||||
/** Adds or replaces a drawing; kept (for a rebuild) only when the core accepted it. */
|
||||
addDrawing(drawing: PreviewDrawing): CoreOutcome<null> {
|
||||
const handle = this.current()
|
||||
if (!handle.ok) return handle
|
||||
const added = this.runtime.invoke(() =>
|
||||
parseOutcome<null>(
|
||||
handle.value.add_drawing(
|
||||
drawing.assetId,
|
||||
drawing.sha256,
|
||||
drawing.bytes,
|
||||
),
|
||||
),
|
||||
)
|
||||
if (added.ok) this.drawings.set(drawing.assetId, drawing)
|
||||
return added
|
||||
}
|
||||
|
||||
/** The first call builds the handle: the builder's refusals come back as closed kinds. */
|
||||
state(): CoreOutcome<PreviewState> {
|
||||
@@ -53,6 +129,32 @@ export class CataloguePreview {
|
||||
/** After this, every call answers `core_unavailable`: a page that left never rebuilds. */
|
||||
free(): void {
|
||||
this.freed = true
|
||||
this.discardHandle()
|
||||
}
|
||||
|
||||
private judge<R>(
|
||||
accept: ((state: PreviewState) => Acceptance<R>) | undefined,
|
||||
): ReplaceOutcome<R> {
|
||||
const state = this.state()
|
||||
if (!state.ok) return { ok: false, reason: 'core', failure: state }
|
||||
const verdict = accept?.(state.value) ?? { accepted: true }
|
||||
return verdict.accepted
|
||||
? { ok: true, state: state.value }
|
||||
: { ok: false, reason: 'refused', refusal: verdict.refusal }
|
||||
}
|
||||
|
||||
/** Puts the handle back on the kept text; if even that fails the handle is dropped and rebuilt. */
|
||||
private rollback(previous: string): void {
|
||||
const handle = this.current()
|
||||
const restored = handle.ok
|
||||
? this.runtime.invoke(() =>
|
||||
parseOutcome<null>(handle.value.replace_document(previous)),
|
||||
)
|
||||
: handle
|
||||
if (!restored.ok) this.discardHandle()
|
||||
}
|
||||
|
||||
private discardHandle(): void {
|
||||
const handle = this.handle
|
||||
this.handle = null
|
||||
if (!handle || this.handleGeneration !== this.runtime.generation) return
|
||||
@@ -79,7 +181,7 @@ export class CataloguePreview {
|
||||
this.handle = null
|
||||
return this.runtime.invoke<GluePreviewSheet>((glue) => {
|
||||
const builder = new glue.PreviewBuilder()
|
||||
for (const drawing of this.drawings) {
|
||||
for (const drawing of this.drawings.values()) {
|
||||
// `false` past the handle's bounds: the build then refuses with `caps`.
|
||||
builder.add_drawing(drawing.assetId, drawing.sha256, drawing.bytes)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,14 @@ import {
|
||||
type PointerEvent,
|
||||
type RefObject,
|
||||
} from 'react'
|
||||
import { FIT, ZOOM_STEP, panBy, zoomBy, type Box, type Framing } from './framing'
|
||||
import {
|
||||
FIT,
|
||||
ZOOM_STEP,
|
||||
panBy,
|
||||
zoomBy,
|
||||
type Box,
|
||||
type Framing,
|
||||
} from './framing'
|
||||
|
||||
/** Movement past which a press is a drag, not a click on a zone. */
|
||||
const DRAG_THRESHOLD = 4
|
||||
@@ -15,7 +22,14 @@ const KEY_PAN = 40
|
||||
|
||||
type Point = { x: number; y: number }
|
||||
|
||||
function boxOf(element: HTMLElement | null): Box & { left: number; top: number } {
|
||||
export interface FramingOptions {
|
||||
/** Asked on a one-pointer press; false leaves that drag to the caller (a drawing tool). */
|
||||
canPan?: (event: PointerEvent) => boolean
|
||||
}
|
||||
|
||||
function boxOf(
|
||||
element: HTMLElement | null,
|
||||
): Box & { left: number; top: number } {
|
||||
const rect = element?.getBoundingClientRect()
|
||||
return {
|
||||
width: rect?.width ?? 0,
|
||||
@@ -36,13 +50,21 @@ function distance(a: Point, b: Point): number {
|
||||
* + and −, 0 fits). `reset` comes back to the whole drawing. A press that
|
||||
* moved is not a click: `swallowClick` says so once.
|
||||
*/
|
||||
export function useFraming(viewport: RefObject<HTMLElement | null>, resetKey: string) {
|
||||
export function useFraming(
|
||||
viewport: RefObject<HTMLElement | null>,
|
||||
resetKey: string,
|
||||
options: FramingOptions = {},
|
||||
) {
|
||||
const [framing, setFraming] = useState<Framing>(FIT)
|
||||
const pointers = useRef(new Map<number, Point>())
|
||||
const moved = useRef(0)
|
||||
const dragged = useRef(false)
|
||||
// The wheel listener reads the scale without re-subscribing on every change.
|
||||
const scale = useRef(FIT.scale)
|
||||
// The press decision is made once, at pointer-down, and kept for the whole gesture.
|
||||
const panAllowed = useRef(true)
|
||||
const canPan = useRef(options.canPan)
|
||||
canPan.current = options.canPan
|
||||
|
||||
useEffect(() => setFraming(FIT), [resetKey])
|
||||
useEffect(() => {
|
||||
@@ -81,10 +103,14 @@ export function useFraming(viewport: RefObject<HTMLElement | null>, resetKey: st
|
||||
}, [viewport, zoom])
|
||||
|
||||
const onPointerDown = useCallback((event: PointerEvent) => {
|
||||
pointers.current.set(event.pointerId, { x: event.clientX, y: event.clientY })
|
||||
pointers.current.set(event.pointerId, {
|
||||
x: event.clientX,
|
||||
y: event.clientY,
|
||||
})
|
||||
if (pointers.current.size === 1) {
|
||||
moved.current = 0
|
||||
dragged.current = false
|
||||
panAllowed.current = canPan.current?.(event) ?? true
|
||||
}
|
||||
}, [])
|
||||
|
||||
@@ -93,17 +119,23 @@ export function useFraming(viewport: RefObject<HTMLElement | null>, resetKey: st
|
||||
const before = pointers.current.get(event.pointerId)
|
||||
if (!before) return
|
||||
const now = { x: event.clientX, y: event.clientY }
|
||||
const others = [...pointers.current].filter(([id]) => id !== event.pointerId)
|
||||
const others = [...pointers.current].filter(
|
||||
([id]) => id !== event.pointerId,
|
||||
)
|
||||
pointers.current.set(event.pointerId, now)
|
||||
moved.current += distance(before, now)
|
||||
if (moved.current > DRAG_THRESHOLD) dragged.current = true
|
||||
if (others.length === 1) {
|
||||
const other = others[0][1]
|
||||
const box = boxOf(viewport.current)
|
||||
const at = { x: (now.x + other.x) / 2 - box.left, y: (now.y + other.y) / 2 - box.top }
|
||||
const factor = distance(now, other) / Math.max(distance(before, other), 1)
|
||||
const at = {
|
||||
x: (now.x + other.x) / 2 - box.left,
|
||||
y: (now.y + other.y) / 2 - box.top,
|
||||
}
|
||||
const factor =
|
||||
distance(now, other) / Math.max(distance(before, other), 1)
|
||||
zoom(factor, at)
|
||||
} else if (others.length === 0 && dragged.current) {
|
||||
} else if (others.length === 0 && dragged.current && panAllowed.current) {
|
||||
event.currentTarget.setPointerCapture?.(event.pointerId)
|
||||
pan(now.x - before.x, now.y - before.y)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user