@overmvp/kumi 0.0.0-stage → 0.3.0

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 (319) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +67 -2
  3. package/animation/color-transition.ts +196 -0
  4. package/animation/cycle.ts +151 -0
  5. package/animation/ease.ts +29 -0
  6. package/animation/index.ts +13 -0
  7. package/animation/package.json +12 -0
  8. package/animation/presets.ts +376 -0
  9. package/animation/spec.ts +197 -0
  10. package/builder/Cargo.lock +192 -0
  11. package/builder/Cargo.toml +7 -0
  12. package/builder/css-v1.mjs +387 -0
  13. package/builder/css-v1.test.mjs +361 -0
  14. package/builder/generated-output.mjs +61 -0
  15. package/builder/generated-output.test.mjs +132 -0
  16. package/builder/jsx-form.mjs +93 -0
  17. package/builder/jsx-form.test.mjs +48 -0
  18. package/builder/jsx-lists.mjs +209 -0
  19. package/builder/link-addon-package.mjs +127 -0
  20. package/builder/lua-assets.mjs +74 -0
  21. package/builder/lua-builtin-values.mjs +57 -0
  22. package/builder/lua-builtin-values.test.mjs +155 -0
  23. package/builder/lua-loop-closures.mjs +183 -0
  24. package/builder/lua-loop-closures.test.mjs +89 -0
  25. package/builder/lua-truthiness.mjs +61 -0
  26. package/builder/lua-truthiness.test.mjs +113 -0
  27. package/builder/module-declarations.mjs +594 -0
  28. package/builder/module-declarations.test.mjs +435 -0
  29. package/builder/native-kernels.mjs +60 -0
  30. package/builder/native-lua.d.ts +57 -0
  31. package/builder/native-routes.mjs +51 -0
  32. package/builder/no-static-ref-read.mjs +93 -0
  33. package/builder/no-static-ref-read.test.mjs +181 -0
  34. package/builder/secure-snippet.mjs +317 -0
  35. package/builder/secure-snippet.test.mjs +101 -0
  36. package/builder/shared-lua-runtime.mjs +150 -0
  37. package/builder/src/addon-link.lua +29 -0
  38. package/builder/src/arrays.lua +260 -0
  39. package/builder/src/assets.rs +177 -0
  40. package/builder/src/classes.lua +75 -0
  41. package/builder/src/collections.lua +190 -0
  42. package/builder/src/compiler.rs +2336 -0
  43. package/builder/src/config.rs +42 -0
  44. package/builder/src/entries.rs +166 -0
  45. package/builder/src/errors.lua +15 -0
  46. package/builder/src/lua_data.rs +231 -0
  47. package/builder/src/main.rs +381 -0
  48. package/builder/src/native_ir.rs +65 -0
  49. package/builder/src/native_signal.rs +83 -0
  50. package/builder/src/numbers.lua +76 -0
  51. package/builder/src/project-front.mjs +3682 -0
  52. package/builder/src/records.lua +75 -0
  53. package/builder/src/routes.rs +124 -0
  54. package/builder/src/setup-store.lua +18 -0
  55. package/builder/src/signal-kernel.fingerprint +1 -0
  56. package/builder/src/strings.lua +103 -0
  57. package/builder/src/ts-lower.mjs +68 -0
  58. package/builder/src/wow-reads.lua +22 -0
  59. package/builder/src/wow_client.rs +253 -0
  60. package/builder/src/wow_types.rs +564 -0
  61. package/builder/style-modules.mjs +93 -0
  62. package/builder/style-states.mjs +100 -0
  63. package/builder/style-states.test.mjs +103 -0
  64. package/builder/vite-assets.mjs +53 -0
  65. package/builder/vite-project.mjs +10 -0
  66. package/config.ts +59 -0
  67. package/eslint.mjs +74 -0
  68. package/form/index.ts +526 -0
  69. package/form/package.json +13 -0
  70. package/motion/index.ts +2 -0
  71. package/motion/package.json +10 -0
  72. package/package.json +26 -4
  73. package/router/index.ts +50 -0
  74. package/router/navigation.ts +22 -0
  75. package/router/package.json +12 -0
  76. package/router/router-view.ts +134 -0
  77. package/router/routes.ts +64 -0
  78. package/scripts/addon-build.mjs +364 -0
  79. package/scripts/addon-project.mjs +78 -0
  80. package/scripts/kumi-cli.mjs +105 -0
  81. package/scripts/link-addons.mjs +61 -0
  82. package/src/bindings.ts +35 -0
  83. package/src/context.ts +15 -0
  84. package/src/css-tokens.ts +239 -0
  85. package/src/css-v1.ts +68 -0
  86. package/src/cvars.ts +91 -0
  87. package/src/element-node.ts +32 -0
  88. package/src/elements.ts +505 -0
  89. package/src/feature-scope.ts +82 -0
  90. package/src/flex-layout.ts +807 -0
  91. package/src/frame-scheduler.ts +336 -0
  92. package/src/grid-tracks.ts +165 -0
  93. package/src/image-imports.d.ts +24 -0
  94. package/src/images.ts +79 -0
  95. package/src/index.ts +29 -0
  96. package/src/jsx-dev-runtime.ts +2 -0
  97. package/src/jsx-runtime.ts +29 -0
  98. package/src/jsx.d.ts +12 -0
  99. package/src/keyed-list.ts +366 -0
  100. package/src/media-query.ts +144 -0
  101. package/src/module-ui.ts +147 -0
  102. package/src/module.ts +311 -0
  103. package/src/node-selectors.ts +114 -0
  104. package/src/owned-collections.ts +52 -0
  105. package/src/providers.ts +241 -0
  106. package/src/reactive-scope.ts +34 -0
  107. package/src/reactivity.ts +275 -0
  108. package/src/result.ts +64 -0
  109. package/src/runtime.ts +52 -0
  110. package/src/scheduler.ts +185 -0
  111. package/src/signal-kernel.ts +63 -0
  112. package/src/style-classes.ts +51 -0
  113. package/src/style-states.ts +134 -0
  114. package/src/style-variables.ts +53 -0
  115. package/src/text-children.ts +42 -0
  116. package/src/theme.ts +50 -0
  117. package/src/transition.ts +18 -0
  118. package/state/index.ts +9 -0
  119. package/state/package.json +12 -0
  120. package/state/setup-store-persistence.ts +206 -0
  121. package/state/setup-store-view.ts +10 -0
  122. package/state/setup-store.ts +342 -0
  123. package/state/store-bindings.ts +25 -0
  124. package/state/store.ts +84 -0
  125. package/styles/_layers.scss +14 -0
  126. package/templates/addon/.agents/skills/kumi-build-debug/SKILL.md +36 -0
  127. package/templates/addon/.agents/skills/kumi-feature/SKILL.md +50 -0
  128. package/templates/addon/.agents/skills/kumi-plugin/SKILL.md +36 -0
  129. package/templates/addon/.agents/skills/kumi-store/SKILL.md +47 -0
  130. package/templates/addon/.agents/skills/kumi-ui/SKILL.md +73 -0
  131. package/templates/addon/.agents/skills/kumi-wow-api/SKILL.md +30 -0
  132. package/templates/addon/.claude/skills/kumi-build-debug/SKILL.md +36 -0
  133. package/templates/addon/.claude/skills/kumi-feature/SKILL.md +50 -0
  134. package/templates/addon/.claude/skills/kumi-plugin/SKILL.md +36 -0
  135. package/templates/addon/.claude/skills/kumi-store/SKILL.md +47 -0
  136. package/templates/addon/.claude/skills/kumi-ui/SKILL.md +73 -0
  137. package/templates/addon/.claude/skills/kumi-wow-api/SKILL.md +30 -0
  138. package/templates/addon/AGENTS.md +34 -0
  139. package/templates/addon/Assets/Art/Corner10.tga +0 -0
  140. package/templates/addon/Assets/Art/Corner11.tga +0 -0
  141. package/templates/addon/Assets/Art/Corner12.tga +0 -0
  142. package/templates/addon/Assets/Art/Corner13.tga +0 -0
  143. package/templates/addon/Assets/Art/Corner14.tga +0 -0
  144. package/templates/addon/Assets/Art/Corner15.tga +0 -0
  145. package/templates/addon/Assets/Art/Corner16.tga +0 -0
  146. package/templates/addon/Assets/Art/Corner2.tga +0 -0
  147. package/templates/addon/Assets/Art/Corner3.tga +0 -0
  148. package/templates/addon/Assets/Art/Corner4.tga +0 -0
  149. package/templates/addon/Assets/Art/Corner5.tga +0 -0
  150. package/templates/addon/Assets/Art/Corner6.tga +0 -0
  151. package/templates/addon/Assets/Art/Corner7.tga +0 -0
  152. package/templates/addon/Assets/Art/Corner8.tga +0 -0
  153. package/templates/addon/Assets/Art/Corner9.tga +0 -0
  154. package/templates/addon/Assets/Art/Dot.tga +0 -0
  155. package/templates/addon/Assets/Art/Gear.tga +0 -0
  156. package/templates/addon/Assets/Art/Grip.tga +0 -0
  157. package/templates/addon/Assets/Art/Outline10.tga +0 -0
  158. package/templates/addon/Assets/Art/Outline12.tga +0 -0
  159. package/templates/addon/Assets/Art/Outline13.tga +0 -0
  160. package/templates/addon/Assets/Art/Outline14.tga +0 -0
  161. package/templates/addon/Assets/Art/Outline15.tga +0 -0
  162. package/templates/addon/Assets/Art/Outline2.tga +0 -0
  163. package/templates/addon/Assets/Art/Outline3.tga +0 -0
  164. package/templates/addon/Assets/Art/Outline5.tga +0 -0
  165. package/templates/addon/Assets/Art/Outline7.tga +0 -0
  166. package/templates/addon/Assets/Art/Outline9.tga +0 -0
  167. package/templates/addon/Assets/Art/Round11.tga +0 -0
  168. package/templates/addon/Assets/Art/Round11Border.tga +0 -0
  169. package/templates/addon/Assets/Art/Round4.tga +0 -0
  170. package/templates/addon/Assets/Art/Round4Border.tga +0 -0
  171. package/templates/addon/Assets/Art/Round6.tga +0 -0
  172. package/templates/addon/Assets/Art/Round6Border.tga +0 -0
  173. package/templates/addon/Assets/Art/Round8.tga +0 -0
  174. package/templates/addon/Assets/Art/Round8Border.tga +0 -0
  175. package/templates/addon/Assets/Art/Search.tga +0 -0
  176. package/templates/addon/CLAUDE.md +1 -0
  177. package/templates/addon/README.md +49 -0
  178. package/templates/addon/eslint.config.mjs +6 -0
  179. package/templates/addon/gitignore +9 -0
  180. package/templates/addon/kumi.config.ts +17 -0
  181. package/templates/addon/package.json +20 -0
  182. package/templates/addon/src/app/app.module.ts +18 -0
  183. package/templates/addon/src/app/commands.store.ts +11 -0
  184. package/templates/addon/src/app/layout/AppWindow.module.scss +92 -0
  185. package/templates/addon/src/app/layout/AppWindow.tsx +32 -0
  186. package/templates/addon/src/app/main.ts +5 -0
  187. package/templates/addon/src/app/routes/(window)/home/page.tsx +8 -0
  188. package/templates/addon/src/app/routes/(window)/plugins/page.tsx +38 -0
  189. package/templates/addon/src/app/routes/(window)/settings/page.tsx +8 -0
  190. package/templates/addon/src/app/theme.ts +6 -0
  191. package/templates/addon/src/modules/greeting/api/player.api.ts +29 -0
  192. package/templates/addon/src/modules/greeting/greeting.module.ts +18 -0
  193. package/templates/addon/src/modules/greeting/model/greeting.store.ts +81 -0
  194. package/templates/addon/src/modules/greeting/services/greeting.service.ts +38 -0
  195. package/templates/addon/src/modules/greeting/ui/Greeting.module.scss +105 -0
  196. package/templates/addon/src/modules/greeting/ui/GreetingCard.tsx +29 -0
  197. package/templates/addon/src/modules/greeting/widgets/GreetingHud.tsx +54 -0
  198. package/templates/addon/src/modules/greeting/widgets/GreetingOverview.tsx +18 -0
  199. package/templates/addon/src/modules/greeting/widgets/GreetingSettings.tsx +56 -0
  200. package/templates/addon/src/modules/playtime/api/clock.api.ts +6 -0
  201. package/templates/addon/src/modules/playtime/model/playtime.store.ts +19 -0
  202. package/templates/addon/src/modules/playtime/playtime.module.ts +18 -0
  203. package/templates/addon/src/modules/playtime/services/playtime.service.ts +18 -0
  204. package/templates/addon/src/modules/playtime/ui/Playtime.module.scss +25 -0
  205. package/templates/addon/src/modules/playtime/ui/PlaytimeBadge.tsx +10 -0
  206. package/templates/addon/src/modules/playtime/widgets/PlaytimePage.tsx +16 -0
  207. package/templates/addon/src/shared/api/native-art.ts +28 -0
  208. package/templates/addon/src/shared/assets/close.png +0 -0
  209. package/templates/addon/src/shared/lib/format.ts +20 -0
  210. package/templates/addon/src/shared/styles/_tokens.scss +12 -0
  211. package/templates/addon/src/shared/ui/Button.tsx +29 -0
  212. package/templates/addon/src/shared/ui/Choice.tsx +34 -0
  213. package/templates/addon/src/shared/ui/Controls.module.scss +93 -0
  214. package/templates/addon/src/shared/ui/Glyph.tsx +18 -0
  215. package/templates/addon/src/shared/ui/Slider.tsx +25 -0
  216. package/templates/addon/src/shared/ui/Toggle.tsx +29 -0
  217. package/templates/addon/src/shared/ui/focus-ref.ts +7 -0
  218. package/templates/addon/src/shared/ui/range-skin.tsx +15 -0
  219. package/templates/addon/tsconfig.json +63 -0
  220. package/translate/index.ts +14 -0
  221. package/translate/package.json +9 -0
  222. package/utils/colors.ts +76 -0
  223. package/utils/format.ts +37 -0
  224. package/utils/index.ts +4 -0
  225. package/utils/number.ts +17 -0
  226. package/utils/package.json +7 -0
  227. package/utils/text.ts +6 -0
  228. package/validate/index.ts +119 -0
  229. package/validate/package.json +9 -0
  230. package/wow/absolute-position.ts +142 -0
  231. package/wow/action-bar-layout.ts +108 -0
  232. package/wow/action-button.ts +219 -0
  233. package/wow/action-catalog.ts +98 -0
  234. package/wow/addon-manifest.ts +36 -0
  235. package/wow/animation.ts +428 -0
  236. package/wow/app-ui.ts +221 -0
  237. package/wow/aura-button.ts +76 -0
  238. package/wow/aura-container.ts +591 -0
  239. package/wow/aura-countdown.ts +201 -0
  240. package/wow/aura-slot.ts +153 -0
  241. package/wow/bar-shade.ts +93 -0
  242. package/wow/borrowed-slot.ts +216 -0
  243. package/wow/button-drag.ts +48 -0
  244. package/wow/cast-clock.ts +153 -0
  245. package/wow/cast-text.ts +83 -0
  246. package/wow/class-colors.ts +126 -0
  247. package/wow/clock.ts +7 -0
  248. package/wow/color-picker.ts +128 -0
  249. package/wow/commands.ts +17 -0
  250. package/wow/contained-frame.ts +128 -0
  251. package/wow/contract.ts +42 -0
  252. package/wow/cooldown-timing.ts +24 -0
  253. package/wow/css-box.ts +144 -0
  254. package/wow/css-color.ts +12 -0
  255. package/wow/css-style.ts +109 -0
  256. package/wow/css-surfaces.ts +603 -0
  257. package/wow/css-text.ts +399 -0
  258. package/wow/cursor.ts +45 -0
  259. package/wow/cvar-schema.ts +32 -0
  260. package/wow/cvars.ts +98 -0
  261. package/wow/dropdown-art.ts +39 -0
  262. package/wow/element-click.ts +20 -0
  263. package/wow/element-event-coordinator.ts +219 -0
  264. package/wow/element-events.ts +317 -0
  265. package/wow/event-dispatcher.ts +151 -0
  266. package/wow/events.ts +50 -0
  267. package/wow/font-cache.ts +92 -0
  268. package/wow/frame-access.ts +53 -0
  269. package/wow/frame-layer.ts +5 -0
  270. package/wow/frame-timing.ts +13 -0
  271. package/wow/frames.ts +236 -0
  272. package/wow/heal-prediction.ts +305 -0
  273. package/wow/hooks.ts +133 -0
  274. package/wow/image-layers.ts +55 -0
  275. package/wow/index.ts +111 -0
  276. package/wow/layout-view.ts +663 -0
  277. package/wow/locale-font.ts +15 -0
  278. package/wow/lua-string.ts +18 -0
  279. package/wow/lua-table.ts +18 -0
  280. package/wow/macro-book.ts +84 -0
  281. package/wow/map-position.ts +27 -0
  282. package/wow/media-query.ts +65 -0
  283. package/wow/module-events.ts +11 -0
  284. package/wow/native-hooks.ts +168 -0
  285. package/wow/native-placement.ts +88 -0
  286. package/wow/native.ts +153 -0
  287. package/wow/number-format.ts +8 -0
  288. package/wow/number-input.ts +10 -0
  289. package/wow/panel-host.ts +97 -0
  290. package/wow/pixel.ts +35 -0
  291. package/wow/plugins.ts +246 -0
  292. package/wow/protect.ts +48 -0
  293. package/wow/protected-layout.ts +133 -0
  294. package/wow/protected-view.ts +103 -0
  295. package/wow/range-art.ts +81 -0
  296. package/wow/readable-api.ts +120 -0
  297. package/wow/reparent-frame.ts +99 -0
  298. package/wow/rounded-texture.ts +183 -0
  299. package/wow/saved-variables.ts +109 -0
  300. package/wow/scroll-view.ts +64 -0
  301. package/wow/secret-ref.ts +14 -0
  302. package/wow/secure-bindings.ts +89 -0
  303. package/wow/secure-clicks.ts +432 -0
  304. package/wow/secure-snippet.ts +235 -0
  305. package/wow/secure-state.ts +130 -0
  306. package/wow/secure-target-visibility.ts +89 -0
  307. package/wow/secure-transaction.ts +331 -0
  308. package/wow/special-window.ts +269 -0
  309. package/wow/spell-cooldown.ts +36 -0
  310. package/wow/start-addon.ts +122 -0
  311. package/wow/state-paint.ts +256 -0
  312. package/wow/transition.ts +86 -0
  313. package/wow/unit-button.ts +39 -0
  314. package/wow/unit-cast.ts +92 -0
  315. package/wow/unit-health-text.ts +47 -0
  316. package/wow/view-nodes.ts +304 -0
  317. package/wow/view.ts +1864 -0
  318. package/wow/visible-frame-job.ts +34 -0
  319. package/wow/width-reserve.ts +108 -0
