// Ressourcen-Tab „Ansichtsvorlagen" ([34]): Vorlagen anlegen (aus der aktiven Ansicht), duplizieren, // umbenennen, löschen, bearbeiten und auf Ansichten/Ausschnitte anwenden (einmal oder verknüpft). // Die Logik (Ops, Weitergabe an verknüpfte Ansichten) liegt in `state/viewTemplates.ts`; die // Handler kommen aus der App, ein Aufruf = ein Undo-Schritt. import { useMemo, useState } from "react"; import type { OverrideRule, Project, ViewTemplate } from "../../model/types"; import { flattenCategories } from "../../model/layers"; import { standardOverrideRules } from "../../model/standards"; import { t } from "../../i18n"; import { DeleteButton, FieldRow, NumberField } from "./fields"; /** Ziel einer Anwendung: aktive Ansicht, ein Geschoss/Schnitt/Ansicht oder ein Ausschnitt. */ export type ViewTemplateTarget = { kind: "active" } | { kind: "level"; id: string } | { kind: "snapshot"; id: string }; export interface ViewTemplateHandlers { onAddViewTemplate?: () => void; onDuplicateViewTemplate?: (id: string) => void; /** Teiländerung; `null` = Feld entfernen (die Vorlage lässt die Einstellung dann unberührt). */ onPatchViewTemplate?: (id: string, patch: Record) => void; onDeleteViewTemplate?: (id: string) => void; onApplyViewTemplate?: (id: string, target: ViewTemplateTarget, linked: boolean) => void; /** [33] Override-Baustein (Standardregel) ins Projekt holen und in der Vorlage einschalten. */ onAddRuleBlock?: (id: string, rule: OverrideRule) => void; } const DISPLAY_MODES = ["all_force", "all", "active", "grey", "grey_locked", "locked"] as const; /** Einstellung mit Ein-/Ausschalter: aus = die Vorlage lässt sie unberührt. */ function Optional({ label, on, onToggle, children }: { label: string; on: boolean; onToggle: (on: boolean) => void; children: React.ReactNode }) { return (
{on ? children : –}
); } /** * [33] Override-Regeln der Vorlage (`enabledOverrideRuleIds`): an = die Vorlage legt fest, welche Regeln gelten. * Bausteine (mitgelieferte Regeln wie „Deckenaufbau weiss") lassen sich übernehmen: Regel ins Projekt, in der * Vorlage eingeschaltet. */ function RulesSection({ tpl, project, h }: { tpl: ViewTemplate; project: Project; h: ViewTemplateHandlers }) { const rules = project.overrideRules ?? []; const blocks = useMemo(() => standardOverrideRules(project), [project]); const [block, setBlock] = useState(""); const ids = tpl.enabledOverrideRuleIds; const patch = (p: Record) => h.onPatchViewTemplate?.(tpl.id, p); const chosen = blocks.find((b) => b.id === block) ?? blocks[0]; return ( <> patch({ enabledOverrideRuleIds: on ? rules.filter((r) => r.enabled).map((r) => r.id) : null })} > {t("resources.viewTemplates.rulesHint")} {ids !== undefined && (
{rules.map((r) => ( patch({ enabledOverrideRuleIds: e.target.checked ? [...ids, r.id] : ids.filter((x) => x !== r.id) })} /> ))}
)} {chosen && ( )} ); } function TemplateDetail({ tpl, project, h, }: { tpl: ViewTemplate; project: Project; h: ViewTemplateHandlers; }) { const [target, setTarget] = useState("active"); const patch = (p: Record) => h.onPatchViewTemplate?.(tpl.id, p); const num = (key: "cutHeight" | "viewTop" | "viewBottom" | "viewDepth", label: string, dflt: number) => ( patch({ [key]: on ? dflt : null })}> patch({ [key]: v })} /> ); const bool = (key: "mono" | "shadows" | "hiddenLines" | "scaleLineWeights" | "sketch" | "cutHatch", label: string) => ( patch({ [key]: on ? false : null })}> patch({ [key]: e.target.checked })} /> ); const cats = flattenCategories(project.layers); const levels = project.drawingLevels.filter((l) => l.kind !== "drawing"); const parseTarget = (v: string): ViewTemplateTarget => v === "active" ? { kind: "active" } : v.startsWith("level:") ? { kind: "level", id: v.slice(6) } : { kind: "snapshot", id: v.slice(5) }; return (
patch({ name: e.target.value })} /> patch({ detail: on ? "mittel" : null })}> patch({ scaleDenominator: on ? 100 : null })}> patch({ scaleDenominator: Math.max(1, v) })} /> patch({ displayMode: on ? "all" : null })}> {bool("mono", t("resources.viewTemplates.mono"))} {bool("shadows", t("resources.viewTemplates.shadows"))} {bool("hiddenLines", t("resources.viewTemplates.hiddenLines"))} {bool("scaleLineWeights", t("resources.viewTemplates.scaleLineWeights"))} {bool("sketch", t("resources.viewTemplates.sketch"))} {bool("cutHatch", t("resources.viewTemplates.cutHatch"))} patch({ floorFinish: on ? "off" : null })}>
{t("resources.viewTemplates.rangeHint")}
{num("cutHeight", t("resources.viewTemplates.cutHeight"), 1.0)} {num("viewTop", t("resources.viewTemplates.viewTop"), 2.5)} {num("viewBottom", t("resources.viewTemplates.viewBottom"), 0)} {num("viewDepth", t("resources.viewTemplates.viewDepth"), 0.5)}
{t("resources.viewTemplates.categories")}
{cats.map((c) => { const v = tpl.layerVisibility?.[c.code]; return ( ); })}
{t("resources.viewTemplates.apply")}
); } export function ViewTemplatesTab({ project, handlers }: { project: Project; handlers: ViewTemplateHandlers }) { const list = project.viewTemplates ?? []; const [selectedId, setSelectedId] = useState(null); const selected = list.find((x) => x.id === selectedId) ?? list[0]; const usedBy = (id: string) => project.drawingLevels.filter((l) => l.templateId === id && l.templateLinked).length + (project.viewSnapshots ?? []).filter((s) => s.templateId === id && s.templateLinked).length; return (
{t("resources.viewTemplates.hint")}
{list.map((x) => (
setSelectedId(x.id)} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") setSelectedId(x.id); }} > {x.name} {usedBy(x.id) > 0 && {t("resources.viewTemplates.linkedCount", { n: usedBy(x.id) })}} handlers.onDeleteViewTemplate?.(x.id)} />
))} {list.length === 0 &&
{t("resources.viewTemplates.empty")}
}
{selected ? :
{t("resources.viewTemplates.empty")}
}
); }