@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.
- package/CHANGELOG.md +42 -0
- package/dist/bulk-image-dialog.d.ts +31 -0
- package/dist/bulk-image-dialog.d.ts.map +1 -0
- package/dist/bulk-image-dialog.js +339 -0
- package/dist/bulk-image-import.d.ts +158 -0
- package/dist/bulk-image-import.d.ts.map +1 -0
- package/dist/bulk-image-import.js +310 -0
- package/dist/dynamic-crud-page.d.ts.map +1 -1
- package/dist/dynamic-crud-page.js +2 -1
- package/dist/dynamic-form.d.ts +10 -1
- package/dist/dynamic-form.d.ts.map +1 -1
- package/dist/dynamic-form.js +6 -6
- package/dist/dynamic-relation-helpers.d.ts +65 -0
- package/dist/dynamic-relation-helpers.d.ts.map +1 -1
- package/dist/dynamic-relation-helpers.js +178 -2
- package/dist/dynamic-relation.d.ts +16 -0
- package/dist/dynamic-relation.d.ts.map +1 -1
- package/dist/dynamic-relation.js +277 -13
- package/dist/dynamic-relations.d.ts.map +1 -1
- package/dist/dynamic-relations.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/relation-gallery.d.ts +31 -0
- package/dist/relation-gallery.d.ts.map +1 -0
- package/dist/relation-gallery.js +154 -0
- package/dist/upload-field.d.ts +19 -1
- package/dist/upload-field.d.ts.map +1 -1
- package/dist/upload-field.js +50 -3
- package/package.json +2 -1
- package/src/__tests__/bulk-image-dialog.test.tsx +127 -0
- package/src/__tests__/bulk-image-import.test.ts +129 -0
- package/src/__tests__/relation-gallery-helpers.test.ts +154 -0
- package/src/__tests__/relation-gallery.test.tsx +229 -0
- package/src/bulk-image-dialog.tsx +594 -0
- package/src/bulk-image-import.ts +456 -0
- package/src/dynamic-crud-page.tsx +2 -0
- package/src/dynamic-form.tsx +20 -2
- package/src/dynamic-relation-helpers.ts +210 -2
- package/src/dynamic-relation.tsx +329 -15
- package/src/dynamic-relations.tsx +1 -0
- package/src/index.ts +12 -0
- package/src/relation-gallery.tsx +489 -0
- package/src/upload-field.tsx +60 -2
|
@@ -22,7 +22,172 @@ const MANAGED_RELATION_COLUMNS = new Set([
|
|
|
22
22
|
'created_by_id',
|
|
23
23
|
'updated_by',
|
|
24
24
|
'updated_by_id',
|
|
25
|
+
'deleted_by',
|
|
26
|
+
'deleted_by_id',
|
|
27
|
+
'organization_id',
|
|
25
28
|
]);
|
|
29
|
+
// isManagedRelationColumn: an audit/server column, including the dotted
|
|
30
|
+
// projections the backend serves for them (`created_by.avatar`, the creator
|
|
31
|
+
// avatar column every table gets). Any dotted key is a projection of another
|
|
32
|
+
// column, never an editable value of the row.
|
|
33
|
+
export function isManagedRelationColumn(key) {
|
|
34
|
+
const k = key.toLowerCase();
|
|
35
|
+
if (k.includes('.'))
|
|
36
|
+
return true;
|
|
37
|
+
return MANAGED_RELATION_COLUMNS.has(k);
|
|
38
|
+
}
|
|
39
|
+
const MEDIA_WIDGETS = new Set(['image', 'upload', 'photo', 'file', 'media']);
|
|
40
|
+
const IMAGE_CELL_STYLES = new Set(['image', 'image_stack']);
|
|
41
|
+
// relationMediaKind: 'image' / 'file' when a child column holds an uploaded
|
|
42
|
+
// file path (manifest `widget: image|upload|file`, `type: image`, or a ref-less
|
|
43
|
+
// column the backend renders as an image — `/metadata/table` carries the
|
|
44
|
+
// manifest `display: image_stack` as `cellStyle`, not the widget). A `ref`
|
|
45
|
+
// column is an FK picker (brand logo chip), never an upload.
|
|
46
|
+
export function relationMediaKind(col) {
|
|
47
|
+
if (col.ref)
|
|
48
|
+
return undefined;
|
|
49
|
+
const widget = String(col.widget ?? '').toLowerCase();
|
|
50
|
+
if (widget === 'file')
|
|
51
|
+
return 'file';
|
|
52
|
+
if (MEDIA_WIDGETS.has(widget))
|
|
53
|
+
return 'image';
|
|
54
|
+
if (col.type === 'image')
|
|
55
|
+
return 'image';
|
|
56
|
+
if (IMAGE_CELL_STYLES.has(String(col.cellStyle ?? '').toLowerCase()))
|
|
57
|
+
return 'image';
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
const DERIVES = new Set(['image_width', 'image_height', 'next_position']);
|
|
61
|
+
export function relationColumnUi(col) {
|
|
62
|
+
const sc = (col.styleConfig ?? {});
|
|
63
|
+
const out = {};
|
|
64
|
+
const form = sc.form;
|
|
65
|
+
if (form === 'hidden' || form === 'advanced')
|
|
66
|
+
out.form = form;
|
|
67
|
+
const derive = sc.derive;
|
|
68
|
+
if (typeof derive === 'string' && DERIVES.has(derive))
|
|
69
|
+
out.derive = derive;
|
|
70
|
+
const dfp = sc.default_from_parent ?? sc.defaultFromParent;
|
|
71
|
+
if (typeof dfp === 'string' && dfp.trim() !== '')
|
|
72
|
+
out.defaultFromParent = dfp.trim();
|
|
73
|
+
const empty = sc.empty_label ?? sc.emptyLabel;
|
|
74
|
+
if (typeof empty === 'string' && empty.trim() !== '')
|
|
75
|
+
out.emptyLabel = empty;
|
|
76
|
+
if (sc.gallery === true)
|
|
77
|
+
out.gallery = true;
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
/** The gallery image column (media + `display_config.gallery: true`), if any. */
|
|
81
|
+
export function relationGalleryColumn(metadata) {
|
|
82
|
+
return metadata?.columns?.find((c) => relationMediaKind(c) === 'image' && relationColumnUi(c).gallery === true);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Values the SDK fills for derived columns of a child row. `next_position`
|
|
86
|
+
* only on create (an edit keeps the stored order); image size only when the
|
|
87
|
+
* upload measured one. Never invents a value it does not know.
|
|
88
|
+
*/
|
|
89
|
+
export function relationDerivedValues(metadata, ctx) {
|
|
90
|
+
const out = {};
|
|
91
|
+
for (const col of metadata?.columns ?? []) {
|
|
92
|
+
const d = relationColumnUi(col).derive;
|
|
93
|
+
if (d === 'next_position' && ctx.isCreate)
|
|
94
|
+
out[col.key] = nextRelationPosition(ctx.rows, col.key);
|
|
95
|
+
if (d === 'image_width' && ctx.image?.width)
|
|
96
|
+
out[col.key] = Math.round(ctx.image.width);
|
|
97
|
+
if (d === 'image_height' && ctx.image?.height)
|
|
98
|
+
out[col.key] = Math.round(ctx.image.height);
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
/** Create-form initial values copied from the parent (`default_from_parent`). */
|
|
103
|
+
export function relationInitialValues(metadata, parent) {
|
|
104
|
+
const out = {};
|
|
105
|
+
if (!parent)
|
|
106
|
+
return out;
|
|
107
|
+
for (const col of metadata?.columns ?? []) {
|
|
108
|
+
const k = relationColumnUi(col).defaultFromParent;
|
|
109
|
+
if (!k)
|
|
110
|
+
continue;
|
|
111
|
+
const v = parent[k];
|
|
112
|
+
if (typeof v === 'string' && v !== '')
|
|
113
|
+
out[col.key] = v;
|
|
114
|
+
else if (typeof v === 'number')
|
|
115
|
+
out[col.key] = String(v);
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
/** Key of the child model's ordering column (`position`), if it declares one. */
|
|
120
|
+
export function relationPositionKey(metadata) {
|
|
121
|
+
return metadata?.columns?.some((c) => c.key === 'position') ? 'position' : undefined;
|
|
122
|
+
}
|
|
123
|
+
/** True when the list endpoint can sort by the position column (declared sortable). */
|
|
124
|
+
export function relationPositionSortable(metadata) {
|
|
125
|
+
return metadata?.columns?.some((c) => c.key === 'position' && c.sortable === true) ?? false;
|
|
126
|
+
}
|
|
127
|
+
/** Rows sorted ascending by their position column; rows without one go last, stable. */
|
|
128
|
+
export function sortRelationRows(rows, positionKey) {
|
|
129
|
+
if (!positionKey)
|
|
130
|
+
return rows;
|
|
131
|
+
const num = (r) => {
|
|
132
|
+
const n = Number(r?.[positionKey]);
|
|
133
|
+
return r?.[positionKey] === null || r?.[positionKey] === undefined || r?.[positionKey] === '' || !Number.isFinite(n)
|
|
134
|
+
? Number.POSITIVE_INFINITY
|
|
135
|
+
: n;
|
|
136
|
+
};
|
|
137
|
+
return rows
|
|
138
|
+
.map((r, i) => [r, i])
|
|
139
|
+
.sort((a, b) => num(a[0]) - num(b[0]) || a[1] - b[1])
|
|
140
|
+
.map(([r]) => r);
|
|
141
|
+
}
|
|
142
|
+
/** Default position for a new child row: one past the highest loaded, 0 when empty. */
|
|
143
|
+
export function nextRelationPosition(rows, positionKey = 'position') {
|
|
144
|
+
let max = -1;
|
|
145
|
+
for (const r of rows) {
|
|
146
|
+
const n = Number(r?.[positionKey]);
|
|
147
|
+
if (r?.[positionKey] !== null && r?.[positionKey] !== '' && Number.isFinite(n) && n > max)
|
|
148
|
+
max = n;
|
|
149
|
+
}
|
|
150
|
+
return max + 1;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Renumbers a gallery contiguously (0..n-1). Order: current `position`
|
|
154
|
+
* ascending (empty/invalid last); ties — the duplicated positions an older
|
|
155
|
+
* client could leave — go oldest first (`created_at`, then `id`), so a
|
|
156
|
+
* product's original photo stays ahead of the ones uploaded later.
|
|
157
|
+
* `updates` holds only the rows whose stored position differs.
|
|
158
|
+
*/
|
|
159
|
+
export function contiguousPositionUpdates(rows, positionKey = 'position') {
|
|
160
|
+
const pos = (r) => {
|
|
161
|
+
const raw = r?.[positionKey];
|
|
162
|
+
const n = Number(raw);
|
|
163
|
+
return raw === null || raw === undefined || raw === '' || !Number.isFinite(n) ? Infinity : n;
|
|
164
|
+
};
|
|
165
|
+
const ordered = rows
|
|
166
|
+
.map((r, i) => ({ r, i }))
|
|
167
|
+
.sort((a, b) => {
|
|
168
|
+
const pa = pos(a.r);
|
|
169
|
+
const pb = pos(b.r);
|
|
170
|
+
if (pa !== pb)
|
|
171
|
+
return pa < pb ? -1 : 1;
|
|
172
|
+
const ca = String(a.r?.created_at ?? '');
|
|
173
|
+
const cb = String(b.r?.created_at ?? '');
|
|
174
|
+
if (ca && cb && ca !== cb)
|
|
175
|
+
return ca < cb ? -1 : 1;
|
|
176
|
+
// Last resort: the id, so two tabs (or two runs) agree on the order.
|
|
177
|
+
const ia = String(a.r?.id ?? '');
|
|
178
|
+
const ib = String(b.r?.id ?? '');
|
|
179
|
+
if (ia !== ib)
|
|
180
|
+
return ia < ib ? -1 : 1;
|
|
181
|
+
return a.i - b.i;
|
|
182
|
+
})
|
|
183
|
+
.map((x) => x.r);
|
|
184
|
+
const updates = [];
|
|
185
|
+
ordered.forEach((r, i) => {
|
|
186
|
+
if (pos(r) !== i && r?.id !== undefined && r?.id !== null)
|
|
187
|
+
updates.push({ id: r.id, position: i });
|
|
188
|
+
});
|
|
189
|
+
return { ordered, updates };
|
|
190
|
+
}
|
|
26
191
|
// Pulls a human label off a resolved relation/user object a backend serves:
|
|
27
192
|
// `{ value, label }` (FK sibling), `{ name, … }` (user object such as
|
|
28
193
|
// created_by) or `{ title }`. Returns undefined for plain/empty objects so the
|
|
@@ -138,12 +303,23 @@ export function deriveRelationFormFields(metadata, foreignKey) {
|
|
|
138
303
|
// Managed/audit columns are server-owned and ship resolved objects
|
|
139
304
|
// (`created_by = { name, avatar, … }`); making them editable inputs
|
|
140
305
|
// renders `[object Object]`. Never surface them in the inline form.
|
|
141
|
-
if (
|
|
306
|
+
if (isManagedRelationColumn(col.key))
|
|
307
|
+
continue;
|
|
308
|
+
// Derived / hidden columns are filled by the SDK or the server, never
|
|
309
|
+
// typed by whoever uploads a photo (width, height, position…).
|
|
310
|
+
const ui = relationColumnUi(col);
|
|
311
|
+
if (ui.form || ui.derive)
|
|
142
312
|
continue;
|
|
313
|
+
const media = relationMediaKind(col);
|
|
314
|
+
const accept = col.accept;
|
|
143
315
|
out.push({
|
|
144
316
|
key: col.key,
|
|
145
317
|
label: col.label,
|
|
146
|
-
|
|
318
|
+
// A file/image column gets the same upload widget (with preview) as
|
|
319
|
+
// the parent form, not a free-text path input.
|
|
320
|
+
type: media ?? columnTypeToFieldType(col),
|
|
321
|
+
...(media === 'image' ? { accept: accept || 'image/*' } : accept ? { accept } : {}),
|
|
322
|
+
...(ui.emptyLabel ? { placeholder: ui.emptyLabel } : {}),
|
|
147
323
|
required: false,
|
|
148
324
|
options: col.options?.map(o => ({ value: String(o.value), label: o.label })),
|
|
149
325
|
// Carry the FK target through so a belongs_to column renders the
|
|
@@ -21,6 +21,16 @@ export interface DynamicRelationStrings {
|
|
|
21
21
|
searchPlaceholder: string;
|
|
22
22
|
/** Pie de la sub-tabla: "{{loaded}} de {{total}}". */
|
|
23
23
|
countLabel: string;
|
|
24
|
+
/** Galería: toast tras eliminar una foto. Default "Foto eliminada". */
|
|
25
|
+
deletedToast?: string;
|
|
26
|
+
/** Galería: acción del toast. Default "Deshacer". */
|
|
27
|
+
undoLabel?: string;
|
|
28
|
+
/** Galería: falla al guardar el orden. */
|
|
29
|
+
reorderError?: string;
|
|
30
|
+
/** Galería: falla al eliminar. */
|
|
31
|
+
deleteError?: string;
|
|
32
|
+
/** Galería: confirmación al eliminar una foto. Default "Esta foto deja de verse en la tienda. Puedes deshacerlo unos segundos." */
|
|
33
|
+
confirmRemovePhotoDescription?: string;
|
|
24
34
|
}
|
|
25
35
|
interface CommonProps {
|
|
26
36
|
/** id del registro padre. */
|
|
@@ -75,6 +85,12 @@ export interface DynamicRelationOneToManyProps extends CommonProps {
|
|
|
75
85
|
foreignKey: string;
|
|
76
86
|
/** Endpoint override; default `/data/${model}`. */
|
|
77
87
|
endpoint?: string;
|
|
88
|
+
/**
|
|
89
|
+
* El registro padre. Solo lo leen los campos que declaran
|
|
90
|
+
* `display_config.default_from_parent` (p. ej. el nombre del producto como
|
|
91
|
+
* texto alternativo sugerido de una foto). Opcional.
|
|
92
|
+
*/
|
|
93
|
+
parentRecord?: Record<string, unknown> | null;
|
|
78
94
|
}
|
|
79
95
|
export interface DynamicRelationManyToManyProps extends CommonProps {
|
|
80
96
|
kind: 'many_to_many';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dynamic-relation.d.ts","sourceRoot":"","sources":["../src/dynamic-relation.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"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