wyuc 191689f98d
feat(renderer/editing): selection + drag-to-move core (@openmaic/renderer v2, #851 Part A) (#859)
* chore(renderer): enable component tests (jsdom + testing-library, tsx include)

* feat(renderer/editing): pure geometry helpers for the editing core

* feat(renderer/editing): alignment snapping math (element + viewport lines)

* feat(renderer/editing): single-element drag math + move intent derivation

* feat(renderer/editing): presentational selection overlay + border handle

* feat(renderer/editing): controlled selection + drag-to-move on EditableSlideCanvas

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer/editing): align interaction overlay with SlideCanvas centering offset

Finding 1: the hit layer and SelectionOverlay sat at el.left*scale from the
overlay wrapper origin, ignoring the viewportStyles.left/top offset SlideCanvas
applies when letterboxing/centering the slide, so pointer-down hit-tested the
wrong element. EditableSlideCanvas now computes the same viewportStyles via the
exported useViewportSize on the overlay wrapper and offsets the hit divs (and a
positioning container wrapping the unchanged SelectionOverlay) to match.

Finding 2: window pointermove/pointerup listeners armed in onElementPointerDown
were removed only on pointer-up, leaking on mid-drag unmount. Track teardown in
a ref and remove it from a useEffect unmount cleanup; single-pointer re-arm kept.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* chore(renderer/editing): typecheck/lint/build green for the editing core

- Apply prettier formatting to editing source/test files flagged by `pnpm check`.
- Replace `any`/`Partial<any>` test fixture helpers with `Partial<PPTElement>`/`Slide`
  typed builders cast via `as unknown as T`, clearing @typescript-eslint/no-explicit-any
  errors in the editing test suite.

No behavior change; typecheck, full editing test suite (18/18) plus package suite (20/20),
prettier, eslint (incl. #853 no-restricted-imports boundary rule), and the ./editing
subpath build are all green.

* fix(renderer/editing): reuse line-aware getElementRange from utils

geometry.ts re-implemented getElementRange/getRectRotatedRange without line
support (start/end), producing NaN alignment guides when a slide contained a
PPTLineElement. Re-export the renderer's line- and rotation-aware
getElementRange from utils/element so snapping.ts/drag.ts are unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer/editing): auto-fit, gate hit layer, defer lines, padding-safe layout

- Restore SlideCanvas auto-fit: when scale is omitted, read fitScale from the
  overlay's own useViewportSize and pass scale through so both measure the same
  box and stay aligned at auto-fit (was hard-forced to scale=1).
- Gate the interactive hit layer on onElementsChange||onSelectionChange so a
  read-only mount does not swallow pointer events.
- Defer line elements: skip type==='line' in the hit layer and SelectionOverlay
  (box-model update intent can't represent line moves); narrow via predicate so
  width/height/rotate are available without in/as fallbacks.
- Move className/style to an outer wrapper with a padding-free inner relative
  wrapper so consumer padding can't diverge SlideCanvas vs overlay box models.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer/editing): Euclidean drag threshold + multi-pointer guard

- Classify click-vs-drag by combined Math.hypot(dx,dy) so a diagonal move past
  the threshold on both axes isn't misread as a click.
- Track the active gesture's pointerId: ignore further pointer-downs while a
  gesture is in flight and drop window move/up events from other pointerIds, so
  a second touch can't overwrite the teardown ref or drag the first element.
- Doc: SnappingOptions.range is in canvas units (viewportSize space), not px.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* style(renderer/editing): prettier formatting for SelectionOverlay line filter

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer/editing): fill container on outer wrapper to restore auto-fit

Round-1 split moved className/style to an outer wrapper that lost its
dimensions, leaving it auto-height; the inner height:100% then resolved
against a zero-height box so useViewportSize read clientHeight~0 ->
fitScale~0 -> blank render when scale is omitted. Give the outer wrapper
width/height:100% merged BEFORE ...style so consumers can still override.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer/editing): select-only fallback and pointercancel teardown

C2: only emit a move intent when onElementsChange exists; a drag-classified
gesture on a read-only (onSelectionChange-only) host now falls back to
selection instead of doing nothing on >2px jitter.

