// Layout-Blatt als erstklassige Ansicht im HAUPT-Viewport (Phase 2b) — das Blatt // liegt auf einem papierartigen „Tisch", ist pan-/zoombar (Zoom-to-Cursor) und // die platzierten Viewports lassen sich mit der Maus platzieren/verschieben/ // skalieren/löschen (wie ArchiCAD/Vectorworks). Der Render-Kern (echter Plan je // Viewport, Master-Titelblock) lebt hier zentral (ViewportPlan, TitleBlockSvg // unten) und ersetzt das frühere schwebende Layout-Fenster (Phase 2a). // // Geometrie/Transform/Trefferlogik liegen rein + testbar in layoutSheetMath.ts. // Bezeichner englisch, UI-Text/Kommentare deutsch (CONVENTIONS.md). import { useEffect, useRef, useState } from "react"; import type { PointerEvent as ReactPointerEvent, MouseEvent as ReactMouseEvent, CSSProperties, } from "react"; import type { Layout, LayoutAnnotation, LayoutAnnotationPatch, LayoutOrientation, LayoutPaperFormat, LayoutViewport, Project, } from "../model/types"; import { generatePlan } from "../plan/generatePlan"; import { planToPrintSvg } from "../export/planToPrintSvg"; import { effectiveSheetSizeMm, findMaster, findSnapshot, resolveTitleBlock, resolveViewportScale, visibleCodesFromSnapshot, } from "../panels/layoutModel"; import { annotationBoundsMm, applyHandleDrag, applyLineHandleDrag, clampRectToSheet, fitTransform, handleAnchors, handleCursor, lineHandleAnchors, mmToScreen, normalizeRect, panBy, pickAnnotationHandle, pickViewport, screenToMm, translateAnnotation, vpRect, zoomAtCursor, type AnnotHandleHit, type Handle, type LineHandle, type RectMm, type SheetTransform, } from "./layoutSheetMath"; import { RichTextEditor } from "../text/RichTextEditor"; import { docFromText } from "../text/richText"; import { wrapDocToLines, type SvgLine } from "../text/renderHtml"; import { contentFontBaselineFromTop, TEXT_LINE_HEIGHT_MULT } from "../text/fontMetrics"; import { readImageFile, defaultImageSizeMm } from "../io/imageAsset"; import { rasterizePdfPages } from "../io/pdfImport"; import type { ToolId } from "../tools/types"; import { t } from "../i18n"; // ── Geteilter Render-Kern (auch vom schwebenden LayoutEditor genutzt) ───────── /** * Rendert den ECHTEN Plan eines Viewports: Ausschnitt auflösen → `generatePlan` * → `planToPrintSvg` beim effektiven Viewport-Massstab, Blattgrösse = Viewport- * mm-Grösse. Das SVG (viewBox in mm) wird per 100 % in den Rahmen skaliert * (`overflow:hidden` → Clipping). Neu-Aufbau nur bei relevanten Änderungen * (Projekt/Bindung/Grösse/Massstab), NICHT beim Verschieben (x/y) oder Zoomen. */ export function ViewportPlan({ project, viewport, }: { project: Project; viewport: LayoutViewport; }) { const ref = useRef(null); useEffect(() => { const el = ref.current; if (!el) return; const snap = findSnapshot(project, viewport.snapshotId); if (!snap) { el.replaceChildren(); return; } try { const codes = visibleCodesFromSnapshot(snap); // Massstab ZUERST auflösen: er geht in die Plan-Erzeugung ein, weil // Bemassungs-Feinheiten in Papier-mm angegeben sind (s. model/dimension.ts). const scaleN = resolveViewportScale(viewport, snap); const plan = generatePlan( project, snap.activeLevelId, codes, undefined, snap.detail, false, false, true, scaleN, ); const print = planToPrintSvg(plan, { scaleDenominator: scaleN, pageWidthMm: viewport.widthMm, pageHeightMm: viewport.heightMm, }); const svg = print.svg; svg.setAttribute("width", "100%"); svg.setAttribute("height", "100%"); svg.style.display = "block"; el.replaceChildren(svg); } catch { el.replaceChildren(); } }, [ project, viewport.snapshotId, viewport.widthMm, viewport.heightMm, viewport.scaleDenominator, ]); return
; } /** * Schlichtes Schriftfeld unten rechts (mm-Koordinaten des Blatt-viewBox) mit den * aufgelösten Master-Werten — dieselbe Anmutung wie der PDF-Titelblock. */ export function TitleBlockSvg({ sheet, tb, paper, orientation, }: { sheet: { w: number; h: number }; tb: { projectName: string; sheetName: string; scale: string; date: string; author: string }; paper: LayoutPaperFormat; orientation: LayoutOrientation; }) { const boxW = 70; const boxH = 30; const pad = 6; const x = sheet.w - boxW - pad; const y = sheet.h - boxH - pad; const fmt = `${paper.toUpperCase()} ${orientation === "landscape" ? "quer" : "hoch"}`; return ( {clip(tb.projectName, 34)} {clip(tb.sheetName, 36)} {tb.author && ( {clip(tb.author, 40)} )} {tb.scale} {tb.date} · {fmt} ); } function clip(s: string, max: number): string { return s.length > max ? s.slice(0, max - 1) + "…" : s; } // ── In-Viewport-Layout-Ansicht ──────────────────────────────────────────────── export interface LayoutSheetHandlers { /** Zurück zur Modell-Ansicht (Grundriss/3D). */ onBack: () => void; /** Ändert Position/Grösse/Massstab eines Viewports (mm). */ onPatchViewport: ( viewportId: string, patch: Partial>, ) => void; /** Entfernt einen Viewport per Id. */ onRemoveViewport: (viewportId: string) => void; /** Fügt einen Viewport (gebunden an `snapshotId`) mit gegebener Geometrie an. */ onAddViewport: ( snapshotId: string, init: Partial>, ) => void; /** * Fügt eine neue Annotation (Linie/Rechteck/Text/Bild) mit gegebener * Geometrie an — liefert die neue Id zurück (z. B. um nach dem Aufziehen * eines Text-Rahmens sofort inline hineinzuschreiben, s. `inlineEdit`). */ onAddAnnotation: (kind: LayoutAnnotation["kind"], init: LayoutAnnotationPatch) => string; /** * Ändert Felder einer Annotation (Position/Farbe/Strichstärke/Text). * `coalesceKey`: s. `projectSlice.ts` `setProject` — vom Inline-Text-Editor * genutzt, damit EIN Tipp-Vorgang EINEN Undo-Schritt erzeugt statt bei * jedem Tastendruck einen neuen zu pushen (s. `inlineEdit`-Block unten). */ onPatchAnnotation: ( annotationId: string, patch: LayoutAnnotationPatch, coalesceKey?: string, ) => void; /** Entfernt eine Annotation per Id. */ onRemoveAnnotation: (annotationId: string) => void; } /** Fangzone der Resize-Griffe in Bildschirm-Pixeln (→ mm über den Massstab). */ const HANDLE_TOL_PX = 7; /** Mindest-Kantenlänge eines Viewports (mm). */ const MIN_VP_MM = 8; /** Mindest-Kantenlänge eines aufgezogenen Platzierungs-Rechtecks (mm). */ const MIN_PLACE_MM = 6; /** Mindestgrösse einer neu gezeichneten Linie/eines Rechtecks (mm) — sonst verworfen. */ const MIN_ANNOT_MM = 3; /** Default-Zeilenhöhe eines neuen Text-Markups (mm). */ const DEFAULT_ANNOT_TEXT_MM = 3.5; /** * CSS-Pixel pro Millimeter bei „echten" 100 % — der CSS-Referenzpixel ist per * Spezifikation auf 1/96 Zoll fixiert (96 px = 25.4 mm), UNABHÄNGIG von der * physischen Bildschirmauflösung (`devicePixelRatio` gleicht das pro Gerät * wieder aus). `SheetTransform.scale` ist „Pixel pro Millimeter" — bei diesem * Wert misst ein A4-Blatt (210 mm) auf dem Bildschirm also wirklich 21 cm * (Nutzer-Wunsch), nicht nur „passt ins Fenster" wie bei „Einpassen". */ const PX_PER_MM_AT_100 = 96 / 25.4; /** Zeichenwerkzeug für die Annotations-Schicht (zusätzlich zum Viewport-„placing"). */ type AnnotTool = "select" | "line" | "rect" | "text" | "image"; /** * Bildet das GLOBALE Werkzeug (linkes Werkzeugpanel, `ToolId`) auf ein * `AnnotTool` ab — das Blatt hat keine eigene Werkzeugleiste mehr (Nutzer- * Feedback: „ein Layout soll daherkommen wie eine normale Zeichnung, nur dass * ein Blatt drin liegt" — dieselben Zeichenwerkzeuge links wie im Grundriss, * nichts Eigenes nur fürs Layout). `textbox` zeichnet hier wie `text` (kein * eigener Spaltenbreite-Ablauf im Blatt); alle übrigen Werkzeuge (Wand, Fenster, * Kreis, …) ergeben auf dem Blatt keinen Sinn → „select". */ function annotToolFromGlobal(tool: ToolId): AnnotTool { if (tool === "line" || tool === "rect" || tool === "image") return tool; if (tool === "text" || tool === "textbox") return "text"; return "select"; } type Drag = | { mode: "pan"; lastX: number; lastY: number } | { mode: "move"; id: string; start: RectMm; grabX: number; grabY: number } | { mode: "resize"; id: string; handle: Handle; start: RectMm; grabX: number; grabY: number } | { mode: "place"; x0: number; y0: number } | { mode: "annot-move"; id: string; start: LayoutAnnotation; grabX: number; grabY: number } | { mode: "annot-resize"; id: string; hit: AnnotHandleHit; start: LayoutAnnotation; grabX: number; grabY: number; } | null; /** Cursor für einen Linien-Endpunkt-Griff (kein direktes `Handle`-Äquivalent). */ function lineHandleCursor(handle: LineHandle): string { return handle === "body" ? "move" : "crosshair"; } /** Annotation abzüglich `id`/`kind` als Patch — für den Commit nach Move/Draw. */ function annotationPatch(a: LayoutAnnotation): LayoutAnnotationPatch { const { id: _id, kind: _kind, ...rest } = a as LayoutAnnotation & Record; return rest as LayoutAnnotationPatch; } export function LayoutSheetView({ project, layout, handlers, activeTool, onToolChange, selectedAnnotId, onSelectAnnot, }: { project: Project; layout: Layout; handlers: LayoutSheetHandlers; /** Globales Werkzeug (linkes Werkzeugpanel) — s. `annotToolFromGlobal`. */ activeTool: ToolId; /** Setzt das globale Werkzeug (z. B. zurück auf „select" nach einer Platzierung). */ onToolChange: (id: ToolId) => void; /** * Gewählte Annotation — von App.tsx gehoben (statt lokalem State), damit die * echte Attribute-Palette (rechtes Panel) dieselbe Auswahl sieht wie ein * Drawing2D im Grundriss (Nutzer-Feedback: „Attribute genau wie bei * normalen Linien beim Zeichnen"). */ selectedAnnotId: string | null; onSelectAnnot: (id: string | null) => void; }) { // Alias, damit die bestehenden `setSelectedAnnotId(...)`-Aufrufe unten // unverändert bleiben (jetzt ein gehobener Prop-Setter statt lokalem State). const setSelectedAnnotId = onSelectAnnot; const stageRef = useRef(null); const [canvas, setCanvas] = useState({ w: 800, h: 600 }); const [transform, setTransform] = useState(null); const [selectedId, setSelectedId] = useState(null); const [hoverCursor, setHoverCursor] = useState("default"); // Werkzeug-Modus „Viewport aufziehen" (armiert). Danach folgt der Ausschnitt-Picker. const [placing, setPlacing] = useState(false); // Live-Geometrie während eines Move/Resize/Place-Drags (Ghost), sonst null. const [previewRect, setPreviewRect] = useState(null); // Nach dem Aufziehen: das fertige Rechteck (mm) wartet auf die Ausschnitt-Bindung. const [pendingRect, setPendingRect] = useState(null); const [bindSnapId, setBindSnapId] = useState(""); const dragRef = useRef(null); const spaceDownRef = useRef(false); // ── Annotationen (Linie/Rechteck/Text) — eigenständige Zusatzschicht ──────── // Werkzeug „select" = normale Auswahl/Verschieben von Viewports+Annotationen; // line/rect/text armieren das jeweilige Zeichnen (analog `placing` für Viewports). // KEIN eigener Werkzeug-Zustand mehr — armiert wird über das globale Werkzeug // (linkes Panel), s. `annotToolFromGlobal`. const annotTool = annotToolFromGlobal(activeTool); // Ghost-Geometrie: beim Aufziehen einer NEUEN Linie/eines Rechtecks (id="draft") // ODER beim Verschieben einer bestehenden Annotation (id = deren echte Id). const [annotPreview, setAnnotPreview] = useState(null); // Textwerkzeug: Rahmen aufziehen (Ghost, wie ein Rechteck) — direkt danach // öffnet `inlineEdit` den Editor IM Rahmen (kein eigenes Fenster). const [textFrameDraft, setTextFrameDraft] = useState(null); // Erster Klick von Linie/Rechteck/Text: KEIN Ziehen nötig (Nutzer-Feedback — // „alle 2D-Werkzeuge sollen sich gleich verhalten wie im Grundriss"), also // Klick-Bewegen-Klick wie die Plan-Zeichenwerkzeuge (tools.ts `lineTool`/ // `rectTool`): erster Klick setzt diesen Punkt, die Maus bewegt den Ghost // (`onPointerMove` läuft auch OHNE gehaltene Taste), der zweite Klick // schliesst ab. `null` = kein Werkzeug wartet auf den zweiten Klick. const [pendingPoint, setPendingPoint] = useState< { tool: "line" | "rect" | "text"; xMm: number; yMm: number } | null >(null); // Wechselt der Nutzer das Werkzeug, während ein erster Klick offen ist (z. B. // Escape/anderes Werkzeug im linken Panel), verwirft das den offenen Entwurf. useEffect(() => { setPendingPoint(null); setAnnotPreview((p) => (p?.id === "draft" ? null : p)); setTextFrameDraft(null); }, [annotTool]); // Aktuell inline bearbeitete Text-Annotation (neu erstellt ODER per HUD „✎" // geöffnet) — `minHeightMm` ist die beim Aufziehen gewählte Rahmenhöhe (rein // fürs Editor-Startmass, nicht Teil des Modells — Text wächst mit dem Inhalt). const [inlineEdit, setInlineEdit] = useState<{ id: string; minHeightMm: number } | null>(null); // Bild-Werkzeug: nach dem Klick wartet die Position auf die Datei-Auswahl // (async, s. imageInputRef weiter unten). const [imagePendingAt, setImagePendingAt] = useState<{ xMm: number; yMm: number } | null>(null); const imageInputRef = useRef(null); const master = findMaster(project, layout); // Effektive Grösse respektiert freie Grösse (custom Width/Height) — bindet // das Layout an ein Masterlayout, gewinnt dessen Grösse (wie im PDF-Export, // `folderPdfPages`: `effectiveSheetSizeMm(master ?? layout)`). const sheet = effectiveSheetSizeMm(master ?? layout); const titleBlock = resolveTitleBlock(project, layout, master); const drawBorder = master ? master.border ?? true : false; const snapshots = project.viewSnapshots ?? []; const selectedVp = layout.viewports.find((v) => v.id === selectedId) ?? null; const annotations = layout.annotations ?? []; const selectedAnnot = annotations.find((a) => a.id === selectedAnnotId) ?? null; // Verfügbare Fläche messen. useEffect(() => { const el = stageRef.current; if (!el) return; const ro = new ResizeObserver((entries) => { for (const e of entries) { setCanvas({ w: e.contentRect.width, h: e.contentRect.height }); } }); ro.observe(el); return () => ro.disconnect(); }, []); // Erst-Einpassung, sobald die Fläche gemessen ist (einmal). Bei Blattwechsel // (Papier/Ausrichtung) neu einpassen. useEffect(() => { if (canvas.w <= 0 || canvas.h <= 0) return; setTransform((cur) => cur ?? fitTransform(sheet.w, sheet.h, canvas.w, canvas.h), ); // eslint-disable-next-line react-hooks/exhaustive-deps }, [canvas.w, canvas.h]); useEffect(() => { if (canvas.w <= 0 || canvas.h <= 0) return; setTransform(fitTransform(sheet.w, sheet.h, canvas.w, canvas.h)); // eslint-disable-next-line react-hooks/exhaustive-deps }, [sheet.w, sheet.h]); const fit = () => setTransform(fitTransform(sheet.w, sheet.h, canvas.w, canvas.h)); const zoomStep = (factor: number) => setTransform((t) => (t ? zoomAtCursor(t, factor, canvas.w / 2, canvas.h / 2) : t)); // Springt auf echte 1:1-Grösse (s. PX_PER_MM_AT_100), zentriert auf die // Canvas-Mitte statt den Cursor (kein aktives Zoom-Gesten-Event hier). const zoomTo100 = () => setTransform((t) => t ? zoomAtCursor(t, PX_PER_MM_AT_100 / t.scale, canvas.w / 2, canvas.h / 2) : t, ); const zoomPercent = Math.round(((transform?.scale ?? 0) / PX_PER_MM_AT_100) * 100); // Leertaste = Pan-Modifikator (wie in CAD). Delete/Backspace löscht die Auswahl. useEffect(() => { const down = (e: KeyboardEvent) => { const el = e.target as HTMLElement | null; const typing = el && (el.tagName === "INPUT" || el.tagName === "TEXTAREA" || el.isContentEditable); if (typing) return; if (e.code === "Space") { spaceDownRef.current = true; e.preventDefault(); // kein Button-Reklick / Seiten-Scroll return; } if (e.key === "Escape") { if (placing) { setPlacing(false); setPreviewRect(null); } else if (annotTool !== "select") { onToolChange("select"); setAnnotPreview(null); setTextFrameDraft(null); } else if (pendingRect) { setPendingRect(null); } else if (selectedAnnotId) { setSelectedAnnotId(null); } else { setSelectedId(null); } } else if ((e.key === "Delete" || e.key === "Backspace") && selectedId) { handlers.onRemoveViewport(selectedId); setSelectedId(null); e.preventDefault(); } else if ((e.key === "Delete" || e.key === "Backspace") && selectedAnnotId) { handlers.onRemoveAnnotation(selectedAnnotId); setSelectedAnnotId(null); e.preventDefault(); } }; const up = (e: KeyboardEvent) => { if (e.code === "Space") spaceDownRef.current = false; }; window.addEventListener("keydown", down); window.addEventListener("keyup", up); return () => { window.removeEventListener("keydown", down); window.removeEventListener("keyup", up); }; }, [ placing, pendingRect, selectedId, selectedAnnotId, annotTool, handlers, onToolChange, ]); // Bildschirm-Pixel (canvas-lokal) eines Pointer-Events. const localXY = (e: { clientX: number; clientY: number }) => { const rect = stageRef.current?.getBoundingClientRect(); return { x: e.clientX - (rect?.left ?? 0), y: e.clientY - (rect?.top ?? 0) }; }; // Zoom-to-Cursor am Rad: nativer, NICHT-passiver Listener (preventDefault muss // greifen — React-onWheel ist passiv). Funktionales setTransform, damit der // Listener nur einmal registriert werden muss. useEffect(() => { const el = stageRef.current; if (!el) return; const onWheel = (e: WheelEvent) => { e.preventDefault(); const rect = el.getBoundingClientRect(); const x = e.clientX - rect.left; const y = e.clientY - rect.top; const factor = e.deltaY < 0 ? 1.12 : 1 / 1.12; setTransform((t) => (t ? zoomAtCursor(t, factor, x, y) : t)); }; el.addEventListener("wheel", onWheel, { passive: false }); return () => el.removeEventListener("wheel", onWheel); }, []); // Schliesst Linie/Rechteck/Text-Rahmen beim ZWEITEN Klick ab (erster Punkt = // `pendingPoint`, zweiter = `mm`) — dieselbe Zwei-Klick-Logik, die vorher am // `pointerup` einer gehaltenen Ziehgeste hing (s. Git-Historie), jetzt ohne // gehaltene Taste nötig. const commitTwoPointAnnot = ( tool: "line" | "rect" | "text", p0: { xMm: number; yMm: number }, p1: { xMm: number; yMm: number }, ) => { if (tool === "line") { const len = Math.hypot(p1.xMm - p0.xMm, p1.yMm - p0.yMm); if (len >= MIN_ANNOT_MM) { handlers.onAddAnnotation("line", { x1Mm: round1(p0.xMm), y1Mm: round1(p0.yMm), x2Mm: round1(p1.xMm), y2Mm: round1(p1.yMm), }); } return; } const r = normalizeRect(p0.xMm, p0.yMm, p1.xMm, p1.yMm); if (tool === "rect") { if (r.widthMm >= MIN_ANNOT_MM && r.heightMm >= MIN_ANNOT_MM) { const clamped = clampRectToSheet(r, sheet.w, sheet.h); handlers.onAddAnnotation("rect", { xMm: round1(clamped.xMm), yMm: round1(clamped.yMm), widthMm: round1(clamped.widthMm), heightMm: round1(clamped.heightMm), }); } return; } // Text: zu klein aufgezogen (praktisch ein Doppelklick auf der Stelle) → // Default-Rahmen an der Klickstelle, statt eine Mini-Box zu erzwingen. const tooSmall = r.widthMm < MIN_ANNOT_MM || r.heightMm < MIN_ANNOT_MM; const widthMm = tooSmall ? 60 : r.widthMm; const frameHeightMm = tooSmall ? 20 : r.heightMm; const clamped = clampRectToSheet({ xMm: r.xMm, yMm: r.yMm, widthMm, heightMm: frameHeightMm }, sheet.w, sheet.h); // `yMm` einer Text-Annotation ist die BASELINE der ersten Zeile (s. // `annotationBoundsMm`) — der aufgezogene Rahmen liefert die OBERE Kante, // daher + eine Zeilenhöhe (heightMm = Schriftgrösse). const id = handlers.onAddAnnotation("text", { xMm: round1(clamped.xMm), yMm: round1(clamped.yMm + DEFAULT_ANNOT_TEXT_MM), widthMm: round1(clamped.widthMm), heightMm: DEFAULT_ANNOT_TEXT_MM, text: "", }); setSelectedAnnotId(id); setInlineEdit({ id, minHeightMm: clamped.heightMm }); }; const onPointerDown = (e: ReactPointerEvent) => { if (!transform) return; // Klicks aus dem HUD/Popover (echte Bedienelemente) NICHT als Blatt-Geste werten. if ((e.target as HTMLElement).closest("[data-sheet-ui]")) return; const { x, y } = localXY(e); (e.currentTarget as HTMLElement).setPointerCapture(e.pointerId); // Pan: mittlere Maustaste oder Leertaste-Drag. if (e.button === 1 || spaceDownRef.current) { dragRef.current = { mode: "pan", lastX: x, lastY: y }; return; } if (e.button !== 0) return; const mm = screenToMm(transform, x, y); // Platzierungs-Modus: Rechteck aufziehen. if (placing) { dragRef.current = { mode: "place", x0: mm.xMm, y0: mm.yMm }; setPreviewRect({ xMm: mm.xMm, yMm: mm.yMm, widthMm: 0, heightMm: 0 }); return; } // Annotations-Werkzeuge Linie/Rechteck/Text: Klick-Bewegen-Klick wie im // Grundriss (kein Ziehen nötig) — erster Klick merkt sich den Punkt // (`pendingPoint`), der zweite (hier) schliesst ab. Bild bleibt ein // Einzelklick, der sofort den Datei-Dialog öffnet (async, s. imageInputRef). if (annotTool === "image") { setImagePendingAt({ xMm: mm.xMm, yMm: mm.yMm }); imageInputRef.current?.click(); return; } if (annotTool === "line" || annotTool === "rect" || annotTool === "text") { if (pendingPoint && pendingPoint.tool === annotTool) { commitTwoPointAnnot(annotTool, pendingPoint, mm); setPendingPoint(null); setAnnotPreview(null); setTextFrameDraft(null); // Linie/Rechteck bleiben armiert für die nächste (Plan-Konvention: // tools.ts `lineTool`/`rectTool` bleiben nach dem Commit ebenfalls // aktiv statt auf „Auswahl" zu springen — mehrere Linien am Stück ohne // erneuten Werkzeug-Klick). Text springt zurück auf „Auswahl": direkt // danach öffnet der Inline-Editor, ein weiterer Klick soll den Cursor // darin setzen, keinen neuen Rahmen aufziehen. if (annotTool === "text") onToolChange("select"); return; } setPendingPoint({ tool: annotTool, xMm: mm.xMm, yMm: mm.yMm }); if (annotTool === "line") { setAnnotPreview({ id: "draft", kind: "line", x1Mm: mm.xMm, y1Mm: mm.yMm, x2Mm: mm.xMm, y2Mm: mm.yMm }); } else if (annotTool === "rect") { setAnnotPreview({ id: "draft", kind: "rect", xMm: mm.xMm, yMm: mm.yMm, widthMm: 0, heightMm: 0 }); } else { setTextFrameDraft({ xMm: mm.xMm, yMm: mm.yMm, widthMm: 0, heightMm: 0 }); } return; } const tolMm = HANDLE_TOL_PX / transform.scale; const hit = pickViewport(layout.viewports, mm.xMm, mm.yMm, tolMm); if (hit) { setSelectedId(hit.id); setSelectedAnnotId(null); const vp = layout.viewports.find((v) => v.id === hit.id)!; if (hit.handle === "body") { dragRef.current = { mode: "move", id: hit.id, start: vpRect(vp), grabX: mm.xMm, grabY: mm.yMm, }; } else { dragRef.current = { mode: "resize", id: hit.id, handle: hit.handle, start: vpRect(vp), grabX: mm.xMm, grabY: mm.yMm, }; } return; } const annotHit = pickAnnotationHandle(annotations, mm.xMm, mm.yMm, tolMm); if (annotHit) { setSelectedId(null); setSelectedAnnotId(annotHit.id); const ann = annotations.find((a) => a.id === annotHit.id)!; if (annotHit.hit.handle === "body") { dragRef.current = { mode: "annot-move", id: annotHit.id, start: ann, grabX: mm.xMm, grabY: mm.yMm, }; } else { dragRef.current = { mode: "annot-resize", id: annotHit.id, hit: annotHit.hit, start: ann, grabX: mm.xMm, grabY: mm.yMm, }; } } else { setSelectedId(null); setSelectedAnnotId(null); } }; // Doppelklick auf ein bestehendes Textfeld öffnet die Bearbeitung wieder — // wie InDesign/Affinity (bisher nur über den „✎"-Knopf im HUD erreichbar). const onDoubleClick = (e: ReactMouseEvent) => { if (!transform || annotTool !== "select") return; if ((e.target as HTMLElement).closest("[data-sheet-ui]")) return; const { x, y } = localXY(e); const mm = screenToMm(transform, x, y); const tolMm = HANDLE_TOL_PX / transform.scale; const hit = pickAnnotationHandle(annotations, mm.xMm, mm.yMm, tolMm); if (!hit) return; const ann = annotations.find((a) => a.id === hit.id); if (!ann || ann.kind !== "text") return; setSelectedId(null); setSelectedAnnotId(ann.id); setInlineEdit({ id: ann.id, minHeightMm: annotationBoundsMm(ann).heightMm }); }; const onPointerMove = (e: ReactPointerEvent) => { if (!transform) return; const { x, y } = localXY(e); const drag = dragRef.current; if (!drag) { // Wartet Linie/Rechteck/Text auf den zweiten Klick (`pendingPoint`), folgt // der Ghost dem Cursor auch OHNE gehaltene Taste (Klick-Bewegen-Klick wie // im Grundriss) — kein Hover-Cursor-Update nötig, das Werkzeug ist eh aktiv. if (pendingPoint) { const mm = screenToMm(transform, x, y); if (pendingPoint.tool === "line") { setAnnotPreview({ id: "draft", kind: "line", x1Mm: pendingPoint.xMm, y1Mm: pendingPoint.yMm, x2Mm: mm.xMm, y2Mm: mm.yMm, }); } else if (pendingPoint.tool === "rect") { const r = normalizeRect(pendingPoint.xMm, pendingPoint.yMm, mm.xMm, mm.yMm); setAnnotPreview({ id: "draft", kind: "rect", ...r }); } else { setTextFrameDraft(normalizeRect(pendingPoint.xMm, pendingPoint.yMm, mm.xMm, mm.yMm)); } return; } // Nur Hover-Cursor aktualisieren (Griffe/Körper), wenn nicht am Ziehen. const mm = screenToMm(transform, x, y); const tolMm = HANDLE_TOL_PX / transform.scale; const hit = pickViewport(layout.viewports, mm.xMm, mm.yMm, tolMm); const annotHit = !hit ? pickAnnotationHandle(annotations, mm.xMm, mm.yMm, tolMm) : null; setHoverCursor( placing || annotTool !== "select" ? "crosshair" : hit ? handleCursor(hit.handle) : annotHit ? annotHit.hit.kind === "line" ? lineHandleCursor(annotHit.hit.handle) : handleCursor(annotHit.hit.handle) : "default", ); return; } if (drag.mode === "pan") { setTransform((t) => (t ? panBy(t, x - drag.lastX, y - drag.lastY) : t)); dragRef.current = { ...drag, lastX: x, lastY: y }; return; } const mm = screenToMm(transform, x, y); if (drag.mode === "place") { setPreviewRect(normalizeRect(drag.x0, drag.y0, mm.xMm, mm.yMm)); return; } if (drag.mode === "move") { const dx = mm.xMm - drag.grabX; const dy = mm.yMm - drag.grabY; const moved = applyHandleDrag(drag.start, "body", dx, dy, MIN_VP_MM); setPreviewRect(clampRectToSheet(moved, sheet.w, sheet.h)); return; } if (drag.mode === "resize") { const dx = mm.xMm - drag.grabX; const dy = mm.yMm - drag.grabY; setPreviewRect(applyHandleDrag(drag.start, drag.handle, dx, dy, MIN_VP_MM)); return; } if (drag.mode === "annot-move") { const dx = mm.xMm - drag.grabX; const dy = mm.yMm - drag.grabY; setAnnotPreview(translateAnnotation(drag.start, dx, dy)); return; } if (drag.mode === "annot-resize") { const dx = mm.xMm - drag.grabX; const dy = mm.yMm - drag.grabY; if (drag.hit.kind === "line") { if (drag.start.kind === "line") { setAnnotPreview({ ...drag.start, ...applyLineHandleDrag(drag.start, drag.hit.handle, dx, dy) }); } } else if (drag.start.kind === "rect" || drag.start.kind === "image") { const r = applyHandleDrag( { xMm: drag.start.xMm, yMm: drag.start.yMm, widthMm: drag.start.widthMm, heightMm: drag.start.heightMm }, drag.hit.handle, dx, dy, MIN_ANNOT_MM, ); setAnnotPreview({ ...drag.start, ...r }); } return; } }; const endDrag = (e: ReactPointerEvent) => { const el = e.currentTarget as HTMLElement; if (el.hasPointerCapture(e.pointerId)) el.releasePointerCapture(e.pointerId); const drag = dragRef.current; dragRef.current = null; if (!drag) return; if (drag.mode === "place") { const r = previewRect; setPreviewRect(null); setPlacing(false); if (r && r.widthMm >= MIN_PLACE_MM && r.heightMm >= MIN_PLACE_MM) { const clamped = clampRectToSheet(r, sheet.w, sheet.h); if (snapshots.length === 1) { // Nur ein Ausschnitt → direkt binden. handlers.onAddViewport(snapshots[0].id, clamped); } else { setPendingRect(clamped); setBindSnapId(""); } } return; } if ((drag.mode === "move" || drag.mode === "resize") && previewRect) { const r = clampRectToSheet(previewRect, sheet.w, sheet.h); handlers.onPatchViewport(drag.id, { xMm: round1(r.xMm), yMm: round1(r.yMm), widthMm: round1(Math.max(MIN_VP_MM, r.widthMm)), heightMm: round1(Math.max(MIN_VP_MM, r.heightMm)), }); } setPreviewRect(null); if (drag.mode === "annot-move" || drag.mode === "annot-resize") { if (annotPreview) { handlers.onPatchAnnotation(drag.id, annotationPatch(annotPreview)); } setAnnotPreview(null); return; } }; const confirmBind = () => { if (bindSnapId && pendingRect) { handlers.onAddViewport(bindSnapId, pendingRect); } setPendingRect(null); setBindSnapId(""); }; // Blatt-Position (canvas-lokale Pixel) + Grösse. const tf = transform ?? fitTransform(sheet.w, sheet.h, canvas.w, canvas.h); const paperLeft = tf.tx; const paperTop = tf.ty; const paperW = sheet.w * tf.scale; const paperH = sheet.h * tf.scale; // Effektives Rechteck eines Viewports (Ghost während seines Drags). const displayRect = (vp: LayoutViewport): RectMm => previewRect && (dragRef.current?.mode === "move" || dragRef.current?.mode === "resize") && dragRef.current?.id === vp.id ? previewRect : vpRect(vp); return (
{/* Werkzeugleiste (Blatt-Navigation + Platzieren + Zurück). */}
{layout.name} {/* Zeigt den echten Massstab (100 % = 1:1 auf dem Bildschirm, s. PX_PER_MM_AT_100) und setzt ihn per Klick — wie die %-Anzeige in Affinity/Figma, nicht nur „Einpassen" (das passt bloss ins Fenster). */}
{/* Blatt-Tisch: papierartiger Hintergrund, weisses Blatt zentriert. */}
{/* Weisses Papier. */}
{/* Master: Rahmen + Titelblock (read-only). */} {drawBorder && ( )} {master && ( )} {/* Platzierte Viewports (echter Plan, geklippt). */} {layout.viewports.map((vp) => { const r = displayRect(vp); const snap = findSnapshot(project, vp.snapshotId); const selected = vp.id === selectedId; return (
{snap ? ( ) : (
{t("layouts.missingSnapshot")}
)}
); })} {/* Annotationen (Linie/Rechteck/Text) — eigene Schicht ÜBER den Viewports, in mm über dieselbe viewBox wie Rahmen/Titelblock. Während des Zeichnens/Verschiebens ersetzt `annotPreview` die betroffene Annotation (Ghost); ein neu gezeichnetes Element (id="draft") ist noch nicht in `layout.annotations` und wird zusätzlich gerendert. */} {annotations.map((ann) => { // Während des Inline-Editors NICHT zusätzlich statisch zeichnen // (nur bei Text-Annotationen möglich, s. `inlineEdit`-Block // unten) — sonst blieben Reste des alten Umbruchs sichtbar, // weil das SVG nach geschätzter Zeichenbreite umbricht, der // Editor-Browser aber nach echter Font-Metrik (leicht andere // Zeilenumbrüche, Editor-Weiss deckt nicht jede Abweichung). if (ann.id === inlineEdit?.id) return null; const a = annotPreview && annotPreview.id === ann.id ? annotPreview : ann; return ; })} {annotPreview && annotPreview.id === "draft" && ( )} {/* Gestrichelter Auswahl-Rahmen um die gewählte Annotation. */} {selectedAnnot && (() => { const display = annotPreview && annotPreview.id === selectedAnnot.id ? annotPreview : selectedAnnot; const b = annotationBoundsMm(display); return ( ); })()} {/* Auswahl-Griffe des gewählten Viewports. */} {selectedVp && handleAnchors(displayRect(selectedVp)).map((a) => (
))} {/* Auswahl-Griffe der gewählten Annotation — Rechteck/Bild wie ein Viewport (8 Ecken/Kanten), Linie mit ihren zwei Endpunkten (wie im Grundriss). Text hat (noch) keine Griffe. */} {selectedAnnot && (() => { const display = annotPreview && annotPreview.id === selectedAnnot.id ? annotPreview : selectedAnnot; if (display.kind === "rect" || display.kind === "image") { return handleAnchors({ xMm: display.xMm, yMm: display.yMm, widthMm: display.widthMm, heightMm: display.heightMm, }).map((a) => (
)); } if (display.kind === "line") { return lineHandleAnchors(display).map((p, i) => (
)); } return null; })()} {/* Ghost-Rechteck beim Aufziehen (Platzieren). */} {previewRect && dragRef.current?.mode === "place" && (
)} {/* Ghost-Rahmen beim Aufziehen eines neuen Textfelds. */} {textFrameDraft && (
)}
{/* HUD: Eigenschaften des gewählten Viewports (Massstab/Grösse/Löschen). */} {selectedVp && (
{findSnapshot(project, selectedVp.snapshotId)?.name ?? t("layouts.missingSnapshot")}
{Math.round(selectedVp.widthMm)} × {Math.round(selectedVp.heightMm)} mm
{selectedVp.scaleDenominator !== undefined && ( )}
)} {/* HUD: Eigenschaften der gewählten Annotation (Farbe/Strichstärke/Text/ Bild-Grösse/Löschen — je nach Art). */} {selectedAnnot && (
{t(`layouts.annot.${selectedAnnot.kind}`)}
{/* Position (mm, Blattkoordinaten oben-links) — für ALLE Arten über dieselbe Bbox-Ableitung wie die Griffe (`annotationBoundsMm`); der Commit verschiebt die ganze Annotation um das Delta (`translateAnnotation`, dieselbe Funktion wie beim Ziehen). Das ist der eigentliche Ort für X/Y einer Blatt-Annotation (Papier-Millimeter) — NICHT das Objekt-Info-Panel (Modell-Meter, zeigt für Layout-Markups bewusst „nicht anwendbar" statt 0/0). */} {(() => { const b = annotationBoundsMm(selectedAnnot); const moveTo = (nxMm: number, nyMm: number) => { const moved = translateAnnotation(selectedAnnot, nxMm - b.xMm, nyMm - b.yMm); handlers.onPatchAnnotation(selectedAnnot.id, annotationPatch(moved)); }; return ( <> ); })()} {/* Farbe/Strichstärke stehen jetzt in der echten Attribute-Palette (dieselbe wie bei einer Linie im Grundriss, Nutzer-Feedback) — hier nur noch Position + Grösse (Bild) + Aktionen, keine Dopplung. */} {selectedAnnot.kind === "text" && (
{clip(selectedAnnot.text, 40)}
)} {selectedAnnot.kind === "image" && ( <> )}
{selectedAnnot.kind === "text" && ( )}
)} {/* Ausschnitt-Bindung nach dem Aufziehen (Inline-Dropdown, kein prompt). */} {pendingRect && (
{t("layouts.bindSnapshot")}
)} {/* Leer-Hinweis (keine Ausschnitte / keine Viewports). */} {layout.viewports.length === 0 && annotations.length === 0 && !previewRect && !pendingRect && (
{snapshots.length === 0 ? t("layouts.noSnapshots") : placing ? t("layouts.placeHint") : t("layouts.emptyViewports")}
)}
{/* Inline-Text-Editor DIREKT im Rahmen auf dem Blatt (kein eigenes Fenster, Nutzer-Feedback) — für eine neu aufgezogene Text-Annotation (sofort nach dem Aufziehen) ODER eine per HUD „✎" geöffnete bestehende. Jede Änderung schreibt sofort zurück (`onPatchAnnotation`); ein Klick daneben (Backdrop) oder Escape schliesst nur, es gibt nichts zu verwerfen. WYSIWYG: Schriftgrösse UND Feldhöhe folgen dem echten Blatt-Zoom über die CSS-Variablen `--rt-font-size`/`--rt-min-height` (identisches Muster wie PlanView.tsx, s. dort) — NUR die Textfläche skaliert, NICHT die Toolbar. Vorher lief das über ein `transform:scale()` auf die GANZE Box (inkl. Toolbar, die dadurch beim Reinzoomen riesig/unbedienbar wurde) UND `RichTextEditor` schrieb pro Run trotzdem eine ABSOLUTE `pt`-Grösse (per Inline- `style` auf jedem ``), die die geerbte Container-Grösse ignoriert hätte, ohne den Transform-Umweg — beides zusammen war der Bug hinter "Editor ist immer unterschiedlich gross wie der Text". `sizeUnit="em"` auf `RichTextEditor` lässt Runs jetzt relativ zur geerbten Grösse skalieren, s. `text/renderHtml.ts`. ZUSÄTZLICHER Positions-Fix (Folgemeldung "Editor ist tiefer als der Text"): die Toolbar stand bisher im normalen Fluss ÜBER der Textfläche und schob sie um ihre eigene, zoomunabhängige Höhe nach unten — bei üblichem Zoom mehr als die EINE Zeilenhöhe Versatz, mit der die Box positioniert wird. `.rt-toolbar` schwebt jetzt per CSS (`position:absolute; bottom:100%`, s. styles.css) OBERHALB der Fläche, ausserhalb von deren Layout-Fluss. */} {inlineEdit && (() => { const ann = annotations.find((a) => a.id === inlineEdit.id); if (!ann || ann.kind !== "text") return null; const doc = ann.doc ?? docFromText(ann.text); const widthMm = ann.widthMm ?? 60; const contentHeightMm = annotationBoundsMm(ann).heightMm; const heightMm = Math.max(inlineEdit.minHeightMm, contentHeightMm); const fontSizePx = Math.max(9, ann.heightMm * tf.scale); const widthPx = widthMm * tf.scale; const minHeightPx = heightMm * tf.scale; // Baseline → Box-Oberkante: echtes Font-Measuring statt der groben // "ganzes heightMm"-Näherung (s. PlanView.tsx, identischer Fix). const baselineFromTop = contentFontBaselineFromTop(TEXT_LINE_HEIGHT_MULT); const screen = mmToScreen(tf, { xMm: ann.xMm, yMm: ann.yMm - ann.heightMm * baselineFromTop, }); const cssVars = { "--rt-font-size": `${fontSizePx}px`, "--rt-min-height": `${minHeightPx}px`, } as CSSProperties; const commit = (edited: typeof doc) => handlers.onPatchAnnotation( ann.id, { doc: edited, text: edited.paragraphs.map((p) => p.runs.map((r) => r.text).join("")).join("\n"), }, `layoutAnnotationText:${ann.id}`, ); return ( <>
setInlineEdit(null)} />
e.stopPropagation()} onKeyDown={(e) => { if (e.key === "Escape") { e.stopPropagation(); setInlineEdit(null); } }} >
); })()} {/* Verstecktes Bild/PDF-Datei-Input (Werkzeug „image"). */} { const f = e.target.files?.[0]; e.target.value = ""; onToolChange("select"); const at = imagePendingAt; setImagePendingAt(null); if (!f || !at) return; const isPdf = f.type === "application/pdf" || f.name.toLowerCase().endsWith(".pdf"); if (isPdf) { void rasterizePdfPages(f).then((pages) => { pages.forEach((pg, i) => { const { widthMm, heightMm } = defaultImageSizeMm(pg.naturalWidth, pg.naturalHeight); const cascade = i * 6; handlers.onAddAnnotation("image", { xMm: at.xMm - widthMm / 2 + cascade, yMm: at.yMm - heightMm / 2 + cascade, widthMm, heightMm, src: pg.src, }); }); }); } else { void readImageFile(f).then(({ src, naturalWidth, naturalHeight }) => { const { widthMm, heightMm } = defaultImageSizeMm(naturalWidth, naturalHeight); handlers.onAddAnnotation("image", { xMm: at.xMm - widthMm / 2, yMm: at.yMm - heightMm / 2, widthMm, heightMm, src, }); }); } }} />
); } /** * Rendert EINE Annotation im mm-viewBox: Linie/Rechteck als Umriss (Default * `#111111`/0.25 mm, überschreibbar), Text als Baseline-`` mit * `font-size = heightMm`. Ausgewählte Annotationen werden blau eingefärbt; * `ghost` (Zeichnen-Vorschau) wird gestrichelt + halbtransparent dargestellt. */ function AnnotationSvg({ ann, selected, ghost, }: { ann: LayoutAnnotation; selected?: boolean; ghost?: boolean; }) { const selectedColor = "#2478ff"; const dash = ghost ? "2 1.5" : undefined; const opacity = ghost ? 0.7 : 1; if (ann.kind === "line") { return ( ); } if (ann.kind === "rect") { return ( ); } if (ann.kind === "image") { return ( ); } // Text: reiches Doc (mehrere Absätze/gemischte Formatierung) hat Vorrang vor // dem einfachen String — bricht bei gesetzter Rahmenbreite (`widthMm`) // wortweise um (Run-Grenzen bleiben erhalten, s. `wrapDocToLines`). if (ann.doc) { const lineHeight = ann.heightMm * 1.25; const lines: SvgLine[] = wrapDocToLines(ann.doc, { x: 0, y: 0, lineHeight, unitPerPt: 25.4 / 72, basePt: (ann.heightMm / 25.4) * 72, color: ann.color ?? "#111111", ...(ann.widthMm && ann.widthMm > 0 ? { wrapWidthUnits: ann.widthMm } : {}), }); let y = ann.yMm; return ( {lines.map((line, k) => { if (k > 0) y += line.lineHeight; const anchor = line.align === "center" ? "middle" : line.align === "right" ? "end" : "start"; const x = !ann.widthMm ? ann.xMm : line.align === "center" ? ann.xMm + ann.widthMm / 2 : line.align === "right" ? ann.xMm + ann.widthMm : ann.xMm; return ( {line.tspans.length === 0 ? "​" : line.tspans.map((ts, j) => ( {ts.text} ))} ); })} ); } return ( {ann.text} ); } /** Auf 0.1 mm runden (weniger Rausch-Nachkommastellen beim Commit). */ function round1(v: number): number { return Math.round(v * 10) / 10; }