@asteby/metacore-runtime-react 53.10.2 → 53.12.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 (44) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/dist/bulk-image-dialog.d.ts +31 -0
  3. package/dist/bulk-image-dialog.d.ts.map +1 -0
  4. package/dist/bulk-image-dialog.js +339 -0
  5. package/dist/bulk-image-import.d.ts +158 -0
  6. package/dist/bulk-image-import.d.ts.map +1 -0
  7. package/dist/bulk-image-import.js +310 -0
  8. package/dist/dynamic-crud-page.d.ts.map +1 -1
  9. package/dist/dynamic-crud-page.js +2 -1
  10. package/dist/dynamic-form.d.ts +10 -1
  11. package/dist/dynamic-form.d.ts.map +1 -1
  12. package/dist/dynamic-form.js +6 -6
  13. package/dist/dynamic-relation-helpers.d.ts +65 -0
  14. package/dist/dynamic-relation-helpers.d.ts.map +1 -1
  15. package/dist/dynamic-relation-helpers.js +178 -2
  16. package/dist/dynamic-relation.d.ts +16 -0
  17. package/dist/dynamic-relation.d.ts.map +1 -1
  18. package/dist/dynamic-relation.js +277 -13
  19. package/dist/dynamic-relations.d.ts.map +1 -1
  20. package/dist/dynamic-relations.js +1 -1
  21. package/dist/index.d.ts +2 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +2 -0
  24. package/dist/relation-gallery.d.ts +31 -0
  25. package/dist/relation-gallery.d.ts.map +1 -0
  26. package/dist/relation-gallery.js +154 -0
  27. package/dist/upload-field.d.ts +19 -1
  28. package/dist/upload-field.d.ts.map +1 -1
  29. package/dist/upload-field.js +50 -3
  30. package/package.json +2 -1
  31. package/src/__tests__/bulk-image-dialog.test.tsx +127 -0
  32. package/src/__tests__/bulk-image-import.test.ts +129 -0
  33. package/src/__tests__/relation-gallery-helpers.test.ts +154 -0
  34. package/src/__tests__/relation-gallery.test.tsx +229 -0
  35. package/src/bulk-image-dialog.tsx +594 -0
  36. package/src/bulk-image-import.ts +456 -0
  37. package/src/dynamic-crud-page.tsx +2 -0
  38. package/src/dynamic-form.tsx +20 -2
  39. package/src/dynamic-relation-helpers.ts +210 -2
  40. package/src/dynamic-relation.tsx +329 -15
  41. package/src/dynamic-relations.tsx +1 -0
  42. package/src/index.ts +12 -0
  43. package/src/relation-gallery.tsx +489 -0
  44. package/src/upload-field.tsx +60 -2
@@ -32,8 +32,206 @@ const MANAGED_RELATION_COLUMNS = new Set([
32
32
  'created_by_id',
33
33
  'updated_by',
34
34
  'updated_by_id',
35
+ 'deleted_by',
36
+ 'deleted_by_id',
37
+ 'organization_id',
35
38
  ])
36
39
 