C3: listen for pointercancel (filtered by active pointerId) and tear down
without emitting any intent or selection change, reverting the working copy
so a cancelled gesture leaves the hook ready for a new one. removeListeners
(and thus the unmount cleanup) now also drops the pointercancel listener.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(renderer/editing): narrow lines in computeDragMove without casts

Replace the double as-unknown-as cast (used to read box fields off the
PPTElement union) with a type === 'line' guard: a line reaching
computeDragMove now early-returns its current position unchanged, and the
remaining code is narrowed to the non-line box variants so left/top/width/
height/rotate are directly available.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer/editing): don't allow dragging locked elements

PPTBaseElement.lock was not honored by the editing overlay: a locked
element still rendered an active move hit target and useEditGesture
would still emit element.update after a drag on it. Skip locked
elements when mounting the interactive hit layer, and defense-in-depth
early-return them in onElementPointerDown so no gesture can arm even if
a hit target somehow reached one. SelectionOverlay is left untouched.

* fix(renderer/editing): locked elements block pointer events instead of click-through

A locked element previously had no hit target at all in the interactive
overlay, so a pointer-down over a locked element that visually overlapped
an unlocked element fell through to the unlocked element's hit target
underneath, moving/selecting the wrong element. Render an inert blocker
div for locked elements (same stacking position, no data-element-id, no
gesture wiring) so it consumes the pointer instead. Line elements remain
deliberately click-through (bbox != stroke hit area).

* fix(renderer/editing): route line pointer-downs to an inert stroke blocker

A line was skipped entirely from the hit layer, so a pointer-down on a
rendered line fell through to a box element underneath and moved/selected
the wrong thing. Render an inert stroke-shaped blocker per line instead: a
thin rotated rectangle laid along the segment from the start endpoint
(left+start) to the end endpoint (left+end), with a fixed zoom-independent
grab thickness. It consumes pointer-downs on the stroke (no gesture armed)
while leaving the rest of the line's bbox click-through, so it never
over-blocks other visible elements around a thin diagonal line. Locked
blocker and box move-target behavior are unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer/editing): sync selection on interaction; no live-move on select-only hosts

Keep the controlled selection in step with what the pointer acts on, and
stop faking drags where nothing can commit:

- Select-on-pointer-down: a gesture that starts on an element selects it
  (collapsing to that single element) unless it is already the sole/primary
  selection. Both a click and a drag then operate on — and end with — a
  selected element, so dragging a not-yet-selected element no longer leaves
  the selection stale. Selection is emitted once (on down); pointer-up emits
  only the move intent for a drag, and nothing extra for a click.

- Select-only hosts: when there is no onElementsChange channel, the live
  working copy no longer follows the pointer during pointermove (it could
  never commit and would snap back on release). Such surfaces only select.

Moving all members of a multi-selection together remains out of scope.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer/editing): mirror v1 line renderer with an SVG-path hit blocker

Replace the straight start->end strip blocker with an inert SVG <path> that
reuses getLineElementPath, so the hit region traces the same path the v1
renderer draws for every line shape (straight/broken/broken2/curve/cubic).
pointer-events:stroke covers only the visible stroke, leaving the empty bbox
click-through (P2). stroke-width is the grab band, max(10, el.width*canvasScale)
in screen px (divided by canvasScale for the scaled svg), so a wide or zoomed
stroke stays covered (P3). Blocker stays inert: onPointerDown only stops
propagation, no gesture armed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs(renderer/editing): note deferred line endpoint-marker hit coverage

* fix(renderer): make line elements selectable but not draggable

The line hit blocker in EditableSlideCanvas only called stopPropagation on
pointer-down, which blocked fall-through to an overlapped box but also made
lines unselectable (a regression from the pre-series behavior where the host
delegated line selection via onElementClick). SelectionOverlay additionally
filtered lines out, so even a host-controlled line selection showed no
feedback.

- EditableSlideCanvas: the line blocker's onPointerDown now also selects the
  line via onSelectionChange (on pointer-down, for parity with box elements)
  when a selection callback is present. No drag gesture is armed and no
  element.update is ever emitted (line editing stays deferred). It still
  consumes the pointer to block fall-through even when onSelectionChange is
  absent, and never selects/moves an overlapped box beneath.
