Files
DOSSIER/src/commands/cmds/textbox.ts
T
Claude a24cfbb3d7 [K4][6][8] Aktionen erzeugen Ops direkt statt Projekt → Projekt + diffToOps
- Store `applyChange(ops | (p) => ops, coalesceKey?)` geht ohne Differenz an den Kern;
  `setProject` erkennt `opsMutation`-Funktionen und nimmt deren Ops.
- Umgestellt: Griffe (moveGripOf/moveElementByOf/moveEdgeOf, Decke/Dach/Treppe/Raum),
  Objekt-Info (updateWall/-Ceiling/-Opening/-Roof/-Stair/-Room/-Drawing2D, Farbe, Füllung,
  Vorder-/Hintergrund, Resize, Wand-/Deckendicke, Polylinien-Punkte), Auswahl verschieben/
  kopieren/drehen (`transformOps`), Löschen der Auswahl (+ Schnittlinien, eine Gruppe),
  Ebenen/Sichtbarkeit/Kategorien, Text-Tippen, Panel-Zeichnungs-Eigenschaften und die
  Zeichenbefehle (Wand, Decke, Raum, Treppe, Öffnung, Dach, Stütze, Extrusion, alle
  2D-Zeichenbefehle, move/copy/mirror/rotate/scale/array). Gruppen-Zuordnung beim Zeichnen
  in einer Gruppe über `activeGroupOps`.
- Griff-Zug mit mitgeführten Ecken: je Mausschritt eine Gruppe mit Schlüssel → ein
  Undo-Schritt für den ganzen Zug; aufgeschobene Commits beim Loslassen als Gruppe.
- Tests: Store-Zufallsfolgen (Leseansicht = Kern, Aktion = applyOps(vorher, ihre Ops),
  Undo bis Anfang/Redo bis Ende exakt), activeGroupOps = addNewElementsToActiveGroup;
  beim Umbau zusätzlich 2400 Zufallsschritte gegen die alte setProject-Fassung und 400
  Transformationen gegen das alte commitTransform geprüft (gleiches Projekt bis auf
  erzeugte ids und fehlend/leere Listen).
- Messung (vitest, 3000 Wände): Griff ziehen 3,3 → 0,2–0,3 ms/Schritt, Auswahl (300 Wände)
  verschieben 30 → 6–7 ms, Eigenschaft tippen 3 → 0,03 ms.

Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
2026-10-28 22:02:40 +00:00

