@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,82 @@
|
|
|
1
|
+
import type { ToolcraftCanvasSize } from "../schema/types";
|
|
2
|
+
|
|
3
|
+
export type ToolcraftImportedImageFile = {
|
|
4
|
+
dataUrl: string;
|
|
5
|
+
size: ToolcraftCanvasSize;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
function readFileDataUrl(file: File): Promise<string | null> {
|
|
9
|
+
return new Promise((resolve) => {
|
|
10
|
+
const reader = new FileReader();
|
|
11
|
+
|
|
12
|
+
reader.addEventListener("load", () => {
|
|
13
|
+
resolve(typeof reader.result === "string" ? reader.result : null);
|
|
14
|
+
});
|
|
15
|
+
reader.addEventListener("error", () => resolve(null));
|
|
16
|
+
reader.readAsDataURL(file);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function readImageDataUrlSize(dataUrl: string): Promise<ToolcraftCanvasSize | null> {
|
|
21
|
+
if (typeof Image === "undefined") {
|
|
22
|
+
return Promise.resolve(null);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return new Promise((resolve) => {
|
|
26
|
+
const image = new Image();
|
|
27
|
+
let resolved = false;
|
|
28
|
+
let fallbackTimeout: number | undefined;
|
|
29
|
+
const jsdomFallback =
|
|
30
|
+
typeof navigator !== "undefined" &&
|
|
31
|
+
navigator.userAgent.toLowerCase().includes("jsdom");
|
|
32
|
+
const finish = (size: ToolcraftCanvasSize | null): void => {
|
|
33
|
+
if (resolved) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
resolved = true;
|
|
38
|
+
if (fallbackTimeout !== undefined) {
|
|
39
|
+
window.clearTimeout(fallbackTimeout);
|
|
40
|
+
}
|
|
41
|
+
resolve(size);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
fallbackTimeout = window.setTimeout(
|
|
45
|
+
() => finish(null),
|
|
46
|
+
jsdomFallback ? 0 : 5000,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
image.addEventListener("load", () => {
|
|
50
|
+
const width = Math.round(image.naturalWidth || image.width);
|
|
51
|
+
const height = Math.round(image.naturalHeight || image.height);
|
|
52
|
+
|
|
53
|
+
finish(
|
|
54
|
+
width > 0 && height > 0
|
|
55
|
+
? {
|
|
56
|
+
height,
|
|
57
|
+
unit: "px",
|
|
58
|
+
width,
|
|
59
|
+
}
|
|
60
|
+
: null,
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
image.addEventListener("error", () => finish(null));
|
|
64
|
+
image.src = dataUrl;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function readImportedImageFile(
|
|
69
|
+
file: File,
|
|
70
|
+
fallbackSize: ToolcraftCanvasSize,
|
|
71
|
+
): Promise<ToolcraftImportedImageFile | null> {
|
|
72
|
+
const dataUrl = await readFileDataUrl(file);
|
|
73
|
+
|
|
74
|
+
if (!dataUrl) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
dataUrl,
|
|
80
|
+
size: (await readImageDataUrlSize(dataUrl)) ?? fallbackSize,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { ToolcraftPanelType, PanelDragMode, PanelSnapEdge } from "./panel-host-types";
|
|
2
|
+
|
|
3
|
+
type PanelTypeConfig = {
|
|
4
|
+
dragMode: PanelDragMode;
|
|
5
|
+
panelId: ToolcraftPanelType;
|
|
6
|
+
snapEdges: readonly PanelSnapEdge[];
|
|
7
|
+
stageClassName: string;
|
|
8
|
+
wrapperClassName: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const panelHostConfig = {
|
|
12
|
+
controls: {
|
|
13
|
+
dragMode: "handle",
|
|
14
|
+
panelId: "controls",
|
|
15
|
+
snapEdges: ["left", "right"],
|
|
16
|
+
stageClassName: "min-h-[560px]",
|
|
17
|
+
wrapperClassName: "absolute top-2.5 right-2.5 z-30",
|
|
18
|
+
},
|
|
19
|
+
layers: {
|
|
20
|
+
dragMode: "handle",
|
|
21
|
+
panelId: "layers",
|
|
22
|
+
snapEdges: ["left", "right"],
|
|
23
|
+
stageClassName: "min-h-[560px]",
|
|
24
|
+
wrapperClassName: "absolute top-2.5 left-2.5 z-30",
|
|
25
|
+
},
|
|
26
|
+
timeline: {
|
|
27
|
+
dragMode: "panel",
|
|
28
|
+
panelId: "timeline",
|
|
29
|
+
snapEdges: ["top", "bottom"],
|
|
30
|
+
stageClassName: "min-h-[320px]",
|
|
31
|
+
wrapperClassName: "absolute top-2.5 left-1/2 z-40 -translate-x-1/2",
|
|
32
|
+
},
|
|
33
|
+
toolbar: {
|
|
34
|
+
dragMode: "panel",
|
|
35
|
+
panelId: "toolbar",
|
|
36
|
+
snapEdges: ["top", "bottom"],
|
|
37
|
+
stageClassName: "min-h-[180px]",
|
|
38
|
+
wrapperClassName: "absolute bottom-2.5 left-1/2 z-[70] -translate-x-1/2",
|
|
39
|
+
},
|
|
40
|
+
} satisfies Record<ToolcraftPanelType, PanelTypeConfig>;
|
|
41
|
+
|
|
42
|
+
export const panelSnapMarginPx = 10;
|
|
43
|
+
export const panelSnapZonePx = 40;
|
|
44
|
+
export const panelVelocityMultiplierMs = 150;
|
|
45
|
+
export const panelDragHandleSelector = "[data-panel-drag-handle]";
|
|
46
|
+
|
|
47
|
+
export const panelDragTransition = {
|
|
48
|
+
bounceDamping: 90,
|
|
49
|
+
bounceStiffness: 1200,
|
|
50
|
+
power: 0,
|
|
51
|
+
} as const;
|
|
52
|
+
|
|
53
|
+
export const panelSnapAnimation = {
|
|
54
|
+
duration: 0.3,
|
|
55
|
+
ease: [0.22, 1, 0.36, 1],
|
|
56
|
+
} as const;
|
|
57
|
+
|
|
58
|
+
export const panelDragIgnoredTargetSelector = [
|
|
59
|
+
".app-no-drag",
|
|
60
|
+
"a[href]",
|
|
61
|
+
"button",
|
|
62
|
+
"input",
|
|
63
|
+
"label",
|
|
64
|
+
"select",
|
|
65
|
+
"textarea",
|
|
66
|
+
"[contenteditable='true']",
|
|
67
|
+
"[data-panel-drag-ignore]",
|
|
68
|
+
"[data-slot='input-group']",
|
|
69
|
+
"[data-slot='select-trigger']",
|
|
70
|
+
"[data-slot='slider']",
|
|
71
|
+
"[role='button']",
|
|
72
|
+
"[role='checkbox']",
|
|
73
|
+
"[role='combobox']",
|
|
74
|
+
"[role='menuitem']",
|
|
75
|
+
"[role='option']",
|
|
76
|
+
"[role='radio']",
|
|
77
|
+
"[role='slider']",
|
|
78
|
+
"[role='switch']",
|
|
79
|
+
"[role='textbox']",
|
|
80
|
+
].join(",");
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { resolvePanelSnapPosition } from "./panel-host-geometry";
|
|
4
|
+
import type { PanelViewport } from "./panel-host-types";
|
|
5
|
+
|
|
6
|
+
const viewport = {
|
|
7
|
+
height: 500,
|
|
8
|
+
offsetLeft: 0,
|
|
9
|
+
offsetTop: 0,
|
|
10
|
+
width: 800,
|
|
11
|
+
} satisfies PanelViewport;
|
|
12
|
+
|
|
13
|
+
const dimensions = {
|
|
14
|
+
height: 100,
|
|
15
|
+
width: 200,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
describe("resolvePanelSnapPosition", () => {
|
|
19
|
+
it("snaps a side panel to the left edge", () => {
|
|
20
|
+
expect(
|
|
21
|
+
resolvePanelSnapPosition({
|
|
22
|
+
dimensions,
|
|
23
|
+
edges: ["left", "right"],
|
|
24
|
+
position: { x: 12, y: 80 },
|
|
25
|
+
velocity: { x: 0, y: 0 },
|
|
26
|
+
viewport,
|
|
27
|
+
}),
|
|
28
|
+
).toEqual({ x: 10, y: 80 });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("snaps a top or bottom panel to the bottom edge", () => {
|
|
32
|
+
expect(
|
|
33
|
+
resolvePanelSnapPosition({
|
|
34
|
+
dimensions,
|
|
35
|
+
edges: ["top", "bottom"],
|
|
36
|
+
position: { x: 160, y: 360 },
|
|
37
|
+
velocity: { x: 0, y: 0 },
|
|
38
|
+
viewport,
|
|
39
|
+
}),
|
|
40
|
+
).toEqual({ x: 160, y: 390 });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("returns null when the panel is outside the snap zone", () => {
|
|
44
|
+
expect(
|
|
45
|
+
resolvePanelSnapPosition({
|
|
46
|
+
dimensions,
|
|
47
|
+
edges: ["left"],
|
|
48
|
+
position: { x: 120, y: 80 },
|
|
49
|
+
velocity: { x: 0, y: 0 },
|
|
50
|
+
viewport,
|
|
51
|
+
}),
|
|
52
|
+
).toBeNull();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("clamps snapped panel positions inside the viewport", () => {
|
|
56
|
+
expect(
|
|
57
|
+
resolvePanelSnapPosition({
|
|
58
|
+
dimensions,
|
|
59
|
+
edges: ["top"],
|
|
60
|
+
position: { x: -100, y: 4 },
|
|
61
|
+
velocity: { x: 0, y: 0 },
|
|
62
|
+
viewport,
|
|
63
|
+
}),
|
|
64
|
+
).toEqual({ x: 10, y: 10 });
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import {
|
|
2
|
+
panelSnapMarginPx,
|
|
3
|
+
panelSnapZonePx,
|
|
4
|
+
panelVelocityMultiplierMs,
|
|
5
|
+
} from "./panel-host-config";
|
|
6
|
+
import type {
|
|
7
|
+
PanelDimensions,
|
|
8
|
+
PanelPoint,
|
|
9
|
+
PanelSnapEdge,
|
|
10
|
+
PanelViewport,
|
|
11
|
+
} from "./panel-host-types";
|
|
12
|
+
|
|
13
|
+
export function clampPanelValue(value: number, min: number, max: number): number {
|
|
14
|
+
const safeMax = Math.max(min, max);
|
|
15
|
+
|
|
16
|
+
return Math.max(min, Math.min(value, safeMax));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function getProjectedPanelPosition(position: PanelPoint, velocity: PanelPoint): PanelPoint {
|
|
20
|
+
return {
|
|
21
|
+
x: position.x + velocity.x * panelVelocityMultiplierMs,
|
|
22
|
+
y: position.y + velocity.y * panelVelocityMultiplierMs,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function getPanelEdgeDistance(
|
|
27
|
+
edge: PanelSnapEdge,
|
|
28
|
+
position: PanelPoint,
|
|
29
|
+
dimensions: PanelDimensions,
|
|
30
|
+
viewport: PanelViewport,
|
|
31
|
+
): number {
|
|
32
|
+
if (edge === "top") {
|
|
33
|
+
return position.y - viewport.offsetTop;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (edge === "right") {
|
|
37
|
+
return viewport.offsetLeft + viewport.width - (position.x + dimensions.width);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (edge === "bottom") {
|
|
41
|
+
return viewport.offsetTop + viewport.height - (position.y + dimensions.height);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return position.x - viewport.offsetLeft;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function getPanelSnapEdge(
|
|
48
|
+
projectedPosition: PanelPoint,
|
|
49
|
+
dimensions: PanelDimensions,
|
|
50
|
+
viewport: PanelViewport,
|
|
51
|
+
edges: readonly PanelSnapEdge[],
|
|
52
|
+
zone: number,
|
|
53
|
+
): PanelSnapEdge | null {
|
|
54
|
+
const candidates = edges
|
|
55
|
+
.map((edge) => ({
|
|
56
|
+
edge,
|
|
57
|
+
value: getPanelEdgeDistance(edge, projectedPosition, dimensions, viewport),
|
|
58
|
+
}))
|
|
59
|
+
.filter((candidate) => candidate.value <= zone);
|
|
60
|
+
|
|
61
|
+
if (candidates.length === 0) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return candidates.reduce((closest, candidate) =>
|
|
66
|
+
candidate.value < closest.value ? candidate : closest,
|
|
67
|
+
).edge;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function resolvePanelSnapPosition({
|
|
71
|
+
dimensions,
|
|
72
|
+
edges,
|
|
73
|
+
margin = panelSnapMarginPx,
|
|
74
|
+
position,
|
|
75
|
+
velocity,
|
|
76
|
+
viewport,
|
|
77
|
+
zone = panelSnapZonePx,
|
|
78
|
+
}: {
|
|
79
|
+
dimensions: PanelDimensions;
|
|
80
|
+
edges: readonly PanelSnapEdge[];
|
|
81
|
+
margin?: number;
|
|
82
|
+
position: PanelPoint;
|
|
83
|
+
velocity: PanelPoint;
|
|
84
|
+
viewport: PanelViewport;
|
|
85
|
+
zone?: number;
|
|
86
|
+
}): PanelPoint | null {
|
|
87
|
+
const projectedPosition = getProjectedPanelPosition(position, velocity);
|
|
88
|
+
const edge = getPanelSnapEdge(projectedPosition, dimensions, viewport, edges, zone);
|
|
89
|
+
const minX = viewport.offsetLeft + margin;
|
|
90
|
+
const maxX = viewport.offsetLeft + viewport.width - dimensions.width - margin;
|
|
91
|
+
const minY = viewport.offsetTop + margin;
|
|
92
|
+
const maxY = viewport.offsetTop + viewport.height - dimensions.height - margin;
|
|
93
|
+
|
|
94
|
+
if (!edge) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (edge === "top" || edge === "bottom") {
|
|
99
|
+
return {
|
|
100
|
+
x: clampPanelValue(projectedPosition.x, minX, maxX),
|
|
101
|
+
y: edge === "top" ? minY : maxY,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
x: edge === "left" ? minX : maxX,
|
|
107
|
+
y: clampPanelValue(projectedPosition.y, minY, maxY),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type * as React from "react";
|
|
2
|
+
|
|
3
|
+
import type { ToolcraftPanelId, ToolcraftPanelState } from "../state/types";
|
|
4
|
+
|
|
5
|
+
export type { ToolcraftPanelState } from "../state/types";
|
|
6
|
+
|
|
7
|
+
export type ToolcraftPanelType = ToolcraftPanelId;
|
|
8
|
+
|
|
9
|
+
export type PanelSnapEdge = "bottom" | "left" | "right" | "top";
|
|
10
|
+
|
|
11
|
+
export type PanelDragMode = "handle" | "panel";
|
|
12
|
+
|
|
13
|
+
export type PanelPoint = {
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type PanelDimensions = {
|
|
19
|
+
height: number;
|
|
20
|
+
width: number;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type PanelViewport = PanelDimensions & {
|
|
24
|
+
offsetLeft: number;
|
|
25
|
+
offsetTop: number;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type PanelSnapConfig = {
|
|
29
|
+
edges: readonly PanelSnapEdge[];
|
|
30
|
+
margin?: number;
|
|
31
|
+
zone?: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type PanelPlacement = "floating" | "frame" | "surface";
|
|
35
|
+
|
|
36
|
+
export type PanelStateChange = (state: Partial<ToolcraftPanelState>) => void;
|
|
37
|
+
|
|
38
|
+
export type PanelHostProps = {
|
|
39
|
+
children: React.ReactNode;
|
|
40
|
+
className?: string;
|
|
41
|
+
dragMode?: PanelDragMode;
|
|
42
|
+
innerClassName?: string;
|
|
43
|
+
onPositionChange?: (position: PanelPoint) => void;
|
|
44
|
+
onResetPosition?: () => void;
|
|
45
|
+
panelId?: string;
|
|
46
|
+
panelType: ToolcraftPanelType;
|
|
47
|
+
position?: PanelPoint;
|
|
48
|
+
snap?: PanelSnapConfig;
|
|
49
|
+
style?: React.CSSProperties;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export type PanelStageProps = React.HTMLAttributes<HTMLDivElement> & {
|
|
53
|
+
children: React.ReactNode;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type PanelContainerProps = {
|
|
57
|
+
children: React.ReactNode;
|
|
58
|
+
className?: string;
|
|
59
|
+
dragMode?: PanelDragMode;
|
|
60
|
+
onPanelStateChange?: PanelStateChange;
|
|
61
|
+
panelClassName?: string;
|
|
62
|
+
panelState?: ToolcraftPanelState;
|
|
63
|
+
panelType: ToolcraftPanelType;
|
|
64
|
+
placement: PanelPlacement;
|
|
65
|
+
} & Omit<React.HTMLAttributes<HTMLDivElement>, "children" | "className">;
|
|
66
|
+
|
|
67
|
+
export type ToolcraftPanelHostProps = Omit<
|
|
68
|
+
PanelHostProps,
|
|
69
|
+
"onPositionChange" | "onResetPosition" | "position"
|
|
70
|
+
> & {
|
|
71
|
+
onPositionChange?: (position: PanelPoint) => void;
|
|
72
|
+
onResetPosition?: () => void;
|
|
73
|
+
position?: PanelPoint;
|
|
74
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { fireEvent, render, screen } from "@testing-library/react";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { defineToolcraft } from "../schema/define-toolcraft";
|
|
6
|
+
import { ToolcraftRoot } from "./toolcraft-root";
|
|
7
|
+
import { ToolcraftPanelHost, PanelHost } from "./panel-host";
|
|
8
|
+
import { useToolcraft } from "./use-toolcraft";
|
|
9
|
+
|
|
10
|
+
function PanelOffsetReader() {
|
|
11
|
+
const { dispatch, state } = useToolcraft();
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<>
|
|
15
|
+
<button
|
|
16
|
+
onClick={() =>
|
|
17
|
+
dispatch({
|
|
18
|
+
offset: { x: 40, y: 80 },
|
|
19
|
+
panelId: "toolbar",
|
|
20
|
+
type: "panels.setOffset",
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
type="button"
|
|
24
|
+
>
|
|
25
|
+
Move toolbar
|
|
26
|
+
</button>
|
|
27
|
+
<span data-testid="toolbar-offset">
|
|
28
|
+
{state.panels.toolbar.offset.x},{state.panels.toolbar.offset.y}
|
|
29
|
+
</span>
|
|
30
|
+
<ToolcraftPanelHost panelType="toolbar">
|
|
31
|
+
<div>Toolbar</div>
|
|
32
|
+
</ToolcraftPanelHost>
|
|
33
|
+
</>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe("PanelHost", () => {
|
|
38
|
+
it("renders default placement attributes for a panel type", () => {
|
|
39
|
+
const { container } = render(
|
|
40
|
+
<PanelHost panelType="toolbar">
|
|
41
|
+
<div>Toolbar</div>
|
|
42
|
+
</PanelHost>,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const host = container.querySelector("[data-slot='toolcraft-runtime-panel-host']");
|
|
46
|
+
|
|
47
|
+
expect(host?.getAttribute("data-panel-type")).toBe("toolbar");
|
|
48
|
+
expect(host?.getAttribute("data-drag-mode")).toBe("panel");
|
|
49
|
+
expect(host?.getAttribute("data-snap-edges")).toBe("top bottom");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("resets a panel on double click", () => {
|
|
53
|
+
const handleReset = vi.fn();
|
|
54
|
+
const { container } = render(
|
|
55
|
+
<PanelHost onResetPosition={handleReset} panelType="toolbar">
|
|
56
|
+
<div>Toolbar</div>
|
|
57
|
+
</PanelHost>,
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const host = container.querySelector("[data-slot='toolcraft-runtime-panel-host']");
|
|
61
|
+
|
|
62
|
+
expect(host).not.toBeNull();
|
|
63
|
+
fireEvent.doubleClick(host as Element);
|
|
64
|
+
|
|
65
|
+
expect(handleReset).toHaveBeenCalledTimes(1);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("does not reset when double clicking an ignored child control", () => {
|
|
69
|
+
const handleReset = vi.fn();
|
|
70
|
+
render(
|
|
71
|
+
<PanelHost onResetPosition={handleReset} panelType="toolbar">
|
|
72
|
+
<button type="button">Zoom</button>
|
|
73
|
+
</PanelHost>,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
fireEvent.doubleClick(screen.getByRole("button", { name: "Zoom" }));
|
|
77
|
+
|
|
78
|
+
expect(handleReset).not.toHaveBeenCalled();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("lets the runtime reset stored panel offsets", () => {
|
|
82
|
+
const schema = defineToolcraft({
|
|
83
|
+
canvas: { enabled: true },
|
|
84
|
+
panels: {},
|
|
85
|
+
});
|
|
86
|
+
const { container } = render(
|
|
87
|
+
<ToolcraftRoot schema={schema}>
|
|
88
|
+
<PanelOffsetReader />
|
|
89
|
+
</ToolcraftRoot>,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
fireEvent.click(screen.getByRole("button", { name: "Move toolbar" }));
|
|
93
|
+
expect(screen.getByTestId("toolbar-offset").textContent).toBe("40,80");
|
|
94
|
+
|
|
95
|
+
const host = container.querySelector("[data-slot='toolcraft-runtime-panel-host']");
|
|
96
|
+
|
|
97
|
+
expect(host).not.toBeNull();
|
|
98
|
+
fireEvent.doubleClick(host as Element);
|
|
99
|
+
|
|
100
|
+
expect(screen.getByTestId("toolbar-offset").textContent).toBe("0,0");
|
|
101
|
+
});
|
|
102
|
+
});
|