- SelectionOverlay: render a selection border for a selected line, computing
  its bounding box via getElementRange and reusing BorderLine (no rotation).
  Box-element rendering is unchanged.

Adds regression tests (line selects on pointer-down with no update intent;
blocker still consumes the pointer without a selection callback; SelectionOverlay
draws a border at the line bbox) and adjusts the tests whose behavior
intentionally changed. Editing suite: 41 passed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer): correct line selection bounds and locked/idempotent line selection

F1: line selection border now encloses the actual drawn path. Add a pure
getLineBounds helper in editing/core/geometry.ts that spans every control
point (start/end/broken/broken2/curve/cubic, each offset by left/top), and use
it in SelectionOverlay instead of getElementRange, which only bounded the
start/end chord (curve/broken/cubic lines got a wrong, sometimes zero-size box).

F2: a locked line is now inert like a locked box — its blocker still stops
propagation (blocks fall-through) but no longer selects.

F3: the line blocker skips re-emitting onSelectionChange when the line is
already the sole primary selection, mirroring the box alreadySolePrimary guard.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer): derive line selection bounds from the rendered path

getLineBounds min/maxed the raw line control points, but the renderer's
getLineElementPath does not draw broken2 as a point — it uses only
broken2[0] (horizontal) or broken2[1] (vertical). A horizontal
broken2=[50,80] line renders flat at y=0, yet the bounds reported
maxY=80, painting an oversized selection border.

Reimplement getLineBounds to parse the coordinates out of
getLineElementPath (the single source of truth for the drawn path) and
min/max over them, offset by left/top. This is exact for straight,
broken, broken2 (one-axis), curve (Q control point in the path) and
cubic (C) lines. Guard an empty/odd match list by falling back to the
element origin.

Also document a deferred limitation in EditableSlideCanvas: line
selection bypasses useEditGesture and thus the activePointerRef
multi-pointer guard (comment only, no behavior change).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer): compute tight Bézier bbox for line selection bounds

getLineBounds took min/max over every number in the rendered path,
which includes Bézier control points. A control point can lie outside
the drawn curve, so the selection border was oversized (e.g.
M0,0 Q50,80 100,0 reported maxY=80 though the quadratic only peaks at
y=40).

Replace it with an exact per-segment path bbox: parse the M/L/Q/C path
(tracking the SVG current point and implicit command repeats) and, for
Q/C, take the true Bézier extrema instead of the control points —
quadratic extremum at t*=(P0-P1)/(P0-2P1+P2), cubic roots of B'(t)=0
(with the degenerate a≈0 linear case). Endpoints and offset by
left/top are preserved; fall back to the element origin for empty
paths. Exposed quadraticBounds/cubicBounds/getPathBounds as pure,
unit-tested helpers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer/editing): parse exponent-form path coordinates in getPathBounds

* fix(renderer/editing): inflate line selection bounds by half the stroke width

* feat(renderer): add pure line-handle drag core for editing v2

Port the app's useDragLineElement math into a store-free, React-free core:
computeLineDrag lifts endpoints/controls to absolute canvas coords, applies
the pointer delta with axis snapping (endpoint straighten, ctrl midpoint snap),
re-normalizes the bbox, and recomputes only the control field(s) the line
actually carries (broken/broken2/curve/cubic). Add LineHandle type and
7 hand-computed tests. External-element adsorption is deferred.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(renderer): draggable line endpoint/control handles for v2 editor

Replace a selected line's approximate bbox selection border with draggable
endpoint/control handles that reshape it. Add a presentational LineHandles
component and a useLineHandleGesture hook (mirroring useEditGesture: working-copy
preview, one intent on pointer-up, pointerId guard, pointercancel revert) driven
by the existing computeLineDrag core. Wire both into EditableSlideCanvas so the
v1 canvas, the line stroke blocker, and the handles preview off the same working
element. Remove the line branch from SelectionOverlay.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer): line selection highlight, precedence, and dead-code cleanup

F1: a selected line now always shows selection chrome. Its inert blocker path
takes a visible accent stroke when selected, so a locked or read-only (no
callback) selected line still gets feedback; the highlight renders even when no
handles do, and stays pointer-inert in a read-only mount.

