@firecms/core 3.0.0-canary.287 → 3.0.0-canary.288

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.
@@ -25,3 +25,4 @@ export declare function removeEntityFromCache(path: string): void;
25
25
  * Clears the entire in-memory cache and removes all related entities from `localStorage`.
26
26
  */
27
27
  export declare function clearEntityCache(): void;
28
+ export declare function flattenKeys(obj: any, prefix?: string, result?: string[]): string[];
@@ -1,5 +1,6 @@
1
1
  export declare const pick: <T>(obj: T, ...args: any[]) => T;
2
2
  export declare function isObject(item: any): any;
3
+ export declare function isPlainObject(obj: any): boolean;
3
4
  export declare function mergeDeep<T extends Record<any, any>, U extends Record<any, any>>(target: T, source: U, ignoreUndefined?: boolean): T & U;
4
5
  export declare function getValueInPath(o: object | undefined, path: string): any;
5
6
  export declare function removeInPath(o: object, path: string): object | undefined;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@firecms/core",
3
3
  "type": "module",
4
- "version": "3.0.0-canary.287",
4
+ "version": "3.0.0-canary.288",
5
5
  "description": "Awesome Firebase/Firestore-based headless open-source CMS",
6
6
  "funding": {
7
7
  "url": "https://github.com/sponsors/firecmsco"
@@ -53,9 +53,9 @@
53
53
  "@dnd-kit/core": "^6.3.1",
54
54
  "@dnd-kit/modifiers": "^9.0.0",
55
55
  "@dnd-kit/sortable": "^10.0.0",
56
- "@firecms/editor": "^3.0.0-canary.287",
57
- "@firecms/formex": "^3.0.0-canary.287",
58
- "@firecms/ui": "^3.0.0-canary.287",
56
+ "@firecms/editor": "^3.0.0-canary.288",
57
+ "@firecms/formex": "^3.0.0-canary.288",
58
+ "@firecms/ui": "^3.0.0-canary.288",
59
59
  "@radix-ui/react-portal": "^1.1.9",
60
60
  "clsx": "^2.1.1",
61
61
  "date-fns": "^3.6.0",
@@ -108,7 +108,7 @@
108
108
  "dist",
109
109
  "src"
110
110
  ],
111
- "gitHead": "c6606ddc2309cbdaeacb3103fbb2bb50a20418ce",
111
+ "gitHead": "e609cafa0c505803a4628324a77404ebdfbb62c4",
112
112
  "publishConfig": {
113
113
  "access": "public"
114
114
  },
@@ -1,11 +1,11 @@
1
1
  import React, { useMemo } from "react";
2
- import { PropertyPreview } from "../preview";
3
2
  import { Entity, EntityCollection, ResolvedEntityCollection, ResolvedProperties } from "../types";
4
3
  import { resolveCollection } from "../util";
5
- import { cls, defaultBorderMixin, IconButton, OpenInNewIcon } from "@firecms/ui";
4
+ import { cls, defaultBorderMixin, IconButton, OpenInNewIcon, Typography } from "@firecms/ui";
6
5
  import { CustomizationController } from "../types/customization_controller";
7
6
  import { useCustomizationController } from "../hooks/useCustomizationController";
8
7
  import { useAuthController } from "../hooks";
8
+ import { PropertyCollectionView } from "./PropertyCollectionView";
9
9
 
