Improve editor history, CAD tools, and ruler workflows

This commit is contained in:
Formsmith
2026-07-14 09:43:37 +03:00
parent 20731043e4
commit 400c4bf86e
23 changed files with 1727 additions and 388 deletions
+5
View File
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import {
CAD_MODIFIER_MAX_SHARP_ANGLE,
CAD_MODIFIER_REQUEST_TIMEOUT_MS,
CAD_MODIFIER_RUNTIME_BASE,
cadModifierTimeoutMessage,
@@ -22,4 +23,8 @@ describe("CAD modifier runtime state", () => {
expect(CAD_MODIFIER_REQUEST_TIMEOUT_MS).toBeLessThanOrEqual(60_000);
expect(cadModifierTimeoutMessage("prepare")).toContain("Firefox 121+");
});
it("does not expose thresholds above the worker's folded edge-angle range", () => {
expect(CAD_MODIFIER_MAX_SHARP_ANGLE).toBe(90);
});
});
+46 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { restoreShapeBeforeEdgeTreatment } from "@/lib/edgeTreatmentHistory";
import { compactEdgeTreatmentHistory, edgeTreatmentAppliedFrame, restoreShapeBeforeEdgeTreatment } from "@/lib/edgeTreatmentHistory";
import type { EdgeTreatmentHistoryEntry, WorkplaneShape } from "@/types/sketchforge";
function box(overrides: Partial<WorkplaneShape> = {}): WorkplaneShape {
@@ -116,4 +116,49 @@ describe("edge treatment history restoration", () => {
expect(restored).toMatchObject({ kind: "box", x: 18, width: 8, depth: 6, height: 50, size: 8 });
expect(restored.importedMesh).toBeUndefined();
});
it("keeps history snapshots flat instead of recursively nesting older history", () => {
const first = historyEntry(box({ id: "before-first" }));
const second = {
...historyEntry(box({ id: "before-second", edgeTreatmentHistory: [first] })),
id: "history-2",
};
const compacted = compactEdgeTreatmentHistory([first, second]);
expect(compacted).toHaveLength(2);
expect(compacted[0].before.edgeTreatmentHistory).toBeUndefined();
expect(compacted[1].before.edgeTreatmentHistory).toBeUndefined();
});
it("preserves edits made after applying a feature while restoring the pre-feature geometry", () => {
const before = box({ x: 8, z: 3, width: 40, depth: 20, height: 30, rotation: 15 });
const applied = box({ kind: "mesh", x: 7.5, z: 3, width: 39, depth: 20, height: 30, rotation: 0 });
const entry: EdgeTreatmentHistoryEntry = {
...historyEntry(before),
appliedFrame: edgeTreatmentAppliedFrame(applied),
};
const current = box({
...applied,
x: 17.5,
z: -2,
width: 78,
depth: 30,
height: 45,
rotation: 20,
edgeTreatmentHistory: [entry],
});
const restored = restoreShapeBeforeEdgeTreatment(current, entry);
expect(restored).toMatchObject({
kind: "box",
x: 18,
z: -2,
width: 80,
depth: 30,
height: 45,
rotation: 35,
});
});
});
+110
View File
@@ -0,0 +1,110 @@
import { describe, expect, it } from "vitest";
import { appendEditorHistorySnapshot, boundedEditorHistory, editorHistoryEntry, hydrateEditorHistoryState, projectShapesFingerprint } from "@/lib/editorHistory";
import type { WorkplaneShape } from "@/types/sketchforge";
function box(overrides: Partial<WorkplaneShape> = {}): WorkplaneShape {
return {
id: "box-1",
name: "Box",
kind: "box",
color: "#d41721",
x: 0,
z: 0,
elevation: 0,
size: 20,
width: 20,
depth: 20,
height: 20,
rotation: 0,
locked: false,
hidden: false,
...overrides,
};
}
describe("editor history snapshots", () => {
it("seeds history with the real loaded scene and valid selection", () => {
const shape = box();
const entry = editorHistoryEntry([shape], [shape.id, "missing", shape.id]);
expect(entry.shapes).toHaveLength(1);
expect(entry.shapes[0].id).toBe(shape.id);
expect(entry.selectedIds).toEqual([shape.id]);
});
it("detects persistence-relevant fields that the old fingerprint omitted", () => {
const shape = box({ kind: "cylinder", sides: 32 });
const baseline = projectShapesFingerprint([shape]);
expect(projectShapesFingerprint([{ ...shape, locked: true }])).not.toBe(baseline);
expect(projectShapesFingerprint([{ ...shape, hidden: true }])).not.toBe(baseline);
expect(projectShapesFingerprint([{ ...shape, sides: 64 }])).not.toBe(baseline);
});
it("detects mesh coordinate changes even when array lengths are unchanged", () => {
const shape = box({
kind: "mesh",
importedMesh: {
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
baseWidth: 1,
baseDepth: 1,
baseHeight: 1,
triangleCount: 1,
sourceFormat: "json",
},
});
const changed = {
...shape,
importedMesh: { ...shape.importedMesh!, positions: [0, 0, 0, 2, 0, 0, 0, 1, 0] },
};
expect(projectShapesFingerprint([changed])).not.toBe(projectShapesFingerprint([shape]));
});
it("bounds entry count and estimated memory while retaining undo depth", () => {
const entries = Array.from({ length: 140 }, (_, index) => ({
...editorHistoryEntry([box({ id: `box-${index}`, x: index })], []),
estimatedBytes: 2 * 1024 * 1024,
}));
const bounded = boundedEditorHistory(entries);
expect(bounded.length).toBeLessThanOrEqual(100);
expect(bounded.length).toBeGreaterThanOrEqual(2);
expect(bounded.reduce((total, entry) => total + entry.estimatedBytes, 0)).toBeLessThanOrEqual(64 * 1024 * 1024);
expect(bounded.at(-1)?.shapes[0].id).toBe("box-139");
});
it("trims redo only for a real new edit and preserves it for a no-op", () => {
const entries = [0, 1, 2].map((x) => editorHistoryEntry([box({ x })], []));
const noOp = appendEditorHistorySnapshot(entries, 1, editorHistoryEntry([box({ x: 1 })], ["box-1"]));
expect(noOp.changed).toBe(false);
expect(noOp.entries).toHaveLength(3);
expect(noOp.entries[1].selectedIds).toEqual(["box-1"]);
const branch = appendEditorHistorySnapshot(noOp.entries, 1, editorHistoryEntry([box({ x: 5 })], []));
expect(branch.changed).toBe(true);
expect(branch.entries).toHaveLength(3);
expect(branch.entries.at(-1)?.shapes[0].x).toBe(5);
});
it("restores a persisted undo and redo stack at its saved index", () => {
const entries = [0, 5, 10].map((x) => editorHistoryEntry([box({ x })], []));
const restored = hydrateEditorHistoryState([box({ x: 5 })], entries, 1);
expect(restored.index).toBe(1);
expect(restored.entries).toHaveLength(3);
expect(restored.entries[restored.index].shapes[0].x).toBe(5);
expect(restored.entries[0].shapes[0].x).toBe(0);
expect(restored.entries[2].shapes[0].x).toBe(10);
});
it("falls back to the loaded scene when persisted history is stale", () => {
const stale = [editorHistoryEntry([box({ x: 1 })], [])];
const restored = hydrateEditorHistoryState([box({ x: 9 })], stale, 0);
expect(restored.index).toBe(0);
expect(restored.entries).toHaveLength(1);
expect(restored.entries[0].shapes[0].x).toBe(9);
});
});
+15
View File
@@ -0,0 +1,15 @@
import { describe, expect, it } from "vitest";
import { sphereTessellation } from "@/lib/sphereTessellation";
describe("sphere tessellation", () => {
it("uses the visible sphere Steps value for both rendering and boolean operands", () => {
expect(sphereTessellation(6)).toEqual({ widthSegments: 12, heightSegments: 6 });
expect(sphereTessellation(24)).toEqual({ widthSegments: 48, heightSegments: 24 });
expect(sphereTessellation(64)).toEqual({ widthSegments: 128, heightSegments: 64 });
});
it("uses the catalog default and normalizes fractional input", () => {
expect(sphereTessellation()).toEqual({ widthSegments: 48, heightSegments: 24 });
expect(sphereTessellation(27.6)).toEqual({ widthSegments: 56, heightSegments: 28 });
});
});
+14 -1
View File
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import type { WorkplaneWorkspaceSettings } from "@/types/sketchforge";
import { formatMeasurementNumber, lengthDisplayUnit, millimetersToDisplay, normalizeScaleForUnits, scaleOptionsForUnits } from "@/lib/measurementUnits";
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, workplaneSettingsFingerprint } from "@/lib/workplaneSettings";
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, workplaneSettingsFingerprint, workspaceHydrationSyncDecision } from "@/lib/workplaneSettings";
describe("workplane settings helpers", () => {
it("accepts known snap grid values and falls back for unknown values", () => {
@@ -88,4 +88,17 @@ describe("workplane settings helpers", () => {
expect(changedSnap).not.toBe(base);
expect(changedWorkspace).not.toBe(base);
});
it("blocks the previous project's workspace while a new project hydrates", () => {
const projectOne = workplaneSettingsFingerprint({ ...DEFAULT_WORKPLANE_WORKSPACE, width: 350, depth: 260 }, "5.0 mm");
const projectTwo = workplaneSettingsFingerprint(DEFAULT_WORKPLANE_WORKSPACE, "1.0 mm");
const staleCommit = workspaceHydrationSyncDecision(projectTwo, projectOne);
expect(staleCommit).toEqual({ shouldSync: false, pendingFingerprint: projectTwo });
const hydratedCommit = workspaceHydrationSyncDecision(staleCommit.pendingFingerprint, projectTwo);
expect(hydratedCommit).toEqual({ shouldSync: false, pendingFingerprint: null });
expect(workspaceHydrationSyncDecision(hydratedCommit.pendingFingerprint, projectTwo).shouldSync).toBe(true);
});
});
+21
View File
@@ -167,4 +167,25 @@ describe("workplane shape helpers", () => {
expect(resizedImportedMeshPositions({ ...modified, edgeResizeMode: "scale" })[3]).toBe(-18);
expect(resizedImportedCoordinates(modified, [-9, 1, 0, 9, 19, 0])).toEqual([-19, 1, 0, 19, 39, 0]);
});
it("uses child edge features when preserving the size of grouped treatments", () => {
const grouped = shape({
kind: "mesh",
edgeResizeMode: "preserve",
importedMesh: {
positions: [-10, 0, 0, -8, 2, 0, 8, 18, 0, 10, 20, 0],
baseWidth: 20,
baseDepth: 20,
baseHeight: 20,
triangleCount: 1,
sourceFormat: "json",
},
groupedShapes: [shape({ edgeTreatments: [{ kind: "fillet", amount: 2, edgeCount: 1 }] })],
width: 40,
height: 40,
});
expect(preservesEdgeTreatmentSize(grouped)).toBe(true);
expect(resizedImportedMeshPositions(grouped)).toEqual([-20, 0, 0, -18, 2, 0, 18, 38, 0, 20, 40, 0]);
});
});