40
+ // isManagedRelationColumn: an audit/server column, including the dotted
41
+ // projections the backend serves for them (`created_by.avatar`, the creator
42
+ // avatar column every table gets). Any dotted key is a projection of another
43
+ // column, never an editable value of the row.
44
+ export function isManagedRelationColumn(key: string): boolean {
45
+ const k = key.toLowerCase()
46
+ if (k.includes('.')) return true
47
+ return MANAGED_RELATION_COLUMNS.has(k)
48
+ }
49
+
50
+ const MEDIA_WIDGETS = new Set(['image', 'upload', 'photo', 'file', 'media'])
51
+ const IMAGE_CELL_STYLES = new Set(['image', 'image_stack'])
52
+
53
+ // relationMediaKind: 'image' / 'file' when a child column holds an uploaded
54
+ // file path (manifest `widget: image|upload|file`, `type: image`, or a ref-less
55
+ // column the backend renders as an image — `/metadata/table` carries the
56
+ // manifest `display: image_stack` as `cellStyle`, not the widget). A `ref`
57
+ // column is an FK picker (brand logo chip), never an upload.
58
+ export function relationMediaKind(col: ColumnDefinition): 'image' | 'file' | undefined {
59
+ if (col.ref) return undefined
60
+ const widget = String((col as ColumnDefinition & { widget?: string }).widget ?? '').toLowerCase()
61
+ if (widget === 'file') return 'file'
62
+ if (MEDIA_WIDGETS.has(widget)) return 'image'
63
+ if (col.type === 'image') return 'image'
64
+ if (IMAGE_CELL_STYLES.has(String(col.cellStyle ?? '').toLowerCase())) return 'image'
65
+ return undefined
66
+ }
67
+
68
+ export type RelationDerive = 'image_width' | 'image_height' | 'next_position'
69
+
70
+ /**
71
+ * Declarative form hints a child column carries in its manifest
72
+ * `display_config` (served as `styleConfig`, no kernel field needed):
73
+ * - `form: "hidden"` → never an input in the row form.
74
+ * - `derive: <RelationDerive>` → not an input; filled by the SDK (image
75
+ * size measured in the browser on upload, next gallery position on create).
76
+ * - `default_from_parent: <k>` → create-form initial value from the parent
77
+ * record's `<k>` (e.g. the product name as the image alt text), editable.
78
+ * - `empty_label: <text>` → placeholder of an optional picker
79
+ * ("Para todas las variantes").
80
+ * - `gallery: true` (on the image column) → the relation renders as a
81
+ * thumbnail gallery instead of a table.
82
+ * A column with none of them behaves exactly as before.
83
+ */
84
+ export interface RelationColumnUi {
85
+ form?: 'hidden' | 'advanced'
86
+ derive?: RelationDerive
87
+ defaultFromParent?: string
88
+ emptyLabel?: string
89
+ gallery?: boolean
90
+ }
91
+
92
+ const DERIVES = new Set<RelationDerive>(['image_width', 'image_height', 'next_position'])
93
+
94
+ export function relationColumnUi(col: ColumnDefinition): RelationColumnUi {
95
+ const sc = (col.styleConfig ?? {}) as Record<string, unknown>
96
+ const out: RelationColumnUi = {}
97
+ const form = sc.form
98
+ if (form === 'hidden' || form === 'advanced') out.form = form
99
+ const derive = sc.derive
100
+ if (typeof derive === 'string' && DERIVES.has(derive as RelationDerive)) out.derive = derive as RelationDerive
101
+ const dfp = sc.default_from_parent ?? sc.defaultFromParent
102
+ if (typeof dfp === 'string' && dfp.trim() !== '') out.defaultFromParent = dfp.trim()
103
+ const empty = sc.empty_label ?? sc.emptyLabel
104
+ if (typeof empty === 'string' && empty.trim() !== '') out.emptyLabel = empty
105
+ if (sc.gallery === true) out.gallery = true
106
+ return out
107
+ }
108
+
109
+ /** The gallery image column (media + `display_config.gallery: true`), if any. */
110
+ export function relationGalleryColumn(
111
+ metadata: Pick<TableMetadata, 'columns'> | null | undefined,
112
+ ): ColumnDefinition | undefined {
113
+ return metadata?.columns?.find((c) => relationMediaKind(c) === 'image' && relationColumnUi(c).gallery === true)
114
+ }
115
+
116
+ export interface ImageMeta {
117
+ width?: number
118
+ height?: number
119
+ }
120
+
121
+ /**
122
+ * Values the SDK fills for derived columns of a child row. `next_position`
123
+ * only on create (an edit keeps the stored order); image size only when the
124
+ * upload measured one. Never invents a value it does not know.
125
+ */
126
+ export function relationDerivedValues(
127
+ metadata: Pick<TableMetadata, 'columns'> | null | undefined,
128
+ ctx: { isCreate: boolean; rows: Record<string, any>[]; image?: ImageMeta | null },
129
+ ): Record<string, number> {
130
+ const out: Record<string, number> = {}
131
+ for (const col of metadata?.columns ?? []) {
132
+ const d = relationColumnUi(col).derive
133
+ if (d === 'next_position' && ctx.isCreate) out[col.key] = nextRelationPosition(ctx.rows, col.key)
134
+ if (d === 'image_width' && ctx.image?.width) out[col.key] = Math.round(ctx.image.width)
135
+ if (d === 'image_height' && ctx.image?.height) out[col.key] = Math.round(ctx.image.height)
136
+ }
137
+ return out
138
+ }
139
+
140
+ /** Create-form initial values copied from the parent (`default_from_parent`). */
141
+ export function relationInitialValues(
142
+ metadata: Pick<TableMetadata, 'columns'> | null | undefined,
143
+ parent: Record<string, unknown> | null | undefined,
144
+ ): Record<string, any> {
145
+ const out: Record<string, any> = {}
146
+ if (!parent) return out
147
+ for (const col of metadata?.columns ?? []) {
148
+ const k = relationColumnUi(col).defaultFromParent
149
+ if (!k) continue
150
+ const v = parent[k]
151
+ if (typeof v === 'string' && v !== '') out[col.key] = v
152
+ else if (typeof v === 'number') out[col.key] = String(v)
153
+ }
154
+ return out
155
+ }
156
+
157
+ /** Key of the child model's ordering column (`position`), if it declares one. */
158
+ export function relationPositionKey(
159
+ metadata: Pick<TableMetadata, 'columns'> | null | undefined,
160
+ ): string | undefined {
161
+ return metadata?.columns?.some((c) => c.key === 'position') ? 'position' : undefined
162
+ }
163
+
164
+ /** True when the list endpoint can sort by the position column (declared sortable). */
165
+ export function relationPositionSortable(
166
+ metadata: Pick<TableMetadata, 'columns'> | null | undefined,
167
+ ): boolean {
168
+ return metadata?.columns?.some((c) => c.key === 'position' && c.sortable === true) ?? false
169
+ }
170
+
171
+ /** Rows sorted ascending by their position column; rows without one go last, stable. */
172
+ export function sortRelationRows<T extends Record<string, any>>(rows: T[], positionKey?: string): T[] {
173
+ if (!positionKey) return rows
174
+ const num = (r: T) => {
175
+ const n = Number(r?.[positionKey])
176
+ return r?.[positionKey] === null || r?.[positionKey] === undefined || r?.[positionKey] === '' || !Number.isFinite(n)
177
+ ? Number.POSITIVE_INFINITY
178
+ : n
179
+ }
180
+ return rows
181
+ .map((r, i) => [r, i] as const)
182
+ .sort((a, b) => num(a[0]) - num(b[0]) || a[1] - b[1])
183
+ .map(([r]) => r)
184
+ }
185
+
186
+ /** Default position for a new child row: one past the highest loaded, 0 when empty. */
187
+ export function nextRelationPosition(rows: Record<string, any>[], positionKey = 'position'): number {
188
+ let max = -1
189
+ for (const r of rows) {
190
+ const n = Number(r?.[positionKey])
191
+ if (r?.[positionKey] !== null && r?.[positionKey] !== '' && Number.isFinite(n) && n > max) max = n
192
+ }
193
+ return max + 1
194
+ }
195
+
196
+ /**
197
+ * Renumbers a gallery contiguously (0..n-1). Order: current `position`
198
+ * ascending (empty/invalid last); ties — the duplicated positions an older
199
+ * client could leave — go oldest first (`created_at`, then `id`), so a
200
+ * product's original photo stays ahead of the ones uploaded later.
201
+ * `updates` holds only the rows whose stored position differs.
202
+ */
203
+ export function contiguousPositionUpdates(
204
+ rows: Record<string, any>[],
205
+ positionKey = 'position',
206
+ ): { ordered: Record<string, any>[]; updates: Array<{ id: string | number; position: number }> } {
207
+ const pos = (r: Record<string, any>) => {
208
+ const raw = r?.[positionKey]
209
+ const n = Number(raw)
210
+ return raw === null || raw === undefined || raw === '' || !Number.isFinite(n) ? Infinity : n
211
+ }
212
+ const ordered = rows
213
+ .map((r, i) => ({ r, i }))
214
+ .sort((a, b) => {
215
+ const pa = pos(a.r)
216
+ const pb = pos(b.r)
217
+ if (pa !== pb) return pa < pb ? -1 : 1
218
+ const ca = String(a.r?.created_at ?? '')
219
+ const cb = String(b.r?.created_at ?? '')
220
+ if (ca && cb && ca !== cb) return ca < cb ? -1 : 1
221
+ // Last resort: the id, so two tabs (or two runs) agree on the order.
222
+ const ia = String(a.r?.id ?? '')
223
+ const ib = String(b.r?.id ?? '')
224
+ if (ia !== ib) return ia < ib ? -1 : 1
225
+ return a.i - b.i
226
+ })
227
+ .map((x) => x.r)
228
+ const updates: Array<{ id: string | number; position: number }> = []
229
+ ordered.forEach((r, i) => {
230
+ if (pos(r) !== i && r?.id !== undefined && r?.id !== null) updates.push({ id: r.id, position: i })
231
+ })
232
+ return { ordered, updates }
233
+ }
234
+
37
235
  // Pulls a human label off a resolved relation/user object a backend serves:
