@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.
Files changed (121) hide show
  1. package/dist/stonecrop.js +3548 -2740
  2. package/dist/stonecrop.js.map +1 -1
  3. package/dist/tsdoc-metadata.json +1 -1
  4. package/package.json +33 -27
  5. package/dist/src/client-action.d.ts +0 -31
  6. package/dist/src/client-action.d.ts.map +0 -1
  7. package/dist/src/client-action.js +0 -169
  8. package/dist/src/client-handler.d.ts +0 -43
  9. package/dist/src/client-handler.d.ts.map +0 -1
  10. package/dist/src/client-handler.js +0 -42
  11. package/dist/src/composables/lazy-link.d.ts +0 -25
  12. package/dist/src/composables/lazy-link.d.ts.map +0 -1
  13. package/dist/src/composables/lazy-link.js +0 -124
  14. package/dist/src/composables/operation-log.d.ts +0 -136
  15. package/dist/src/composables/operation-log.d.ts.map +0 -1
  16. package/dist/src/composables/operation-log.js +0 -224
  17. package/dist/src/composables/stonecrop.d.ts +0 -37
  18. package/dist/src/composables/stonecrop.d.ts.map +0 -1
  19. package/dist/src/composables/stonecrop.js +0 -433
  20. package/dist/src/doctype.d.ts +0 -243
  21. package/dist/src/doctype.d.ts.map +0 -1
  22. package/dist/src/doctype.js +0 -325
  23. package/dist/src/draft.d.ts +0 -35
  24. package/dist/src/draft.d.ts.map +0 -1
  25. package/dist/src/draft.js +0 -36
  26. package/dist/src/exceptions.d.ts +0 -14
  27. package/dist/src/exceptions.d.ts.map +0 -1
  28. package/dist/src/exceptions.js +0 -16
  29. package/dist/src/field-triggers.d.ts +0 -184
  30. package/dist/src/field-triggers.d.ts.map +0 -1
  31. package/dist/src/field-triggers.js +0 -577
  32. package/dist/src/index.d.ts +0 -25
  33. package/dist/src/index.d.ts.map +0 -1
  34. package/dist/src/index.js +0 -39
  35. package/dist/src/plugins/index.d.ts +0 -35
  36. package/dist/src/plugins/index.d.ts.map +0 -1
  37. package/dist/src/plugins/index.js +0 -98
  38. package/dist/src/registry.d.ts +0 -161
  39. package/dist/src/registry.d.ts.map +0 -1
  40. package/dist/src/registry.js +0 -384
  41. package/dist/src/schema-validator.d.ts +0 -78
  42. package/dist/src/schema-validator.d.ts.map +0 -1
  43. package/dist/src/schema-validator.js +0 -398
  44. package/dist/src/stonecrop.d.ts +0 -539
  45. package/dist/src/stonecrop.d.ts.map +0 -1
  46. package/dist/src/stonecrop.js +0 -610
  47. package/dist/src/stores/hst.d.ts +0 -92
  48. package/dist/src/stores/hst.d.ts.map +0 -1
  49. package/dist/src/stores/hst.js +0 -487
  50. package/dist/src/stores/index.d.ts +0 -9
  51. package/dist/src/stores/index.d.ts.map +0 -1
  52. package/dist/src/stores/index.js +0 -13
  53. package/dist/src/stores/operation-log.d.ts +0 -262
  54. package/dist/src/stores/operation-log.d.ts.map +0 -1
  55. package/dist/src/stores/operation-log.js +0 -571
  56. package/dist/src/stores/validation.d.ts +0 -83
  57. package/dist/src/stores/validation.d.ts.map +0 -1
  58. package/dist/src/stores/validation.js +0 -104
  59. package/dist/src/types/client-action.d.ts +0 -121
  60. package/dist/src/types/client-action.d.ts.map +0 -1
  61. package/dist/src/types/client-action.js +0 -0
  62. package/dist/src/types/composable.d.ts +0 -268
  63. package/dist/src/types/composable.d.ts.map +0 -1
  64. package/dist/src/types/composable.js +0 -0
  65. package/dist/src/types/doctype.d.ts +0 -34
  66. package/dist/src/types/doctype.d.ts.map +0 -1
  67. package/dist/src/types/doctype.js +0 -0
  68. package/dist/src/types/field-triggers.d.ts +0 -186
  69. package/dist/src/types/field-triggers.d.ts.map +0 -1
  70. package/dist/src/types/field-triggers.js +0 -4
  71. package/dist/src/types/hst.d.ts +0 -70
  72. package/dist/src/types/hst.d.ts.map +0 -1
  73. package/dist/src/types/hst.js +0 -0
  74. package/dist/src/types/index.d.ts +0 -11
  75. package/dist/src/types/index.d.ts.map +0 -1
  76. package/dist/src/types/index.js +0 -11
  77. package/dist/src/types/operation-log.d.ts +0 -163
  78. package/dist/src/types/operation-log.d.ts.map +0 -1
  79. package/dist/src/types/operation-log.js +0 -0
  80. package/dist/src/types/plugin.d.ts +0 -37
  81. package/dist/src/types/plugin.d.ts.map +0 -1
  82. package/dist/src/types/plugin.js +0 -0
  83. package/dist/src/types/registry.d.ts +0 -11
  84. package/dist/src/types/registry.d.ts.map +0 -1
  85. package/dist/src/types/registry.js +0 -0
  86. package/dist/src/types/schema-validator.d.ts +0 -66
  87. package/dist/src/types/schema-validator.d.ts.map +0 -1
  88. package/dist/src/types/schema-validator.js +0 -13
  89. package/dist/src/types/stonecrop.d.ts +0 -32
  90. package/dist/src/types/stonecrop.d.ts.map +0 -1
  91. package/dist/src/types/stonecrop.js +0 -0
  92. package/dist/stonecrop.tsbuildinfo +0 -1
  93. package/src/client-action.ts +0 -207
  94. package/src/client-handler.ts +0 -55
  95. package/src/composables/lazy-link.ts +0 -145
  96. package/src/composables/operation-log.ts +0 -257
  97. package/src/composables/stonecrop.ts +0 -556
  98. package/src/doctype.ts +0 -360
  99. package/src/draft.ts +0 -38
  100. package/src/exceptions.ts +0 -16
  101. package/src/field-triggers.ts +0 -686
  102. package/src/index.ts +0 -81
  103. package/src/plugins/index.ts +0 -113
  104. package/src/registry.ts +0 -433
  105. package/src/schema-validator.ts +0 -476
  106. package/src/stonecrop.ts +0 -721
  107. package/src/stores/hst.ts +0 -626
  108. package/src/stores/index.ts +0 -21
  109. package/src/stores/operation-log.ts +0 -675
  110. package/src/stores/validation.ts +0 -138
  111. package/src/types/client-action.ts +0 -121
  112. package/src/types/composable.ts +0 -288
  113. package/src/types/doctype.ts +0 -35
  114. package/src/types/field-triggers.ts +0 -201
  115. package/src/types/hst.ts +0 -77
  116. package/src/types/index.ts +0 -11
  117. package/src/types/operation-log.ts +0 -202
  118. package/src/types/plugin.ts +0 -38
  119. package/src/types/registry.ts +0 -10
  120. package/src/types/schema-validator.ts +0 -69
  121. package/src/types/stonecrop.ts +0 -33
