Attribute: Fuellart Keine/Vollflaeche/Schraffur fuer geschlossene 2D-Elemente
This commit is contained in:
1 parent
4b2e02b37a
commit
6fbc8d4ff8
7 files changed
+111
No files matched your search
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { Drawing2D, HatchStyle } from "../model/types";
|
||||
import { DEFAULT_SOLID_FILL, applyFillMode } from "./fillMode";
|
||||
|
||||
const hatches = [
|
||||
{ id: "none", name: "Ohne", pattern: "none" },
|
||||
{ id: "diag", name: "Diagonal", pattern: "diagonal" },
|
||||
] as unknown as HatchStyle[];
|
||||
const base = { id: "d", type: "drawing2d", levelId: "L", categoryCode: "60" } as unknown as Drawing2D;
|
||||
|
||||
describe("applyFillMode", () => {
|
||||
it("none: entfernt Farbe und Schraffur, Quellen auf eigenen Wert", () => {
|
||||
const d = applyFillMode({ ...base, background: "#fff", fillColor: "#eee", hatchId: "diag" } as Drawing2D, hatches, "none");
|
||||
expect(d.background).toBeUndefined();
|
||||
expect(d.fillColor).toBeUndefined();
|
||||
expect(d.hatchId).toBeUndefined();
|
||||
expect(d.backgroundSource).toBe("object");
|
||||
});
|
||||
it("solid: behält vorhandene Füllfarbe, sonst Vorgabe; ohne Schraffur", () => {
|
||||
expect(applyFillMode({ ...base, fillColor: "#123456" } as Drawing2D, hatches, "solid").background).toBe("#123456");
|
||||
expect(applyFillMode(base, hatches, "solid").background).toBe(DEFAULT_SOLID_FILL);
|
||||
expect(applyFillMode({ ...base, hatchId: "diag" } as Drawing2D, hatches, "solid").hatchId).toBeUndefined();
|
||||
});
|
||||
it("hatch: wählt die erste echte Schraffur (nie 'Ohne'), behält die Füllfarbe", () => {
|
||||
const d = applyFillMode({ ...base, background: "#abcdef" } as Drawing2D, hatches, "hatch");
|
||||
expect(d.hatchId).toBe("diag");
|
||||
expect(d.background).toBe("#abcdef");
|
||||
expect(applyFillMode({ ...base, hatchId: "none" } as Drawing2D, hatches, "hatch").hatchId).toBe("diag");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
// Füllart eines geschlossenen 2D-Elements: Keine / Vollfläche / Schraffur.
|
||||
// Reine Umrechnung auf die vorhandenen Felder (`background`, `fillColor`, `hatchId`
|
||||
// samt Quellen) — der Renderer (`addDrawing2D`) braucht kein neues Feld.
|
||||
|
||||
import type { Drawing2D, HatchStyle } from "../model/types";
|
||||
|
||||
export type FillMode = "none" | "solid" | "hatch";
|
||||
|
||||
/** Vorgabe-Füllfarbe, wenn "Vollfläche" gewählt wird und noch keine gesetzt ist. */
|
||||
export const DEFAULT_SOLID_FILL = "#d9dde2";
|
||||
|
||||
export function applyFillMode(d: Drawing2D, hatches: readonly HatchStyle[], mode: FillMode): Drawing2D {
|
||||
const next = { ...d } as Drawing2D & Record<string, unknown>;
|
||||
if (mode === "none") {
|
||||
delete next.background;
|
||||
delete next.fillColor;
|
||||
delete next.hatchId;
|
||||
next.backgroundSource = "object";
|
||||
next.hatchSource = "object";
|
||||
} else if (mode === "solid") {
|
||||
next.background = d.background ?? d.fillColor ?? DEFAULT_SOLID_FILL;
|
||||
next.backgroundSource = "object";
|
||||
delete next.hatchId;
|
||||
next.hatchSource = "object";
|
||||
} else {
|
||||
const current = hatches.find((h) => h.id === d.hatchId);
|
||||
const keep = current && current.pattern !== "none" ? current.id : hatches.find((h) => h.pattern !== "none")?.id;
|
||||
if (keep) next.hatchId = keep;
|
||||
next.hatchSource = "object";
|
||||
}
|
||||
return next as Drawing2D;
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
// Extrahiert aus App.tsx. Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
|
||||
|
||||
import { applyFillMode } from "./fillMode";
|
||||
import { useMemo, type RefObject } from "react";
|
||||
import type { DrawingLevel, Layout, Project } from "../model/types";
|
||||
import { resolvePenColor } from "../model/penSets";
|
||||
@@ -470,6 +471,13 @@ export function usePanelHostBase(opts: {
|
||||
if (selection?.kind === "drawing2d")
|
||||
setElementFillColor(selection.id, color);
|
||||
},
|
||||
onSetSelectionFillMode: (mode) => {
|
||||
if (selection?.kind !== "drawing2d") return;
|
||||
setProject((p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) => (d.id === selection.id ? applyFillMode(d, p.hatches, mode) : d)),
|
||||
}));
|
||||
},
|
||||
onSetSelectionFillOpacity: (opacity) => {
|
||||
if (selection?.kind === "drawing2d")
|
||||
setProject((p) => ({
|
||||
|
||||
Reference in new issue
Block a user