checkpoint: stabilize editor geometry and workspace settings

This commit is contained in:
Formsmith
2026-06-19 21:47:13 +03:00
parent 272fac5069
commit 2d0ed7ef23
8 changed files with 316 additions and 85 deletions
@@ -1,10 +1,10 @@
"use client";
import { ChevronDown, ChevronUp, LockKeyhole, LockKeyholeOpen } from "lucide-react";
import { useState, type CSSProperties, type Dispatch, type SetStateAction } from "react";
import { useEffect, useState, type CSSProperties, type Dispatch, type SetStateAction } from "react";
import { ToolbarHideSelectedIcon } from "@/components/icons";
import { fallbackSolidColor, resizedShapeSize, shapeDepth, shapeWidth } from "@/lib/workplaneShapes";
import type { GridSize, WorkplaneShape } from "@/types/sketchforge";
import type { GridSize, MeasurementAccuracy, WorkplaneShape } from "@/types/sketchforge";
const GRID_SIZES: GridSize[] = ["Off", "0.1 mm", "0.25 mm", "0.5 mm", "1.0 mm", "2.0 mm", "5.0 mm", "Brick"];
const MIN_SHAPE_SIZE = 0.01;
@@ -73,8 +73,9 @@ function clamp(value: number, min: number, max: number) {
return Math.min(max, Math.max(min, value));
}
function formatPanelNumber(value: number) {
return (Math.abs(value) < 0.005 ? 0 : value).toFixed(2);
function formatPanelNumber(value: number, accuracy: MeasurementAccuracy) {
const zeroThreshold = 0.5 * 10 ** -accuracy;
return (Math.abs(value) < zeroThreshold ? 0 : value).toFixed(accuracy);
}
function getShapeProperties(shape: WorkplaneShape, onUpdate: ShapeInspectorUpdate): ShapePropertyConfig[] {
@@ -196,6 +197,7 @@ export function ShapeInspector({
shape,
snap,
snapOpen,
accuracy,
onUpdate,
onClose,
onSnapChange,
@@ -204,6 +206,7 @@ export function ShapeInspector({
shape: WorkplaneShape;
snap: GridSize;
snapOpen: boolean;
accuracy: MeasurementAccuracy;
onUpdate: ShapeInspectorUpdate;
onClose: () => void;
onSnapChange: Dispatch<SetStateAction<GridSize>>;
@@ -319,7 +322,7 @@ export function ShapeInspector({
if (property.type === "select") {
return <SelectProperty key={property.label} {...property} disabled={locked} />;
}
return <RangeProperty key={property.label} {...property} disabled={locked} />;
return <RangeProperty key={property.label} {...property} accuracy={accuracy} disabled={locked} />;
})}
</div>
) : null}
@@ -369,14 +372,31 @@ export function SnapGridControl({
);
}
function RangeProperty({ label, value, min, max, step = 0.01, disabled, onChange }: RangePropertyConfig & { disabled?: boolean }) {
const safeValue = clamp(value, min, max);
const position = ((safeValue - min) / Math.max(1, max - min)) * 100;
function RangeProperty({
label,
value,
min,
max,
step = 0.01,
accuracy,
disabled,
onChange,
}: RangePropertyConfig & { accuracy: MeasurementAccuracy; disabled?: boolean }) {
const allowsAboveSliderMax = label === "Length" || label === "Width" || label === "Height";
const actualValue = Math.max(min, Number.isFinite(value) ? value : min);
const sliderValue = clamp(actualValue, min, max);
const position = ((sliderValue - min) / Math.max(1, max - min)) * 100;
const [editing, setEditing] = useState(false);
const [draft, setDraft] = useState(formatPanelNumber(safeValue));
const [draft, setDraft] = useState(formatPanelNumber(actualValue, accuracy));
useEffect(() => {
if (!editing) {
setDraft(formatPanelNumber(actualValue, accuracy));
}
}, [accuracy, actualValue, editing]);
const commitDraft = () => {
const next = Number(draft);
onChange(clamp(Number.isFinite(next) ? next : safeValue, min, max));
const finiteNext = Number.isFinite(next) ? next : actualValue;
onChange(allowsAboveSliderMax ? Math.max(min, finiteNext) : clamp(finiteNext, min, max));
setEditing(false);
};
return (
@@ -388,7 +408,7 @@ function RangeProperty({ label, value, min, max, step = 0.01, disabled, onChange
className="range-value-input"
type="number"
min={min}
max={max}
max={allowsAboveSliderMax ? undefined : max}
step={step}
value={draft}
autoFocus
@@ -398,7 +418,7 @@ function RangeProperty({ label, value, min, max, step = 0.01, disabled, onChange
if (event.key === "Enter") {
event.currentTarget.blur();
} else if (event.key === "Escape") {
setDraft(formatPanelNumber(safeValue));
setDraft(formatPanelNumber(actualValue, accuracy));
setEditing(false);
}
}}
@@ -410,11 +430,11 @@ function RangeProperty({ label, value, min, max, step = 0.01, disabled, onChange
return;
}
event.preventDefault();
setDraft(formatPanelNumber(safeValue));
setDraft(formatPanelNumber(actualValue, accuracy));
setEditing(true);
}}
>
{formatPanelNumber(safeValue)}
{formatPanelNumber(actualValue, accuracy)}
</output>
)}
<input
@@ -422,7 +442,7 @@ function RangeProperty({ label, value, min, max, step = 0.01, disabled, onChange
min={min}
max={max}
step={step}
value={safeValue}
value={sliderValue}
disabled={disabled}
onChange={(event) => {
const next = Number(event.currentTarget.value);
@@ -1,7 +1,7 @@
"use client";
import { X } from "lucide-react";
import type { CSSProperties } from "react";
import { useState, type CSSProperties } from "react";
import { DEFAULT_WORKPLANE_WORKSPACE } from "@/lib/workplaneSettings";
import type { GridSize, WorkplaneWorkspaceSettings } from "@/types/sketchforge";
@@ -38,15 +38,21 @@ export function WorkspaceSettingsModal({
snap,
onWorkspaceChange,
onSnapChange,
onMakeDefault,
onClose,
}: {
workspace: WorkspaceSettings;
snap: GridSize;
onWorkspaceChange: (next: WorkspaceSettings) => void;
onSnapChange: (next: GridSize) => void;
onMakeDefault: () => void;
onClose: () => void;
}) {
const patchWorkspace = (patch: Partial<WorkspaceSettings>) => onWorkspaceChange({ ...workspace, ...patch });
const [defaultSaved, setDefaultSaved] = useState(false);
const patchWorkspace = (patch: Partial<WorkspaceSettings>) => {
setDefaultSaved(false);
onWorkspaceChange({ ...workspace, ...patch });
};
const setDimension = (key: "width" | "depth", value: string) => {
const next = clamp(Number.parseFloat(value) || DEFAULT_WORKPLANE_WORKSPACE[key], MIN_WORKSPACE_SIZE, MAX_WORKSPACE_SIZE);
patchWorkspace({ [key]: next, sizePreset: "Custom" } as Partial<WorkspaceSettings>);
@@ -122,7 +128,21 @@ export function WorkspaceSettingsModal({
options={["1:1 (millimeters)", "1:10 (centimeters)", "1:100 (meters)", "1:1000 (meters)"]}
onChange={(scale) => patchWorkspace({ scale })}
/>
<WorkspaceSelect label="Snap Grid" value={snap} options={GRID_SIZES} onChange={(next) => onSnapChange(next as GridSize)} />
<WorkspaceSelect
label="Accuracy"
value={`0.${"0".repeat(workspace.accuracy)}`}
options={["0.0", "0.00", "0.000"]}
onChange={(accuracy) => patchWorkspace({ accuracy: accuracy.slice(2).length as WorkspaceSettings["accuracy"] })}
/>
<WorkspaceSelect
label="Snap Grid"
value={snap}
options={GRID_SIZES}
onChange={(next) => {
setDefaultSaved(false);
onSnapChange(next as GridSize);
}}
/>
<WorkspaceSelect
label="Workplane size"
value={workspace.sizePreset}
@@ -135,7 +155,7 @@ export function WorkspaceSettingsModal({
<span>Width</span>
<input
type="number"
value={workspace.width.toFixed(2)}
value={workspace.width.toFixed(workspace.accuracy)}
min={MIN_WORKSPACE_SIZE}
max={MAX_WORKSPACE_SIZE}
step={1}
@@ -146,7 +166,7 @@ export function WorkspaceSettingsModal({
<span>Length</span>
<input
type="number"
value={workspace.depth.toFixed(2)}
value={workspace.depth.toFixed(workspace.accuracy)}
min={MIN_WORKSPACE_SIZE}
max={MAX_WORKSPACE_SIZE}
step={1}
@@ -161,7 +181,7 @@ export function WorkspaceSettingsModal({
<span>Block size</span>
<input
type="number"
value={workspace.gridBlockSize.toFixed(2)}
value={workspace.gridBlockSize.toFixed(workspace.accuracy)}
min={MIN_GRID_BLOCK_SIZE}
max={MAX_GRID_BLOCK_SIZE}
step={0.5}
@@ -171,8 +191,14 @@ export function WorkspaceSettingsModal({
</div>
) : null}
<button className="make-default-button" onClick={() => onWorkspaceChange(DEFAULT_WORKPLANE_WORKSPACE)}>
Make default
<button
className="make-default-button"
onClick={() => {
onMakeDefault();
setDefaultSaved(true);
}}
>
{defaultSaved ? "Default saved" : "Make default"}
</button>
</div>
</div>