@payloadcms/live-preview 3.32.0-internal.f2e77bd → 3.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/handleMessage.d.ts +1 -1
- package/dist/handleMessage.d.ts.map +1 -1
- package/dist/handleMessage.js.map +1 -1
- package/dist/mergeData.d.ts +1 -1
- package/dist/mergeData.d.ts.map +1 -1
- package/dist/mergeData.js +1 -1
- package/dist/mergeData.js.map +1 -1
- package/dist/subscribe.d.ts +1 -1
- package/dist/subscribe.d.ts.map +1 -1
- package/dist/subscribe.js.map +1 -1
- package/dist/traverseFields.d.ts +4 -5
- package/dist/traverseFields.d.ts.map +1 -1
- package/dist/traverseFields.js +3 -3
- package/dist/traverseFields.js.map +1 -1
- package/dist/traverseRichText.js +1 -1
- package/dist/traverseRichText.js.map +1 -1
- package/package.json +2 -2
package/dist/handleMessage.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleMessage.d.ts","sourceRoot":"","sources":["../src/handleMessage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"handleMessage.d.ts","sourceRoot":"","sources":["../src/handleMessage.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAwBzD,eAAO,MAAM,aAAa,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IACvE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,uBAAuB,CAAC,CAAC,CAAC,CAAA;IACjC,WAAW,EAAE,CAAC,CAAA;IACd,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,OAAO,CAAC,CAAC,CAoCZ,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/handleMessage.ts"],"sourcesContent":["import type { LivePreviewMessageEvent } from './types.js'\n\nimport { isLivePreviewEvent } from './isLivePreviewEvent.js'\nimport { mergeData } from './mergeData.js'\n\nconst _payloadLivePreview = {\n /**\n * For performance reasons, `fieldSchemaJSON` will only be sent once on the initial message\n * We need to cache this value so that it can be used across subsequent messages\n * To do this, save `fieldSchemaJSON` when it arrives as a global variable\n * Send this cached value to `mergeData`, instead of `eventData.fieldSchemaJSON` directly\n */\n fieldSchema: undefined,\n /**\n * Each time the data is merged, cache the result as a `previousData` variable\n * This will ensure changes compound overtop of each other\n */\n previousData: undefined,\n}\n\nexport const handleMessage = async <T
|
|
1
|
+
{"version":3,"sources":["../src/handleMessage.ts"],"sourcesContent":["import type { FieldSchemaJSON } from 'payload'\n\nimport type { LivePreviewMessageEvent } from './types.js'\n\nimport { isLivePreviewEvent } from './isLivePreviewEvent.js'\nimport { mergeData } from './mergeData.js'\n\nconst _payloadLivePreview: {\n fieldSchema: FieldSchemaJSON | undefined\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n previousData: any\n} = {\n /**\n * For performance reasons, `fieldSchemaJSON` will only be sent once on the initial message\n * We need to cache this value so that it can be used across subsequent messages\n * To do this, save `fieldSchemaJSON` when it arrives as a global variable\n * Send this cached value to `mergeData`, instead of `eventData.fieldSchemaJSON` directly\n */\n fieldSchema: undefined,\n /**\n * Each time the data is merged, cache the result as a `previousData` variable\n * This will ensure changes compound overtop of each other\n */\n previousData: undefined,\n}\n\nexport const handleMessage = async <T extends Record<string, any>>(args: {\n apiRoute?: string\n depth?: number\n event: LivePreviewMessageEvent<T>\n initialData: T\n serverURL: string\n}): Promise<T> => {\n const { apiRoute, depth, event, initialData, serverURL } = args\n\n if (isLivePreviewEvent(event, serverURL)) {\n const { data, externallyUpdatedRelationship, fieldSchemaJSON, locale } = event.data\n\n if (!_payloadLivePreview?.fieldSchema && fieldSchemaJSON) {\n _payloadLivePreview.fieldSchema = fieldSchemaJSON\n }\n\n if (!_payloadLivePreview?.fieldSchema) {\n // eslint-disable-next-line no-console\n console.warn(\n 'Payload Live Preview: No `fieldSchemaJSON` was received from the parent window. Unable to merge data.',\n )\n\n return initialData\n }\n\n const mergedData = await mergeData<T>({\n apiRoute,\n depth,\n externallyUpdatedRelationship,\n fieldSchema: _payloadLivePreview.fieldSchema,\n incomingData: data,\n initialData: _payloadLivePreview?.previousData || initialData,\n locale,\n serverURL,\n })\n\n _payloadLivePreview.previousData = mergedData\n\n return mergedData\n }\n\n return initialData\n}\n"],"names":["isLivePreviewEvent","mergeData","_payloadLivePreview","fieldSchema","undefined","previousData","handleMessage","args","apiRoute","depth","event","initialData","serverURL","data","externallyUpdatedRelationship","fieldSchemaJSON","locale","console","warn","mergedData","incomingData"],"mappings":"AAIA,SAASA,kBAAkB,QAAQ,0BAAyB;AAC5D,SAASC,SAAS,QAAQ,iBAAgB;AAE1C,MAAMC,sBAIF;IACF;;;;;GAKC,GACDC,aAAaC;IACb;;;GAGC,GACDC,cAAcD;AAChB;AAEA,OAAO,MAAME,gBAAgB,OAAsCC;IAOjE,MAAM,EAAEC,QAAQ,EAAEC,KAAK,EAAEC,KAAK,EAAEC,WAAW,EAAEC,SAAS,EAAE,GAAGL;IAE3D,IAAIP,mBAAmBU,OAAOE,YAAY;QACxC,MAAM,EAAEC,IAAI,EAAEC,6BAA6B,EAAEC,eAAe,EAAEC,MAAM,EAAE,GAAGN,MAAMG,IAAI;QAEnF,IAAI,CAACX,qBAAqBC,eAAeY,iBAAiB;YACxDb,oBAAoBC,WAAW,GAAGY;QACpC;QAEA,IAAI,CAACb,qBAAqBC,aAAa;YACrC,sCAAsC;YACtCc,QAAQC,IAAI,CACV;YAGF,OAAOP;QACT;QAEA,MAAMQ,aAAa,MAAMlB,UAAa;YACpCO;YACAC;YACAK;YACAX,aAAaD,oBAAoBC,WAAW;YAC5CiB,cAAcP;YACdF,aAAaT,qBAAqBG,gBAAgBM;YAClDK;YACAJ;QACF;QAEAV,oBAAoBG,YAAY,GAAGc;QAEnC,OAAOA;IACT;IAEA,OAAOR;AACT,EAAC"}
|
package/dist/mergeData.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DocumentEvent, FieldSchemaJSON } from 'payload';
|
|
2
|
-
export declare const mergeData: <T
|
|
2
|
+
export declare const mergeData: <T extends Record<string, any>>(args: {
|
|
3
3
|
apiRoute?: string;
|
|
4
4
|
collectionPopulationRequestHandler?: ({ apiPath, endpoint, serverURL, }: {
|
|
5
5
|
apiPath: string;
|
package/dist/mergeData.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mergeData.d.ts","sourceRoot":"","sources":["../src/mergeData.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAiB,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"mergeData.d.ts","sourceRoot":"","sources":["../src/mergeData.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAiB,MAAM,SAAS,CAAA;AA6B5E,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IACnE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,kCAAkC,CAAC,EAAE,CAAC,EACpC,OAAO,EACP,QAAQ,EACR,SAAS,GACV,EAAE;QACD,OAAO,EAAE,MAAM,CAAA;QACf,QAAQ,EAAE,MAAM,CAAA;QAChB,SAAS,EAAE,MAAM,CAAA;KAClB,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;IACvB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,6BAA6B,CAAC,EAAE,aAAa,CAAA;IAC7C,WAAW,EAAE,eAAe,CAAA;IAC5B,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;IACxB,WAAW,EAAE,CAAC,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,sBAAsB,CAAC,EAAE,OAAO,CAAA;IAChC,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,OAAO,CACT;IACE,iBAAiB,CAAC,EAAE,MAAM,CAAA;CAC3B,GAAG,CAAC,CAkEN,CAAA"}
|
package/dist/mergeData.js
CHANGED
|
@@ -38,7 +38,7 @@ export const mergeData = async (args)=>{
|
|
|
38
38
|
}).then((res)=>res.json());
|
|
39
39
|
if (res?.docs?.length > 0) {
|
|
40
40
|
res.docs.forEach((doc)=>{
|
|
41
|
-
populationsByCollection[collection]
|
|
41
|
+
populationsByCollection[collection]?.forEach((population)=>{
|
|
42
42
|
if (population.id === doc.id) {
|
|
43
43
|
population.ref[population.accessor] = doc;
|
|
44
44
|
}
|
package/dist/mergeData.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/mergeData.ts"],"sourcesContent":["import type { DocumentEvent, FieldSchemaJSON, PaginatedDocs } from 'payload'\n\nimport type { PopulationsByCollection } from './types.js'\n\nimport { traverseFields } from './traverseFields.js'\n\nconst defaultRequestHandler = ({ apiPath
|
|
1
|
+
{"version":3,"sources":["../src/mergeData.ts"],"sourcesContent":["import type { DocumentEvent, FieldSchemaJSON, PaginatedDocs } from 'payload'\n\nimport type { PopulationsByCollection } from './types.js'\n\nimport { traverseFields } from './traverseFields.js'\n\nconst defaultRequestHandler = ({\n apiPath,\n endpoint,\n serverURL,\n}: {\n apiPath: string\n endpoint: string\n serverURL: string\n}) => {\n const url = `${serverURL}${apiPath}/${endpoint}`\n return fetch(url, {\n credentials: 'include',\n headers: {\n 'Content-Type': 'application/json',\n },\n })\n}\n\n// Relationships are only updated when their `id` or `relationTo` changes, by comparing the old and new values\n// This needs to also happen when locale changes, except this is not not part of the API response\n// Instead, we keep track of the old locale ourselves and trigger a re-population when it changes\nlet prevLocale: string | undefined\n\nexport const mergeData = async <T extends Record<string, any>>(args: {\n apiRoute?: string\n collectionPopulationRequestHandler?: ({\n apiPath,\n endpoint,\n serverURL,\n }: {\n apiPath: string\n endpoint: string\n serverURL: string\n }) => Promise<Response>\n depth?: number\n externallyUpdatedRelationship?: DocumentEvent\n fieldSchema: FieldSchemaJSON\n incomingData: Partial<T>\n initialData: T\n locale?: string\n returnNumberOfRequests?: boolean\n serverURL: string\n}): Promise<\n {\n _numberOfRequests?: number\n } & T\n> => {\n const {\n apiRoute,\n depth,\n externallyUpdatedRelationship,\n fieldSchema,\n incomingData,\n initialData,\n locale,\n returnNumberOfRequests,\n serverURL,\n } = args\n\n const result = { ...initialData }\n\n const populationsByCollection: PopulationsByCollection = {}\n\n traverseFields({\n externallyUpdatedRelationship,\n fieldSchema,\n incomingData,\n localeChanged: prevLocale !== locale,\n populationsByCollection,\n result,\n })\n\n await Promise.all(\n Object.entries(populationsByCollection).map(async ([collection, populations]) => {\n let res: PaginatedDocs\n\n const ids = new Set(populations.map(({ id }) => id))\n const requestHandler = args.collectionPopulationRequestHandler || defaultRequestHandler\n\n try {\n res = await requestHandler({\n apiPath: apiRoute || '/api',\n endpoint: encodeURI(\n `${collection}?depth=${depth}&where[id][in]=${Array.from(ids).join(',')}${locale ? `&locale=${locale}` : ''}`,\n ),\n serverURL,\n }).then((res) => res.json())\n\n if (res?.docs?.length > 0) {\n res.docs.forEach((doc) => {\n populationsByCollection[collection]?.forEach((population) => {\n if (population.id === doc.id) {\n population.ref[population.accessor] = doc\n }\n })\n })\n }\n } catch (err) {\n console.error(err) // eslint-disable-line no-console\n }\n }),\n )\n\n prevLocale = locale\n\n return {\n ...result,\n ...(returnNumberOfRequests\n ? { _numberOfRequests: Object.keys(populationsByCollection).length }\n : {}),\n }\n}\n"],"names":["traverseFields","defaultRequestHandler","apiPath","endpoint","serverURL","url","fetch","credentials","headers","prevLocale","mergeData","args","apiRoute","depth","externallyUpdatedRelationship","fieldSchema","incomingData","initialData","locale","returnNumberOfRequests","result","populationsByCollection","localeChanged","Promise","all","Object","entries","map","collection","populations","res","ids","Set","id","requestHandler","collectionPopulationRequestHandler","encodeURI","Array","from","join","then","json","docs","length","forEach","doc","population","ref","accessor","err","console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G;gBACzBb,IAAIY,IAAI,CAACE,OAAO,CAAC,CAACC;oBAChBxB,uBAAuB,CAACO,WAAW,EAAEgB,QAAQ,CAACE;wBAC5C,IAAIA,WAAWb,EAAE,KAAKY,IAAIZ,EAAE,EAAE;4BAC5Ba,WAAWC,GAAG,CAACD,WAAWE,QAAQ,CAAC,GAAGH;wBACxC;oBACF;gBACF;YACF;QACF,EAAE,OAAOI,KAAK;YACZC,QAAQC,KAAK,CAACF,KAAK,iCAAiC;;QACtD;IACF;IAGFxC,aAAaS;IAEb,OAAO;QACL,GAAGE,MAAM;QACT,GAAID,yBACA;YAAEiC,mBAAmB3B,OAAO4B,IAAI,CAAChC,yBAAyBsB,MAAM;QAAC,IACjE,CAAC,CAAC;IACR;AACF,EAAC"}
|
package/dist/subscribe.d.ts
CHANGED
package/dist/subscribe.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscribe.d.ts","sourceRoot":"","sources":["../src/subscribe.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,SAAS,GAAI,CAAC,QAAQ;
|
|
1
|
+
{"version":3,"file":"subscribe.d.ts","sourceRoot":"","sources":["../src/subscribe.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IAC7D,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAA;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,CAAC,CAAA;IACd,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,CAAC,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAoBjD,CAAA"}
|
package/dist/subscribe.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/subscribe.ts"],"sourcesContent":["import { handleMessage } from './handleMessage.js'\n\nexport const subscribe = <T
|
|
1
|
+
{"version":3,"sources":["../src/subscribe.ts"],"sourcesContent":["import { handleMessage } from './handleMessage.js'\n\nexport const subscribe = <T extends Record<string, any>>(args: {\n apiRoute?: string\n callback: (data: T) => void\n depth?: number\n initialData: T\n serverURL: string\n}): ((event: MessageEvent) => Promise<void> | void) => {\n const { apiRoute, callback, depth, initialData, serverURL } = args\n\n const onMessage = async (event: MessageEvent) => {\n const mergedData = await handleMessage<T>({\n apiRoute,\n depth,\n event,\n initialData,\n serverURL,\n })\n\n callback(mergedData)\n }\n\n if (typeof window !== 'undefined') {\n window.addEventListener('message', onMessage)\n }\n\n return onMessage\n}\n"],"names":["handleMessage","subscribe","args","apiRoute","callback","depth","initialData","serverURL","onMessage","event","mergedData","window","addEventListener"],"mappings":"AAAA,SAASA,aAAa,QAAQ,qBAAoB;AAElD,OAAO,MAAMC,YAAY,CAAgCC;IAOvD,MAAM,EAAEC,QAAQ,EAAEC,QAAQ,EAAEC,KAAK,EAAEC,WAAW,EAAEC,SAAS,EAAE,GAAGL;IAE9D,MAAMM,YAAY,OAAOC;QACvB,MAAMC,aAAa,MAAMV,cAAiB;YACxCG;YACAE;YACAI;YACAH;YACAC;QACF;QAEAH,SAASM;IACX;IAEA,IAAI,OAAOC,WAAW,aAAa;QACjCA,OAAOC,gBAAgB,CAAC,WAAWJ;IACrC;IAEA,OAAOA;AACT,EAAC"}
|
package/dist/traverseFields.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import type { DocumentEvent } from 'payload';
|
|
2
|
-
import type { fieldSchemaToJSON } from 'payload/shared';
|
|
1
|
+
import type { DocumentEvent, FieldSchemaJSON } from 'payload';
|
|
3
2
|
import type { PopulationsByCollection } from './types.js';
|
|
4
|
-
export declare const traverseFields: <T
|
|
3
|
+
export declare const traverseFields: <T extends Record<string, any>>(args: {
|
|
5
4
|
externallyUpdatedRelationship?: DocumentEvent;
|
|
6
|
-
fieldSchema:
|
|
5
|
+
fieldSchema: FieldSchemaJSON;
|
|
7
6
|
incomingData: T;
|
|
8
7
|
localeChanged: boolean;
|
|
9
8
|
populationsByCollection: PopulationsByCollection;
|
|
10
|
-
result:
|
|
9
|
+
result: Record<string, any>;
|
|
11
10
|
}) => void;
|
|
12
11
|
//# sourceMappingURL=traverseFields.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"traverseFields.d.ts","sourceRoot":"","sources":["../src/traverseFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,
|
|
1
|
+
{"version":3,"file":"traverseFields.d.ts","sourceRoot":"","sources":["../src/traverseFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAE7D,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAIzD,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IAClE,6BAA6B,CAAC,EAAE,aAAa,CAAA;IAC7C,WAAW,EAAE,eAAe,CAAA;IAC5B,YAAY,EAAE,CAAC,CAAA;IACf,aAAa,EAAE,OAAO,CAAA;IACtB,uBAAuB,EAAE,uBAAuB,CAAA;IAChD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC5B,KAAG,IA6RH,CAAA"}
|
package/dist/traverseFields.js
CHANGED
|
@@ -32,7 +32,7 @@ export const traverseFields = (args)=>{
|
|
|
32
32
|
case 'blocks':
|
|
33
33
|
if (Array.isArray(incomingData[fieldName])) {
|
|
34
34
|
result[fieldName] = incomingData[fieldName].map((incomingBlock, i)=>{
|
|
35
|
-
const incomingBlockJSON = fieldSchema.blocks[incomingBlock.blockType];
|
|
35
|
+
const incomingBlockJSON = fieldSchema.blocks?.[incomingBlock.blockType];
|
|
36
36
|
if (!result[fieldName]) {
|
|
37
37
|
result[fieldName] = [];
|
|
38
38
|
}
|
|
@@ -112,7 +112,7 @@ export const traverseFields = (args)=>{
|
|
|
112
112
|
if (!populationsByCollection[fieldSchema.relationTo]) {
|
|
113
113
|
populationsByCollection[fieldSchema.relationTo] = [];
|
|
114
114
|
}
|
|
115
|
-
populationsByCollection[fieldSchema.relationTo]
|
|
115
|
+
populationsByCollection[fieldSchema.relationTo]?.push({
|
|
116
116
|
id: incomingRelation,
|
|
117
117
|
accessor: i,
|
|
118
118
|
ref: result[fieldName]
|
|
@@ -171,7 +171,7 @@ export const traverseFields = (args)=>{
|
|
|
171
171
|
if (!populationsByCollection[fieldSchema.relationTo]) {
|
|
172
172
|
populationsByCollection[fieldSchema.relationTo] = [];
|
|
173
173
|
}
|
|
174
|
-
populationsByCollection[fieldSchema.relationTo]
|
|
174
|
+
populationsByCollection[fieldSchema.relationTo]?.push({
|
|
175
175
|
id: newID,
|
|
176
176
|
accessor: fieldName,
|
|
177
177
|
ref: result
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/traverseFields.ts"],"sourcesContent":["import type { DocumentEvent } from 'payload'\nimport type { fieldSchemaToJSON } from 'payload/shared'\n\nimport type { PopulationsByCollection } from './types.js'\n\nimport { traverseRichText } from './traverseRichText.js'\n\nexport const traverseFields = <T>(args: {\n externallyUpdatedRelationship?: DocumentEvent\n fieldSchema: ReturnType<typeof fieldSchemaToJSON>\n incomingData: T\n localeChanged: boolean\n populationsByCollection: PopulationsByCollection\n result: T\n}): void => {\n const {\n externallyUpdatedRelationship,\n fieldSchema: fieldSchemas,\n incomingData,\n localeChanged,\n populationsByCollection,\n result,\n } = args\n\n fieldSchemas.forEach((fieldSchema) => {\n if ('name' in fieldSchema && typeof fieldSchema.name === 'string') {\n const fieldName = fieldSchema.name\n\n switch (fieldSchema.type) {\n case 'array':\n if (\n !incomingData[fieldName] &&\n incomingData[fieldName] !== undefined &&\n result?.[fieldName] !== undefined\n ) {\n result[fieldName] = []\n }\n\n if (Array.isArray(incomingData[fieldName])) {\n result[fieldName] = incomingData[fieldName].map((incomingRow, i) => {\n if (!result[fieldName]) {\n result[fieldName] = []\n }\n\n if (!result[fieldName][i]) {\n result[fieldName][i] = {}\n }\n\n traverseFields({\n externallyUpdatedRelationship,\n fieldSchema: fieldSchema.fields,\n incomingData: incomingRow,\n localeChanged,\n populationsByCollection,\n result: result[fieldName][i],\n })\n\n return result[fieldName][i]\n })\n }\n\n break\n\n case 'blocks':\n if (Array.isArray(incomingData[fieldName])) {\n result[fieldName] = incomingData[fieldName].map((incomingBlock, i) => {\n const incomingBlockJSON = fieldSchema.blocks[incomingBlock.blockType]\n\n if (!result[fieldName]) {\n result[fieldName] = []\n }\n\n if (\n !result[fieldName][i] ||\n result[fieldName][i].id !== incomingBlock.id ||\n result[fieldName][i].blockType !== incomingBlock.blockType\n ) {\n result[fieldName][i] = {\n blockType: incomingBlock.blockType,\n }\n }\n\n traverseFields({\n externallyUpdatedRelationship,\n fieldSchema: incomingBlockJSON.fields,\n incomingData: incomingBlock,\n localeChanged,\n populationsByCollection,\n result: result[fieldName][i],\n })\n\n return result[fieldName][i]\n })\n } else {\n result[fieldName] = []\n }\n\n break\n\n case 'group':\n // falls through\n case 'tabs':\n if (!result[fieldName]) {\n result[fieldName] = {}\n }\n\n traverseFields({\n externallyUpdatedRelationship,\n fieldSchema: fieldSchema.fields,\n incomingData: incomingData[fieldName] || {},\n localeChanged,\n populationsByCollection,\n result: result[fieldName],\n })\n\n break\n\n case 'relationship':\n // falls through\n case 'upload':\n // Handle `hasMany` relationships\n if (fieldSchema.hasMany && Array.isArray(incomingData[fieldName])) {\n if (!result[fieldName] || !incomingData[fieldName].length) {\n result[fieldName] = []\n }\n\n incomingData[fieldName].forEach((incomingRelation, i) => {\n // Handle `hasMany` polymorphic\n if (Array.isArray(fieldSchema.relationTo)) {\n // if the field doesn't exist on the result, create it\n // the value will be populated later\n if (!result[fieldName][i]) {\n result[fieldName][i] = {\n relationTo: incomingRelation.relationTo,\n }\n }\n\n const oldID = result[fieldName][i]?.value?.id\n const oldRelation = result[fieldName][i]?.relationTo\n const newID = incomingRelation.value\n const newRelation = incomingRelation.relationTo\n\n const hasChanged = newID !== oldID || newRelation !== oldRelation\n\n const hasUpdated =\n newRelation === externallyUpdatedRelationship?.entitySlug &&\n newID === externallyUpdatedRelationship?.id\n\n if (hasChanged || hasUpdated || localeChanged) {\n if (!populationsByCollection[newRelation]) {\n populationsByCollection[newRelation] = []\n }\n\n populationsByCollection[newRelation].push({\n id: incomingRelation.value,\n accessor: 'value',\n ref: result[fieldName][i],\n })\n }\n } else {\n // Handle `hasMany` monomorphic\n const hasChanged = incomingRelation !== result[fieldName][i]?.id\n\n const hasUpdated =\n fieldSchema.relationTo === externallyUpdatedRelationship?.entitySlug &&\n incomingRelation === externallyUpdatedRelationship?.id\n\n if (hasChanged || hasUpdated || localeChanged) {\n if (!populationsByCollection[fieldSchema.relationTo]) {\n populationsByCollection[fieldSchema.relationTo] = []\n }\n\n populationsByCollection[fieldSchema.relationTo].push({\n id: incomingRelation,\n accessor: i,\n ref: result[fieldName],\n })\n }\n }\n })\n } else {\n // Handle `hasOne` polymorphic\n if (Array.isArray(fieldSchema.relationTo)) {\n // if the field doesn't exist on the result, create it\n // the value will be populated later\n if (!result[fieldName]) {\n result[fieldName] = {\n relationTo: incomingData[fieldName]?.relationTo,\n }\n }\n\n const hasNewValue =\n incomingData[fieldName] &&\n typeof incomingData[fieldName] === 'object' &&\n incomingData[fieldName] !== null\n\n const hasOldValue =\n result[fieldName] &&\n typeof result[fieldName] === 'object' &&\n result[fieldName] !== null\n\n const newID = hasNewValue\n ? typeof incomingData[fieldName].value === 'object'\n ? incomingData[fieldName].value.id\n : incomingData[fieldName].value\n : ''\n\n const oldID = hasOldValue\n ? typeof result[fieldName].value === 'object'\n ? result[fieldName].value.id\n : result[fieldName].value\n : ''\n\n const newRelation = hasNewValue ? incomingData[fieldName].relationTo : ''\n const oldRelation = hasOldValue ? result[fieldName].relationTo : ''\n\n const hasChanged = newID !== oldID || newRelation !== oldRelation\n\n const hasUpdated =\n newRelation === externallyUpdatedRelationship?.entitySlug &&\n newID === externallyUpdatedRelationship?.id\n\n // if the new value/relation is different from the old value/relation\n // populate the new value, otherwise leave it alone\n if (hasChanged || hasUpdated || localeChanged) {\n // if the new value is not empty, populate it\n // otherwise set the value to null\n if (newID) {\n if (!populationsByCollection[newRelation]) {\n populationsByCollection[newRelation] = []\n }\n\n populationsByCollection[newRelation].push({\n id: newID,\n accessor: 'value',\n ref: result[fieldName],\n })\n } else {\n result[fieldName] = null\n }\n }\n } else {\n // Handle `hasOne` monomorphic\n const newID: number | string | undefined =\n (incomingData[fieldName] &&\n typeof incomingData[fieldName] === 'object' &&\n incomingData[fieldName].id) ||\n incomingData[fieldName]\n\n const oldID: number | string | undefined =\n (result[fieldName] &&\n typeof result[fieldName] === 'object' &&\n result[fieldName].id) ||\n result[fieldName]\n\n const hasChanged = newID !== oldID\n\n const hasUpdated =\n fieldSchema.relationTo === externallyUpdatedRelationship?.entitySlug &&\n newID === externallyUpdatedRelationship?.id\n\n // if the new value is different from the old value\n // populate the new value, otherwise leave it alone\n if (hasChanged || hasUpdated || localeChanged) {\n // if the new value is not empty, populate it\n // otherwise set the value to null\n if (newID) {\n if (!populationsByCollection[fieldSchema.relationTo]) {\n populationsByCollection[fieldSchema.relationTo] = []\n }\n\n populationsByCollection[fieldSchema.relationTo].push({\n id: newID,\n accessor: fieldName,\n ref: result as Record<string, unknown>,\n })\n } else {\n result[fieldName] = null\n }\n }\n }\n }\n\n break\n case 'richText':\n result[fieldName] = traverseRichText({\n externallyUpdatedRelationship,\n incomingData: incomingData[fieldName],\n populationsByCollection,\n result: result[fieldName],\n })\n\n break\n\n default:\n result[fieldName] = incomingData[fieldName]\n }\n }\n })\n}\n"],"names":["traverseRichText","traverseFields","args","externallyUpdatedRelationship","fieldSchema","fieldSchemas","incomingData","localeChanged","populationsByCollection","result","forEach","name","fieldName","type","undefined","Array","isArray","map","incomingRow","i","fields","incomingBlock","incomingBlockJSON","blocks","blockType","id","hasMany","length","incomingRelation","relationTo","oldID","value","oldRelation","newID","newRelation","hasChanged","hasUpdated","entitySlug","push","accessor","ref","hasNewValue","hasOldValue"],"mappings":"AAKA,SAASA,gBAAgB,QAAQ,wBAAuB;AAExD,OAAO,MAAMC,iBAAiB,CAAIC;IAQhC,MAAM,EACJC,6BAA6B,EAC7BC,aAAaC,YAAY,EACzBC,YAAY,EACZC,aAAa,EACbC,uBAAuB,EACvBC,MAAM,EACP,GAAGP;IAEJG,aAAaK,OAAO,CAAC,CAACN;QACpB,IAAI,UAAUA,eAAe,OAAOA,YAAYO,IAAI,KAAK,UAAU;YACjE,MAAMC,YAAYR,YAAYO,IAAI;YAElC,OAAQP,YAAYS,IAAI;gBACtB,KAAK;oBACH,IACE,CAACP,YAAY,CAACM,UAAU,IACxBN,YAAY,CAACM,UAAU,KAAKE,aAC5BL,QAAQ,CAACG,UAAU,KAAKE,WACxB;wBACAL,MAAM,CAACG,UAAU,GAAG,EAAE;oBACxB;oBAEA,IAAIG,MAAMC,OAAO,CAACV,YAAY,CAACM,UAAU,GAAG;wBAC1CH,MAAM,CAACG,UAAU,GAAGN,YAAY,CAACM,UAAU,CAACK,GAAG,CAAC,CAACC,aAAaC;4BAC5D,IAAI,CAACV,MAAM,CAACG,UAAU,EAAE;gCACtBH,MAAM,CAACG,UAAU,GAAG,EAAE;4BACxB;4BAEA,IAAI,CAACH,MAAM,CAACG,UAAU,CAACO,EAAE,EAAE;gCACzBV,MAAM,CAACG,UAAU,CAACO,EAAE,GAAG,CAAC;4BAC1B;4BAEAlB,eAAe;gCACbE;gCACAC,aAAaA,YAAYgB,MAAM;gCAC/Bd,cAAcY;gCACdX;gCACAC;gCACAC,QAAQA,MAAM,CAACG,UAAU,CAACO,EAAE;4BAC9B;4BAEA,OAAOV,MAAM,CAACG,UAAU,CAACO,EAAE;wBAC7B;oBACF;oBAEA;gBAEF,KAAK;oBAC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DrB,uBAAuB,CAACJ,YAAYyB,UAAU,CAAC,GAAG,EAAE;oCACtD;oCAEArB,uBAAuB,CAACJ,YAAYyB,UAAU,CAAC,CAACS,IAAI,CAAC;wCACnDb,IAAIQ;wCACJM,UAAU3B;wCACV4B,KAAK/B;oCACP;gCACF,OAAO;oCACLA,MAAM,CAACG,UAAU,GAAG;gCACtB;4BACF;wBACF;oBACF;oBAEA;gBACF,KAAK;oBACHH,MAAM,CAACG,UAAU,GAAGZ,iBAAiB;wBACnCG;wBACAG,cAAcA,YAAY,CAACM,UAAU;wBACrCJ;wBACAC,QAAQA,MAAM,CAACG,UAAU;oBAC3B;oBAEA;gBAEF;oBACEH,MAAM,CAACG,UAAU,GAAGN,YAAY,CAACM,UAAU;YAC/C;QACF;IACF;AACF,EAAC"}
|
|
1
|
+
{"version":3,"sources":["../src/traverseFields.ts"],"sourcesContent":["import type { DocumentEvent, FieldSchemaJSON } from 'payload'\n\nimport type { PopulationsByCollection } from './types.js'\n\nimport { traverseRichText } from './traverseRichText.js'\n\nexport const traverseFields = <T extends Record<string, any>>(args: {\n externallyUpdatedRelationship?: DocumentEvent\n fieldSchema: FieldSchemaJSON\n incomingData: T\n localeChanged: boolean\n populationsByCollection: PopulationsByCollection\n result: Record<string, any>\n}): void => {\n const {\n externallyUpdatedRelationship,\n fieldSchema: fieldSchemas,\n incomingData,\n localeChanged,\n populationsByCollection,\n result,\n } = args\n\n fieldSchemas.forEach((fieldSchema) => {\n if ('name' in fieldSchema && typeof fieldSchema.name === 'string') {\n const fieldName = fieldSchema.name\n\n switch (fieldSchema.type) {\n case 'array':\n if (\n !incomingData[fieldName] &&\n incomingData[fieldName] !== undefined &&\n result?.[fieldName] !== undefined\n ) {\n result[fieldName] = []\n }\n\n if (Array.isArray(incomingData[fieldName])) {\n result[fieldName] = incomingData[fieldName].map((incomingRow, i) => {\n if (!result[fieldName]) {\n result[fieldName] = []\n }\n\n if (!result[fieldName][i]) {\n result[fieldName][i] = {}\n }\n\n traverseFields({\n externallyUpdatedRelationship,\n fieldSchema: fieldSchema.fields!,\n incomingData: incomingRow,\n localeChanged,\n populationsByCollection,\n result: result[fieldName][i],\n })\n\n return result[fieldName][i]\n })\n }\n\n break\n\n case 'blocks':\n if (Array.isArray(incomingData[fieldName])) {\n result[fieldName] = incomingData[fieldName].map((incomingBlock, i) => {\n const incomingBlockJSON = fieldSchema.blocks?.[incomingBlock.blockType]\n\n if (!result[fieldName]) {\n result[fieldName] = []\n }\n\n if (\n !result[fieldName][i] ||\n result[fieldName][i].id !== incomingBlock.id ||\n result[fieldName][i].blockType !== incomingBlock.blockType\n ) {\n result[fieldName][i] = {\n blockType: incomingBlock.blockType,\n }\n }\n\n traverseFields({\n externallyUpdatedRelationship,\n fieldSchema: incomingBlockJSON!.fields!,\n incomingData: incomingBlock,\n localeChanged,\n populationsByCollection,\n result: result[fieldName][i],\n })\n\n return result[fieldName][i]\n })\n } else {\n result[fieldName] = []\n }\n\n break\n\n case 'group':\n // falls through\n case 'tabs':\n if (!result[fieldName]) {\n result[fieldName] = {}\n }\n\n traverseFields({\n externallyUpdatedRelationship,\n fieldSchema: fieldSchema.fields!,\n incomingData: incomingData[fieldName] || {},\n localeChanged,\n populationsByCollection,\n result: result[fieldName],\n })\n\n break\n\n case 'relationship':\n // falls through\n case 'upload':\n // Handle `hasMany` relationships\n if (fieldSchema.hasMany && Array.isArray(incomingData[fieldName])) {\n if (!result[fieldName] || !incomingData[fieldName].length) {\n result[fieldName] = []\n }\n\n incomingData[fieldName].forEach((incomingRelation, i) => {\n // Handle `hasMany` polymorphic\n if (Array.isArray(fieldSchema.relationTo)) {\n // if the field doesn't exist on the result, create it\n // the value will be populated later\n if (!result[fieldName][i]) {\n result[fieldName][i] = {\n relationTo: incomingRelation.relationTo,\n }\n }\n\n const oldID = result[fieldName][i]?.value?.id\n const oldRelation = result[fieldName][i]?.relationTo\n const newID = incomingRelation.value\n const newRelation = incomingRelation.relationTo\n\n const hasChanged = newID !== oldID || newRelation !== oldRelation\n\n const hasUpdated =\n newRelation === externallyUpdatedRelationship?.entitySlug &&\n newID === externallyUpdatedRelationship?.id\n\n if (hasChanged || hasUpdated || localeChanged) {\n if (!populationsByCollection[newRelation]) {\n populationsByCollection[newRelation] = []\n }\n\n populationsByCollection[newRelation].push({\n id: incomingRelation.value,\n accessor: 'value',\n ref: result[fieldName][i],\n })\n }\n } else {\n // Handle `hasMany` monomorphic\n const hasChanged = incomingRelation !== result[fieldName][i]?.id\n\n const hasUpdated =\n fieldSchema.relationTo === externallyUpdatedRelationship?.entitySlug &&\n incomingRelation === externallyUpdatedRelationship?.id\n\n if (hasChanged || hasUpdated || localeChanged) {\n if (!populationsByCollection[fieldSchema.relationTo!]) {\n populationsByCollection[fieldSchema.relationTo!] = []\n }\n\n populationsByCollection[fieldSchema.relationTo!]?.push({\n id: incomingRelation,\n accessor: i,\n ref: result[fieldName],\n })\n }\n }\n })\n } else {\n // Handle `hasOne` polymorphic\n if (Array.isArray(fieldSchema.relationTo)) {\n // if the field doesn't exist on the result, create it\n // the value will be populated later\n if (!result[fieldName]) {\n result[fieldName] = {\n relationTo: incomingData[fieldName]?.relationTo,\n }\n }\n\n const hasNewValue =\n incomingData[fieldName] &&\n typeof incomingData[fieldName] === 'object' &&\n incomingData[fieldName] !== null\n\n const hasOldValue =\n result[fieldName] &&\n typeof result[fieldName] === 'object' &&\n result[fieldName] !== null\n\n const newID = hasNewValue\n ? typeof incomingData[fieldName].value === 'object'\n ? incomingData[fieldName].value.id\n : incomingData[fieldName].value\n : ''\n\n const oldID = hasOldValue\n ? typeof result[fieldName].value === 'object'\n ? result[fieldName].value.id\n : result[fieldName].value\n : ''\n\n const newRelation = hasNewValue ? incomingData[fieldName].relationTo : ''\n const oldRelation = hasOldValue ? result[fieldName].relationTo : ''\n\n const hasChanged = newID !== oldID || newRelation !== oldRelation\n\n const hasUpdated =\n newRelation === externallyUpdatedRelationship?.entitySlug &&\n newID === externallyUpdatedRelationship?.id\n\n // if the new value/relation is different from the old value/relation\n // populate the new value, otherwise leave it alone\n if (hasChanged || hasUpdated || localeChanged) {\n // if the new value is not empty, populate it\n // otherwise set the value to null\n if (newID) {\n if (!populationsByCollection[newRelation]) {\n populationsByCollection[newRelation] = []\n }\n\n populationsByCollection[newRelation].push({\n id: newID,\n accessor: 'value',\n ref: result[fieldName],\n })\n } else {\n result[fieldName] = null\n }\n }\n } else {\n // Handle `hasOne` monomorphic\n const newID: number | string | undefined =\n (incomingData[fieldName] &&\n typeof incomingData[fieldName] === 'object' &&\n incomingData[fieldName].id) ||\n incomingData[fieldName]\n\n const oldID: number | string | undefined =\n (result[fieldName] &&\n typeof result[fieldName] === 'object' &&\n result[fieldName].id) ||\n result[fieldName]\n\n const hasChanged = newID !== oldID\n\n const hasUpdated =\n fieldSchema.relationTo === externallyUpdatedRelationship?.entitySlug &&\n newID === externallyUpdatedRelationship?.id\n\n // if the new value is different from the old value\n // populate the new value, otherwise leave it alone\n if (hasChanged || hasUpdated || localeChanged) {\n // if the new value is not empty, populate it\n // otherwise set the value to null\n if (newID) {\n if (!populationsByCollection[fieldSchema.relationTo!]) {\n populationsByCollection[fieldSchema.relationTo!] = []\n }\n\n populationsByCollection[fieldSchema.relationTo!]?.push({\n id: newID,\n accessor: fieldName,\n ref: result as Record<string, unknown>,\n })\n } else {\n result[fieldName] = null\n }\n }\n }\n }\n\n break\n case 'richText':\n result[fieldName] = traverseRichText({\n externallyUpdatedRelationship,\n incomingData: incomingData[fieldName],\n populationsByCollection,\n result: result[fieldName],\n })\n\n break\n\n default:\n result[fieldName] = incomingData[fieldName]\n }\n }\n })\n}\n"],"names":["traverseRichText","traverseFields","args","externallyUpdatedRelationship","fieldSchema","fieldSchemas","incomingData","localeChanged","populationsByCollection","result","forEach","name","fieldName","type","undefined","Array","isArray","map","incomingRow","i","fields","incomingBlock","incomingBlockJSON","blocks","blockType","id","hasMany","length","incomingRelation","relationTo","oldID","value","oldRelation","newID","newRelation","hasChanged","hasUpdated","entitySlug","push","accessor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Q;wCACJM,UAAU3B;wCACV4B,KAAK/B;oCACP;gCACF,OAAO;oCACLA,MAAM,CAACG,UAAU,GAAG;gCACtB;4BACF;wBACF;oBACF;oBAEA;gBACF,KAAK;oBACHH,MAAM,CAACG,UAAU,GAAGZ,iBAAiB;wBACnCG;wBACAG,cAAcA,YAAY,CAACM,UAAU;wBACrCJ;wBACAC,QAAQA,MAAM,CAACG,UAAU;oBAC3B;oBAEA;gBAEF;oBACEH,MAAM,CAACG,UAAU,GAAGN,YAAY,CAACM,UAAU;YAC/C;QACF;IACF;AACF,EAAC"}
|
package/dist/traverseRichText.js
CHANGED
|
@@ -43,7 +43,7 @@ export const traverseRichText = ({ externallyUpdatedRelationship, incomingData,
|
|
|
43
43
|
if (!populationsByCollection[incomingData.relationTo]) {
|
|
44
44
|
populationsByCollection[incomingData.relationTo] = [];
|
|
45
45
|
}
|
|
46
|
-
populationsByCollection[incomingData.relationTo]
|
|
46
|
+
populationsByCollection[incomingData.relationTo]?.push({
|
|
47
47
|
id: incomingData[key] && typeof incomingData[key] === 'object' ? incomingData[key].id : incomingData[key],
|
|
48
48
|
accessor: 'value',
|
|
49
49
|
ref: result
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/traverseRichText.ts"],"sourcesContent":["import type { DocumentEvent } from 'payload'\n\nimport type { PopulationsByCollection } from './types.js'\n\nexport const traverseRichText = ({\n externallyUpdatedRelationship,\n incomingData,\n populationsByCollection,\n result,\n}: {\n externallyUpdatedRelationship?: DocumentEvent\n incomingData: any\n populationsByCollection: PopulationsByCollection\n result: any\n}): any => {\n if (Array.isArray(incomingData)) {\n if (!result) {\n result = []\n }\n\n result = incomingData.map((item, index) => {\n if (!result[index]) {\n result[index] = item\n }\n\n return traverseRichText({\n externallyUpdatedRelationship,\n incomingData: item,\n populationsByCollection,\n result: result[index],\n })\n })\n } else if (incomingData && typeof incomingData === 'object') {\n if (!result) {\n result = {}\n }\n\n // Remove keys from `result` that do not appear in `incomingData`\n // There's likely another way to do this,\n // But recursion and references make this very difficult\n Object.keys(result).forEach((key) => {\n if (!(key in incomingData)) {\n delete result[key]\n }\n })\n\n // Iterate over the keys of `incomingData` and populate `result`\n Object.keys(incomingData).forEach((key) => {\n if (!result[key]) {\n // Instantiate the key in `result` if it doesn't exist\n // Ensure its type matches the type of the `incomingData`\n // We don't have a schema to check against here\n result[key] =\n incomingData[key] && typeof incomingData[key] === 'object'\n ? Array.isArray(incomingData[key])\n ? []\n : {}\n : undefined\n }\n\n const isRelationship = key === 'value' && 'relationTo' in incomingData\n\n if (isRelationship) {\n // or if there are no keys besides id\n const needsPopulation =\n !result.value ||\n typeof result.value !== 'object' ||\n (typeof result.value === 'object' &&\n Object.keys(result.value).length === 1 &&\n 'id' in result.value)\n\n const hasChanged =\n result &&\n typeof result === 'object' &&\n result.value.id === externallyUpdatedRelationship?.id\n\n if (needsPopulation || hasChanged) {\n if (!populationsByCollection[incomingData.relationTo]) {\n populationsByCollection[incomingData.relationTo] = []\n }\n\n populationsByCollection[incomingData.relationTo]
|
|
1
|
+
{"version":3,"sources":["../src/traverseRichText.ts"],"sourcesContent":["import type { DocumentEvent } from 'payload'\n\nimport type { PopulationsByCollection } from './types.js'\n\nexport const traverseRichText = ({\n externallyUpdatedRelationship,\n incomingData,\n populationsByCollection,\n result,\n}: {\n externallyUpdatedRelationship?: DocumentEvent\n incomingData: any\n populationsByCollection: PopulationsByCollection\n result: any\n}): any => {\n if (Array.isArray(incomingData)) {\n if (!result) {\n result = []\n }\n\n result = incomingData.map((item, index) => {\n if (!result[index]) {\n result[index] = item\n }\n\n return traverseRichText({\n externallyUpdatedRelationship,\n incomingData: item,\n populationsByCollection,\n result: result[index],\n })\n })\n } else if (incomingData && typeof incomingData === 'object') {\n if (!result) {\n result = {}\n }\n\n // Remove keys from `result` that do not appear in `incomingData`\n // There's likely another way to do this,\n // But recursion and references make this very difficult\n Object.keys(result).forEach((key) => {\n if (!(key in incomingData)) {\n delete result[key]\n }\n })\n\n // Iterate over the keys of `incomingData` and populate `result`\n Object.keys(incomingData).forEach((key) => {\n if (!result[key]) {\n // Instantiate the key in `result` if it doesn't exist\n // Ensure its type matches the type of the `incomingData`\n // We don't have a schema to check against here\n result[key] =\n incomingData[key] && typeof incomingData[key] === 'object'\n ? Array.isArray(incomingData[key])\n ? []\n : {}\n : undefined\n }\n\n const isRelationship = key === 'value' && 'relationTo' in incomingData\n\n if (isRelationship) {\n // or if there are no keys besides id\n const needsPopulation =\n !result.value ||\n typeof result.value !== 'object' ||\n (typeof result.value === 'object' &&\n Object.keys(result.value).length === 1 &&\n 'id' in result.value)\n\n const hasChanged =\n result &&\n typeof result === 'object' &&\n result.value.id === externallyUpdatedRelationship?.id\n\n if (needsPopulation || hasChanged) {\n if (!populationsByCollection[incomingData.relationTo]) {\n populationsByCollection[incomingData.relationTo] = []\n }\n\n populationsByCollection[incomingData.relationTo]?.push({\n id:\n incomingData[key] && typeof incomingData[key] === 'object'\n ? incomingData[key].id\n : incomingData[key],\n accessor: 'value',\n ref: result,\n })\n }\n } else {\n result[key] = traverseRichText({\n externallyUpdatedRelationship,\n incomingData: incomingData[key],\n populationsByCollection,\n result: result[key],\n })\n }\n })\n } else {\n result = incomingData\n }\n\n return result\n}\n"],"names":["traverseRichText","externallyUpdatedRelationship","incomingData","populationsByCollection","result","Array","isArray","map","item","index","Object","keys","forEach","key","undefined","isRelationship","needsPopulation","value","length","hasChanged","id","relationTo","push","accessor","ref"],"mappings":"AAIA,OAAO,MAAMA,mBAAmB,CAAC,EAC/BC,6BAA6B,EAC7BC,YAAY,EACZC,uBAAuB,EACvBC,MAAM,EAMP;IACC,IAAIC,MAAMC,OAAO,CAACJ,eAAe;QAC/B,IAAI,CAACE,QAAQ;YACXA,SAAS,EAAE;QACb;QAEAA,SAASF,aAAaK,GAAG,CAAC,CAACC,MAAMC;YAC/B,IAAI,CAACL,MAAM,CAACK,MAAM,EAAE;gBAClBL,MAAM,CAACK,MAAM,GAAGD;YAClB;YAEA,O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AuB,CAACD,aAAamB,UAAU,CAAC,EAAEC,KAAK;wBACrDF,IACElB,YAAY,CAACW,IAAI,IAAI,OAAOX,YAAY,CAACW,IAAI,KAAK,WAC9CX,YAAY,CAACW,IAAI,CAACO,EAAE,GACpBlB,YAAY,CAACW,IAAI;wBACvBU,UAAU;wBACVC,KAAKpB;oBACP;gBACF;YACF,OAAO;gBACLA,MAAM,CAACS,IAAI,GAAGb,iBAAiB;oBAC7BC;oBACAC,cAAcA,YAAY,CAACW,IAAI;oBAC/BV;oBACAC,QAAQA,MAAM,CAACS,IAAI;gBACrB;YACF;QACF;IACF,OAAO;QACLT,SAASF;IACX;IAEA,OAAOE;AACT,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/live-preview",
|
|
3
|
-
"version": "3.32.0
|
|
3
|
+
"version": "3.32.0",
|
|
4
4
|
"description": "The official live preview JavaScript SDK for Payload",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
],
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@payloadcms/eslint-config": "3.28.0",
|
|
35
|
-
"payload": "3.32.0
|
|
35
|
+
"payload": "3.32.0"
|
|
36
36
|
},
|
|
37
37
|
"publishConfig": {
|
|
38
38
|
"registry": "https://registry.npmjs.org/"
|