@stonecrop/stonecrop 0.30.0 → 0.32.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/dist/stonecrop.js +3548 -2740
- package/dist/stonecrop.js.map +1 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/package.json +33 -27
- package/dist/src/client-action.d.ts +0 -31
- package/dist/src/client-action.d.ts.map +0 -1
- package/dist/src/client-action.js +0 -169
- package/dist/src/client-handler.d.ts +0 -43
- package/dist/src/client-handler.d.ts.map +0 -1
- package/dist/src/client-handler.js +0 -42
- package/dist/src/composables/lazy-link.d.ts +0 -25
- package/dist/src/composables/lazy-link.d.ts.map +0 -1
- package/dist/src/composables/lazy-link.js +0 -124
- package/dist/src/composables/operation-log.d.ts +0 -136
- package/dist/src/composables/operation-log.d.ts.map +0 -1
- package/dist/src/composables/operation-log.js +0 -224
- package/dist/src/composables/stonecrop.d.ts +0 -37
- package/dist/src/composables/stonecrop.d.ts.map +0 -1
- package/dist/src/composables/stonecrop.js +0 -433
- package/dist/src/doctype.d.ts +0 -243
- package/dist/src/doctype.d.ts.map +0 -1
- package/dist/src/doctype.js +0 -325
- package/dist/src/draft.d.ts +0 -35
- package/dist/src/draft.d.ts.map +0 -1
- package/dist/src/draft.js +0 -36
- package/dist/src/exceptions.d.ts +0 -14
- package/dist/src/exceptions.d.ts.map +0 -1
- package/dist/src/exceptions.js +0 -16
- package/dist/src/field-triggers.d.ts +0 -184
- package/dist/src/field-triggers.d.ts.map +0 -1
- package/dist/src/field-triggers.js +0 -577
- package/dist/src/index.d.ts +0 -25
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -39
- package/dist/src/plugins/index.d.ts +0 -35
- package/dist/src/plugins/index.d.ts.map +0 -1
- package/dist/src/plugins/index.js +0 -98
- package/dist/src/registry.d.ts +0 -161
- package/dist/src/registry.d.ts.map +0 -1
- package/dist/src/registry.js +0 -384
- package/dist/src/schema-validator.d.ts +0 -78
- package/dist/src/schema-validator.d.ts.map +0 -1
- package/dist/src/schema-validator.js +0 -398
- package/dist/src/stonecrop.d.ts +0 -539
- package/dist/src/stonecrop.d.ts.map +0 -1
- package/dist/src/stonecrop.js +0 -610
- package/dist/src/stores/hst.d.ts +0 -92
- package/dist/src/stores/hst.d.ts.map +0 -1
- package/dist/src/stores/hst.js +0 -487
- package/dist/src/stores/index.d.ts +0 -9
- package/dist/src/stores/index.d.ts.map +0 -1
- package/dist/src/stores/index.js +0 -13
- package/dist/src/stores/operation-log.d.ts +0 -262
- package/dist/src/stores/operation-log.d.ts.map +0 -1
- package/dist/src/stores/operation-log.js +0 -571
- package/dist/src/stores/validation.d.ts +0 -83
- package/dist/src/stores/validation.d.ts.map +0 -1
- package/dist/src/stores/validation.js +0 -104
- package/dist/src/types/client-action.d.ts +0 -121
- package/dist/src/types/client-action.d.ts.map +0 -1
- package/dist/src/types/client-action.js +0 -0
- package/dist/src/types/composable.d.ts +0 -268
- package/dist/src/types/composable.d.ts.map +0 -1
- package/dist/src/types/composable.js +0 -0
- package/dist/src/types/doctype.d.ts +0 -34
- package/dist/src/types/doctype.d.ts.map +0 -1
- package/dist/src/types/doctype.js +0 -0
- package/dist/src/types/field-triggers.d.ts +0 -186
- package/dist/src/types/field-triggers.d.ts.map +0 -1
- package/dist/src/types/field-triggers.js +0 -4
- package/dist/src/types/hst.d.ts +0 -70
- package/dist/src/types/hst.d.ts.map +0 -1
- package/dist/src/types/hst.js +0 -0
- package/dist/src/types/index.d.ts +0 -11
- package/dist/src/types/index.d.ts.map +0 -1
- package/dist/src/types/index.js +0 -11
- package/dist/src/types/operation-log.d.ts +0 -163
- package/dist/src/types/operation-log.d.ts.map +0 -1
- package/dist/src/types/operation-log.js +0 -0
- package/dist/src/types/plugin.d.ts +0 -37
- package/dist/src/types/plugin.d.ts.map +0 -1
- package/dist/src/types/plugin.js +0 -0
- package/dist/src/types/registry.d.ts +0 -11
- package/dist/src/types/registry.d.ts.map +0 -1
- package/dist/src/types/registry.js +0 -0
- package/dist/src/types/schema-validator.d.ts +0 -66
- package/dist/src/types/schema-validator.d.ts.map +0 -1
- package/dist/src/types/schema-validator.js +0 -13
- package/dist/src/types/stonecrop.d.ts +0 -32
- package/dist/src/types/stonecrop.d.ts.map +0 -1
- package/dist/src/types/stonecrop.js +0 -0
- package/dist/stonecrop.tsbuildinfo +0 -1
- package/src/client-action.ts +0 -207
- package/src/client-handler.ts +0 -55
- package/src/composables/lazy-link.ts +0 -145
- package/src/composables/operation-log.ts +0 -257
- package/src/composables/stonecrop.ts +0 -556
- package/src/doctype.ts +0 -360
- package/src/draft.ts +0 -38
- package/src/exceptions.ts +0 -16
- package/src/field-triggers.ts +0 -686
- package/src/index.ts +0 -81
- package/src/plugins/index.ts +0 -113
- package/src/registry.ts +0 -433
- package/src/schema-validator.ts +0 -476
- package/src/stonecrop.ts +0 -721
- package/src/stores/hst.ts +0 -626
- package/src/stores/index.ts +0 -21
- package/src/stores/operation-log.ts +0 -675
- package/src/stores/validation.ts +0 -138
- package/src/types/client-action.ts +0 -121
- package/src/types/composable.ts +0 -288
- package/src/types/doctype.ts +0 -35
- package/src/types/field-triggers.ts +0 -201
- package/src/types/hst.ts +0 -77
- package/src/types/index.ts +0 -11
- package/src/types/operation-log.ts +0 -202
- package/src/types/plugin.ts +0 -38
- package/src/types/registry.ts +0 -10
- package/src/types/schema-validator.ts +0 -69
- package/src/types/stonecrop.ts +0 -33
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":"6.0.3"}
|
package/src/client-action.ts
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import type { WorkflowMeta } from '@stonecrop/schema'
|
|
2
|
-
import { useRouter } from 'vue-router'
|
|
3
|
-
|
|
4
|
-
import { executeClientHandler } from './client-handler'
|
|
5
|
-
import { useStonecrop } from './composables/stonecrop'
|
|
6
|
-
import Doctype from './doctype'
|
|
7
|
-
import { isDraftRecordId } from './draft'
|
|
8
|
-
import type {
|
|
9
|
-
ActionDispatchResult,
|
|
10
|
-
ActionEventPayload,
|
|
11
|
-
ActionFailure,
|
|
12
|
-
FollowRecordContext,
|
|
13
|
-
UseClientActionOptions,
|
|
14
|
-
} from './types/client-action'
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The identity a server response settled on, or `undefined` when it did not state one.
|
|
18
|
-
*
|
|
19
|
-
* Deliberately stricter than `getRecordId`: that falls back to `id` when the declared key is
|
|
20
|
-
* absent, which here would let a handler returning a partial record (say `{ id, total }` for a
|
|
21
|
-
* natural-keyed doctype) look like a rename and relocate the record to a key the adapter cannot
|
|
22
|
-
* look up. An action whose result carries no identity at all — a `{ state }` outcome — leaves the
|
|
23
|
-
* record exactly where it is.
|
|
24
|
-
*/
|
|
25
|
-
function settledRecordId(doctype: Doctype, record: Record<string, unknown>): string | undefined {
|
|
26
|
-
if (record[doctype.recordIdField] === undefined) return undefined
|
|
27
|
-
return doctype.getRecordId(record)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Default failure surface: a blocking alert beats a silent `console.error` for a failed action
|
|
32
|
-
* (e.g. a Save that the server refused), and there is no notification system in the framework to
|
|
33
|
-
* route it into. A host with one passes `onError` and this is never called.
|
|
34
|
-
*/
|
|
35
|
-
function notifyActionError(failure: ActionFailure): void {
|
|
36
|
-
console.error('Action failed:', failure.message)
|
|
37
|
-
if (typeof window !== 'undefined') window.alert(failure.message)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Shared executor for doctype action clicks. A host's Desktop `@action` handler delegates
|
|
42
|
-
* here so every host runs the same logic from one definition:
|
|
43
|
-
*
|
|
44
|
-
* - If the clicked action carries a `clientHandler`, run it. The handler **owns
|
|
45
|
-
* orchestration** — it calls `runAction` itself when it needs the server, navigates via
|
|
46
|
-
* `router`, reads `record`, or queries `graphql`. It supersedes the default dispatch.
|
|
47
|
-
* - Otherwise dispatch the action to its server `handler` (the pre-existing behavior),
|
|
48
|
-
* so actions without a `clientHandler` are unchanged.
|
|
49
|
-
*
|
|
50
|
-
* `runAction` is the only blessed write: it dispatches **and** leaves the store consistent,
|
|
51
|
-
* filing the returned record under the identity the *server* settled on and following the route
|
|
52
|
-
* there when that differs from the one dispatched. For a created record those are never the same,
|
|
53
|
-
* which is what makes hand-rolling this reliably wrong.
|
|
54
|
-
*
|
|
55
|
-
* The store write itself lives one layer down, in {@link Stonecrop.dispatchAction}, so a host that
|
|
56
|
-
* never adopts this composable still cannot file a record under the wrong key. What stays here is
|
|
57
|
-
* only what needs the *dispatched* id: dropping the stale key, and moving the route.
|
|
58
|
-
*
|
|
59
|
-
* Pass `buildArgs` to change the argument envelope your backend receives, `followRecord` to change
|
|
60
|
-
* where a created record sends the user, and `onError` to route failures into your own
|
|
61
|
-
* notification system. Identity resolution and HST keying are deliberately not configurable — see
|
|
62
|
-
* {@link UseClientActionOptions}.
|
|
63
|
-
*
|
|
64
|
-
* @public
|
|
65
|
-
*/
|
|
66
|
-
export function useClientAction(options: UseClientActionOptions = {}) {
|
|
67
|
-
const { stonecrop } = useStonecrop()
|
|
68
|
-
// Called during setup so the injection is available; the registry fallback below covers a
|
|
69
|
-
// caller outside an injection context, and a host that installed the plugin with `{ router }`.
|
|
70
|
-
const injectedRouter = useRouter()
|
|
71
|
-
const onError = options.onError ?? notifyActionError
|
|
72
|
-
|
|
73
|
-
const resolveRouter = () => injectedRouter ?? stonecrop.value?.registry.router
|
|
74
|
-
|
|
75
|
-
const buildArgs =
|
|
76
|
-
options.buildArgs ??
|
|
77
|
-
// Spreading `undefined` in an object literal adds nothing, so `extra` needs no fallback.
|
|
78
|
-
(({ recordId, isDraft, data, extra }) => [{ ...(isDraft ? {} : { id: recordId }), data, ...extra }])
|
|
79
|
-
|
|
80
|
-
const followRecord =
|
|
81
|
-
options.followRecord ??
|
|
82
|
-
(async ({ doctype, recordId }: FollowRecordContext) => {
|
|
83
|
-
await resolveRouter()?.replace(`/${doctype}/${recordId}`)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Dispatch `action` for the record and reconcile the store and the route with whichever
|
|
88
|
-
* identity the result carries.
|
|
89
|
-
*/
|
|
90
|
-
async function dispatchAndWriteback(
|
|
91
|
-
doctypeSlug: string,
|
|
92
|
-
recordId: string,
|
|
93
|
-
data: Record<string, unknown>,
|
|
94
|
-
action: string,
|
|
95
|
-
extra?: Record<string, unknown>
|
|
96
|
-
): Promise<ActionDispatchResult> {
|
|
97
|
-
const sc = stonecrop.value
|
|
98
|
-
if (!sc) return { success: false, data: null, error: 'Stonecrop is not initialized' }
|
|
99
|
-
|
|
100
|
-
const doctype = sc.registry.getDoctype(doctypeSlug)
|
|
101
|
-
if (!doctype) return { success: false, data: null, error: `Unknown doctype: ${doctypeSlug}` }
|
|
102
|
-
|
|
103
|
-
// A draft omits the id, which the write path reads as "create". Sending the route segment
|
|
104
|
-
// instead reaches the same branch by accident — via a lookup for a record named `new`.
|
|
105
|
-
const isDraft = isDraftRecordId(recordId)
|
|
106
|
-
const result = await sc.dispatchAction(
|
|
107
|
-
doctype,
|
|
108
|
-
action,
|
|
109
|
-
buildArgs({ doctype: doctypeSlug, action, recordId, isDraft, data, extra })
|
|
110
|
-
)
|
|
111
|
-
|
|
112
|
-
if (result.success && result.data) {
|
|
113
|
-
// 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
|
|
114
|
-
const record = result.data as Record<string, unknown>
|
|
115
|
-
const settledId = settledRecordId(doctype, record)
|
|
116
|
-
|
|
117
|
-
if (settledId === undefined) {
|
|
118
|
-
// The result states no identity of its own — a `{ state }` outcome. `dispatchAction`
|
|
119
|
-
// could not file it, because only this layer knows which record was dispatched. A
|
|
120
|
-
// draft has nowhere to put it: filing it under the route segment would leave a
|
|
121
|
-
// record named `new` in the list view.
|
|
122
|
-
if (!isDraft) sc.addRecord(doctypeSlug, recordId, record)
|
|
123
|
-
return result
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// `dispatchAction` has already filed the record under `settledId`. Only the two steps
|
|
127
|
-
// that need the dispatched id are left.
|
|
128
|
-
if (recordId !== settledId) {
|
|
129
|
-
// No-op for a draft, which was never in the store — `removeRecord` checks first.
|
|
130
|
-
sc.removeRecord(doctypeSlug, recordId)
|
|
131
|
-
await followRecord({ doctype: doctypeSlug, recordId: settledId, previousRecordId: recordId })
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return result
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Handle a Desktop `@action` event: run the action's `clientHandler` if present,
|
|
139
|
-
* else dispatch to the server handler.
|
|
140
|
-
*/
|
|
141
|
-
async function run(payload: ActionEventPayload): Promise<void> {
|
|
142
|
-
const sc = stonecrop.value
|
|
143
|
-
if (!sc) return
|
|
144
|
-
|
|
145
|
-
const { name, doctype: doctypeSlug, recordId, data } = payload
|
|
146
|
-
|
|
147
|
-
const doctype = sc.registry.getDoctype(doctypeSlug)
|
|
148
|
-
// `workflow` is a union (XState | WorkflowMeta); only WorkflowMeta carries an
|
|
149
|
-
// `actions` map with `clientHandler`. An XState workflow has no `actions`, so the
|
|
150
|
-
// lookup is undefined and we fall through to the server dispatch.
|
|
151
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- narrowing the workflow union to read the optional actions map
|
|
152
|
-
const workflow = doctype?.workflow as WorkflowMeta | undefined
|
|
153
|
-
const clientHandler = workflow?.actions?.[name]?.clientHandler
|
|
154
|
-
|
|
155
|
-
try {
|
|
156
|
-
if (!clientHandler) {
|
|
157
|
-
// No client handler — preserve the existing server-dispatch behavior.
|
|
158
|
-
const result = await dispatchAndWriteback(doctypeSlug, recordId, data, name)
|
|
159
|
-
if (!result.success) {
|
|
160
|
-
onError({
|
|
161
|
-
message: result.error ?? `Action "${name}" failed`,
|
|
162
|
-
action: name,
|
|
163
|
-
doctype: doctypeSlug,
|
|
164
|
-
recordId,
|
|
165
|
-
})
|
|
166
|
-
}
|
|
167
|
-
return
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Client handler present — it owns orchestration. Assemble the capability map.
|
|
171
|
-
// Prefer the live HST record; fall back to the form snapshot in the payload.
|
|
172
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- HSTNode.get returns any; a record node is always an object of field values
|
|
173
|
-
const record = (sc.getRecordById(doctypeSlug, recordId)?.get('') as Record<string, unknown> | undefined) ?? data
|
|
174
|
-
|
|
175
|
-
const runAction = (action: string, extra?: Record<string, unknown>) =>
|
|
176
|
-
dispatchAndWriteback(doctypeSlug, recordId, data, action, extra)
|
|
177
|
-
|
|
178
|
-
// Read-only GraphQL escape hatch — no mutation is injected (a raw mutation would
|
|
179
|
-
// bypass the dispatch and leave HST stale). `query` is the GraphQL transport's
|
|
180
|
-
// method, not part of the abstract DataClient interface (a non-GraphQL client may
|
|
181
|
-
// not support it), so probe for it structurally and reject if unsupported.
|
|
182
|
-
const graphql = {
|
|
183
|
-
query<T = unknown>(query: string, variables?: Record<string, unknown>): Promise<T> {
|
|
184
|
-
// 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
|
|
185
|
-
const client = sc.getClient() as unknown as
|
|
186
|
-
{ query?: <R>(q: string, v?: Record<string, unknown>) => Promise<R> } | undefined
|
|
187
|
-
if (!client?.query) {
|
|
188
|
-
return Promise.reject(new Error('The configured data client does not support graphql.query'))
|
|
189
|
-
}
|
|
190
|
-
return client.query<T>(query, variables)
|
|
191
|
-
},
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
await executeClientHandler(clientHandler, { router: resolveRouter(), record, runAction, graphql })
|
|
195
|
-
} catch (error) {
|
|
196
|
-
onError({
|
|
197
|
-
message: error instanceof Error ? error.message : String(error),
|
|
198
|
-
action: name,
|
|
199
|
-
doctype: doctypeSlug,
|
|
200
|
-
recordId,
|
|
201
|
-
cause: error,
|
|
202
|
-
})
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return { run }
|
|
207
|
-
}
|
package/src/client-handler.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* clientHandler execution
|
|
3
|
-
* Pure executor for docbuilder-authored client handlers.
|
|
4
|
-
* @packageDocumentation
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Named capabilities injected into a clientHandler body as function parameters.
|
|
9
|
-
* The caller (the assembly composable) owns what each name resolves to — typically
|
|
10
|
-
* `router`, `record`, `runAction`, and a read-only `graphql`.
|
|
11
|
-
*
|
|
12
|
-
* @public
|
|
13
|
-
*/
|
|
14
|
-
export type ClientHandlerApi = Record<string, unknown>
|
|
15
|
-
|
|
16
|
-
// AsyncFunction is not a global; reach it via an async function's constructor so authored
|
|
17
|
-
// bodies can use top-level `await` without writing an async wrapper.
|
|
18
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- an async function's constructor is always the AsyncFunction constructor
|
|
19
|
-
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor as new (
|
|
20
|
-
...args: string[]
|
|
21
|
-
) => (...args: unknown[]) => Promise<unknown>
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Execute a docbuilder-authored `clientHandler` body against an injected API map.
|
|
25
|
-
*
|
|
26
|
-
* `code` is a function *body* (statements), not a full function — authored in the
|
|
27
|
-
* docbuilder code editor and stored on `ActionDefinition.clientHandler`. It is compiled
|
|
28
|
-
* with the AsyncFunction constructor, so `await` works directly. Each key of `api`
|
|
29
|
-
* becomes a parameter name bound to its value, so a handler can reference `router`,
|
|
30
|
-
* `record`, `runAction`, etc. by name.
|
|
31
|
-
*
|
|
32
|
-
* This executor is deliberately concern-free: it performs no routing, dispatch, or HST
|
|
33
|
-
* writes itself — the caller assembles `api`. The injected set is an *intent* contract,
|
|
34
|
-
* not a sandbox (an AsyncFunction body can still reach `fetch`/`window`); enforcement of
|
|
35
|
-
* what a handler may actually do lives server-side, not here.
|
|
36
|
-
*
|
|
37
|
-
* Errors are propagated to the caller as a rejected promise — syntax errors at compile
|
|
38
|
-
* time and thrown errors / rejections at run time — so callers handle both uniformly.
|
|
39
|
-
*
|
|
40
|
-
* @param code - the clientHandler body, e.g. `"router.push('/users')"`
|
|
41
|
-
* @param api - named capabilities injected as parameters (default: none)
|
|
42
|
-
* @returns whatever the body returns (typically `void`)
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* ```ts
|
|
46
|
-
* await executeClientHandler("await runAction('Assign', [record.id])", { runAction, record })
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* @public
|
|
50
|
-
*/
|
|
51
|
-
export async function executeClientHandler(code: string, api: ClientHandlerApi = {}): Promise<unknown> {
|
|
52
|
-
const names = Object.keys(api)
|
|
53
|
-
const fn = new AsyncFunction(...names, code)
|
|
54
|
-
return fn(...names.map(name => api[name]))
|
|
55
|
-
}
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import type { FetchStrategy, CustomFetch } from '@stonecrop/schema'
|
|
2
|
-
import { computed, inject, ref } from 'vue'
|
|
3
|
-
|
|
4
|
-
import Doctype from '../doctype'
|
|
5
|
-
import { Stonecrop } from '../stonecrop'
|
|
6
|
-
import type { LazyLink } from '../types/composable'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Get the lazy link state for a specific link field on a doctype record.
|
|
10
|
-
*
|
|
11
|
-
* This composable provides reactive state for lazy-loaded links:
|
|
12
|
-
* - `loading`: true while fetching
|
|
13
|
-
* - `loaded`: true after successful fetch (permanent until reload)
|
|
14
|
-
* - `error`: error state if any
|
|
15
|
-
* - `reload()`: explicitly trigger a fetch
|
|
16
|
-
* - `data`: computed from HST, or undefined if not loaded
|
|
17
|
-
*
|
|
18
|
-
* The reload() function respects the link's fetch strategy:
|
|
19
|
-
* - `sync`: fetches via GraphQL query through fetchNestedData
|
|
20
|
-
* - `lazy`: fetches via GraphQL query through fetchNestedData
|
|
21
|
-
* - `custom`: invokes the serialized handler function directly
|
|
22
|
-
*
|
|
23
|
-
* @param doctype - The doctype instance
|
|
24
|
-
* @param recordId - The record ID
|
|
25
|
-
* @param linkFieldname - The link fieldname to load
|
|
26
|
-
* @returns LazyLink with loading, loaded, error, reload, and data
|
|
27
|
-
* @public
|
|
28
|
-
*/
|
|
29
|
-
export function useLazyLink(doctype: Doctype, recordId: string, linkFieldname: string): LazyLink {
|
|
30
|
-
const stonecropInstance = inject<Stonecrop>('$stonecrop') || Stonecrop._root
|
|
31
|
-
|
|
32
|
-
if (!stonecropInstance) {
|
|
33
|
-
throw new Error('Stonecrop instance not available. Ensure useStonecrop() has been called first.')
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const loading = ref(false)
|
|
37
|
-
const loaded = ref(false)
|
|
38
|
-
const error = ref<Error | null>(null)
|
|
39
|
-
|
|
40
|
-
const hstStore = stonecropInstance.getStore()
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Build the HST path for a lazy link field
|
|
44
|
-
*/
|
|
45
|
-
const getLinkPath = (): string => {
|
|
46
|
-
const slug = doctype.slug || doctype.doctype
|
|
47
|
-
return `${slug}.${recordId}.${linkFieldname}`
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Get the link declaration from the doctype schema
|
|
52
|
-
*/
|
|
53
|
-
const getLinkDeclaration = (): FetchStrategy | undefined => {
|
|
54
|
-
return doctype.links?.[linkFieldname]?.fetch
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Invoke a custom fetch handler
|
|
59
|
-
* The handler is a serialized function string that we execute via new Function()
|
|
60
|
-
*/
|
|
61
|
-
const invokeCustomHandler = async (handler: CustomFetch['handler']): Promise<any> => {
|
|
62
|
-
try {
|
|
63
|
-
// Create function from serialized string and invoke it
|
|
64
|
-
// The function receives the stonecrop instance and path as parameters
|
|
65
|
-
// oxlint-disable-next-line @typescript-eslint/no-implied-eval
|
|
66
|
-
const fn = new Function(
|
|
67
|
-
'stonecrop',
|
|
68
|
-
'path',
|
|
69
|
-
'hst',
|
|
70
|
-
`
|
|
71
|
-
return (${handler})(stonecrop, path, hst)
|
|
72
|
-
`
|
|
73
|
-
)
|
|
74
|
-
return await fn(stonecropInstance, getLinkPath(), hstStore)
|
|
75
|
-
} catch (err) {
|
|
76
|
-
throw new Error(`Custom handler failed: ${err instanceof Error ? err.message : String(err)}`, { cause: err })
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Fetch the link data using the appropriate strategy
|
|
82
|
-
*/
|
|
83
|
-
const fetchLinkData = async (): Promise<void> => {
|
|
84
|
-
const linkFetch = getLinkDeclaration()
|
|
85
|
-
const ancestorPath = `${doctype.slug || doctype.doctype}.${recordId}`
|
|
86
|
-
|
|
87
|
-
if (linkFetch?.method === 'custom') {
|
|
88
|
-
// Ensure ancestor path exists before invoking custom handler
|
|
89
|
-
if (!hstStore.has(ancestorPath)) {
|
|
90
|
-
hstStore.set(ancestorPath, {}, 'system')
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Custom handler - invoke directly
|
|
94
|
-
const result = await invokeCustomHandler(linkFetch.handler)
|
|
95
|
-
|
|
96
|
-
// Store result in HST at the link path
|
|
97
|
-
hstStore.set(getLinkPath(), result, 'system')
|
|
98
|
-
} else {
|
|
99
|
-
// sync or lazy (both use fetchNestedData but with different includeNested)
|
|
100
|
-
// For lazy links, we still use fetchNestedData but only for this specific link
|
|
101
|
-
await stonecropInstance.fetchNestedData(ancestorPath, doctype, recordId, { includeNested: [linkFieldname] })
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Explicitly reload the lazy link data
|
|
107
|
-
*/
|
|
108
|
-
const reload = async (): Promise<void> => {
|
|
109
|
-
if (loading.value) return
|
|
110
|
-
|
|
111
|
-
loading.value = true
|
|
112
|
-
error.value = null
|
|
113
|
-
|
|
114
|
-
try {
|
|
115
|
-
await fetchLinkData()
|
|
116
|
-
loaded.value = true
|
|
117
|
-
} catch (err) {
|
|
118
|
-
error.value = err instanceof Error ? err : new Error(String(err))
|
|
119
|
-
throw err
|
|
120
|
-
} finally {
|
|
121
|
-
loading.value = false
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Computed property that returns the loaded data from HST
|
|
127
|
-
*/
|
|
128
|
-
const data = computed(() => {
|
|
129
|
-
if (!loaded.value) return undefined
|
|
130
|
-
return hstStore.get(getLinkPath())
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
return {
|
|
134
|
-
// State
|
|
135
|
-
loading,
|
|
136
|
-
loaded,
|
|
137
|
-
error,
|
|
138
|
-
|
|
139
|
-
// Computed
|
|
140
|
-
data,
|
|
141
|
-
|
|
142
|
-
// Actions
|
|
143
|
-
reload,
|
|
144
|
-
}
|
|
145
|
-
}
|
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import { useMagicKeys, whenever } from '@vueuse/core'
|
|
2
|
-
import { storeToRefs } from 'pinia'
|
|
3
|
-
import { getCurrentInstance, inject } from 'vue'
|
|
4
|
-
|
|
5
|
-
import type { HSTNode } from '../types/hst'
|
|
6
|
-
import { useOperationLogStore } from '../stores/operation-log'
|
|
7
|
-
import type { OperationLogConfig } from '../types/operation-log'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Composable for operation log management
|
|
11
|
-
* Provides easy access to undo/redo functionality and operation history
|
|
12
|
-
*
|
|
13
|
-
* @param config - Optional configuration for the operation log
|
|
14
|
-
* @returns Operation log interface
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* const { undo, redo, canUndo, canRedo, operations, configure } = useOperationLog()
|
|
19
|
-
*
|
|
20
|
-
* // Configure the log
|
|
21
|
-
* configure({
|
|
22
|
-
* maxOperations: 50,
|
|
23
|
-
* enableCrossTabSync: true,
|
|
24
|
-
* enablePersistence: true
|
|
25
|
-
* })
|
|
26
|
-
*
|
|
27
|
-
* // Undo/redo
|
|
28
|
-
* await undo(hstStore)
|
|
29
|
-
* await redo(hstStore)
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* @public
|
|
33
|
-
*/
|
|
34
|
-
export function useOperationLog(config?: Partial<OperationLogConfig>) {
|
|
35
|
-
// inject() is only valid inside a component setup() context. When this
|
|
36
|
-
// composable is called outside one (e.g. directly in test bodies or plain
|
|
37
|
-
// scripts) skip the injection entirely and fall back to the Pinia store.
|
|
38
|
-
const injectedStore = getCurrentInstance()
|
|
39
|
-
? inject<ReturnType<typeof useOperationLogStore> | undefined>('$operationLogStore', undefined)
|
|
40
|
-
: undefined
|
|
41
|
-
const store = injectedStore || useOperationLogStore()
|
|
42
|
-
|
|
43
|
-
// Apply configuration if provided
|
|
44
|
-
if (config) {
|
|
45
|
-
store.configure(config)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Extract reactive state
|
|
49
|
-
const { operations, currentIndex, undoRedoState, canUndo, canRedo, undoCount, redoCount } = storeToRefs(store)
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Undo the last operation
|
|
53
|
-
*/
|
|
54
|
-
function undo(hstStore: HSTNode): boolean {
|
|
55
|
-
return store.undo(hstStore)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Redo the next operation
|
|
60
|
-
*/
|
|
61
|
-
function redo(hstStore: HSTNode): boolean {
|
|
62
|
-
return store.redo(hstStore)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Start a batch operation
|
|
67
|
-
*/
|
|
68
|
-
function startBatch() {
|
|
69
|
-
store.startBatch()
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Commit the current batch
|
|
74
|
-
*/
|
|
75
|
-
function commitBatch(description?: string): string | null {
|
|
76
|
-
return store.commitBatch(description)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Cancel the current batch
|
|
81
|
-
*/
|
|
82
|
-
function cancelBatch() {
|
|
83
|
-
store.cancelBatch()
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Clear all operations
|
|
88
|
-
*/
|
|
89
|
-
function clear() {
|
|
90
|
-
store.clear()
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Get operations for a specific doctype/record
|
|
95
|
-
*/
|
|
96
|
-
function getOperationsFor(doctype: string, recordId?: string) {
|
|
97
|
-
return store.getOperationsFor(doctype, recordId)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Get a snapshot of the operation log
|
|
102
|
-
*/
|
|
103
|
-
function getSnapshot() {
|
|
104
|
-
return store.getSnapshot()
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Mark an operation as irreversible
|
|
109
|
-
* @param operationId - The ID of the operation to mark
|
|
110
|
-
* @param reason - The reason why the operation is irreversible
|
|
111
|
-
*/
|
|
112
|
-
function markIrreversible(operationId: string, reason: string) {
|
|
113
|
-
store.markIrreversible(operationId, reason)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Log an action execution (stateless actions like print, email, etc.)
|
|
118
|
-
* @param doctype - The doctype the action was executed on
|
|
119
|
-
* @param actionName - The name of the action that was executed
|
|
120
|
-
* @param recordIds - Optional array of record IDs the action was executed on
|
|
121
|
-
* @param result - The result of the action execution
|
|
122
|
-
* @param error - Optional error message if action failed
|
|
123
|
-
* @returns The operation ID
|
|
124
|
-
*/
|
|
125
|
-
function logAction(
|
|
126
|
-
doctype: string,
|
|
127
|
-
actionName: string,
|
|
128
|
-
recordIds?: string[],
|
|
129
|
-
result: 'success' | 'failure' | 'pending' = 'success',
|
|
130
|
-
error?: string
|
|
131
|
-
): string {
|
|
132
|
-
return store.logAction(doctype, actionName, recordIds, result, error)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Update configuration
|
|
137
|
-
* @param options - Configuration options to update
|
|
138
|
-
*/
|
|
139
|
-
function configure(options: Partial<OperationLogConfig>) {
|
|
140
|
-
store.configure(options)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return {
|
|
144
|
-
// State
|
|
145
|
-
operations,
|
|
146
|
-
currentIndex,
|
|
147
|
-
undoRedoState,
|
|
148
|
-
canUndo,
|
|
149
|
-
canRedo,
|
|
150
|
-
undoCount,
|
|
151
|
-
redoCount,
|
|
152
|
-
|
|
153
|
-
// Methods
|
|
154
|
-
undo,
|
|
155
|
-
redo,
|
|
156
|
-
startBatch,
|
|
157
|
-
commitBatch,
|
|
158
|
-
cancelBatch,
|
|
159
|
-
clear,
|
|
160
|
-
getOperationsFor,
|
|
161
|
-
getSnapshot,
|
|
162
|
-
markIrreversible,
|
|
163
|
-
logAction,
|
|
164
|
-
configure,
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Keyboard shortcut handler for undo/redo
|
|
170
|
-
* Automatically binds Ctrl+Z (undo) and Ctrl+Shift+Z/Ctrl+Y (redo) using VueUse
|
|
171
|
-
*
|
|
172
|
-
* @param hstStore - The HST store to operate on
|
|
173
|
-
* @param enabled - Whether shortcuts are enabled (default: true)
|
|
174
|
-
*
|
|
175
|
-
* @example
|
|
176
|
-
* ```typescript
|
|
177
|
-
* import { onMounted } from 'vue'
|
|
178
|
-
*
|
|
179
|
-
* const stonecrop = useStonecrop({ doctype, recordId })
|
|
180
|
-
* useUndoRedoShortcuts(stonecrop.hstStore)
|
|
181
|
-
* ```
|
|
182
|
-
*
|
|
183
|
-
* @public
|
|
184
|
-
*/
|
|
185
|
-
export function useUndoRedoShortcuts(hstStore: HSTNode, enabled = true) {
|
|
186
|
-
if (!enabled) return
|
|
187
|
-
|
|
188
|
-
const { undo, redo, canUndo, canRedo } = useOperationLog()
|
|
189
|
-
const keys = useMagicKeys()
|
|
190
|
-
|
|
191
|
-
// Undo shortcuts: Ctrl+Z or Cmd+Z (Mac)
|
|
192
|
-
whenever(keys['Ctrl+Z'], () => {
|
|
193
|
-
if (canUndo.value) {
|
|
194
|
-
undo(hstStore)
|
|
195
|
-
}
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
whenever(keys['Meta+Z'], () => {
|
|
199
|
-
if (canUndo.value) {
|
|
200
|
-
undo(hstStore)
|
|
201
|
-
}
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
// Redo shortcuts: Ctrl+Shift+Z, Cmd+Shift+Z (Mac), or Ctrl+Y
|
|
205
|
-
whenever(keys['Ctrl+Shift+Z'], () => {
|
|
206
|
-
if (canRedo.value) {
|
|
207
|
-
redo(hstStore)
|
|
208
|
-
}
|
|
209
|
-
})
|
|
210
|
-
|
|
211
|
-
whenever(keys['Meta+Shift+Z'], () => {
|
|
212
|
-
if (canRedo.value) {
|
|
213
|
-
redo(hstStore)
|
|
214
|
-
}
|
|
215
|
-
})
|
|
216
|
-
|
|
217
|
-
whenever(keys['Ctrl+Y'], () => {
|
|
218
|
-
if (canRedo.value) {
|
|
219
|
-
redo(hstStore)
|
|
220
|
-
}
|
|
221
|
-
})
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Batch operation helper
|
|
226
|
-
* Wraps a function execution in a batch operation
|
|
227
|
-
*
|
|
228
|
-
* @param fn - The function to execute within a batch
|
|
229
|
-
* @param description - Optional description for the batch
|
|
230
|
-
* @returns The batch operation ID
|
|
231
|
-
*
|
|
232
|
-
* @example
|
|
233
|
-
* ```typescript
|
|
234
|
-
* const { withBatch } = useOperationLog()
|
|
235
|
-
*
|
|
236
|
-
* const batchId = await withBatch(() => {
|
|
237
|
-
* hstStore.set('task.123.title', 'New Title')
|
|
238
|
-
* hstStore.set('task.123.status', 'active')
|
|
239
|
-
* hstStore.set('task.123.priority', 'high')
|
|
240
|
-
* }, 'Update task details')
|
|
241
|
-
* ```
|
|
242
|
-
*
|
|
243
|
-
* @public
|
|
244
|
-
*/
|
|
245
|
-
export async function withBatch<T>(fn: () => T | Promise<T>, description?: string): Promise<string | null> {
|
|
246
|
-
const { startBatch, commitBatch, cancelBatch } = useOperationLog()
|
|
247
|
-
|
|
248
|
-
startBatch()
|
|
249
|
-
|
|
250
|
-
try {
|
|
251
|
-
await fn()
|
|
252
|
-
return commitBatch(description)
|
|
253
|
-
} catch (error) {
|
|
254
|
-
cancelBatch()
|
|
255
|
-
throw error
|
|
256
|
-
}
|
|
257
|
-
}
|