10
10
  /**
11
11
  * @group Components
@@ -40,47 +40,36 @@ export function EntityView<M extends Record<string, any>>(
40
40
 
41
41
  return (
42
42
  <div className={"w-full " + className}>
43
- <div className={"w-full mb-4"}>
44
- <div className={cls(defaultBorderMixin, "flex justify-between py-2 border-b last:border-b-0")}>
45
- <div className="flex items-center w-1/4">
46
- <span className="pl-2 text-sm text-surface-600">Id</span>
43
+ <div className={"w-full mb-4 p-4"}>
44
+
45
+ <div className={`grid grid-cols-12 gap-x-4 py-4 items-start border-b ${defaultBorderMixin}`}>
46
+ <div className="col-span-4 pr-2">
47
+ <Typography variant="caption"
48
+ color={"secondary"}
49
+ component={"span"}
50
+ className="break-words">
51
+ Id
52
+ </Typography>
47
53
  </div>
48
- <div
49
- className="flex-grow p-2 ml-2 w-3/4 text-surface-900 dark:text-white min-h-[56px] flex items-center">
50
- <span className="flex-grow mr-2">{entity.id}</span>
51
- {customizationController?.entityLinkBuilder &&
52
- <a href={customizationController.entityLinkBuilder({ entity })}
53
- rel="noopener noreferrer"
54
- target="_blank">
55
- <IconButton>
56
- <OpenInNewIcon
57
- size={"small"}/>
58
- </IconButton>
59
- </a>}
54
+ <div className="col-span-8">
55
+ <div
56
+ className="flex-grow text-surface-900 dark:text-white flex items-center">
57
+ <span className="flex-grow mr-2">{entity.id}</span>
58
+ {customizationController?.entityLinkBuilder &&
59
+ <a href={customizationController.entityLinkBuilder({ entity })}
60
+ rel="noopener noreferrer"
61
+ target="_blank">
62
+ <IconButton>
63
+ <OpenInNewIcon
64
+ size={"small"}/>
65
+ </IconButton>
66
+ </a>}
67
+ </div>
60
68
  </div>
61
69
  </div>
62
- {Object.entries(properties)
63
- .map(([key, property]) => {
64
- const value = entity.values?.[key];
65
- return (
66
- <div
67
- key={`reference_previews_${key}`}
68
- className={cls(defaultBorderMixin, "flex justify-between py-2 border-b last:border-b-0")}>
69
- <div className="flex items-center w-1/4">
70
- <span className="pl-2 text-sm text-surface-600">{property.name}</span>
71
- </div>
72
- <div
73
- className="flex-grow p-2 ml-2 w-3/4 text-surface-900 dark:text-white min-h-[56px] flex items-center">
74
- <PropertyPreview
75
- propertyKey={key}
76
- value={value}
77
- // entity={entity}
78
- property={property}
79
- size={"medium"}/>
80
- </div>
81
- </div>
82
- )
83
- })}
70
+
71
+ <PropertyCollectionView data={entity.values} properties={properties} size={"medium"}/>
72
+
84
73
  </div>
85
74
  </div>
86
75
  );
@@ -0,0 +1,329 @@
1
+ import React from "react";
2
+ import { defaultBorderMixin, Typography } from "@firecms/ui";
3
+ import { PreviewSize, PropertyPreview } from "../preview";
4
+ import { ResolvedProperties, ResolvedProperty } from "../types";
5
+ import { getValueInPath } from "../util";
6
+
7
+ /**
8
+ * Build a readable label for a path and resolve the property
9
+ * Supports map and array (including arrays of maps)
10
+ */
11
+ export function buildPropertyLabelAndGetProperty(
12
+ properties: ResolvedProperties,
13
+ key: string
14
+ ): { label: string; property: ResolvedProperty | undefined } {
15
+ if (!key) return {
16
+ label: "",
17
+ property: undefined
18
+ };
19
+
20
+ // Parse "a[0].b.c[2]" -> ["a", 0, "b", "c", 2]
21
+ const segments: (string | number)[] = [];
22
+ const re = /([^[.\]]+)|\[(\d+)\]/g;
23
+ let m: RegExpExecArray | null;
24
+ while ((m = re.exec(key)) !== null) {
25
+ if (m[1] !== undefined) segments.push(m[1]);
26
+ else if (m[2] !== undefined) segments.push(Number(m[2]));
27
+ }
28
+
29
+ let currentProps: ResolvedProperties | undefined = properties;
30
+ let currentProp: ResolvedProperty | undefined;
31
+ let lastLabel = "";
32
+
33
+ const getArrayOfProp = (p?: ResolvedProperty): ResolvedProperty | undefined => {
34
+ if (!p || p.dataType !== "array") return undefined;
35
+ return Array.isArray(p.of) ? (p.of[0] as ResolvedProperty) : (p.of as ResolvedProperty | undefined);
36
+ };
37
+
38
+ for (const seg of segments) {
39
+ if (typeof seg === "number") {
40
+ // Last segment label should be the index itself
41
+ lastLabel = `[${seg}]`;
42
+
43
+ // Move schema context into the array element
44
+ if (currentProp?.dataType === "array") {
45
+ currentProp = getArrayOfProp(currentProp);
46
+ if (currentProp?.dataType === "map" && currentProp.properties) {
47
+ currentProps = currentProp.properties as ResolvedProperties;
48
+ } else {
49
+ currentProps = undefined;
50
+ }
51
+ } else {
52
+ // Index without array schema context
53
+ currentProp = undefined;
54
+ currentProps = undefined;
55
+ }
56
+ continue;
57
+ }
58
+
59
+ // seg is a string key
60
+ if (currentProps && (currentProps as any)[seg]) {
61
+ const nextProp = (currentProps as any)[seg] as ResolvedProperty;
62
+ currentProp = nextProp;
63
+ // Last segment label should be the property name (or the raw key)
64
+ lastLabel = nextProp.name || String(seg);
65
+
66
+ if (nextProp.dataType === "map" && nextProp.properties) {
67
+ currentProps = nextProp.properties as ResolvedProperties;
68
+ } else if (nextProp.dataType === "array") {
69
+ // Keep array prop; the next segment (index) will step into its element schema
70
+ currentProps = undefined;
71
+ } else {
72
+ currentProps = undefined;
73
+ }
74
+ } else {
75
+ // Unknown key or no schema context
76
+ currentProp = undefined;
77
+ currentProps = undefined;
78
+ lastLabel = String(seg);
79
+ }
80
+ }
81
+
82
+ return {
83
+ label: lastLabel,
84
+ property: currentProp
85
+ };
86
+ }
87
+
88
+ const pathEndsWithIndex = (p: string) => /\[\d+\]$/.test(p);
89
+
90
+ /**
91
+ * Improved simple layout for nested changes:
92
+ * - Map or Array-of-Map -> section header + indented rows
93
+ * - Leaf or Array-of-Primitives -> single row with label and value
94
+ */
95
+ export const PropertyCollectionView = ({
96
+ data,
97
+ properties,
98
+ baseKey = "",
99
+ suppressHeader = false,
100
+ size = "small"
101
+ }: {
102
+ data: any;
103
+ properties: ResolvedProperties;
104
+ baseKey?: string;
105
+ suppressHeader?: boolean;
106
+ size?: PreviewSize;
107
+ }) => {
108
+
109
+ const isTopLevel = !!baseKey && !baseKey.includes(".") && !baseKey.includes("[");
110
+
111
+ // Arrays
112
+ if (Array.isArray(data)) {
113
+ const {
114
+ label: arrayLabel,
115
+ property
116
+ } = baseKey
117
+ ? buildPropertyLabelAndGetProperty(properties, baseKey)
118
+ : {
119
+ label: "",
120
+ property: undefined as ResolvedProperty | undefined
121
+ };
122
+
123
+ const ofProp = property?.dataType === "array"
124
+ ? (Array.isArray(property.of) ? property.of[0] : property.of) as ResolvedProperty | undefined
125
+ : undefined;
126
+
127
+ const isArrayOfMaps = ofProp?.dataType === "map";
128
+ const isArrayOfPrimitives = property?.dataType === "array" && ofProp && ofProp.dataType !== "map";
129
+
130
+ // Array of primitives -> single row
131
+ if (baseKey && property && isArrayOfPrimitives) {
132
+ return (
133
+ <div
134
+ className={`grid grid-cols-12 gap-x-4 ${isTopLevel ? "py-4" : "py-2"} items-start ${isTopLevel ? `border-b ${defaultBorderMixin}` : ""}`}>
135
+ <div className="col-span-4 pr-2">
136
+ <Typography variant="caption"
137
+ color={"secondary"}
138
+ component={"span"}
139
+ className="break-words">
140
+ {arrayLabel}
141
+ </Typography>
142
+ </div>
143
+ <div className="col-span-8">
144
+ <PropertyPreview propertyKey={baseKey}
145
+ value={data}
146
+ property={property}
147
+ size={size}/>
148
+ </div>
149
+ </div>
150
+ );
151
+ }
152
+
153
+ // Array of maps or unknown -> array header + combined item header (MapName [index]) then content
154
+ return (
155
+ <div className={`${isTopLevel ? "py-4" : "py-1"} ${isTopLevel ? `border-b ${defaultBorderMixin}` : ""}`}>
156
+ {baseKey && arrayLabel && !suppressHeader && (
157
+ <Typography variant="caption"
158
+ color={"secondary"}
159
+ component={"span"}>
160
+ {arrayLabel}
161
+ </Typography>
162
+ )}
163
+ <div className={baseKey ? `pl-4 mt-1 border-l ${defaultBorderMixin}` : ""}>
164
+ {data.map((item, index) => {
165
+ if (item === null || item === undefined) return null;
166
+ const currentKey = baseKey ? `${baseKey}[${index}]` : `[${index}]`;
167
+
168
+ // Combined header text
169
+ const itemHeader = isArrayOfMaps && ofProp?.name
170
+ ? `${ofProp.name} [${index}]`
171
+ : `[${index}]`;
172
+
173
+ return (
174
+ <div key={currentKey} className="py-1">
175
+ <Typography variant="caption"
176
+ color={"secondary"}
177
+ component={"span"}>
178
+ {itemHeader}
179
+ </Typography>
180
+ <div className={`pl-4 mt-1 border-l ${defaultBorderMixin}`}>
181
+ <PropertyCollectionView
182
+ data={item}
183
+ properties={properties}
184
+ baseKey={currentKey}
185
+ suppressHeader={true} // don’t repeat the inner map header
186
+ size={size}
187
+ />
188
+ </div>
189
+ </div>
190
+ );
191
+ })}
192
+ </div>
193
+ </div>
194
+ );
195
+ }
196
+
197
+ // Objects (maps or plain objects)
198
+ if (typeof data === "object" && data !== null) {
199
+ const {
200
+ label,
201
+ property
202
+ } = baseKey
203
+ ? buildPropertyLabelAndGetProperty(properties, baseKey)
204
+ : {
205
+ label: "",
206
+ property: undefined as ResolvedProperty | undefined
207
+ };
208
+
209
+ // Non-map leaf-like object -> single row
210
+ if (baseKey && (!property || property.dataType !== "map" || !property.properties)) {
211
+ if (!property) return null;
212
+ return (
213
+ <div
214
+ className={`grid grid-cols-12 gap-x-4 ${isTopLevel ? "py-4" : "py-2"} items-start ${isTopLevel ? `border-b ${defaultBorderMixin}` : ""}`}>
215
+ <div className="col-span-4 pr-2">
216
+ <Typography variant="caption"
217
+ color={"secondary"}
218
+ component={"span"}
219
+ className="break-words">
220
+ {label}
221
+ </Typography>
222
+ </div>
223
+ <div className="col-span-8">
224
+ <PropertyPreview propertyKey={baseKey}
225
+ value={data}
226
+ property={property}
227
+ size={size}/>
228
+ </div>
229
+ </div>
230
+ );
231
+ }
232
+
233
+ // Map with defined properties -> show map header only if not suppressed
234
+ const showMapHeader =
235
+ baseKey &&
236
+ !suppressHeader &&
237
+ property?.dataType === "map" &&
238
+ (property.name || !pathEndsWithIndex(baseKey));
239
+
240
+ const headerText = property?.name || label;
241
+
242
+ return (
243
+ <div className={`${isTopLevel ? "py-4" : "py-1"} ${isTopLevel ? `border-b ${defaultBorderMixin}` : ""}`}>
244
+ {showMapHeader && (
245
+ <Typography variant="caption"
246
+ color={"secondary"}
247
+ component={"span"}
248
+ >
249
+ {headerText}
250
+ </Typography>
251
+ )}
252
+ <div className={baseKey ? `pl-4 mt-1 border-l ${defaultBorderMixin}` : ""}>
253
+ {Object.entries(data).map(([key, value]) => {
254
+ if (value === null || value === undefined) return null;
255
+ const currentKey = baseKey ? `${baseKey}.${key}` : key;
256
+ return (
257
+ <PropertyCollectionView
258
+ key={currentKey}
259
+ data={value}
260
+ properties={properties}
261
+ baseKey={currentKey}
262
+ size={size}
263
+ />
264
+ );
265
+ })}
266
+ </div>
267
+ </div>
268
+ );
269
+ }
270
+
271
+ // Primitives
272
+ if (baseKey) {
273
+ const {
274
+ label,
275
+ property
276
+ } = buildPropertyLabelAndGetProperty(properties, baseKey);
277
+ if (!property) return null;
278
+ return (
279
+ <div
280
+ className={`grid grid-cols-12 gap-x-4 ${isTopLevel ? "py-4" : "py-2"} items-start ${isTopLevel ? `border-b ${defaultBorderMixin}` : ""}`}>
281
+ <div className="col-span-4 pr-2">
282
+ <Typography variant="caption"
283
+ color={"secondary"}
284
+ component={"span"}
285
+ className="break-words">
286
+ {label}
287
+ </Typography>
288
+ </div>
289
+ <div className="col-span-8">
290
+ <PropertyPreview propertyKey={baseKey}
291
+ value={data}
292
+ property={property}
293
+ size={size}/>
294
+ </div>
295
+ </div>
296
+ );
297
+ }
298
+
299
+ return null;
300
+ };
301
+
302
+ export function buildDataFromPaths(values: object, paths: string[]): object {
303
+ const result = {};
304
+ paths.forEach(path => {
305
+ const value = getValueInPath(values, path);
306
+ if (value === undefined) return;
307
+
308
+ // lodash.set would be perfect here
309
+ const segments = path.replace(/\[(\d+)\]/g, ".$1").split(".");
310
+ let current: any = result;
311
+ segments.forEach((segment, index) => {
312
+ if (index === segments.length - 1) {
313
+ current[segment] = value;
314
+ } else {
315
+ const nextSegment = segments[index + 1];
316
+ const isNextAnIndex = /^\d+$/.test(nextSegment);
317
+ if (!current[segment]) {
318
+ if (isNextAnIndex) {
319
+ current[segment] = [];
320
+ } else {
321
+ current[segment] = {};
322
+ }
323
+ }
324
+ current = current[segment];
325
+ }
326
+ });
327
+ });
328
+ return result;
329
+ }
@@ -22,6 +22,7 @@ import {
22
22
  getLocalChangesBackup,
23
23
  getValueInPath,
24
24
  isHidden,
25
+ isObject,
25
26
  isReadOnly,
26
27
  mergeDeep,
27
28
  resolveCollection,
@@ -39,11 +40,12 @@ import {
39
40
  useSnackbarController
40
41
  } from "../hooks";
41
42
  import { Alert, CheckIcon, Chip, cls, EditIcon, NotesIcon, paperMixin, Tooltip, Typography } from "@firecms/ui";
42
- import { flattenKeys, Formex, FormexController, getIn, setIn, useCreateFormex } from "@firecms/formex";
43
+ import { Formex, FormexController, getIn, setIn, useCreateFormex } from "@firecms/formex";
43
44
  import { useAnalyticsController } from "../hooks/useAnalyticsController";
44
45
  import { FormEntry, FormLayout, LabelWithIconAndTooltip, PropertyFieldBinding } from "../form";
45
46
  import { ValidationError } from "yup";
46
47
  import {
48
+ flattenKeys,
47
49
  getEntityFromCache,
48
50
  removeEntityFromCache,
49
51
  removeEntityFromMemoryCache,
@@ -120,6 +122,64 @@ export function extractTouchedValues(values: any, touched: Record<string, boolea
120
122
  return acc;
121
123
  }
122
124
 
125
+ export function getChanges<T extends object>(source: Partial<T>, comparison: Partial<T>): Partial<T> {
126
+ const changes: Partial<T> = {};
127
+
128
+ if (!source) {
129
+ return {};
130
+ }
131
+ if (!comparison) {
132
+ return source;
133
+ }
134
+
135
+ const allKeys = Array.from(new Set([...Object.keys(source), ...Object.keys(comparison)]));
136
+
137
+ for (const key of allKeys) {
138
+ const sourceValue = (source as any)[key];
139
+ const comparisonValue = (comparison as any)[key];
140
+
141
+ if (equal(sourceValue, comparisonValue)) {
142
+ continue;
143
+ }
144
+
145
+ const sourceHasKey = source && typeof source === "object" && Object.prototype.hasOwnProperty.call(source, key);
146
+ const comparisonHasKey = comparison && typeof comparison === "object" && Object.prototype.hasOwnProperty.call(comparison, key);
147
+
148
+ if (comparisonHasKey && !sourceHasKey) {
149
+ (changes as any)[key] = undefined;
150
+ } else if (Array.isArray(sourceValue)) {
151
+ const comparisonArray = Array.isArray(comparisonValue) ? comparisonValue : [];
152
+ if (sourceValue.length < comparisonArray.length) {
153
+ (changes as any)[key] = sourceValue;
154
+ continue;
155
+ }
156
+ const changedArray = sourceValue.map((item, index) => {
157
+ const comparisonItem = comparisonArray[index];
158
+ if (equal(item, comparisonItem)) {
159
+ return null;
160
+ }
161
+ if (isObject(item) && item && isObject(comparisonItem) && comparisonItem) {
162
+ const nestedChanges = getChanges(item, comparisonItem);
163
+ return Object.keys(nestedChanges).length > 0 ? nestedChanges : item;
164
+ }
165
+ return item;
166
+ });
167
+ if (changedArray.some(item => item !== null) || sourceValue.length > comparisonArray.length) {
168
+ (changes as any)[key] = changedArray;
169
+ }
170
+ } else if (isObject(sourceValue) && sourceValue && isObject(comparisonValue) && comparisonValue) {
171
+ const nestedChanges = getChanges(sourceValue, comparisonValue);
172
+ if (Object.keys(nestedChanges).length > 0) {
173
+ (changes as any)[key] = nestedChanges;
174
+ }
175
+ } else {
176
+ (changes as any)[key] = sourceValue;
177
+ }
178
+ }
179
+
180
+ return changes;
181
+ }
182
+
123
183
  export function EntityForm<M extends Record<string, any>>({
124
184
  path,
125
185
  fullIdPath,
@@ -272,16 +332,7 @@ export function EntityForm<M extends Record<string, any>>({
272
332
  if (!localChangesDataRaw) {
273
333
  return undefined;
274
334
  }
275
- let filteredChanges = {};
276
- const flattenedKeys = flattenKeys(localChangesDataRaw);
277
- flattenedKeys.forEach(key => {
278
- const localValue = getIn(localChangesDataRaw, key);
279
- const initialValue = getIn(initialValues, key);
280
- if (!equal(localValue, initialValue)) {
281
- filteredChanges = setIn(filteredChanges, key, localValue);
282
- }
283
- });
284
- return filteredChanges;
335
+ return getChanges(localChangesDataRaw, initialValues);
285
336
  }, [localChangesDataRaw, initialValues]);
286
337
 
287
338
  const hasLocalChanges = !localChangesCleared && localChangesData && Object.keys(localChangesData).length > 0;
@@ -891,3 +942,4 @@ function useOnAutoSave(autoSave: undefined | boolean, formex: FormexController<a
891
942
  }
892
943
  }, [formex.values]);
893
944
  }
945
+