@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.
Files changed (257) hide show
  1. package/LICENSE.md +98 -0
  2. package/README.md +41 -0
  3. package/bin/create-toolcraft-app.mjs +8 -0
  4. package/bin/toolcraft.mjs +8 -0
  5. package/package.json +24 -0
  6. package/scripts/prepare-pack.mjs +29 -0
  7. package/src/cli.mjs +392 -0
  8. package/src/cli.test.mjs +284 -0
  9. package/src/copy-recursive.mjs +86 -0
  10. package/src/generate.mjs +212 -0
  11. package/src/generate.test.mjs +322 -0
  12. package/src/import-map.mjs +14 -0
  13. package/src/package-json.mjs +80 -0
  14. package/src/package-json.test.mjs +67 -0
  15. package/src/rewrite-imports.mjs +85 -0
  16. package/src/rewrite-imports.test.mjs +58 -0
  17. package/templates/runtime/contracts/component-contracts.test.ts +1165 -0
  18. package/templates/runtime/contracts/component-contracts.ts +1340 -0
  19. package/templates/runtime/contracts/decision-contracts.test.ts +206 -0
  20. package/templates/runtime/contracts/decision-contracts.ts +283 -0
  21. package/templates/runtime/contracts/index.test.ts +14 -0
  22. package/templates/runtime/contracts/index.ts +3 -0
  23. package/templates/runtime/contracts/types.ts +56 -0
  24. package/templates/runtime/export/export.test.ts +203 -0
  25. package/templates/runtime/export/export.ts +132 -0
  26. package/templates/runtime/export/index.ts +1 -0
  27. package/templates/runtime/index.ts +14 -0
  28. package/templates/runtime/react/canvas-shell.test.tsx +424 -0
  29. package/templates/runtime/react/canvas-shell.tsx +408 -0
  30. package/templates/runtime/react/control-renderers.ts +31 -0
  31. package/templates/runtime/react/controls-panel.test.tsx +3736 -0
  32. package/templates/runtime/react/controls-panel.tsx +2327 -0
  33. package/templates/runtime/react/curve-geometry.test.ts +70 -0
  34. package/templates/runtime/react/index.ts +15 -0
  35. package/templates/runtime/react/layer-tree.ts +96 -0
  36. package/templates/runtime/react/layers-panel.test.tsx +487 -0
  37. package/templates/runtime/react/layers-panel.tsx +1348 -0
  38. package/templates/runtime/react/media-file.ts +82 -0
  39. package/templates/runtime/react/panel-host-config.ts +80 -0
  40. package/templates/runtime/react/panel-host-geometry.test.ts +66 -0
  41. package/templates/runtime/react/panel-host-geometry.ts +109 -0
  42. package/templates/runtime/react/panel-host-types.ts +74 -0
  43. package/templates/runtime/react/panel-host.test.tsx +102 -0
  44. package/templates/runtime/react/panel-host.tsx +353 -0
  45. package/templates/runtime/react/runtime-public-api.test.tsx +132 -0
  46. package/templates/runtime/react/settings-transfer.test.ts +150 -0
  47. package/templates/runtime/react/settings-transfer.ts +279 -0
  48. package/templates/runtime/react/storage-key-migration.ts +48 -0
  49. package/templates/runtime/react/theme-runtime.tsx +177 -0
  50. package/templates/runtime/react/timeline-panel.test.tsx +668 -0
  51. package/templates/runtime/react/timeline-panel.tsx +2953 -0
  52. package/templates/runtime/react/toolbar-panel.test.tsx +212 -0
  53. package/templates/runtime/react/toolbar-panel.tsx +205 -0
  54. package/templates/runtime/react/toolcraft-app.integration.test.tsx +350 -0
  55. package/templates/runtime/react/toolcraft-app.test.tsx +339 -0
  56. package/templates/runtime/react/toolcraft-app.tsx +81 -0
  57. package/templates/runtime/react/toolcraft-root.test.tsx +347 -0
  58. package/templates/runtime/react/toolcraft-root.tsx +203 -0
  59. package/templates/runtime/react/use-toolcraft.ts +41 -0
  60. package/templates/runtime/schema/define-toolcraft.test.ts +1524 -0
  61. package/templates/runtime/schema/define-toolcraft.ts +1442 -0
  62. package/templates/runtime/schema/keyframe-capability.test.ts +90 -0
  63. package/templates/runtime/schema/keyframe-capability.ts +51 -0
  64. package/templates/runtime/schema/runtime-targets.ts +40 -0
  65. package/templates/runtime/schema/types.ts +370 -0
  66. package/templates/runtime/state/canvas-zoom.ts +8 -0
  67. package/templates/runtime/state/create-template-state.test.ts +242 -0
  68. package/templates/runtime/state/create-template-state.ts +95 -0
  69. package/templates/runtime/state/keyframe-evaluation.test.ts +141 -0
  70. package/templates/runtime/state/keyframe-evaluation.ts +203 -0
  71. package/templates/runtime/state/persistence.test.ts +217 -0
  72. package/templates/runtime/state/persistence.ts +511 -0
  73. package/templates/runtime/state/reducer.test.ts +937 -0
  74. package/templates/runtime/state/reducer.ts +1212 -0
  75. package/templates/runtime/state/timeline-readiness.ts +43 -0
  76. package/templates/runtime/state/types.ts +242 -0
  77. package/templates/runtime/styles.css +125 -0
  78. package/templates/runtime/testing/performance.test.ts +1058 -0
  79. package/templates/runtime/testing/performance.ts +1078 -0
  80. package/templates/starter/AGENTS.md +186 -0
  81. package/templates/starter/LICENSE.md +98 -0
  82. package/templates/starter/NOTICE.md +8 -0
  83. package/templates/starter/docs/toolcraft/README.md +41 -0
  84. package/templates/starter/docs/toolcraft/acceptance-testing.md +205 -0
  85. package/templates/starter/docs/toolcraft/agent-worklog.md +81 -0
  86. package/templates/starter/docs/toolcraft/assembly-workflow.md +206 -0
  87. package/templates/starter/docs/toolcraft/component-rules.md +299 -0
  88. package/templates/starter/docs/toolcraft/custom-controls.md +71 -0
  89. package/templates/starter/docs/toolcraft/decision-contract.md +71 -0
  90. package/templates/starter/docs/toolcraft/performance.md +112 -0
  91. package/templates/starter/docs/toolcraft/renderer-technique.md +48 -0
  92. package/templates/starter/docs/toolcraft/schema-reference.md +265 -0
  93. package/templates/starter/docs/toolcraft/workflow.md +87 -0
  94. package/templates/starter/e2e/app-browser-acceptance.spec.ts +785 -0
  95. package/templates/starter/e2e/app-controls.spec.ts +41 -0
  96. package/templates/starter/e2e/app-performance.spec.ts +326 -0
  97. package/templates/starter/e2e/canvas-handle-helpers.ts +244 -0
  98. package/templates/starter/e2e/performance-helpers.ts +612 -0
  99. package/templates/starter/e2e/product-observable-helpers.ts +170 -0
  100. package/templates/starter/index.html +12 -0
  101. package/templates/starter/package.json +52 -0
  102. package/templates/starter/playwright.config.ts +43 -0
  103. package/templates/starter/scripts/check-ai-skills.mjs +95 -0
  104. package/templates/starter/scripts/check-toolcraft-docs.mjs +159 -0
  105. package/templates/starter/scripts/check-toolcraft-integrity.mjs +232 -0
  106. package/templates/starter/scripts/run-vite-on-free-port.mjs +48 -0
  107. package/templates/starter/scripts/toolcraft-port.mjs +54 -0
  108. package/templates/starter/scripts/toolcraft-port.test.mjs +73 -0
  109. package/templates/starter/src/app/starter-acceptance.test.ts +5959 -0
  110. package/templates/starter/src/app/starter-acceptance.ts +2646 -0
  111. package/templates/starter/src/app/starter-performance.test.ts +1390 -0
  112. package/templates/starter/src/app/starter-performance.ts +12 -0
  113. package/templates/starter/src/app/starter-schema.test.ts +70 -0
  114. package/templates/starter/src/app/starter-schema.ts +15 -0
  115. package/templates/starter/src/main.tsx +18 -0
  116. package/templates/starter/src/router.tsx +16 -0
  117. package/templates/starter/src/routes/index.tsx +7 -0
  118. package/templates/starter/src/routes/root.tsx +19 -0
  119. package/templates/starter/src/styles.css +120 -0
  120. package/templates/starter/tsconfig.json +11 -0
  121. package/templates/starter/vite.config.ts +13 -0
  122. package/templates/ui/components/composites/accordion.tsx +73 -0
  123. package/templates/ui/components/composites/alert-dialog.tsx +190 -0
  124. package/templates/ui/components/composites/alert.tsx +74 -0
  125. package/templates/ui/components/composites/aspect-ratio.tsx +22 -0
  126. package/templates/ui/components/composites/avatar.tsx +98 -0
  127. package/templates/ui/components/composites/badge.tsx +69 -0
  128. package/templates/ui/components/composites/breadcrumb.tsx +106 -0
  129. package/templates/ui/components/composites/card.tsx +91 -0
  130. package/templates/ui/components/composites/combobox.tsx +486 -0
  131. package/templates/ui/components/composites/command.tsx +296 -0
  132. package/templates/ui/components/composites/context-menu.tsx +247 -0
  133. package/templates/ui/components/composites/dialog.tsx +282 -0
  134. package/templates/ui/components/composites/dropdown-menu.tsx +299 -0
  135. package/templates/ui/components/composites/empty.tsx +110 -0
  136. package/templates/ui/components/composites/hover-card.tsx +44 -0
  137. package/templates/ui/components/composites/index.ts +30 -0
  138. package/templates/ui/components/composites/menubar.tsx +214 -0
  139. package/templates/ui/components/composites/navigation-menu.tsx +167 -0
  140. package/templates/ui/components/composites/pagination.tsx +131 -0
  141. package/templates/ui/components/composites/progress.tsx +72 -0
  142. package/templates/ui/components/composites/radio-group.tsx +84 -0
  143. package/templates/ui/components/composites/resizable.tsx +42 -0
  144. package/templates/ui/components/composites/sheet.tsx +153 -0
  145. package/templates/ui/components/composites/sidebar-structural.tsx +310 -0
  146. package/templates/ui/components/composites/sidebar.tsx +431 -0
  147. package/templates/ui/components/composites/sonner.tsx +35 -0
  148. package/templates/ui/components/composites/spinner.tsx +43 -0
  149. package/templates/ui/components/composites/table.tsx +108 -0
  150. package/templates/ui/components/composites/tabs.tsx +83 -0
  151. package/templates/ui/components/control-layout/index.tsx +437 -0
  152. package/templates/ui/components/controls/actions/actions-control.tsx +139 -0
  153. package/templates/ui/components/controls/actions/index.ts +9 -0
  154. package/templates/ui/components/controls/anchor-grid/anchor-grid-control.tsx +107 -0
  155. package/templates/ui/components/controls/anchor-grid/index.ts +4 -0
  156. package/templates/ui/components/controls/boolean/boolean-controls.tsx +79 -0
  157. package/templates/ui/components/controls/boolean/index.ts +4 -0
  158. package/templates/ui/components/controls/channel-mixer/channel-mixer-control.tsx +95 -0
  159. package/templates/ui/components/controls/channel-mixer/index.ts +4 -0
  160. package/templates/ui/components/controls/channel-tabs/channel-tabs.tsx +42 -0
  161. package/templates/ui/components/controls/channel-tabs/index.ts +6 -0
  162. package/templates/ui/components/controls/code-textarea/code-textarea-control.tsx +90 -0
  163. package/templates/ui/components/controls/code-textarea/index.ts +4 -0
  164. package/templates/ui/components/controls/color/color-control.tsx +571 -0
  165. package/templates/ui/components/controls/color/color-picker-popover.tsx +104 -0
  166. package/templates/ui/components/controls/color/index.ts +41 -0
  167. package/templates/ui/components/controls/color/palette-control-data.ts +436 -0
  168. package/templates/ui/components/controls/color/palette-control.tsx +535 -0
  169. package/templates/ui/components/controls/color/style-guide-color-picker-channel-utils.ts +162 -0
  170. package/templates/ui/components/controls/color/style-guide-color-picker-interactions.ts +190 -0
  171. package/templates/ui/components/controls/color/style-guide-color-picker-logic.ts +485 -0
  172. package/templates/ui/components/controls/color/style-guide-color-picker-parts.tsx +710 -0
  173. package/templates/ui/components/controls/color/style-guide-color-picker.tsx +503 -0
  174. package/templates/ui/components/controls/control-types.ts +43 -0
  175. package/templates/ui/components/controls/curves/curve-geometry.ts +355 -0
  176. package/templates/ui/components/controls/curves/curve-graph.tsx +390 -0
  177. package/templates/ui/components/controls/curves/curves-control.tsx +445 -0
  178. package/templates/ui/components/controls/curves/index.ts +6 -0
  179. package/templates/ui/components/controls/file-drop/file-drop-control.tsx +191 -0
  180. package/templates/ui/components/controls/file-drop/index.ts +5 -0
  181. package/templates/ui/components/controls/font-picker/font-catalog.json +15360 -0
  182. package/templates/ui/components/controls/font-picker/font-catalog.ts +116 -0
  183. package/templates/ui/components/controls/font-picker/font-picker-control.tsx +1202 -0
  184. package/templates/ui/components/controls/font-picker/font-preview-loader.ts +336 -0
  185. package/templates/ui/components/controls/font-picker/index.ts +24 -0
  186. package/templates/ui/components/controls/font-picker/use-hover-intent.ts +46 -0
  187. package/templates/ui/components/controls/gradient/gradient-control-utils.ts +190 -0
  188. package/templates/ui/components/controls/gradient/gradient-control.tsx +612 -0
  189. package/templates/ui/components/controls/gradient/gradient-stop-list.tsx +400 -0
  190. package/templates/ui/components/controls/gradient/gradient-toolbar.tsx +152 -0
  191. package/templates/ui/components/controls/gradient/index.ts +4 -0
  192. package/templates/ui/components/controls/image-picker/image-picker-control.tsx +139 -0
  193. package/templates/ui/components/controls/image-picker/index.ts +7 -0
  194. package/templates/ui/components/controls/index.ts +192 -0
  195. package/templates/ui/components/controls/range-input/index.ts +4 -0
  196. package/templates/ui/components/controls/range-input/range-input-control.tsx +173 -0
  197. package/templates/ui/components/controls/range-slider/index.ts +4 -0
  198. package/templates/ui/components/controls/range-slider/range-slider-control.tsx +122 -0
  199. package/templates/ui/components/controls/range-slider/range-slider-value.ts +61 -0
  200. package/templates/ui/components/controls/segmented/index.ts +8 -0
  201. package/templates/ui/components/controls/segmented/segmented-control.tsx +94 -0
  202. package/templates/ui/components/controls/select/index.ts +4 -0
  203. package/templates/ui/components/controls/select/select-control.tsx +223 -0
  204. package/templates/ui/components/controls/slider/index.ts +4 -0
  205. package/templates/ui/components/controls/slider/slider-control.tsx +150 -0
  206. package/templates/ui/components/controls/slider/slider-value.ts +56 -0
  207. package/templates/ui/components/controls/text-input/index.ts +4 -0
  208. package/templates/ui/components/controls/text-input/text-input-control.tsx +158 -0
  209. package/templates/ui/components/controls/use-measured-element-width.ts +42 -0
  210. package/templates/ui/components/controls/vector/index.ts +8 -0
  211. package/templates/ui/components/controls/vector/vector-control.tsx +401 -0
  212. package/templates/ui/components/panel/index.ts +19 -0
  213. package/templates/ui/components/panel/panel-actions.tsx +165 -0
  214. package/templates/ui/components/panel/panel-header.tsx +61 -0
  215. package/templates/ui/components/panel/panel-icon-button.tsx +96 -0
  216. package/templates/ui/components/panel/panel-section.tsx +168 -0
  217. package/templates/ui/components/panel/panel-surface.tsx +206 -0
  218. package/templates/ui/components/panel/panel.tsx +210 -0
  219. package/templates/ui/components/primitives/animated-loader.tsx +61 -0
  220. package/templates/ui/components/primitives/button-group.tsx +134 -0
  221. package/templates/ui/components/primitives/button.tsx +429 -0
  222. package/templates/ui/components/primitives/checkbox.tsx +62 -0
  223. package/templates/ui/components/primitives/editable-slider-value-label.tsx +337 -0
  224. package/templates/ui/components/primitives/field.tsx +225 -0
  225. package/templates/ui/components/primitives/index.ts +82 -0
  226. package/templates/ui/components/primitives/input-group.tsx +298 -0
  227. package/templates/ui/components/primitives/input.tsx +61 -0
  228. package/templates/ui/components/primitives/internal/button-loading.tsx +178 -0
  229. package/templates/ui/components/primitives/label.tsx +16 -0
  230. package/templates/ui/components/primitives/popover.tsx +126 -0
  231. package/templates/ui/components/primitives/portal-layer-context.tsx +33 -0
  232. package/templates/ui/components/primitives/primitive-arrow-icon.tsx +38 -0
  233. package/templates/ui/components/primitives/scroll-fade-logic.ts +441 -0
  234. package/templates/ui/components/primitives/scroll-fade-render.tsx +75 -0
  235. package/templates/ui/components/primitives/scroll-fade-types.ts +41 -0
  236. package/templates/ui/components/primitives/scroll-fade.tsx +72 -0
  237. package/templates/ui/components/primitives/select.tsx +408 -0
  238. package/templates/ui/components/primitives/selection-state.ts +31 -0
  239. package/templates/ui/components/primitives/separator.tsx +21 -0
  240. package/templates/ui/components/primitives/slider/index.ts +4 -0
  241. package/templates/ui/components/primitives/slider/slider-interaction.tsx +96 -0
  242. package/templates/ui/components/primitives/slider/slider-parts.tsx +303 -0
  243. package/templates/ui/components/primitives/slider/slider-reset.ts +152 -0
  244. package/templates/ui/components/primitives/slider/slider-value.ts +114 -0
  245. package/templates/ui/components/primitives/slider/slider.tsx +511 -0
  246. package/templates/ui/components/primitives/switch.tsx +35 -0
  247. package/templates/ui/components/primitives/textarea.tsx +49 -0
  248. package/templates/ui/components/primitives/toggle-group.tsx +114 -0
  249. package/templates/ui/components/primitives/toggle.tsx +46 -0
  250. package/templates/ui/components/primitives/tooltip.tsx +100 -0
  251. package/templates/ui/hooks/use-mobile.ts +21 -0
  252. package/templates/ui/index.ts +31 -0
  253. package/templates/ui/lib/control-outline.ts +3 -0
  254. package/templates/ui/lib/input-control-style.ts +131 -0
  255. package/templates/ui/lib/style-guide-color-utils.ts +111 -0
  256. package/templates/ui/lib/utils.ts +6 -0
  257. package/templates/ui/styles.css +291 -0
