@payloadcms/live-preview-react 3.0.0-beta.16 → 3.0.0-beta.27

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.
@@ -0,0 +1,8 @@
1
+ import type React from 'react';
2
+ export declare const RefreshRouteOnSave: React.FC<{
3
+ apiRoute?: string;
4
+ depth?: number;
5
+ refresh: () => void;
6
+ serverURL: string;
7
+ }>;
8
+ //# sourceMappingURL=RefreshRouteOnSave.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RefreshRouteOnSave.d.ts","sourceRoot":"","sources":["../src/RefreshRouteOnSave.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAK9B,eAAO,MAAM,kBAAkB,EAAE,KAAK,CAAC,EAAE,CAAC;IACxC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,IAAI,CAAA;IACnB,SAAS,EAAE,MAAM,CAAA;CAClB,CAuCA,CAAA"}
@@ -0,0 +1,44 @@
1
+ 'use client';
2
+ import { isDocumentEvent, ready } from '@payloadcms/live-preview';
3
+ import { useCallback, useEffect, useRef } from 'react';
4
+ export const RefreshRouteOnSave = (props)=>{
5
+ const { apiRoute, depth, refresh, serverURL } = props;
6
+ const hasSentReadyMessage = useRef(false);
7
+ const onMessage = useCallback((event)=>{
8
+ if (isDocumentEvent(event, serverURL)) {
9
+ if (typeof refresh === 'function') {
10
+ refresh();
11
+ } else {
12
+ // eslint-disable-next-line no-console
13
+ console.error('You must provide a refresh function to `RefreshRouteOnSave`');
14
+ }
15
+ }
16
+ }, [
17
+ refresh,
18
+ serverURL
19
+ ]);
20
+ useEffect(()=>{
21
+ if (typeof window !== 'undefined') {
22
+ window.addEventListener('message', onMessage);
23
+ }
24
+ if (!hasSentReadyMessage.current) {
25
+ hasSentReadyMessage.current = true;
26
+ ready({
27
+ serverURL
28
+ });
29
+ }
30
+ return ()=>{
31
+ if (typeof window !== 'undefined') {
32
+ window.removeEventListener('message', onMessage);
33
+ }
34
+ };
35
+ }, [
36
+ serverURL,
37
+ onMessage,
38
+ depth,
39
+ apiRoute
40
+ ]);
41
+ return null;
42
+ };
43
+
44
+ //# sourceMappingURL=RefreshRouteOnSave.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/RefreshRouteOnSave.tsx"],"sourcesContent":["'use client'\n\nimport type React from 'react'\n\nimport { isDocumentEvent, ready } from '@payloadcms/live-preview'\nimport { useCallback, useEffect, useRef } from 'react'\n\nexport const RefreshRouteOnSave: React.FC<{\n apiRoute?: string\n depth?: number\n refresh: () => void\n serverURL: string\n}> = (props) => {\n const { apiRoute, depth, refresh, serverURL } = props\n const hasSentReadyMessage = useRef<boolean>(false)\n\n const onMessage = useCallback(\n (event: MessageEvent) => {\n if (isDocumentEvent(event, serverURL)) {\n if (typeof refresh === 'function') {\n refresh()\n } else {\n // eslint-disable-next-line no-console\n console.error('You must provide a refresh function to `RefreshRouteOnSave`')\n }\n }\n },\n [refresh, serverURL],\n )\n\n useEffect(() => {\n if (typeof window !== 'undefined') {\n window.addEventListener('message', onMessage)\n }\n\n if (!hasSentReadyMessage.current) {\n hasSentReadyMessage.current = true\n\n ready({\n serverURL,\n })\n }\n\n return () => {\n if (typeof window !== 'undefined') {\n window.removeEventListener('message', onMessage)\n }\n }\n }, [serverURL, onMessage, depth, apiRoute])\n\n return null\n}\n"],"names":["isDocumentEvent","ready","useCallback","useEffect","useRef","RefreshRouteOnSave","props","apiRoute","depth","refresh","serverURL","hasSentReadyMessage","onMessage","event","console","error","window","addEventListener","current","removeEventListener"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA;AAIA,SAASA,eAAe,EAAEC,KAAK,QAAQ,2BAA0B;AACjE,SAASC,WAAW,EAAEC,SAAS,EAAEC,MAAM,QAAQ,QAAO;AAEtD,OAAO,MAAMC,qBAKR,CAACC;IACJ,MAAM,EAAEC,QAAQ,EAAEC,KAAK,EAAEC,OAAO,EAAEC,SAAS,EAAE,GAAGJ;IAChD,MAAMK,sBAAsBP,OAAgB;IAE5C,MAAMQ,YAAYV,YAChB,CAACW;QACC,IAAIb,gBAAgBa,OAAOH,YAAY;YACrC,IAAI,OAAOD,YAAY,YAAY;gBACjCA;YACF,OAAO;gBACL,sCAAsC;gBACtCK,QAAQC,KAAK,CAAC;YAChB;QACF;IACF,GACA;QAACN;QAASC;KAAU;IAGtBP,UAAU;QACR,IAAI,OAAOa,WAAW,aAAa;YACjCA,OAAOC,gBAAgB,CAAC,WAAWL;QACrC;QAEA,IAAI,CAACD,oBAAoBO,OAAO,EAAE;YAChCP,oBAAoBO,OAAO,GAAG;YAE9BjB,MAAM;gBACJS;YACF;QACF;QAEA,OAAO;YACL,IAAI,OAAOM,WAAW,aAAa;gBACjCA,OAAOG,mBAAmB,CAAC,WAAWP;YACxC;QACF;IACF,GAAG;QAACF;QAAWE;QAAWJ;QAAOD;KAAS;IAE1C,OAAO;AACT,EAAC"}
package/dist/index.d.ts CHANGED
@@ -1,10 +1,3 @@
1
- export declare const useLivePreview: <T extends unknown>(props: {
2
- apiRoute?: string;
3
- depth?: number;
4
- initialData: T;
5
- serverURL: string;
6
- }) => {
7
- data: T;
8
- isLoading: boolean;
9
- };
1
+ export { RefreshRouteOnSave } from './RefreshRouteOnSave.js';
2
+ export { useLivePreview } from './useLivePreview.js';
10
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA"}
package/dist/index.js CHANGED
@@ -1,46 +1,4 @@
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)=>{
13
- setData(mergedData);
14
- setIsLoading(false);
15
- }, []);
16
- useEffect(()=>{
17
- const subscription = subscribe({
18
- apiRoute,
19
- callback: onChange,
20
- depth,
21
- initialData,
22
- serverURL
23
- });
24
- if (!hasSentReadyMessage.current) {
25
- hasSentReadyMessage.current = true;
26
- ready({
27
- serverURL
28
- });
29
- }
30
- return ()=>{
31
- unsubscribe(subscription);
32
- };
33
- }, [
34
- serverURL,
35
- onChange,
36
- depth,
37
- initialData,
38
- apiRoute
39
- ]);
40
- return {
41
- data,
42
- isLoading
43
- };
44
- };
1
+ export { RefreshRouteOnSave } from './RefreshRouteOnSave.js';
2
+ export { useLivePreview } from './useLivePreview.js';
45
3
 
