Files
DOSSIER/src/commands/cmds/textbox.ts
T
Claude 61263efe13 [K4] Zeichenbefehle im Kern: Elemente, Vorschauen, Gruppen-Zuordnung
Alle Erzeuge-Befehle bauen ihre Elemente im Kern: cmd_create.rs (Wand, Decke,
Raum, Treppe, Öffnung, Dach, Stütze, Extrusion), cmd_draw.rs (2D-Formen,
Texte, Bemassung, autodim), cmd_solid.rs (solid, solidbool, voidbox, terrain,
Deckenöffnung, Dachhof, Schnittlinie). Vorschauen über doc_create_preview(_again)
ohne JSON je Mausschritt, Abfragen über doc_create_query; Gruppen-Zuordnung als
`group` im Request ersetzt activeGroupOps. TS hält nur die Zustandsautomaten
(commands/createCore.ts). Ersetzte TS-Geometrie und alte Werkzeug-Commits in
tools/tools.ts gelöscht. Gleichheit alt/neu mit zeitweiligen Zufallstests
geprüft (s. TODO.md). Die drei Gruppen hängen über den gemeinsamen Dispatcher
zusammen und sind deshalb ein Commit.
2026-10-29 15:21:04 +00:00

108 lines
4.1 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 { uniqueId } from "../../tools/types";
import type {
Command,
CommandContext,
CommandResult,
CommandState,
DraftShape,
Vec2,
} from "../types";
import { createMutation, createPreview } from "../createCore";
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). */
/**
* Textrahmen im Kern (`cmd_draw.rs`): Rechteck aus zwei Ecken, Breite/Höhe mindestens 0.2 m
* (`info = [Breite, Höhe]`), Anker oben links eine Zeilenhöhe tief.
*/
function frameDraft(ctx: CommandContext, a: Vec2, cursor: Vec2): { preview: DraftShape[]; vertices: Vec2[]; hud: { at: Vec2; text: string } } {
const pv = createPreview(ctx, { cmd: "textbox" }, [a, cursor]);
const [w, h] = pv.info;
return {
preview: pv.shapes,
vertices: [a, cursor],
hud: { at: cursor, text: `${w.toFixed(2)} × ${h.toFixed(2)} m` },
};
}
/** Commit (Kern): leere Textspalte (Breite des Rahmens), mit vorab vergebener id. */
const appendEmptyTextbox = (id: string, a: Vec2, b: Vec2, ctx: CommandContext) =>
createMutation(ctx, { cmd: "textbox", id, a, b });
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 heightM = createPreview(ctx, { cmd: "textbox" }, [s.a, input.point]).info[1];
const id = uniqueId("dr2d");
return [
{ phase: "first", lastPoint: null } as TbFirst,
{
draft: null,
done: true,
commit: appendEmptyTextbox(id, s.a, input.point, ctx),
focusDrawingId: id,
focusMinHeightM: heightM,
},
];
},
onMove: (state, point, _snap, ctx): [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(ctx, s.a, point) }];
},
onConfirm: (): [CommandState, CommandResult] => idle(),
onCancel: (): [CommandState, CommandResult] => idle(),
};