@@ -0,0 +1,1212 @@
1
+ import { getToolcraftCanvasSizeTargetDimension } from "../schema/runtime-targets";
2
+ import {
3
+ clampToolcraftCanvasZoom,
4
+ toolcraftCanvasZoomDefault,
5
+ toolcraftCanvasZoomStep,
6
+ } from "./canvas-zoom";
7
+ import { getMediaReadyTimelineState } from "./timeline-readiness";
8
+ import type {
9
+ ToolcraftCommand,
10
+ ToolcraftHistoryPatch,
11
+ ToolcraftHistoryMode,
12
+ ToolcraftLayer,
13
+ ToolcraftLayerDraft,
14
+ ToolcraftState,
15
+ ToolcraftTimelineKeyframe,
16
+ ToolcraftTimelineKeyframeGroup,
17
+ } from "./types";
18
+
19
+ const minTimelineDurationSeconds = 1;
20
+ const maxTimelineDurationSeconds = 60;
21
+
22
+ function asCanvasSizeDimension(value: unknown): number | null {
23
+ const numberValue =
24
+ typeof value === "number"
25
+ ? value
26
+ : typeof value === "string"
27
+ ? Number.parseFloat(value)
28
+ : Number.NaN;
29
+
30
+ if (!Number.isFinite(numberValue)) {
31
+ return null;
32
+ }
33
+
34
+ return Math.max(1, Math.round(numberValue));
35
+ }
36
+
37
+ function getResetCanvasSize(
38
+ state: ToolcraftState,
39
+ ): ToolcraftState["canvas"]["size"] | null {
40
+ const width = asCanvasSizeDimension(state.defaults["canvas.size.width"]);
41
+ const height = asCanvasSizeDimension(state.defaults["canvas.size.height"]);
42
+
43
+ if (width === null && height === null) {
44
+ return null;
45
+ }
46
+
47
+ return {
48
+ ...state.canvas.size,
49
+ height: height ?? state.canvas.size.height,
50
+ width: width ?? state.canvas.size.width,
51
+ };
52
+ }
53
+
54
+ function clampTimelineDuration(value: number): number {
55
+ if (!Number.isFinite(value)) {
56
+ return minTimelineDurationSeconds;
57
+ }
58
+
59
+ return Math.max(minTimelineDurationSeconds, Math.min(maxTimelineDurationSeconds, value));
60
+ }
61
+
62
+ function clampTimelineTime(value: number, durationSeconds: number): number {
63
+ if (!Number.isFinite(value)) {
64
+ return 0;
65
+ }
66
+
67
+ return Math.max(0, Math.min(durationSeconds, value));
68
+ }
69
+
70
+ function formatTimelineSeconds(value: number): string {
71
+ return Number.isInteger(value)
72
+ ? String(value)
73
+ : value.toFixed(2).replace(/0+$/, "").replace(/\.$/, "");
74
+ }
75
+
76
+ function getRoundedTimelineKeyframeTime(value: number): number {
77
+ return Math.round(value * 100) / 100;
78
+ }
79
+
80
+ function getTimelineKeyframeId(controlId: string, timeSeconds: number): string {
81
+ return `${controlId}::${formatTimelineSeconds(timeSeconds)}`;
82
+ }
83
+
84
+ function createTimelineControlKeyframe({
85
+ controlId,
86
+ controlLabel,
87
+ state,
88
+ timeSeconds,
89
+ value,
90
+ valueLabel,
91
+ }: {
92
+ controlId: string;
93
+ controlLabel: string;
94
+ state: ToolcraftState;
95
+ timeSeconds?: number;
96
+ value: unknown;
97
+ valueLabel: string;
98
+ }): ToolcraftTimelineKeyframe {
99
+ const resolvedTimeSeconds = getRoundedTimelineKeyframeTime(
100
+ clampTimelineTime(
101
+ timeSeconds ?? state.timeline.currentTimeSeconds,
102
+ state.timeline.durationSeconds,
103
+ ),
104
+ );
105
+
106
+ return {
107
+ controlId,
108
+ controlLabel,
109
+ id: getTimelineKeyframeId(controlId, resolvedTimeSeconds),
110
+ timeSeconds: resolvedTimeSeconds,
111
+ value,
112
+ valueLabel,
113
+ };
114
+ }
115
+
116
+ function upsertTimelineControlKeyframeGroup({
117
+ controlId,
118
+ controlLabel,
119
+ keyframe,
120
+ keyframeGroups,
121
+ }: {
122
+ controlId: string;
123
+ controlLabel: string;
124
+ keyframe: ToolcraftTimelineKeyframe;
125
+ keyframeGroups: readonly ToolcraftTimelineKeyframeGroup[];
126
+ }): ToolcraftTimelineKeyframeGroup[] {
127
+ const existingGroup = keyframeGroups.find((group) => group.controlId === controlId);
128
+ const nextKeyframes = [
129
+ ...(existingGroup?.keyframes.filter((item) => item.id !== keyframe.id) ?? []),
130
+ keyframe,
131
+ ].sort(
132
+ (firstKeyframe, secondKeyframe) => firstKeyframe.timeSeconds - secondKeyframe.timeSeconds,
133
+ );
134
+ const nextGroup: ToolcraftTimelineKeyframeGroup = {
135
+ controlId,
136
+ keyframes: nextKeyframes,
137
+ label: existingGroup?.label ?? controlLabel,
138
+ };
139
+
140
+ if (!existingGroup) {
141
+ return [...keyframeGroups, nextGroup];
142
+ }
143
+
144
+ return keyframeGroups.map((group) => (group.controlId === controlId ? nextGroup : group));
145
+ }
146
+
147
+ function commitPatch(
148
+ state: ToolcraftState,
149
+ patch: ToolcraftHistoryPatch,
150
+ values: Record<string, unknown>,
151
+ historyOptions?: ToolcraftHistoryOptions,
152
+ ): ToolcraftState {
153
+ return {
154
+ ...state,
155
+ history: getNextHistoryState(state, patch, historyOptions),
156
+ values,
157
+ };
158
+ }
159
+
160
+ function commitStatePatch(
161
+ state: ToolcraftState,
162
+ patch: ToolcraftHistoryPatch,
163
+ historyOptions?: ToolcraftHistoryOptions,
164
+ ): ToolcraftState {
165
+ const next = applyHistoryPatch(state, patch.after);
166
+
167
+ return {
168
+ ...state,
169
+ canvas: next.canvas,
170
+ history: getNextHistoryState(state, patch, historyOptions),
171
+ layers: next.layers,
172
+ mediaAssets: next.mediaAssets,
173
+ selectedLayerId: next.selectedLayerId,
174
+ timeline: next.timeline,
175
+ values: next.values,
176
+ };
177
+ }
178
+
179
+ type ToolcraftHistoryOptions = {
180
+ group?: string;
181
+ mode?: ToolcraftHistoryMode;
182
+ };
183
+
184
+ function getNextHistoryState(
185
+ state: ToolcraftState,
186
+ patch: ToolcraftHistoryPatch,
187
+ options?: ToolcraftHistoryOptions,
188
+ ): ToolcraftState["history"] {
189
+ const mode = options?.mode ?? "record";
190
+
191
+ if (mode === "skip") {
192
+ return state.history;
193
+ }
194
+
195
+ const group = mode === "merge" ? options?.group : undefined;
196
+
197
+ if (group) {
198
+ const previousPatch = state.history.undo.at(-1);
199
+
200
+ if (previousPatch?.group === group) {
201
+ return {
202
+ redo: [],
203
+ undo: [
204
+ ...state.history.undo.slice(0, -1),
205
+ {
206
+ ...previousPatch,
207
+ after: patch.after,
208
+ label: patch.label,
209
+ },
210
+ ],
211
+ };
212
+ }
213
+ }
214
+
215
+ return {
216
+ redo: [],
217
+ undo: [...state.history.undo, group ? { ...patch, group } : patch],
218
+ };
219
+ }
220
+
221
+ function applyValuePatch(
222
+ values: Record<string, unknown>,
223
+ patch: Record<string, unknown>,
224
+ ): Record<string, unknown> {
225
+ const nextValues = { ...values };
226
+
227
+ for (const [target, value] of Object.entries(patch)) {
228
+ if (target in nextValues) {
229
+ nextValues[target] = value;
230
+ }
231
+ }
232
+
233
+ return nextValues;
234
+ }
235
+
236
+ function applyHistoryPatch(
237
+ state: ToolcraftState,
238
+ patch: Record<string, unknown>,
239
+ ): Pick<
240
+ ToolcraftState,
241
+ "canvas" | "layers" | "mediaAssets" | "selectedLayerId" | "timeline" | "values"
242
+ > {
243
+ const nextCanvas =
244
+ "canvas.size" in patch
245
+ ? {
246
+ ...state.canvas,
247
+ size: patch["canvas.size"] as ToolcraftState["canvas"]["size"],
248
+ }
249
+ : state.canvas;
250
+
251
+ return {
252
+ canvas: nextCanvas,
253
+ layers: "layers" in patch ? (patch.layers as ToolcraftState["layers"]) : state.layers,
254
+ mediaAssets:
255
+ "mediaAssets" in patch
256
+ ? (patch.mediaAssets as ToolcraftState["mediaAssets"])
257
+ : state.mediaAssets,
258
+ selectedLayerId:
259
+ "selectedLayerId" in patch
260
+ ? (patch.selectedLayerId as ToolcraftState["selectedLayerId"])
261
+ : state.selectedLayerId,
262
+ timeline:
263
+ "timeline" in patch ? (patch.timeline as ToolcraftState["timeline"]) : state.timeline,
264
+ values: applyValuePatch(state.values, patch),
265
+ };
266
+ }
267
+
268
+ function getNextMediaId(state: ToolcraftState): string {
269
+ const existingIds = new Set(state.mediaAssets.map((asset) => asset.id));
270
+ let index = state.mediaAssets.length + 1;
271
+
272
+ while (existingIds.has(`media-${index}`)) {
273
+ index += 1;
274
+ }
275
+
276
+ return `media-${index}`;
277
+ }
278
+
279
+ function getNextLayerId(state: ToolcraftState): string {
280
+ const existingIds = new Set(state.layers.map((layer) => layer.id));
281
+ let index = state.layers.length + 1;
282
+
283
+ while (existingIds.has(`layer-${index}`)) {
284
+ index += 1;
285
+ }
286
+
287
+ return `layer-${index}`;
288
+ }
289
+
290
+ function getSingleLayerImportId(state: ToolcraftState): string | undefined {
291
+ return state.schema.panels.layers ? undefined : state.layers.find((layer) => layer.kind !== "group")?.id;
292
+ }
293
+
294
+ function getSingleMediaImportId(state: ToolcraftState): string | undefined {
295
+ return state.schema.panels.layers ? undefined : state.mediaAssets[0]?.id;
296
+ }
297
+
298
+ function getImportedLayerName(fileName: string): string {
299
+ const name = fileName.replace(/\.[^.]+$/, "").trim();
300
+
301
+ return name || "Material";
302
+ }
303
+
304
+ function getNextLayerName(
305
+ layers: readonly ToolcraftLayer[],
306
+ prefix: "Group" | "Layer",
307
+ ): string {
308
+ const nextIndex =
309
+ layers.reduce((highestIndex, layer) => {
310
+ const label = layer.displayName ?? layer.name;
311
+ const match = new RegExp(`^${prefix} (\\d+)$`).exec(label);
312
+ const currentIndex = Number(match?.[1] ?? 0);
313
+
314
+ return Math.max(highestIndex, currentIndex);
315
+ }, 0) + 1;
316
+
317
+ return `${prefix} ${nextIndex}`;
318
+ }
319
+
320
+ function createLayer(
321
+ state: ToolcraftState,
322
+ draft: ToolcraftLayerDraft | undefined,
323
+ ): ToolcraftLayer {
324
+ const kind = draft?.kind ?? "layer";
325
+ const name =
326
+ draft?.name ??
327
+ draft?.displayName ??
328
+ getNextLayerName(state.layers, kind === "group" ? "Group" : "Layer");
329
+
330
+ return {
331
+ collapsed: kind === "group" ? (draft?.collapsed ?? false) : draft?.collapsed,
332
+ displayName: draft?.displayName ?? name,
333
+ id: draft?.id ?? getNextLayerId(state),
334
+ kind,
335
+ name,
336
+ parentGroupId: draft?.parentGroupId,
337
+ visible: draft?.visible ?? true,
338
+ };
339
+ }
340
+
341
+ function clampInsertIndex(length: number, insertIndex: number | undefined): number {
342
+ return Math.max(0, Math.min(length, insertIndex ?? length));
343
+ }
344
+
345
+ function getLayerBlockIds(
346
+ layers: readonly ToolcraftLayer[],
347
+ layerId: string,
348
+ ): Set<string> {
349
+ const blockIds = new Set<string>([layerId]);
350
+ let changed = true;
351
+
352
+ while (changed) {
353
+ changed = false;
354
+
355
+ for (const layer of layers) {
356
+ if (layer.parentGroupId && blockIds.has(layer.parentGroupId) && !blockIds.has(layer.id)) {
357
+ blockIds.add(layer.id);
358
+ changed = true;
359
+ }
360
+ }
361
+ }
362
+
363
+ return blockIds;
364
+ }
365
+
366
+ function canMoveLayerToParent(
367
+ layers: readonly ToolcraftLayer[],
368
+ layerId: string,
369
+ parentGroupId: string | null,
370
+ ): boolean {
371
+ if (!parentGroupId) {
372
+ return true;
373
+ }
374
+
375
+ if (layerId === parentGroupId) {
376
+ return false;
377
+ }
378
+
379
+ const parent = layers.find((layer) => layer.id === parentGroupId);
380
+
381
+ if (!parent || parent.kind !== "group") {
382
+ return false;
383
+ }
384
+
385
+ return !getLayerBlockIds(layers, layerId).has(parentGroupId);
386
+ }
387
+
388
+ function mapTimelineKeyframeGroups(
389
+ keyframeGroups: readonly ToolcraftTimelineKeyframeGroup[],
390
+ keyframeId: string,
391
+ updateKeyframe: (
392
+ keyframe: ToolcraftTimelineKeyframeGroup["keyframes"][number],
393
+ ) => ToolcraftTimelineKeyframeGroup["keyframes"][number],
394
+ ): ToolcraftTimelineKeyframeGroup[] {
395
+ return keyframeGroups.map((group) => ({
396
+ ...group,
397
+ keyframes: group.keyframes.map((keyframe) =>
398
+ keyframe.id === keyframeId ? updateKeyframe(keyframe) : keyframe,
399
+ ),
400
+ }));
401
+ }
402
+
403
+ export function toolcraftReducer(
404
+ state: ToolcraftState,
405
+ command: ToolcraftCommand,
406
+ ): ToolcraftState {
407
+ switch (command.type) {
408
+ case "controls.setValue": {
409
+ const canvasSizeDimension = getToolcraftCanvasSizeTargetDimension(command.target);
410
+
411
+ if (canvasSizeDimension) {
412
+ const dimensionValue = asCanvasSizeDimension(command.value);
413
+
414
+ if (dimensionValue === null) {
415
+ return state;
416
+ }
417
+
418
+ if (
419
+ state.canvas.size[canvasSizeDimension] === dimensionValue &&
420
+ state.values[command.target] === dimensionValue
421
+ ) {
422
+ return state;
423
+ }
424
+
425
+ const size = {
426
+ ...state.canvas.size,
427
+ [canvasSizeDimension]: dimensionValue,
428
+ };
429
+
430
+ return commitStatePatch(state, {
431
+ after: {
432
+ "canvas.size": size,
433
+ [command.target]: dimensionValue,
434
+ },
435
+ before: {
436
+ "canvas.size": state.canvas.size,
437
+ [command.target]: state.values[command.target],
438
+ },
439
+ label: command.label ?? command.target,
440
+ }, {
441
+ group: command.historyGroup,
442
+ mode: command.history,
443
+ });
444
+ }
445
+
446
+ if (Object.is(state.values[command.target], command.value)) {
447
+ return state;
448
+ }
449
+
450
+ return commitPatch(
451
+ state,
452
+ {
453
+ after: { [command.target]: command.value },
454
+ before: { [command.target]: state.values[command.target] },
455
+ label: command.label ?? command.target,
456
+ },
457
+ { ...state.values, [command.target]: command.value },
458
+ {
459
+ group: command.historyGroup,
460
+ mode: command.history,
461
+ },
462
+ );
463
+ }
464
+
465
+ case "controls.apply":
466
+ return state;
467
+
468
+ case "controls.reset": {
469
+ const resetCanvasSize = getResetCanvasSize(state);
470
+
471
+ if (resetCanvasSize) {
472
+ return commitStatePatch(state, {
473
+ after: {
474
+ ...state.defaults,
475
+ "canvas.size": resetCanvasSize,
476
+ },
477
+ before: {
478
+ ...state.values,
479
+ "canvas.size": state.canvas.size,
480
+ },
481
+ label: "Reset controls",
482
+ });
483
+ }
484
+
485
+ return commitPatch(
486
+ state,
487
+ {
488
+ after: { ...state.defaults },
489
+ before: { ...state.values },
490
+ label: "Reset controls",
491
+ },
492
+ { ...state.defaults },
493
+ );
494
+ }
495
+
496
+ case "layers.add": {
497
+ const layer = createLayer(state, command.layer);
498
+ const insertIndex = clampInsertIndex(state.layers.length, command.insertIndex);
499
+ const layers = [
500
+ ...state.layers.slice(0, insertIndex),
501
+ layer,
502
+ ...state.layers.slice(insertIndex),
503
+ ];
504
+
505
+ return commitStatePatch(state, {
506
+ after: {
507
+ layers,
508
+ selectedLayerId: layer.id,
509
+ },
510
+ before: {
511
+ layers: state.layers,
512
+ selectedLayerId: state.selectedLayerId,
513
+ },
514
+ label: layer.kind === "group" ? "Add group" : "Add layer",
515
+ });
516
+ }
517
+
518
+ case "layers.delete": {
519
+ if (!state.layers.some((layer) => layer.id === command.layerId)) {
520
+ return state;
521
+ }
522
+
523
+ const deletedLayerIds = getLayerBlockIds(state.layers, command.layerId);
524
+ const layers = state.layers.filter((layer) => !deletedLayerIds.has(layer.id));
525
+ const mediaAssets = state.mediaAssets.filter((asset) => !deletedLayerIds.has(asset.layerId));
526
+ const selectedLayerId = deletedLayerIds.has(state.selectedLayerId ?? "")
527
+ ? (layers[0]?.id ?? null)
528
+ : state.selectedLayerId;
529
+
530
+ return commitStatePatch(state, {
531
+ after: {
532
+ layers,
533
+ mediaAssets,
534
+ selectedLayerId,
535
+ },
536
+ before: {
537
+ layers: state.layers,
538
+ mediaAssets: state.mediaAssets,
539
+ selectedLayerId: state.selectedLayerId,
540
+ },
541
+ label: "Delete layer",
542
+ });
543
+ }
544
+
545
+ case "layers.moveToGroup": {
546
+ const movedRootLayerIds = new Set(
547
+ command.layerIds.filter((layerId) =>
548
+ canMoveLayerToParent(state.layers, layerId, command.parentGroupId),
549
+ ),
550
+ );
551
+
552
+ if (movedRootLayerIds.size === 0) {
553
+ return state;
554
+ }
555
+
556
+ const nextParentGroupId = command.parentGroupId ?? undefined;
557
+ const movedBlockIds = new Set<string>();
558
+
559
+ for (const layerId of movedRootLayerIds) {
560
+ getLayerBlockIds(state.layers, layerId).forEach((blockLayerId) => {
561
+ movedBlockIds.add(blockLayerId);
562
+ });
563
+ }
564
+
565
+ const movingBlock = state.layers.filter((layer) => movedBlockIds.has(layer.id));
566
+ const updatedMovingBlock = movingBlock.map((layer) =>
567
+ movedRootLayerIds.has(layer.id) ? { ...layer, parentGroupId: nextParentGroupId } : layer,
568
+ );
569
+ const remainingLayers = state.layers.filter((layer) => !movedBlockIds.has(layer.id));
570
+ const targetGroupIndex = command.parentGroupId
571
+ ? remainingLayers.findIndex((layer) => layer.id === command.parentGroupId)
572
+ : -1;
573
+ const movedLayers = command.parentGroupId
574
+ ? targetGroupIndex >= 0
575
+ ? [
576
+ ...remainingLayers.slice(0, targetGroupIndex + 1),
577
+ ...updatedMovingBlock,
578
+ ...remainingLayers.slice(targetGroupIndex + 1),
579
+ ]
580
+ : state.layers
581
+ : state.layers.map((layer) =>
582
+ movedRootLayerIds.has(layer.id) ? { ...layer, parentGroupId: nextParentGroupId } : layer,
583
+ );
584
+ const layers = command.parentGroupId
585
+ ? movedLayers.map((layer) =>
586
+ layer.id === command.parentGroupId && layer.kind === "group" && layer.collapsed
587
+ ? { ...layer, collapsed: false }
588
+ : layer,
589
+ )
590
+ : movedLayers;
591
+
592
+ if (layers === state.layers) {
593
+ return state;
594
+ }
595
+
596
+ if (
597
+ layers.every(
598
+ (layer, index) =>
599
+ layer.id === state.layers[index]?.id &&
600
+ layer.parentGroupId === state.layers[index]?.parentGroupId &&
601
+ layer.collapsed === state.layers[index]?.collapsed,
602
+ )
603
+ ) {
604
+ return state;
605
+ }
606
+
607
+ return commitStatePatch(state, {
608
+ after: { layers },
609
+ before: { layers: state.layers },
610
+ label: command.parentGroupId ? "Move layers to group" : "Move layers to root",
611
+ });
612
+ }
613
+
614
+ case "layers.select":
615
+ if (!state.layers.some((layer) => layer.id === command.layerId)) {
616
+ return state;
617
+ }
618
+
619
+ return {
620
+ ...state,
621
+ selectedLayerId: command.layerId,
622
+ };
623
+
624
+ case "layers.rename": {
625
+ const name = command.name.trim();
626
+
627
+ if (!name || !state.layers.some((layer) => layer.id === command.layerId)) {
628
+ return state;
629
+ }
630
+
631
+ const layers = state.layers.map((layer) =>
632
+ layer.id === command.layerId ? { ...layer, displayName: name } : layer,
633
+ );
634
+
635
+ return commitStatePatch(state, {
636
+ after: { layers },
637
+ before: { layers: state.layers },
638
+ label: "Rename layer",
639
+ });
640
+ }
641
+
642
+ case "layers.toggleCollapsed": {
643
+ const targetLayer = state.layers.find((layer) => layer.id === command.layerId);
644
+
645
+ if (!targetLayer || targetLayer.kind !== "group") {
646
+ return state;
647
+ }
648
+
649
+ const layers = state.layers.map((layer) =>
650
+ layer.id === command.layerId ? { ...layer, collapsed: !layer.collapsed } : layer,
651
+ );
652
+
653
+ return commitStatePatch(state, {
654
+ after: { layers },
655
+ before: { layers: state.layers },
656
+ label: "Toggle group",
657
+ });
658
+ }
659
+
660
+ case "layers.toggleVisibility": {
661
+ if (!state.layers.some((layer) => layer.id === command.layerId)) {
662
+ return state;
663
+ }
664
+
665
+ const layers = state.layers.map((layer) =>
666
+ layer.id === command.layerId ? { ...layer, visible: !layer.visible } : layer,
667
+ );
668
+
669
+ return commitStatePatch(state, {
670
+ after: { layers },
671
+ before: { layers: state.layers },
672
+ label: "Toggle layer visibility",
673
+ });
674
+ }
675
+
676
+ case "layers.reorder": {
677
+ const nextLayerIds = new Set(command.layers.map((layer) => layer.id));
678
+
679
+ if (nextLayerIds.size !== command.layers.length || nextLayerIds.size !== state.layers.length) {
680
+ return state;
681
+ }
682
+
683
+ if (!state.layers.every((layer) => nextLayerIds.has(layer.id))) {
684
+ return state;
685
+ }
686
+
687
+ return commitStatePatch(state, {
688
+ after: {
689
+ layers: command.layers,
690
+ selectedLayerId: command.selectedLayerId ?? state.selectedLayerId,
691
+ },
692
+ before: {
693
+ layers: state.layers,
694
+ selectedLayerId: state.selectedLayerId,
695
+ },
696
+ label: "Reorder layers",
697
+ });
698
+ }
699
+
700
+ case "canvas.setOffset":
701
+ return {
702
+ ...state,
703
+ canvas: {
704
+ ...state.canvas,
705
+ offset: command.offset,
706
+ },
707
+ };
708
+
709
+ case "canvas.panBy":
710
+ return {
711
+ ...state,
712
+ canvas: {
713
+ ...state.canvas,
714
+ offset: {
715
+ x: state.canvas.offset.x + command.delta.x,
716
+ y: state.canvas.offset.y + command.delta.y,
717
+ },
718
+ },
719
+ };
720
+
721
+ case "canvas.setSize":
722
+ return {
723
+ ...state,
724
+ canvas: {
725
+ ...state.canvas,
726
+ size: command.size,
727
+ },
728
+ history: {
729
+ redo: [],
730
+ undo: [
731
+ ...state.history.undo,
732
+ {
733
+ after: { "canvas.size": command.size },
734
+ before: { "canvas.size": state.canvas.size },
735
+ label: "Resize canvas",
736
+ },
737
+ ],
738
+ },
739
+ };
740
+
741
+ case "canvas.center":
742
+ return {
743
+ ...state,
744
+ canvas: {
745
+ ...state.canvas,
746
+ offset: { x: 0, y: 0 },
747
+ },
748
+ };
749
+
750
+ case "canvas.zoomIn":
751
+ return {
752
+ ...state,
753
+ canvas: {
754
+ ...state.canvas,
755
+ zoom: clampToolcraftCanvasZoom(state.canvas.zoom + toolcraftCanvasZoomStep),
756
+ },
757
+ };
758
+
759
+ case "canvas.zoomOut":
760
+ return {
761
+ ...state,
762
+ canvas: {
763
+ ...state.canvas,
764
+ zoom: clampToolcraftCanvasZoom(state.canvas.zoom - toolcraftCanvasZoomStep),
765
+ },
766
+ };
767
+
768
+ case "canvas.zoomReset":
769
+ return {
770
+ ...state,
771
+ canvas: {
772
+ ...state.canvas,
773
+ zoom: toolcraftCanvasZoomDefault,
774
+ },
775
+ };
776
+
777
+ case "canvas.setViewport":
778
+ return {
779
+ ...state,
780
+ canvas: {
781
+ ...state.canvas,
782
+ offset: command.offset,
783
+ zoom: clampToolcraftCanvasZoom(command.zoom),
784
+ },
785
+ };
786
+
787
+ case "panels.setOffset":
788
+ return {
789
+ ...state,
790
+ panels: {
791
+ ...state.panels,
792
+ [command.panelId]: {
793
+ ...state.panels[command.panelId],
794
+ offset: command.offset,
795
+ },
796
+ },
797
+ };
798
+
799
+ case "panels.resetOffset":
800
+ return {
801
+ ...state,
802
+ panels: {
803
+ ...state.panels,
804
+ [command.panelId]: {
805
+ ...state.panels[command.panelId],
806
+ offset: { x: 0, y: 0 },
807
+ },
808
+ },
809
+ };
810
+
811
+ case "media.import": {
812
+ const shouldReplaceSingleLayerMedia = !state.schema.panels.layers;
813
+ const shouldResizeCanvas =
814
+ state.schema.canvas.sizing.mode === "intrinsic-media";
815
+ const layerId =
816
+ command.asset.layerId ?? getSingleLayerImportId(state) ?? getNextLayerId(state);
817
+ const mediaId =
818
+ command.asset.id ?? getSingleMediaImportId(state) ?? getNextMediaId(state);
819
+ const layer = {
820
+ displayName: command.asset.layerName ?? getImportedLayerName(command.asset.fileName),
821
+ id: layerId,
822
+ kind: "layer" as const,
823
+ name: command.asset.layerName ?? getImportedLayerName(command.asset.fileName),
824
+ visible: true,
825
+ };
826
+ const mediaAsset = {
827
+ dataUrl: command.asset.dataUrl,
828
+ fileName: command.asset.fileName,
829
+ id: mediaId,
830
+ layerId,
831
+ mimeType: command.asset.mimeType,
832
+ position: shouldResizeCanvas ? { x: 0, y: 0 } : command.asset.position,
833
+ size: command.asset.size,
834
+ };
835
+ const layers = shouldReplaceSingleLayerMedia ? [layer] : [...state.layers, layer];
836
+ const mediaAssets = shouldReplaceSingleLayerMedia
837
+ ? [mediaAsset]
838
+ : [...state.mediaAssets, mediaAsset];
839
+ const after = {
840
+ ...(shouldResizeCanvas ? { "canvas.size": command.asset.size } : {}),
841
+ layers,
842
+ mediaAssets,
843
+ selectedLayerId: layerId,
844
+ };
845
+ const before = {
846
+ ...(shouldResizeCanvas ? { "canvas.size": state.canvas.size } : {}),
847
+ layers: state.layers,
848
+ mediaAssets: state.mediaAssets,
849
+ selectedLayerId: state.selectedLayerId,
850
+ };
851
+
852
+ return commitStatePatch(state, {
853
+ after,
854
+ before,
855
+ label: "Import media",
856
+ });
857
+ }
858
+
859
+ case "media.delete": {
860
+ if (!state.mediaAssets.some((asset) => asset.id === command.mediaId)) {
861
+ return state;
862
+ }
863
+
864
+ const mediaAssets = state.mediaAssets.filter((asset) => asset.id !== command.mediaId);
865
+ const timeline = getMediaReadyTimelineState(state.schema, state.timeline, mediaAssets);
866
+ const shouldCommitTimeline = timeline !== state.timeline;
867
+
868
+ return commitStatePatch(state, {
869
+ after: {
870
+ mediaAssets,
871
+ ...(shouldCommitTimeline ? { timeline } : {}),
872
+ },
873
+ before: {
874
+ mediaAssets: state.mediaAssets,
875
+ ...(shouldCommitTimeline ? { timeline: state.timeline } : {}),
876
+ },
877
+ label: "Delete media",
878
+ });
879
+ }
880
+
881
+ case "timeline.setCurrentTime": {
882
+ return {
883
+ ...state,
884
+ timeline: {
885
+ ...state.timeline,
886
+ currentTimeSeconds: clampTimelineTime(
887
+ command.currentTimeSeconds,
888
+ state.timeline.durationSeconds,
889
+ ),
890
+ },
891
+ };
892
+ }
893
+
894
+ case "timeline.setDuration": {
895
+ const durationSeconds = clampTimelineDuration(command.durationSeconds);
896
+ const timeline = {
897
+ ...state.timeline,
898
+ currentTimeSeconds: clampTimelineTime(state.timeline.currentTimeSeconds, durationSeconds),
899
+ durationSeconds,
900
+ };
901
+
902
+ return commitStatePatch(state, {
903
+ after: { timeline },
904
+ before: { timeline: state.timeline },
905
+ label: "Set timeline duration",
906
+ });
907
+ }
908
+
909
+ case "timeline.setExpanded": {
910
+ if (state.timeline.expanded === command.expanded) {
911
+ return state;
912
+ }
913
+
914
+ return {
915
+ ...state,
916
+ timeline: {
917
+ ...state.timeline,
918
+ expanded: command.expanded,
919
+ },
920
+ };
921
+ }
922
+
923
+ case "timeline.toggleExpanded": {
924
+ return {
925
+ ...state,
926
+ timeline: {
927
+ ...state.timeline,
928
+ expanded: !state.timeline.expanded,
929
+ },
930
+ };
931
+ }
932
+
933
+ case "timeline.setPlaying": {
934
+ return {
935
+ ...state,
936
+ timeline: {
937
+ ...state.timeline,
938
+ isPlaying: command.isPlaying,
939
+ },
940
+ };
941
+ }
942
+
943
+ case "timeline.togglePlayback": {
944
+ const shouldRestartPlayback =
945
+ !state.timeline.isPlaying &&
946
+ state.timeline.currentTimeSeconds >= state.timeline.durationSeconds;
947
+
948
+ return {
949
+ ...state,
950
+ timeline: {
951
+ ...state.timeline,
952
+ currentTimeSeconds: shouldRestartPlayback ? 0 : state.timeline.currentTimeSeconds,
953
+ isPlaying: !state.timeline.isPlaying,
954
+ },
955
+ };
956
+ }
957
+
958
+ case "timeline.toggleLoop": {
959
+ return {
960
+ ...state,
961
+ timeline: {
962
+ ...state.timeline,
963
+ isLooping: !state.timeline.isLooping,
964
+ },
965
+ };
966
+ }
967
+
968
+ case "timeline.selectKeyframe": {
969
+ return {
970
+ ...state,
971
+ timeline: {
972
+ ...state.timeline,
973
+ selectedKeyframeId: command.keyframeId,
974
+ },
975
+ };
976
+ }
977
+
978
+ case "timeline.deleteKeyframe": {
979
+ if (
980
+ !state.timeline.keyframeGroups.some((group) =>
981
+ group.keyframes.some((keyframe) => keyframe.id === command.keyframeId),
982
+ )
983
+ ) {
984
+ return state;
985
+ }
986
+
987
+ const timeline = {
988
+ ...state.timeline,
989
+ keyframeGroups: state.timeline.keyframeGroups
990
+ .map((group) => ({
991
+ ...group,
992
+ keyframes: group.keyframes.filter((keyframe) => keyframe.id !== command.keyframeId),
993
+ }))
994
+ .filter((group) => group.keyframes.length > 0),
995
+ selectedKeyframeId: null,
996
+ };
997
+
998
+ return commitStatePatch(state, {
999
+ after: { timeline },
1000
+ before: { timeline: state.timeline },
1001
+ label: "Delete keyframe",
1002
+ });
1003
+ }
1004
+
1005
+ case "timeline.deleteControlKeyframes": {
1006
+ if (!state.timeline.keyframeGroups.some((group) => group.controlId === command.controlId)) {
1007
+ return state;
1008
+ }
1009
+
1010
+ const timeline = {
1011
+ ...state.timeline,
1012
+ keyframeGroups: state.timeline.keyframeGroups.filter(
1013
+ (group) => group.controlId !== command.controlId,
1014
+ ),
1015
+ selectedKeyframeId: null,
1016
+ };
1017
+
1018
+ return commitStatePatch(state, {
1019
+ after: { timeline },
1020
+ before: { timeline: state.timeline },
1021
+ label: "Delete control keyframes",
1022
+ });
1023
+ }
1024
+
1025
+ case "timeline.toggleControlKeyframes": {
1026
+ const existingGroup = state.timeline.keyframeGroups.find(
1027
+ (group) => group.controlId === command.controlId,
1028
+ );
1029
+
1030
+ if (existingGroup) {
1031
+ const timeline = {
1032
+ ...state.timeline,
1033
+ expanded: true,
1034
+ keyframeGroups: state.timeline.keyframeGroups.filter(
1035
+ (group) => group.controlId !== command.controlId,
1036
+ ),
1037
+ selectedKeyframeId: null,
1038
+ };
1039
+
1040
+ return commitStatePatch(state, {
1041
+ after: { timeline },
1042
+ before: { timeline: state.timeline },
1043
+ label: "Delete control keyframes",
1044
+ });
1045
+ }
1046
+
1047
+ const keyframe = createTimelineControlKeyframe({
1048
+ controlId: command.controlId,
1049
+ controlLabel: command.controlLabel,
1050
+ state,
1051
+ timeSeconds: command.timeSeconds,
1052
+ value: command.value,
1053
+ valueLabel: command.valueLabel,
1054
+ });
1055
+ const timeline = {
1056
+ ...state.timeline,
1057
+ expanded: true,
1058
+ keyframeGroups: upsertTimelineControlKeyframeGroup({
1059
+ controlId: command.controlId,
1060
+ controlLabel: command.controlLabel,
1061
+ keyframe,
1062
+ keyframeGroups: state.timeline.keyframeGroups,
1063
+ }),
1064
+ selectedKeyframeId: keyframe.id,
1065
+ };
1066
+
1067
+ return commitStatePatch(state, {
1068
+ after: { timeline },
1069
+ before: { timeline: state.timeline },
1070
+ label: "Add control keyframe",
1071
+ });
1072
+ }
1073
+
1074
+ case "timeline.upsertControlKeyframe": {
1075
+ const keyframe = createTimelineControlKeyframe({
1076
+ controlId: command.controlId,
1077
+ controlLabel: command.controlLabel,
1078
+ state,
1079
+ timeSeconds: command.timeSeconds,
1080
+ value: command.value,
1081
+ valueLabel: command.valueLabel,
1082
+ });
1083
+ const timeline = {
1084
+ ...state.timeline,
1085
+ expanded: true,
1086
+ keyframeGroups: upsertTimelineControlKeyframeGroup({
1087
+ controlId: command.controlId,
1088
+ controlLabel: command.controlLabel,
1089
+ keyframe,
1090
+ keyframeGroups: state.timeline.keyframeGroups,
1091
+ }),
1092
+ selectedKeyframeId: keyframe.id,
1093
+ };
1094
+
1095
+ return commitStatePatch(state, {
1096
+ after: { timeline },
1097
+ before: { timeline: state.timeline },
1098
+ label: "Set control keyframe",
1099
+ });
1100
+ }
1101
+
1102
+ case "timeline.moveKeyframe": {
1103
+ const targetKeyframe = state.timeline.keyframeGroups
1104
+ .flatMap((group) => group.keyframes)
1105
+ .find((keyframe) => keyframe.id === command.keyframeId);
1106
+
1107
+ if (!targetKeyframe) {
1108
+ return state;
1109
+ }
1110
+
1111
+ const timeSeconds = getRoundedTimelineKeyframeTime(
1112
+ clampTimelineTime(command.timeSeconds, state.timeline.durationSeconds),
1113
+ );
1114
+ const nextKeyframeId = getTimelineKeyframeId(targetKeyframe.controlId, timeSeconds);
1115
+ const timeline = {
1116
+ ...state.timeline,
1117
+ keyframeGroups: mapTimelineKeyframeGroups(
1118
+ state.timeline.keyframeGroups,
1119
+ command.keyframeId,
1120
+ (keyframe) => ({
1121
+ ...keyframe,
1122
+ id: nextKeyframeId,
1123
+ timeSeconds,
1124
+ }),
1125
+ ),
1126
+ selectedKeyframeId: nextKeyframeId,
1127
+ };
1128
+
1129
+ return commitStatePatch(state, {
1130
+ after: { timeline },
1131
+ before: { timeline: state.timeline },
1132
+ label: "Move keyframe",
1133
+ });
1134
+ }
1135
+
1136
+ case "timeline.changeKeyframeEasing": {
1137
+ if (
1138
+ !state.timeline.keyframeGroups.some((group) =>
1139
+ group.keyframes.some((keyframe) => keyframe.id === command.keyframeId),
1140
+ )
1141
+ ) {
1142
+ return state;
1143
+ }
1144
+
1145
+ const timeline = {
1146
+ ...state.timeline,
1147
+ keyframeGroups: mapTimelineKeyframeGroups(
1148
+ state.timeline.keyframeGroups,
1149
+ command.keyframeId,
1150
+ (keyframe) => ({
1151
+ ...keyframe,
1152
+ easing: command.easing,
1153
+ }),
1154
+ ),
1155
+ };
1156
+
1157
+ return commitStatePatch(state, {
1158
+ after: { timeline },
1159
+ before: { timeline: state.timeline },
1160
+ label: "Change keyframe easing",
1161
+ });
1162
+ }
1163
+
1164
+ case "history.undo": {
1165
+ const patch = state.history.undo.at(-1);
1166
+
1167
+ if (!patch) {
1168
+ return state;
1169
+ }
1170
+
1171
+ const next = applyHistoryPatch(state, patch.before);
1172
+
1173
+ return {
1174
+ ...state,
1175
+ canvas: next.canvas,
1176
+ history: {
1177
+ redo: [...state.history.redo, patch],
1178
+ undo: state.history.undo.slice(0, -1),
1179
+ },
1180
+ layers: next.layers,
1181
+ mediaAssets: next.mediaAssets,
1182
+ selectedLayerId: next.selectedLayerId,
1183
+ timeline: next.timeline,
1184
+ values: next.values,
1185
+ };
1186
+ }
1187
+
1188
+ case "history.redo": {
1189
+ const patch = state.history.redo.at(-1);
1190
+
1191
+ if (!patch) {
1192
+ return state;
1193
+ }
1194
+
1195
+ const next = applyHistoryPatch(state, patch.after);
1196
+
1197
+ return {
1198
+ ...state,
1199
+ canvas: next.canvas,
1200
+ history: {
1201
+ redo: state.history.redo.slice(0, -1),
1202
+ undo: [...state.history.undo, patch],
1203
+ },
1204
+ layers: next.layers,
1205
+ mediaAssets: next.mediaAssets,
1206
+ selectedLayerId: next.selectedLayerId,
1207
+ timeline: next.timeline,
1208
+ values: next.values,
1209
+ };
1210
+ }
1211
+ }
1212
+ }