Administration page (board 10), species, rights, official sheets list with filters, creation from the core's empty document, overview with the working-copy action; catalogue list (search by sheet name), sheet and version pages through PreviewSheet with zoom and pan. App 650, typecheck, lint, build green.
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
/**
|
||
* Display framing of the catalogue canvas (board 03): a CSS scale and
|
||
* translation of the inserted drawing, never a change to the drawing. At
|
||
* scale 1 the drawing fills the box with its whole frame visible (the
|
||
* composer's `xMidYMid meet`); zooming keeps the point under the finger or
|
||
* the box centre in place, and panning never uncovers the box.
|
||
*/
|
||
export interface Framing {
|
||
scale: number
|
||
x: number
|
||
y: number
|
||
}
|
||
|
||
export interface Box {
|
||
width: number
|
||
height: number
|
||
}
|
||
|
||
export const FIT: Framing = { scale: 1, x: 0, y: 0 }
|
||
export const MAX_SCALE = 6
|
||
/** One press of − or +. */
|
||
export const ZOOM_STEP = 1.5
|
||
|
||
function clamp(value: number, min: number, max: number): number {
|
||
return Math.min(max, Math.max(min, value))
|
||
}
|
||
|
||
/** The translation that keeps the scaled drawing over the whole box. */
|
||
function contained(framing: Framing, box: Box): Framing {
|
||
const { scale } = framing
|
||
return {
|
||
scale,
|
||
x: clamp(framing.x, box.width * (1 - scale), 0),
|
||
y: clamp(framing.y, box.height * (1 - scale), 0),
|
||
}
|
||
}
|
||
|
||
/** Scales by `factor` around a point of the box (its centre by default). */
|
||
export function zoomBy(
|
||
framing: Framing,
|
||
factor: number,
|
||
box: Box,
|
||
at = { x: box.width / 2, y: box.height / 2 },
|
||
): Framing {
|
||
const scale = clamp(framing.scale * factor, 1, MAX_SCALE)
|
||
const ratio = scale / framing.scale
|
||
return contained(
|
||
{
|
||
scale,
|
||
x: at.x - (at.x - framing.x) * ratio,
|
||
y: at.y - (at.y - framing.y) * ratio,
|
||
},
|
||
box,
|
||
)
|
||
}
|
||
|
||
export function panBy(framing: Framing, dx: number, dy: number, box: Box): Framing {
|
||
return contained({ ...framing, x: framing.x + dx, y: framing.y + dy }, box)
|
||
}
|
||
|
||
export function transformOf(framing: Framing): string {
|
||
return `translate(${framing.x}px, ${framing.y}px) scale(${framing.scale})`
|
||
}
|