@payloadcms/live-preview
Advanced tools
Comparing version 3.11.1-canary.3f6217a to 3.11.1-canary.54f6c59
@@ -0,5 +1,6 @@ | ||
import type { LivePreviewMessageEvent } from './types.js'; | ||
export declare const handleMessage: <T>(args: { | ||
apiRoute?: string; | ||
depth?: number; | ||
event: MessageEvent; | ||
event: LivePreviewMessageEvent<T>; | ||
initialData: T; | ||
@@ -6,0 +7,0 @@ serverURL: string; |
import { isLivePreviewEvent } from './isLivePreviewEvent.js'; | ||
import { mergeData } from './mergeData.js'; | ||
// For performance reasons, `fieldSchemaJSON` will only be sent once on the initial message | ||
// We need to cache this value so that it can be used across subsequent messages | ||
// To do this, save `fieldSchemaJSON` when it arrives as a global variable | ||
// Send this cached value to `mergeData`, instead of `eventData.fieldSchemaJSON` directly | ||
let payloadLivePreviewFieldSchema = undefined // TODO: type this from `fieldSchemaToJSON` return type | ||
; | ||
// Each time the data is merged, cache the result as a `previousData` variable | ||
// This will ensure changes compound overtop of each other | ||
let payloadLivePreviewPreviousData = undefined; | ||
const _payloadLivePreview = { | ||
/** | ||
* For performance reasons, `fieldSchemaJSON` will only be sent once on the initial message | ||
* We need to cache this value so that it can be used across subsequent messages | ||
* To do this, save `fieldSchemaJSON` when it arrives as a global variable | ||
* Send this cached value to `mergeData`, instead of `eventData.fieldSchemaJSON` directly | ||
*/ fieldSchema: undefined, | ||
/** | ||
* Each time the data is merged, cache the result as a `previousData` variable | ||
* This will ensure changes compound overtop of each other | ||
*/ previousData: undefined | ||
}; | ||
export const handleMessage = async (args)=>{ | ||
const { apiRoute, depth, event, initialData, serverURL } = args; | ||
if (isLivePreviewEvent(event, serverURL)) { | ||
const { data, externallyUpdatedRelationship, fieldSchemaJSON } = event.data; | ||
if (!payloadLivePreviewFieldSchema && fieldSchemaJSON) { | ||
payloadLivePreviewFieldSchema = fieldSchemaJSON; | ||
const { data, externallyUpdatedRelationship, fieldSchemaJSON, locale } = event.data; | ||
if (!_payloadLivePreview?.fieldSchema && fieldSchemaJSON) { | ||
_payloadLivePreview.fieldSchema = fieldSchemaJSON; | ||
} | ||
if (!payloadLivePreviewFieldSchema) { | ||
if (!_payloadLivePreview?.fieldSchema) { | ||
// eslint-disable-next-line no-console | ||
@@ -28,9 +31,9 @@ console.warn('Payload Live Preview: No `fieldSchemaJSON` was received from the parent window. Unable to merge data.'); | ||
externallyUpdatedRelationship, | ||
fieldSchema: payloadLivePreviewFieldSchema, | ||
fieldSchema: _payloadLivePreview.fieldSchema, | ||
incomingData: data, | ||
initialData: payloadLivePreviewPreviousData || initialData, | ||
locale: event.data.locale, | ||
initialData: _payloadLivePreview?.previousData || initialData, | ||
locale, | ||
serverURL | ||
}); | ||
payloadLivePreviewPreviousData = mergedData; | ||
_payloadLivePreview.previousData = mergedData; | ||
return mergedData; | ||
@@ -37,0 +40,0 @@ } |
@@ -8,3 +8,4 @@ export { handleMessage } from './handleMessage.js'; | ||
export { traverseRichText } from './traverseRichText.js'; | ||
export type { LivePreviewMessageEvent } from './types.js'; | ||
export { unsubscribe } from './unsubscribe.js'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,3 +0,2 @@ | ||
import type { fieldSchemaToJSON } from 'payload/shared'; | ||
import type { UpdatedDocument } from './types.js'; | ||
import type { DocumentEvent, FieldSchemaJSON } from 'payload'; | ||
export declare const mergeData: <T>(args: { | ||
@@ -11,4 +10,4 @@ apiRoute?: string; | ||
depth?: number; | ||
externallyUpdatedRelationship?: UpdatedDocument; | ||
fieldSchema: ReturnType<typeof fieldSchemaToJSON>; | ||
externallyUpdatedRelationship?: DocumentEvent; | ||
fieldSchema: FieldSchemaJSON; | ||
incomingData: Partial<T>; | ||
@@ -15,0 +14,0 @@ initialData: T; |
@@ -0,5 +1,6 @@ | ||
import type { DocumentEvent } from 'payload'; | ||
import type { fieldSchemaToJSON } from 'payload/shared'; | ||
import type { PopulationsByCollection, UpdatedDocument } from './types.js'; | ||
import type { PopulationsByCollection } from './types.js'; | ||
export declare const traverseFields: <T>(args: { | ||
externallyUpdatedRelationship?: UpdatedDocument; | ||
externallyUpdatedRelationship?: DocumentEvent; | ||
fieldSchema: ReturnType<typeof fieldSchemaToJSON>; | ||
@@ -6,0 +7,0 @@ incomingData: T; |
@@ -1,4 +0,5 @@ | ||
import type { PopulationsByCollection, UpdatedDocument } from './types.js'; | ||
import type { DocumentEvent } from 'payload'; | ||
import type { PopulationsByCollection } from './types.js'; | ||
export declare const traverseRichText: ({ externallyUpdatedRelationship, incomingData, populationsByCollection, result, }: { | ||
externallyUpdatedRelationship?: UpdatedDocument; | ||
externallyUpdatedRelationship?: DocumentEvent; | ||
incomingData: any; | ||
@@ -5,0 +6,0 @@ populationsByCollection: PopulationsByCollection; |
@@ -0,1 +1,2 @@ | ||
import type { DocumentEvent, FieldSchemaJSON } from 'payload'; | ||
export type LivePreviewArgs = {}; | ||
@@ -10,7 +11,9 @@ export type LivePreview = void; | ||
}; | ||
export type UpdatedDocument = { | ||
entitySlug: string; | ||
id?: number | string; | ||
updatedAt: string; | ||
}; | ||
export type LivePreviewMessageEvent<T> = MessageEvent<{ | ||
data: T; | ||
externallyUpdatedRelationship?: DocumentEvent; | ||
fieldSchemaJSON: FieldSchemaJSON; | ||
locale?: string; | ||
type: 'payload-live-preview'; | ||
}>; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -1,4 +0,3 @@ | ||
// TODO: import this from `payload/admin/components/utilities/DocumentEvents/types.ts` | ||
export { }; | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "@payloadcms/live-preview", | ||
"version": "3.11.1-canary.3f6217a", | ||
"version": "3.11.1-canary.54f6c59", | ||
"description": "The official live preview JavaScript SDK for Payload", | ||
@@ -35,3 +35,3 @@ "homepage": "https://payloadcms.com", | ||
"@payloadcms/eslint-config": "3.9.0", | ||
"payload": "3.11.1-canary.3f6217a" | ||
"payload": "3.11.1-canary.54f6c59" | ||
}, | ||
@@ -38,0 +38,0 @@ "publishConfig": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68206
497