Persist project workspace settings

This commit is contained in:
Formsmith
2026-06-13 10:26:40 +03:00
parent f1d914e2c3
commit 7457c94581
8 changed files with 193 additions and 21 deletions
+58
View File
@@ -0,0 +1,58 @@
import type { GridSize, WorkplaneWorkspaceSettings } from "@/types/sketchforge";
export const DEFAULT_SNAP_GRID: GridSize = "1.0 mm";
export const DEFAULT_WORKPLANE_WORKSPACE: WorkplaneWorkspaceSettings = {
width: 200,
depth: 200,
sizePreset: "200 x 200 mm",
gridBlockSize: 5,
gridBlockPreset: "5 mm",
background: "#f8fbfc",
showShadows: true,
showGrid: true,
cruiseShapes: true,
zoomSpeed: 5,
units: "Metric (Default)",
scale: "1:1 (millimeters)",
};
const snapGridOptions: GridSize[] = ["Off", "0.1 mm", "0.25 mm", "0.5 mm", "1.0 mm", "2.0 mm", "5.0 mm", "Brick"];
function numberOrDefault(value: unknown, fallback: number) {
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
}
function stringOrDefault(value: unknown, fallback: string) {
return typeof value === "string" && value.trim() ? value : fallback;
}
function booleanOrDefault(value: unknown, fallback: boolean) {
return typeof value === "boolean" ? value : fallback;
}
export function normalizeSnapGrid(value: unknown, fallback: GridSize = DEFAULT_SNAP_GRID): GridSize {
return snapGridOptions.includes(value as GridSize) ? (value as GridSize) : fallback;
}
export function normalizeWorkspaceSettings(value: unknown, fallback: WorkplaneWorkspaceSettings = DEFAULT_WORKPLANE_WORKSPACE): WorkplaneWorkspaceSettings {
const candidate = value && typeof value === "object" ? (value as Partial<WorkplaneWorkspaceSettings>) : {};
return {
width: numberOrDefault(candidate.width, fallback.width),
depth: numberOrDefault(candidate.depth, fallback.depth),
sizePreset: stringOrDefault(candidate.sizePreset, fallback.sizePreset),
gridBlockSize: numberOrDefault(candidate.gridBlockSize, fallback.gridBlockSize),
gridBlockPreset: stringOrDefault(candidate.gridBlockPreset, fallback.gridBlockPreset),
background: stringOrDefault(candidate.background, fallback.background),
showShadows: booleanOrDefault(candidate.showShadows, fallback.showShadows),
showGrid: booleanOrDefault(candidate.showGrid, fallback.showGrid),
cruiseShapes: booleanOrDefault(candidate.cruiseShapes, fallback.cruiseShapes),
zoomSpeed: numberOrDefault(candidate.zoomSpeed, fallback.zoomSpeed),
units: stringOrDefault(candidate.units, fallback.units),
scale: stringOrDefault(candidate.scale, fallback.scale),
};
}
export function workplaneSettingsFingerprint(workspace: WorkplaneWorkspaceSettings, snapGrid: GridSize) {
return JSON.stringify({ workspace, snapGrid });
}