@payloadcms/live-preview-react 1.0.0-beta.1
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 +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/package.json +35 -0
- package/src/index.ts +43 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,cAAc;YACjB,MAAM;;eAEH,MAAM;;;eAGN,OAAO;CA4BnB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "useLivePreview", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return useLivePreview;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _livepreview = require("@payloadcms/live-preview");
|
|
12
|
+
const _react = require("react");
|
|
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)=>{
|
|
18
|
+
setData(mergedData);
|
|
19
|
+
setIsLoading(false);
|
|
20
|
+
}, []);
|
|
21
|
+
(0, _react.useEffect)(()=>{
|
|
22
|
+
const subscription = (0, _livepreview.subscribe)({
|
|
23
|
+
callback: onChange,
|
|
24
|
+
depth,
|
|
25
|
+
initialData,
|
|
26
|
+
serverURL
|
|
27
|
+
});
|
|
28
|
+
return ()=>{
|
|
29
|
+
(0, _livepreview.unsubscribe)(subscription);
|
|
30
|
+
};
|
|
31
|
+
}, [
|
|
32
|
+
serverURL,
|
|
33
|
+
onChange,
|
|
34
|
+
depth,
|
|
35
|
+
initialData
|
|
36
|
+
]);
|
|
37
|
+
return {
|
|
38
|
+
data,
|
|
39
|
+
isLoading
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9pbmRleC50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgeyBzdWJzY3JpYmUsIHVuc3Vic2NyaWJlIH0gZnJvbSAnQHBheWxvYWRjbXMvbGl2ZS1wcmV2aWV3J1xuaW1wb3J0IHsgdXNlQ2FsbGJhY2ssIHVzZUVmZmVjdCwgdXNlU3RhdGUgfSBmcm9tICdyZWFjdCdcblxuLy8gVG8gcHJldmVudCB0aGUgZmxpY2tlciBvZiBtaXNzaW5nIGRhdGEgb24gaW5pdGlhbCBsb2FkLFxuLy8geW91IGNhbiBwYXNzIGluIHRoZSBpbml0aWFsIHBhZ2UgZGF0YSBmcm9tIHRoZSBzZXJ2ZXJcbi8vIFRvIHByZXZlbnQgdGhlIGZsaWNrZXIgb2Ygc3RhbGUgZGF0YSB3aGlsZSB0aGUgcG9zdCBtZXNzYWdlIGlzIGJlaW5nIHNlbnQsXG4vLyB5b3UgY2FuIGNvbmRpdGlvbmFsbHkgcmVuZGVyIGxvYWRpbmcgVUkgYmFzZWQgb24gdGhlIGBpc0xvYWRpbmdgIHN0YXRlXG5cbmV4cG9ydCBjb25zdCB1c2VMaXZlUHJldmlldyA9IDxUIGV4dGVuZHMgYW55Pihwcm9wczoge1xuICBkZXB0aD86IG51bWJlclxuICBpbml0aWFsRGF0YTogVFxuICBzZXJ2ZXJVUkw6IHN0cmluZ1xufSk6IHtcbiAgZGF0YTogVFxuICBpc0xvYWRpbmc6IGJvb2xlYW5cbn0gPT4ge1xuICBjb25zdCB7IGRlcHRoID0gMCwgaW5pdGlhbERh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FBQTs7OzZCQVIwQjt1QkFDVTtBQU8xQyxNQUFNQSxpQkFBaUIsQ0FBZ0JDO0lBUTVDLE1BQU0sRUFBRUMsUUFBUSxDQUFDLEVBQUVDLFdBQVcsRUFBRUMsU0FBUyxFQUFFLEdBQUdIO0lBQzlDLE1BQU0sQ0FBQ0ksTUFBTUMsUUFBUSxHQUFHQyxJQUFBQSxlQUFRLEVBQUlKO0lBQ3BDLE1BQU0sQ0FBQ0ssV0FBV0MsYUFBYSxHQUFHRixJQUFBQSxlQUFRLEVBQVU7SUFFcEQsTUFBTUcsV0FBV0MsSUFBQUEsa0JBQVcsRUFBQyxDQUFDQztRQUM1Qk4sUUFBUU07UUFDUkgsYUFBYTtJQUNmLEdBQUcsRUFBRTtJQUVMSSxJQUFBQSxnQkFBUyxFQUFDO1FBQ1IsTUFBTUMsZUFBZUMsSUFBQUEsc0JBQVMsRUFBQztZQUM3QkMsVUFBVU47WUFDVlI7WUFDQUM7WUFDQUM7UUFDRjtRQUVBLE9BQU87WUFDTGEsSUFBQUEsd0JBQVcsRUFBQ0g7UUFDZDtJQUNGLEdBQUc7UUFBQ1Y7UUFBV007UUFBVVI7UUFBT0M7S0FBWTtJQUU1QyxPQUFPO1FBQ0xFO1FBQ0FHO0lBQ0Y7QUFDRiJ9
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@payloadcms/live-preview-react",
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
|
+
"description": "The official live preview React SDK for Payload",
|
|
5
|
+
"repository": "https://github.com/payloadcms/payload",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://payloadcms.com",
|
|
8
|
+
"author": "Payload CMS, Inc.",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"react": "18.2.0",
|
|
13
|
+
"@payloadcms/live-preview": "1.0.0-beta.1"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/node": "20.5.7",
|
|
17
|
+
"@types/react": "18.2.15",
|
|
18
|
+
"payload": "2.0.0-beta.15",
|
|
19
|
+
"@payloadcms/eslint-config": "0.0.1"
|
|
20
|
+
},
|
|
21
|
+
"exports": null,
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"registry": "https://registry.npmjs.org/"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "pnpm copyfiles && pnpm build:swc && pnpm build:types",
|
|
30
|
+
"build:swc": "swc ./src -d ./dist --config-file .swcrc",
|
|
31
|
+
"build:types": "tsc --emitDeclarationOnly --outDir dist",
|
|
32
|
+
"clean": "rimraf {dist,*.tsbuildinfo}",
|
|
33
|
+
"copyfiles": "copyfiles -u 1 \"src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,png,json}\" dist/"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
}
|