144 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Textspalte (Absatztext) — wie das Text-Werkzeug, aber als aufgezogener
// RAHMEN: Breite UND Höhe kommen aus einem Rechteck (zwei diagonale Punkte),
// nicht nur einer Breitenlinie (InDesign-artiges Textrahmen-Verhalten,
// Nutzer-Wunsch). Schritte:
// 1) „Erste Ecke:" → Punkt
// 2) „Gegenecke:" → Punkt (Live-Vorschau des Rahmens, beliebige Richtung)
// Committet danach SOFORT ein leeres Text-Element (mit `width`) und meldet
// `focusDrawingId` + `focusMinHeightM` (die aufgezogene Rahmenhöhe als
// MINDESThöhe, s. types.ts) — die UI (PlanView) öffnet den Inline-Rich-
// Text-Editor direkt auf der Zeichenfläche, mit der Spaltenbreite als CSS-
// Breite (echter Wortumbruch beim Tippen, kein Dialog) und dem Rahmen als
// Mindesthöhe (wächst bei mehr Text darüber hinaus). Bleibt der Editor
// leer, verwirft `commitTextEdit`/`closeTextEdit` (App.tsx) das Element
// wieder.
//
// Reuse des bestehenden {shape:"text"}-Elements (mit `width`) — so erben
// Selektion, Verschieben und Griff automatisch vom Einzeltext; die Modell-
// Geometrie selbst kennt aber keine feste Rahmenhöhe (nur `width` für den
// Wortumbruch) — die Höhe bleibt reine Editor-Startgrösse (PlanView-lokal,
// s. focusMinHeightM), die Textmenge bestimmt die tatsächliche Höhe.
import type { Drawing2D } from "../../model/types";
import { uniqueId } from "../../tools/types";
import type {
Command,
CommandContext,
CommandResult,
CommandState,
DraftShape,
Vec2,
} from "../types";
import { addItems, opsMutation } from "../../core";
import type { CoreOp } from "../../core";
/** Default-Schrifthöhe einer Textspalte in Modell-Metern (= Baseline-Versatz vom Rahmen-Top). */
const DEFAULT_TEXT_HEIGHT_M = 0.25;
/** Kleinste sinnvolle Rahmen-Breite/-Höhe (Meter). */
const MIN_SIZE_M = 0.2;
interface TbFirst extends CommandState {
phase: "first";
}
interface TbSecond extends CommandState {
phase: "second";
a: Vec2;
cursor: Vec2 | null;
}
type TbState = TbFirst | TbSecond;
/** Normalisiertes Rechteck aus zwei diagonalen Punkten (beliebige Zugrichtung). */
function rectOf(a: Vec2, b: Vec2): { minX: number; maxX: number; minY: number; maxY: number } {
return {
minX: Math.min(a.x, b.x),
maxX: Math.max(a.x, b.x),
minY: Math.min(a.y, b.y),
maxY: Math.max(a.y, b.y),
};
}
/** Breite/Höhe (geklemmt auf MIN_SIZE_M) + die Textanker-Position (oben-links, als Baseline). */
function frameOf(a: Vec2, b: Vec2): { at: Vec2; width: number; heightM: number } {
const r = rectOf(a, b);
const width = Math.max(MIN_SIZE_M, r.maxX - r.minX);
const heightM = Math.max(MIN_SIZE_M, r.maxY - r.minY);
// `at` ist die Baseline-links der ERSTEN Zeile (s. drawing2d.ts/primitives.tsx);
// die Rahmen-Oberkante liegt eine Zeilenhöhe darüber → at.y = Rahmen-Top − Zeilenhöhe.
return { at: { x: r.minX, y: r.maxY - DEFAULT_TEXT_HEIGHT_M }, width, heightM };
}
/** Vorschau: Rahmen-Rechteck zwischen den beiden Ecken. */
function frameDraft(a: Vec2, cursor: Vec2): { preview: DraftShape[]; vertices: Vec2[]; hud: { at: Vec2; text: string } } {
const r = rectOf(a, cursor);
const pts: Vec2[] = [
{ x: r.minX, y: r.minY },
{ x: r.maxX, y: r.minY },
{ x: r.maxX, y: r.maxY },
{ x: r.minX, y: r.maxY },
];
const w = Math.max(MIN_SIZE_M, r.maxX - r.minX);
const h = Math.max(MIN_SIZE_M, r.maxY - r.minY);
return {
preview: [{ kind: "poly", pts, closed: true }],
vertices: [a, cursor],
hud: { at: cursor, text: `${w.toFixed(2)} × ${h.toFixed(2)} m` },
};
}
function appendEmptyTextbox(id: string, at: Vec2, width: number, ctx: CommandContext): CoreOp[] {
const d: Drawing2D = {
id,
type: "drawing2d",
levelId: ctx.level.id,
categoryCode: ctx.defaultCategoryCode,
geom: { shape: "text", at, text: "", height: DEFAULT_TEXT_HEIGHT_M, angle: 0, width },
};
return addItems("drawings2d", [d]);
}
const idle = (): [CommandState, CommandResult] => [
{ phase: "first", lastPoint: null } as TbFirst,
{ draft: null, done: true },
];
export const textboxCommand: Command = {
name: "textbox",
labelKey: "cmd.textbox.label",
prompt: (s) => ((s as TbState).phase === "second" ? "cmd.textbox.second" : "cmd.textbox.first"),
accepts: () => ["point"],
options: () => [],
init: (): TbFirst => ({ phase: "first", lastPoint: null }),
onInput: (state, input, ctx): [CommandState, CommandResult] => {
const s = state as TbState;
if (input.kind !== "point") return [s, { draft: null }];
if (s.phase === "first") {
const ns: TbSecond = { phase: "second", a: input.point, cursor: input.point, lastPoint: input.point };
return [ns, { draft: { preview: [], vertices: [input.point] } }];
}
// phase === "second"
const { at, width, heightM } = frameOf(s.a, input.point);
const id = uniqueId("dr2d");
return [
{ phase: "first", lastPoint: null } as TbFirst,
{
draft: null,
done: true,
commit: opsMutation(() => appendEmptyTextbox(id, at, width, ctx)),
focusDrawingId: id,
focusMinHeightM: heightM,
},
];
},
onMove: (state, point): [CommandState, CommandResult] => {
const s = state as TbState;
if (s.phase !== "second") return [s, { draft: null }];
const ns: TbSecond = { ...s, cursor: point };
return [ns, { draft: frameDraft(s.a, point) }];
},
onConfirm: (): [CommandState, CommandResult] => idle(),
onCancel: (): [CommandState, CommandResult] => idle(),
};