Files
snapcad-studio/apps/web/src/components/workplane/ShapeInspector.tsx
T

773 lines
31 KiB
TypeScript
Raw Normal View History

2026-06-14 22:05:58 +03:00
"use client";
2026-07-05 12:28:23 +03:00
import { ChevronDown, ChevronUp, LockKeyhole, LockKeyholeOpen, Split } from "lucide-react";
import { useEffect, useLayoutEffect, useRef, useState, type CSSProperties, type Dispatch, type SetStateAction } from "react";
2026-06-14 22:05:58 +03:00
import { ToolbarHideSelectedIcon } from "@/components/icons";
import {
DEFAULT_GEAR_HELIX_ANGLE,
DEFAULT_GEAR_HELIX_QUALITY,
DEFAULT_GEAR_TEETH,
DEFAULT_GEAR_TOOTH_SIZE,
MAX_GEAR_HELIX_ANGLE,
MAX_GEAR_HELIX_QUALITY,
MIN_GEAR_HELIX_ANGLE,
MIN_GEAR_HELIX_QUALITY,
gearCenterHoleLimits,
normalizeGearHelixAngle,
normalizeGearHelixQuality,
normalizeGearCenterHoleSize,
normalizeGearToothSize,
normalizeGearToothWidth,
normalizeGearType,
gearToothPitch,
} from "@/lib/gearGeometry";
import { displayStepFromMillimeters, displayToMillimeters, formatMeasurementNumber, lengthDisplayUnit, millimetersToDisplay, parseMeasurementInput } from "@/lib/measurementUnits";
2026-06-14 22:05:58 +03:00
import { fallbackSolidColor, resizedShapeSize, shapeDepth, shapeWidth } from "@/lib/workplaneShapes";
import { normalizeSketchRevolveSettings } from "@/lib/sketchRevolve";
import type { GearType, GridSize, MeasurementAccuracy, WorkplaneShape, WorkplaneWorkspaceSettings } from "@/types/sketchforge";
2026-06-14 22:05:58 +03:00
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;
const SOLID_COLORS = [
"#d41721",
"#ff4b4b",
"#ff7a1a",
"#d97813",
"#f6a21a",
"#f2cf10",
"#f7e65a",
"#a8d642",
"#33983d",
"#1fb66d",
"#18b99a",
"#0098c7",
"#49c7ef",
"#3b82f6",
"#294c93",
"#5b5ce2",
"#6e2786",
"#9b3bd2",
"#c9009a",
"#f062b6",
"#8a5a2b",
"#b98254",
"#f2caa0",
"#ffffff",
"#cfd8df",
"#8a98a6",
"#4b5563",
"#111111",
];
const TEXT_FONT_OPTIONS = ["Multilanguage", "Sans", "Serif", "Script", "Monospace", "Rounded", "Stencil"];
const GEAR_TYPE_OPTIONS: Array<{ value: GearType; label: string }> = [
{ value: "spur", label: "Spur gear" },
{ value: "helical", label: "Helical gear" },
{ value: "bevel", label: "Bevel gear" },
];
2026-06-14 22:05:58 +03:00
type RangePropertyConfig = {
type?: "range";
label: string;
value: number;
min: number;
max: number;
step?: number;
onChange: (value: number) => void;
};
type TextPropertyConfig = {
type: "text";
label: string;
value: string;
onChange: (value: string) => void;
};
type SelectPropertyConfig = {
type: "select";
label: string;
value: string;
options: string[];
onChange: (value: string) => void;
};
type ShapePropertyConfig = RangePropertyConfig | TextPropertyConfig | SelectPropertyConfig;
2026-06-15 12:56:23 +03:00
export type ShapeInspectorUpdateOptions = { resizeAxis?: "width" | "depth" | "height" };
type ShapeInspectorUpdate = (patch: Partial<WorkplaneShape>, options?: ShapeInspectorUpdateOptions) => void;
2026-06-14 22:05:58 +03:00
function clamp(value: number, min: number, max: number) {
return Math.min(max, Math.max(min, value));
}
2026-07-05 12:28:23 +03:00
function formatPropertyNumber(value: number, accuracy: MeasurementAccuracy, step: number) {
if (step >= 1) return String(Math.round(value));
return formatMeasurementNumber(value, accuracy, step);
}
function propertyUsesLengthUnit(label: string) {
return ["Radius", "Length", "Width", "Height", "Bevel", "Top Radius", "Base Radius", "Thickness", "Tooth Size", "Tooth Width", "Center Hole"].includes(label);
2026-06-14 22:05:58 +03:00
}
2026-06-15 12:56:23 +03:00
function getShapeProperties(shape: WorkplaneShape, onUpdate: ShapeInspectorUpdate): ShapePropertyConfig[] {
2026-06-14 22:05:58 +03:00
const width = shapeWidth(shape);
const depth = shapeDepth(shape);
2026-06-15 12:56:23 +03:00
const setWidth = (value: number) => onUpdate({ width: value, size: resizedShapeSize(value, depth) }, { resizeAxis: "width" });
const setDepth = (value: number) => onUpdate({ depth: value, size: resizedShapeSize(width, value) }, { resizeAxis: "depth" });
const setConeWidth = (value: number) => onUpdate({ width: value, baseRadius: value / 2, size: resizedShapeSize(value, depth) }, { resizeAxis: "width" });
2026-06-14 22:05:58 +03:00
const setBaseRadius = (value: number) => {
const diameter = value * 2;
2026-06-15 12:56:23 +03:00
onUpdate({ baseRadius: value, width: diameter, size: resizedShapeSize(diameter, depth) }, { resizeAxis: "width" });
2026-06-14 22:05:58 +03:00
};
2026-06-15 12:56:23 +03:00
const setHeight = (height: number) => onUpdate({ height }, { resizeAxis: "height" });
2026-06-14 22:05:58 +03:00
if (shape.sketchOperation === "revolve" || shape.sketchRevolve) {
const settings = normalizeSketchRevolveSettings(shape.sketchRevolve);
const updateRevolve = (patch: Partial<typeof settings>) => onUpdate({ sketchRevolve: normalizeSketchRevolveSettings({ ...settings, ...patch }) });
return [
{ label: "Start Angle", value: settings.startAngle, min: 0, max: 359, step: 1, onChange: (startAngle) => updateRevolve({ startAngle }) },
{ label: "Sweep", value: settings.sweepAngle, min: -360, max: 360, step: 1, onChange: (sweepAngle) => updateRevolve({ sweepAngle }) },
{ label: "Sides", value: settings.sides, min: 3, max: 128, step: 1, onChange: (sides) => updateRevolve({ sides }) },
{ label: "Thickness", value: settings.thickness, min: 0.1, max: 20, step: 0.1, onChange: (thickness) => updateRevolve({ thickness }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
];
}
2026-06-14 22:05:58 +03:00
if (shape.kind === "box") {
return [
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
2026-06-15 12:56:23 +03:00
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
2026-06-14 22:05:58 +03:00
];
}
if (shape.kind === "cylinder") {
return [
{ label: "Sides", value: shape.sides ?? 96, min: 3, max: 128, step: 1, onChange: (sides) => onUpdate({ sides: Math.round(sides) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
2026-06-15 12:56:23 +03:00
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
2026-06-14 22:05:58 +03:00
];
}
if (shape.kind === "sphere") {
return [
{ label: "Steps", value: shape.steps ?? 24, min: 6, max: 64, step: 1, onChange: (steps) => onUpdate({ steps: Math.round(steps) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
2026-06-15 12:56:23 +03:00
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
2026-06-14 22:05:58 +03:00
];
}
if (shape.kind === "halfSphere") {
return [
{ label: "Steps", value: shape.steps ?? 32, min: 6, max: 64, step: 1, onChange: (steps) => onUpdate({ steps: Math.round(steps) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
2026-06-15 12:56:23 +03:00
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
2026-06-14 22:05:58 +03:00
];
}
if (shape.kind === "cone") {
return [
{ label: "Top Radius", value: shape.topRadius ?? 0, min: 0, max: 40, onChange: (topRadius) => onUpdate({ topRadius }) },
{ label: "Base Radius", value: shape.baseRadius ?? width / 2, min: MIN_SHAPE_SIZE, max: 80, onChange: setBaseRadius },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setConeWidth },
2026-06-15 12:56:23 +03:00
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
2026-06-14 22:05:58 +03:00
{ label: "Sides", value: shape.sides ?? 96, min: 3, max: 128, step: 1, onChange: (sides) => onUpdate({ sides: Math.round(sides) }) },
];
}
if (shape.kind === "pyramid") {
return [
{ label: "Sides", value: shape.sides ?? 4, min: 3, max: 24, step: 1, onChange: (sides) => onUpdate({ sides: Math.round(sides) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
2026-06-15 12:56:23 +03:00
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
2026-06-14 22:05:58 +03:00
];
}
if (shape.kind === "roundRoof") {
return [
{ label: "Sides", value: shape.sides ?? 64, min: 4, max: 128, step: 1, onChange: (sides) => onUpdate({ sides: Math.round(sides) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
2026-06-15 12:56:23 +03:00
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
2026-06-14 22:05:58 +03:00
];
}
if (shape.kind === "tube" || shape.kind === "ring") {
return [
{ label: "Thickness", value: shape.bevel ?? 4, min: 0.5, max: 20, onChange: (bevel) => onUpdate({ bevel }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
2026-06-15 12:56:23 +03:00
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
2026-06-14 22:05:58 +03:00
];
}
if (shape.kind === "gear") {
const setGearWidth = (value: number) => {
const toothSize = normalizeGearToothSize(shape.toothSize, value, depth);
const toothWidth = normalizeGearToothWidth(shape.toothWidth, value, depth, shape.teeth);
const centerHoleSize = normalizeGearCenterHoleSize(shape.centerHoleSize, value, depth, toothSize);
onUpdate({ width: value, size: resizedShapeSize(value, depth), toothSize, toothWidth, centerHoleSize }, { resizeAxis: "width" });
};
const setGearDepth = (value: number) => {
const toothSize = normalizeGearToothSize(shape.toothSize, width, value);
const toothWidth = normalizeGearToothWidth(shape.toothWidth, width, value, shape.teeth);
const centerHoleSize = normalizeGearCenterHoleSize(shape.centerHoleSize, width, value, toothSize);
onUpdate({ depth: value, size: resizedShapeSize(width, value), toothSize, toothWidth, centerHoleSize }, { resizeAxis: "depth" });
};
const teeth = shape.teeth ?? DEFAULT_GEAR_TEETH;
const toothPitch = gearToothPitch(width, depth, teeth);
const toothSize = normalizeGearToothSize(shape.toothSize ?? DEFAULT_GEAR_TOOTH_SIZE, width, depth);
const centerHoleLimits = gearCenterHoleLimits(width, depth, toothSize);
const properties: ShapePropertyConfig[] = [
{
label: "Teeth",
value: teeth,
min: 6,
max: 64,
step: 1,
onChange: (value) => {
const nextTeeth = Math.round(value);
onUpdate({
teeth: nextTeeth,
toothWidth: normalizeGearToothWidth(shape.toothWidth, width, depth, nextTeeth),
});
},
},
{
label: "Tooth Size",
value: toothSize,
min: 0.2,
max: Math.max(0.2, Math.min(width, depth) * 0.22),
step: 0.1,
onChange: (nextToothSize) => onUpdate({
toothSize: nextToothSize,
centerHoleSize: normalizeGearCenterHoleSize(shape.centerHoleSize, width, depth, nextToothSize),
}),
},
{
label: "Tooth Width",
value: normalizeGearToothWidth(shape.toothWidth, width, depth, teeth),
min: toothPitch * 0.12,
max: toothPitch * 0.82,
step: 0.1,
onChange: (toothWidth) => onUpdate({ toothWidth }),
},
];
if (normalizeGearType(shape.gearType) === "helical") {
properties.push({
label: "Helix Angle",
value: normalizeGearHelixAngle(shape.helixAngle ?? DEFAULT_GEAR_HELIX_ANGLE),
min: MIN_GEAR_HELIX_ANGLE,
max: MAX_GEAR_HELIX_ANGLE,
step: 1,
onChange: (helixAngle) => onUpdate({ helixAngle }),
});
properties.push({
label: "Quality",
value: normalizeGearHelixQuality(shape.helixQuality ?? DEFAULT_GEAR_HELIX_QUALITY),
min: MIN_GEAR_HELIX_QUALITY,
max: MAX_GEAR_HELIX_QUALITY,
step: 1,
onChange: (helixQuality) => onUpdate({ helixQuality: Math.round(helixQuality) }),
});
}
properties.push(
{
label: "Center Hole",
value: normalizeGearCenterHoleSize(shape.centerHoleSize, width, depth, toothSize),
min: centerHoleLimits.min,
max: centerHoleLimits.max,
step: 0.1,
onChange: (centerHoleSize) => onUpdate({ centerHoleSize }),
},
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setGearDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setGearWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
);
return properties;
}
2026-06-14 22:05:58 +03:00
if (shape.kind === "text") {
return [
{
type: "text",
label: "Text",
value: shape.text ?? "TEXT",
onChange: (text) => {
const nextText = text.slice(0, 24) || " ";
const nextWidth = clamp(Math.max(46, nextText.length * 19), 46, 260);
onUpdate({ text: nextText, width: nextWidth, size: nextWidth });
},
},
{ type: "select", label: "Font", value: shape.font ?? "Multilanguage", options: TEXT_FONT_OPTIONS, onChange: (font) => onUpdate({ font }) },
2026-06-15 12:56:23 +03:00
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 40, onChange: setHeight },
2026-06-14 22:05:58 +03:00
{ label: "Bevel", value: shape.bevel ?? 0, min: 0, max: 8, onChange: (bevel) => onUpdate({ bevel }) },
{ label: "Segments", value: shape.segments ?? 0, min: 0, max: 24, step: 1, onChange: (segments) => onUpdate({ segments: Math.round(segments) }) },
];
}
return [
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
2026-06-15 12:56:23 +03:00
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
2026-06-14 22:05:58 +03:00
];
}
export function ShapeInspector({
shape,
snap,
snapOpen,
2026-07-05 12:28:23 +03:00
workspace,
2026-06-14 22:05:58 +03:00
onUpdate,
onSnapChange,
onSnapOpenChange,
onEditSketch,
2026-07-05 12:28:23 +03:00
canSeparateParts = false,
onSeparateParts,
onInteractionActiveChange,
2026-06-14 22:05:58 +03:00
}: {
shape: WorkplaneShape;
snap: GridSize;
snapOpen: boolean;
2026-07-05 12:28:23 +03:00
workspace: WorkplaneWorkspaceSettings;
2026-06-15 12:56:23 +03:00
onUpdate: ShapeInspectorUpdate;
2026-06-14 22:05:58 +03:00
onSnapChange: Dispatch<SetStateAction<GridSize>>;
onSnapOpenChange: Dispatch<SetStateAction<boolean>>;
onEditSketch?: () => void;
2026-07-05 12:28:23 +03:00
canSeparateParts?: boolean;
onSeparateParts?: () => void;
onInteractionActiveChange?: (active: boolean) => void;
2026-06-14 22:05:58 +03:00
}) {
const solidColor = shape.hole ? fallbackSolidColor(shape) : shape.color;
const locked = Boolean(shape.locked);
const properties = getShapeProperties(shape, onUpdate);
const gearType = shape.kind === "gear" ? normalizeGearType(shape.gearType) : null;
const primaryProperties = shape.kind === "gear"
? properties.filter((property) => ["Center Hole", "Length", "Width", "Height"].includes(property.label))
: properties;
const gearTeethProperties = shape.kind === "gear"
? properties.filter((property) => ["Teeth", "Tooth Size", "Tooth Width"].includes(property.label))
: [];
const gearHelixProperties = shape.kind === "gear"
? properties.filter((property) => ["Helix Angle", "Quality"].includes(property.label))
: [];
const isSketchRevolve = shape.sketchOperation === "revolve" || Boolean(shape.sketchRevolve);
const inspectorRef = useRef<HTMLElement>(null);
2026-06-14 22:05:58 +03:00
const [propertiesOpen, setPropertiesOpen] = useState(true);
const [gearTeethOpen, setGearTeethOpen] = useState(true);
const [gearHelixOpen, setGearHelixOpen] = useState(true);
2026-06-14 22:05:58 +03:00
const [colorOpen, setColorOpen] = useState(false);
const [minimized, setMinimized] = useState(false);
const customColorInputRef = useRef<HTMLInputElement>(null);
2026-06-14 22:05:58 +03:00
useEffect(() => () => onInteractionActiveChange?.(false), [onInteractionActiveChange]);
useEffect(() => {
const input = customColorInputRef.current;
if (!colorOpen || !input) {
return;
}
// React's color-input onChange follows the native input event and fires for
// every movement in the picker. Commit only the native change event, which
// fires after the user finishes choosing, so dragging stays responsive.
const commitCustomColor = () => {
onUpdate({ color: input.value, hole: false });
};
input.addEventListener("change", commitCustomColor);
return () => input.removeEventListener("change", commitCustomColor);
}, [colorOpen, onUpdate]);
useLayoutEffect(() => {
inspectorRef.current?.scrollTo({ top: 0, left: 0 });
}, [isSketchRevolve, shape.id]);
2026-06-14 22:05:58 +03:00
return (
<aside ref={inspectorRef} className={`shape-inspector ${isSketchRevolve ? "sketch-revolve-inspector" : ""} ${shape.kind === "gear" ? "gear-inspector" : ""} ${minimized ? "minimized" : ""}`} aria-label={`${shape.name} shape settings`} onPointerDown={(event) => event.stopPropagation()}>
2026-06-14 22:05:58 +03:00
<div className="shape-inspector-header">
<button
className="inspector-header-icon"
aria-label={minimized ? "Expand shape settings" : "Minimize shape settings"}
aria-expanded={!minimized}
onClick={() => setMinimized((current) => !current)}
>
{minimized ? <ChevronDown size={26} strokeWidth={2.8} /> : <ChevronUp size={26} strokeWidth={2.8} />}
2026-06-14 22:05:58 +03:00
</button>
<strong>{shape.name}</strong>
<div className="inspector-header-actions">
<button className={locked ? "inspector-header-icon active" : "inspector-header-icon"} aria-label={locked ? "Unlock shape" : "Lock shape"} onClick={() => onUpdate({ locked: !locked })}>
{locked ? <LockKeyhole size={31} strokeWidth={2.4} /> : <LockKeyholeOpen size={31} strokeWidth={2.4} />}
</button>
<button className={shape.hidden ? "inspector-header-icon active" : "inspector-header-icon"} aria-label={shape.hidden ? "Show shape" : "Hide shape"} onClick={() => onUpdate({ hidden: !shape.hidden })}>
<ToolbarHideSelectedIcon />
</button>
</div>
</div>
{!minimized ? (
<>
2026-06-14 22:05:58 +03:00
<div className="shape-state-card" role="group" aria-label="Shape mode">
<button
className={!shape.hole ? "active solid-choice" : "solid-choice"}
onClick={() => {
const wasHole = Boolean(shape.hole);
onUpdate({ hole: false, color: solidColor });
setColorOpen((open) => (wasHole ? false : !open));
}}
disabled={locked}
aria-pressed={!shape.hole}
aria-expanded={colorOpen}
>
<span className="large-solid-swatch" style={{ "--swatch": solidColor } as CSSProperties} />
<span>Solid</span>
</button>
<button
className={shape.hole ? "active hole-choice" : "hole-choice"}
onClick={() => {
onUpdate({ hole: true, color: "#b8c2cc" });
setColorOpen(false);
}}
disabled={locked}
aria-pressed={shape.hole}
>
<span className="large-hole-swatch" />
<span>Hole</span>
</button>
</div>
{colorOpen ? (
<div className="color-card" aria-label="Shape color">
<div className="color-card-header">
<span>Color</span>
<span className="color-value">{solidColor.toUpperCase()}</span>
</div>
<div className="color-grid">
{SOLID_COLORS.map((color) => (
<button
key={color}
className={solidColor.toLowerCase() === color.toLowerCase() && !shape.hole ? "selected" : ""}
type="button"
style={{ "--shape-swatch": color } as CSSProperties}
title={color.toUpperCase()}
aria-label={`Set color ${color}`}
disabled={locked}
onClick={() => {
onUpdate({ color, hole: false });
setColorOpen(false);
}}
/>
))}
<label className={locked ? "custom-color disabled" : "custom-color"} title="Custom color">
<input
key={`${shape.id}-${solidColor}`}
ref={customColorInputRef}
2026-06-14 22:05:58 +03:00
type="color"
defaultValue={solidColor}
2026-06-14 22:05:58 +03:00
disabled={locked}
onFocus={() => onInteractionActiveChange?.(true)}
onBlur={() => onInteractionActiveChange?.(false)}
2026-06-14 22:05:58 +03:00
/>
<span>Custom</span>
</label>
</div>
</div>
) : null}
{shape.sketchProfile && onEditSketch ? (
<button className="edit-sketch-button" type="button" disabled={locked} onClick={onEditSketch}>
Edit sketch
</button>
) : null}
2026-07-05 12:28:23 +03:00
{canSeparateParts && onSeparateParts ? (
<button className="inspector-action-button" type="button" disabled={locked} onClick={onSeparateParts}>
<Split size={17} strokeWidth={2.5} />
<span>Separate Parts</span>
</button>
) : null}
<div className={`property-card ${propertiesOpen ? "" : "collapsed"}`}>
2026-06-14 22:05:58 +03:00
<button
className="property-card-header"
type="button"
aria-expanded={propertiesOpen}
aria-controls={`properties-${shape.id}`}
onClick={() => setPropertiesOpen((open) => !open)}
>
<span>Properties</span>
<ChevronUp className={propertiesOpen ? "" : "collapsed"} size={25} strokeWidth={2.8} />
</button>
{propertiesOpen ? (
<div className="property-list" id={`properties-${shape.id}`}>
{gearType ? (
<GearTypeSelector
value={gearType}
disabled={locked}
onChange={(gearType) => onUpdate({ gearType })}
/>
) : null}
<ShapePropertyRows properties={primaryProperties} workspace={workspace} disabled={locked} onInteractionActiveChange={onInteractionActiveChange} />
2026-06-14 22:05:58 +03:00
</div>
) : null}
</div>
{shape.kind === "gear" ? (
<div className={`property-card ${gearTeethOpen ? "" : "collapsed"}`}>
<button
className="property-card-header"
type="button"
aria-expanded={gearTeethOpen}
aria-controls={`gear-teeth-${shape.id}`}
onClick={() => setGearTeethOpen((open) => !open)}
>
<span>Teeth</span>
<ChevronUp className={gearTeethOpen ? "" : "collapsed"} size={25} strokeWidth={2.8} />
</button>
{gearTeethOpen ? (
<div className="property-list" id={`gear-teeth-${shape.id}`}>
<ShapePropertyRows properties={gearTeethProperties} workspace={workspace} disabled={locked} onInteractionActiveChange={onInteractionActiveChange} />
</div>
) : null}
</div>
) : null}
{gearType === "helical" ? (
<div className={`property-card ${gearHelixOpen ? "" : "collapsed"}`}>
<button
className="property-card-header"
type="button"
aria-expanded={gearHelixOpen}
aria-controls={`gear-helix-${shape.id}`}
onClick={() => setGearHelixOpen((open) => !open)}
>
<span>Helix</span>
<ChevronUp className={gearHelixOpen ? "" : "collapsed"} size={25} strokeWidth={2.8} />
</button>
{gearHelixOpen ? (
<div className="property-list" id={`gear-helix-${shape.id}`}>
<ShapePropertyRows properties={gearHelixProperties} workspace={workspace} disabled={locked} onInteractionActiveChange={onInteractionActiveChange} />
</div>
) : null}
</div>
) : null}
2026-06-14 22:05:58 +03:00
<div className="inspector-snap-dock">
<SnapGridControl snap={snap} snapOpen={snapOpen} onSnapChange={onSnapChange} onSnapOpenChange={onSnapOpenChange} />
</div>
</>
) : null}
2026-06-14 22:05:58 +03:00
</aside>
);
}
function ShapePropertyRows({
properties,
workspace,
disabled,
onInteractionActiveChange,
}: {
properties: ShapePropertyConfig[];
workspace: WorkplaneWorkspaceSettings;
disabled?: boolean;
onInteractionActiveChange?: (active: boolean) => void;
}) {
return properties.map((property) => {
if (property.type === "text") {
return <TextProperty key={property.label} {...property} disabled={disabled} onInteractionActiveChange={onInteractionActiveChange} />;
}
if (property.type === "select") {
return <SelectProperty key={property.label} {...property} disabled={disabled} />;
}
return <RangeProperty key={property.label} {...property} workspace={workspace} disabled={disabled} onInteractionActiveChange={onInteractionActiveChange} />;
});
}
2026-06-14 22:05:58 +03:00
export function SnapGridControl({
snap,
snapOpen,
onSnapChange,
onSnapOpenChange,
}: {
snap: GridSize;
snapOpen: boolean;
onSnapChange: Dispatch<SetStateAction<GridSize>>;
onSnapOpenChange: Dispatch<SetStateAction<boolean>>;
}) {
return (
<div className="snap-row">
<span>Snap Grid</span>
<button className="snap-select" onClick={() => onSnapOpenChange((value) => !value)}>
{snap}
<ChevronDown size={12} fill="currentColor" />
</button>
{snapOpen ? (
<div className="snap-menu">
{GRID_SIZES.map((size) => (
<button
key={size}
className={size === snap ? "selected" : ""}
onClick={() => {
onSnapChange(size);
onSnapOpenChange(false);
}}
>
{size}
</button>
))}
</div>
) : null}
</div>
);
}
function RangeProperty({
label,
value,
min,
max,
step = 0.01,
2026-07-05 12:28:23 +03:00
workspace,
disabled,
onChange,
onInteractionActiveChange,
}: RangePropertyConfig & { workspace: WorkplaneWorkspaceSettings; disabled?: boolean; onInteractionActiveChange?: (active: boolean) => void }) {
const allowsAboveSliderMax = label === "Length" || label === "Width" || label === "Height";
2026-07-05 12:28:23 +03:00
const isLength = propertyUsesLengthUnit(label);
const accuracy = workspace.accuracy;
const actualValue = Math.max(min, Number.isFinite(value) ? value : min);
2026-07-05 12:28:23 +03:00
const controlValue = isLength ? millimetersToDisplay(actualValue, workspace) : actualValue;
const controlMin = isLength ? millimetersToDisplay(min, workspace) : min;
const controlMax = isLength ? millimetersToDisplay(max, workspace) : max;
const controlStep = isLength ? displayStepFromMillimeters(step, workspace) : step;
const sliderValue = clamp(controlValue, controlMin, controlMax);
const position = ((sliderValue - controlMin) / Math.max(Number.EPSILON, controlMax - controlMin)) * 100;
2026-06-14 22:05:58 +03:00
const [editing, setEditing] = useState(false);
2026-07-05 12:28:23 +03:00
const [draft, setDraft] = useState(formatPropertyNumber(controlValue, accuracy, controlStep));
const unit = isLength ? lengthDisplayUnit(workspace).label : null;
useEffect(() => {
if (!editing) {
2026-07-05 12:28:23 +03:00
setDraft(formatPropertyNumber(controlValue, accuracy, controlStep));
}
2026-07-05 12:28:23 +03:00
}, [accuracy, controlStep, controlValue, editing]);
const toModelValue = (nextValue: number) => isLength ? displayToMillimeters(nextValue, workspace) : nextValue;
2026-06-14 22:05:58 +03:00
const commitDraft = () => {
const next = parseMeasurementInput(draft);
2026-07-05 12:28:23 +03:00
const finiteNext = Number.isFinite(next) ? next : controlValue;
const nextModelValue = toModelValue(finiteNext);
onChange(allowsAboveSliderMax ? Math.max(min, nextModelValue) : clamp(nextModelValue, min, max));
2026-06-14 22:05:58 +03:00
setEditing(false);
onInteractionActiveChange?.(false);
2026-06-14 22:05:58 +03:00
};
2026-07-05 12:28:23 +03:00
const handleSliderChange = (nextValue: number) => {
const next = clamp(Number.isFinite(nextValue) ? nextValue : controlMin, controlMin, controlMax);
onChange(clamp(toModelValue(next), min, max));
setDraft(formatPropertyNumber(next, accuracy, controlStep));
};
2026-06-14 22:05:58 +03:00
return (
<label className="range-property" style={{ "--slider-pos": `${position}%` } as CSSProperties}>
2026-07-05 12:28:23 +03:00
<span className="range-property-header">
<span className="range-property-name">{label}</span>
<span className="range-value-control">
2026-06-14 22:05:58 +03:00
<input
type="text"
2026-07-05 12:28:23 +03:00
value={editing ? draft : formatPropertyNumber(controlValue, accuracy, controlStep)}
disabled={disabled}
inputMode="decimal"
onFocus={() => {
onInteractionActiveChange?.(true);
2026-07-05 12:28:23 +03:00
setDraft(formatPropertyNumber(controlValue, accuracy, controlStep));
setEditing(true);
}}
2026-06-14 22:05:58 +03:00
onChange={(event) => setDraft(event.currentTarget.value)}
onBlur={commitDraft}
onKeyDown={(event) => {
if (event.key === "Enter") {
event.currentTarget.blur();
} else if (event.key === "Escape") {
2026-07-05 12:28:23 +03:00
setDraft(formatPropertyNumber(controlValue, accuracy, controlStep));
2026-06-14 22:05:58 +03:00
setEditing(false);
}
}}
/>
2026-07-05 12:28:23 +03:00
{unit ? <span className="range-value-unit">{unit}</span> : null}
</span>
</span>
<div className="range-control">
2026-06-14 22:05:58 +03:00
<input
type="range"
2026-07-05 12:28:23 +03:00
min={controlMin}
max={controlMax}
step={controlStep}
value={sliderValue}
2026-06-14 22:05:58 +03:00
disabled={disabled}
onFocus={() => onInteractionActiveChange?.(true)}
onBlur={() => onInteractionActiveChange?.(false)}
onPointerDown={() => onInteractionActiveChange?.(true)}
onPointerUp={() => onInteractionActiveChange?.(false)}
onPointerCancel={() => onInteractionActiveChange?.(false)}
2026-07-05 12:28:23 +03:00
onChange={(event) => handleSliderChange(Number(event.currentTarget.value))}
2026-06-14 22:05:58 +03:00
/>
</div>
</label>
);
}
function TextProperty({ label, value, disabled, onChange, onInteractionActiveChange }: TextPropertyConfig & { disabled?: boolean; onInteractionActiveChange?: (active: boolean) => void }) {
2026-06-14 22:05:58 +03:00
return (
<label className="text-property">
<span>{label}</span>
<input
type="text"
value={value}
disabled={disabled}
maxLength={24}
spellCheck={false}
onFocus={() => onInteractionActiveChange?.(true)}
onBlur={() => onInteractionActiveChange?.(false)}
2026-06-14 22:05:58 +03:00
onChange={(event) => onChange(event.currentTarget.value)}
/>
</label>
);
}
function SelectProperty({ label, value, options, disabled, onChange }: SelectPropertyConfig & { disabled?: boolean }) {
return (
<label className="select-property">
<span>{label}</span>
<select value={value} disabled={disabled} onChange={(event) => onChange(event.currentTarget.value)}>
{options.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
</label>
);
}
function GearTypePreview({ type }: { type: GearType }) {
return <img src={`assets/sketchforge/gear-types/${type}.png`} alt="" aria-hidden="true" />;
}
function GearTypeSelector({ value, disabled, onChange }: { value: GearType; disabled?: boolean; onChange: (value: GearType) => void }) {
return (
<div className="gear-type-property" role="group" aria-label="Gear type">
<span>Gear Type</span>
<div className="gear-type-options">
{GEAR_TYPE_OPTIONS.map((option) => (
<button
key={option.value}
className={value === option.value ? "selected" : ""}
type="button"
disabled={disabled}
aria-pressed={value === option.value}
onClick={() => onChange(option.value)}
>
<GearTypePreview type={option.value} />
<span>{option.label}</span>
</button>
))}
</div>
</div>
);
}