fix(editor): loupe copy without ids, two fingers counted in the pinch

Minor 1: the loupe's copy of the drawing drops every id (the equine
drawing has 142); its url(#…) and href="#…" resolve to the originals.
Minor 2: useFraming's onPinch marks the two-finger flag at the pinch
move itself; the passive effect that read the live press phase and
the press hook's multi() are removed.
This commit is contained in:
bchanot committed 2026-11-18 11:17:42 +01:00
1 parent 181f81789a
commit dd3f245bca
5 files changed
+59 -33

No files matched your search

+12
View File
@@ -35,6 +35,8 @@ export interface FramingOptions {
* follows the fingers' spread since the pinch began (a clamp at the fit never ratchets).
*/
twoPointerPan?: boolean
/** A two-pointer move changed the framing: told at the move itself, never later. */
onPinch?: () => void
}
/** Where a pinch began: its spread and the scale then. */
@@ -59,6 +61,9 @@ function distance(a: Point, b: Point): number {
return Math.hypot(a.x - b.x, a.y - b.y)
}
const sameFraming = (a: Framing, b: Framing) =>
a.scale === b.scale && a.x === b.x && a.y === b.y
const midpoint = (a: Point, b: Point, box: { left: number; top: number }) => ({
x: (a.x + b.x) / 2 - box.left,
y: (a.y + b.y) / 2 - box.top,
@@ -131,6 +136,11 @@ export function useFraming(
const twoPointerPan = useRef(options.twoPointerPan ?? false)
twoPointerPan.current = options.twoPointerPan ?? false
const pinch = useRef<PinchStart | null>(null)
const onPinch = useRef(options.onPinch)
onPinch.current = options.onPinch
// The framing as last rendered: a pinch move compares its result with it.
const shown = useRef(framing)
shown.current = framing
useEffect(() => setFraming(FIT), [resetKey])
useEffect(() => {
@@ -186,6 +196,8 @@ export function useFraming(
const box = boxOf(viewport.current)
const start = pinch.current
if (twoPointerPan.current && start) {
const next = pinchPan(shown.current, start, fingers, box)
if (!sameFraming(next, shown.current)) onPinch.current?.()
setFraming((f) => pinchPan(f, start, fingers, box))
return
}
@@ -45,8 +45,9 @@ function useBoxAtOpen(viewport: RefObject<HTMLDivElement | null>) {
}
/**
* The drawing copied once into `into`: a picture, so every `data-anno-id` is removed and
* no press or locator ever reads the copy as a marker.
* The drawing copied once into `into`: a picture, so every `data-anno-id` is removed (no
* press or locator ever reads the copy as a marker) and every `id` too (the document keeps
* unique ids; the copy's `url(#…)` and `href="#…"` resolve to the originals, same content).
*/
function usePicture(
host: RefObject<HTMLDivElement | null>,
@@ -56,8 +57,10 @@ function usePicture(
const drawing = host.current?.querySelector('svg')
if (!drawing || !into.current) return
const copy = drawing.cloneNode(true) as SVGSVGElement
for (const marked of [copy, ...copy.querySelectorAll('[data-anno-id]')]) {
marked.removeAttribute('data-anno-id')
const named = copy.querySelectorAll('[data-anno-id], [id]')
for (const element of [copy, ...named]) {
element.removeAttribute('data-anno-id')
element.removeAttribute('id')
}
into.current.replaceChildren(copy)
}, [host, into])
@@ -1,11 +1,4 @@
import {
useEffect,
useRef,
type CSSProperties,
type KeyboardEvent,
type PointerEvent,
} from 'react'
import type { Framing as Frame } from '@/catalogue/framing'
import type { CSSProperties, KeyboardEvent, PointerEvent } from 'react'
import { useFraming } from '@/catalogue/useFraming'
import type { ToolId } from './uiState'
import { usePressGesture, type PressInput } from './usePressGesture'
@@ -58,24 +51,6 @@ function touchStyle(
return { touchAction: none ? 'none' : 'pan-x pan-y' }
}
const sameFrame = (a: Frame, b: Frame) =>
a.scale === b.scale && a.x === b.x && a.y === b.y
/**
* R-1: a framing that changed while two pointers or more were down is a two-finger pan or
* zoom that worked; the editor visit then never shows the two-finger hint again.
*/
function useTwoFingersWorked(framing: Frame, multi: () => boolean): void {
const last = useRef(framing)
const isMulti = useRef(multi)
isMulti.current = multi
useEffect(() => {
const changed = !sameFrame(last.current, framing)
last.current = framing
if (changed && isMulti.current()) markTwoFingers()
}, [framing])
}
/**
* Everything the canvas box listens to: zoom and pan, the press machine (click, drag,
* tap), and the keys. Keys only count when the box itself is the target: Enter on
@@ -89,8 +64,9 @@ export function useCanvasGestures(input: GestureInput) {
// press is either a pan or a click, never both (R-8).
panning: () => input.tool === 'select' && press.panning(),
twoPointerPan: true,
// R-1: a two-finger pan or zoom that worked; the visit never shows the hint again.
onPinch: markTwoFingers,
})
useTwoFingersWorked(framing.framing, press.multi)
const onKeyDown = (event: KeyboardEvent) => {
if (event.target !== event.currentTarget) return
framing.handlers.onKeyDown(event)
@@ -369,8 +369,6 @@ export function usePressGesture(input: PressInput) {
return {
preview: drag.preview,
finger: drag.finger,
/** Two pointers or more are down: a pan or zoom of the fingers, never a click. */
multi: () => machine.current.press.phase === 'multi',
panning: () => {
const press = machine.current.press
return press.phase === 'drag' && press.action.type === 'pan'