package/form/index.ts ADDED
@@ -0,0 +1,526 @@
1
+ import { Fragment, createContext, direct, fail, ok, ref, type FocusRef, type FocusTarget, type Protect, type ReadonlyRef, type Result, type UIElement, type UINode } from "@kumi/core";
2
+ import type { StateStore } from "@kumi/state";
3
+
4
+ export interface FormError {
5
+ readonly message: string;
6
+ readonly code?: string;
7
+ }
8
+
9
+ export interface FormIssue extends FormError {
10
+ readonly path: readonly PropertyKey[];
11
+ }
12
+
13
+ export type FormValidator<State> = (this: void, values: Readonly<State>) => readonly FormIssue[];
14
+ export interface FormSchema {
15
+ safeParse(this: object, value: unknown): { success: true } | { success: false; error: { issues: readonly FormIssue[] } };
16
+ }
17
+
18
+ export interface FormField<Value> {
19
+ readonly value: ReadonlyRef<Value>;
20
+ readonly error: ReadonlyRef<string | undefined>;
21
+ readonly errors: ReadonlyRef<readonly FormIssue[]>;
22
+ readonly ref: FocusRef;
23
+ readonly touched: ReadonlyRef<boolean>;
24
+ readonly dirty: ReadonlyRef<boolean>;
25
+ set(this: void, value: Value): void;
26
+ blur(this: void): void;
27
+ }
28
+
29
+ type Key<State> = keyof State & string;
30
+
31
+ export interface FormValuesSource<State extends object> {
32
+ get<K extends Key<State>>(this: void, key: K): State[K] | undefined;
33
+ set<K extends Key<State>>(this: void, key: K, value: NoInfer<State[K]>): void;
34
+ }
35
+
36
+ export type FormValueFields<State extends object> = {
37
+ [K in Key<State>]: {
38
+ get(this: void): State[K] | undefined;
39
+ set(this: void, value: State[K]): void;
40
+ };
41
+ };
42
+
43
+ export type FormValueStore<State extends object> = Pick<StateStore<State>, "field" | "patch">;
44
+ export type FormValueCommit<State extends object> = (this: void, patch: Partial<State>) => Result<void>;
45
+ export type FormValidationMode = "onChange" | "onBlur" | "onSubmit";
46
+
47
+ export type FormValueOptions<State extends object> =
48
+ | { values?: FormValuesSource<State> | FormValueStore<State>; fields?: never; commit?: never }
49
+ | { values: Readonly<State>; commit: FormValueCommit<State>; fields?: never }
50
+ | { fields: FormValueFields<State>; values?: never; commit?: never };
51
+
52
+ export interface FormBinding<Value, Name extends string = string> {
53
+ readonly name: Name;
54
+ readonly defaultValue: Value;
55
+ readonly value: ReadonlyRef<Value>;
56
+ readonly ref: FocusRef;
57
+ onChange(this: void, value: Value): void;
58
+ onBlur(this: void): void;
59
+ }
60
+
61
+ class LinkedField<Value> implements ReadonlyRef<Value> {
62
+ constructor(private readonly cached: ReadonlyRef<Value>, private readonly read: (this: void) => Value) {}
63
+
64
+ get value(): Value {
65
+ const last = this.cached.value;
66
+ const live = this.read();
67
+ return live === last ? last : live;
68
+ }
69
+ }
70
+
71
+ export type FormOptions<State extends object> = {
72
+ defaultValues: State;
73
+ protect?: Protect;
74
+ mode?: FormValidationMode;
75
+ } & ({ validator?: FormSchema; validate?: never } | { validate: FormValidator<NoInfer<State>>; validator?: never })
76
+ & FormValueOptions<NoInfer<State>>;
77
+
78
+ export function createForm<State extends object>(defaults: State, validate?: FormValidator<State>, protect: Protect = direct) {
79
+ return validate ? useForm({ defaultValues: defaults, validate, protect }) : useForm({ defaultValues: defaults, protect });
80
+ }
81
+
82
+ export function useForm<State extends object>({ defaultValues, validator, validate, protect = direct, mode = "onBlur",
83
+ values: source, fields: bindings, commit }: FormOptions<State>) {
84
+ const initial = Object.fromEntries(Object.entries(defaultValues).filter(([, value]) => value != null)) as State;
85
+ const issues = ref<readonly FormIssue[]>([]);
86
+ let validationIssues: readonly FormIssue[] = [];
87
+ const errorsByField = new Map<Key<State>, FormIssue>();
88
+ let formErrors: FormIssue[] = [];
89
+ let storageError: FormIssue | undefined;
90
+ let attempted = false;
91
+ const fields = new Map<Key<State>, FieldState<State[Key<State>]>>();
92
+ const targets = new Map<Key<State>, FocusTarget>();
93
+
94
+ type FieldState<Value> = {
95
+ field: FormField<Value>;
96
+ setErrors: (this: void, issues: readonly FormIssue[]) => void;
97
+ touch: (this: void) => void;
98
+ edit: (this: void, value: Value | undefined) => void;
99
+ clear: (this: void) => void;
100
+ sync: (this: void) => void;
101
+ reset: (this: void) => void;
102
+ };
103
+
104
+ const keys = Object.keys(initial) as Key<State>[];
105
+ const writable = new Set(keys);
106
+ const store = commit === undefined && source && "patch" in source ? source : undefined;
107
+ const adapter = commit === undefined && source && "get" in source ? source : undefined;
108
+ const save = commit ?? store?.patch;
109
+ const linked = source !== undefined || bindings !== undefined;
110
+
111
+ function read<K extends Key<State>>(key: K): State[K] | undefined {
112
+ if (commit !== undefined) return source[key];
113
+ if (store) return store.field(key).value;
114
+ return bindings ? bindings[key].get() : adapter?.get(key);
115
+ }
116
+
117
+ function readCurrent<K extends Key<State>>(key: K): State[K] {
118
+ const value = read(key);
119
+ if (value != null) return value;
120
+ return initial[key];
121
+ }
122
+
123
+ function writeLinked<K extends Key<State>>(key: K, value: State[K]) {
124
+ if (bindings) bindings[key].set(value);
125
+ else adapter?.set(key, value);
126
+ }
127
+
128
+ function values(): State {
129
+ const result = { ...initial };
130
+ for (const key of keys) result[key] = readValue(key);
131
+ return result;
132
+ }
133
+
134
+ function readValue<K extends Key<State>>(key: K): State[K] {
135
+ const registered = fields.get(key);
136
+ if (registered) return registered.field.value.value as State[K];
137
+ return readCurrent(key);
138
+ }
139
+
140
+ function getValues(): State;
141
+ function getValues<K extends Key<State>>(key: K): State[K];
142
+ function getValues<K extends Key<State>>(keys: readonly K[]): Pick<State, K>;
143
+ function getValues<K extends Key<State>>(selection?: K | readonly K[]) {
144
+ if (selection === undefined) return values();
145
+ if (typeof selection === "string") return readValue(selection);
146
+ const selected = {} as Pick<State, K>;
147
+ for (const key of selection) selected[key] = readValue(key);
148
+ return selected;
149
+ }
150
+
151
+ function getFieldErrors(key: Key<State>): readonly FormIssue[] {
152
+ return issues.value.filter(issue => issue.path[0] === key);
153
+ }
154
+
155
+ function getFormErrors(): readonly FormIssue[] {
156
+ return issues.value.filter(issue => issue.path.length === 0);
157
+ }
158
+
159
+ function getFieldState(key: Key<State>) {
160
+ const registered = fields.get(key)?.field;
161
+ return {
162
+ dirty: registered?.dirty.value ?? false,
163
+ touched: registered?.touched.value ?? false,
164
+ errors: getFieldErrors(key),
165
+ };
166
+ }
167
+
168
+ const formState = { getValues, getFieldErrors, getFormErrors, getFieldState };
169
+
170
+ function sameErrors(left: readonly FormIssue[], right: readonly FormIssue[]) {
171
+ return left.length === right.length && left.every((issue, index) => {
172
+ const next = right[index];
173
+ return issue.message === next.message && issue.code === next.code && issue.path.length === next.path.length
174
+ && issue.path.every((part, offset) => part === next.path[offset]);
175
+ });
176
+ }
177
+
178
+ const validateSnapshot = validator ? fromSchema<State>(validator) : validate;
179
+
180
+ function validateValues(snapshot: State): readonly FormIssue[] {
181
+ const result = protect(() => validateSnapshot?.(snapshot) ?? []);
182
+ return result.ok ? result.value : [{ path: [], message: result.reason }];
183
+ }
184
+
185
+ function publishIssues() {
186
+ const next = [...validationIssues, ...formErrors];
187
+ if (storageError) next.push(storageError);
188
+ for (const error of errorsByField.values()) next.push(error);
189
+ if (sameErrors(issues.value, next)) return;
190
+ issues.value = next;
191
+ for (const [key, state] of fields) state.setErrors(next.filter(issue => issue.path[0] === key));
192
+ }
193
+
194
+ function reportIssues(next: readonly FormIssue[]) {
195
+ validationIssues = next;
196
+ publishIssues();
197
+ }
198
+
199
+ function setError(key: Key<State>, error: FormError) {
200
+ errorsByField.set(key, { ...error, path: [key] });
201
+ publishIssues();
202
+ }
203
+
204
+ function clearErrors(key?: Key<State>) {
205
+ if (key === undefined) {
206
+ errorsByField.clear();
207
+ validationIssues = validationIssues.filter(issue => issue.path.length === 0);
208
+ } else {
209
+ errorsByField.delete(key);
210
+ validationIssues = validationIssues.filter(issue => issue.path[0] !== key);
211
+ }
212
+ publishIssues();
213
+ }
214
+
215
+ function setFormError(error: FormError) {
216
+ formErrors = [...formErrors, { ...error, path: [] }];
217
+ publishIssues();
218
+ }
219
+
220
+ function clearFormErrors() {
221
+ formErrors = [];
222
+ storageError = undefined;
223
+ validationIssues = validationIssues.filter(issue => issue.path.length !== 0);
224
+ publishIssues();
225
+ }
226
+
227
+ function check(snapshot = values()) {
228
+ const next = validateValues(snapshot);
229
+ reportIssues(next);
230
+ return next.length === 0;
231
+ }
232
+
233
+ function applyStore(commitValues: FormValueCommit<State>, patch: Partial<State>, changed: readonly Key<State>[]) {
234
+ if (validateSnapshot) {
235
+ // Validate the proposed saved values. An unrelated invalid draft stays local.
236
+ const candidate = { ...initial, ...patch };
237
+ const changedKeys = new Set(changed);
238
+ for (const key of keys) {
239
+ if (!changedKeys.has(key)) candidate[key] = readCurrent(key);
240
+ }
241
+ const errors = validateValues(candidate);
242
+ if (errors.length > 0) {
243
+ for (const key of changed) {
244
+ field(key);
245
+ fields.get(key)?.edit(patch[key]);
246
+ }
247
+ if (attempted || mode === "onChange" || mode === "onBlur"
248
+ && changed.some(key => fields.get(key)?.field.touched.value === true)) check();
249
+ return fail(errors[0].message);
250
+ }
251
+ }
252
+ const saved = commitValues(patch);
253
+ if (!saved.ok) {
254
+ for (const key of changed) {
255
+ field(key);
256
+ fields.get(key)?.edit(patch[key]);
257
+ }
258
+ storageError = { path: [], message: saved.reason, code: "store-write" };
259
+ publishIssues();
260
+ return saved;
261
+ }
262
+ storageError = undefined;
263
+ for (const key of changed) {
264
+ fields.get(key)?.edit(undefined);
265
+ errorsByField.delete(key);
266
+ }
267
+ if (attempted || mode === "onChange" || validationIssues.length > 0) check();
268
+ else publishIssues();
269
+ return saved;
270
+ }
271
+
272
+ function field<K extends Key<State>>(key: K): FormField<State[K]> {
273
+ const existing = fields.get(key);
274
+ if (existing) return existing.field as FormField<State[K]>;
275
+ const current = () => readCurrent(key);
276
+ const cached = ref<State[K]>(current());
277
+ const draft = save ? ref<State[K] | undefined>(undefined) : undefined;
278
+ const value = linked ? new LinkedField(cached, () => {
279
+ const edited = draft?.value;
280
+ if (edited != null) return edited;
281
+ return current();
282
+ }) : cached;
283
+
284
+ function write(next: State[K]) {
285
+ writeLinked(key, next);
286
+ cached.value = linked ? current() : next;
287
+ }
288
+ const errors = ref(getFieldErrors(key));
289
+ const error = ref<string | undefined>(errors.value[0]?.message);
290
+ const touched = ref(false);
291
+ const dirty = ref(false);
292
+ const binding: FormField<State[K]> = {
293
+ value, error, errors, touched, dirty,
294
+ ref: target => {
295
+ if (target === undefined) targets.delete(key);
296
+ else targets.set(key, target);
297
+ },
298
+ set: next => { setValue(key, next); },
299
+ blur: () => {
300
+ touched.value = true;
301
+ if (mode !== "onSubmit" || attempted) check();
302
+ },
303
+ };
304
+ const state: FieldState<State[K]> = {
305
+ field: binding,
306
+ touch: () => { touched.value = true; },
307
+ setErrors: next => {
308
+ if (!sameErrors(errors.value, next)) errors.value = next;
309
+ error.value = next[0]?.message;
310
+ },
311
+ edit: next => {
312
+ if (draft) draft.value = next;
313
+ else if (next !== undefined) write(next);
314
+ dirty.value = value.value !== initial[key];
315
+ },
316
+ clear: () => {
317
+ if (draft) draft.value = undefined;
318
+ error.value = undefined;
319
+ touched.value = false;
320
+ dirty.value = false;
321
+ },
322
+ sync: () => { cached.value = current(); },
323
+ reset: () => {
324
+ write(initial[key]);
325
+ error.value = undefined;
326
+ touched.value = false;
327
+ dirty.value = false;
328
+ },
329
+ };
330
+ // The map erases the correlation between its key and the field's value type.
331
+ fields.set(key, state as unknown as FieldState<State[Key<State>]>);
332
+ return binding;
333
+ }
334
+
335
+ function register<K extends Key<State>>(name: K): FormBinding<State[K], K> {
336
+ const { value, ref, set, blur } = field(name);
337
+ return { name, defaultValue: initial[name], value, ref, onChange: set, onBlur: blur };
338
+ }
339
+
340
+ function rejectWrite(key: Key<State>, message: string): Result<void> {
341
+ setError(key, { message, code: "form-write" });
342
+ return fail(message);
343
+ }
344
+
345
+ function setValues(patch: Partial<State>): Result<void> {
346
+ const entries = Object.entries(patch).filter(([, value]) => value != null);
347
+ const changed = entries.map(([key]) => key as Key<State>);
348
+ for (const key of changed) {
349
+ if (!writable.has(key)) return rejectWrite(key, "Unknown form field " + String(key));
350
+ }
351
+ if (changed.length === 0) return ok(undefined);
352
+ return applyValues(Object.fromEntries(entries) as Partial<State>, changed);
353
+ }
354
+
355
+ function applyValues(patch: Partial<State>, changed: readonly Key<State>[]): Result<void> {
356
+ if (save) return applyStore(save, patch, changed);
357
+ if (linked && changed.length > 1) return fail("Atomic edits require a reactive form store");
358
+ for (const key of changed) {
359
+ field(key);
360
+ fields.get(key)?.edit(patch[key]);
361
+ errorsByField.delete(key);
362
+ }
363
+ if (attempted || mode === "onChange" || mode === "onBlur"
364
+ && changed.some(key => fields.get(key)?.field.touched.value === true)) check();
365
+ else publishIssues();
366
+ return ok(undefined);
367
+ }
368
+
369
+ function setValue<K extends Key<State>>(key: K, value: NoInfer<State[K]>, options?: { shouldTouch?: boolean }): Result<void> {
370
+ if (value == null) return rejectWrite(key, "Use a concrete value for " + String(key));
371
+ if (!writable.has(key)) return rejectWrite(key, "Unknown form field " + String(key));
372
+ if (options?.shouldTouch) {
373
+ field(key);
374
+ fields.get(key)?.touch();
375
+ }
376
+ return applyValues({ [key]: value } as Partial<State>, [key]);
377
+ }
378
+
379
+ function setFocus(key: Key<State>): Result<void> {
380
+ const target = targets.get(key);
381
+ if (!target) return fail("No focus target for form field " + key);
382
+ const result = protect(target.focus);
383
+ if (!result.ok) return result;
384
+ return result.value ? ok(undefined) : fail("Form field cannot receive focus: " + key);
385
+ }
386
+
387
+ function submit(save: (this: void, values: State) => void | Result<void>, options?: { focusFirstError?: boolean }): Result<State> {
388
+ attempted = true;
389
+ const snapshot = values();
390
+ if (!check(snapshot)) {
391
+ if (options?.focusFirstError) {
392
+ for (const key of keys) if (getFieldErrors(key).length > 0 && setFocus(key).ok) break;
393
+ }
394
+ return fail(issues.value[0]?.message ?? "The form has validation errors");
395
+ }
396
+ const result = protect(() => save(snapshot));
397
+ if (!result.ok) return result;
398
+ return result.value && !result.value.ok ? result.value : ok(snapshot);
399
+ }
400
+
401
+ function handleSubmit(save: Parameters<typeof submit>[0], options?: Parameters<typeof submit>[1]) {
402
+ return () => submit(save, options);
403
+ }
404
+
405
+ function resetField<K extends Key<State>>(key: K): Result<void> {
406
+ const result = setValue(key, initial[key]);
407
+ if (!result.ok) return result;
408
+ fields.get(key)?.clear();
409
+ clearErrors(key);
410
+ return result;
411
+ }
412
+
413
+ function revertField<K extends Key<State>>(key: K): Result<void> {
414
+ if (!linked) return resetField(key);
415
+ fields.get(key)?.clear();
416
+ clearErrors(key);
417
+ return ok(undefined);
418
+ }
419
+
420
+ function reset(): Result<void> {
421
+ if (save) {
422
+ const saved = applyStore(save, initial, keys);
423
+ if (!saved.ok) return saved;
424
+ for (const state of fields.values()) state.clear();
425
+ } else {
426
+ for (const key of keys) {
427
+ const state = fields.get(key);
428
+ if (state) state.reset();
429
+ else writeLinked(key, initial[key]);
430
+ }
431
+ }
432
+ clearAllIssues();
433
+ return ok(undefined);
434
+ }
435
+
436
+ function clearAllIssues() {
437
+ attempted = false;
438
+ errorsByField.clear();
439
+ formErrors = [];
440
+ storageError = undefined;
441
+ reportIssues([]);
442
+ }
443
+
444
+ function revert(): Result<void> {
445
+ if (!linked) return reset();
446
+ for (const state of fields.values()) state.clear();
447
+ clearAllIssues();
448
+ return ok(undefined);
449
+ }
450
+
451
+ function sync(key?: Key<State>) {
452
+ if (!linked) return;
453
+ if (key !== undefined) fields.get(key)?.sync();
454
+ else for (const state of fields.values()) state.sync();
455
+ }
456
+
457
+ return { field, register, bind: register, values, formState, setValue, setValues, check, submit, handleSubmit, reset, resetField, revert, revertField,
458
+ setFocus, setError, clearErrors, setFormError, clearFormErrors, sync, issues: issues as ReadonlyRef<readonly FormIssue[]>, protect };
459
+ }
460
+
461
+ /** Adapt a synchronous safeParse schema. The submit callback receives the form's input values. */
462
+ export function fromSchema<State extends object>(schema: FormSchema): FormValidator<State> {
463
+ return values => {
464
+ const result = schema.safeParse(values);
465
+ return result.success ? [] : result.error.issues;
466
+ };
467
+ }
468
+
469
+ export type FormController<State extends object> = ReturnType<typeof useForm<State>>;
470
+
471
+ const context = createContext<unknown>();
472
+ const missingForm = fail("Form fields require a parent Form or an explicit control");
473
+
474
+ type FormProps<State extends object> =
475
+ | (FormController<State> & { children?: UINode | ((this: void) => UIElement) })
476
+ | { form: FormController<State>; children?: UINode | ((this: void, form: FormController<State>) => UIElement) };
477
+
478
+ export function renderForm<State extends object, Output>(control: FormController<State>,
479
+ render: (this: void) => Output): Result<Output> {
480
+ return context.run(control, render, control.protect);
481
+ }
482
+
483
+ export function Form<State extends object>(props: FormProps<State>): UIElement {
484
+ if ("form" in props) {
485
+ const { form, children } = props;
486
+ const result = renderForm(form, () => typeof children === "function" ? children(form) : Fragment({ children }));
487
+ return result.ok ? result.value : Fragment({ children: result.reason });
488
+ }
489
+ const { children } = props;
490
+ const result = renderForm(props, () => typeof children === "function" ? children() : Fragment({ children }));
491
+ return result.ok ? result.value : Fragment({ children: result.reason });
492
+ }
493
+
494
+ export function useFormContext<State extends object>(): Result<FormController<State>> {
495
+ const control = context.use();
496
+ return control === undefined ? missingForm : ok(control as FormController<State>);
497
+ }
498
+
499
+ export interface FormFieldControl<Value, Name extends string = string> extends FormBinding<Value, Name> {
500
+ readonly error: FormField<Value>["error"];
501
+ readonly errors: FormField<Value>["errors"];
502
+ readonly touched: FormField<Value>["touched"];
503
+ readonly dirty: FormField<Value>["dirty"];
504
+ }
505
+
506
+ export function useFormField<State extends object, Name extends Key<State> = Key<State>>(name: Name,
507
+ control: FormController<State>): FormFieldControl<State[Name], Name>;
508
+ export function useFormField<State extends object, Name extends Key<State> = Key<State>>(name: Name):
509
+ Result<FormFieldControl<State[Name], Name>>;
510
+ export function useFormField<State extends object, Name extends Key<State>>(name: Name, control?: FormController<State>) {
511
+ const form = control === undefined ? useFormContext<State>() : ok(control);
512
+ if (!form.ok) return form;
513
+ const { error, errors, dirty, touched } = form.value.field(name);
514
+ const binding = { ...form.value.register(name), error, errors, dirty, touched };
515
+ return control === undefined ? ok(binding) : binding;
516
+ }
517
+
518
+ export function Field<State extends object, Name extends Key<State> = Key<State>>(props: {
519
+ name: Name;
520
+ control?: FormController<State>;
521
+ render(this: void, field: FormFieldControl<State[Name], Name>): UIElement;
522
+ }): UIElement {
523
+ if (props.control) return props.render(useFormField(props.name, props.control));
524
+ const field = useFormField<State, Name>(props.name);
525
+ return field.ok ? props.render(field.value) : Fragment({ children: field.reason });
526
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@kumi/form",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "exports": {
7
+ ".": "./index.ts"
8
+ },
9
+ "peerDependencies": {
10
+ "@kumi/core": "0.1.x",
11
+ "@kumi/state": "0.1.x"
12
+ }
13
+ }
@@ -0,0 +1,2 @@
1
+ export { Transition, transitionProgress } from "@kumi/core";
2
+ export type { Timing, TransitionOptions } from "@kumi/core";
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@kumi/motion",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "exports": "./index.ts",
7
+ "peerDependencies": {
8
+ "@kumi/core": "0.1.x"
9
+ }
10
+ }
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
1
  {
2
2
  "name": "@overmvp/kumi",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.0",
4
+ "description": "TypeScript and TSX framework with a native Lua 5.1 compiler for World of Warcraft Retail addons",
5
+ "license": "MIT",
6
+ "repository": { "type": "git", "url": "git+https://github.com/OverMVP/kumi.git" },
7
+ "keywords": ["world-of-warcraft", "wow-addon", "typescript", "tsx", "lua"],
8
+ "engines": { "node": ">=24" },
9
+ "bin": { "kumi": "scripts/kumi-cli.mjs" },
10
+ "files": [
11
+ "animation", "builder", "config.ts", "eslint.mjs", "form", "motion", "router", "src", "state", "styles", "translate", "utils", "validate", "wow",
12
+ "scripts/kumi-cli.mjs", "scripts/addon-project.mjs", "scripts/addon-build.mjs", "scripts/link-addons.mjs", "templates/addon"
13
+ ],
14
+ "publishConfig": { "access": "public" },
15
+ "scripts": {
16
+ "check": "tsc -p tsconfig.json",
17
+ "check:starter": "node scripts/check-starter.mjs",
18
+ "test:lua": "node scripts/lua-tests.mjs",
19
+ "test:unit": "node --import ./tests/support/ts-resolve.mjs --test builder/*.test.mjs tests/*.test.mjs scripts/*.test.mjs"
20
+ },
21
+ "dependencies": {
22
+ "postcss": "8.5.6",
23
+ "sass": "1.108.2"
24
+ },
25
+ "devDependencies": {
26
+ "typescript": "6.0.2"
27
+ }
28
+ }
@@ -0,0 +1,50 @@
1
+ import type { UIElement } from "../src/elements";
2
+ import { fail, ok } from "@kumi/core";
3
+ import { ownedArray, ownedMap } from "../src/owned-collections";
4
+ import { collectRoutes, renderRoute, type RouteLayoutProps } from "./routes";
5
+
6
+ export type { RouteContext, RouteLayoutProps } from "./routes";
7
+ export { useNavigation, withNavigation, type Navigation } from "./navigation";
8
+ export { RouterView, onActivated, onDeactivated, withRouterHost, type RoutePage, type RouterHost } from "./router-view";
9
+
10
+ type PageFactory<Context> = (this: void, context: Context) => UIElement;
11
+ /** A page file: its default export renders the page. `title` and `order` describe its tab. */
12
+ type PageModule = { default: PageFactory<never> | { render: PageFactory<never> }; title?: string; order?: number };
13
+ type ContextReader<Definition> = Definition extends PageFactory<infer Context> ? PageFactory<Context>
14
+ : Definition extends { render: PageFactory<infer Context> } ? PageFactory<Context> : never;
15
+ type PageContext<Page extends PageModule> = ContextReader<Page["default"]> extends PageFactory<infer Context> ? Context : never;
16
+ type Layout<Context> = (this: void, props: RouteLayoutProps<Context>) => UIElement;
17
+
18
+ export function createRouter<Page extends PageModule, Path extends string = string>(files: {
19
+ pages: Record<string, Page>;
20
+ layouts: Record<string, Layout<PageContext<Page>>>;
21
+ paths?: readonly Path[];
22
+ }) {
23
+ const registry = collectRoutes(files);
24
+ if (!registry.ok) return registry;
25
+ const routes = ownedMap<Path, NonNullable<ReturnType<typeof registry.value.get>>>();
26
+ const pages = ownedArray<{ readonly path: Path; readonly title?: string; readonly order?: number }>();
27
+ const paths = files.paths ?? Array.from(registry.value.keys()) as Path[];
28
+ for (const path of paths) {
29
+ const route = registry.value.get(path);
30
+ if (!route || routes.has(path)) return fail(`Invalid generated route: ${path}`);
31
+ routes.set(path, route);
32
+ pages.push({ path, title: route.page.title, order: route.page.order });
33
+ }
34
+ if (routes.size !== registry.value.size) return fail("Generated routes are missing paths");
35
+
36
+ return ok({
37
+ entries: () => routes.entries(),
38
+ /** The file routes with the `title` and `order` their page files export. */
39
+ pages,
40
+ match: (path: string) => routes.get(path as Path),
41
+ has: (path: string): path is Path => routes.has(path as Path),
42
+ render(this: void, path: Path, context: PageContext<Page>) {
43
+ const route = routes.get(path);
44
+ if (!route) return;
45
+ const definition = route.page.default;
46
+ const page = (typeof definition === "function" ? definition : definition.render) as PageFactory<PageContext<Page>>;
47
+ return renderRoute(page, route.layouts, context);
48
+ },
49
+ });
50
+ }
@@ -0,0 +1,22 @@
1
+ import { createContext } from "../src/context";
2
+ import type { ReadonlyRef } from "../src/reactivity";
3
+ import { fail, ok, type Protect, type Result } from "../src/result";
4
+
5
+ export interface Navigation<Path extends string = string> {
6
+ readonly path: ReadonlyRef<Path | undefined>;
7
+ navigate(this: void, path: Path): void;
8
+ }
9
+
10
+ const context = createContext<unknown>();
11
+ const missingNavigation = fail("Navigation requires a router context");
12
+
13
+ /** Run view setup with the navigation state of its host. */
14
+ export function withNavigation<Path extends string, View>(this: void, navigation: Navigation<Path>,
15
+ render: (this: void) => View, protect: Protect): Result<View> {
16
+ return context.run(navigation, render, protect);
17
+ }
18
+
19
+ export function useNavigation<Path extends string = string>(this: void): Result<Navigation<Path>> {
20
+ const navigation = context.use();
21
+ return navigation === undefined ? missingNavigation : ok(navigation as Navigation<Path>);
22
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@kumi/router",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "exports": {
7
+ ".": "./index.ts"
8
+ },
9
+ "peerDependencies": {
10
+ "@kumi/core": "0.1.x"
11
+ }
12
+ }