@@ -1,556 +0,0 @@
1
- import { type ResolvedField } from '@stonecrop/aform'
2
- import { storeToRefs } from 'pinia'
3
- import { inject, onMounted, Ref, ref, watch, provide, computed } from 'vue'
4
-
5
- import Doctype from '../doctype'
6
- import { isDraftRecordId } from '../draft'
7
- import Registry from '../registry'
8
- import { Stonecrop } from '../stonecrop'
9
- import type { HSTNode } from '../types/hst'
10
- import type { BaseStonecropReturn, HSTStonecropReturn, HSTChangeData, OperationLogAPI } from '../types/composable'
11
- import type { HSTOperation, OperationLogConfig } from '../types/operation-log'
12
- import type { RouteContext } from '../types/registry'
13
-
14
- /**
15
- * Unified Stonecrop composable - handles both general operations and HST reactive integration.
16
- *
17
- * Called with no doctype, it returns the Stonecrop instance and the operation log, and touches
18
- * the network not at all. Name a doctype to get the HST surface.
19
- *
20
- * @returns The Stonecrop instance and the operation log
21
- * @public
22
- */
23
- export function useStonecrop(): BaseStonecropReturn
24
- /**
25
- * Unified Stonecrop composable with HST integration for a specific doctype and record.
26
- *
27
- * When a `Doctype` instance is passed, all synchronous initialisation (`hstStore`,
28
- * `resolvedSchema`, `formData`, `handleHSTChange`, operation-log wiring) is performed
29
- * during `setup()` — before the first render and without awaiting any lifecycle hook.
30
- * Callers can read `hstStore.value`, `resolvedSchema.value`, and `formData.value`
31
- * immediately after calling this composable; no `nextTick`, `flushPromises`, or
32
- * `setTimeout` is required.
33
- *
34
- * The only async work in `onMounted` is fetching an existing record from the server when
35
- * `recordId` is not a draft, and lazy-loading a doctype by slug string. Both need a doctype
36
- * named here — nothing is inferred from the route.
37
- *
38
- * @param options - Configuration with doctype (string slug or Doctype instance) and optional recordId
39
- * @returns Stonecrop instance with full HST integration utilities
40
- * @public
41
- */
42
- export function useStonecrop(options: {
43
- registry?: Registry
44
- doctype: Doctype | string
45
- recordId?: string
46
- }): HSTStonecropReturn
47
- /**
48
- * @public
49
- */
50
- export function useStonecrop(options?: {
51
- registry?: Registry
52
- doctype?: Doctype | string
53
- recordId?: string
54
- }): BaseStonecropReturn | HSTStonecropReturn {
55
- if (!options) options = {}
56
-
57
- const registry = options.registry || inject<Registry>('$registry')
58
- const providedStonecrop = inject<Stonecrop>('$stonecrop')
59
- const stonecrop = ref<Stonecrop | undefined>()
60
- const hstStore = ref<HSTNode>()
61
- const formData = ref<Record<string, any>>({})
62
-
63
- // Resolved schema with nested Doctype fields expanded
64
- const resolvedSchema = ref<ResolvedField[]>([])
65
-
66
- // Loading state for lazy-loaded doctypes
67
- const isLoading = ref(false)
68
- const error = ref<Error | null>(null)
69
- const resolvedDoctype = ref<Doctype | undefined>()
70
-
71
- // Workflow readiness computed properties
72
- const isWorkflowReady = computed(() => {
73
- if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || isDraftRecordId(options.recordId)) {
74
- return true
75
- }
76
- const status = stonecrop.value.isWorkflowReady(resolvedDoctype.value, options.recordId)
77
- return status.ready
78
- })
79
-
80
- const blockedLinks = computed(() => {
81
- if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || isDraftRecordId(options.recordId)) {
82
- return []
83
- }
84
- const status = stonecrop.value.isWorkflowReady(resolvedDoctype.value, options.recordId)
85
- return status.blockedLinks ?? []
86
- })
87
-
88
- // Initialize stonecrop instance synchronously using singleton pattern
89
- // Use injected instance if available, otherwise fall back to the singleton root
90
- const stonecropInstance = providedStonecrop || Stonecrop._root
91
- if (stonecropInstance) {
92
- stonecrop.value = stonecropInstance
93
- }
94
-
95
- // If doctype is a Doctype instance (not string), set resolved immediately
96
- if (options?.doctype && typeof options.doctype !== 'string') {
97
- resolvedDoctype.value = options.doctype
98
- }
99
-
100
- // Operation log state and methods
101
- const operations = ref<HSTOperation[]>([])
102
- const currentIndex = ref(-1)
103
- const canUndo = computed(() => stonecrop.value?.getOperationLogStore().canUndo ?? false)
104
- const canRedo = computed(() => stonecrop.value?.getOperationLogStore().canRedo ?? false)
105
- const undoCount = computed(() => stonecrop.value?.getOperationLogStore().undoCount ?? 0)
106
- const redoCount = computed(() => stonecrop.value?.getOperationLogStore().redoCount ?? 0)
107
- const undoRedoState = computed(
108
- () =>
109
- stonecrop.value?.getOperationLogStore().undoRedoState ?? {
110
- canUndo: false,
111
- canRedo: false,
112
- undoCount: 0,
113
- redoCount: 0,
114
- currentIndex: -1,
115
- }
116
- )
117
-
118
- // Operation log methods
119
- const undo = (hstNode: HSTNode): boolean => {
120
- return stonecrop.value?.getOperationLogStore().undo(hstNode) ?? false
121
- }
122
-
123
- const redo = (hstNode: HSTNode): boolean => {
124
- return stonecrop.value?.getOperationLogStore().redo(hstNode) ?? false
125
- }
126
-
127
- const startBatch = () => {
128
- stonecrop.value?.getOperationLogStore().startBatch()
129
- }
130
-
131
- const commitBatch = (description?: string): string | null => {
132
- return stonecrop.value?.getOperationLogStore().commitBatch(description) ?? null
133
- }
134
-
135
- const cancelBatch = () => {
136
- stonecrop.value?.getOperationLogStore().cancelBatch()
137
- }
138
-
139
- const clear = () => {
140
- stonecrop.value?.getOperationLogStore().clear()
141
- }
142
-
143
- const getOperationsFor = (doctype: string, recordId?: string) => {
144
- return stonecrop.value?.getOperationLogStore().getOperationsFor(doctype, recordId) ?? []
145
- }
146
-
147
- const getSnapshot = () => {
148
- return (
149
- stonecrop.value?.getOperationLogStore().getSnapshot() ?? {
150
- operations: [],
151
- currentIndex: -1,
152
- totalOperations: 0,
153
- reversibleOperations: 0,
154
- irreversibleOperations: 0,
155
- }
156
- )
157
- }
158
-
159
- const markIrreversible = (operationId: string, reason: string) => {
160
- stonecrop.value?.getOperationLogStore().markIrreversible(operationId, reason)
161
- }
162
-
163
- const logAction = (
164
- doctype: string,
165
- actionName: string,
166
- recordIds?: string[],
167
- result: 'success' | 'failure' | 'pending' = 'success',
168
- errorMessage?: string
169
- ): string => {
170
- return stonecrop.value?.getOperationLogStore().logAction(doctype, actionName, recordIds, result, errorMessage) ?? ''
171
- }
172
-
173
- const configure = (config: Partial<OperationLogConfig>) => {
174
- stonecrop.value?.getOperationLogStore().configure(config)
175
- }
176
-
177
- // Wire operation log reactive state synchronously — no lifecycle hook needed.
178
- // storeToRefs and watch are both safe to call in setup() body.
179
- if (registry && stonecrop.value) {
180
- try {
181
- const opLogStore = stonecrop.value.getOperationLogStore()
182
- const opLogRefs = storeToRefs(opLogStore)
183
- operations.value = opLogRefs.operations.value
184
- currentIndex.value = opLogRefs.currentIndex.value
185
-
186
- // Watch for changes in operation log state
187
- watch(
188
- () => opLogRefs.operations.value,
189
- newOps => {
190
- operations.value = newOps
191
- }
192
- )
193
- watch(
194
- () => opLogRefs.currentIndex.value,
195
- newIndex => {
196
- currentIndex.value = newIndex
197
- }
198
- )
199
- } catch {
200
- // Pinia not available — operation log is optional, silently skip
201
- }
202
- }
203
-
204
- // Synchronous HST initialisation for an explicit Doctype instance.
205
- // When the caller passes a Doctype object (not a slug string), every piece of
206
- // setup that doesn't require network I/O runs here during setup() so that
207
- // hstStore, resolvedSchema, and formData are populated before the first render
208
- // and are immediately available to callers without any await.
209
- if (options.doctype && typeof options.doctype !== 'string' && registry && stonecrop.value) {
210
- hstStore.value = stonecrop.value.getStore()
211
- resolvedSchema.value = registry.resolveSchema(options.doctype)
212
- if (!options.recordId || isDraftRecordId(options.recordId)) {
213
- formData.value = registry.initializeRecord(resolvedSchema.value)
214
- }
215
- if (hstStore.value) {
216
- setupDeepReactivity(options.doctype, options.recordId || 'new', formData, hstStore.value)
217
- }
218
- }
219
-
220
- // onMounted handles only work that is genuinely async: lazy-loading a doctype by slug and
221
- // fetching an existing record from the server. Both require the caller to name a doctype.
222
- //
223
- // Rejected: inferring the doctype from the route when the caller names none. Every output of
224
- // that inference landed in refs the no-argument callers never read — all of them wanted the
225
- // instance, which is resolved synchronously above — so each paid a getMeta plus a getRecord to
226
- // read a value already in hand. It also lowercased the route's record id, which silently missed
227
- // any natural key carrying uppercase.
228
- onMounted(async () => {
229
- if (!registry || !stonecrop.value) {
230
- return
231
- }
232
-
233
- // Handle HST integration if doctype is provided explicitly
234
- if (options.doctype) {
235
- const recordId = options.recordId
236
-
237
- if (typeof options.doctype === 'string') {
238
- // String doctype — resolve lazily, then do full sync-equivalent setup here.
239
- const doctypeSlug = options.doctype
240
- hstStore.value = stonecrop.value.getStore()
241
- isLoading.value = true
242
- error.value = null
243
-
244
- let doctype: Doctype | undefined
245
- try {
246
- // Check if already in registry
247
- doctype = registry.getDoctype(doctypeSlug)
248
-
249
- if (!doctype && registry.getMeta) {
250
- // Lazy-load via getMeta
251
- const routeContext: RouteContext = {
252
- path: `/${doctypeSlug}`,
253
- segments: [doctypeSlug],
254
- }
255
- doctype = await registry.getMeta(routeContext)
256
- if (doctype) {
257
- registry.addDoctype(doctype)
258
- }
259
- }
260
-
261
- if (!doctype) {
262
- error.value = new Error(`Doctype '${doctypeSlug}' not found in registry and getMeta returned no result`)
263
- }
264
- } catch (e) {
265
- error.value = e instanceof Error ? e : new Error(String(e))
266
- } finally {
267
- isLoading.value = false
268
- }
269
-
270
- resolvedDoctype.value = doctype
271
- if (!doctype) return
272
-
273
- resolvedSchema.value = registry.resolveSchema(doctype)
274
-
275
- if (recordId && !isDraftRecordId(recordId)) {
276
- const existingRecord = stonecrop.value.getRecordById(doctype, recordId)
277
- if (existingRecord) {
278
- formData.value = existingRecord.get('') || {}
279
- } else {
280
- try {
281
- await stonecrop.value.getRecord(doctype, recordId)
282
- const loadedRecord = stonecrop.value.getRecordById(doctype, recordId)
283
- if (loadedRecord) {
284
- formData.value = loadedRecord.get('') || {}
285
- }
286
- } catch {
287
- formData.value = registry.initializeRecord(resolvedSchema.value)
288
- }
289
- }
290
- } else {
291
- formData.value = registry.initializeRecord(resolvedSchema.value)
292
- }
293
-
294
- if (hstStore.value) {
295
- setupDeepReactivity(doctype, recordId || 'new', formData, hstStore.value)
296
- }
297
- } else {
298
- // Doctype instance — sync init was done during setup().
299
- // Only handle the async path: fetching an existing record from the server.
300
- if (recordId && !isDraftRecordId(recordId)) {
301
- const doctype = options.doctype
302
- const existingRecord = stonecrop.value.getRecordById(doctype, recordId)
303
- if (existingRecord) {
304
- formData.value = existingRecord.get('') || {}
305
- } else {
306
- try {
307
- await stonecrop.value.getRecord(doctype, recordId)
308
- const loadedRecord = stonecrop.value.getRecordById(doctype, recordId)
309
- if (loadedRecord) {
310
- formData.value = loadedRecord.get('') || {}
311
- }
312
- } catch {
313
- formData.value = registry.initializeRecord(resolvedSchema.value)
314
- }
315
- }
316
- }
317
- }
318
- }
319
- })
320
-
321
- // HST integration functions - always created but only populated when HST is available
322
- const provideHSTPath = (fieldname: string, customRecordId?: string): string => {
323
- const doctype = resolvedDoctype.value
324
- if (!doctype) return ''
325
-
326
- const actualRecordId = customRecordId || options.recordId || 'new'
327
- return `${doctype.slug}.${actualRecordId}.${fieldname}`
328
- }
329
-
330
- const handleHSTChange = (changeData: HSTChangeData): void => {
331
- const doctype = resolvedDoctype.value
332
- if (!hstStore.value || !stonecrop.value || !doctype) {
333
- return
334
- }
335
-
336
- try {
337
- const pathParts = changeData.path.split('.')
338
- if (pathParts.length >= 2) {
339
- const doctypeSlug = pathParts[0]
340
- const recordId = pathParts[1]
341
-
342
- if (!hstStore.value.has(`${doctypeSlug}.${recordId}`)) {
343
- stonecrop.value.addRecord(doctype, recordId, { ...formData.value })
344
- }
345
-
346
- if (pathParts.length > 3) {
347
- const recordPath = `${doctypeSlug}.${recordId}`
348
- const nestedParts = pathParts.slice(2)
349
-
350
- let currentPath = recordPath
351
- for (let i = 0; i < nestedParts.length - 1; i++) {
352
- currentPath += `.${nestedParts[i]}`
353
-
354
- if (!hstStore.value.has(currentPath)) {
355
- const nextPart = nestedParts[i + 1]
356
- const isArray = !isNaN(Number(nextPart))
357
- hstStore.value.set(currentPath, isArray ? [] : {})
358
- }
359
- }
360
- }
361
- }
362
-
363
- hstStore.value.set(changeData.path, changeData.value)
364
-
365
- const fieldParts = changeData.fieldname.split('.')
366
- const newFormData = { ...formData.value }
367
-
368
- if (fieldParts.length === 1) {
369
- newFormData[fieldParts[0]] = changeData.value
370
- } else {
371
- updateNestedObject(newFormData, fieldParts, changeData.value)
372
- }
373
-
374
- formData.value = newFormData
375
- } catch {
376
- // Silently handle errors
377
- }
378
- }
379
-
380
- // Provide injection tokens if HST will be available
381
- if (options.doctype) {
382
- provide('hstPathProvider', provideHSTPath)
383
- provide('hstChangeHandler', handleHSTChange)
384
- }
385
-
386
- /**
387
- * Scaffold empty descendant records from defaults for all descendant links.
388
- * Delegates to Stonecrop.initializeNestedData method.
389
- * @param path - The HST path where initialized data should be stored
390
- * @param doctype - The doctype to initialize
391
- */
392
- const initializeNestedData = (path: string, doctype: Doctype): void => {
393
- if (!stonecrop.value) {
394
- throw new Error('Stonecrop instance not available')
395
- }
396
- return stonecrop.value.initializeNestedData(path, doctype)
397
- }
398
-
399
- /**
400
- * Fetch a record and its nested data from the server.
401
- * Delegates to Stonecrop.fetchNestedData method.
402
- * @param path - The HST path (e.g., "recipe.r1")
403
- * @param doctype - The doctype to fetch
404
- * @param recordId - Record ID to fetch
405
- * @param options - Query options (includeNested to control which links are fetched)
406
- */
407
- const fetchNestedData = async (
408
- path: string,
409
- doctype: Doctype,
410
- recordId: string,
411
- fetchOptions?: { includeNested?: boolean | string[] }
412
- ): Promise<void> => {
413
- if (!stonecrop.value) {
414
- throw new Error('Stonecrop instance not available')
415
- }
416
- return stonecrop.value.fetchNestedData(path, doctype, recordId, fetchOptions)
417
- }
418
-
419
- /**
420
- * Collect a record payload with all nested doctype fields from HST
421
- * Delegates to Stonecrop.collectRecordPayload method
422
- * @param doctype - The doctype metadata
423
- * @param recordId - The record ID to collect
424
- * @returns The complete record payload ready for API submission
425
- */
426
- const collectRecordPayload = (doctype: Doctype, recordId: string): Record<string, any> => {
427
- if (!stonecrop.value) {
428
- throw new Error('Stonecrop instance not available')
429
- }
430
- return stonecrop.value.collectRecordPayload(doctype, recordId)
431
- }
432
-
433
- /**
434
- * Create a nested context for descendant forms
435
- * @param basePath - The base path for the nested context (e.g., "customer.123.address")
436
- * @param _descendantDoctype - The descendant doctype metadata (unused but kept for API consistency)
437
- * @returns Object with scoped provideHSTPath and handleHSTChange
438
- */
439
- const createNestedContext = (basePath: string, _descendantDoctype: Doctype) => {
440
- const nestedProvideHSTPath = (fieldname: string): string => {
441
- return `${basePath}.${fieldname}`
442
- }
443
-
444
- const nestedHandleHSTChange = (changeData: HSTChangeData): void => {
445
- // Update the path to be relative to the nested base path
446
- const nestedPath = changeData.path.startsWith(basePath) ? changeData.path : `${basePath}.${changeData.fieldname}`
447
-
448
- handleHSTChange({
449
- ...changeData,
450
- path: nestedPath,
451
- })
452
- }
453
-
454
- return {
455
- provideHSTPath: nestedProvideHSTPath,
456
- handleHSTChange: nestedHandleHSTChange,
457
- }
458
- }
459
-
460
- // Create operation log API object
461
- const operationLog: OperationLogAPI = {
462
- operations,
463
- currentIndex,
464
- undoRedoState,
465
- canUndo,
466
- canRedo,
467
- undoCount,
468
- redoCount,
469
- undo,
470
- redo,
471
- startBatch,
472
- commitBatch,
473
- cancelBatch,
474
- clear,
475
- getOperationsFor,
476
- getSnapshot,
477
- markIrreversible,
478
- logAction,
479
- configure,
480
- }
481
- // Always return HST functions if doctype is provided or will be loaded from router
482
- if (options.doctype) {
483
- // Explicit doctype - return HST immediately
484
- return {
485
- stonecrop,
486
- operationLog,
487
- provideHSTPath,
488
- handleHSTChange,
489
- hstStore,
490
- formData,
491
- resolvedSchema,
492
- initializeNestedData,
493
- fetchNestedData,
494
- collectRecordPayload,
495
- createNestedContext,
496
- isLoading,
497
- error,
498
- resolvedDoctype,
499
- isWorkflowReady,
500
- blockedLinks,
501
- } satisfies HSTStonecropReturn
502
- }
503
-
504
- // No doctype named: the instance and the operation log are everything this can offer.
505
- return {
506
- stonecrop,
507
- operationLog,
508
- }
509
- }
510
-
511
- /**
512
- * Setup deep reactivity between form data and HST store
513
- */
514
- function setupDeepReactivity(
515
- doctype: Doctype,
516
- recordId: string,
517
- formData: Ref<Record<string, any>>,
518
- hstStore: HSTNode
519
- ): void {
520
- watch(
521
- formData,
522
- newData => {
523
- const recordPath = `${doctype.slug}.${recordId}`
524
-
525
- Object.keys(newData).forEach(fieldname => {
526
- const path = `${recordPath}.${fieldname}`
527
- try {
528
- hstStore.set(path, newData[fieldname])
529
- } catch {
530
- // Silently handle errors
531
- }
532
- })
533
- },
534
- { deep: true }
535
- )
536
- }
537
-
538
- /**
539
- * Update nested object with dot-notation path
540
- */
541
- function updateNestedObject(obj: any, path: string[], value: any): void {
542
- let current: Record<string, any> = obj
543
-
544
- for (let i = 0; i < path.length - 1; i++) {
545
- const key = path[i]
546
-
547
- if (!(key in current) || typeof current[key] !== 'object') {
548
- current[key] = isNaN(Number(path[i + 1])) ? {} : []
549
- }
550
-
551
- current = current[key]
552
- }
553
-
554
- const finalKey = path[path.length - 1]
555
- current[finalKey] = value
556
- }