@stonecrop/stonecrop 0.16.6 → 0.17.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.
- package/README.md +16 -8
- package/dist/src/client-action.d.ts +31 -0
- package/dist/src/client-action.d.ts.map +1 -0
- package/dist/src/client-action.js +169 -0
- package/dist/src/composables/stonecrop.d.ts +9 -6
- package/dist/src/composables/stonecrop.d.ts.map +1 -1
- package/dist/src/composables/stonecrop.js +19 -90
- package/dist/src/doctype.d.ts +15 -12
- package/dist/src/doctype.d.ts.map +1 -1
- package/dist/src/doctype.js +19 -20
- package/dist/src/draft.d.ts +35 -0
- package/dist/src/draft.d.ts.map +1 -0
- package/dist/src/draft.js +36 -0
- package/dist/src/index.d.ts +4 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +10 -4
- package/dist/src/registry.d.ts.map +1 -1
- package/dist/src/registry.js +6 -8
- package/dist/src/stonecrop.d.ts +67 -27
- package/dist/src/stonecrop.d.ts.map +1 -1
- package/dist/src/stonecrop.js +98 -84
- package/dist/src/stores/operation-log.d.ts +1 -1
- package/dist/src/types/client-action.d.ts +121 -0
- package/dist/src/types/client-action.d.ts.map +1 -0
- package/dist/src/types/client-action.js +0 -0
- package/dist/src/types/doctype.d.ts +1 -4
- package/dist/src/types/doctype.d.ts.map +1 -1
- package/dist/src/types/index.d.ts +1 -0
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/types/index.js +1 -0
- package/dist/stonecrop.d.ts +274 -49
- package/dist/stonecrop.js +662 -617
- package/dist/stonecrop.js.map +1 -1
- package/package.json +4 -4
- package/src/client-action.ts +207 -0
- package/src/composables/stonecrop.ts +28 -102
- package/src/doctype.ts +18 -20
- package/src/draft.ts +38 -0
- package/src/index.ts +8 -2
- package/src/registry.ts +6 -8
- package/src/stonecrop.ts +107 -93
- package/src/types/client-action.ts +121 -0
- package/src/types/doctype.ts +1 -4
- package/src/types/index.ts +1 -0
package/README.md
CHANGED
|
@@ -29,8 +29,8 @@ app.use(Stonecrop, {
|
|
|
29
29
|
// Run `onRouterInitialized` once the router is ready (defaults to false).
|
|
30
30
|
autoInitializeRouter: true,
|
|
31
31
|
|
|
32
|
-
// Lazy-load doctype metadata from your API
|
|
33
|
-
//
|
|
32
|
+
// Lazy-load doctype metadata from your API. Called with the doctype slug you name in
|
|
33
|
+
// `useStonecrop({ doctype: 'task' })`, as { path: '/task', segments: ['task'] }.
|
|
34
34
|
getMeta: async ({ segments }) => {
|
|
35
35
|
return await fetchDoctypeMeta(segments[0])
|
|
36
36
|
},
|
|
@@ -50,13 +50,22 @@ app.use(Stonecrop, {
|
|
|
50
50
|
|
|
51
51
|
### Accessing Stonecrop Outside Vue Components
|
|
52
52
|
|
|
53
|
-
Inside a component, use `useStonecrop()`. Outside a component (e.g., workflow action handlers, utilities),
|
|
53
|
+
Inside a component, use `useStonecrop()`. Outside a component (e.g., workflow action handlers, utilities), keep the instance the plugin hands you at install time — it is passed to `onRouterInitialized` along with the Registry:
|
|
54
54
|
|
|
55
55
|
```typescript
|
|
56
|
-
import {
|
|
56
|
+
import StonecropPlugin, { type Stonecrop } from '@stonecrop/stonecrop'
|
|
57
|
+
|
|
58
|
+
export let stonecrop: Stonecrop | undefined
|
|
59
|
+
|
|
60
|
+
app.use(StonecropPlugin, {
|
|
61
|
+
router,
|
|
62
|
+
autoInitializeRouter: true,
|
|
63
|
+
onRouterInitialized: (_registry, instance) => {
|
|
64
|
+
stonecrop = instance
|
|
65
|
+
},
|
|
66
|
+
})
|
|
57
67
|
|
|
58
68
|
// In a workflow action handler or non-component utility:
|
|
59
|
-
const stonecrop = getStonecrop()
|
|
60
69
|
if (stonecrop) {
|
|
61
70
|
const payload = stonecrop.collectRecordPayload(doctype, recordId)
|
|
62
71
|
// ...
|
|
@@ -89,8 +98,8 @@ function buildMetaMap(registry: Registry): Map<string, DoctypeMeta> {
|
|
|
89
98
|
|
|
90
99
|
| Option | Type | Description |
|
|
91
100
|
| ---------------------- | ---------------------------------------------------- | --------------------------------------------------------------------------------------------- |
|
|
92
|
-
| `router` | `Router` | Vue Router instance.
|
|
93
|
-
| `getMeta` | `(ctx: RouteContext) => Doctype \| Promise<Doctype>` | Lazy-loads
|
|
101
|
+
| `router` | `Router` | Vue Router instance. Used to register a route per doctype, and by the desktop shell. |
|
|
102
|
+
| `getMeta` | `(ctx: RouteContext) => Doctype \| Promise<Doctype>` | Lazy-loads metadata for a named doctype slug. `ctx` has `path` and `segments`. |
|
|
94
103
|
| `components` | `Record<string, Component>` | Additional Vue components to register globally. |
|
|
95
104
|
| `autoInitializeRouter` | `boolean` | Call `onRouterInitialized` automatically after mount. Default: `false`. |
|
|
96
105
|
| `onRouterInitialized` | `(registry, stonecrop) => void` | Callback invoked after plugin install + mount. Receives the Registry and Stonecrop instances. |
|
|
@@ -107,7 +116,6 @@ import {
|
|
|
107
116
|
Registry, // Doctype registry (singleton)
|
|
108
117
|
Doctype, // Doctype definition class
|
|
109
118
|
useStonecrop, // Vue composable — primary integration point
|
|
110
|
-
getStonecrop, // Access singleton outside Vue components
|
|
111
119
|
HST, // HST store class
|
|
112
120
|
createHST, // HST factory function
|
|
113
121
|
} from '@stonecrop/stonecrop'
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ActionEventPayload, UseClientActionOptions } from './types/client-action';
|
|
2
|
+
/**
|
|
3
|
+
* Shared executor for doctype action clicks. A host's Desktop `@action` handler delegates
|
|
4
|
+
* here so every host runs the same logic from one definition:
|
|
5
|
+
*
|
|
6
|
+
* - If the clicked action carries a `clientHandler`, run it. The handler **owns
|
|
7
|
+
* orchestration** — it calls `runAction` itself when it needs the server, navigates via
|
|
8
|
+
* `router`, reads `record`, or queries `graphql`. It supersedes the default dispatch.
|
|
9
|
+
* - Otherwise dispatch the action to its server `handler` (the pre-existing behavior),
|
|
10
|
+
* so actions without a `clientHandler` are unchanged.
|
|
11
|
+
*
|
|
12
|
+
* `runAction` is the only blessed write: it dispatches **and** leaves the store consistent,
|
|
13
|
+
* filing the returned record under the identity the *server* settled on and following the route
|
|
14
|
+
* there when that differs from the one dispatched. For a created record those are never the same,
|
|
15
|
+
* which is what makes hand-rolling this reliably wrong.
|
|
16
|
+
*
|
|
17
|
+
* The store write itself lives one layer down, in {@link Stonecrop.dispatchAction}, so a host that
|
|
18
|
+
* never adopts this composable still cannot file a record under the wrong key. What stays here is
|
|
19
|
+
* only what needs the *dispatched* id: dropping the stale key, and moving the route.
|
|
20
|
+
*
|
|
21
|
+
* Pass `buildArgs` to change the argument envelope your backend receives, `followRecord` to change
|
|
22
|
+
* where a created record sends the user, and `onError` to route failures into your own
|
|
23
|
+
* notification system. Identity resolution and HST keying are deliberately not configurable — see
|
|
24
|
+
* {@link UseClientActionOptions}.
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export declare function useClientAction(options?: UseClientActionOptions): {
|
|
29
|
+
run: (payload: ActionEventPayload) => Promise<void>;
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=client-action.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-action.d.ts","sourceRoot":"","sources":["../../src/client-action.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAEX,kBAAkB,EAGlB,sBAAsB,EACtB,MAAM,uBAAuB,CAAA;AA0B9B;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,eAAe,CAAC,OAAO,GAAE,sBAA2B;mBA2EvC,kBAAkB,KAAG,OAAO,CAAC,IAAI,CAAC;EAkE9D"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { useRouter } from 'vue-router';
|
|
2
|
+
import { executeClientHandler } from './client-handler';
|
|
3
|
+
import { useStonecrop } from './composables/stonecrop';
|
|
4
|
+
import { isDraftRecordId } from './draft';
|
|
5
|
+
/**
|
|
6
|
+
* The identity a server response settled on, or `undefined` when it did not state one.
|
|
7
|
+
*
|
|
8
|
+
* Deliberately stricter than `getRecordId`: that falls back to `id` when the declared key is
|
|
9
|
+
* absent, which here would let a handler returning a partial record (say `{ id, total }` for a
|
|
10
|
+
* natural-keyed doctype) look like a rename and relocate the record to a key the adapter cannot
|
|
11
|
+
* look up. An action whose result carries no identity at all — a `{ state }` outcome — leaves the
|
|
12
|
+
* record exactly where it is.
|
|
13
|
+
*/
|
|
14
|
+
function settledRecordId(doctype, record) {
|
|
15
|
+
if (record[doctype.recordIdField] === undefined)
|
|
16
|
+
return undefined;
|
|
17
|
+
return doctype.getRecordId(record);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Default failure surface: a blocking alert beats a silent `console.error` for a failed action
|
|
21
|
+
* (e.g. a Save that the server refused), and there is no notification system in the framework to
|
|
22
|
+
* route it into. A host with one passes `onError` and this is never called.
|
|
23
|
+
*/
|
|
24
|
+
function notifyActionError(failure) {
|
|
25
|
+
console.error('Action failed:', failure.message);
|
|
26
|
+
if (typeof window !== 'undefined')
|
|
27
|
+
window.alert(failure.message);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Shared executor for doctype action clicks. A host's Desktop `@action` handler delegates
|
|
31
|
+
* here so every host runs the same logic from one definition:
|
|
32
|
+
*
|
|
33
|
+
* - If the clicked action carries a `clientHandler`, run it. The handler **owns
|
|
34
|
+
* orchestration** — it calls `runAction` itself when it needs the server, navigates via
|
|
35
|
+
* `router`, reads `record`, or queries `graphql`. It supersedes the default dispatch.
|
|
36
|
+
* - Otherwise dispatch the action to its server `handler` (the pre-existing behavior),
|
|
37
|
+
* so actions without a `clientHandler` are unchanged.
|
|
38
|
+
*
|
|
39
|
+
* `runAction` is the only blessed write: it dispatches **and** leaves the store consistent,
|
|
40
|
+
* filing the returned record under the identity the *server* settled on and following the route
|
|
41
|
+
* there when that differs from the one dispatched. For a created record those are never the same,
|
|
42
|
+
* which is what makes hand-rolling this reliably wrong.
|
|
43
|
+
*
|
|
44
|
+
* The store write itself lives one layer down, in {@link Stonecrop.dispatchAction}, so a host that
|
|
45
|
+
* never adopts this composable still cannot file a record under the wrong key. What stays here is
|
|
46
|
+
* only what needs the *dispatched* id: dropping the stale key, and moving the route.
|
|
47
|
+
*
|
|
48
|
+
* Pass `buildArgs` to change the argument envelope your backend receives, `followRecord` to change
|
|
49
|
+
* where a created record sends the user, and `onError` to route failures into your own
|
|
50
|
+
* notification system. Identity resolution and HST keying are deliberately not configurable — see
|
|
51
|
+
* {@link UseClientActionOptions}.
|
|
52
|
+
*
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export function useClientAction(options = {}) {
|
|
56
|
+
const { stonecrop } = useStonecrop();
|
|
57
|
+
// Called during setup so the injection is available; the registry fallback below covers a
|
|
58
|
+
// caller outside an injection context, and a host that installed the plugin with `{ router }`.
|
|
59
|
+
const injectedRouter = useRouter();
|
|
60
|
+
const onError = options.onError ?? notifyActionError;
|
|
61
|
+
const resolveRouter = () => injectedRouter ?? stonecrop.value?.registry.router;
|
|
62
|
+
const buildArgs = options.buildArgs ??
|
|
63
|
+
// Spreading `undefined` in an object literal adds nothing, so `extra` needs no fallback.
|
|
64
|
+
(({ recordId, isDraft, data, extra }) => [{ ...(isDraft ? {} : { id: recordId }), data, ...extra }]);
|
|
65
|
+
const followRecord = options.followRecord ??
|
|
66
|
+
(async ({ doctype, recordId }) => {
|
|
67
|
+
await resolveRouter()?.replace(`/${doctype}/${recordId}`);
|
|
68
|
+
});
|
|
69
|
+
/**
|
|
70
|
+
* Dispatch `action` for the record and reconcile the store and the route with whichever
|
|
71
|
+
* identity the result carries.
|
|
72
|
+
*/
|
|
73
|
+
async function dispatchAndWriteback(doctypeSlug, recordId, data, action, extra) {
|
|
74
|
+
const sc = stonecrop.value;
|
|
75
|
+
if (!sc)
|
|
76
|
+
return { success: false, data: null, error: 'Stonecrop is not initialized' };
|
|
77
|
+
const doctype = sc.registry.getDoctype(doctypeSlug);
|
|
78
|
+
if (!doctype)
|
|
79
|
+
return { success: false, data: null, error: `Unknown doctype: ${doctypeSlug}` };
|
|
80
|
+
// A draft omits the id, which the write path reads as "create". Sending the route segment
|
|
81
|
+
// instead reaches the same branch by accident — via a lookup for a record named `new`.
|
|
82
|
+
const isDraft = isDraftRecordId(recordId);
|
|
83
|
+
const result = await sc.dispatchAction(doctype, action, buildArgs({ doctype: doctypeSlug, action, recordId, isDraft, data, extra }));
|
|
84
|
+
if (result.success && result.data) {
|
|
85
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the action result payload is an opaque JSON scalar; every adapter returns the record as an object
|
|
86
|
+
const record = result.data;
|
|
87
|
+
const settledId = settledRecordId(doctype, record);
|
|
88
|
+
if (settledId === undefined) {
|
|
89
|
+
// The result states no identity of its own — a `{ state }` outcome. `dispatchAction`
|
|
90
|
+
// could not file it, because only this layer knows which record was dispatched. A
|
|
91
|
+
// draft has nowhere to put it: filing it under the route segment would leave a
|
|
92
|
+
// record named `new` in the list view.
|
|
93
|
+
if (!isDraft)
|
|
94
|
+
sc.addRecord(doctypeSlug, recordId, record);
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
// `dispatchAction` has already filed the record under `settledId`. Only the two steps
|
|
98
|
+
// that need the dispatched id are left.
|
|
99
|
+
if (recordId !== settledId) {
|
|
100
|
+
// No-op for a draft, which was never in the store — `removeRecord` checks first.
|
|
101
|
+
sc.removeRecord(doctypeSlug, recordId);
|
|
102
|
+
await followRecord({ doctype: doctypeSlug, recordId: settledId, previousRecordId: recordId });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Handle a Desktop `@action` event: run the action's `clientHandler` if present,
|
|
109
|
+
* else dispatch to the server handler.
|
|
110
|
+
*/
|
|
111
|
+
async function run(payload) {
|
|
112
|
+
const sc = stonecrop.value;
|
|
113
|
+
if (!sc)
|
|
114
|
+
return;
|
|
115
|
+
const { name, doctype: doctypeSlug, recordId, data } = payload;
|
|
116
|
+
const doctype = sc.registry.getDoctype(doctypeSlug);
|
|
117
|
+
// `workflow` is a union (XState | WorkflowMeta); only WorkflowMeta carries an
|
|
118
|
+
// `actions` map with `clientHandler`. An XState workflow has no `actions`, so the
|
|
119
|
+
// lookup is undefined and we fall through to the server dispatch.
|
|
120
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- narrowing the workflow union to read the optional actions map
|
|
121
|
+
const workflow = doctype?.workflow;
|
|
122
|
+
const clientHandler = workflow?.actions?.[name]?.clientHandler;
|
|
123
|
+
try {
|
|
124
|
+
if (!clientHandler) {
|
|
125
|
+
// No client handler — preserve the existing server-dispatch behavior.
|
|
126
|
+
const result = await dispatchAndWriteback(doctypeSlug, recordId, data, name);
|
|
127
|
+
if (!result.success) {
|
|
128
|
+
onError({
|
|
129
|
+
message: result.error ?? `Action "${name}" failed`,
|
|
130
|
+
action: name,
|
|
131
|
+
doctype: doctypeSlug,
|
|
132
|
+
recordId,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
// Client handler present — it owns orchestration. Assemble the capability map.
|
|
138
|
+
// Prefer the live HST record; fall back to the form snapshot in the payload.
|
|
139
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- HSTNode.get returns any; a record node is always an object of field values
|
|
140
|
+
const record = sc.getRecordById(doctypeSlug, recordId)?.get('') ?? data;
|
|
141
|
+
const runAction = (action, extra) => dispatchAndWriteback(doctypeSlug, recordId, data, action, extra);
|
|
142
|
+
// Read-only GraphQL escape hatch — no mutation is injected (a raw mutation would
|
|
143
|
+
// bypass the dispatch and leave HST stale). `query` is the GraphQL transport's
|
|
144
|
+
// method, not part of the abstract DataClient interface (a non-GraphQL client may
|
|
145
|
+
// not support it), so probe for it structurally and reject if unsupported.
|
|
146
|
+
const graphql = {
|
|
147
|
+
query(query, variables) {
|
|
148
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- probing DataClient for the GraphQL transport's `query`, which the abstract interface deliberately does not declare
|
|
149
|
+
const client = sc.getClient();
|
|
150
|
+
if (!client?.query) {
|
|
151
|
+
return Promise.reject(new Error('The configured data client does not support graphql.query'));
|
|
152
|
+
}
|
|
153
|
+
return client.query(query, variables);
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
await executeClientHandler(clientHandler, { router: resolveRouter(), record, runAction, graphql });
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
onError({
|
|
160
|
+
message: error instanceof Error ? error.message : String(error),
|
|
161
|
+
action: name,
|
|
162
|
+
doctype: doctypeSlug,
|
|
163
|
+
recordId,
|
|
164
|
+
cause: error,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return { run };
|
|
169
|
+
}
|
|
@@ -2,13 +2,15 @@ import Doctype from '../doctype';
|
|
|
2
2
|
import Registry from '../registry';
|
|
3
3
|
import type { BaseStonecropReturn, HSTStonecropReturn } from '../types/composable';
|
|
4
4
|
/**
|
|
5
|
-
* Unified Stonecrop composable - handles both general operations and HST reactive integration
|
|
5
|
+
* Unified Stonecrop composable - handles both general operations and HST reactive integration.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* Called with no doctype, it returns the Stonecrop instance and the operation log, and touches
|
|
8
|
+
* the network not at all. Name a doctype to get the HST surface.
|
|
9
|
+
*
|
|
10
|
+
* @returns The Stonecrop instance and the operation log
|
|
9
11
|
* @public
|
|
10
12
|
*/
|
|
11
|
-
export declare function useStonecrop(): BaseStonecropReturn
|
|
13
|
+
export declare function useStonecrop(): BaseStonecropReturn;
|
|
12
14
|
/**
|
|
13
15
|
* Unified Stonecrop composable with HST integration for a specific doctype and record.
|
|
14
16
|
*
|
|
@@ -19,8 +21,9 @@ export declare function useStonecrop(): BaseStonecropReturn | HSTStonecropReturn
|
|
|
19
21
|
* immediately after calling this composable; no `nextTick`, `flushPromises`, or
|
|
20
22
|
* `setTimeout` is required.
|
|
21
23
|
*
|
|
22
|
-
* The only
|
|
23
|
-
*
|
|
24
|
+
* The only async work in `onMounted` is fetching an existing record from the server when
|
|
25
|
+
* `recordId` is not a draft, and lazy-loading a doctype by slug string. Both need a doctype
|
|
26
|
+
* named here — nothing is inferred from the route.
|
|
24
27
|
*
|
|
25
28
|
* @param options - Configuration with doctype (string slug or Doctype instance) and optional recordId
|
|
26
29
|
* @returns Stonecrop instance with full HST integration utilities
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stonecrop.d.ts","sourceRoot":"","sources":["../../../src/composables/stonecrop.ts"],"names":[],"mappings":"AAIA,OAAO,OAAO,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"stonecrop.d.ts","sourceRoot":"","sources":["../../../src/composables/stonecrop.ts"],"names":[],"mappings":"AAIA,OAAO,OAAO,MAAM,YAAY,CAAA;AAEhC,OAAO,QAAQ,MAAM,aAAa,CAAA;AAGlC,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAkC,MAAM,qBAAqB,CAAA;AAIlH;;;;;;;;GAQG;AACH,wBAAgB,YAAY,IAAI,mBAAmB,CAAA;AACnD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE;IACrC,QAAQ,CAAC,EAAE,QAAQ,CAAA;IACnB,OAAO,EAAE,OAAO,GAAG,MAAM,CAAA;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAA;CACjB,GAAG,kBAAkB,CAAA"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { storeToRefs } from 'pinia';
|
|
2
2
|
import { inject, onMounted, ref, watch, provide, computed } from 'vue';
|
|
3
|
+
import { isDraftRecordId } from '../draft';
|
|
3
4
|
import { Stonecrop } from '../stonecrop';
|
|
4
5
|
/**
|
|
5
6
|
* @public
|
|
@@ -12,9 +13,6 @@ export function useStonecrop(options) {
|
|
|
12
13
|
const stonecrop = ref();
|
|
13
14
|
const hstStore = ref();
|
|
14
15
|
const formData = ref({});
|
|
15
|
-
// Use refs for router-loaded doctype to maintain reactivity
|
|
16
|
-
const routerDoctype = ref();
|
|
17
|
-
const routerRecordId = ref();
|
|
18
16
|
// Resolved schema with nested Doctype fields expanded
|
|
19
17
|
const resolvedSchema = ref([]);
|
|
20
18
|
// Loading state for lazy-loaded doctypes
|
|
@@ -23,14 +21,14 @@ export function useStonecrop(options) {
|
|
|
23
21
|
const resolvedDoctype = ref();
|
|
24
22
|
// Workflow readiness computed properties
|
|
25
23
|
const isWorkflowReady = computed(() => {
|
|
26
|
-
if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || options.recordId
|
|
24
|
+
if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || isDraftRecordId(options.recordId)) {
|
|
27
25
|
return true;
|
|
28
26
|
}
|
|
29
27
|
const status = stonecrop.value.isWorkflowReady(resolvedDoctype.value, options.recordId);
|
|
30
28
|
return status.ready;
|
|
31
29
|
});
|
|
32
30
|
const blockedLinks = computed(() => {
|
|
33
|
-
if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || options.recordId
|
|
31
|
+
if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || isDraftRecordId(options.recordId)) {
|
|
34
32
|
return [];
|
|
35
33
|
}
|
|
36
34
|
const status = stonecrop.value.isWorkflowReady(resolvedDoctype.value, options.recordId);
|
|
@@ -128,73 +126,25 @@ export function useStonecrop(options) {
|
|
|
128
126
|
if (options.doctype && typeof options.doctype !== 'string' && registry && stonecrop.value) {
|
|
129
127
|
hstStore.value = stonecrop.value.getStore();
|
|
130
128
|
resolvedSchema.value = registry.resolveSchema(options.doctype);
|
|
131
|
-
if (!options.recordId || options.recordId
|
|
129
|
+
if (!options.recordId || isDraftRecordId(options.recordId)) {
|
|
132
130
|
formData.value = registry.initializeRecord(resolvedSchema.value);
|
|
133
131
|
}
|
|
134
132
|
if (hstStore.value) {
|
|
135
133
|
setupDeepReactivity(options.doctype, options.recordId || 'new', formData, hstStore.value);
|
|
136
134
|
}
|
|
137
135
|
}
|
|
138
|
-
// onMounted handles only work that is genuinely async: lazy-loading a doctype
|
|
139
|
-
//
|
|
136
|
+
// onMounted handles only work that is genuinely async: lazy-loading a doctype by slug and
|
|
137
|
+
// fetching an existing record from the server. Both require the caller to name a doctype.
|
|
138
|
+
//
|
|
139
|
+
// Rejected: inferring the doctype from the route when the caller names none. Every output of
|
|
140
|
+
// that inference landed in refs the no-argument callers never read — all of them wanted the
|
|
141
|
+
// instance, which is resolved synchronously above — so each paid a getMeta plus a getRecord to
|
|
142
|
+
// read a value already in hand. It also lowercased the route's record id, which silently missed
|
|
143
|
+
// any natural key carrying uppercase.
|
|
140
144
|
onMounted(async () => {
|
|
141
145
|
if (!registry || !stonecrop.value) {
|
|
142
146
|
return;
|
|
143
147
|
}
|
|
144
|
-
// Handle router-based setup if no specific doctype provided
|
|
145
|
-
if (!options.doctype && registry.router) {
|
|
146
|
-
const route = registry.router.currentRoute.value;
|
|
147
|
-
// Parse route path - let the application determine the doctype from the route
|
|
148
|
-
if (!route.path)
|
|
149
|
-
return; // Early return if no path available
|
|
150
|
-
const pathSegments = route.path.split('/').filter(segment => segment.length > 0);
|
|
151
|
-
const recordId = pathSegments[1]?.toLowerCase();
|
|
152
|
-
if (pathSegments.length > 0) {
|
|
153
|
-
// Create route context for getMeta function
|
|
154
|
-
const routeContext = {
|
|
155
|
-
path: route.path,
|
|
156
|
-
segments: pathSegments,
|
|
157
|
-
};
|
|
158
|
-
const doctype = await registry.getMeta?.(routeContext);
|
|
159
|
-
if (doctype) {
|
|
160
|
-
registry.addDoctype(doctype);
|
|
161
|
-
stonecrop.value.setup(doctype);
|
|
162
|
-
// Set reactive refs for router-based doctype
|
|
163
|
-
routerDoctype.value = doctype;
|
|
164
|
-
routerRecordId.value = recordId;
|
|
165
|
-
hstStore.value = stonecrop.value.getStore();
|
|
166
|
-
// Resolve schema for router-loaded doctype
|
|
167
|
-
if (registry) {
|
|
168
|
-
resolvedSchema.value = registry.resolveSchema(doctype);
|
|
169
|
-
}
|
|
170
|
-
if (recordId && recordId !== 'new') {
|
|
171
|
-
const existingRecord = stonecrop.value.getRecordById(doctype, recordId);
|
|
172
|
-
if (existingRecord) {
|
|
173
|
-
formData.value = existingRecord.get('') || {};
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
try {
|
|
177
|
-
await stonecrop.value.getRecord(doctype, recordId);
|
|
178
|
-
const loadedRecord = stonecrop.value.getRecordById(doctype, recordId);
|
|
179
|
-
if (loadedRecord) {
|
|
180
|
-
formData.value = loadedRecord.get('') || {};
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
catch {
|
|
184
|
-
formData.value = registry.initializeRecord(resolvedSchema.value);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
formData.value = registry.initializeRecord(resolvedSchema.value);
|
|
190
|
-
}
|
|
191
|
-
if (hstStore.value) {
|
|
192
|
-
setupDeepReactivity(doctype, recordId || 'new', formData, hstStore.value);
|
|
193
|
-
}
|
|
194
|
-
stonecrop.value.runAction(doctype, 'load', recordId ? [recordId] : undefined);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
148
|
// Handle HST integration if doctype is provided explicitly
|
|
199
149
|
if (options.doctype) {
|
|
200
150
|
const recordId = options.recordId;
|
|
@@ -233,7 +183,7 @@ export function useStonecrop(options) {
|
|
|
233
183
|
if (!doctype)
|
|
234
184
|
return;
|
|
235
185
|
resolvedSchema.value = registry.resolveSchema(doctype);
|
|
236
|
-
if (recordId && recordId
|
|
186
|
+
if (recordId && !isDraftRecordId(recordId)) {
|
|
237
187
|
const existingRecord = stonecrop.value.getRecordById(doctype, recordId);
|
|
238
188
|
if (existingRecord) {
|
|
239
189
|
formData.value = existingRecord.get('') || {};
|
|
@@ -261,7 +211,7 @@ export function useStonecrop(options) {
|
|
|
261
211
|
else {
|
|
262
212
|
// Doctype instance — sync init was done during setup().
|
|
263
213
|
// Only handle the async path: fetching an existing record from the server.
|
|
264
|
-
if (recordId && recordId
|
|
214
|
+
if (recordId && !isDraftRecordId(recordId)) {
|
|
265
215
|
const doctype = options.doctype;
|
|
266
216
|
const existingRecord = stonecrop.value.getRecordById(doctype, recordId);
|
|
267
217
|
if (existingRecord) {
|
|
@@ -285,14 +235,14 @@ export function useStonecrop(options) {
|
|
|
285
235
|
});
|
|
286
236
|
// HST integration functions - always created but only populated when HST is available
|
|
287
237
|
const provideHSTPath = (fieldname, customRecordId) => {
|
|
288
|
-
const doctype = resolvedDoctype.value
|
|
238
|
+
const doctype = resolvedDoctype.value;
|
|
289
239
|
if (!doctype)
|
|
290
240
|
return '';
|
|
291
|
-
const actualRecordId = customRecordId || options.recordId ||
|
|
241
|
+
const actualRecordId = customRecordId || options.recordId || 'new';
|
|
292
242
|
return `${doctype.slug}.${actualRecordId}.${fieldname}`;
|
|
293
243
|
};
|
|
294
244
|
const handleHSTChange = (changeData) => {
|
|
295
|
-
const doctype = resolvedDoctype.value
|
|
245
|
+
const doctype = resolvedDoctype.value;
|
|
296
246
|
if (!hstStore.value || !stonecrop.value || !doctype) {
|
|
297
247
|
return;
|
|
298
248
|
}
|
|
@@ -334,7 +284,7 @@ export function useStonecrop(options) {
|
|
|
334
284
|
}
|
|
335
285
|
};
|
|
336
286
|
// Provide injection tokens if HST will be available
|
|
337
|
-
if (options.doctype
|
|
287
|
+
if (options.doctype) {
|
|
338
288
|
provide('hstPathProvider', provideHSTPath);
|
|
339
289
|
provide('hstChangeHandler', handleHSTChange);
|
|
340
290
|
}
|
|
@@ -443,28 +393,7 @@ export function useStonecrop(options) {
|
|
|
443
393
|
blockedLinks,
|
|
444
394
|
};
|
|
445
395
|
}
|
|
446
|
-
|
|
447
|
-
// Router-based - return HST (will be populated after mount)
|
|
448
|
-
return {
|
|
449
|
-
stonecrop,
|
|
450
|
-
operationLog,
|
|
451
|
-
provideHSTPath,
|
|
452
|
-
handleHSTChange,
|
|
453
|
-
hstStore,
|
|
454
|
-
formData,
|
|
455
|
-
resolvedSchema,
|
|
456
|
-
initializeNestedData,
|
|
457
|
-
fetchNestedData,
|
|
458
|
-
collectRecordPayload,
|
|
459
|
-
createNestedContext,
|
|
460
|
-
isLoading,
|
|
461
|
-
error,
|
|
462
|
-
resolvedDoctype,
|
|
463
|
-
isWorkflowReady,
|
|
464
|
-
blockedLinks,
|
|
465
|
-
};
|
|
466
|
-
}
|
|
467
|
-
// No doctype and no router - basic mode
|
|
396
|
+
// No doctype named: the instance and the operation log are everything this can offer.
|
|
468
397
|
return {
|
|
469
398
|
stonecrop,
|
|
470
399
|
operationLog,
|
package/dist/src/doctype.d.ts
CHANGED
|
@@ -30,12 +30,6 @@ export default class Doctype {
|
|
|
30
30
|
* @readonly
|
|
31
31
|
*/
|
|
32
32
|
readonly workflow: ImmutableDoctype['workflow'];
|
|
33
|
-
/**
|
|
34
|
-
* The doctype actions and field triggers
|
|
35
|
-
* @public
|
|
36
|
-
* @readonly
|
|
37
|
-
*/
|
|
38
|
-
readonly actions: ImmutableDoctype['actions'];
|
|
39
33
|
/**
|
|
40
34
|
* The doctype component
|
|
41
35
|
* @public
|
|
@@ -53,11 +47,10 @@ export default class Doctype {
|
|
|
53
47
|
* @param doctype - The doctype name
|
|
54
48
|
* @param schema - The doctype schema definition
|
|
55
49
|
* @param workflow - The doctype workflow configuration (XState machine)
|
|
56
|
-
* @param actions - The doctype actions and field triggers
|
|
57
50
|
* @param component - Optional Vue component for rendering the doctype
|
|
58
51
|
* @param links - Optional relationship links to other doctypes
|
|
59
52
|
*/
|
|
60
|
-
constructor(doctype: string, schema: ImmutableDoctype['schema'], workflow: ImmutableDoctype['workflow'],
|
|
53
|
+
constructor(doctype: string, schema: ImmutableDoctype['schema'], workflow: ImmutableDoctype['workflow'], component?: Component, links?: Record<string, LinkDeclaration>);
|
|
61
54
|
/**
|
|
62
55
|
* Creates a Doctype instance from a plain configuration object.
|
|
63
56
|
* Handles conversion of arrays to Immutable.js collections internally.
|
|
@@ -127,14 +120,24 @@ export default class Doctype {
|
|
|
127
120
|
*/
|
|
128
121
|
getRecordId(record: Record<string, unknown>): string | undefined;
|
|
129
122
|
/**
|
|
130
|
-
*
|
|
131
|
-
*
|
|
123
|
+
* The field a record of this doctype is identified by: the declared `primaryKey`, or `id`
|
|
124
|
+
* when nothing is declared.
|
|
125
|
+
*
|
|
126
|
+
* The client-side twin of the adapters' `recordLookupField`. Both are the same call to
|
|
127
|
+
* `@stonecrop/schema`'s `getRecordIdField`, so the field a caller reads an identity out of is
|
|
128
|
+
* the same field the adapter builds its lookup predicate on.
|
|
129
|
+
*
|
|
130
|
+
* Use this to ask whether a record *states* its own identity. `getRecordId` deliberately
|
|
131
|
+
* falls back to `id` when the declared key is missing, which is right for resolving a link
|
|
132
|
+
* from a record already in hand and wrong for deciding whether a server response settled on
|
|
133
|
+
* a new identity: a response that omits a natural key would resolve through that fallback to
|
|
134
|
+
* a surrogate the adapter cannot look up.
|
|
132
135
|
*
|
|
133
|
-
* @returns
|
|
136
|
+
* @returns The identifying fieldname
|
|
134
137
|
*
|
|
135
138
|
* @public
|
|
136
139
|
*/
|
|
137
|
-
|
|
140
|
+
get recordIdField(): string;
|
|
138
141
|
/**
|
|
139
142
|
* Returns the transitions available from a given workflow state, derived from the
|
|
140
143
|
* doctype's workflow configuration. Supports both XState format and WorkflowMeta format.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"doctype.d.ts","sourceRoot":"","sources":["../../src/doctype.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,iBAAiB,EAAgB,MAAM,mBAAmB,CAAA;AAGvG,OAAO,EAAE,SAAS,EAAE,MAAM,KAAK,CAAA;AAE/B,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAEtE;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,OAAO;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IAExB;;;;OAIG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC,QAAQ,CAAC,CAAA;IAE3C;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,gBAAgB,CAAC,UAAU,CAAC,CAAA;IAE/C;;;;OAIG;IACH,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"doctype.d.ts","sourceRoot":"","sources":["../../src/doctype.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,iBAAiB,EAAgB,MAAM,mBAAmB,CAAA;AAGvG,OAAO,EAAE,SAAS,EAAE,MAAM,KAAK,CAAA;AAE/B,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAEtE;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,OAAO;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IAExB;;;;OAIG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC,QAAQ,CAAC,CAAA;IAE3C;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,gBAAgB,CAAC,UAAU,CAAC,CAAA;IAE/C;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAA;IAE9B;;;;OAIG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IAEhD;;;;;;;OAOG;gBAEF,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,gBAAgB,CAAC,QAAQ,CAAC,EAClC,QAAQ,EAAE,gBAAgB,CAAC,UAAU,CAAC,EACtC,SAAS,CAAC,EAAE,SAAS,EACrB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC;IASxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqCG;IACH,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO;IAYjD;;;;;;;;;;;;;;OAcG;IACH,cAAc,IAAI,YAAY,EAAE;IAKhC;;;;;;;;;;;OAWG;IACH,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,SAAS;IAIhE;;;;;;;;;;;;;;;;;OAiBG;IACH,IAAI,aAAa,IAAI,MAAM,CAE1B;IAED;;;;;;;;;;;;;;OAcG;IACH,uBAAuB,CAAC,YAAY,EAAE,MAAM,GAAG,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,CAAC;IAqC3F;;;;;;;;;;;;;;;;;;OAkBG;IACH,oBAAoB,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAcpE;;;;;;;;;;;;;;OAcG;IACH,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,GAAG,SAAS;IAS5D;;;;;;;;;;;;;;OAcG;IACH,aAAa,CAAC,UAAU,EAAE,MAAM,GAC7B;QACA,KAAK,EAAE,MAAM,CAAA;QACb,cAAc,CAAC,EAAE,MAAM,EAAE,CAAA;QACzB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAA;KACvB,GACD,SAAS;IAWZ;;;;;;;;;;;;;;;OAeG;IACH,IAAI,IAAI,WAKP;CACD"}
|
package/dist/src/doctype.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getRecordIdentity, isActionAllowedInState, normalizeFieldKind } from '@stonecrop/schema';
|
|
2
|
-
import { List
|
|
1
|
+
import { getRecordIdentity, getRecordIdField, isActionAllowedInState, normalizeFieldKind } from '@stonecrop/schema';
|
|
2
|
+
import { List } from 'immutable';
|
|
3
3
|
/**
|
|
4
4
|
* Doctype runtime class with Immutable.js collections for HST change tracking.
|
|
5
5
|
* @public
|
|
@@ -31,12 +31,6 @@ export default class Doctype {
|
|
|
31
31
|
* @readonly
|
|
32
32
|
*/
|
|
33
33
|
workflow;
|
|
34
|
-
/**
|
|
35
|
-
* The doctype actions and field triggers
|
|
36
|
-
* @public
|
|
37
|
-
* @readonly
|
|
38
|
-
*/
|
|
39
|
-
actions;
|
|
40
34
|
/**
|
|
41
35
|
* The doctype component
|
|
42
36
|
* @public
|
|
@@ -54,15 +48,13 @@ export default class Doctype {
|
|
|
54
48
|
* @param doctype - The doctype name
|
|
55
49
|
* @param schema - The doctype schema definition
|
|
56
50
|
* @param workflow - The doctype workflow configuration (XState machine)
|
|
57
|
-
* @param actions - The doctype actions and field triggers
|
|
58
51
|
* @param component - Optional Vue component for rendering the doctype
|
|
59
52
|
* @param links - Optional relationship links to other doctypes
|
|
60
53
|
*/
|
|
61
|
-
constructor(doctype, schema, workflow,
|
|
54
|
+
constructor(doctype, schema, workflow, component, links) {
|
|
62
55
|
this.doctype = doctype;
|
|
63
56
|
this.schema = schema;
|
|
64
57
|
this.workflow = workflow;
|
|
65
|
-
this.actions = actions;
|
|
66
58
|
this.component = component;
|
|
67
59
|
this.links = links;
|
|
68
60
|
}
|
|
@@ -112,8 +104,7 @@ export default class Doctype {
|
|
|
112
104
|
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- normalizeFieldKind only injects the `kind` discriminant; the field shape is otherwise preserved
|
|
113
105
|
const fields = config.fields?.map(normalizeFieldKind);
|
|
114
106
|
const schema = fields ? List(fields) : List();
|
|
115
|
-
|
|
116
|
-
return new Doctype(config.name, schema, config.workflow, actions, undefined, config.links);
|
|
107
|
+
return new Doctype(config.name, schema, config.workflow, undefined, config.links);
|
|
117
108
|
}
|
|
118
109
|
/**
|
|
119
110
|
* Returns the raw authoring schema as a plain array.
|
|
@@ -151,17 +142,25 @@ export default class Doctype {
|
|
|
151
142
|
return getRecordIdentity(this.getSchemaArray(), record);
|
|
152
143
|
}
|
|
153
144
|
/**
|
|
154
|
-
*
|
|
155
|
-
*
|
|
145
|
+
* The field a record of this doctype is identified by: the declared `primaryKey`, or `id`
|
|
146
|
+
* when nothing is declared.
|
|
147
|
+
*
|
|
148
|
+
* The client-side twin of the adapters' `recordLookupField`. Both are the same call to
|
|
149
|
+
* `@stonecrop/schema`'s `getRecordIdField`, so the field a caller reads an identity out of is
|
|
150
|
+
* the same field the adapter builds its lookup predicate on.
|
|
151
|
+
*
|
|
152
|
+
* Use this to ask whether a record *states* its own identity. `getRecordId` deliberately
|
|
153
|
+
* falls back to `id` when the declared key is missing, which is right for resolving a link
|
|
154
|
+
* from a record already in hand and wrong for deciding whether a server response settled on
|
|
155
|
+
* a new identity: a response that omits a natural key would resolve through that fallback to
|
|
156
|
+
* a surrogate the adapter cannot look up.
|
|
156
157
|
*
|
|
157
|
-
* @returns
|
|
158
|
+
* @returns The identifying fieldname
|
|
158
159
|
*
|
|
159
160
|
* @public
|
|
160
161
|
*/
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
return {};
|
|
164
|
-
return this.actions.toObject();
|
|
162
|
+
get recordIdField() {
|
|
163
|
+
return getRecordIdField(this.getSchemaArray());
|
|
165
164
|
}
|
|
166
165
|
/**
|
|
167
166
|
* Returns the transitions available from a given workflow state, derived from the
|