38
236
  // `{ value, label }` (FK sibling), `{ name, … }` (user object such as
39
237
  // created_by) or `{ title }`. Returns undefined for plain/empty objects so the
@@ -163,11 +361,21 @@ export function deriveRelationFormFields(
163
361
  // Managed/audit columns are server-owned and ship resolved objects
164
362
  // (`created_by = { name, avatar, … }`); making them editable inputs
165
363
  // renders `[object Object]`. Never surface them in the inline form.
166
- if (MANAGED_RELATION_COLUMNS.has(col.key.toLowerCase())) continue
364
+ if (isManagedRelationColumn(col.key)) continue
365
+ // Derived / hidden columns are filled by the SDK or the server, never
366
+ // typed by whoever uploads a photo (width, height, position…).
367
+ const ui = relationColumnUi(col)
368
+ if (ui.form || ui.derive) continue
369
+ const media = relationMediaKind(col)
370
+ const accept = (col as ColumnDefinition & { accept?: string }).accept
167
371
  out.push({
168
372
  key: col.key,
169
373
  label: col.label,
170
- type: columnTypeToFieldType(col),
374
+ // A file/image column gets the same upload widget (with preview) as
375
+ // the parent form, not a free-text path input.
376
+ type: media ?? columnTypeToFieldType(col),
377
+ ...(media === 'image' ? { accept: accept || 'image/*' } : accept ? { accept } : {}),
378
+ ...(ui.emptyLabel ? { placeholder: ui.emptyLabel } : {}),
171
379
  required: false,
172
380
  options: col.options?.map(o => ({ value: String(o.value), label: o.label })),
173
381
  // Carry the FK target through so a belongs_to column renders the