@xmachines/json-render-svelte 0.19.0-xm.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +155 -0
- package/dist/CatalogRenderer.svelte +59 -0
- package/dist/CatalogRenderer.svelte.d.ts +22 -0
- package/dist/CatalogRenderer.svelte.d.ts.map +1 -0
- package/dist/ConfirmDialog.svelte +98 -0
- package/dist/ConfirmDialog.svelte.d.ts +10 -0
- package/dist/ConfirmDialog.svelte.d.ts.map +1 -0
- package/dist/ConfirmDialogManager.svelte +15 -0
- package/dist/ConfirmDialogManager.svelte.d.ts +4 -0
- package/dist/ConfirmDialogManager.svelte.d.ts.map +1 -0
- package/dist/ElementRenderer.svelte +215 -0
- package/dist/ElementRenderer.svelte.d.ts +15 -0
- package/dist/ElementRenderer.svelte.d.ts.map +1 -0
- package/dist/JsonUIProvider.svelte +74 -0
- package/dist/JsonUIProvider.svelte.d.ts +39 -0
- package/dist/JsonUIProvider.svelte.d.ts.map +1 -0
- package/dist/Renderer.svelte +35 -0
- package/dist/Renderer.svelte.d.ts +19 -0
- package/dist/Renderer.svelte.d.ts.map +1 -0
- package/dist/RendererWithProvider.test.svelte +37 -0
- package/dist/RendererWithProvider.test.svelte.d.ts +14 -0
- package/dist/RendererWithProvider.test.svelte.d.ts.map +1 -0
- package/dist/RepeatChildren.svelte +51 -0
- package/dist/RepeatChildren.svelte.d.ts +13 -0
- package/dist/RepeatChildren.svelte.d.ts.map +1 -0
- package/dist/TestBoom.svelte +11 -0
- package/dist/TestBoom.svelte.d.ts +7 -0
- package/dist/TestBoom.svelte.d.ts.map +1 -0
- package/dist/TestButton.svelte +11 -0
- package/dist/TestButton.svelte.d.ts +9 -0
- package/dist/TestButton.svelte.d.ts.map +1 -0
- package/dist/TestContainer.svelte +19 -0
- package/dist/TestContainer.svelte.d.ts +11 -0
- package/dist/TestContainer.svelte.d.ts.map +1 -0
- package/dist/TestText.svelte +9 -0
- package/dist/TestText.svelte.d.ts +9 -0
- package/dist/TestText.svelte.d.ts.map +1 -0
- package/dist/catalog-types.d.ts +63 -0
- package/dist/catalog-types.d.ts.map +1 -0
- package/dist/catalog-types.js +1 -0
- package/dist/contexts/ActionProvider.svelte +375 -0
- package/dist/contexts/ActionProvider.svelte.d.ts +62 -0
- package/dist/contexts/ActionProvider.svelte.d.ts.map +1 -0
- package/dist/contexts/DirectivesContextProvider.svelte +46 -0
- package/dist/contexts/DirectivesContextProvider.svelte.d.ts +21 -0
- package/dist/contexts/DirectivesContextProvider.svelte.d.ts.map +1 -0
- package/dist/contexts/FunctionsContextProvider.svelte +44 -0
- package/dist/contexts/FunctionsContextProvider.svelte.d.ts +21 -0
- package/dist/contexts/FunctionsContextProvider.svelte.d.ts.map +1 -0
- package/dist/contexts/RepeatScopeProvider.svelte +49 -0
- package/dist/contexts/RepeatScopeProvider.svelte.d.ts +26 -0
- package/dist/contexts/RepeatScopeProvider.svelte.d.ts.map +1 -0
- package/dist/contexts/StateProvider.svelte +182 -0
- package/dist/contexts/StateProvider.svelte.d.ts +47 -0
- package/dist/contexts/StateProvider.svelte.d.ts.map +1 -0
- package/dist/contexts/ValidationProvider.svelte +198 -0
- package/dist/contexts/ValidationProvider.svelte.d.ts +57 -0
- package/dist/contexts/ValidationProvider.svelte.d.ts.map +1 -0
- package/dist/contexts/VisibilityProvider.svelte +82 -0
- package/dist/contexts/VisibilityProvider.svelte.d.ts +29 -0
- package/dist/contexts/VisibilityProvider.svelte.d.ts.map +1 -0
- package/dist/contexts/actions.test.js +156 -0
- package/dist/contexts/state.test.js +118 -0
- package/dist/contexts/visibility.test.js +71 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/render-error.test.js +65 -0
- package/dist/renderer.d.ts +134 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +145 -0
- package/dist/renderer.test.js +168 -0
- package/dist/schema.d.ts +57 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +81 -0
- package/dist/streaming.svelte.d.ts +75 -0
- package/dist/streaming.svelte.d.ts.map +1 -0
- package/dist/streaming.svelte.js +404 -0
- package/dist/utils.svelte.d.ts +26 -0
- package/dist/utils.svelte.d.ts.map +1 -0
- package/dist/utils.svelte.js +92 -0
- package/dist/utils.test.js +307 -0
- package/package.json +71 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Component, Snippet } from "svelte";
|
|
2
|
+
import type { Catalog, InferCatalogComponents, InferCatalogActions, InferComponentProps, InferActionParams, StateModel } from "@xmachines/json-render-core";
|
|
3
|
+
export type { StateModel };
|
|
4
|
+
/**
|
|
5
|
+
* State setter function for updating application state
|
|
6
|
+
*/
|
|
7
|
+
export type SetState = (updater: (prev: Record<string, unknown>) => Record<string, unknown>) => void;
|
|
8
|
+
/**
|
|
9
|
+
* Handle returned by the `on()` function for a specific event.
|
|
10
|
+
* Provides metadata about the event binding and a method to fire it.
|
|
11
|
+
*/
|
|
12
|
+
export interface EventHandle {
|
|
13
|
+
/** Fire the event (resolve action bindings) */
|
|
14
|
+
emit: () => void;
|
|
15
|
+
/** Whether any binding requested preventDefault */
|
|
16
|
+
shouldPreventDefault: boolean;
|
|
17
|
+
/** Whether any handler is bound to this event */
|
|
18
|
+
bound: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Catalog-agnostic base type for component render function arguments.
|
|
22
|
+
* Use this when building reusable component libraries.
|
|
23
|
+
*/
|
|
24
|
+
export interface BaseComponentProps<P = Record<string, unknown>> {
|
|
25
|
+
props: P;
|
|
26
|
+
children?: Snippet;
|
|
27
|
+
/** Simple event emitter (shorthand). Fires the event and returns void. */
|
|
28
|
+
emit: (event: string) => void;
|
|
29
|
+
/** Get an event handle with metadata. Use when you need shouldPreventDefault or bound checks. */
|
|
30
|
+
on: (event: string) => EventHandle;
|
|
31
|
+
/**
|
|
32
|
+
* Two-way binding paths resolved from `$bindState` / `$bindItem` expressions.
|
|
33
|
+
* Maps prop name → absolute state path for write-back.
|
|
34
|
+
*/
|
|
35
|
+
bindings?: Record<string, string>;
|
|
36
|
+
loading?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Context passed to component render functions
|
|
40
|
+
*/
|
|
41
|
+
export interface ComponentContext<C extends Catalog, K extends keyof InferCatalogComponents<C>> extends BaseComponentProps<InferComponentProps<C, K>> {
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Component render function type for Svelte
|
|
45
|
+
*/
|
|
46
|
+
export type ComponentFn<C extends Catalog, K extends keyof InferCatalogComponents<C>> = Component<BaseComponentProps<InferComponentProps<C, K>>>;
|
|
47
|
+
/**
|
|
48
|
+
* Registry of Svelte component constructors for a catalog
|
|
49
|
+
*/
|
|
50
|
+
export type Components<C extends Catalog> = {
|
|
51
|
+
[K in keyof InferCatalogComponents<C>]: ComponentFn<C, K>;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Action handler function type
|
|
55
|
+
*/
|
|
56
|
+
export type ActionFn<C extends Catalog, K extends keyof InferCatalogActions<C>> = (params: InferActionParams<C, K> | undefined, setState: SetState, state: StateModel) => Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Registry of all action handlers for a catalog
|
|
59
|
+
*/
|
|
60
|
+
export type Actions<C extends Catalog> = {
|
|
61
|
+
[K in keyof InferCatalogActions<C>]: ActionFn<C, K>;
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=catalog-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog-types.d.ts","sourceRoot":"","sources":["../src/catalog-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AACjD,OAAO,KAAK,EACV,OAAO,EACP,sBAAsB,EACtB,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,UAAU,EACX,MAAM,mBAAmB,CAAC;AAE3B,YAAY,EAAE,UAAU,EAAE,CAAC;AAM3B;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,CACrB,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAChE,IAAI,CAAC;AAMV;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,+CAA+C;IAC/C,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB,mDAAmD;IACnD,oBAAoB,EAAE,OAAO,CAAC;IAC9B,iDAAiD;IACjD,KAAK,EAAE,OAAO,CAAC;CAChB;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAkB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAC7D,KAAK,EAAE,CAAC,CAAC;IACT,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,0EAA0E;IAC1E,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9B,iGAAiG;IACjG,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,WAAW,CAAC;IACnC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB,CAC/B,CAAC,SAAS,OAAO,EACjB,CAAC,SAAS,MAAM,sBAAsB,CAAC,CAAC,CAAC,CACzC,SAAQ,kBAAkB,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CAAG;AAE1D;;GAEG;AACH,MAAM,MAAM,WAAW,CACrB,CAAC,SAAS,OAAO,EACjB,CAAC,SAAS,MAAM,sBAAsB,CAAC,CAAC,CAAC,IACvC,SAAS,CAAC,kBAAkB,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAE7D;;GAEG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,OAAO,IAAI;KACzC,CAAC,IAAI,MAAM,sBAAsB,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC;CAC1D,CAAC;AAMF;;GAEG;AACH,MAAM,MAAM,QAAQ,CAClB,CAAC,SAAS,OAAO,EACjB,CAAC,SAAS,MAAM,mBAAmB,CAAC,CAAC,CAAC,IACpC,CACF,MAAM,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAC3C,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,UAAU,KACd,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB;;GAEG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,OAAO,IAAI;KACtC,CAAC,IAAI,MAAM,mBAAmB,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC;CACpD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
import { getContext } from "svelte";
|
|
3
|
+
import type {
|
|
4
|
+
ActionBinding,
|
|
5
|
+
ActionConfirm,
|
|
6
|
+
ActionHandler,
|
|
7
|
+
ResolvedAction,
|
|
8
|
+
} from "@xmachines/json-render-core";
|
|
9
|
+
|
|
10
|
+
const ACTION_KEY = Symbol.for("json-render-actions");
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Pending confirmation state
|
|
14
|
+
*/
|
|
15
|
+
export interface PendingConfirmation {
|
|
16
|
+
action: ResolvedAction;
|
|
17
|
+
handler: ActionHandler;
|
|
18
|
+
resolve: () => void;
|
|
19
|
+
reject: () => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface CurrentValue<T> {
|
|
23
|
+
readonly current: T;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Action context value
|
|
28
|
+
*/
|
|
29
|
+
export interface ActionContext {
|
|
30
|
+
/** Registered action handlers */
|
|
31
|
+
handlers: Record<string, ActionHandler>;
|
|
32
|
+
/** Currently loading action names */
|
|
33
|
+
loadingActions: Set<string>;
|
|
34
|
+
/** Pending confirmation dialog */
|
|
35
|
+
pendingConfirmation: PendingConfirmation | null;
|
|
36
|
+
/** Execute an action binding */
|
|
37
|
+
execute: (binding: ActionBinding) => Promise<void>;
|
|
38
|
+
/** Confirm the pending action */
|
|
39
|
+
confirm: () => void;
|
|
40
|
+
/** Cancel the pending action */
|
|
41
|
+
cancel: () => void;
|
|
42
|
+
/** Register an action handler */
|
|
43
|
+
registerHandler: (name: string, handler: ActionHandler) => void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Get the action context from component tree
|
|
48
|
+
*/
|
|
49
|
+
export function getActionContext(): ActionContext {
|
|
50
|
+
const ctx = getContext<ActionContext>(ACTION_KEY);
|
|
51
|
+
if (!ctx) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
"getActionContext must be called within an ActionProvider",
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return ctx;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Convenience helper to get a registered action handler by name
|
|
61
|
+
*/
|
|
62
|
+
export function getAction(
|
|
63
|
+
name: string,
|
|
64
|
+
): CurrentValue<ActionHandler | undefined> {
|
|
65
|
+
const context = getActionContext();
|
|
66
|
+
return {
|
|
67
|
+
get current() {
|
|
68
|
+
return context.handlers[name];
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Props for ConfirmDialog component
|
|
75
|
+
*/
|
|
76
|
+
export interface ConfirmDialogProps {
|
|
77
|
+
/** The confirmation config */
|
|
78
|
+
confirm: ActionConfirm;
|
|
79
|
+
/** Called when confirmed */
|
|
80
|
+
onConfirm: () => void;
|
|
81
|
+
/** Called when cancelled */
|
|
82
|
+
onCancel: () => void;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Generate a unique ID for use with the "$id" token.
|
|
87
|
+
*/
|
|
88
|
+
let idCounter = 0;
|
|
89
|
+
function generateUniqueId(): string {
|
|
90
|
+
idCounter += 1;
|
|
91
|
+
return `${Date.now()}-${idCounter}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Deep-resolve dynamic value references within an object.
|
|
96
|
+
*/
|
|
97
|
+
function deepResolveValue(
|
|
98
|
+
value: unknown,
|
|
99
|
+
get: (path: string) => unknown,
|
|
100
|
+
): unknown {
|
|
101
|
+
if (value === null || value === undefined) return value;
|
|
102
|
+
|
|
103
|
+
if (value === "$id") {
|
|
104
|
+
return generateUniqueId();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
108
|
+
const obj = value as Record<string, unknown>;
|
|
109
|
+
const keys = Object.keys(obj);
|
|
110
|
+
|
|
111
|
+
if (keys.length === 1 && typeof obj.$state === "string") {
|
|
112
|
+
return get(obj.$state as string);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (keys.length === 1 && "$id" in obj) {
|
|
116
|
+
return generateUniqueId();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (Array.isArray(value)) {
|
|
121
|
+
return value.map((item) => deepResolveValue(item, get));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (typeof value === "object") {
|
|
125
|
+
const resolved: Record<string, unknown> = {};
|
|
126
|
+
for (const [key, val] of Object.entries(
|
|
127
|
+
value as Record<string, unknown>,
|
|
128
|
+
)) {
|
|
129
|
+
resolved[key] = deepResolveValue(val, get);
|
|
130
|
+
}
|
|
131
|
+
return resolved;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return value;
|
|
135
|
+
}
|
|
136
|
+
</script>
|
|
137
|
+
|
|
138
|
+
<script lang="ts">
|
|
139
|
+
import { setContext, type Snippet } from "svelte";
|
|
140
|
+
import {
|
|
141
|
+
resolveAction,
|
|
142
|
+
executeAction,
|
|
143
|
+
nextActionDispatchId,
|
|
144
|
+
notifyActionDispatch,
|
|
145
|
+
notifyActionSettle,
|
|
146
|
+
type ActionBinding as CoreActionBinding,
|
|
147
|
+
type ActionHandler as CoreActionHandler,
|
|
148
|
+
} from "@xmachines/json-render-core";
|
|
149
|
+
import { getStateContext } from "./StateProvider.svelte";
|
|
150
|
+
import { getOptionalValidationContext } from "./ValidationProvider.svelte";
|
|
151
|
+
import { SvelteSet } from "svelte/reactivity";
|
|
152
|
+
|
|
153
|
+
interface Props {
|
|
154
|
+
handlers?: Record<string, CoreActionHandler>;
|
|
155
|
+
navigate?: (path: string) => void;
|
|
156
|
+
children?: Snippet;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let { handlers = {}, navigate, children }: Props = $props();
|
|
160
|
+
|
|
161
|
+
const stateCtx = getStateContext();
|
|
162
|
+
const validation = getOptionalValidationContext();
|
|
163
|
+
|
|
164
|
+
let registeredHandlers = $state.raw<Record<string, CoreActionHandler>>({});
|
|
165
|
+
let loadingActions = new SvelteSet<string>();
|
|
166
|
+
let pendingConfirmation = $state.raw<PendingConfirmation | null>(null);
|
|
167
|
+
|
|
168
|
+
const execute = async (binding: CoreActionBinding): Promise<void> => {
|
|
169
|
+
const resolved = resolveAction(binding, stateCtx.getSnapshot());
|
|
170
|
+
|
|
171
|
+
// --- devtools / observer hooks ---
|
|
172
|
+
const dispatchId = nextActionDispatchId();
|
|
173
|
+
const dispatchedAt = Date.now();
|
|
174
|
+
notifyActionDispatch({
|
|
175
|
+
id: dispatchId,
|
|
176
|
+
name: resolved.action,
|
|
177
|
+
params: resolved.params,
|
|
178
|
+
at: dispatchedAt,
|
|
179
|
+
});
|
|
180
|
+
let __ok = true;
|
|
181
|
+
let __error: unknown = undefined;
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
if (resolved.action === "setState" && resolved.params) {
|
|
185
|
+
const statePath = resolved.params.statePath as string;
|
|
186
|
+
const value = resolved.params.value;
|
|
187
|
+
if (statePath) {
|
|
188
|
+
stateCtx.set(statePath, value);
|
|
189
|
+
}
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (resolved.action === "pushState" && resolved.params) {
|
|
194
|
+
const statePath = resolved.params.statePath as string;
|
|
195
|
+
const rawValue = resolved.params.value;
|
|
196
|
+
if (statePath) {
|
|
197
|
+
const resolvedValue = deepResolveValue(rawValue, stateCtx.get);
|
|
198
|
+
const arr = (stateCtx.get(statePath) as unknown[] | undefined) ?? [];
|
|
199
|
+
stateCtx.set(statePath, [...arr, resolvedValue]);
|
|
200
|
+
const clearStatePath = resolved.params.clearStatePath as
|
|
201
|
+
| string
|
|
202
|
+
| undefined;
|
|
203
|
+
if (clearStatePath) {
|
|
204
|
+
stateCtx.set(clearStatePath, "");
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (resolved.action === "removeState" && resolved.params) {
|
|
211
|
+
const statePath = resolved.params.statePath as string;
|
|
212
|
+
const index = resolved.params.index as number;
|
|
213
|
+
if (statePath !== undefined && index !== undefined) {
|
|
214
|
+
const arr = (stateCtx.get(statePath) as unknown[] | undefined) ?? [];
|
|
215
|
+
stateCtx.set(
|
|
216
|
+
statePath,
|
|
217
|
+
arr.filter((_, i) => i !== index),
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (resolved.action === "validateForm") {
|
|
224
|
+
if (!validation?.validateAll) {
|
|
225
|
+
console.warn(
|
|
226
|
+
"validateForm action was dispatched but no ValidationProvider is connected. " +
|
|
227
|
+
"Ensure ValidationProvider is rendered inside the provider tree.",
|
|
228
|
+
);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const valid = validation.validateAll();
|
|
232
|
+
const errors: Record<string, string[]> = {};
|
|
233
|
+
for (const [path, fieldState] of Object.entries(validation.fieldStates)) {
|
|
234
|
+
if (fieldState.result && !fieldState.result.valid) {
|
|
235
|
+
errors[path] = fieldState.result.errors;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
const statePath =
|
|
239
|
+
(resolved.params?.statePath as string) || "/formValidation";
|
|
240
|
+
stateCtx.set(statePath, { valid, errors });
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (resolved.action === "push" && resolved.params) {
|
|
245
|
+
const screen = resolved.params.screen as string;
|
|
246
|
+
if (screen) {
|
|
247
|
+
const currentScreen = stateCtx.get("/currentScreen") as
|
|
248
|
+
| string
|
|
249
|
+
| undefined;
|
|
250
|
+
const navStack =
|
|
251
|
+
(stateCtx.get("/navStack") as string[] | undefined) ?? [];
|
|
252
|
+
if (currentScreen) {
|
|
253
|
+
stateCtx.set("/navStack", [...navStack, currentScreen]);
|
|
254
|
+
} else {
|
|
255
|
+
stateCtx.set("/navStack", [...navStack, ""]);
|
|
256
|
+
}
|
|
257
|
+
stateCtx.set("/currentScreen", screen);
|
|
258
|
+
}
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (resolved.action === "pop") {
|
|
263
|
+
const navStack =
|
|
264
|
+
(stateCtx.get("/navStack") as string[] | undefined) ?? [];
|
|
265
|
+
if (navStack.length > 0) {
|
|
266
|
+
const previousScreen = navStack[navStack.length - 1];
|
|
267
|
+
stateCtx.set("/navStack", navStack.slice(0, -1));
|
|
268
|
+
if (previousScreen) {
|
|
269
|
+
stateCtx.set("/currentScreen", previousScreen);
|
|
270
|
+
} else {
|
|
271
|
+
stateCtx.set("/currentScreen", undefined);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const handler =
|
|
278
|
+
registeredHandlers[resolved.action] ?? handlers[resolved.action];
|
|
279
|
+
|
|
280
|
+
if (!handler) {
|
|
281
|
+
console.warn(`No handler registered for action: ${resolved.action}`);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (resolved.confirm) {
|
|
286
|
+
return new Promise<void>((resolve, reject) => {
|
|
287
|
+
pendingConfirmation = {
|
|
288
|
+
action: resolved,
|
|
289
|
+
handler,
|
|
290
|
+
resolve: () => {
|
|
291
|
+
pendingConfirmation = null;
|
|
292
|
+
resolve();
|
|
293
|
+
},
|
|
294
|
+
reject: () => {
|
|
295
|
+
pendingConfirmation = null;
|
|
296
|
+
reject(new Error("Action cancelled"));
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
}).then(async () => {
|
|
300
|
+
loadingActions.add(resolved.action);
|
|
301
|
+
try {
|
|
302
|
+
await executeAction({
|
|
303
|
+
action: resolved,
|
|
304
|
+
handler,
|
|
305
|
+
setState: stateCtx.set,
|
|
306
|
+
navigate,
|
|
307
|
+
executeAction: async (name) => {
|
|
308
|
+
const subBinding: CoreActionBinding = { action: name };
|
|
309
|
+
await execute(subBinding);
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
} finally {
|
|
313
|
+
loadingActions.delete(resolved.action);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
loadingActions.add(resolved.action);
|
|
319
|
+
try {
|
|
320
|
+
await executeAction({
|
|
321
|
+
action: resolved,
|
|
322
|
+
handler,
|
|
323
|
+
setState: stateCtx.set,
|
|
324
|
+
navigate,
|
|
325
|
+
executeAction: async (name) => {
|
|
326
|
+
const subBinding: CoreActionBinding = { action: name };
|
|
327
|
+
await execute(subBinding);
|
|
328
|
+
},
|
|
329
|
+
});
|
|
330
|
+
} finally {
|
|
331
|
+
loadingActions.delete(resolved.action);
|
|
332
|
+
}
|
|
333
|
+
} catch (err) {
|
|
334
|
+
__ok = false;
|
|
335
|
+
__error = err;
|
|
336
|
+
throw err;
|
|
337
|
+
} finally {
|
|
338
|
+
const now = Date.now();
|
|
339
|
+
notifyActionSettle({
|
|
340
|
+
id: dispatchId,
|
|
341
|
+
name: resolved.action,
|
|
342
|
+
ok: __ok,
|
|
343
|
+
at: now,
|
|
344
|
+
durationMs: now - dispatchedAt,
|
|
345
|
+
error: __error,
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
const ctx: ActionContext = {
|
|
351
|
+
get handlers() {
|
|
352
|
+
return { ...handlers, ...registeredHandlers };
|
|
353
|
+
},
|
|
354
|
+
get loadingActions() {
|
|
355
|
+
return loadingActions;
|
|
356
|
+
},
|
|
357
|
+
get pendingConfirmation() {
|
|
358
|
+
return pendingConfirmation;
|
|
359
|
+
},
|
|
360
|
+
execute,
|
|
361
|
+
confirm: () => {
|
|
362
|
+
pendingConfirmation?.resolve();
|
|
363
|
+
},
|
|
364
|
+
cancel: () => {
|
|
365
|
+
pendingConfirmation?.reject();
|
|
366
|
+
},
|
|
367
|
+
registerHandler: (name: string, handler: CoreActionHandler) => {
|
|
368
|
+
registeredHandlers = { ...registeredHandlers, [name]: handler };
|
|
369
|
+
},
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
setContext(ACTION_KEY, ctx);
|
|
373
|
+
</script>
|
|
374
|
+
|
|
375
|
+
{@render children?.()}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ActionBinding, ActionConfirm, ActionHandler, ResolvedAction } from "@xmachines/json-render-core";
|
|
2
|
+
/**
|
|
3
|
+
* Pending confirmation state
|
|
4
|
+
*/
|
|
5
|
+
export interface PendingConfirmation {
|
|
6
|
+
action: ResolvedAction;
|
|
7
|
+
handler: ActionHandler;
|
|
8
|
+
resolve: () => void;
|
|
9
|
+
reject: () => void;
|
|
10
|
+
}
|
|
11
|
+
export interface CurrentValue<T> {
|
|
12
|
+
readonly current: T;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Action context value
|
|
16
|
+
*/
|
|
17
|
+
export interface ActionContext {
|
|
18
|
+
/** Registered action handlers */
|
|
19
|
+
handlers: Record<string, ActionHandler>;
|
|
20
|
+
/** Currently loading action names */
|
|
21
|
+
loadingActions: Set<string>;
|
|
22
|
+
/** Pending confirmation dialog */
|
|
23
|
+
pendingConfirmation: PendingConfirmation | null;
|
|
24
|
+
/** Execute an action binding */
|
|
25
|
+
execute: (binding: ActionBinding) => Promise<void>;
|
|
26
|
+
/** Confirm the pending action */
|
|
27
|
+
confirm: () => void;
|
|
28
|
+
/** Cancel the pending action */
|
|
29
|
+
cancel: () => void;
|
|
30
|
+
/** Register an action handler */
|
|
31
|
+
registerHandler: (name: string, handler: ActionHandler) => void;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Get the action context from component tree
|
|
35
|
+
*/
|
|
36
|
+
export declare function getActionContext(): ActionContext;
|
|
37
|
+
/**
|
|
38
|
+
* Convenience helper to get a registered action handler by name
|
|
39
|
+
*/
|
|
40
|
+
export declare function getAction(name: string): CurrentValue<ActionHandler | undefined>;
|
|
41
|
+
/**
|
|
42
|
+
* Props for ConfirmDialog component
|
|
43
|
+
*/
|
|
44
|
+
export interface ConfirmDialogProps {
|
|
45
|
+
/** The confirmation config */
|
|
46
|
+
confirm: ActionConfirm;
|
|
47
|
+
/** Called when confirmed */
|
|
48
|
+
onConfirm: () => void;
|
|
49
|
+
/** Called when cancelled */
|
|
50
|
+
onCancel: () => void;
|
|
51
|
+
}
|
|
52
|
+
import { type Snippet } from "svelte";
|
|
53
|
+
import { type ActionHandler as CoreActionHandler } from "@xmachines/json-render-core";
|
|
54
|
+
interface Props {
|
|
55
|
+
handlers?: Record<string, CoreActionHandler>;
|
|
56
|
+
navigate?: (path: string) => void;
|
|
57
|
+
children?: Snippet;
|
|
58
|
+
}
|
|
59
|
+
declare const ActionProvider: import("svelte").Component<Props, {}, "">;
|
|
60
|
+
type ActionProvider = ReturnType<typeof ActionProvider>;
|
|
61
|
+
export default ActionProvider;
|
|
62
|
+
//# sourceMappingURL=ActionProvider.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ActionProvider.svelte.d.ts","sourceRoot":"","sources":["../../src/contexts/ActionProvider.svelte.ts"],"names":[],"mappings":"AAIE,OAAO,KAAK,EACV,aAAa,EACb,aAAa,EACb,aAAa,EACb,cAAc,EACf,MAAM,mBAAmB,CAAC;AAI3B;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,cAAc,CAAC;IACvB,OAAO,EAAE,aAAa,CAAC;IACvB,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB;AAED,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,iCAAiC;IACjC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IACxC,qCAAqC;IACrC,cAAc,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAC5B,kCAAkC;IAClC,mBAAmB,EAAE,mBAAmB,GAAG,IAAI,CAAC;IAChD,gCAAgC;IAChC,OAAO,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACnD,iCAAiC;IACjC,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,gCAAgC;IAChC,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,iCAAiC;IACjC,eAAe,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,KAAK,IAAI,CAAC;CACjE;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,aAAa,CAQhD;AAED;;GAEG;AACH,wBAAgB,SAAS,CACvB,IAAI,EAAE,MAAM,GACX,YAAY,CAAC,aAAa,GAAG,SAAS,CAAC,CAOzC;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,8BAA8B;IAC9B,OAAO,EAAE,aAAa,CAAC;IACvB,4BAA4B;IAC5B,SAAS,EAAE,MAAM,IAAI,CAAC;IACtB,4BAA4B;IAC5B,QAAQ,EAAE,MAAM,IAAI,CAAC;CACtB;AAuDH,OAAO,EAAc,KAAK,OAAO,EAAE,MAAM,QAAQ,CAAC;AAClD,OAAO,EAOH,KAAK,aAAa,IAAI,iBAAiB,EACxC,MAAM,mBAAmB,CAAC;AAM3B,UAAU,KAAK;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC;IAC7C,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAuOH,QAAA,MAAM,cAAc,2CAAwC,CAAC;AAC7D,KAAK,cAAc,GAAG,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;AACxD,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
import { getContext } from "svelte";
|
|
3
|
+
import type { DirectiveDefinition, DirectiveRegistry } from "@xmachines/json-render-core";
|
|
4
|
+
import { createDirectiveRegistry } from "@xmachines/json-render-core";
|
|
5
|
+
|
|
6
|
+
const DIRECTIVES_KEY = Symbol.for("json-render-directives");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Directives context value
|
|
10
|
+
*/
|
|
11
|
+
export interface DirectivesContext {
|
|
12
|
+
/** Custom directive registry */
|
|
13
|
+
registry: DirectiveRegistry | undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get the directives registry from component tree
|
|
18
|
+
*/
|
|
19
|
+
export function getDirectives(): DirectiveRegistry | undefined {
|
|
20
|
+
const ctx = getContext<DirectivesContext>(DIRECTIVES_KEY);
|
|
21
|
+
return ctx?.registry;
|
|
22
|
+
}
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<script lang="ts">
|
|
26
|
+
import { setContext, type Snippet } from "svelte";
|
|
27
|
+
|
|
28
|
+
interface Props {
|
|
29
|
+
directives?: DirectiveDefinition[];
|
|
30
|
+
children?: Snippet;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let { directives, children }: Props = $props();
|
|
34
|
+
|
|
35
|
+
let registry = $derived(
|
|
36
|
+
directives ? createDirectiveRegistry(directives) : undefined,
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
setContext(DIRECTIVES_KEY, {
|
|
40
|
+
get registry() {
|
|
41
|
+
return registry;
|
|
42
|
+
},
|
|
43
|
+
} satisfies DirectivesContext);
|
|
44
|
+
</script>
|
|
45
|
+
|
|
46
|
+
{@render children?.()}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { DirectiveDefinition, DirectiveRegistry } from "@xmachines/json-render-core";
|
|
2
|
+
/**
|
|
3
|
+
* Directives context value
|
|
4
|
+
*/
|
|
5
|
+
export interface DirectivesContext {
|
|
6
|
+
/** Custom directive registry */
|
|
7
|
+
registry: DirectiveRegistry | undefined;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Get the directives registry from component tree
|
|
11
|
+
*/
|
|
12
|
+
export declare function getDirectives(): DirectiveRegistry | undefined;
|
|
13
|
+
import { type Snippet } from "svelte";
|
|
14
|
+
interface Props {
|
|
15
|
+
directives?: DirectiveDefinition[];
|
|
16
|
+
children?: Snippet;
|
|
17
|
+
}
|
|
18
|
+
declare const DirectivesContextProvider: import("svelte").Component<Props, {}, "">;
|
|
19
|
+
type DirectivesContextProvider = ReturnType<typeof DirectivesContextProvider>;
|
|
20
|
+
export default DirectivesContextProvider;
|
|
21
|
+
//# sourceMappingURL=DirectivesContextProvider.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DirectivesContextProvider.svelte.d.ts","sourceRoot":"","sources":["../../src/contexts/DirectivesContextProvider.svelte.ts"],"names":[],"mappings":"AAIE,OAAO,KAAK,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAKhF;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,gCAAgC;IAChC,QAAQ,EAAE,iBAAiB,GAAG,SAAS,CAAC;CACzC;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,iBAAiB,GAAG,SAAS,CAG7D;AAGH,OAAO,EAAc,KAAK,OAAO,EAAE,MAAM,QAAQ,CAAC;AAGhD,UAAU,KAAK;IACb,UAAU,CAAC,EAAE,mBAAmB,EAAE,CAAC;IACnC,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAwBH,QAAA,MAAM,yBAAyB,2CAAwC,CAAC;AACxE,KAAK,yBAAyB,GAAG,UAAU,CAAC,OAAO,yBAAyB,CAAC,CAAC;AAC9E,eAAe,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
import { getContext } from "svelte";
|
|
3
|
+
import type { ComputedFunction } from "@xmachines/json-render-core";
|
|
4
|
+
|
|
5
|
+
const FUNCTIONS_KEY = Symbol.for("json-render-functions");
|
|
6
|
+
const EMPTY_FUNCTIONS: Record<string, ComputedFunction> = {};
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Functions context value
|
|
10
|
+
*/
|
|
11
|
+
export interface FunctionsContext {
|
|
12
|
+
/** Named functions for `$computed` expressions */
|
|
13
|
+
functions: Record<string, ComputedFunction>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get the functions context from component tree
|
|
18
|
+
*/
|
|
19
|
+
export function getFunctions(): Record<string, ComputedFunction> {
|
|
20
|
+
const ctx = getContext<FunctionsContext>(FUNCTIONS_KEY);
|
|
21
|
+
return ctx?.functions ?? EMPTY_FUNCTIONS;
|
|
22
|
+
}
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<script lang="ts">
|
|
26
|
+
import { setContext, type Snippet } from "svelte";
|
|
27
|
+
|
|
28
|
+
interface Props {
|
|
29
|
+
functions?: Record<string, ComputedFunction>;
|
|
30
|
+
children?: Snippet;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let { functions, children }: Props = $props();
|
|
34
|
+
|
|
35
|
+
const ctx: FunctionsContext = {
|
|
36
|
+
get functions() {
|
|
37
|
+
return functions ?? EMPTY_FUNCTIONS;
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
setContext(FUNCTIONS_KEY, ctx);
|
|
42
|
+
</script>
|
|
43
|
+
|
|
44
|
+
{@render children?.()}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ComputedFunction } from "@xmachines/json-render-core";
|
|
2
|
+
/**
|
|
3
|
+
* Functions context value
|
|
4
|
+
*/
|
|
5
|
+
export interface FunctionsContext {
|
|
6
|
+
/** Named functions for `$computed` expressions */
|
|
7
|
+
functions: Record<string, ComputedFunction>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Get the functions context from component tree
|
|
11
|
+
*/
|
|
12
|
+
export declare function getFunctions(): Record<string, ComputedFunction>;
|
|
13
|
+
import { type Snippet } from "svelte";
|
|
14
|
+
interface Props {
|
|
15
|
+
functions?: Record<string, ComputedFunction>;
|
|
16
|
+
children?: Snippet;
|
|
17
|
+
}
|
|
18
|
+
declare const FunctionsContextProvider: import("svelte").Component<Props, {}, "">;
|
|
19
|
+
type FunctionsContextProvider = ReturnType<typeof FunctionsContextProvider>;
|
|
20
|
+
export default FunctionsContextProvider;
|
|
21
|
+
//# sourceMappingURL=FunctionsContextProvider.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FunctionsContextProvider.svelte.d.ts","sourceRoot":"","sources":["../../src/contexts/FunctionsContextProvider.svelte.ts"],"names":[],"mappings":"AAIE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAK1D;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,kDAAkD;IAClD,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;CAC7C;AAED;;GAEG;AACH,wBAAgB,YAAY,IAAI,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAG/D;AAGH,OAAO,EAAc,KAAK,OAAO,EAAE,MAAM,QAAQ,CAAC;AAGhD,UAAU,KAAK;IACb,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;IAC7C,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAsBH,QAAA,MAAM,wBAAwB,2CAAwC,CAAC;AACvE,KAAK,wBAAwB,GAAG,UAAU,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAC5E,eAAe,wBAAwB,CAAC"}
|