@payloadcms/ui 3.80.0-internal.cee0ccf → 3.80.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/elements/BulkUpload/FormsManager/index.d.ts.map +1 -1
- package/dist/elements/BulkUpload/FormsManager/index.js +29 -7
- package/dist/elements/BulkUpload/FormsManager/index.js.map +1 -1
- package/dist/elements/ClipboardAction/mergeFormStateFromClipboard.d.ts.map +1 -1
- package/dist/elements/ClipboardAction/mergeFormStateFromClipboard.js +53 -3
- package/dist/elements/ClipboardAction/mergeFormStateFromClipboard.js.map +1 -1
- package/dist/elements/ClipboardAction/mergeFormStateFromClipboard.spec.js +541 -0
- package/dist/elements/ClipboardAction/mergeFormStateFromClipboard.spec.js.map +1 -0
- package/dist/elements/Combobox/index.d.ts.map +1 -1
- package/dist/elements/Combobox/index.js +1 -0
- package/dist/elements/Combobox/index.js.map +1 -1
- package/dist/elements/Link/index.d.ts.map +1 -1
- package/dist/elements/LivePreview/Window/index.d.ts.map +1 -1
- package/dist/elements/LivePreview/Window/index.js +9 -7
- package/dist/elements/LivePreview/Window/index.js.map +1 -1
- package/dist/elements/PublishButton/index.d.ts.map +1 -1
- package/dist/elements/PublishButton/index.js +3 -1
- package/dist/elements/PublishButton/index.js.map +1 -1
- package/dist/elements/RelationshipTable/index.d.ts.map +1 -1
- package/dist/elements/RelationshipTable/index.js +14 -9
- package/dist/elements/RelationshipTable/index.js.map +1 -1
- package/dist/elements/Toasts/fieldErrors.d.ts +4 -0
- package/dist/elements/Toasts/fieldErrors.d.ts.map +1 -1
- package/dist/elements/Toasts/fieldErrors.js +4 -2
- package/dist/elements/Toasts/fieldErrors.js.map +1 -1
- package/dist/elements/Toasts/fieldErrors.spec.js +46 -0
- package/dist/elements/Toasts/fieldErrors.spec.js.map +1 -0
- package/dist/elements/WhereBuilder/Condition/Relationship/index.d.ts.map +1 -1
- package/dist/elements/WhereBuilder/Condition/Relationship/index.js +8 -6
- package/dist/elements/WhereBuilder/Condition/Relationship/index.js.map +1 -1
- package/dist/elements/WhereBuilder/field-types.d.ts.map +1 -1
- package/dist/elements/WhereBuilder/field-types.js +2 -5
- package/dist/elements/WhereBuilder/field-types.js.map +1 -1
- package/dist/exports/client/{DatePicker-CL2EGBVQ.js → DatePicker-T2DMDMM5.js} +2 -2
- package/dist/exports/client/chunk-WDZJLNNB.js +29 -0
- package/dist/exports/client/{chunk-SH42NW5R.js.map → chunk-WDZJLNNB.js.map} +4 -4
- package/dist/exports/client/index.d.ts +1 -1
- package/dist/exports/client/index.d.ts.map +1 -1
- package/dist/exports/client/index.js +24 -24
- package/dist/exports/client/index.js.map +4 -4
- package/dist/fields/Blocks/BlockSelector/index.scss +4 -4
- package/dist/fields/shared/index.d.ts +2 -2
- package/dist/fields/shared/index.d.ts.map +1 -1
- package/dist/fields/shared/index.js.map +1 -1
- package/dist/forms/Form/mergeServerFormState.d.ts.map +1 -1
- package/dist/forms/Form/mergeServerFormState.js +101 -24
- package/dist/forms/Form/mergeServerFormState.js.map +1 -1
- package/dist/providers/LivePreview/context.d.ts +6 -0
- package/dist/providers/LivePreview/context.d.ts.map +1 -1
- package/dist/providers/LivePreview/context.js +1 -0
- package/dist/providers/LivePreview/context.js.map +1 -1
- package/dist/providers/LivePreview/index.d.ts.map +1 -1
- package/dist/providers/LivePreview/index.js +13 -1
- package/dist/providers/LivePreview/index.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/views/Edit/index.d.ts.map +1 -1
- package/dist/views/Edit/index.js +15 -2
- package/dist/views/Edit/index.js.map +1 -1
- package/package.json +4 -4
- package/dist/exports/client/chunk-SH42NW5R.js +0 -29
- /package/dist/exports/client/{DatePicker-CL2EGBVQ.js.map → DatePicker-T2DMDMM5.js.map} +0 -0
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
padding: 0;
|
|
12
12
|
list-style: none;
|
|
13
13
|
display: grid;
|
|
14
|
-
grid-template-columns: repeat(6, 1fr);
|
|
14
|
+
grid-template-columns: repeat(6, minmax(0, 1fr));
|
|
15
15
|
gap: base(1);
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
|
|
60
60
|
@include large-break {
|
|
61
61
|
&__blocks {
|
|
62
|
-
grid-template-columns: repeat(5, 1fr);
|
|
62
|
+
grid-template-columns: repeat(5, minmax(0, 1fr));
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
&__blocks {
|
|
72
|
-
grid-template-columns: repeat(3, 1fr);
|
|
72
|
+
grid-template-columns: repeat(3, minmax(0, 1fr));
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
&__block-groups {
|
|
@@ -87,7 +87,7 @@
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
&__blocks {
|
|
90
|
-
grid-template-columns: repeat(2, 1fr);
|
|
90
|
+
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
&__block-groups {
|
|
@@ -6,8 +6,8 @@ export declare const fieldBaseClass = "field-type";
|
|
|
6
6
|
* @returns Whether the field should be displayed as RTL.
|
|
7
7
|
*/
|
|
8
8
|
export declare function isFieldRTL({ fieldLocalized, fieldRTL, locale, localizationConfig, }: {
|
|
9
|
-
fieldLocalized
|
|
10
|
-
fieldRTL
|
|
9
|
+
fieldLocalized?: boolean;
|
|
10
|
+
fieldRTL?: boolean;
|
|
11
11
|
locale: Locale;
|
|
12
12
|
localizationConfig?: SanitizedLocalizationConfig;
|
|
13
13
|
}): boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/fields/shared/index.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,2BAA2B,EAAE,MAAM,SAAS,CAAA;AAElE,eAAO,MAAM,cAAc,eAAe,CAAA;AAE1C;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,EACzB,cAAc,EACd,QAAQ,EACR,MAAM,EACN,kBAAkB,GACnB,EAAE;IACD,cAAc,EAAE,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/fields/shared/index.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,2BAA2B,EAAE,MAAM,SAAS,CAAA;AAElE,eAAO,MAAM,cAAc,eAAe,CAAA;AAE1C;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,EACzB,cAAc,EACd,QAAQ,EACR,MAAM,EACN,kBAAkB,GACnB,EAAE;IACD,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,MAAM,EAAE,MAAM,CAAA;IACd,kBAAkB,CAAC,EAAE,2BAA2B,CAAA;CACjD,WAiBA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["fieldBaseClass","isFieldRTL","fieldLocalized","fieldRTL","locale","localizationConfig","hasMultipleLocales","locales","length","isCurrentLocaleDefaultLocale","code","defaultLocale","rtl"],"sources":["../../../src/fields/shared/index.tsx"],"sourcesContent":["'use client'\nimport type { Locale, SanitizedLocalizationConfig } from 'payload'\n\nexport const fieldBaseClass = 'field-type'\n\n/**\n * Determines whether a field should be displayed as right-to-left (RTL) based on its configuration, payload's localization configuration and the adming user's currently enabled locale.\n\n * @returns Whether the field should be displayed as RTL.\n */\nexport function isFieldRTL({\n fieldLocalized,\n fieldRTL,\n locale,\n localizationConfig,\n}: {\n fieldLocalized
|
|
1
|
+
{"version":3,"file":"index.js","names":["fieldBaseClass","isFieldRTL","fieldLocalized","fieldRTL","locale","localizationConfig","hasMultipleLocales","locales","length","isCurrentLocaleDefaultLocale","code","defaultLocale","rtl"],"sources":["../../../src/fields/shared/index.tsx"],"sourcesContent":["'use client'\nimport type { Locale, SanitizedLocalizationConfig } from 'payload'\n\nexport const fieldBaseClass = 'field-type'\n\n/**\n * Determines whether a field should be displayed as right-to-left (RTL) based on its configuration, payload's localization configuration and the adming user's currently enabled locale.\n\n * @returns Whether the field should be displayed as RTL.\n */\nexport function isFieldRTL({\n fieldLocalized,\n fieldRTL,\n locale,\n localizationConfig,\n}: {\n fieldLocalized?: boolean\n fieldRTL?: boolean\n locale: Locale\n localizationConfig?: SanitizedLocalizationConfig\n}) {\n const hasMultipleLocales =\n locale &&\n localizationConfig &&\n localizationConfig.locales &&\n localizationConfig.locales.length > 1\n\n const isCurrentLocaleDefaultLocale = locale?.code === localizationConfig?.defaultLocale\n\n return (\n (fieldRTL !== false &&\n locale?.rtl === true &&\n (fieldLocalized ||\n (!fieldLocalized && !hasMultipleLocales) || // If there is only one locale which is also rtl, that field is rtl too\n (!fieldLocalized && isCurrentLocaleDefaultLocale))) || // If the current locale is the default locale, but the field is not localized, that field is rtl too\n fieldRTL === true\n ) // If fieldRTL is true. This should be useful for when no localization is set at all in the payload config, but you still want fields to be rtl.\n}\n"],"mappings":"AAAA;;AAGA,OAAO,MAAMA,cAAA,GAAiB;AAE9B;;;;;AAKA,OAAO,SAASC,WAAW;EACzBC,cAAc;EACdC,QAAQ;EACRC,MAAM;EACNC;AAAkB,CAMnB;EACC,MAAMC,kBAAA,GACJF,MAAA,IACAC,kBAAA,IACAA,kBAAA,CAAmBE,OAAO,IAC1BF,kBAAA,CAAmBE,OAAO,CAACC,MAAM,GAAG;EAEtC,MAAMC,4BAAA,GAA+BL,MAAA,EAAQM,IAAA,KAASL,kBAAA,EAAoBM,aAAA;EAE1E,OACER,QAAC,KAAa,SACZC,MAAA,EAAQQ,GAAA,KAAQ,SACfV,cAAA,IACE,CAACA,cAAA,IAAkB,CAACI,kBAAA;EAAuB;EAC3C,CAACJ,cAAA,IAAkBO,4BAA4B;EAAO;EAC3DN,QAAA,KAAa,KACb;AAAA;AACJ","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mergeServerFormState.d.ts","sourceRoot":"","sources":["../../../src/forms/Form/mergeServerFormState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAIxC;;;GAGG;AACH,MAAM,MAAM,YAAY,GACpB;IACE;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B,GACD,OAAO,CAAA;AAEX,KAAK,IAAI,GAAG;IACV,YAAY,CAAC,EAAE,YAAY,CAAA;IAC3B,YAAY,CAAC,EAAE,SAAS,CAAA;IACxB,aAAa,EAAE,SAAS,CAAA;CACzB,CAAA;
|
|
1
|
+
{"version":3,"file":"mergeServerFormState.d.ts","sourceRoot":"","sources":["../../../src/forms/Form/mergeServerFormState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAIxC;;;GAGG;AACH,MAAM,MAAM,YAAY,GACpB;IACE;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B,GACD,OAAO,CAAA;AAEX,KAAK,IAAI,GAAG;IACV,YAAY,CAAC,EAAE,YAAY,CAAA;IAC3B,YAAY,CAAC,EAAE,SAAS,CAAA;IACxB,aAAa,EAAE,SAAS,CAAA;CACzB,CAAA;AA6CD;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,oBAAoB,mDAI9B,IAAI,KAAG,SAwJT,CAAA"}
|
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import { dequal } from 'dequal/lite'; // lite: no need for Map and Set support
|
|
4
|
+
/**
|
|
5
|
+
* Parses an array field path to extract the array path, row index, and field path.
|
|
6
|
+
* Handles nested arrays by finding the deepest numeric index.
|
|
7
|
+
*
|
|
8
|
+
* @param path - The field path to parse (e.g., 'array.1.text' or 'blocks.0.items.1.title')
|
|
9
|
+
* @returns Object with arrayPath, rowIndex, and fieldPath, or null if not an array field
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* parseArrayFieldPath('array.1.text')
|
|
13
|
+
* // Returns: { arrayPath: 'array', rowIndex: 1, fieldPath: 'text' }
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* parseArrayFieldPath('blocks.0.items.1.title')
|
|
17
|
+
* // Returns: { arrayPath: 'blocks.0.items', rowIndex: 1, fieldPath: 'title' }
|
|
18
|
+
*/
|
|
19
|
+
function parseArrayFieldPath(path) {
|
|
20
|
+
const segments = path.split('.');
|
|
21
|
+
// Find the last numeric index (indicates array row)
|
|
22
|
+
let lastNumericIndex = -1;
|
|
23
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
24
|
+
if (/^\d+$/.test(segments[i])) {
|
|
25
|
+
lastNumericIndex = i;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Not an array field if no numeric index or nothing after it
|
|
30
|
+
if (lastNumericIndex === -1 || lastNumericIndex === segments.length - 1) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
arrayPath: segments.slice(0, lastNumericIndex).join('.'),
|
|
35
|
+
fieldPath: segments.slice(lastNumericIndex + 1).join('.'),
|
|
36
|
+
rowIndex: parseInt(segments[lastNumericIndex], 10)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
4
39
|
/**
|
|
5
40
|
* This function receives form state from the server and intelligently merges it into the client state.
|
|
6
41
|
* The server contains extra properties that the client may not have, e.g. custom components and error states.
|
|
@@ -31,9 +66,32 @@ export const mergeServerFormState = ({
|
|
|
31
66
|
* a. accept all values when explicitly requested, e.g. on submit
|
|
32
67
|
* b. only accept values for unmodified fields, e.g. on autosave
|
|
33
68
|
*/
|
|
34
|
-
|
|
69
|
+
let shouldAcceptValue = incomingField.addedByServer || acceptValues === true || typeof acceptValues === 'object' && acceptValues !== null &&
|
|
35
70
|
// Note: Must be explicitly `false`, allow `null` or `undefined` to mean true
|
|
36
71
|
acceptValues.overrideLocalChanges === false && !currentState[path]?.isModified;
|
|
72
|
+
/**
|
|
73
|
+
* For array row fields, verify the row IDs match at the given index before accepting
|
|
74
|
+
* server values. If rows were reordered or deleted while the request was in-flight,
|
|
75
|
+
* the same index may refer to different rows, and accepting the server value would
|
|
76
|
+
* overwrite the wrong row's data.
|
|
77
|
+
*
|
|
78
|
+
* This guard only applies during autosave. On explicit save (acceptValues === true),
|
|
79
|
+
* the server response is authoritative and this check is bypassed.
|
|
80
|
+
*/
|
|
81
|
+
if (shouldAcceptValue && !incomingField.addedByServer && acceptValues !== true) {
|
|
82
|
+
const parsed = parseArrayFieldPath(path);
|
|
83
|
+
if (parsed) {
|
|
84
|
+
const {
|
|
85
|
+
arrayPath,
|
|
86
|
+
rowIndex
|
|
87
|
+
} = parsed;
|
|
88
|
+
const clientRowId = currentState[arrayPath]?.rows?.[rowIndex]?.id;
|
|
89
|
+
const serverRowId = incomingState[arrayPath]?.rows?.[rowIndex]?.id;
|
|
90
|
+
if (clientRowId === undefined || serverRowId && clientRowId !== serverRowId) {
|
|
91
|
+
shouldAcceptValue = false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
37
95
|
let sanitizedIncomingField = incomingField;
|
|
38
96
|
if (!shouldAcceptValue) {
|
|
39
97
|
/**
|
|
@@ -60,32 +118,51 @@ export const mergeServerFormState = ({
|
|
|
60
118
|
* For example, the server response could come back with a row which has been deleted on the client
|
|
61
119
|
* Loop over the incoming rows, if it exists in client side form state, merge in any new properties from the server
|
|
62
120
|
* Note: read `currentState` and not `newState` here, as the `rows` property have already been merged above
|
|
121
|
+
*
|
|
122
|
+
* On explicit save (acceptValues === true), the server is authoritative - accept server row order.
|
|
123
|
+
* On autosave (acceptValues !== true), the client is source of truth - use ID-based matching.
|
|
63
124
|
*/
|
|
64
125
|
if (Array.isArray(incomingField.rows) && path in currentState) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
126
|
+
if (acceptValues === true) {
|
|
127
|
+
// Explicit save: server is authoritative, use index-based merging
|
|
128
|
+
newState[path].rows = incomingField.rows.map((serverRow, index) => ({
|
|
129
|
+
...(currentState[path]?.rows?.[index] || {}),
|
|
130
|
+
...serverRow
|
|
131
|
+
}));
|
|
132
|
+
} else {
|
|
133
|
+
// Autosave: client is source of truth, use ID-based matching
|
|
134
|
+
newState[path].rows = [...(currentState[path]?.rows || [])]; // shallow copy to avoid mutating the original array
|
|
135
|
+
incomingField.rows.forEach(row => {
|
|
136
|
+
const indexInCurrentState = currentState[path].rows?.findIndex(existingRow => existingRow.id === row.id);
|
|
137
|
+
if (indexInCurrentState > -1) {
|
|
138
|
+
newState[path].rows[indexInCurrentState] = {
|
|
139
|
+
...currentState[path].rows[indexInCurrentState],
|
|
140
|
+
...row
|
|
141
|
+
};
|
|
142
|
+
} else if (row.addedByServer) {
|
|
143
|
+
/**
|
|
144
|
+
* Note: This is a known limitation of computed array and block rows
|
|
145
|
+
* If a new row was added by the server, we append it to the _end_ of this array
|
|
146
|
+
* This is because the client is the source of truth, and it has arrays ordered in a certain position
|
|
147
|
+
* For example, the user may have re-ordered rows client-side while a long running request is processing
|
|
148
|
+
* This means that we _cannot_ slice a new row into the second position on the server, for example
|
|
149
|
+
* By the time it gets back to the client, its index is stale
|
|
150
|
+
*/
|
|
151
|
+
const newRow = {
|
|
152
|
+
...row
|
|
153
|
+
};
|
|
154
|
+
delete newRow.addedByServer;
|
|
155
|
+
newState[path].rows.push(newRow);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
/**
|
|
159
|
+
* Sync the value field to match the actual row count after merging.
|
|
160
|
+
* Client is source of truth for row count when rows were added/removed during the request.
|
|
161
|
+
*/
|
|
162
|
+
if ('value' in incomingField && newState[path].rows.length !== incomingField.value) {
|
|
163
|
+
newState[path].value = newState[path].rows.length;
|
|
87
164
|
}
|
|
88
|
-
}
|
|
165
|
+
}
|
|
89
166
|
}
|
|
90
167
|
// If `valid` is `undefined`, mark it as `true`
|
|
91
168
|
if (incomingField.valid !== false) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mergeServerFormState.js","names":["dequal","mergeServerFormState","acceptValues","currentState","incomingState","newState","path","incomingField","Object","entries","addedByServer","shouldAcceptValue","overrideLocalChanges","isModified","sanitizedIncomingField","initialValue","value","rest","errorPaths","Array","isArray","rows","forEach","row","indexInCurrentState","findIndex","existingRow","id","newRow","push","valid","passesCondition","blocksFilterOptions"],"sources":["../../../src/forms/Form/mergeServerFormState.ts"],"sourcesContent":["'use client'\nimport type { FormState } from 'payload'\n\nimport { dequal } from 'dequal/lite' // lite: no need for Map and Set support\n\n/**\n * If true, will accept all values from the server, overriding any current values in local state.\n * Can also provide an options object for more granular control.\n */\nexport type AcceptValues =\n | {\n /**\n * When `false`, will accept the values from the server _UNLESS_ the value has been modified locally since the request was made.\n * This is useful for autosave, for example, where hooks may have modified the field's value on the server while you were still making changes.\n * @default undefined\n */\n overrideLocalChanges?: boolean\n }\n | boolean\n\ntype Args = {\n acceptValues?: AcceptValues\n currentState?: FormState\n incomingState: FormState\n}\n\n/**\n * This function receives form state from the server and intelligently merges it into the client state.\n * The server contains extra properties that the client may not have, e.g. custom components and error states.\n * We typically do not want to merge properties that rely on user input, however, such as values, unless explicitly requested.\n * Doing this would cause the client to lose any local changes to those fields.\n *\n * Note: Local state is the source of truth, not the new server state that is getting merged in. This is critical for array row\n * manipulation specifically, where the user may have added, removed, or reordered rows while a request was pending and is now stale.\n *\n * This function applies some defaults, as well as cleans up the server response in preparation for the client.\n * e.g. it will set `valid` and `passesCondition` to true if undefined, and remove `addedByServer` from the response.\n */\nexport const mergeServerFormState = ({\n acceptValues,\n currentState = {},\n incomingState,\n}: Args): FormState => {\n const newState = { ...currentState }\n\n for (const [path, incomingField] of Object.entries(incomingState || {})) {\n if (!(path in currentState) && !incomingField.addedByServer) {\n continue\n }\n\n /**\n * If it's a new field added by the server, always accept the value.\n * Otherwise:\n * a. accept all values when explicitly requested, e.g. on submit\n * b. only accept values for unmodified fields, e.g. on autosave\n */\n const shouldAcceptValue =\n incomingField.addedByServer ||\n acceptValues === true ||\n (typeof acceptValues === 'object' &&\n acceptValues !== null &&\n // Note: Must be explicitly `false`, allow `null` or `undefined` to mean true\n acceptValues.overrideLocalChanges === false &&\n !currentState[path]?.isModified)\n\n let sanitizedIncomingField = incomingField\n\n if (!shouldAcceptValue) {\n /**\n * Note: do not delete properties off `incomingField` as this will mutate the original object\n * Instead, omit them from the destructured object by excluding specific keys\n * This will also ensure we don't set `undefined` into the result unnecessarily\n */\n const { initialValue, value, ...rest } = incomingField\n sanitizedIncomingField = rest\n }\n\n newState[path] = {\n ...currentState[path],\n ...sanitizedIncomingField,\n }\n\n if (\n currentState[path] &&\n 'errorPaths' in currentState[path] &&\n !('errorPaths' in incomingField)\n ) {\n newState[path].errorPaths = []\n }\n\n /**\n * Deeply merge the rows array to ensure changes to local state are not lost while the request was pending\n * For example, the server response could come back with a row which has been deleted on the client\n * Loop over the incoming rows, if it exists in client side form state, merge in any new properties from the server\n * Note: read `currentState` and not `newState` here, as the `rows` property have already been merged above\n */\n if (Array.isArray(incomingField.rows) && path in currentState) {\n newState[path].rows = [...(currentState[path]?.rows || [])] // shallow copy to avoid mutating the original array\n\n incomingField.rows.forEach((row) => {\n const indexInCurrentState = currentState[path].rows?.findIndex(\n (existingRow) => existingRow.id === row.id,\n )\n\n if (indexInCurrentState > -1) {\n newState[path].rows[indexInCurrentState] = {\n ...currentState[path].rows[indexInCurrentState],\n ...row,\n }\n } else if (row.addedByServer) {\n /**\n * Note: This is a known limitation of computed array and block rows\n * If a new row was added by the server, we append it to the _end_ of this array\n * This is because the client is the source of truth, and it has arrays ordered in a certain position\n * For example, the user may have re-ordered rows client-side while a long running request is processing\n * This means that we _cannot_ slice a new row into the second position on the server, for example\n * By the time it gets back to the client, its index is stale\n */\n const newRow = { ...row }\n delete newRow.addedByServer\n newState[path].rows.push(newRow)\n }\n })\n }\n\n // If `valid` is `undefined`, mark it as `true`\n if (incomingField.valid !== false) {\n newState[path].valid = true\n }\n\n // If `passesCondition` is `undefined`, mark it as `true`\n if (incomingField.passesCondition !== false) {\n newState[path].passesCondition = true\n }\n\n /**\n * Undefined values for blocksFilterOptions coming back should be treated as \"all blocks allowed\" and\n * should always be merged in.\n * Without this, an undefined value coming back will incorrectly be ignored, and the previous filter will remain.\n */\n if (!incomingField.blocksFilterOptions) {\n delete newState[path].blocksFilterOptions\n }\n\n // Strip away the `addedByServer` property from the client\n // This will prevent it from being passed back to the server\n delete newState[path].addedByServer\n }\n\n // Return the original object reference if the state is unchanged\n // This will avoid unnecessary re-renders and dependency updates\n return dequal(newState, currentState) ? currentState : newState\n}\n"],"mappings":"AAAA;;AAGA,SAASA,MAAM,QAAQ,cAAa,CAAC;AAuBrC;;;;;;;;;;;;AAYA,OAAO,MAAMC,oBAAA,GAAuBA,CAAC;EACnCC,YAAY;EACZC,YAAA,GAAe,CAAC,CAAC;EACjBC;AAAa,CACR;EACL,MAAMC,QAAA,GAAW;IAAE,GAAGF;EAAa;EAEnC,KAAK,MAAM,CAACG,IAAA,EAAMC,aAAA,CAAc,IAAIC,MAAA,CAAOC,OAAO,CAACL,aAAA,IAAiB,CAAC,IAAI;IACvE,IAAI,EAAEE,IAAA,IAAQH,YAAW,KAAM,CAACI,aAAA,CAAcG,aAAa,EAAE;MAC3D;IACF;IAEA;;;;;;IAMA,MAAMC,iBAAA,GACJJ,aAAA,CAAcG,aAAa,IAC3BR,YAAA,KAAiB,QAChB,OAAOA,YAAA,KAAiB,YACvBA,YAAA,KAAiB;IACjB;IACAA,YAAA,CA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MkB,MAAA,GAAS;YAAE,GAAGL;UAAI;UACxB,OAAOK,MAAA,CAAOlB,aAAa;UAC3BL,QAAQ,CAACC,IAAA,CAAK,CAACe,IAAI,CAACQ,IAAI,CAACD,MAAA;QAC3B;MACF;IACF;IAEA;IACA,IAAIrB,aAAA,CAAcuB,KAAK,KAAK,OAAO;MACjCzB,QAAQ,CAACC,IAAA,CAAK,CAACwB,KAAK,GAAG;IACzB;IAEA;IACA,IAAIvB,aAAA,CAAcwB,eAAe,KAAK,OAAO;MAC3C1B,QAAQ,CAACC,IAAA,CAAK,CAACyB,eAAe,GAAG;IACnC;IAEA;;;;;IAKA,IAAI,CAACxB,aAAA,CAAcyB,mBAAmB,EAAE;MACtC,OAAO3B,QAAQ,CAACC,IAAA,CAAK,CAAC0B,mBAAmB;IAC3C;IAEA;IACA;IACA,OAAO3B,QAAQ,CAACC,IAAA,CAAK,CAACI,aAAa;EACrC;EAEA;EACA;EACA,OAAOV,MAAA,CAAOK,QAAA,EAAUF,YAAA,IAAgBA,YAAA,GAAeE,QAAA;AACzD","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"mergeServerFormState.js","names":["dequal","parseArrayFieldPath","path","segments","split","lastNumericIndex","i","length","test","arrayPath","slice","join","fieldPath","rowIndex","parseInt","mergeServerFormState","acceptValues","currentState","incomingState","newState","incomingField","Object","entries","addedByServer","shouldAcceptValue","overrideLocalChanges","isModified","parsed","clientRowId","rows","id","serverRowId","undefined","sanitizedIncomingField","initialValue","value","rest","errorPaths","Array","isArray","map","serverRow","index","forEach","row","indexInCurrentState","findIndex","existingRow","newRow","push","valid","passesCondition","blocksFilterOptions"],"sources":["../../../src/forms/Form/mergeServerFormState.ts"],"sourcesContent":["'use client'\nimport type { FormState } from 'payload'\n\nimport { dequal } from 'dequal/lite' // lite: no need for Map and Set support\n\n/**\n * If true, will accept all values from the server, overriding any current values in local state.\n * Can also provide an options object for more granular control.\n */\nexport type AcceptValues =\n | {\n /**\n * When `false`, will accept the values from the server _UNLESS_ the value has been modified locally since the request was made.\n * This is useful for autosave, for example, where hooks may have modified the field's value on the server while you were still making changes.\n * @default undefined\n */\n overrideLocalChanges?: boolean\n }\n | boolean\n\ntype Args = {\n acceptValues?: AcceptValues\n currentState?: FormState\n incomingState: FormState\n}\n\n/**\n * Parses an array field path to extract the array path, row index, and field path.\n * Handles nested arrays by finding the deepest numeric index.\n *\n * @param path - The field path to parse (e.g., 'array.1.text' or 'blocks.0.items.1.title')\n * @returns Object with arrayPath, rowIndex, and fieldPath, or null if not an array field\n *\n * @example\n * parseArrayFieldPath('array.1.text')\n * // Returns: { arrayPath: 'array', rowIndex: 1, fieldPath: 'text' }\n *\n * @example\n * parseArrayFieldPath('blocks.0.items.1.title')\n * // Returns: { arrayPath: 'blocks.0.items', rowIndex: 1, fieldPath: 'title' }\n */\nfunction parseArrayFieldPath(path: string): {\n arrayPath: string\n fieldPath: string\n rowIndex: number\n} | null {\n const segments = path.split('.')\n\n // Find the last numeric index (indicates array row)\n let lastNumericIndex = -1\n for (let i = segments.length - 1; i >= 0; i--) {\n if (/^\\d+$/.test(segments[i])) {\n lastNumericIndex = i\n break\n }\n }\n\n // Not an array field if no numeric index or nothing after it\n if (lastNumericIndex === -1 || lastNumericIndex === segments.length - 1) {\n return null\n }\n\n return {\n arrayPath: segments.slice(0, lastNumericIndex).join('.'),\n fieldPath: segments.slice(lastNumericIndex + 1).join('.'),\n rowIndex: parseInt(segments[lastNumericIndex], 10),\n }\n}\n\n/**\n * This function receives form state from the server and intelligently merges it into the client state.\n * The server contains extra properties that the client may not have, e.g. custom components and error states.\n * We typically do not want to merge properties that rely on user input, however, such as values, unless explicitly requested.\n * Doing this would cause the client to lose any local changes to those fields.\n *\n * Note: Local state is the source of truth, not the new server state that is getting merged in. This is critical for array row\n * manipulation specifically, where the user may have added, removed, or reordered rows while a request was pending and is now stale.\n *\n * This function applies some defaults, as well as cleans up the server response in preparation for the client.\n * e.g. it will set `valid` and `passesCondition` to true if undefined, and remove `addedByServer` from the response.\n */\nexport const mergeServerFormState = ({\n acceptValues,\n currentState = {},\n incomingState,\n}: Args): FormState => {\n const newState = { ...currentState }\n\n for (const [path, incomingField] of Object.entries(incomingState || {})) {\n if (!(path in currentState) && !incomingField.addedByServer) {\n continue\n }\n\n /**\n * If it's a new field added by the server, always accept the value.\n * Otherwise:\n * a. accept all values when explicitly requested, e.g. on submit\n * b. only accept values for unmodified fields, e.g. on autosave\n */\n let shouldAcceptValue =\n incomingField.addedByServer ||\n acceptValues === true ||\n (typeof acceptValues === 'object' &&\n acceptValues !== null &&\n // Note: Must be explicitly `false`, allow `null` or `undefined` to mean true\n acceptValues.overrideLocalChanges === false &&\n !currentState[path]?.isModified)\n\n /**\n * For array row fields, verify the row IDs match at the given index before accepting\n * server values. If rows were reordered or deleted while the request was in-flight,\n * the same index may refer to different rows, and accepting the server value would\n * overwrite the wrong row's data.\n *\n * This guard only applies during autosave. On explicit save (acceptValues === true),\n * the server response is authoritative and this check is bypassed.\n */\n if (shouldAcceptValue && !incomingField.addedByServer && acceptValues !== true) {\n const parsed = parseArrayFieldPath(path)\n if (parsed) {\n const { arrayPath, rowIndex } = parsed\n const clientRowId = currentState[arrayPath]?.rows?.[rowIndex]?.id\n const serverRowId = incomingState[arrayPath]?.rows?.[rowIndex]?.id\n\n if (clientRowId === undefined || (serverRowId && clientRowId !== serverRowId)) {\n shouldAcceptValue = false\n }\n }\n }\n\n let sanitizedIncomingField = incomingField\n\n if (!shouldAcceptValue) {\n /**\n * Note: do not delete properties off `incomingField` as this will mutate the original object\n * Instead, omit them from the destructured object by excluding specific keys\n * This will also ensure we don't set `undefined` into the result unnecessarily\n */\n const { initialValue, value, ...rest } = incomingField\n sanitizedIncomingField = rest\n }\n\n newState[path] = {\n ...currentState[path],\n ...sanitizedIncomingField,\n }\n\n if (\n currentState[path] &&\n 'errorPaths' in currentState[path] &&\n !('errorPaths' in incomingField)\n ) {\n newState[path].errorPaths = []\n }\n\n /**\n * Deeply merge the rows array to ensure changes to local state are not lost while the request was pending\n * For example, the server response could come back with a row which has been deleted on the client\n * Loop over the incoming rows, if it exists in client side form state, merge in any new properties from the server\n * Note: read `currentState` and not `newState` here, as the `rows` property have already been merged above\n *\n * On explicit save (acceptValues === true), the server is authoritative - accept server row order.\n * On autosave (acceptValues !== true), the client is source of truth - use ID-based matching.\n */\n if (Array.isArray(incomingField.rows) && path in currentState) {\n if (acceptValues === true) {\n // Explicit save: server is authoritative, use index-based merging\n newState[path].rows = incomingField.rows.map((serverRow, index) => ({\n ...(currentState[path]?.rows?.[index] || {}),\n ...serverRow,\n }))\n } else {\n // Autosave: client is source of truth, use ID-based matching\n newState[path].rows = [...(currentState[path]?.rows || [])] // shallow copy to avoid mutating the original array\n\n incomingField.rows.forEach((row) => {\n const indexInCurrentState = currentState[path].rows?.findIndex(\n (existingRow) => existingRow.id === row.id,\n )\n\n if (indexInCurrentState > -1) {\n newState[path].rows[indexInCurrentState] = {\n ...currentState[path].rows[indexInCurrentState],\n ...row,\n }\n } else if (row.addedByServer) {\n /**\n * Note: This is a known limitation of computed array and block rows\n * If a new row was added by the server, we append it to the _end_ of this array\n * This is because the client is the source of truth, and it has arrays ordered in a certain position\n * For example, the user may have re-ordered rows client-side while a long running request is processing\n * This means that we _cannot_ slice a new row into the second position on the server, for example\n * By the time it gets back to the client, its index is stale\n */\n const newRow = { ...row }\n delete newRow.addedByServer\n newState[path].rows.push(newRow)\n }\n })\n\n /**\n * Sync the value field to match the actual row count after merging.\n * Client is source of truth for row count when rows were added/removed during the request.\n */\n if ('value' in incomingField && newState[path].rows.length !== incomingField.value) {\n newState[path].value = newState[path].rows.length\n }\n }\n }\n\n // If `valid` is `undefined`, mark it as `true`\n if (incomingField.valid !== false) {\n newState[path].valid = true\n }\n\n // If `passesCondition` is `undefined`, mark it as `true`\n if (incomingField.passesCondition !== false) {\n newState[path].passesCondition = true\n }\n\n /**\n * Undefined values for blocksFilterOptions coming back should be treated as \"all blocks allowed\" and\n * should always be merged in.\n * Without this, an undefined value coming back will incorrectly be ignored, and the previous filter will remain.\n */\n if (!incomingField.blocksFilterOptions) {\n delete newState[path].blocksFilterOptions\n }\n\n // Strip away the `addedByServer` property from the client\n // This will prevent it from being passed back to the server\n delete newState[path].addedByServer\n }\n\n // Return the original object reference if the state is unchanged\n // This will avoid unnecessary re-renders and dependency updates\n return dequal(newState, currentState) ? currentState : newState\n}\n"],"mappings":"AAAA;;AAGA,SAASA,MAAM,QAAQ,cAAa,CAAC;AAuBrC;;;;;;;;;;;;;;;AAeA,SAASC,oBAAoBC,IAAY;EAKvC,MAAMC,QAAA,GAAWD,IAAA,CAAKE,KAAK,CAAC;EAE5B;EACA,IAAIC,gBAAA,GAAmB,CAAC;EACxB,KAAK,IAAIC,CAAA,GAAIH,QAAA,CAASI,MAAM,GAAG,GAAGD,CAAA,IAAK,GAAGA,CAAA,IAAK;IAC7C,IAAI,QAAQE,IAAI,CAACL,QAAQ,CAACG,CAAA,CAAE,GAAG;MAC7BD,gBAAA,GAAmBC,CAAA;MACnB;IACF;EACF;EAEA;EACA,IAAID,gBAAA,KAAqB,CAAC,KAAKA,gBAAA,KAAqBF,QAAA,CAASI,MAAM,GAAG,GAAG;IACvE,OAAO;EACT;EAEA,OAAO;IACLE,SAAA,EAAWN,QAAA,CAASO,KAAK,CAAC,GAAGL,gBAAA,EAAkBM,IAAI,CAAC;IACpDC,SAAA,EAAWT,QAAA,CAASO,KAAK,CAACL,gBAAA,GAAmB,GAAGM,IAAI,CAAC;IACrDE,QAAA,EAAUC,QAAA,CAASX,QAAQ,CAACE,gBAAA,CAAiB,EAAE;EACjD;AACF;AAEA;;;;;;;;;;;;AAYA,OAAO,MAAMU,oBAAA,GAAuBA,CAAC;EACnCC,YAAY;EACZC,YAAA,GAAe,CAAC,CAAC;EACjBC;AAAa,CACR;EACL,MAAMC,QAAA,GAAW;IAAE,GAAGF;EAAa;EAEnC,KAAK,MAAM,CAACf,IAAA,EAAMkB,aAAA,CAAc,IAAIC,MAAA,CAAO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,CAACjB,IAAA,CAAK,CAAC2B,IAAI,CAACoB,IAAI,CAACD,MAAA;UAC3B;QACF;QAEA;;;;QAIA,IAAI,WAAW5B,aAAA,IAAiBD,QAAQ,CAACjB,IAAA,CAAK,CAAC2B,IAAI,CAACtB,MAAM,KAAKa,aAAA,CAAce,KAAK,EAAE;UAClFhB,QAAQ,CAACjB,IAAA,CAAK,CAACiC,KAAK,GAAGhB,QAAQ,CAACjB,IAAA,CAAK,CAAC2B,IAAI,CAACtB,MAAM;QACnD;MACF;IACF;IAEA;IACA,IAAIa,aAAA,CAAc8B,KAAK,KAAK,OAAO;MACjC/B,QAAQ,CAACjB,IAAA,CAAK,CAACgD,KAAK,GAAG;IACzB;IAEA;IACA,IAAI9B,aAAA,CAAc+B,eAAe,KAAK,OAAO;MAC3ChC,QAAQ,CAACjB,IAAA,CAAK,CAACiD,eAAe,GAAG;IACnC;IAEA;;;;;IAKA,IAAI,CAAC/B,aAAA,CAAcgC,mBAAmB,EAAE;MACtC,OAAOjC,QAAQ,CAACjB,IAAA,CAAK,CAACkD,mBAAmB;IAC3C;IAEA;IACA;IACA,OAAOjC,QAAQ,CAACjB,IAAA,CAAK,CAACqB,aAAa;EACrC;EAEA;EACA;EACA,OAAOvB,MAAA,CAAOmB,QAAA,EAAUF,YAAA,IAAgBA,YAAA,GAAeE,QAAA;AACzD","ignoreList":[]}
|
|
@@ -50,6 +50,12 @@ export interface LivePreviewContextType {
|
|
|
50
50
|
setURL: (url: string) => void;
|
|
51
51
|
setWidth: (width: number) => void;
|
|
52
52
|
setZoom: (zoom: number) => void;
|
|
53
|
+
/**
|
|
54
|
+
* Do not render the iframe until the user is actively live previewing. This will:
|
|
55
|
+
* 1. Prevent running through URL proxies set up on their `admin.livePreview.url` endpoint, e.g. to enter Next.js draft mode.
|
|
56
|
+
* 2. Avoid unnecessary performance and network costs of rendering the iframe before it's needed.
|
|
57
|
+
*/
|
|
58
|
+
shouldRenderIframe?: boolean;
|
|
53
59
|
size: {
|
|
54
60
|
height: number;
|
|
55
61
|
width: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/providers/LivePreview/context.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AACpE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AACrC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAI9B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAA;AACnE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAEzD,MAAM,WAAW,sBAAsB;IACrC,UAAU,EAAE,OAAO,CAAA;IACnB,UAAU,EAAE,iBAAiB,CAAC,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAA;IAC5D,WAAW,EAAE,iBAAiB,CAAC,aAAa,CAAC,CAAA;IAC7C,SAAS,EAAE,KAAK,CAAC,SAAS,CAAC,iBAAiB,GAAG,IAAI,CAAC,CAAA;IACpD,oBAAoB,EAAE,OAAO,CAAA;IAC7B,gBAAgB,EAAE,OAAO,CAAA;IACzB,WAAW,EAAE,OAAO,CAAA;IACpB,gBAAgB,EAAE,OAAO,CAAA;IACzB,oBAAoB,CAAC,EAAE,OAAO,CAAA;IAC9B;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,kBAAkB,EAAE;QAClB,MAAM,EAAE,MAAM,CAAA;QACd,KAAK,EAAE,MAAM,CAAA;KACd,CAAA;IACD,eAAe,EAAE,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC,iBAAiB,CAAC,CAAA;IACrE,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC,IAAI,GAAG,MAAM,CAAC,CAAA;IACzC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,iBAAiB,EAAE,QAAQ,GAAG,OAAO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AK,IAAI,CAAA;IACxE,aAAa,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;IACpC,oBAAoB,EAAE,CAAC,iBAAiB,EAAE,QAAQ,GAAG,OAAO,KAAK,IAAI,CAAA;IACrE,OAAO,EAAE,QAAQ,CAAC,iBAAiB,CAAC,CAAA;IACpC,kBAAkB,EAAE,CAAC,QAAQ,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAA;IAChE;;;OAGG;IACH,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;IAC7B,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;IACjC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAC/B;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAC5B,IAAI,EAAE;QACJ,MAAM,EAAE,MAAM,CAAA;QACd,KAAK,EAAE,MAAM,CAAA;KACd,CAAA;IACD,eAAe,EAAE;QACf,CAAC,EAAE,MAAM,CAAA;QACT,CAAC,EAAE,MAAM,CAAA;KACV,CAAA;IACD;;;OAGG;IACH,oBAAoB,CAAC,EAAE,UAAU,GAAG,QAAQ,CAAA;IAC5C,GAAG,EAAE,kBAAkB,CAAA;IACvB,IAAI,EAAE,MAAM,CAAA;CACb;AAED,eAAO,MAAM,kBAAkB,uCA0C7B,CAAA;AAEF,eAAO,MAAM,qBAAqB,8BAAgC,CAAA;AAElE;;GAEG;AACH,eAAO,MAAM,aAAa;;;yBAnFH,MAAM,KAAK,IAAI;CAuFrC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","names":["createContext","use","LivePreviewContext","appIsReady","breakpoint","undefined","breakpoints","iframeRef","isLivePreviewEnabled","isLivePreviewing","isPopupOpen","isPreviewEnabled","measuredDeviceSize","height","width","openPopupWindow","popupRef","previewURL","previewWindowType","setAppIsReady","setBreakpoint","setHeight","setIsLivePreviewing","setLoadedURL","setMeasuredDeviceSize","setPreviewURL","setPreviewWindowType","setSize","setToolbarPosition","setURL","setWidth","setZoom","size","toolbarPosition","x","y","typeofLivePreviewURL","url","zoom","useLivePreviewContext","usePreviewURL"],"sources":["../../../src/providers/LivePreview/context.ts"],"sourcesContent":["'use client'\nimport type { LivePreviewConfig, LivePreviewURLType } from 'payload'\nimport type { Dispatch } from 'react'\nimport type React from 'react'\n\nimport { createContext, use } from 'react'\n\nimport type { usePopupWindow } from '../../hooks/usePopupWindow.js'\nimport type { SizeReducerAction } from './sizeReducer.js'\n\nexport interface LivePreviewContextType {\n appIsReady: boolean\n breakpoint: LivePreviewConfig['breakpoints'][number]['name']\n breakpoints: LivePreviewConfig['breakpoints']\n iframeRef: React.RefObject<HTMLIFrameElement | null>\n isLivePreviewEnabled: boolean\n isLivePreviewing: boolean\n isPopupOpen: boolean\n isPreviewEnabled: boolean\n listeningForMessages?: boolean\n /**\n * The URL that has finished loading in the iframe or popup.\n * For example, if you set the `url`, it will begin to load into the iframe,\n * but `loadedURL` will not be set until the iframe's `onLoad` event fires.\n */\n loadedURL?: string\n measuredDeviceSize: {\n height: number\n width: number\n }\n openPopupWindow: ReturnType<typeof usePopupWindow>['openPopupWindow']\n popupRef?: React.RefObject<null | Window>\n previewURL?: string\n previewWindowType: 'iframe' | 'popup'\n setAppIsReady: (appIsReady: boolean) => void\n setBreakpoint: (breakpoint: LivePreviewConfig['breakpoints'][number]['name']) => void\n setHeight: (height: number) => void\n setIsLivePreviewing: (isLivePreviewing: boolean) => void\n setLoadedURL: (loadedURL: string) => void\n setMeasuredDeviceSize: (size: { height: number; width: number }) => void\n setPreviewURL: (url: string) => void\n setPreviewWindowType: (previewWindowType: 'iframe' | 'popup') => void\n setSize: Dispatch<SizeReducerAction>\n setToolbarPosition: (position: { x: number; y: number }) => void\n
|
|
1
|
+
{"version":3,"file":"context.js","names":["createContext","use","LivePreviewContext","appIsReady","breakpoint","undefined","breakpoints","iframeRef","isLivePreviewEnabled","isLivePreviewing","isPopupOpen","isPreviewEnabled","measuredDeviceSize","height","width","openPopupWindow","popupRef","previewURL","previewWindowType","setAppIsReady","setBreakpoint","setHeight","setIsLivePreviewing","setLoadedURL","setMeasuredDeviceSize","setPreviewURL","setPreviewWindowType","setSize","setToolbarPosition","setURL","setWidth","setZoom","shouldRenderIframe","size","toolbarPosition","x","y","typeofLivePreviewURL","url","zoom","useLivePreviewContext","usePreviewURL"],"sources":["../../../src/providers/LivePreview/context.ts"],"sourcesContent":["'use client'\nimport type { LivePreviewConfig, LivePreviewURLType } from 'payload'\nimport type { Dispatch } from 'react'\nimport type React from 'react'\n\nimport { createContext, use } from 'react'\n\nimport type { usePopupWindow } from '../../hooks/usePopupWindow.js'\nimport type { SizeReducerAction } from './sizeReducer.js'\n\nexport interface LivePreviewContextType {\n appIsReady: boolean\n breakpoint: LivePreviewConfig['breakpoints'][number]['name']\n breakpoints: LivePreviewConfig['breakpoints']\n iframeRef: React.RefObject<HTMLIFrameElement | null>\n isLivePreviewEnabled: boolean\n isLivePreviewing: boolean\n isPopupOpen: boolean\n isPreviewEnabled: boolean\n listeningForMessages?: boolean\n /**\n * The URL that has finished loading in the iframe or popup.\n * For example, if you set the `url`, it will begin to load into the iframe,\n * but `loadedURL` will not be set until the iframe's `onLoad` event fires.\n */\n loadedURL?: string\n measuredDeviceSize: {\n height: number\n width: number\n }\n openPopupWindow: ReturnType<typeof usePopupWindow>['openPopupWindow']\n popupRef?: React.RefObject<null | Window>\n previewURL?: string\n previewWindowType: 'iframe' | 'popup'\n setAppIsReady: (appIsReady: boolean) => void\n setBreakpoint: (breakpoint: LivePreviewConfig['breakpoints'][number]['name']) => void\n setHeight: (height: number) => void\n setIsLivePreviewing: (isLivePreviewing: boolean) => void\n setLoadedURL: (loadedURL: string) => void\n setMeasuredDeviceSize: (size: { height: number; width: number }) => void\n setPreviewURL: (url: string) => void\n setPreviewWindowType: (previewWindowType: 'iframe' | 'popup') => void\n setSize: Dispatch<SizeReducerAction>\n setToolbarPosition: (position: { x: number; y: number }) => void\n /**\n * Sets the URL of the preview (either iframe or popup).\n * Will trigger a reload of the window.\n */\n setURL: (url: string) => void\n setWidth: (width: number) => void\n setZoom: (zoom: number) => void\n /**\n * Do not render the iframe until the user is actively live previewing. This will:\n * 1. Prevent running through URL proxies set up on their `admin.livePreview.url` endpoint, e.g. to enter Next.js draft mode.\n * 2. Avoid unnecessary performance and network costs of rendering the iframe before it's needed.\n */\n shouldRenderIframe?: boolean\n size: {\n height: number\n width: number\n }\n toolbarPosition: {\n x: number\n y: number\n }\n /**\n * The live preview url property can be either a string or a function that returns a string.\n * It is important to know which one it is, so that we can opt in/out of certain behaviors, e.g. calling the server to get the URL.\n */\n typeofLivePreviewURL?: 'function' | 'string'\n url: LivePreviewURLType\n zoom: number\n}\n\nexport const LivePreviewContext = createContext<LivePreviewContextType>({\n appIsReady: false,\n breakpoint: undefined,\n breakpoints: undefined,\n iframeRef: undefined,\n isLivePreviewEnabled: undefined,\n isLivePreviewing: false,\n isPopupOpen: false,\n isPreviewEnabled: undefined,\n measuredDeviceSize: {\n height: 0,\n width: 0,\n },\n openPopupWindow: () => {},\n popupRef: undefined,\n previewURL: undefined,\n previewWindowType: 'iframe',\n setAppIsReady: () => {},\n setBreakpoint: () => {},\n setHeight: () => {},\n setIsLivePreviewing: () => {},\n setLoadedURL: () => {},\n setMeasuredDeviceSize: () => {},\n setPreviewURL: () => {},\n setPreviewWindowType: () => {},\n setSize: () => {},\n setToolbarPosition: () => {},\n setURL: () => {},\n setWidth: () => {},\n setZoom: () => {},\n shouldRenderIframe: undefined,\n size: {\n height: 0,\n width: 0,\n },\n toolbarPosition: {\n x: 0,\n y: 0,\n },\n typeofLivePreviewURL: undefined,\n url: undefined,\n zoom: 1,\n})\n\nexport const useLivePreviewContext = () => use(LivePreviewContext)\n\n/**\n * Hook to access live preview context values. Separated to prevent breaking changes. In the future this hook can be removed in favour of just using the LivePreview one.\n */\nexport const usePreviewURL = () => {\n const { isPreviewEnabled, previewURL, setPreviewURL } = use(LivePreviewContext)\n\n return { isPreviewEnabled, previewURL, setPreviewURL }\n}\n"],"mappings":"AAAA;;AAKA,SAASA,aAAa,EAAEC,GAAG,QAAQ;AAqEnC,OAAO,MAAMC,kBAAA,GAAqBF,aAAA,CAAsC;EACtEG,UAAA,EAAY;EACZC,UAAA,EAAYC,SAAA;EACZC,WAAA,EAAaD,SAAA;EACbE,SAAA,EAAWF,SAAA;EACXG,oBAAA,EAAsBH,SAAA;EACtBI,gBAAA,EAAkB;EAClBC,WAAA,EAAa;EACbC,gBAAA,EAAkBN,SAAA;EAClBO,kBAAA,EAAoB;IAClBC,MAAA,EAAQ;IACRC,KAAA,EAAO;EACT;EACAC,eAAA,EAAiBA,CAAA,MAAO;EACxBC,QAAA,EAAUX,SAAA;EACVY,UAAA,EAAYZ,SAAA;EACZa,iBAAA,EAAmB;EACnBC,aAAA,EAAeA,CAAA,MAAO;EACtBC,aAAA,EAAeA,CAAA,MAAO;EACtBC,SAAA,EAAWA,CAAA,MAAO;EAClBC,mBAAA,EAAqBA,CAAA,MAAO;EAC5BC,YAAA,EAAcA,CAAA,MAAO;EACrBC,qBAAA,EAAuBA,CAAA,MAAO;EAC9BC,aAAA,EAAeA,CAAA,MAAO;EACtBC,oBAAA,EAAsBA,CAAA,MAAO;EAC7BC,OAAA,EAASA,CAAA,MAAO;EAChBC,kBAAA,EAAoBA,CAAA,MAAO;EAC3BC,MAAA,EAAQA,CAAA,MAAO;EACfC,QAAA,EAAUA,CAAA,MAAO;EACjBC,OAAA,EAASA,CAAA,MAAO;EAChBC,kBAAA,EAAoB3B,SAAA;EACpB4B,IAAA,EAAM;IACJpB,MAAA,EAAQ;IACRC,KAAA,EAAO;EACT;EACAoB,eAAA,EAAiB;IACfC,CAAA,EAAG;IACHC,CAAA,EAAG;EACL;EACAC,oBAAA,EAAsBhC,SAAA;EACtBiC,GAAA,EAAKjC,SAAA;EACLkC,IAAA,EAAM;AACR;AAEA,OAAO,MAAMC,qBAAA,GAAwBA,CAAA,KAAMvC,GAAA,CAAIC,kBAAA;AAE/C;;;AAGA,OAAO,MAAMuC,aAAA,GAAgBA,CAAA;EAC3B,MAAM;IAAE9B,gBAAgB;IAAEM,UAAU;IAAEQ;EAAa,CAAE,GAAGxB,GAAA,CAAIC,kBAAA;EAE5D,OAAO;IAAES,gBAAA;IAAkBM,UAAA;IAAYQ;EAAc;AACvD","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/LivePreview/index.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAyB,iBAAiB,EAAsB,MAAM,SAAS,CAAA;AAG3F,OAAO,KAAmE,MAAM,OAAO,CAAA;AAEvF,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAA;AAU1D,MAAM,MAAM,wBAAwB,GAAG;IACrC,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,WAAW,CAAC,EAAE,iBAAiB,CAAC,aAAa,CAAC,CAAA;IAC9C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;IACzB,UAAU,CAAC,EAAE;QACX,MAAM,EAAE,MAAM,CAAA;QACd,KAAK,EAAE,MAAM,CAAA;KACd,CAAA;IACD,oBAAoB,CAAC,EAAE,OAAO,CAAA;IAC9B,gBAAgB,EAAE,OAAO,CAAA;IACzB;;OAEG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB,GAAG,IAAI,CAAC,sBAAsB,EAAE,sBAAsB,GAAG,KAAK,CAAC,CAAA;AAEhE,eAAO,MAAM,mBAAmB,EAAE,KAAK,CAAC,EAAE,CAAC,wBAAwB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/LivePreview/index.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAyB,iBAAiB,EAAsB,MAAM,SAAS,CAAA;AAG3F,OAAO,KAAmE,MAAM,OAAO,CAAA;AAEvF,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAA;AAU1D,MAAM,MAAM,wBAAwB,GAAG;IACrC,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,WAAW,CAAC,EAAE,iBAAiB,CAAC,aAAa,CAAC,CAAA;IAC9C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;IACzB,UAAU,CAAC,EAAE;QACX,MAAM,EAAE,MAAM,CAAA;QACd,KAAK,EAAE,MAAM,CAAA;KACd,CAAA;IACD,oBAAoB,CAAC,EAAE,OAAO,CAAA;IAC9B,gBAAgB,EAAE,OAAO,CAAA;IACzB;;OAEG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB,GAAG,IAAI,CAAC,sBAAsB,EAAE,sBAAsB,GAAG,KAAK,CAAC,CAAA;AAEhE,eAAO,MAAM,mBAAmB,EAAE,KAAK,CAAC,EAAE,CAAC,wBAAwB,CA6RlE,CAAA"}
|
|
@@ -21,7 +21,18 @@ export const LivePreviewProvider = ({
|
|
|
21
21
|
url: urlFromProps
|
|
22
22
|
}) => {
|
|
23
23
|
const [previewWindowType, setPreviewWindowType] = useState('iframe');
|
|
24
|
-
const [isLivePreviewing,
|
|
24
|
+
const [isLivePreviewing, _setIsLivePreviewing] = useState(incomingIsLivePreviewing);
|
|
25
|
+
const [shouldRenderIframe, setShouldRenderIframe] = useState(isLivePreviewing);
|
|
26
|
+
/**
|
|
27
|
+
* Rendering the iframe is a one-way event, e.g. defer load and never unmount.
|
|
28
|
+
* This way, subsequent toggles will appear to load instantly.
|
|
29
|
+
*/
|
|
30
|
+
const setIsLivePreviewing = useCallback(livePreviewing => {
|
|
31
|
+
if (livePreviewing) {
|
|
32
|
+
setShouldRenderIframe(true);
|
|
33
|
+
}
|
|
34
|
+
_setIsLivePreviewing(livePreviewing);
|
|
35
|
+
}, []);
|
|
25
36
|
const breakpoints = useMemo(() => [...(incomingBreakpoints || []), {
|
|
26
37
|
name: 'responsive',
|
|
27
38
|
height: '100%',
|
|
@@ -207,6 +218,7 @@ export const LivePreviewProvider = ({
|
|
|
207
218
|
setURL: setLivePreviewURL,
|
|
208
219
|
setWidth,
|
|
209
220
|
setZoom,
|
|
221
|
+
shouldRenderIframe,
|
|
210
222
|
size,
|
|
211
223
|
toolbarPosition: position,
|
|
212
224
|
typeofLivePreviewURL,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["DndContext","React","useCallback","useEffect","useId","useMemo","useRef","useState","usePopupWindow","useDocumentInfo","usePreferences","formatAbsoluteURL","customCollisionDetection","LivePreviewContext","sizeReducer","LivePreviewProvider","breakpoints","incomingBreakpoints","children","isLivePreviewEnabled","isLivePreviewing","incomingIsLivePreviewing","isPreviewEnabled","previewURL","previewURLFromProps","typeofLivePreviewURL","url","urlFromProps","previewWindowType","setPreviewWindowType","setIsLivePreviewing","name","height","label","width","setURL","setPreviewURL","isPopupOpen","openPopupWindow","popupRef","eventType","appIsReady","setAppIsReady","listeningForMessages","setListeningForMessages","collectionSlug","globalSlug","isFirstRender","setPreference","iframeRef","loadedURL","setLoadedURL","zoom","setZoom","position","setPosition","x","y","size","setSize","useReducer","measuredDeviceSize","setMeasuredDeviceSize","breakpoint","setBreakpoint","setLivePreviewURL","_incomingURL","incomingURL","handleDragEnd","ev","over","id","newPos","delta","setWidth","type","value","setHeight","foundBreakpoint","find","bp","handleMessage","event","startsWith","origin","data","ready","window","addEventListener","removeEventListener","handleWindowChange","newPreviewWindowType","current","editViewType","dndContextID","_jsx","setToolbarPosition","toolbarPosition","collisionDetection","onDragEnd"],"sources":["../../../src/providers/LivePreview/index.tsx"],"sourcesContent":["'use client'\nimport type { CollectionPreferences, LivePreviewConfig, LivePreviewURLType } from 'payload'\n\nimport { DndContext } from '@dnd-kit/core'\nimport React, { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\n\nimport type { LivePreviewContextType } from './context.js'\n\nimport { usePopupWindow } from '../../hooks/usePopupWindow.js'\nimport { useDocumentInfo } from '../../providers/DocumentInfo/index.js'\nimport { usePreferences } from '../../providers/Preferences/index.js'\nimport { formatAbsoluteURL } from '../../utilities/formatAbsoluteURL.js'\nimport { customCollisionDetection } from './collisionDetection.js'\nimport { LivePreviewContext } from './context.js'\nimport { sizeReducer } from './sizeReducer.js'\n\nexport type LivePreviewProviderProps = {\n appIsReady?: boolean\n breakpoints?: LivePreviewConfig['breakpoints']\n children: React.ReactNode\n deviceSize?: {\n height: number\n width: number\n }\n isLivePreviewEnabled?: boolean\n isLivePreviewing: boolean\n /**\n * This specifically relates to `admin.preview` function in the config instead of live preview.\n */\n isPreviewEnabled?: boolean\n /**\n * This specifically relates to `admin.preview` function in the config instead of live preview.\n */\n previewURL?: string\n} & Pick<LivePreviewContextType, 'typeofLivePreviewURL' | 'url'>\n\nexport const LivePreviewProvider: React.FC<LivePreviewProviderProps> = ({\n breakpoints: incomingBreakpoints,\n children,\n isLivePreviewEnabled,\n isLivePreviewing: incomingIsLivePreviewing,\n isPreviewEnabled,\n previewURL: previewURLFromProps,\n typeofLivePreviewURL,\n url: urlFromProps,\n}) => {\n const [previewWindowType, setPreviewWindowType] = useState<'iframe' | 'popup'>('iframe')\n const [isLivePreviewing, setIsLivePreviewing] = useState(incomingIsLivePreviewing)\n\n const breakpoints: LivePreviewConfig['breakpoints'] = useMemo(\n () => [\n ...(incomingBreakpoints || []),\n {\n name: 'responsive',\n height: '100%',\n label: 'Responsive',\n width: '100%',\n },\n ],\n [incomingBreakpoints],\n )\n\n const [url, setURL] = useState<string>('')\n const [previewURL, setPreviewURL] = useState<string>(previewURLFromProps)\n\n const { isPopupOpen, openPopupWindow, popupRef } = usePopupWindow({\n eventType: 'payload-live-preview',\n url,\n })\n\n const [appIsReady, setAppIsReady] = useState(false)\n const [listeningForMessages, setListeningForMessages] = useState(false)\n\n const { collectionSlug, globalSlug } = useDocumentInfo()\n\n const isFirstRender = useRef(true)\n\n const { setPreference } = usePreferences()\n\n const iframeRef = React.useRef<HTMLIFrameElement>(null)\n\n const [loadedURL, setLoadedURL] = useState<string>()\n\n const [zoom, setZoom] = useState(1)\n\n const [position, setPosition] = useState({ x: 0, y: 0 })\n\n const [size, setSize] = React.useReducer(sizeReducer, { height: 0, width: 0 })\n\n const [measuredDeviceSize, setMeasuredDeviceSize] = useState({\n height: 0,\n width: 0,\n })\n\n const [breakpoint, setBreakpoint] =\n React.useState<LivePreviewConfig['breakpoints'][0]['name']>('responsive')\n\n /**\n * A \"middleware\" callback fn that does some additional work before `setURL`.\n * This is what we provide through context, bc it:\n * - ensures the URL is absolute\n * - resets `appIsReady` to `false` while the new URL is loading\n */\n const setLivePreviewURL = useCallback<LivePreviewContextType['setURL']>(\n (_incomingURL) => {\n let incomingURL: LivePreviewURLType\n\n if (typeof _incomingURL === 'string') {\n incomingURL = formatAbsoluteURL(_incomingURL)\n }\n\n if (!incomingURL) {\n setIsLivePreviewing(false)\n }\n\n if (incomingURL !== url) {\n setAppIsReady(false)\n setURL(incomingURL)\n }\n },\n [url],\n )\n\n /**\n * `url` needs to be relative to the window, which cannot be done on initial render.\n */\n useEffect(() => {\n if (typeof urlFromProps === 'string') {\n setURL(formatAbsoluteURL(urlFromProps))\n }\n }, [urlFromProps])\n\n // The toolbar needs to freely drag and drop around the page\n const handleDragEnd = (ev) => {\n // only update position if the toolbar is completely within the preview area\n // otherwise reset it back to the previous position\n // TODO: reset to the nearest edge of the preview area\n if (ev.over && ev.over.id === 'live-preview-area') {\n const newPos = {\n x: position.x + ev.delta.x,\n y: position.y + ev.delta.y,\n }\n\n setPosition(newPos)\n } else {\n // reset\n }\n }\n\n const setWidth = useCallback(\n (width) => {\n setSize({ type: 'width', value: width })\n },\n [setSize],\n )\n\n const setHeight = useCallback(\n (height) => {\n setSize({ type: 'height', value: height })\n },\n [setSize],\n )\n\n // explicitly set new width and height when as new breakpoints are selected\n // exclude `custom` breakpoint as it is handled by the `setWidth` and `setHeight` directly\n useEffect(() => {\n const foundBreakpoint = breakpoints?.find((bp) => bp.name === breakpoint)\n\n if (\n foundBreakpoint &&\n breakpoint !== 'responsive' &&\n breakpoint !== 'custom' &&\n typeof foundBreakpoint?.width === 'number' &&\n typeof foundBreakpoint?.height === 'number'\n ) {\n setSize({\n type: 'reset',\n value: {\n height: foundBreakpoint.height,\n width: foundBreakpoint.width,\n },\n })\n }\n }, [breakpoint, breakpoints])\n\n /**\n * Receive the `ready` message from the popup window\n * This indicates that the app is ready to receive `window.postMessage` events\n * This is also the only cross-origin way of detecting when a popup window has loaded\n * Unlike iframe elements which have an `onLoad` handler, there is no way to access `window.open` on popups\n */\n useEffect(() => {\n const handleMessage = (event: MessageEvent) => {\n if (\n url?.startsWith(event.origin) &&\n event.data &&\n typeof event.data === 'object' &&\n event.data.type === 'payload-live-preview'\n ) {\n if (event.data.ready) {\n setAppIsReady(true)\n }\n }\n }\n\n window.addEventListener('message', handleMessage)\n\n setListeningForMessages(true)\n\n return () => {\n window.removeEventListener('message', handleMessage)\n }\n }, [url, listeningForMessages])\n\n const handleWindowChange = useCallback(\n (type: 'iframe' | 'popup') => {\n setAppIsReady(false)\n setPreviewWindowType(type)\n if (type === 'popup') {\n openPopupWindow()\n }\n },\n [openPopupWindow],\n )\n\n // when the user closes the popup window, switch back to the iframe\n // the `usePopupWindow` reports the `isPopupOpen` state for us to use here\n useEffect(() => {\n const newPreviewWindowType = isPopupOpen ? 'popup' : 'iframe'\n\n if (newPreviewWindowType !== previewWindowType) {\n handleWindowChange('iframe')\n }\n }, [previewWindowType, isPopupOpen, handleWindowChange])\n\n useEffect(() => {\n if (isFirstRender.current) {\n isFirstRender.current = false\n return\n }\n\n void setPreference<CollectionPreferences>(\n collectionSlug ? `collection-${collectionSlug}` : `global-${globalSlug}`,\n {\n editViewType: isLivePreviewing ? 'live-preview' : 'default',\n },\n true,\n )\n }, [isLivePreviewing, setPreference, collectionSlug, globalSlug])\n\n const dndContextID = useId()\n\n return (\n <LivePreviewContext\n value={{\n appIsReady,\n breakpoint,\n breakpoints,\n iframeRef,\n isLivePreviewEnabled,\n isLivePreviewing,\n isPopupOpen,\n isPreviewEnabled,\n listeningForMessages,\n loadedURL,\n measuredDeviceSize,\n openPopupWindow,\n popupRef,\n previewURL,\n previewWindowType,\n setAppIsReady,\n setBreakpoint,\n setHeight,\n setIsLivePreviewing,\n setLoadedURL,\n setMeasuredDeviceSize,\n setPreviewURL,\n setPreviewWindowType: handleWindowChange,\n setSize,\n setToolbarPosition: setPosition,\n setURL: setLivePreviewURL,\n setWidth,\n setZoom,\n size,\n toolbarPosition: position,\n typeofLivePreviewURL,\n url,\n zoom,\n }}\n >\n <DndContext\n collisionDetection={customCollisionDetection}\n // Provide stable ID to fix hydration issues: https://github.com/clauderic/dnd-kit/issues/926\n id={dndContextID}\n onDragEnd={handleDragEnd}\n >\n {children}\n </DndContext>\n </LivePreviewContext>\n )\n}\n"],"mappings":"AAAA;;;AAGA,SAASA,UAAU,QAAQ;AAC3B,OAAOC,KAAA,IAASC,WAAW,EAAEC,SAAS,EAAEC,KAAK,EAAEC,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ;AAIhF,SAASC,cAAc,QAAQ;AAC/B,SAASC,eAAe,QAAQ;AAChC,SAASC,cAAc,QAAQ;AAC/B,SAASC,iBAAiB,QAAQ;AAClC,SAASC,wBAAwB,QAAQ;AACzC,SAASC,kBAAkB,QAAQ;AACnC,SAASC,WAAW,QAAQ;AAsB5B,OAAO,MAAMC,mBAAA,GAA0DA,CAAC;EACtEC,WAAA,EAAaC,mBAAmB;EAChCC,QAAQ;EACRC,oBAAoB;EACpBC,gBAAA,EAAkBC,wBAAwB;EAC1CC,gBAAgB;EAChBC,UAAA,EAAYC,mBAAmB;EAC/BC,oBAAoB;EACpBC,G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ACRS,QAAA;MACArB,OAAA;MACAK,IAAA;MACAuC,eAAA,EAAiB3C,QAAA;MACjB7B,oBAAA;MACAC,GAAA;MACA0B;IACF;cAEA,aAAA2C,IAAA,CAAC/F,UAAA;MACCkG,kBAAA,EAAoBtF,wBAAA;MACpB;MACA2D,EAAA,EAAIuB,YAAA;MACJK,SAAA,EAAW/B,aAAA;gBAEVlD;;;AAIT","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"index.js","names":["DndContext","React","useCallback","useEffect","useId","useMemo","useRef","useState","usePopupWindow","useDocumentInfo","usePreferences","formatAbsoluteURL","customCollisionDetection","LivePreviewContext","sizeReducer","LivePreviewProvider","breakpoints","incomingBreakpoints","children","isLivePreviewEnabled","isLivePreviewing","incomingIsLivePreviewing","isPreviewEnabled","previewURL","previewURLFromProps","typeofLivePreviewURL","url","urlFromProps","previewWindowType","setPreviewWindowType","_setIsLivePreviewing","shouldRenderIframe","setShouldRenderIframe","setIsLivePreviewing","livePreviewing","name","height","label","width","setURL","setPreviewURL","isPopupOpen","openPopupWindow","popupRef","eventType","appIsReady","setAppIsReady","listeningForMessages","setListeningForMessages","collectionSlug","globalSlug","isFirstRender","setPreference","iframeRef","loadedURL","setLoadedURL","zoom","setZoom","position","setPosition","x","y","size","setSize","useReducer","measuredDeviceSize","setMeasuredDeviceSize","breakpoint","setBreakpoint","setLivePreviewURL","_incomingURL","incomingURL","handleDragEnd","ev","over","id","newPos","delta","setWidth","type","value","setHeight","foundBreakpoint","find","bp","handleMessage","event","startsWith","origin","data","ready","window","addEventListener","removeEventListener","handleWindowChange","newPreviewWindowType","current","editViewType","dndContextID","_jsx","setToolbarPosition","toolbarPosition","collisionDetection","onDragEnd"],"sources":["../../../src/providers/LivePreview/index.tsx"],"sourcesContent":["'use client'\nimport type { CollectionPreferences, LivePreviewConfig, LivePreviewURLType } from 'payload'\n\nimport { DndContext } from '@dnd-kit/core'\nimport React, { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\n\nimport type { LivePreviewContextType } from './context.js'\n\nimport { usePopupWindow } from '../../hooks/usePopupWindow.js'\nimport { useDocumentInfo } from '../../providers/DocumentInfo/index.js'\nimport { usePreferences } from '../../providers/Preferences/index.js'\nimport { formatAbsoluteURL } from '../../utilities/formatAbsoluteURL.js'\nimport { customCollisionDetection } from './collisionDetection.js'\nimport { LivePreviewContext } from './context.js'\nimport { sizeReducer } from './sizeReducer.js'\n\nexport type LivePreviewProviderProps = {\n appIsReady?: boolean\n breakpoints?: LivePreviewConfig['breakpoints']\n children: React.ReactNode\n deviceSize?: {\n height: number\n width: number\n }\n isLivePreviewEnabled?: boolean\n isLivePreviewing: boolean\n /**\n * This specifically relates to `admin.preview` function in the config instead of live preview.\n */\n isPreviewEnabled?: boolean\n /**\n * This specifically relates to `admin.preview` function in the config instead of live preview.\n */\n previewURL?: string\n} & Pick<LivePreviewContextType, 'typeofLivePreviewURL' | 'url'>\n\nexport const LivePreviewProvider: React.FC<LivePreviewProviderProps> = ({\n breakpoints: incomingBreakpoints,\n children,\n isLivePreviewEnabled,\n isLivePreviewing: incomingIsLivePreviewing,\n isPreviewEnabled,\n previewURL: previewURLFromProps,\n typeofLivePreviewURL,\n url: urlFromProps,\n}) => {\n const [previewWindowType, setPreviewWindowType] = useState<'iframe' | 'popup'>('iframe')\n\n const [isLivePreviewing, _setIsLivePreviewing] =\n useState<LivePreviewContextType['isLivePreviewing']>(incomingIsLivePreviewing)\n\n const [shouldRenderIframe, setShouldRenderIframe] =\n useState<LivePreviewContextType['shouldRenderIframe']>(isLivePreviewing)\n\n /**\n * Rendering the iframe is a one-way event, e.g. defer load and never unmount.\n * This way, subsequent toggles will appear to load instantly.\n */\n const setIsLivePreviewing = useCallback<LivePreviewContextType['setIsLivePreviewing']>(\n (livePreviewing) => {\n if (livePreviewing) {\n setShouldRenderIframe(true)\n }\n\n _setIsLivePreviewing(livePreviewing)\n },\n [],\n )\n\n const breakpoints: LivePreviewConfig['breakpoints'] = useMemo(\n () => [\n ...(incomingBreakpoints || []),\n {\n name: 'responsive',\n height: '100%',\n label: 'Responsive',\n width: '100%',\n },\n ],\n [incomingBreakpoints],\n )\n\n const [url, setURL] = useState<string>('')\n const [previewURL, setPreviewURL] = useState<string>(previewURLFromProps)\n\n const { isPopupOpen, openPopupWindow, popupRef } = usePopupWindow({\n eventType: 'payload-live-preview',\n url,\n })\n\n const [appIsReady, setAppIsReady] = useState(false)\n const [listeningForMessages, setListeningForMessages] = useState(false)\n\n const { collectionSlug, globalSlug } = useDocumentInfo()\n\n const isFirstRender = useRef(true)\n\n const { setPreference } = usePreferences()\n\n const iframeRef = React.useRef<HTMLIFrameElement>(null)\n\n const [loadedURL, setLoadedURL] = useState<string>()\n\n const [zoom, setZoom] = useState(1)\n\n const [position, setPosition] = useState({ x: 0, y: 0 })\n\n const [size, setSize] = React.useReducer(sizeReducer, { height: 0, width: 0 })\n\n const [measuredDeviceSize, setMeasuredDeviceSize] = useState({\n height: 0,\n width: 0,\n })\n\n const [breakpoint, setBreakpoint] =\n React.useState<LivePreviewConfig['breakpoints'][0]['name']>('responsive')\n\n /**\n * A \"middleware\" callback fn that does some additional work before `setURL`.\n * This is what we provide through context, bc it:\n * - ensures the URL is absolute\n * - resets `appIsReady` to `false` while the new URL is loading\n */\n const setLivePreviewURL = useCallback<LivePreviewContextType['setURL']>(\n (_incomingURL) => {\n let incomingURL: LivePreviewURLType\n\n if (typeof _incomingURL === 'string') {\n incomingURL = formatAbsoluteURL(_incomingURL)\n }\n\n if (!incomingURL) {\n setIsLivePreviewing(false)\n }\n\n if (incomingURL !== url) {\n setAppIsReady(false)\n setURL(incomingURL)\n }\n },\n [url],\n )\n\n /**\n * `url` needs to be relative to the window, which cannot be done on initial render.\n */\n useEffect(() => {\n if (typeof urlFromProps === 'string') {\n setURL(formatAbsoluteURL(urlFromProps))\n }\n }, [urlFromProps])\n\n // The toolbar needs to freely drag and drop around the page\n const handleDragEnd = (ev) => {\n // only update position if the toolbar is completely within the preview area\n // otherwise reset it back to the previous position\n // TODO: reset to the nearest edge of the preview area\n if (ev.over && ev.over.id === 'live-preview-area') {\n const newPos = {\n x: position.x + ev.delta.x,\n y: position.y + ev.delta.y,\n }\n\n setPosition(newPos)\n } else {\n // reset\n }\n }\n\n const setWidth = useCallback(\n (width) => {\n setSize({ type: 'width', value: width })\n },\n [setSize],\n )\n\n const setHeight = useCallback(\n (height) => {\n setSize({ type: 'height', value: height })\n },\n [setSize],\n )\n\n // explicitly set new width and height when as new breakpoints are selected\n // exclude `custom` breakpoint as it is handled by the `setWidth` and `setHeight` directly\n useEffect(() => {\n const foundBreakpoint = breakpoints?.find((bp) => bp.name === breakpoint)\n\n if (\n foundBreakpoint &&\n breakpoint !== 'responsive' &&\n breakpoint !== 'custom' &&\n typeof foundBreakpoint?.width === 'number' &&\n typeof foundBreakpoint?.height === 'number'\n ) {\n setSize({\n type: 'reset',\n value: {\n height: foundBreakpoint.height,\n width: foundBreakpoint.width,\n },\n })\n }\n }, [breakpoint, breakpoints])\n\n /**\n * Receive the `ready` message from the popup window\n * This indicates that the app is ready to receive `window.postMessage` events\n * This is also the only cross-origin way of detecting when a popup window has loaded\n * Unlike iframe elements which have an `onLoad` handler, there is no way to access `window.open` on popups\n */\n useEffect(() => {\n const handleMessage = (event: MessageEvent) => {\n if (\n url?.startsWith(event.origin) &&\n event.data &&\n typeof event.data === 'object' &&\n event.data.type === 'payload-live-preview'\n ) {\n if (event.data.ready) {\n setAppIsReady(true)\n }\n }\n }\n\n window.addEventListener('message', handleMessage)\n\n setListeningForMessages(true)\n\n return () => {\n window.removeEventListener('message', handleMessage)\n }\n }, [url, listeningForMessages])\n\n const handleWindowChange = useCallback(\n (type: 'iframe' | 'popup') => {\n setAppIsReady(false)\n setPreviewWindowType(type)\n if (type === 'popup') {\n openPopupWindow()\n }\n },\n [openPopupWindow],\n )\n\n // when the user closes the popup window, switch back to the iframe\n // the `usePopupWindow` reports the `isPopupOpen` state for us to use here\n useEffect(() => {\n const newPreviewWindowType = isPopupOpen ? 'popup' : 'iframe'\n\n if (newPreviewWindowType !== previewWindowType) {\n handleWindowChange('iframe')\n }\n }, [previewWindowType, isPopupOpen, handleWindowChange])\n\n useEffect(() => {\n if (isFirstRender.current) {\n isFirstRender.current = false\n return\n }\n\n void setPreference<CollectionPreferences>(\n collectionSlug ? `collection-${collectionSlug}` : `global-${globalSlug}`,\n {\n editViewType: isLivePreviewing ? 'live-preview' : 'default',\n },\n true,\n )\n }, [isLivePreviewing, setPreference, collectionSlug, globalSlug])\n\n const dndContextID = useId()\n\n return (\n <LivePreviewContext\n value={{\n appIsReady,\n breakpoint,\n breakpoints,\n iframeRef,\n isLivePreviewEnabled,\n isLivePreviewing,\n isPopupOpen,\n isPreviewEnabled,\n listeningForMessages,\n loadedURL,\n measuredDeviceSize,\n openPopupWindow,\n popupRef,\n previewURL,\n previewWindowType,\n setAppIsReady,\n setBreakpoint,\n setHeight,\n setIsLivePreviewing,\n setLoadedURL,\n setMeasuredDeviceSize,\n setPreviewURL,\n setPreviewWindowType: handleWindowChange,\n setSize,\n setToolbarPosition: setPosition,\n setURL: setLivePreviewURL,\n setWidth,\n setZoom,\n shouldRenderIframe,\n size,\n toolbarPosition: position,\n typeofLivePreviewURL,\n url,\n zoom,\n }}\n >\n <DndContext\n collisionDetection={customCollisionDetection}\n // Provide stable ID to fix hydration issues: https://github.com/clauderic/dnd-kit/issues/926\n id={dndContextID}\n onDragEnd={handleDragEnd}\n >\n {children}\n </DndContext>\n </LivePreviewContext>\n )\n}\n"],"mappings":"AAAA;;;AAGA,SAASA,UAAU,QAAQ;AAC3B,OAAOC,KAAA,IAASC,WAAW,EAAEC,SAAS,EAAEC,KAAK,EAAEC,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ;AAIhF,SAASC,cAAc,QAAQ;AAC/B,SAASC,eAAe,QAAQ;AAChC,SAASC,cAAc,QAAQ;AAC/B,SAASC,iBAAiB,QAAQ;AAClC,SAASC,wBAAwB,QAAQ;AACzC,SAASC,kBAA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