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'
+37
View File
@@ -1,6 +1,7 @@
/** U2 Task 8: the two-finger hint (R-1) and the loupe of a finger's drag. */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
act,
cleanup,
fireEvent,
render,
@@ -181,7 +182,43 @@ describe('the two-finger hint (R-1)', () => {
})
})
describe('fix round 1: the two fingers counted in the pinch itself (Minor 2)', () => {
it('a pinch whose framing change lands after the lift still suppresses the hint', async () => {
await mount()
await select()
fireEvent.pointerDown(canvas(), finger(1, 10, 10))
fireEvent.pointerDown(canvas(), finger(2, 60, 10))
// One batch: the framing renders, and its effects run, only after both fingers lifted.
act(() => {
fireEvent.pointerMove(canvas(), finger(2, 110, 10))
fireEvent.pointerUp(canvas(), { ...finger(2, 110, 10), buttons: 0 })
fireEvent.pointerUp(canvas(), { ...finger(1, 10, 10), buttons: 0 })
})
drag(canvas(), [5, 5], [45, 5])
expect(line()).not.toHaveTextContent(HINT)
})
})
describe('the loupe of a finger drag', () => {
it('copies the drawing without its ids: no id appears twice (Minor 1)', async () => {
await mount()
await select()
// The fixture drawing has no id: give it a few, as a real drawing has.
const drawing = screen
.getByTestId('annotation-canvas-drawing')
.querySelector('svg')!
drawing.setAttribute('id', 'drawing-root')
drawing.firstElementChild?.setAttribute('id', 'drawing-part')
const vertex = hitOf('Sommet 2')
fireEvent.pointerDown(vertex, finger(1, 80, 20))
fireEvent.pointerMove(vertex, finger(1, 95, 35))
expect(loupe()).not.toBeNull()
const ids = [...document.querySelectorAll('[id]')].map((el) => el.id)
expect(ids).toContain('drawing-root')
expect(ids.length).toBe(new Set(ids).size)
fireEvent.pointerUp(vertex, { ...finger(1, 95, 35), buttons: 0 })
})
it('shows a picture of the drawing above the finger, then goes on the drop', async () => {
await mount()
await select()