46
4
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +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"}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export { RefreshRouteOnSave } from './RefreshRouteOnSave.js'\nexport { useLivePreview } from './useLivePreview.js'\n"],"names":["RefreshRouteOnSave","useLivePreview"],"rangeMappings":";","mappings":"AAAA,SAASA,kBAAkB,QAAQ,0BAAyB;AAC5D,SAASC,cAAc,QAAQ,sBAAqB"}
@@ -0,0 +1,10 @@
1
+ export declare const useLivePreview: <T extends unknown>(props: {
2
+ apiRoute?: string;
3
+ depth?: number;
4
+ initialData: T;
5
+ serverURL: string;
6
+ }) => {
7
+ data: T;
8
+ isLoading: boolean;
9
+ };
10
+ //# sourceMappingURL=useLivePreview.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useLivePreview.d.ts","sourceRoot":"","sources":["../src/useLivePreview.ts"],"names":[],"mappings":"AASA,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"}
@@ -0,0 +1,47 @@
1
+ 'use client';
2
+ import { ready, subscribe, unsubscribe } from '@payloadcms/live-preview';
3
+ import { useCallback, useEffect, useRef, useState } from 'react';
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
+ export const useLivePreview = (props)=>{
9
+ const { apiRoute, depth, initialData, serverURL } = props;
10
+ const [data, setData] = useState(initialData);
11
+ const [isLoading, setIsLoading] = useState(true);
12
+ const hasSentReadyMessage = useRef(false);
13
+ const onChange = useCallback((mergedData)=>{
14
+ setData(mergedData);
15
+ setIsLoading(false);
16
+ }, []);
17
+ useEffect(()=>{
18
+ const subscription = subscribe({
19
+ apiRoute,
20
+ callback: onChange,
21
+ depth,
22
+ initialData,
23
+ serverURL
24
+ });
25
+ if (!hasSentReadyMessage.current) {
26
+ hasSentReadyMessage.current = true;
27
+ ready({
28
+ serverURL
29
+ });
30
+ }
31
+ return ()=>{
32
+ unsubscribe(subscription);
33
+ };
34
+ }, [
35
+ serverURL,
36
+ onChange,
37
+ depth,
38
+ initialData,
39
+ apiRoute
40
+ ]);
41
+ return {
42
+ data,
43
+ isLoading
44
+ };
45
+ };
46
+
47
+ //# sourceMappingURL=useLivePreview.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/useLivePreview.ts"],"sourcesContent":["'use client'\nimport { 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;AACA,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,42 +1,42 @@
1
1
  {
2
2
  "name": "@payloadcms/live-preview-react",
3
- "version": "3.0.0-beta.16",
3
+ "version": "3.0.0-beta.27",
4
4
  "description": "The official live preview React SDK for Payload",
5
+ "homepage": "https://payloadcms.com",
5
6
  "repository": {
6
7
  "type": "git",
7
8
  "url": "https://github.com/payloadcms/payload.git",
8
9
  "directory": "packages/live-preview-react"
9
10
  },
10
11
  "license": "MIT",
11
- "homepage": "https://payloadcms.com",
12
- "author": "Payload CMS, Inc.",
12
+ "author": "Payload <dev@payloadcms.com> (https://payloadcms.com)",
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.js",
18
+ "types": "./dist/index.d.ts"
19
+ }
20
+ },
13
21
  "main": "./dist/index.js",
14
22
  "types": "./dist/index.d.ts",
15
- "type": "module",
23
+ "files": [
24
+ "dist"
25
+ ],
16
26
  "dependencies": {
17
27
  "@payloadcms/live-preview": "^0.x"
18
28
  },
19
29
  "devDependencies": {
20
30
  "@types/react": "18.2.74",
21
- "@payloadcms/eslint-config": "3.0.0-beta.16",
22
- "payload": "3.0.0-beta.16"
31
+ "payload": "3.0.0-beta.27",
32
+ "@payloadcms/eslint-config": "1.1.1"
23
33
  },
24
34
  "peerDependencies": {
25
35
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0"
26
36
  },
27
- "exports": {
28
- ".": {
29
- "import": "./dist/index.js",
30
- "require": "./dist/index.js",
31
- "types": "./dist/index.d.ts"
32
- }
33
- },
34
37
  "publishConfig": {
35
38
  "registry": "https://registry.npmjs.org/"
36
39
  },
37
- "files": [
38
- "dist"
39
- ],
40
40
  "scripts": {
41
41
  "build": "pnpm copyfiles && pnpm build:swc && pnpm build:types",
42
42
  "build:swc": "swc ./src -d ./dist --config-file .swcrc",