@payloadcms/live-preview-react 1.0.0-beta.3 → 3.0.0-beta.16
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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -22
- package/dist/index.js.map +1 -0
- package/package.json +21 -9
- package/src/index.ts +0 -43
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,cAAc;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,cAAc,6BAA0B;IACnD,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,CAAC,CAAA;IACd,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG;IACF,IAAI,EAAE,CAAC,CAAA;IACP,SAAS,EAAE,OAAO,CAAA;CAsCnB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,38 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const useLivePreview = (props)=>{
|
|
14
|
-
const { depth = 0, initialData, serverURL } = props;
|
|
15
|
-
const [data, setData] = (0, _react.useState)(initialData);
|
|
16
|
-
const [isLoading, setIsLoading] = (0, _react.useState)(true);
|
|
17
|
-
const onChange = (0, _react.useCallback)((mergedData)=>{
|
|
1
|
+
import { ready, subscribe, unsubscribe } from '@payloadcms/live-preview';
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
3
|
+
// To prevent the flicker of missing data on initial load,
|
|
4
|
+
// you can pass in the initial page data from the server
|
|
5
|
+
// To prevent the flicker of stale data while the post message is being sent,
|
|
6
|
+
// you can conditionally render loading UI based on the `isLoading` state
|
|
7
|
+
export const useLivePreview = (props)=>{
|
|
8
|
+
const { apiRoute, depth, initialData, serverURL } = props;
|
|
9
|
+
const [data, setData] = useState(initialData);
|
|
10
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
11
|
+
const hasSentReadyMessage = useRef(false);
|
|
12
|
+
const onChange = useCallback((mergedData)=>{
|
|
18
13
|
setData(mergedData);
|
|
19
14
|
setIsLoading(false);
|
|
20
15
|
}, []);
|
|
21
|
-
|
|
22
|
-
const subscription =
|
|
16
|
+
useEffect(()=>{
|
|
17
|
+
const subscription = subscribe({
|
|
18
|
+
apiRoute,
|
|
23
19
|
callback: onChange,
|
|
24
20
|
depth,
|
|
25
21
|
initialData,
|
|
26
22
|
serverURL
|
|
27
23
|
});
|
|
24
|
+
if (!hasSentReadyMessage.current) {
|
|
25
|
+
hasSentReadyMessage.current = true;
|
|
26
|
+
ready({
|
|
27
|
+
serverURL
|
|
28
|
+
});
|
|
29
|
+
}
|
|
28
30
|
return ()=>{
|
|
29
|
-
|
|
31
|
+
unsubscribe(subscription);
|
|
30
32
|
};
|
|
31
33
|
}, [
|
|
32
34
|
serverURL,
|
|
33
35
|
onChange,
|
|
34
36
|
depth,
|
|
35
|
-
initialData
|
|
37
|
+
initialData,
|
|
38
|
+
apiRoute
|
|
36
39
|
]);
|
|
37
40
|
return {
|
|
38
41
|
data,
|
|
@@ -40,4 +43,4 @@ const useLivePreview = (props)=>{
|
|
|
40
43
|
};
|
|
41
44
|
};
|
|
42
45
|
|
|
43
|
-
//# sourceMappingURL=
|
|
46
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { ready, subscribe, unsubscribe } from '@payloadcms/live-preview'\nimport { useCallback, useEffect, useRef, useState } from 'react'\n\n// To prevent the flicker of missing data on initial load,\n// you can pass in the initial page data from the server\n// To prevent the flicker of stale data while the post message is being sent,\n// you can conditionally render loading UI based on the `isLoading` state\n\nexport const useLivePreview = <T extends any>(props: {\n apiRoute?: string\n depth?: number\n initialData: T\n serverURL: string\n}): {\n data: T\n isLoading: boolean\n} => {\n const { apiRoute, depth, initialData, serverURL } = props\n const [data, setData] = useState<T>(initialData)\n const [isLoading, setIsLoading] = useState<boolean>(true)\n const hasSentReadyMessage = useRef<boolean>(false)\n\n const onChange = useCallback((mergedData) => {\n setData(mergedData)\n setIsLoading(false)\n }, [])\n\n useEffect(() => {\n const subscription = subscribe({\n apiRoute,\n callback: onChange,\n depth,\n initialData,\n serverURL,\n })\n\n if (!hasSentReadyMessage.current) {\n hasSentReadyMessage.current = true\n\n ready({\n serverURL,\n })\n }\n\n return () => {\n unsubscribe(subscription)\n }\n }, [serverURL, onChange, depth, initialData, apiRoute])\n\n return {\n data,\n isLoading,\n }\n}\n"],"names":["ready","subscribe","unsubscribe","useCallback","useEffect","useRef","useState","useLivePreview","props","apiRoute","depth","initialData","serverURL","data","setData","isLoading","setIsLoading","hasSentReadyMessage","onChange","mergedData","subscription","callback","current"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,SAASA,KAAK,EAAEC,SAAS,EAAEC,WAAW,QAAQ,2BAA0B;AACxE,SAASC,WAAW,EAAEC,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,QAAO;AAEhE,0DAA0D;AAC1D,wDAAwD;AACxD,6EAA6E;AAC7E,yEAAyE;AAEzE,OAAO,MAAMC,iBAAiB,CAAgBC;IAS5C,MAAM,EAAEC,QAAQ,EAAEC,KAAK,EAAEC,WAAW,EAAEC,SAAS,EAAE,GAAGJ;IACpD,MAAM,CAACK,MAAMC,QAAQ,GAAGR,SAAYK;IACpC,MAAM,CAACI,WAAWC,aAAa,GAAGV,SAAkB;IACpD,MAAMW,sBAAsBZ,OAAgB;IAE5C,MAAMa,WAAWf,YAAY,CAACgB;QAC5BL,QAAQK;QACRH,aAAa;IACf,GAAG,EAAE;IAELZ,UAAU;QACR,MAAMgB,eAAenB,UAAU;YAC7BQ;YACAY,UAAUH;YACVR;YACAC;YACAC;QACF;QAEA,IAAI,CAACK,oBAAoBK,OAAO,EAAE;YAChCL,oBAAoBK,OAAO,GAAG;YAE9BtB,MAAM;gBACJY;YACF;QACF;QAEA,OAAO;YACLV,YAAYkB;QACd;IACF,GAAG;QAACR;QAAWM;QAAUR;QAAOC;QAAaF;KAAS;IAEtD,OAAO;QACLI;QACAE;IACF;AACF,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,24 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/live-preview-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-beta.16",
|
|
4
4
|
"description": "The official live preview React SDK for Payload",
|
|
5
|
-
"repository":
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/payloadcms/payload.git",
|
|
8
|
+
"directory": "packages/live-preview-react"
|
|
9
|
+
},
|
|
6
10
|
"license": "MIT",
|
|
7
11
|
"homepage": "https://payloadcms.com",
|
|
8
12
|
"author": "Payload CMS, Inc.",
|
|
9
13
|
"main": "./dist/index.js",
|
|
10
14
|
"types": "./dist/index.d.ts",
|
|
15
|
+
"type": "module",
|
|
11
16
|
"dependencies": {
|
|
12
|
-
"
|
|
13
|
-
"@payloadcms/live-preview": "1.0.0-beta.3"
|
|
17
|
+
"@payloadcms/live-preview": "^0.x"
|
|
14
18
|
},
|
|
15
19
|
"devDependencies": {
|
|
16
|
-
"@types/
|
|
17
|
-
"@
|
|
18
|
-
"payload": "
|
|
19
|
-
|
|
20
|
+
"@types/react": "18.2.74",
|
|
21
|
+
"@payloadcms/eslint-config": "3.0.0-beta.16",
|
|
22
|
+
"payload": "3.0.0-beta.16"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
|
26
|
+
},
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": "./dist/index.js",
|
|
30
|
+
"require": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts"
|
|
32
|
+
}
|
|
20
33
|
},
|
|
21
|
-
"exports": null,
|
|
22
34
|
"publishConfig": {
|
|
23
35
|
"registry": "https://registry.npmjs.org/"
|
|
24
36
|
},
|
package/src/index.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { subscribe, unsubscribe } from '@payloadcms/live-preview'
|
|
2
|
-
import { useCallback, useEffect, useState } from 'react'
|
|
3
|
-
|
|
4
|
-
// To prevent the flicker of missing data on initial load,
|
|
5
|
-
// you can pass in the initial page data from the server
|
|
6
|
-
// To prevent the flicker of stale data while the post message is being sent,
|
|
7
|
-
// you can conditionally render loading UI based on the `isLoading` state
|
|
8
|
-
|
|
9
|
-
export const useLivePreview = <T extends any>(props: {
|
|
10
|
-
depth?: number
|
|
11
|
-
initialData: T
|
|
12
|
-
serverURL: string
|
|
13
|
-
}): {
|
|
14
|
-
data: T
|
|
15
|
-
isLoading: boolean
|
|
16
|
-
} => {
|
|
17
|
-
const { depth = 0, initialData, serverURL } = props
|
|
18
|
-
const [data, setData] = useState<T>(initialData)
|
|
19
|
-
const [isLoading, setIsLoading] = useState<boolean>(true)
|
|
20
|
-
|
|
21
|
-
const onChange = useCallback((mergedData) => {
|
|
22
|
-
setData(mergedData)
|
|
23
|
-
setIsLoading(false)
|
|
24
|
-
}, [])
|
|
25
|
-
|
|
26
|
-
useEffect(() => {
|
|
27
|
-
const subscription = subscribe({
|
|
28
|
-
callback: onChange,
|
|
29
|
-
depth,
|
|
30
|
-
initialData,
|
|
31
|
-
serverURL,
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
return () => {
|
|
35
|
-
unsubscribe(subscription)
|
|
36
|
-
}
|
|
37
|
-
}, [serverURL, onChange, depth, initialData])
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
data,
|
|
41
|
-
isLoading,
|
|
42
|
-
}
|
|
43
|
-
}
|