[89] Layout-Blatt: Zeichnen wie im Grundriss (Blatt-Ebene statt eigener Annotationen)

Jedes Layout hat eine Blatt-Ebene (DrawingLevel.layoutId, Papier 1:1 in Metern); ein offenes Blatt
macht sie zur aktiven Ebene in derselben PlanView: gleiche Befehle, Fang, Griffe, Vorschau/HUD,
Auswahl, Undo und dasselbe Rendering. Das Blatt liefert nur den Hintergrund (PlanView.backdrop) und
das Platzieren der Viewports. Format 3 migriert Layout.annotations zu drawings2d. Layout-PDF legt die
Blatt-Ebene über planToPrintSvg aufs Papier. Alter Annotations-Pfad gelöscht. e2e layoutSheet.spec.ts.
This commit is contained in:
Claude committed 2026-10-29 20:29:55 +00:00
1 parent c78021473b
commit 3ff123773a
43 files changed
+1245 -2756

No files matched your search

+4 -295
View File
@@ -1,25 +1,14 @@
// Reine, testbare Geometrie für die In-Viewport-Layout-Ansicht (Phase 2b):
// Bildschirm↔Blatt-mm-Transform (Pan/Zoom-to-Cursor/Einpassen), Viewport-
// Reine, testbare Geometrie für die Viewports auf dem Layout-Blatt: Viewport-
// Trefferlogik (Body + 8 Resize-Griffe), Resize-Mathe mit Mindestgrösse,
// Rechteck-Normalisierung (Aufziehen) und Clamp an die Blattkanten.
//
// KEINE React-/DOM-Abhängigkeit — nur Zahlen. Das UI (LayoutSheet.tsx) baut
// darauf auf. Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
import type { LayoutAnnotation, LayoutViewport } from "../model/types";
import type { LayoutViewport } from "../model/types";
/**
* Affine Blatt-Transform: Bildschirm-Pixel = mm · scale + translate.
* `scale` ist px pro mm (immer > 0), `tx/ty` die Blatt-Ursprungslage (mm 0,0)
* in Canvas-Pixeln.
*/
export interface SheetTransform {
/** Pixel pro Millimeter (> 0). */
scale: number;
/** Bildschirm-X des Blatt-Ursprungs (mm x=0) in Canvas-Pixeln. */
tx: number;
/** Bildschirm-Y des Blatt-Ursprungs (mm y=0) in Canvas-Pixeln. */
ty: number;
function clamp(v: number, lo: number, hi: number): number {
return v < lo ? lo : v > hi ? hi : v;
}
/** Ein Punkt in Blatt-Millimetern. */
@@ -48,68 +37,6 @@ export type Handle =
| "w"
| "body";
export const MIN_SCALE = 0.05;
export const MAX_SCALE = 40;
function clamp(v: number, lo: number, hi: number): number {
return v < lo ? lo : v > hi ? hi : v;
}
// ── Transform ────────────────────────────────────────────────────────────────
/** Blatt-mm → Canvas-Pixel. */
export function mmToScreen(t: SheetTransform, p: Mm): { x: number; y: number } {
return { x: p.xMm * t.scale + t.tx, y: p.yMm * t.scale + t.ty };
}
/** Canvas-Pixel → Blatt-mm. */
export function screenToMm(t: SheetTransform, sx: number, sy: number): Mm {
return { xMm: (sx - t.tx) / t.scale, yMm: (sy - t.ty) / t.scale };
}
/**
* Einpass-Transform: Blatt (mm) zentriert in die Canvas-Fläche (px), mit
* `margin` (Anteil, z. B. 0.92 = 8 % Rand). Degenerierte Eingaben → scale=1.
*/
export function fitTransform(
sheetWMm: number,
sheetHMm: number,
canvasWPx: number,
canvasHPx: number,
margin = 0.92,
): SheetTransform {
const sx = (canvasWPx * margin) / sheetWMm;
const sy = (canvasHPx * margin) / sheetHMm;
let scale = Math.min(sx, sy);
if (!Number.isFinite(scale) || scale <= 0) scale = 1;
scale = clamp(scale, MIN_SCALE, MAX_SCALE);
const tx = (canvasWPx - sheetWMm * scale) / 2;
const ty = (canvasHPx - sheetHMm * scale) / 2;
return { scale, tx, ty };
}
/**
* Zoom um den Cursor: der mm-Punkt unter (sx,sy) bleibt fix. `factor` > 1 zoomt
* rein, < 1 raus; der Massstab wird auf [MIN_SCALE, MAX_SCALE] geklemmt.
*/
export function zoomAtCursor(
t: SheetTransform,
factor: number,
sx: number,
sy: number,
): SheetTransform {
const newScale = clamp(t.scale * factor, MIN_SCALE, MAX_SCALE);
// mm-Punkt unter dem Cursor VOR dem Zoom.
const m = screenToMm(t, sx, sy);
// Neue Translation, damit derselbe mm-Punkt wieder unter (sx,sy) liegt.
return { scale: newScale, tx: sx - m.xMm * newScale, ty: sy - m.yMm * newScale };
}
/** Verschiebt die Transform um (dxPx,dyPx) Canvas-Pixel (Pan). */
export function panBy(t: SheetTransform, dxPx: number, dyPx: number): SheetTransform {
return { scale: t.scale, tx: t.tx + dxPx, ty: t.ty + dyPx };
}
// ── Viewport-Rechtecke / Treffer ─────────────────────────────────────────────
/** Rechteck-Sicht eines Viewports (nur die Blattgeometrie). */
@@ -320,221 +247,3 @@ export function handleAnchors(r: RectMm): { handle: Handle; xMm: number; yMm: nu
{ handle: "w", xMm: x, yMm: cy },
];
}
// ── Annotationen (Linie/Rechteck/Text/Bild) ─────────────────────────────────
// Rechteck/Bild teilen sich die Viewport-Griff-Mathe (dieselbe RectMm-Form,
// 8 Ecken/Kanten-Griffe). Linie bekommt zwei Endpunkt-Griffe (wie eine Linie
// im Grundriss). Text bleibt vorerst griff-los (Rahmen kommt beim Aufziehen).
/** Kürzester Abstand (mm) eines Punkts zu einem Liniensegment. */
export function distanceToSegment(
px: number,
py: number,
x1: number,
y1: number,
x2: number,
y2: number,
): number {
const dx = x2 - x1;
const dy = y2 - y1;
const lenSq = dx * dx + dy * dy;
if (lenSq === 0) return Math.hypot(px - x1, py - y1);
const t = clamp(((px - x1) * dx + (py - y1) * dy) / lenSq, 0, 1);
const cx = x1 + t * dx;
const cy = y1 + t * dy;
return Math.hypot(px - cx, py - cy);
}
/**
* Umhüllendes Rechteck (mm) einer Annotation — für Text nur eine GROBE Box
* (kein echtes Font-Measuring im SVG; Breite aus Zeichenzahl geschätzt), reicht
* aber für Trefferlogik + Auswahl-Rahmen. `yMm` einer Text-Annotation ist die
* Baseline, die Box liegt daher oberhalb davon.
*/
export function annotationBoundsMm(ann: LayoutAnnotation): RectMm {
if (ann.kind === "rect" || ann.kind === "image") {
return { xMm: ann.xMm, yMm: ann.yMm, widthMm: ann.widthMm, heightMm: ann.heightMm };
}
if (ann.kind === "line") {
return normalizeRect(ann.x1Mm, ann.y1Mm, ann.x2Mm, ann.y2Mm);
}
// Text: grobe Box aus Zeichenzahl geschätzt (kein echtes Font-Measuring im
// SVG) — bei gesetzter Rahmenbreite (`widthMm`, Rich-Text-Frame) gilt diese
// statt der Schätzung; die Höhe bleibt eine grobe Zeilenschätzung.
const estWidthMm = Math.max(ann.heightMm, ann.text.length * ann.heightMm * 0.6);
const lines = ann.doc ? Math.max(1, ann.doc.paragraphs.length) : 1;
return {
xMm: ann.xMm,
yMm: ann.yMm - ann.heightMm,
widthMm: ann.widthMm ?? estWidthMm,
heightMm: ann.heightMm * 1.25 * lines,
};
}
/**
* Trefferlogik (mm) für EINE Annotation: Linie → Abstand zum Segment ≤ tolMm;
* Rechteck → Rahmen ODER Innenfläche (wiederverwendet {@link handleAt}, jeder
* Nicht-null-Rückgabewert zählt als Treffer); Text → Bounding-Box + Toleranz.
*/
export function annotationHit(
ann: LayoutAnnotation,
xMm: number,
yMm: number,
tolMm: number,
): boolean {
if (ann.kind === "line") {
return distanceToSegment(xMm, yMm, ann.x1Mm, ann.y1Mm, ann.x2Mm, ann.y2Mm) <= tolMm;
}
if (ann.kind === "rect" || ann.kind === "image") {
const r: RectMm = { xMm: ann.xMm, yMm: ann.yMm, widthMm: ann.widthMm, heightMm: ann.heightMm };
return handleAt(r, xMm, yMm, tolMm) !== null;
}
const b = annotationBoundsMm(ann);
return (
xMm >= b.xMm - tolMm &&
xMm <= b.xMm + b.widthMm + tolMm &&
yMm >= b.yMm - tolMm &&
yMm <= b.yMm + b.heightMm + tolMm
);
}
/**
* Topmost-Treffer über eine Annotations-Liste (letzte = oberste, wie
* {@link pickViewport}). Liefert die Id des ERSTEN Treffers von oben, sonst null.
*/
export function pickAnnotation(
annotations: LayoutAnnotation[],
xMm: number,
yMm: number,
tolMm: number,
): string | null {
for (let i = annotations.length - 1; i >= 0; i--) {
const a = annotations[i];
if (annotationHit(a, xMm, yMm, tolMm)) return a.id;
}
return null;
}
// ── Resize-Griffe für Annotationen (wie im Grundriss: Endpunkte/Ecken) ──────
/** Griff einer Linien-Annotation: die beiden Endpunkte oder der Körper. */
export type LineHandle = "p1" | "p2" | "body";
/** Griff-Ergebnis für EINE Annotation — je Art ein anderer Griff-Typ. */
export type AnnotHandleHit =
| { kind: "rect" | "image"; handle: Handle }
| { kind: "line"; handle: LineHandle };
/**
* Griff (oder Körper) einer Linien-Annotation an einem mm-Punkt: die
* Endpunkte haben Vorrang vor dem Segment (wie ein Linien-Griff im Grundriss).
*/
export function lineHandleAt(
x1: number,
y1: number,
x2: number,
y2: number,
xMm: number,
yMm: number,
tolMm: number,
): LineHandle | null {
if (Math.hypot(xMm - x1, yMm - y1) <= tolMm) return "p1";
if (Math.hypot(xMm - x2, yMm - y2) <= tolMm) return "p2";
if (distanceToSegment(xMm, yMm, x1, y1, x2, y2) <= tolMm) return "body";
return null;
}
/**
* Griff (oder Körper) EINER Annotation an einem mm-Punkt — Linie liefert einen
* Endpunkt-Griff, Rechteck/Bild die 8 Viewport-artigen Ecken-/Kanten-Griffe
* (wiederverwendet {@link handleAt}). Text hat (noch) keine Griffe → `null`.
*/
export function annotationHandleAt(
ann: LayoutAnnotation,
xMm: number,
yMm: number,
tolMm: number,
): AnnotHandleHit | null {
if (ann.kind === "line") {
const h = lineHandleAt(ann.x1Mm, ann.y1Mm, ann.x2Mm, ann.y2Mm, xMm, yMm, tolMm);
return h ? { kind: "line", handle: h } : null;
}
if (ann.kind === "rect" || ann.kind === "image") {
const r: RectMm = { xMm: ann.xMm, yMm: ann.yMm, widthMm: ann.widthMm, heightMm: ann.heightMm };
const h = handleAt(r, xMm, yMm, tolMm);
return h ? { kind: ann.kind, handle: h } : null;
}
return null;
}
/**
* Topmost Griff-Treffer über eine Annotations-Liste (wie {@link pickAnnotation},
* aber liefert zusätzlich den konkreten Griff für Resize statt nur Verschieben).
*/
export function pickAnnotationHandle(
annotations: LayoutAnnotation[],
xMm: number,
yMm: number,
tolMm: number,
): { id: string; hit: AnnotHandleHit } | null {
for (let i = annotations.length - 1; i >= 0; i--) {
const a = annotations[i];
const hit = annotationHandleAt(a, xMm, yMm, tolMm);
if (hit) return { id: a.id, hit };
if (annotationHit(a, xMm, yMm, tolMm)) {
// Text (kein Griff-System) oder ein Nicht-Griff-Treffer im Inneren.
return { id: a.id, hit: { kind: "line", handle: "body" } };
}
}
return null;
}
/** Endpunkte einer Linien-Annotation als mm-Punkte (für die Griff-Darstellung). */
export function lineHandleAnchors(ann: Extract<LayoutAnnotation, { kind: "line" }>): Mm[] {
return [
{ xMm: ann.x1Mm, yMm: ann.y1Mm },
{ xMm: ann.x2Mm, yMm: ann.y2Mm },
];
}
/**
* Wendet einen Linien-Griff-Drag an: `p1`/`p2` verschiebt genau diesen
* Endpunkt, `body` das ganze Segment (Delta).
*/
export function applyLineHandleDrag(
start: Extract<LayoutAnnotation, { kind: "line" }>,
handle: LineHandle,
dxMm: number,
dyMm: number,
): { x1Mm: number; y1Mm: number; x2Mm: number; y2Mm: number } {
if (handle === "p1") {
return { x1Mm: start.x1Mm + dxMm, y1Mm: start.y1Mm + dyMm, x2Mm: start.x2Mm, y2Mm: start.y2Mm };
}
if (handle === "p2") {
return { x1Mm: start.x1Mm, y1Mm: start.y1Mm, x2Mm: start.x2Mm + dxMm, y2Mm: start.y2Mm + dyMm };
}
return {
x1Mm: start.x1Mm + dxMm,
y1Mm: start.y1Mm + dyMm,
x2Mm: start.x2Mm + dxMm,
y2Mm: start.y2Mm + dyMm,
};
}
/** Verschiebt eine Annotation um ein mm-Delta — alle ihre Punkte wandern mit. */
export function translateAnnotation(
ann: LayoutAnnotation,
dxMm: number,
dyMm: number,
): LayoutAnnotation {
if (ann.kind === "line") {
return {
...ann,
x1Mm: ann.x1Mm + dxMm,
y1Mm: ann.y1Mm + dyMm,
x2Mm: ann.x2Mm + dxMm,
y2Mm: ann.y2Mm + dyMm,
};
}
return { ...ann, xMm: ann.xMm + dxMm, yMm: ann.yMm + dyMm };
}