F2: remove the now-dead getLineBounds and its transitive-only helpers
(getPathBounds, quadraticBounds, cubicBounds, quadraticAt, cubicAt, EPS) plus
their tests; they had no production consumer after the line-bbox removal.

F3: align LineHandles control-field precedence with computeLineDrag
(broken || broken2 || curve) so the handle renders where the drag math reads it.

F4: add tests for the curve ctrl-handle drag and that a handle grab never
re-selects, plus the F1 highlight states.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(renderer): split line hit blocker from highlight; gate handles on editability

The line overlay used a single <path> as both the interaction blocker and the
visual selection chrome. On select its stroke-width switched from the fat grab
band to the thin highlight width, shrinking the pointer-events:stroke hit region
so a thin selected line let clicks fall through to a box beneath.

- FIX B: render two paths sharing the same d/position/scale — a stable
  transparent blocker (grab band, pointer-events:stroke, inert onPointerDown)
  whose hit region never changes with selection, plus a separate accent
  highlight (pointer-events:none) rendered only when selected.
- FIX A: gate LineHandles on onElementsChange (editability) instead of generic
  interactive, so a select-only mount shows the highlight but no draggable
  handles that could never commit.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: 杨慎 <117187635+cosarah@users.noreply.github.com>
2026-07-08 13:03:17 +08:00

113 lines
2.7 KiB
JSON

{
"name": "@openmaic/renderer",
"version": "0.0.2",
"description": "React component for rendering PPTist-style Slide JSON, extracted from OpenMAIC.",
"type": "module",
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./elements": {
"types": "./dist/elements/index.d.ts",
"import": "./dist/elements/index.js"
},
"./types": {
"types": "./dist/types/index.d.ts",
"import": "./dist/types/index.js"
},
"./snapshot": {
"types": "./dist/snapshot/index.d.ts",
"import": "./dist/snapshot/index.js"
},
"./editing": {
"types": "./dist/editing/index.d.ts",
"import": "./dist/editing/index.js"
},
"./fonts.css": "./fonts.css"
},
"files": [
"dist",
"fonts.css",
"README.md",
"DESIGN.md",
"LICENSE",
"FONTS.md",
"font-licenses"
],
"scripts": {
"clean": "rm -rf dist",
"genfonts": "node scripts/generate-fonts-css.mjs",
"build": "node scripts/generate-fonts-css.mjs && rm -rf dist && rollup -c && tsc --emitDeclarationOnly --declarationDir dist",
"dev": "rollup -c -w",
"typecheck": "tsc --noEmit",
"test": "vitest run",
"prepublishOnly": "pnpm run build"
},
"keywords": [
"slide",
"renderer",
"pptist",
"canvas",
"react"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/THU-MAIC/OpenMAIC",
"directory": "packages/@openmaic/renderer"
},
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public"
},
"peerDependencies": {
"echarts": ">=5",
"motion": ">=11",
"react": ">=18",
"react-dom": ">=18",
"shiki": ">=1.0.0",
"tailwindcss": ">=4"
},
"peerDependenciesMeta": {
"echarts": {
"optional": true
},
"shiki": {
"optional": true
}
},
"dependencies": {
"@openmaic/dsl": "workspace:*",
"clsx": "^2.1.1",
"html-to-image": "^1.11.13",
"html2canvas-pro": "^2.0.4",
"katex": "^0.16.33",
"lucide-react": "^0.562.0",
"tailwind-merge": "^3.4.0",
"tinycolor2": "^1.6.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^28.0.1",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-typescript": "^12.1.0",
"@testing-library/dom": "^10.4.1",
"@testing-library/react": "^16.3.2",
"@types/katex": "^0.16.8",
"@types/react": "^19",
"@types/react-dom": "^19",
"@types/tinycolor2": "^1.4.6",
"jsdom": "^29.1.1",
"rollup": "^4.35.0",
"rollup-plugin-preserve-directives": "^0.4.0",
"tslib": "^2.8.0",
"typescript": "^5"
},
"pnpm": {
"onlyBuiltDependencies": []
}
}