@pixel-point/toolcraft 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +98 -0
- package/README.md +41 -0
- package/bin/create-toolcraft-app.mjs +8 -0
- package/bin/toolcraft.mjs +8 -0
- package/package.json +24 -0
- package/scripts/prepare-pack.mjs +29 -0
- package/src/cli.mjs +392 -0
- package/src/cli.test.mjs +284 -0
- package/src/copy-recursive.mjs +86 -0
- package/src/generate.mjs +212 -0
- package/src/generate.test.mjs +322 -0
- package/src/import-map.mjs +14 -0
- package/src/package-json.mjs +80 -0
- package/src/package-json.test.mjs +67 -0
- package/src/rewrite-imports.mjs +85 -0
- package/src/rewrite-imports.test.mjs +58 -0
- package/templates/runtime/contracts/component-contracts.test.ts +1165 -0
- package/templates/runtime/contracts/component-contracts.ts +1340 -0
- package/templates/runtime/contracts/decision-contracts.test.ts +206 -0
- package/templates/runtime/contracts/decision-contracts.ts +283 -0
- package/templates/runtime/contracts/index.test.ts +14 -0
- package/templates/runtime/contracts/index.ts +3 -0
- package/templates/runtime/contracts/types.ts +56 -0
- package/templates/runtime/export/export.test.ts +203 -0
- package/templates/runtime/export/export.ts +132 -0
- package/templates/runtime/export/index.ts +1 -0
- package/templates/runtime/index.ts +14 -0
- package/templates/runtime/react/canvas-shell.test.tsx +424 -0
- package/templates/runtime/react/canvas-shell.tsx +408 -0
- package/templates/runtime/react/control-renderers.ts +31 -0
- package/templates/runtime/react/controls-panel.test.tsx +3736 -0
- package/templates/runtime/react/controls-panel.tsx +2327 -0
- package/templates/runtime/react/curve-geometry.test.ts +70 -0
- package/templates/runtime/react/index.ts +15 -0
- package/templates/runtime/react/layer-tree.ts +96 -0
- package/templates/runtime/react/layers-panel.test.tsx +487 -0
- package/templates/runtime/react/layers-panel.tsx +1348 -0
- package/templates/runtime/react/media-file.ts +82 -0
- package/templates/runtime/react/panel-host-config.ts +80 -0
- package/templates/runtime/react/panel-host-geometry.test.ts +66 -0
- package/templates/runtime/react/panel-host-geometry.ts +109 -0
- package/templates/runtime/react/panel-host-types.ts +74 -0
- package/templates/runtime/react/panel-host.test.tsx +102 -0
- package/templates/runtime/react/panel-host.tsx +353 -0
- package/templates/runtime/react/runtime-public-api.test.tsx +132 -0
- package/templates/runtime/react/settings-transfer.test.ts +150 -0
- package/templates/runtime/react/settings-transfer.ts +279 -0
- package/templates/runtime/react/storage-key-migration.ts +48 -0
- package/templates/runtime/react/theme-runtime.tsx +177 -0
- package/templates/runtime/react/timeline-panel.test.tsx +668 -0
- package/templates/runtime/react/timeline-panel.tsx +2953 -0
- package/templates/runtime/react/toolbar-panel.test.tsx +212 -0
- package/templates/runtime/react/toolbar-panel.tsx +205 -0
- package/templates/runtime/react/toolcraft-app.integration.test.tsx +350 -0
- package/templates/runtime/react/toolcraft-app.test.tsx +339 -0
- package/templates/runtime/react/toolcraft-app.tsx +81 -0
- package/templates/runtime/react/toolcraft-root.test.tsx +347 -0
- package/templates/runtime/react/toolcraft-root.tsx +203 -0
- package/templates/runtime/react/use-toolcraft.ts +41 -0
- package/templates/runtime/schema/define-toolcraft.test.ts +1524 -0
- package/templates/runtime/schema/define-toolcraft.ts +1442 -0
- package/templates/runtime/schema/keyframe-capability.test.ts +90 -0
- package/templates/runtime/schema/keyframe-capability.ts +51 -0
- package/templates/runtime/schema/runtime-targets.ts +40 -0
- package/templates/runtime/schema/types.ts +370 -0
- package/templates/runtime/state/canvas-zoom.ts +8 -0
- package/templates/runtime/state/create-template-state.test.ts +242 -0
- package/templates/runtime/state/create-template-state.ts +95 -0
- package/templates/runtime/state/keyframe-evaluation.test.ts +141 -0
- package/templates/runtime/state/keyframe-evaluation.ts +203 -0
- package/templates/runtime/state/persistence.test.ts +217 -0
- package/templates/runtime/state/persistence.ts +511 -0
- package/templates/runtime/state/reducer.test.ts +937 -0
- package/templates/runtime/state/reducer.ts +1212 -0
- package/templates/runtime/state/timeline-readiness.ts +43 -0
- package/templates/runtime/state/types.ts +242 -0
- package/templates/runtime/styles.css +125 -0
- package/templates/runtime/testing/performance.test.ts +1058 -0
- package/templates/runtime/testing/performance.ts +1078 -0
- package/templates/starter/AGENTS.md +186 -0
- package/templates/starter/LICENSE.md +98 -0
- package/templates/starter/NOTICE.md +8 -0
- package/templates/starter/docs/toolcraft/README.md +41 -0
- package/templates/starter/docs/toolcraft/acceptance-testing.md +205 -0
- package/templates/starter/docs/toolcraft/agent-worklog.md +81 -0
- package/templates/starter/docs/toolcraft/assembly-workflow.md +206 -0
- package/templates/starter/docs/toolcraft/component-rules.md +299 -0
- package/templates/starter/docs/toolcraft/custom-controls.md +71 -0
- package/templates/starter/docs/toolcraft/decision-contract.md +71 -0
- package/templates/starter/docs/toolcraft/performance.md +112 -0
- package/templates/starter/docs/toolcraft/renderer-technique.md +48 -0
- package/templates/starter/docs/toolcraft/schema-reference.md +265 -0
- package/templates/starter/docs/toolcraft/workflow.md +87 -0
- package/templates/starter/e2e/app-browser-acceptance.spec.ts +785 -0
- package/templates/starter/e2e/app-controls.spec.ts +41 -0
- package/templates/starter/e2e/app-performance.spec.ts +326 -0
- package/templates/starter/e2e/canvas-handle-helpers.ts +244 -0
- package/templates/starter/e2e/performance-helpers.ts +612 -0
- package/templates/starter/e2e/product-observable-helpers.ts +170 -0
- package/templates/starter/index.html +12 -0
- package/templates/starter/package.json +52 -0
- package/templates/starter/playwright.config.ts +43 -0
- package/templates/starter/scripts/check-ai-skills.mjs +95 -0
- package/templates/starter/scripts/check-toolcraft-docs.mjs +159 -0
- package/templates/starter/scripts/check-toolcraft-integrity.mjs +232 -0
- package/templates/starter/scripts/run-vite-on-free-port.mjs +48 -0
- package/templates/starter/scripts/toolcraft-port.mjs +54 -0
- package/templates/starter/scripts/toolcraft-port.test.mjs +73 -0
- package/templates/starter/src/app/starter-acceptance.test.ts +5959 -0
- package/templates/starter/src/app/starter-acceptance.ts +2646 -0
- package/templates/starter/src/app/starter-performance.test.ts +1390 -0
- package/templates/starter/src/app/starter-performance.ts +12 -0
- package/templates/starter/src/app/starter-schema.test.ts +70 -0
- package/templates/starter/src/app/starter-schema.ts +15 -0
- package/templates/starter/src/main.tsx +18 -0
- package/templates/starter/src/router.tsx +16 -0
- package/templates/starter/src/routes/index.tsx +7 -0
- package/templates/starter/src/routes/root.tsx +19 -0
- package/templates/starter/src/styles.css +120 -0
- package/templates/starter/tsconfig.json +11 -0
- package/templates/starter/vite.config.ts +13 -0
- package/templates/ui/components/composites/accordion.tsx +73 -0
- package/templates/ui/components/composites/alert-dialog.tsx +190 -0
- package/templates/ui/components/composites/alert.tsx +74 -0
- package/templates/ui/components/composites/aspect-ratio.tsx +22 -0
- package/templates/ui/components/composites/avatar.tsx +98 -0
- package/templates/ui/components/composites/badge.tsx +69 -0
- package/templates/ui/components/composites/breadcrumb.tsx +106 -0
- package/templates/ui/components/composites/card.tsx +91 -0
- package/templates/ui/components/composites/combobox.tsx +486 -0
- package/templates/ui/components/composites/command.tsx +296 -0
- package/templates/ui/components/composites/context-menu.tsx +247 -0
- package/templates/ui/components/composites/dialog.tsx +282 -0
- package/templates/ui/components/composites/dropdown-menu.tsx +299 -0
- package/templates/ui/components/composites/empty.tsx +110 -0
- package/templates/ui/components/composites/hover-card.tsx +44 -0
- package/templates/ui/components/composites/index.ts +30 -0
- package/templates/ui/components/composites/menubar.tsx +214 -0
- package/templates/ui/components/composites/navigation-menu.tsx +167 -0
- package/templates/ui/components/composites/pagination.tsx +131 -0
- package/templates/ui/components/composites/progress.tsx +72 -0
- package/templates/ui/components/composites/radio-group.tsx +84 -0
- package/templates/ui/components/composites/resizable.tsx +42 -0
- package/templates/ui/components/composites/sheet.tsx +153 -0
- package/templates/ui/components/composites/sidebar-structural.tsx +310 -0
- package/templates/ui/components/composites/sidebar.tsx +431 -0
- package/templates/ui/components/composites/sonner.tsx +35 -0
- package/templates/ui/components/composites/spinner.tsx +43 -0
- package/templates/ui/components/composites/table.tsx +108 -0
- package/templates/ui/components/composites/tabs.tsx +83 -0
- package/templates/ui/components/control-layout/index.tsx +437 -0
- package/templates/ui/components/controls/actions/actions-control.tsx +139 -0
- package/templates/ui/components/controls/actions/index.ts +9 -0
- package/templates/ui/components/controls/anchor-grid/anchor-grid-control.tsx +107 -0
- package/templates/ui/components/controls/anchor-grid/index.ts +4 -0
- package/templates/ui/components/controls/boolean/boolean-controls.tsx +79 -0
- package/templates/ui/components/controls/boolean/index.ts +4 -0
- package/templates/ui/components/controls/channel-mixer/channel-mixer-control.tsx +95 -0
- package/templates/ui/components/controls/channel-mixer/index.ts +4 -0
- package/templates/ui/components/controls/channel-tabs/channel-tabs.tsx +42 -0
- package/templates/ui/components/controls/channel-tabs/index.ts +6 -0
- package/templates/ui/components/controls/code-textarea/code-textarea-control.tsx +90 -0
- package/templates/ui/components/controls/code-textarea/index.ts +4 -0
- package/templates/ui/components/controls/color/color-control.tsx +571 -0
- package/templates/ui/components/controls/color/color-picker-popover.tsx +104 -0
- package/templates/ui/components/controls/color/index.ts +41 -0
- package/templates/ui/components/controls/color/palette-control-data.ts +436 -0
- package/templates/ui/components/controls/color/palette-control.tsx +535 -0
- package/templates/ui/components/controls/color/style-guide-color-picker-channel-utils.ts +162 -0
- package/templates/ui/components/controls/color/style-guide-color-picker-interactions.ts +190 -0
- package/templates/ui/components/controls/color/style-guide-color-picker-logic.ts +485 -0
- package/templates/ui/components/controls/color/style-guide-color-picker-parts.tsx +710 -0
- package/templates/ui/components/controls/color/style-guide-color-picker.tsx +503 -0
- package/templates/ui/components/controls/control-types.ts +43 -0
- package/templates/ui/components/controls/curves/curve-geometry.ts +355 -0
- package/templates/ui/components/controls/curves/curve-graph.tsx +390 -0
- package/templates/ui/components/controls/curves/curves-control.tsx +445 -0
- package/templates/ui/components/controls/curves/index.ts +6 -0
- package/templates/ui/components/controls/file-drop/file-drop-control.tsx +191 -0
- package/templates/ui/components/controls/file-drop/index.ts +5 -0
- package/templates/ui/components/controls/font-picker/font-catalog.json +15360 -0
- package/templates/ui/components/controls/font-picker/font-catalog.ts +116 -0
- package/templates/ui/components/controls/font-picker/font-picker-control.tsx +1202 -0
- package/templates/ui/components/controls/font-picker/font-preview-loader.ts +336 -0
- package/templates/ui/components/controls/font-picker/index.ts +24 -0
- package/templates/ui/components/controls/font-picker/use-hover-intent.ts +46 -0
- package/templates/ui/components/controls/gradient/gradient-control-utils.ts +190 -0
- package/templates/ui/components/controls/gradient/gradient-control.tsx +612 -0
- package/templates/ui/components/controls/gradient/gradient-stop-list.tsx +400 -0
- package/templates/ui/components/controls/gradient/gradient-toolbar.tsx +152 -0
- package/templates/ui/components/controls/gradient/index.ts +4 -0
- package/templates/ui/components/controls/image-picker/image-picker-control.tsx +139 -0
- package/templates/ui/components/controls/image-picker/index.ts +7 -0
- package/templates/ui/components/controls/index.ts +192 -0
- package/templates/ui/components/controls/range-input/index.ts +4 -0
- package/templates/ui/components/controls/range-input/range-input-control.tsx +173 -0
- package/templates/ui/components/controls/range-slider/index.ts +4 -0
- package/templates/ui/components/controls/range-slider/range-slider-control.tsx +122 -0
- package/templates/ui/components/controls/range-slider/range-slider-value.ts +61 -0
- package/templates/ui/components/controls/segmented/index.ts +8 -0
- package/templates/ui/components/controls/segmented/segmented-control.tsx +94 -0
- package/templates/ui/components/controls/select/index.ts +4 -0
- package/templates/ui/components/controls/select/select-control.tsx +223 -0
- package/templates/ui/components/controls/slider/index.ts +4 -0
- package/templates/ui/components/controls/slider/slider-control.tsx +150 -0
- package/templates/ui/components/controls/slider/slider-value.ts +56 -0
- package/templates/ui/components/controls/text-input/index.ts +4 -0
- package/templates/ui/components/controls/text-input/text-input-control.tsx +158 -0
- package/templates/ui/components/controls/use-measured-element-width.ts +42 -0
- package/templates/ui/components/controls/vector/index.ts +8 -0
- package/templates/ui/components/controls/vector/vector-control.tsx +401 -0
- package/templates/ui/components/panel/index.ts +19 -0
- package/templates/ui/components/panel/panel-actions.tsx +165 -0
- package/templates/ui/components/panel/panel-header.tsx +61 -0
- package/templates/ui/components/panel/panel-icon-button.tsx +96 -0
- package/templates/ui/components/panel/panel-section.tsx +168 -0
- package/templates/ui/components/panel/panel-surface.tsx +206 -0
- package/templates/ui/components/panel/panel.tsx +210 -0
- package/templates/ui/components/primitives/animated-loader.tsx +61 -0
- package/templates/ui/components/primitives/button-group.tsx +134 -0
- package/templates/ui/components/primitives/button.tsx +429 -0
- package/templates/ui/components/primitives/checkbox.tsx +62 -0
- package/templates/ui/components/primitives/editable-slider-value-label.tsx +337 -0
- package/templates/ui/components/primitives/field.tsx +225 -0
- package/templates/ui/components/primitives/index.ts +82 -0
- package/templates/ui/components/primitives/input-group.tsx +298 -0
- package/templates/ui/components/primitives/input.tsx +61 -0
- package/templates/ui/components/primitives/internal/button-loading.tsx +178 -0
- package/templates/ui/components/primitives/label.tsx +16 -0
- package/templates/ui/components/primitives/popover.tsx +126 -0
- package/templates/ui/components/primitives/portal-layer-context.tsx +33 -0
- package/templates/ui/components/primitives/primitive-arrow-icon.tsx +38 -0
- package/templates/ui/components/primitives/scroll-fade-logic.ts +441 -0
- package/templates/ui/components/primitives/scroll-fade-render.tsx +75 -0
- package/templates/ui/components/primitives/scroll-fade-types.ts +41 -0
- package/templates/ui/components/primitives/scroll-fade.tsx +72 -0
- package/templates/ui/components/primitives/select.tsx +408 -0
- package/templates/ui/components/primitives/selection-state.ts +31 -0
- package/templates/ui/components/primitives/separator.tsx +21 -0
- package/templates/ui/components/primitives/slider/index.ts +4 -0
- package/templates/ui/components/primitives/slider/slider-interaction.tsx +96 -0
- package/templates/ui/components/primitives/slider/slider-parts.tsx +303 -0
- package/templates/ui/components/primitives/slider/slider-reset.ts +152 -0
- package/templates/ui/components/primitives/slider/slider-value.ts +114 -0
- package/templates/ui/components/primitives/slider/slider.tsx +511 -0
- package/templates/ui/components/primitives/switch.tsx +35 -0
- package/templates/ui/components/primitives/textarea.tsx +49 -0
- package/templates/ui/components/primitives/toggle-group.tsx +114 -0
- package/templates/ui/components/primitives/toggle.tsx +46 -0
- package/templates/ui/components/primitives/tooltip.tsx +100 -0
- package/templates/ui/hooks/use-mobile.ts +21 -0
- package/templates/ui/index.ts +31 -0
- package/templates/ui/lib/control-outline.ts +3 -0
- package/templates/ui/lib/input-control-style.ts +131 -0
- package/templates/ui/lib/style-guide-color-utils.ts +111 -0
- package/templates/ui/lib/utils.ts +6 -0
- package/templates/ui/styles.css +291 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { defineToolcraft } from "../schema/define-toolcraft";
|
|
6
|
+
import { CanvasShell } from "./canvas-shell";
|
|
7
|
+
import { ToolcraftRoot } from "./toolcraft-root";
|
|
8
|
+
import { TOOLCRAFT_THEME_PREFERENCE_STORAGE_KEY } from "./theme-runtime";
|
|
9
|
+
import { ToolbarPanel } from "./toolbar-panel";
|
|
10
|
+
import { useToolcraft } from "./use-toolcraft";
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
cleanup();
|
|
14
|
+
document.documentElement.classList.remove("dark");
|
|
15
|
+
document.documentElement.style.colorScheme = "";
|
|
16
|
+
window.localStorage.clear();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
function createSchema() {
|
|
20
|
+
return defineToolcraft({
|
|
21
|
+
canvas: { enabled: true },
|
|
22
|
+
panels: {
|
|
23
|
+
controls: {
|
|
24
|
+
sections: [
|
|
25
|
+
{
|
|
26
|
+
controls: {
|
|
27
|
+
opacity: {
|
|
28
|
+
defaultValue: 75,
|
|
29
|
+
target: "selectedLayer.opacity",
|
|
30
|
+
type: "slider",
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
title: "Controls",
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function StateProbe() {
|
|
42
|
+
const { dispatch, state } = useToolcraft();
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<>
|
|
46
|
+
<button
|
|
47
|
+
onClick={() =>
|
|
48
|
+
dispatch({
|
|
49
|
+
offset: { x: 24, y: 48 },
|
|
50
|
+
type: "canvas.setOffset",
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
type="button"
|
|
54
|
+
>
|
|
55
|
+
Move canvas
|
|
56
|
+
</button>
|
|
57
|
+
<button
|
|
58
|
+
onClick={() =>
|
|
59
|
+
dispatch({
|
|
60
|
+
target: "selectedLayer.opacity",
|
|
61
|
+
type: "controls.setValue",
|
|
62
|
+
value: 25,
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
type="button"
|
|
66
|
+
>
|
|
67
|
+
Change value
|
|
68
|
+
</button>
|
|
69
|
+
<span data-testid="canvas-offset">
|
|
70
|
+
{state.canvas.offset.x},{state.canvas.offset.y}
|
|
71
|
+
</span>
|
|
72
|
+
<span data-testid="opacity-value">
|
|
73
|
+
{String(state.values["selectedLayer.opacity"])}
|
|
74
|
+
</span>
|
|
75
|
+
</>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function renderToolbar() {
|
|
80
|
+
return render(
|
|
81
|
+
<ToolcraftRoot schema={createSchema()}>
|
|
82
|
+
<div style={{ height: 320, position: "relative", width: 640 }}>
|
|
83
|
+
<CanvasShell />
|
|
84
|
+
<ToolbarPanel framed={false} />
|
|
85
|
+
<StateProbe />
|
|
86
|
+
</div>
|
|
87
|
+
</ToolcraftRoot>,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
describe("ToolbarPanel", () => {
|
|
92
|
+
it("dispatches zoom commands", () => {
|
|
93
|
+
renderToolbar();
|
|
94
|
+
|
|
95
|
+
expect(screen.getByText("70%")).toBeTruthy();
|
|
96
|
+
|
|
97
|
+
fireEvent.click(screen.getByRole("button", { name: "Zoom out" }));
|
|
98
|
+
expect(screen.getByText("60%")).toBeTruthy();
|
|
99
|
+
|
|
100
|
+
fireEvent.click(screen.getByRole("button", { name: "Zoom in" }));
|
|
101
|
+
fireEvent.click(screen.getByRole("button", { name: "Zoom in" }));
|
|
102
|
+
expect(screen.getByText("80%")).toBeTruthy();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("resets zoom from the zoom label double click", () => {
|
|
106
|
+
renderToolbar();
|
|
107
|
+
|
|
108
|
+
fireEvent.click(screen.getByRole("button", { name: "Zoom in" }));
|
|
109
|
+
expect(screen.getByText("80%")).toBeTruthy();
|
|
110
|
+
|
|
111
|
+
fireEvent.doubleClick(screen.getByText("80%"));
|
|
112
|
+
|
|
113
|
+
expect(screen.getByText("70%")).toBeTruthy();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("centers the canvas through the radar command", () => {
|
|
117
|
+
renderToolbar();
|
|
118
|
+
|
|
119
|
+
fireEvent.click(screen.getByRole("button", { name: "Move canvas" }));
|
|
120
|
+
expect(screen.getByTestId("canvas-offset").textContent).toBe("24,48");
|
|
121
|
+
|
|
122
|
+
fireEvent.click(screen.getByRole("button", { name: "Center canvas" }));
|
|
123
|
+
|
|
124
|
+
expect(screen.getByTestId("canvas-offset").textContent).toBe("0,0");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("binds undo and redo to runtime history", () => {
|
|
128
|
+
renderToolbar();
|
|
129
|
+
|
|
130
|
+
const undoButton = screen.getByRole("button", { name: "Undo" });
|
|
131
|
+
const redoButton = screen.getByRole("button", { name: "Redo" });
|
|
132
|
+
|
|
133
|
+
expect((undoButton as HTMLButtonElement).disabled).toBe(true);
|
|
134
|
+
expect((redoButton as HTMLButtonElement).disabled).toBe(true);
|
|
135
|
+
|
|
136
|
+
fireEvent.click(screen.getByRole("button", { name: "Change value" }));
|
|
137
|
+
expect(screen.getByTestId("opacity-value").textContent).toBe("25");
|
|
138
|
+
expect((undoButton as HTMLButtonElement).disabled).toBe(false);
|
|
139
|
+
|
|
140
|
+
fireEvent.click(undoButton);
|
|
141
|
+
expect(screen.getByTestId("opacity-value").textContent).toBe("75");
|
|
142
|
+
expect((redoButton as HTMLButtonElement).disabled).toBe(false);
|
|
143
|
+
|
|
144
|
+
fireEvent.click(redoButton);
|
|
145
|
+
expect(screen.getByTestId("opacity-value").textContent).toBe("25");
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("toggles the scoped editor theme through the shared runtime", () => {
|
|
149
|
+
const { container } = renderToolbar();
|
|
150
|
+
|
|
151
|
+
const lightButton = screen.getByRole("button", { name: "Light theme" });
|
|
152
|
+
const themeScope = container.querySelector<HTMLElement>(
|
|
153
|
+
"[data-toolcraft-theme-scope]",
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
expect(lightButton.querySelector('[data-icon="theme-light"]')).toBeTruthy();
|
|
157
|
+
expect(themeScope?.dataset.toolcraftTheme).toBe("dark");
|
|
158
|
+
expect(themeScope?.style.colorScheme).toBe("dark");
|
|
159
|
+
expect(document.documentElement.classList.contains("dark")).toBe(false);
|
|
160
|
+
expect(document.documentElement.style.colorScheme).toBe("");
|
|
161
|
+
|
|
162
|
+
fireEvent.click(lightButton);
|
|
163
|
+
|
|
164
|
+
expect(themeScope?.dataset.toolcraftTheme).toBe("light");
|
|
165
|
+
expect(themeScope?.style.colorScheme).toBe("light");
|
|
166
|
+
expect(document.documentElement.classList.contains("dark")).toBe(false);
|
|
167
|
+
expect(document.documentElement.style.colorScheme).toBe("");
|
|
168
|
+
expect(
|
|
169
|
+
window.localStorage.getItem(TOOLCRAFT_THEME_PREFERENCE_STORAGE_KEY),
|
|
170
|
+
).toBe("light");
|
|
171
|
+
expect(screen.getByRole("button", { name: "Dark theme" })).toBeTruthy();
|
|
172
|
+
expect(
|
|
173
|
+
screen
|
|
174
|
+
.getByRole("button", { name: "Dark theme" })
|
|
175
|
+
.querySelector('[data-icon="theme-dark"]'),
|
|
176
|
+
).toBeTruthy();
|
|
177
|
+
|
|
178
|
+
fireEvent.click(screen.getByRole("button", { name: "Dark theme" }));
|
|
179
|
+
|
|
180
|
+
expect(themeScope?.dataset.toolcraftTheme).toBe("dark");
|
|
181
|
+
expect(themeScope?.style.colorScheme).toBe("dark");
|
|
182
|
+
expect(document.documentElement.classList.contains("dark")).toBe(false);
|
|
183
|
+
expect(document.documentElement.style.colorScheme).toBe("");
|
|
184
|
+
expect(
|
|
185
|
+
window.localStorage.getItem(TOOLCRAFT_THEME_PREFERENCE_STORAGE_KEY),
|
|
186
|
+
).toBe("dark");
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("uses toolbar feature defaults from the app schema", () => {
|
|
190
|
+
render(
|
|
191
|
+
<ToolcraftRoot
|
|
192
|
+
schema={defineToolcraft({
|
|
193
|
+
canvas: { enabled: true },
|
|
194
|
+
panels: {},
|
|
195
|
+
toolbar: {
|
|
196
|
+
history: false,
|
|
197
|
+
radar: false,
|
|
198
|
+
theme: false,
|
|
199
|
+
zoom: false,
|
|
200
|
+
},
|
|
201
|
+
})}
|
|
202
|
+
>
|
|
203
|
+
<ToolbarPanel framed={false} />
|
|
204
|
+
</ToolcraftRoot>,
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
expect(screen.queryByRole("button", { name: "Undo" })).toBeNull();
|
|
208
|
+
expect(screen.queryByRole("button", { name: "Center canvas" })).toBeNull();
|
|
209
|
+
expect(screen.queryByRole("button", { name: "Light theme" })).toBeNull();
|
|
210
|
+
expect(screen.queryByRole("button", { name: "Zoom in" })).toBeNull();
|
|
211
|
+
});
|
|
212
|
+
});
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { TargetIcon } from "@phosphor-icons/react";
|
|
5
|
+
import {
|
|
6
|
+
Button,
|
|
7
|
+
PanelSurface,
|
|
8
|
+
Tooltip,
|
|
9
|
+
TooltipContent,
|
|
10
|
+
TooltipTrigger,
|
|
11
|
+
} from "@repo/ui";
|
|
12
|
+
import { Moon, Redo2, Sun, Undo2, ZoomIn, ZoomOut } from "lucide-react";
|
|
13
|
+
|
|
14
|
+
import type { ToolcraftPanelState } from "../state/types";
|
|
15
|
+
import { PanelContainer } from "./panel-host";
|
|
16
|
+
import type { PanelPlacement, PanelStateChange } from "./panel-host-types";
|
|
17
|
+
import { useToolcraftTheme } from "./theme-runtime";
|
|
18
|
+
import { useToolcraft } from "./use-toolcraft";
|
|
19
|
+
|
|
20
|
+
export type ToolbarPanelProps = {
|
|
21
|
+
className?: string;
|
|
22
|
+
framed?: boolean;
|
|
23
|
+
onPanelStateChange?: PanelStateChange;
|
|
24
|
+
panelPlacement?: PanelPlacement;
|
|
25
|
+
panelState?: ToolcraftPanelState;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type ToolbarIconButtonProps = {
|
|
29
|
+
active?: boolean;
|
|
30
|
+
children: React.ReactNode;
|
|
31
|
+
className?: string;
|
|
32
|
+
disabled?: boolean;
|
|
33
|
+
label: string;
|
|
34
|
+
onClick?: () => void;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const toolbarIconButtonSize = "icon";
|
|
38
|
+
const desktopToolbarTightButtonGapClassName = "-mr-px";
|
|
39
|
+
|
|
40
|
+
function cn(...classNames: Array<string | false | null | undefined>): string {
|
|
41
|
+
return classNames.filter(Boolean).join(" ");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function ToolbarDivider(): React.JSX.Element {
|
|
45
|
+
return (
|
|
46
|
+
<span
|
|
47
|
+
aria-hidden="true"
|
|
48
|
+
className="block h-5 w-px shrink-0 rounded-full bg-[color:color-mix(in_oklab,var(--border)_8%,transparent)]"
|
|
49
|
+
data-slot="desktop-toolbar-divider"
|
|
50
|
+
/>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function ToolbarIconButton({
|
|
55
|
+
active = false,
|
|
56
|
+
children,
|
|
57
|
+
className,
|
|
58
|
+
disabled,
|
|
59
|
+
label,
|
|
60
|
+
onClick,
|
|
61
|
+
}: ToolbarIconButtonProps): React.JSX.Element {
|
|
62
|
+
const handleClick: React.MouseEventHandler<HTMLButtonElement> = (event) => {
|
|
63
|
+
onClick?.();
|
|
64
|
+
|
|
65
|
+
if (typeof event.currentTarget.blur === "function") {
|
|
66
|
+
event.currentTarget.blur();
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<Tooltip>
|
|
72
|
+
<TooltipTrigger
|
|
73
|
+
render={
|
|
74
|
+
<Button
|
|
75
|
+
aria-label={label}
|
|
76
|
+
aria-pressed={active}
|
|
77
|
+
className={cn(
|
|
78
|
+
"data-[icon-active=true]:text-[color:var(--foreground)]",
|
|
79
|
+
className,
|
|
80
|
+
)}
|
|
81
|
+
data-icon-active={active}
|
|
82
|
+
disabled={disabled}
|
|
83
|
+
onClick={handleClick}
|
|
84
|
+
size={toolbarIconButtonSize}
|
|
85
|
+
type="button"
|
|
86
|
+
variant="ghost"
|
|
87
|
+
/>
|
|
88
|
+
}
|
|
89
|
+
>
|
|
90
|
+
{children}
|
|
91
|
+
</TooltipTrigger>
|
|
92
|
+
<TooltipContent side="top">{label}</TooltipContent>
|
|
93
|
+
</Tooltip>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function ToolbarPanel({
|
|
98
|
+
className,
|
|
99
|
+
framed = true,
|
|
100
|
+
onPanelStateChange,
|
|
101
|
+
panelPlacement,
|
|
102
|
+
panelState,
|
|
103
|
+
}: ToolbarPanelProps): React.JSX.Element {
|
|
104
|
+
const { dispatch, state } = useToolcraft();
|
|
105
|
+
const { resolvedTheme, toggleResolvedTheme } = useToolcraftTheme();
|
|
106
|
+
const nextTheme = resolvedTheme === "dark" ? "light" : "dark";
|
|
107
|
+
const canUndo = state.history.undo.length > 0;
|
|
108
|
+
const canRedo = state.history.redo.length > 0;
|
|
109
|
+
const historyEnabled = state.schema.toolbar.history;
|
|
110
|
+
const radarEnabled = state.schema.toolbar.radar;
|
|
111
|
+
const themeEnabled = state.schema.toolbar.theme;
|
|
112
|
+
const zoomEnabled = state.schema.toolbar.zoom;
|
|
113
|
+
|
|
114
|
+
const toolbarSurface = (
|
|
115
|
+
<PanelSurface
|
|
116
|
+
className={cn(
|
|
117
|
+
"pointer-events-auto flex w-auto items-center justify-start gap-1.5 rounded-lg p-1",
|
|
118
|
+
!framed && className,
|
|
119
|
+
)}
|
|
120
|
+
data-toolcraft-inspect-toolbar="true"
|
|
121
|
+
data-panel-id="toolbar"
|
|
122
|
+
>
|
|
123
|
+
{historyEnabled ? (
|
|
124
|
+
<>
|
|
125
|
+
<ToolbarIconButton
|
|
126
|
+
className={desktopToolbarTightButtonGapClassName}
|
|
127
|
+
disabled={!canUndo}
|
|
128
|
+
label="Undo"
|
|
129
|
+
onClick={() => dispatch({ type: "history.undo" })}
|
|
130
|
+
>
|
|
131
|
+
<Undo2 />
|
|
132
|
+
</ToolbarIconButton>
|
|
133
|
+
<ToolbarIconButton
|
|
134
|
+
className={desktopToolbarTightButtonGapClassName}
|
|
135
|
+
disabled={!canRedo}
|
|
136
|
+
label="Redo"
|
|
137
|
+
onClick={() => dispatch({ type: "history.redo" })}
|
|
138
|
+
>
|
|
139
|
+
<Redo2 />
|
|
140
|
+
</ToolbarIconButton>
|
|
141
|
+
<ToolbarDivider />
|
|
142
|
+
</>
|
|
143
|
+
) : null}
|
|
144
|
+
{zoomEnabled ? (
|
|
145
|
+
<>
|
|
146
|
+
<ToolbarIconButton
|
|
147
|
+
label="Zoom out"
|
|
148
|
+
onClick={() => dispatch({ type: "canvas.zoomOut" })}
|
|
149
|
+
>
|
|
150
|
+
<ZoomOut />
|
|
151
|
+
</ToolbarIconButton>
|
|
152
|
+
<span
|
|
153
|
+
className="inline-flex h-7 w-[4ch] shrink-0 cursor-default items-center justify-center font-mono text-[12px] leading-[1.125rem] text-[color:color-mix(in_oklab,var(--foreground)_90%,transparent)] tabular-nums select-none"
|
|
154
|
+
data-panel-drag-ignore=""
|
|
155
|
+
onDoubleClick={(event) => {
|
|
156
|
+
event.preventDefault();
|
|
157
|
+
event.stopPropagation();
|
|
158
|
+
dispatch({ type: "canvas.zoomReset" });
|
|
159
|
+
}}
|
|
160
|
+
>
|
|
161
|
+
{state.canvas.zoom}%
|
|
162
|
+
</span>
|
|
163
|
+
<ToolbarIconButton
|
|
164
|
+
label="Zoom in"
|
|
165
|
+
onClick={() => dispatch({ type: "canvas.zoomIn" })}
|
|
166
|
+
>
|
|
167
|
+
<ZoomIn />
|
|
168
|
+
</ToolbarIconButton>
|
|
169
|
+
<ToolbarDivider />
|
|
170
|
+
</>
|
|
171
|
+
) : null}
|
|
172
|
+
{themeEnabled ? (
|
|
173
|
+
<ToolbarIconButton
|
|
174
|
+
label={nextTheme === "light" ? "Light theme" : "Dark theme"}
|
|
175
|
+
onClick={toggleResolvedTheme}
|
|
176
|
+
>
|
|
177
|
+
{nextTheme === "light" ? (
|
|
178
|
+
<Sun data-icon="theme-light" />
|
|
179
|
+
) : (
|
|
180
|
+
<Moon data-icon="theme-dark" />
|
|
181
|
+
)}
|
|
182
|
+
</ToolbarIconButton>
|
|
183
|
+
) : null}
|
|
184
|
+
{radarEnabled ? (
|
|
185
|
+
<ToolbarIconButton
|
|
186
|
+
label="Center canvas"
|
|
187
|
+
onClick={() => dispatch({ type: "canvas.center" })}
|
|
188
|
+
>
|
|
189
|
+
<TargetIcon />
|
|
190
|
+
</ToolbarIconButton>
|
|
191
|
+
) : null}
|
|
192
|
+
</PanelSurface>
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
return (
|
|
196
|
+
<PanelContainer
|
|
197
|
+
onPanelStateChange={onPanelStateChange}
|
|
198
|
+
panelState={panelState}
|
|
199
|
+
panelType="toolbar"
|
|
200
|
+
placement={panelPlacement ?? (framed ? "frame" : "surface")}
|
|
201
|
+
>
|
|
202
|
+
{toolbarSurface}
|
|
203
|
+
</PanelContainer>
|
|
204
|
+
);
|
|
205
|
+
}
|