@slicemachine/adapter-next 0.3.38 → 0.3.39-alpha.aa-fix-nextjs-rsc-simulator.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/simulator/SliceSimulator.cjs +57 -10
- package/dist/simulator/SliceSimulator.cjs.map +1 -1
- package/dist/simulator/SliceSimulator.d.ts +7 -7
- package/dist/simulator/SliceSimulator.js +58 -11
- package/dist/simulator/SliceSimulator.js.map +1 -1
- package/dist/simulator/index.d.ts +1 -1
- package/dist/simulator/react-server/SliceSimulator.cjs +3 -67
- package/dist/simulator/react-server/SliceSimulator.cjs.map +1 -1
- package/dist/simulator/react-server/SliceSimulator.d.ts +2 -7
- package/dist/simulator/react-server/SliceSimulator.js +3 -50
- package/dist/simulator/react-server/SliceSimulator.js.map +1 -1
- package/dist/simulator.cjs +1 -1
- package/dist/simulator.js +1 -1
- package/package.json +4 -3
- package/src/simulator/SliceSimulator.tsx +133 -49
- package/src/simulator/index.ts +1 -1
- package/src/simulator/react-server/SliceSimulator.tsx +5 -105
- package/dist/simulator/SliceSimulatorWrapper.cjs +0 -19
- package/dist/simulator/SliceSimulatorWrapper.cjs.map +0 -1
- package/dist/simulator/SliceSimulatorWrapper.d.ts +0 -14
- package/dist/simulator/SliceSimulatorWrapper.js +0 -19
- package/dist/simulator/SliceSimulatorWrapper.js.map +0 -1
- package/dist/simulator/getSlices.cjs +0 -7
- package/dist/simulator/getSlices.cjs.map +0 -1
- package/dist/simulator/getSlices.js +0 -7
- package/dist/simulator/getSlices.js.map +0 -1
- package/src/simulator/SliceSimulatorWrapper.tsx +0 -68
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
5
5
|
const jsxRuntime = require("react/jsx-runtime");
|
|
6
6
|
const React = require("react");
|
|
7
|
+
const lzString = require("lz-string");
|
|
7
8
|
const kit = require("@prismicio/simulator/kit");
|
|
8
|
-
const SliceSimulatorWrapper = require("./SliceSimulatorWrapper.cjs");
|
|
9
9
|
function _interopNamespaceDefault(e) {
|
|
10
10
|
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
11
11
|
if (e) {
|
|
@@ -23,28 +23,75 @@ function _interopNamespaceDefault(e) {
|
|
|
23
23
|
return Object.freeze(n);
|
|
24
24
|
}
|
|
25
25
|
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
|
|
26
|
+
const STATE_PARAMS_KEY = "state";
|
|
27
|
+
const SERVER_REVALIDATE_THROTTLE_DELAY = 300;
|
|
28
|
+
const throttle = (
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
(fn, wait) => {
|
|
31
|
+
let timeoutId;
|
|
32
|
+
let lastCallTime = 0;
|
|
33
|
+
return async (...args) => {
|
|
34
|
+
clearTimeout(timeoutId);
|
|
35
|
+
const now = Date.now();
|
|
36
|
+
const timeSinceLastCall = now - lastCallTime;
|
|
37
|
+
const delayForNextCall = wait - timeSinceLastCall;
|
|
38
|
+
if (delayForNextCall <= 0) {
|
|
39
|
+
lastCallTime = now;
|
|
40
|
+
fn(...args);
|
|
41
|
+
} else {
|
|
42
|
+
timeoutId = setTimeout(() => {
|
|
43
|
+
lastCallTime = Date.now();
|
|
44
|
+
fn(...args);
|
|
45
|
+
}, delayForNextCall);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
);
|
|
26
50
|
const simulatorManager = new kit.SimulatorManager();
|
|
27
|
-
const SliceSimulator = ({ background, zIndex, className,
|
|
28
|
-
|
|
51
|
+
const SliceSimulator = ({ background, zIndex, className, sliceZone: SliceZoneComp, children, __revalidatePath }) => {
|
|
52
|
+
const isServerComponent = Boolean(__revalidatePath);
|
|
53
|
+
if (isServerComponent && SliceZoneComp) {
|
|
54
|
+
throw new Error("The sliceZone prop should not be used when <SliceSimulator> is rendered in a Server Component. Use the getSlices helper or convert your simulator to a Client Component.");
|
|
55
|
+
}
|
|
56
|
+
if (!isServerComponent && !SliceZoneComp) {
|
|
29
57
|
throw new Error("A sliceZone prop must be provided when <SliceZone> is rendered in a Client Component. Add a sliceZone prop or convert your simulator to a Server Component with the getSlices helper.");
|
|
30
58
|
}
|
|
31
59
|
const [slices, setSlices] = React__namespace.useState(() => kit.getDefaultSlices());
|
|
32
60
|
const [message, setMessage] = React__namespace.useState(() => kit.getDefaultMessage());
|
|
61
|
+
const throttledRevalidatePath = React__namespace.useRef(__revalidatePath ? throttle(__revalidatePath, SERVER_REVALIDATE_THROTTLE_DELAY) : void 0);
|
|
62
|
+
const defaultProps = kit.getDefaultProps();
|
|
63
|
+
const hasSlices = slices.length > 0;
|
|
33
64
|
React__namespace.useEffect(() => {
|
|
34
|
-
simulatorManager.state.on(kit.StateEventType.Slices, (
|
|
35
|
-
|
|
65
|
+
simulatorManager.state.on(kit.StateEventType.Slices, (newSlices) => {
|
|
66
|
+
if (isServerComponent) {
|
|
67
|
+
const url = new URL(window.location.href);
|
|
68
|
+
url.searchParams.set(STATE_PARAMS_KEY, lzString.compressToEncodedURIComponent(JSON.stringify(newSlices)));
|
|
69
|
+
window.history.pushState(null, "", url);
|
|
70
|
+
const path = window.location.pathname;
|
|
71
|
+
setTimeout(() => {
|
|
72
|
+
var _a;
|
|
73
|
+
return (_a = throttledRevalidatePath.current) == null ? void 0 : _a.call(throttledRevalidatePath, path);
|
|
74
|
+
}, 0);
|
|
75
|
+
}
|
|
76
|
+
setSlices(newSlices);
|
|
36
77
|
}, "simulator-slices");
|
|
37
|
-
simulatorManager.state.on(kit.StateEventType.Message, (
|
|
38
|
-
setMessage(_message);
|
|
39
|
-
}, "simulator-message");
|
|
78
|
+
simulatorManager.state.on(kit.StateEventType.Message, (newMessage) => setMessage(newMessage), "simulator-message");
|
|
40
79
|
simulatorManager.init();
|
|
41
80
|
return () => {
|
|
42
81
|
simulatorManager.state.off(kit.StateEventType.Slices, "simulator-slices");
|
|
43
82
|
simulatorManager.state.off(kit.StateEventType.Message, "simulator-message");
|
|
44
83
|
};
|
|
45
84
|
}, []);
|
|
46
|
-
|
|
47
|
-
|
|
85
|
+
return jsxRuntime.jsx("div", { className: [kit.simulatorClass, className].filter(Boolean).join(" "), style: {
|
|
86
|
+
zIndex: typeof zIndex === "undefined" ? defaultProps.zIndex : zIndex ?? void 0,
|
|
87
|
+
position: "fixed",
|
|
88
|
+
top: 0,
|
|
89
|
+
left: 0,
|
|
90
|
+
width: "100%",
|
|
91
|
+
height: "100vh",
|
|
92
|
+
overflow: "auto",
|
|
93
|
+
background: typeof background === "undefined" ? defaultProps.background : background ?? void 0
|
|
94
|
+
}, children: message ? jsxRuntime.jsx("article", { dangerouslySetInnerHTML: { __html: message } }) : hasSlices ? jsxRuntime.jsx("div", { id: "root", className: kit.simulatorRootClass, onClickCapture: kit.onClickHandler, onSubmitCapture: kit.disableEventHandler, children: SliceZoneComp ? jsxRuntime.jsx(SliceZoneComp, { slices }) : children }) : null });
|
|
48
95
|
};
|
|
49
96
|
exports.SliceSimulator = SliceSimulator;
|
|
50
97
|
//# sourceMappingURL=SliceSimulator.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SliceSimulator.cjs","sources":["../../../src/simulator/SliceSimulator.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\n\nimport {\n\tSliceSimulatorProps as BaseSliceSimulatorProps,\n\
|
|
1
|
+
{"version":3,"file":"SliceSimulator.cjs","sources":["../../../src/simulator/SliceSimulator.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\n\nimport { compressToEncodedURIComponent } from \"lz-string\";\nimport {\n\tSimulatorManager,\n\tStateEventType,\n\tgetDefaultMessage,\n\tSliceSimulatorProps as BaseSliceSimulatorProps,\n\tSliceSimulatorState,\n\tgetDefaultSlices,\n\tdisableEventHandler,\n\tgetDefaultProps,\n\tonClickHandler,\n\tsimulatorClass,\n\tsimulatorRootClass,\n} from \"@prismicio/simulator/kit\";\n\nconst STATE_PARAMS_KEY = \"state\";\nconst SERVER_REVALIDATE_THROTTLE_DELAY = 300;\n\nconst throttle =\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t<TArgs extends [...any]>(fn: (...args: TArgs) => unknown, wait: number) => {\n\t\tlet timeoutId: ReturnType<typeof setTimeout>;\n\t\tlet lastCallTime = 0;\n\n\t\treturn async (...args: TArgs) => {\n\t\t\tclearTimeout(timeoutId);\n\n\t\t\tconst now = Date.now();\n\t\t\tconst timeSinceLastCall = now - lastCallTime;\n\t\t\tconst delayForNextCall = wait - timeSinceLastCall;\n\n\t\t\tif (delayForNextCall <= 0) {\n\t\t\t\tlastCallTime = now;\n\t\t\t\tfn(...args);\n\t\t\t} else {\n\t\t\t\ttimeoutId = setTimeout(() => {\n\t\t\t\t\tlastCallTime = Date.now();\n\t\t\t\t\tfn(...args);\n\t\t\t\t}, delayForNextCall);\n\t\t\t}\n\t\t};\n\t};\n\nconst simulatorManager = new SimulatorManager();\n\nexport type SliceSimulatorSliceZoneProps = {\n\tslices: SliceSimulatorState[\"slices\"];\n};\n\nexport type SliceSimulatorProps = Omit<BaseSliceSimulatorProps, \"state\"> & {\n\t/**\n\t * React component to render simulated Slices.\n\t *\n\t * @example\n\t *\n\t * ```tsx\n\t * import { SliceSimulator } from \"@slicemachine/adapter-next/simulator\";\n\t * import { SliceZone } from \"@prismicio/react\";\n\t *\n\t * import { components } from \"../slices\";\n\t *\n\t * <SliceSimulator\n\t * \tsliceZone={({ slices }) => (\n\t * \t\t<SliceZone slices={slices} components={components} />\n\t * \t)}\n\t * />;\n\t * ```\n\t */\n\tsliceZone?: (props: SliceSimulatorSliceZoneProps) => JSX.Element;\n\n\tchildren: React.ReactNode;\n\tclassName?: string;\n\n\t/** @internal */\n\t__revalidatePath?: (path: string) => Promise<void>;\n};\n\n/**\n * Simulate slices in isolation. The slice simulator enables live slice\n * development in Slice Machine and live previews in the Page Builder.\n */\nexport const SliceSimulator = ({\n\tbackground,\n\tzIndex,\n\tclassName,\n\tsliceZone: SliceZoneComp,\n\tchildren,\n\t__revalidatePath,\n}: SliceSimulatorProps): JSX.Element => {\n\tconst isServerComponent = Boolean(__revalidatePath);\n\n\tif (isServerComponent && SliceZoneComp) {\n\t\tthrow new Error(\n\t\t\t\"The sliceZone prop should not be used when <SliceSimulator> is rendered in a Server Component. Use the getSlices helper or convert your simulator to a Client Component.\",\n\t\t);\n\t}\n\n\tif (!isServerComponent && !SliceZoneComp) {\n\t\tthrow new Error(\n\t\t\t\"A sliceZone prop must be provided when <SliceZone> is rendered in a Client Component. Add a sliceZone prop or convert your simulator to a Server Component with the getSlices helper.\",\n\t\t);\n\t}\n\n\tconst [slices, setSlices] = React.useState(() => getDefaultSlices());\n\tconst [message, setMessage] = React.useState(() => getDefaultMessage());\n\tconst throttledRevalidatePath = React.useRef(\n\t\t__revalidatePath\n\t\t\t? throttle(__revalidatePath, SERVER_REVALIDATE_THROTTLE_DELAY)\n\t\t\t: undefined,\n\t);\n\n\tconst defaultProps = getDefaultProps();\n\tconst hasSlices = slices.length > 0;\n\n\tReact.useEffect(() => {\n\t\tsimulatorManager.state.on(\n\t\t\tStateEventType.Slices,\n\t\t\t(newSlices) => {\n\t\t\t\tif (isServerComponent) {\n\t\t\t\t\tconst url = new URL(window.location.href);\n\t\t\t\t\turl.searchParams.set(\n\t\t\t\t\t\tSTATE_PARAMS_KEY,\n\t\t\t\t\t\tcompressToEncodedURIComponent(JSON.stringify(newSlices)),\n\t\t\t\t\t);\n\n\t\t\t\t\twindow.history.pushState(null, \"\", url);\n\n\t\t\t\t\t// A 0 ms timeout is needed to prevent a bug\n\t\t\t\t\t// where the path is revalidated before the URL\n\t\t\t\t\t// is updated with the new state.\n\t\t\t\t\tconst path = window.location.pathname;\n\t\t\t\t\tsetTimeout(() => throttledRevalidatePath.current?.(path), 0);\n\t\t\t\t}\n\n\t\t\t\tsetSlices(newSlices);\n\t\t\t},\n\t\t\t\"simulator-slices\",\n\t\t);\n\t\tsimulatorManager.state.on(\n\t\t\tStateEventType.Message,\n\t\t\t(newMessage) => setMessage(newMessage),\n\t\t\t\"simulator-message\",\n\t\t);\n\n\t\tsimulatorManager.init();\n\n\t\treturn () => {\n\t\t\tsimulatorManager.state.off(StateEventType.Slices, \"simulator-slices\");\n\t\t\tsimulatorManager.state.off(StateEventType.Message, \"simulator-message\");\n\t\t};\n\t}, []);\n\n\treturn (\n\t\t<div\n\t\t\tclassName={[simulatorClass, className].filter(Boolean).join(\" \")}\n\t\t\tstyle={{\n\t\t\t\tzIndex:\n\t\t\t\t\ttypeof zIndex === \"undefined\"\n\t\t\t\t\t\t? defaultProps.zIndex\n\t\t\t\t\t\t: zIndex ?? undefined,\n\t\t\t\tposition: \"fixed\",\n\t\t\t\ttop: 0,\n\t\t\t\tleft: 0,\n\t\t\t\twidth: \"100%\",\n\t\t\t\theight: \"100vh\",\n\t\t\t\toverflow: \"auto\",\n\t\t\t\tbackground:\n\t\t\t\t\ttypeof background === \"undefined\"\n\t\t\t\t\t\t? defaultProps.background\n\t\t\t\t\t\t: background ?? undefined,\n\t\t\t}}\n\t\t>\n\t\t\t{message ? (\n\t\t\t\t<article dangerouslySetInnerHTML={{ __html: message }} />\n\t\t\t) : hasSlices ? (\n\t\t\t\t<div\n\t\t\t\t\tid=\"root\"\n\t\t\t\t\tclassName={simulatorRootClass}\n\t\t\t\t\tonClickCapture={onClickHandler as unknown as React.MouseEventHandler}\n\t\t\t\t\tonSubmitCapture={\n\t\t\t\t\t\tdisableEventHandler as unknown as React.FormEventHandler\n\t\t\t\t\t}\n\t\t\t\t>\n\t\t\t\t\t{SliceZoneComp ? <SliceZoneComp slices={slices} /> : children}\n\t\t\t\t</div>\n\t\t\t) : null}\n\t\t</div>\n\t);\n};\n"],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAmBA;AACA;AAEA;AAAM;AAAA;AAGA;AACJ;AAEA;AACC;AAEM;AACN;AACA;AAEA;AACgB;AACf;AAAU;AAEV;AACC;AACA;AAAU;AACQ;AACnB;AAAA;AAEH;AAED;AAsCa;AAQN;AAEN;AACO;AACqK;AAIxK;AACG;AACkL;AAInL;AACA;AACA;AAMN;AACM;AAENA;AACC;AAGE;AACC;AACI;AAKJ;AAKM;AACN;;AAAiB;AAAkC;AAAQ;AAG5D;AAAmB;AAIJ;AAMjB;AAEA;AACC;AACA;AAAsE;AAAA;AAIxE;AAGS;AAIQ;AACJ;AACL;AACC;AACC;AACC;AACE;AAIQ;AAmBtB;;"}
|
|
@@ -3,9 +3,7 @@ import { SliceSimulatorProps as BaseSliceSimulatorProps, SliceSimulatorState } f
|
|
|
3
3
|
export type SliceSimulatorSliceZoneProps = {
|
|
4
4
|
slices: SliceSimulatorState["slices"];
|
|
5
5
|
};
|
|
6
|
-
export type SliceSimulatorProps = {
|
|
7
|
-
className?: string;
|
|
8
|
-
} & Omit<BaseSliceSimulatorProps, "state"> & ({
|
|
6
|
+
export type SliceSimulatorProps = Omit<BaseSliceSimulatorProps, "state"> & {
|
|
9
7
|
/**
|
|
10
8
|
* React component to render simulated Slices.
|
|
11
9
|
*
|
|
@@ -24,12 +22,14 @@ export type SliceSimulatorProps = {
|
|
|
24
22
|
* />;
|
|
25
23
|
* ```
|
|
26
24
|
*/
|
|
27
|
-
sliceZone
|
|
28
|
-
} | {
|
|
25
|
+
sliceZone?: (props: SliceSimulatorSliceZoneProps) => JSX.Element;
|
|
29
26
|
children: React.ReactNode;
|
|
30
|
-
|
|
27
|
+
className?: string;
|
|
28
|
+
/** @internal */
|
|
29
|
+
__revalidatePath?: (path: string) => Promise<void>;
|
|
30
|
+
};
|
|
31
31
|
/**
|
|
32
32
|
* Simulate slices in isolation. The slice simulator enables live slice
|
|
33
33
|
* development in Slice Machine and live previews in the Page Builder.
|
|
34
34
|
*/
|
|
35
|
-
export declare const SliceSimulator: ({ background, zIndex, className,
|
|
35
|
+
export declare const SliceSimulator: ({ background, zIndex, className, sliceZone: SliceZoneComp, children, __revalidatePath, }: SliceSimulatorProps) => JSX.Element;
|
|
@@ -2,30 +2,77 @@
|
|
|
2
2
|
'use client';
|
|
3
3
|
import { jsx } from "react/jsx-runtime";
|
|
4
4
|
import * as React from "react";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { compressToEncodedURIComponent } from "lz-string";
|
|
6
|
+
import { SimulatorManager, getDefaultSlices, getDefaultMessage, getDefaultProps, StateEventType, simulatorClass, simulatorRootClass, onClickHandler, disableEventHandler } from "@prismicio/simulator/kit";
|
|
7
|
+
const STATE_PARAMS_KEY = "state";
|
|
8
|
+
const SERVER_REVALIDATE_THROTTLE_DELAY = 300;
|
|
9
|
+
const throttle = (
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
(fn, wait) => {
|
|
12
|
+
let timeoutId;
|
|
13
|
+
let lastCallTime = 0;
|
|
14
|
+
return async (...args) => {
|
|
15
|
+
clearTimeout(timeoutId);
|
|
16
|
+
const now = Date.now();
|
|
17
|
+
const timeSinceLastCall = now - lastCallTime;
|
|
18
|
+
const delayForNextCall = wait - timeSinceLastCall;
|
|
19
|
+
if (delayForNextCall <= 0) {
|
|
20
|
+
lastCallTime = now;
|
|
21
|
+
fn(...args);
|
|
22
|
+
} else {
|
|
23
|
+
timeoutId = setTimeout(() => {
|
|
24
|
+
lastCallTime = Date.now();
|
|
25
|
+
fn(...args);
|
|
26
|
+
}, delayForNextCall);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
);
|
|
7
31
|
const simulatorManager = new SimulatorManager();
|
|
8
|
-
const SliceSimulator = ({ background, zIndex, className,
|
|
9
|
-
|
|
32
|
+
const SliceSimulator = ({ background, zIndex, className, sliceZone: SliceZoneComp, children, __revalidatePath }) => {
|
|
33
|
+
const isServerComponent = Boolean(__revalidatePath);
|
|
34
|
+
if (isServerComponent && SliceZoneComp) {
|
|
35
|
+
throw new Error("The sliceZone prop should not be used when <SliceSimulator> is rendered in a Server Component. Use the getSlices helper or convert your simulator to a Client Component.");
|
|
36
|
+
}
|
|
37
|
+
if (!isServerComponent && !SliceZoneComp) {
|
|
10
38
|
throw new Error("A sliceZone prop must be provided when <SliceZone> is rendered in a Client Component. Add a sliceZone prop or convert your simulator to a Server Component with the getSlices helper.");
|
|
11
39
|
}
|
|
12
40
|
const [slices, setSlices] = React.useState(() => getDefaultSlices());
|
|
13
41
|
const [message, setMessage] = React.useState(() => getDefaultMessage());
|
|
42
|
+
const throttledRevalidatePath = React.useRef(__revalidatePath ? throttle(__revalidatePath, SERVER_REVALIDATE_THROTTLE_DELAY) : void 0);
|
|
43
|
+
const defaultProps = getDefaultProps();
|
|
44
|
+
const hasSlices = slices.length > 0;
|
|
14
45
|
React.useEffect(() => {
|
|
15
|
-
simulatorManager.state.on(StateEventType.Slices, (
|
|
16
|
-
|
|
46
|
+
simulatorManager.state.on(StateEventType.Slices, (newSlices) => {
|
|
47
|
+
if (isServerComponent) {
|
|
48
|
+
const url = new URL(window.location.href);
|
|
49
|
+
url.searchParams.set(STATE_PARAMS_KEY, compressToEncodedURIComponent(JSON.stringify(newSlices)));
|
|
50
|
+
window.history.pushState(null, "", url);
|
|
51
|
+
const path = window.location.pathname;
|
|
52
|
+
setTimeout(() => {
|
|
53
|
+
var _a;
|
|
54
|
+
return (_a = throttledRevalidatePath.current) == null ? void 0 : _a.call(throttledRevalidatePath, path);
|
|
55
|
+
}, 0);
|
|
56
|
+
}
|
|
57
|
+
setSlices(newSlices);
|
|
17
58
|
}, "simulator-slices");
|
|
18
|
-
simulatorManager.state.on(StateEventType.Message, (
|
|
19
|
-
setMessage(_message);
|
|
20
|
-
}, "simulator-message");
|
|
59
|
+
simulatorManager.state.on(StateEventType.Message, (newMessage) => setMessage(newMessage), "simulator-message");
|
|
21
60
|
simulatorManager.init();
|
|
22
61
|
return () => {
|
|
23
62
|
simulatorManager.state.off(StateEventType.Slices, "simulator-slices");
|
|
24
63
|
simulatorManager.state.off(StateEventType.Message, "simulator-message");
|
|
25
64
|
};
|
|
26
65
|
}, []);
|
|
27
|
-
|
|
28
|
-
|
|
66
|
+
return jsx("div", { className: [simulatorClass, className].filter(Boolean).join(" "), style: {
|
|
67
|
+
zIndex: typeof zIndex === "undefined" ? defaultProps.zIndex : zIndex ?? void 0,
|
|
68
|
+
position: "fixed",
|
|
69
|
+
top: 0,
|
|
70
|
+
left: 0,
|
|
71
|
+
width: "100%",
|
|
72
|
+
height: "100vh",
|
|
73
|
+
overflow: "auto",
|
|
74
|
+
background: typeof background === "undefined" ? defaultProps.background : background ?? void 0
|
|
75
|
+
}, children: message ? jsx("article", { dangerouslySetInnerHTML: { __html: message } }) : hasSlices ? jsx("div", { id: "root", className: simulatorRootClass, onClickCapture: onClickHandler, onSubmitCapture: disableEventHandler, children: SliceZoneComp ? jsx(SliceZoneComp, { slices }) : children }) : null });
|
|
29
76
|
};
|
|
30
77
|
export {
|
|
31
78
|
SliceSimulator
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SliceSimulator.js","sources":["../../../src/simulator/SliceSimulator.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\n\nimport {\n\tSliceSimulatorProps as BaseSliceSimulatorProps,\n\
|
|
1
|
+
{"version":3,"file":"SliceSimulator.js","sources":["../../../src/simulator/SliceSimulator.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\n\nimport { compressToEncodedURIComponent } from \"lz-string\";\nimport {\n\tSimulatorManager,\n\tStateEventType,\n\tgetDefaultMessage,\n\tSliceSimulatorProps as BaseSliceSimulatorProps,\n\tSliceSimulatorState,\n\tgetDefaultSlices,\n\tdisableEventHandler,\n\tgetDefaultProps,\n\tonClickHandler,\n\tsimulatorClass,\n\tsimulatorRootClass,\n} from \"@prismicio/simulator/kit\";\n\nconst STATE_PARAMS_KEY = \"state\";\nconst SERVER_REVALIDATE_THROTTLE_DELAY = 300;\n\nconst throttle =\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t<TArgs extends [...any]>(fn: (...args: TArgs) => unknown, wait: number) => {\n\t\tlet timeoutId: ReturnType<typeof setTimeout>;\n\t\tlet lastCallTime = 0;\n\n\t\treturn async (...args: TArgs) => {\n\t\t\tclearTimeout(timeoutId);\n\n\t\t\tconst now = Date.now();\n\t\t\tconst timeSinceLastCall = now - lastCallTime;\n\t\t\tconst delayForNextCall = wait - timeSinceLastCall;\n\n\t\t\tif (delayForNextCall <= 0) {\n\t\t\t\tlastCallTime = now;\n\t\t\t\tfn(...args);\n\t\t\t} else {\n\t\t\t\ttimeoutId = setTimeout(() => {\n\t\t\t\t\tlastCallTime = Date.now();\n\t\t\t\t\tfn(...args);\n\t\t\t\t}, delayForNextCall);\n\t\t\t}\n\t\t};\n\t};\n\nconst simulatorManager = new SimulatorManager();\n\nexport type SliceSimulatorSliceZoneProps = {\n\tslices: SliceSimulatorState[\"slices\"];\n};\n\nexport type SliceSimulatorProps = Omit<BaseSliceSimulatorProps, \"state\"> & {\n\t/**\n\t * React component to render simulated Slices.\n\t *\n\t * @example\n\t *\n\t * ```tsx\n\t * import { SliceSimulator } from \"@slicemachine/adapter-next/simulator\";\n\t * import { SliceZone } from \"@prismicio/react\";\n\t *\n\t * import { components } from \"../slices\";\n\t *\n\t * <SliceSimulator\n\t * \tsliceZone={({ slices }) => (\n\t * \t\t<SliceZone slices={slices} components={components} />\n\t * \t)}\n\t * />;\n\t * ```\n\t */\n\tsliceZone?: (props: SliceSimulatorSliceZoneProps) => JSX.Element;\n\n\tchildren: React.ReactNode;\n\tclassName?: string;\n\n\t/** @internal */\n\t__revalidatePath?: (path: string) => Promise<void>;\n};\n\n/**\n * Simulate slices in isolation. The slice simulator enables live slice\n * development in Slice Machine and live previews in the Page Builder.\n */\nexport const SliceSimulator = ({\n\tbackground,\n\tzIndex,\n\tclassName,\n\tsliceZone: SliceZoneComp,\n\tchildren,\n\t__revalidatePath,\n}: SliceSimulatorProps): JSX.Element => {\n\tconst isServerComponent = Boolean(__revalidatePath);\n\n\tif (isServerComponent && SliceZoneComp) {\n\t\tthrow new Error(\n\t\t\t\"The sliceZone prop should not be used when <SliceSimulator> is rendered in a Server Component. Use the getSlices helper or convert your simulator to a Client Component.\",\n\t\t);\n\t}\n\n\tif (!isServerComponent && !SliceZoneComp) {\n\t\tthrow new Error(\n\t\t\t\"A sliceZone prop must be provided when <SliceZone> is rendered in a Client Component. Add a sliceZone prop or convert your simulator to a Server Component with the getSlices helper.\",\n\t\t);\n\t}\n\n\tconst [slices, setSlices] = React.useState(() => getDefaultSlices());\n\tconst [message, setMessage] = React.useState(() => getDefaultMessage());\n\tconst throttledRevalidatePath = React.useRef(\n\t\t__revalidatePath\n\t\t\t? throttle(__revalidatePath, SERVER_REVALIDATE_THROTTLE_DELAY)\n\t\t\t: undefined,\n\t);\n\n\tconst defaultProps = getDefaultProps();\n\tconst hasSlices = slices.length > 0;\n\n\tReact.useEffect(() => {\n\t\tsimulatorManager.state.on(\n\t\t\tStateEventType.Slices,\n\t\t\t(newSlices) => {\n\t\t\t\tif (isServerComponent) {\n\t\t\t\t\tconst url = new URL(window.location.href);\n\t\t\t\t\turl.searchParams.set(\n\t\t\t\t\t\tSTATE_PARAMS_KEY,\n\t\t\t\t\t\tcompressToEncodedURIComponent(JSON.stringify(newSlices)),\n\t\t\t\t\t);\n\n\t\t\t\t\twindow.history.pushState(null, \"\", url);\n\n\t\t\t\t\t// A 0 ms timeout is needed to prevent a bug\n\t\t\t\t\t// where the path is revalidated before the URL\n\t\t\t\t\t// is updated with the new state.\n\t\t\t\t\tconst path = window.location.pathname;\n\t\t\t\t\tsetTimeout(() => throttledRevalidatePath.current?.(path), 0);\n\t\t\t\t}\n\n\t\t\t\tsetSlices(newSlices);\n\t\t\t},\n\t\t\t\"simulator-slices\",\n\t\t);\n\t\tsimulatorManager.state.on(\n\t\t\tStateEventType.Message,\n\t\t\t(newMessage) => setMessage(newMessage),\n\t\t\t\"simulator-message\",\n\t\t);\n\n\t\tsimulatorManager.init();\n\n\t\treturn () => {\n\t\t\tsimulatorManager.state.off(StateEventType.Slices, \"simulator-slices\");\n\t\t\tsimulatorManager.state.off(StateEventType.Message, \"simulator-message\");\n\t\t};\n\t}, []);\n\n\treturn (\n\t\t<div\n\t\t\tclassName={[simulatorClass, className].filter(Boolean).join(\" \")}\n\t\t\tstyle={{\n\t\t\t\tzIndex:\n\t\t\t\t\ttypeof zIndex === \"undefined\"\n\t\t\t\t\t\t? defaultProps.zIndex\n\t\t\t\t\t\t: zIndex ?? undefined,\n\t\t\t\tposition: \"fixed\",\n\t\t\t\ttop: 0,\n\t\t\t\tleft: 0,\n\t\t\t\twidth: \"100%\",\n\t\t\t\theight: \"100vh\",\n\t\t\t\toverflow: \"auto\",\n\t\t\t\tbackground:\n\t\t\t\t\ttypeof background === \"undefined\"\n\t\t\t\t\t\t? defaultProps.background\n\t\t\t\t\t\t: background ?? undefined,\n\t\t\t}}\n\t\t>\n\t\t\t{message ? (\n\t\t\t\t<article dangerouslySetInnerHTML={{ __html: message }} />\n\t\t\t) : hasSlices ? (\n\t\t\t\t<div\n\t\t\t\t\tid=\"root\"\n\t\t\t\t\tclassName={simulatorRootClass}\n\t\t\t\t\tonClickCapture={onClickHandler as unknown as React.MouseEventHandler}\n\t\t\t\t\tonSubmitCapture={\n\t\t\t\t\t\tdisableEventHandler as unknown as React.FormEventHandler\n\t\t\t\t\t}\n\t\t\t\t>\n\t\t\t\t\t{SliceZoneComp ? <SliceZoneComp slices={slices} /> : children}\n\t\t\t\t</div>\n\t\t\t) : null}\n\t\t</div>\n\t);\n};\n"],"names":[],"mappings":";;;;;;AAmBA;AACA;AAEA;AAAM;AAAA;AAGA;AACJ;AAEA;AACC;AAEM;AACN;AACA;AAEA;AACgB;AACf;AAAU;AAEV;AACC;AACA;AAAU;AACQ;AACnB;AAAA;AAEH;AAED;AAsCa;AAQN;AAEN;AACO;AACqK;AAIxK;AACG;AACkL;AAInL;AACA;AACA;AAMN;AACM;AAEN;AACC;AAGE;AACC;AACI;AAKJ;AAKM;AACN;;AAAiB;AAAkC;AAAQ;AAG5D;AAAmB;AAIJ;AAMjB;AAEA;AACC;AACA;AAAsE;AAAA;AAIxE;AAGS;AAIQ;AACJ;AACL;AACC;AACC;AACC;AACE;AAIQ;AAmBtB;;;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { SliceSimulator } from "./SliceSimulator";
|
|
2
2
|
export type { SliceSimulatorProps, SliceSimulatorSliceZoneProps, } from "./SliceSimulator";
|
|
3
|
-
export { getSlices } from "./getSlices";
|
|
3
|
+
export { getSlices } from "./react-server/getSlices";
|
|
4
4
|
export type { SliceSimulatorParams } from "./types";
|
|
@@ -3,74 +3,10 @@
|
|
|
3
3
|
"use strict";
|
|
4
4
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
5
5
|
const jsxRuntime = require("react/jsx-runtime");
|
|
6
|
-
const
|
|
7
|
-
const kit = require("@prismicio/simulator/kit");
|
|
8
|
-
const lzString = require("lz-string");
|
|
9
|
-
const SliceSimulatorWrapper = require("../SliceSimulatorWrapper.cjs");
|
|
6
|
+
const SliceSimulator$1 = require("../SliceSimulator.cjs");
|
|
10
7
|
const actions = require("./actions.cjs");
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
14
|
-
if (e) {
|
|
15
|
-
for (const k in e) {
|
|
16
|
-
if (k !== "default") {
|
|
17
|
-
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
18
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
19
|
-
enumerable: true,
|
|
20
|
-
get: () => e[k]
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
n.default = e;
|
|
26
|
-
return Object.freeze(n);
|
|
27
|
-
}
|
|
28
|
-
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
|
|
29
|
-
const STATE_PARAMS_KEY = "state";
|
|
30
|
-
const throttle = (
|
|
31
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
-
(fn, wait) => {
|
|
33
|
-
let timeoutId;
|
|
34
|
-
let lastCallTime = 0;
|
|
35
|
-
return (...args) => {
|
|
36
|
-
clearTimeout(timeoutId);
|
|
37
|
-
const now = Date.now();
|
|
38
|
-
const timeSinceLastCall = now - lastCallTime;
|
|
39
|
-
const delayForNextCall = wait - timeSinceLastCall;
|
|
40
|
-
if (delayForNextCall <= 0) {
|
|
41
|
-
lastCallTime = now;
|
|
42
|
-
fn(...args);
|
|
43
|
-
} else {
|
|
44
|
-
timeoutId = setTimeout(() => {
|
|
45
|
-
lastCallTime = Date.now();
|
|
46
|
-
fn(...args);
|
|
47
|
-
}, delayForNextCall);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
);
|
|
52
|
-
const throttledRevalidatePath = throttle(actions.revalidatePath, 300);
|
|
53
|
-
const simulatorManager = new kit.SimulatorManager();
|
|
54
|
-
const SliceSimulator = ({ children, background, zIndex, className }) => {
|
|
55
|
-
const [message, setMessage] = React__namespace.useState(() => kit.getDefaultMessage());
|
|
56
|
-
const state = typeof window !== "undefined" ? new URL(window.location.href).searchParams.get(STATE_PARAMS_KEY) : void 0;
|
|
57
|
-
const hasSlices = getSlices.getSlices(state).length > 0;
|
|
58
|
-
React__namespace.useEffect(() => {
|
|
59
|
-
simulatorManager.state.on(kit.StateEventType.Slices, (newSlices) => {
|
|
60
|
-
const url = new URL(window.location.href);
|
|
61
|
-
url.searchParams.set(STATE_PARAMS_KEY, lzString.compressToEncodedURIComponent(JSON.stringify(newSlices)));
|
|
62
|
-
window.history.pushState(null, "", url);
|
|
63
|
-
const path = window.location.pathname;
|
|
64
|
-
setTimeout(() => throttledRevalidatePath(path), 0);
|
|
65
|
-
}, "simulator-slices");
|
|
66
|
-
simulatorManager.state.on(kit.StateEventType.Message, (newMessage) => setMessage(newMessage), "simulator-message");
|
|
67
|
-
simulatorManager.init();
|
|
68
|
-
return () => {
|
|
69
|
-
simulatorManager.state.off(kit.StateEventType.Slices, "simulator-slices");
|
|
70
|
-
simulatorManager.state.off(kit.StateEventType.Message, "simulator-message");
|
|
71
|
-
};
|
|
72
|
-
}, []);
|
|
73
|
-
return jsxRuntime.jsx(SliceSimulatorWrapper.SliceSimulatorWrapper, { message, hasSlices, background, zIndex, className, children });
|
|
8
|
+
const SliceSimulator = (props) => {
|
|
9
|
+
return jsxRuntime.jsx(SliceSimulator$1.SliceSimulator, { ...props, __revalidatePath: actions.revalidatePath });
|
|
74
10
|
};
|
|
75
11
|
exports.SliceSimulator = SliceSimulator;
|
|
76
12
|
//# sourceMappingURL=SliceSimulator.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SliceSimulator.cjs","sources":["../../../../src/simulator/react-server/SliceSimulator.tsx"],"sourcesContent":["// This `<SliceSimulator>` is only accessible from Server Components.\n\n\"use client\";\n\nimport
|
|
1
|
+
{"version":3,"file":"SliceSimulator.cjs","sources":["../../../../src/simulator/react-server/SliceSimulator.tsx"],"sourcesContent":["// This `<SliceSimulator>` is only accessible from Server Components.\n\n\"use client\";\n\nimport {\n\tSliceSimulator as BaseSliceSimulator,\n\tSliceSimulatorProps,\n} from \"../SliceSimulator\";\nimport { revalidatePath } from \"./actions\";\n\nexport const SliceSimulator = (props: SliceSimulatorProps): JSX.Element => {\n\treturn <BaseSliceSimulator {...props} __revalidatePath={revalidatePath} />;\n};\n"],"names":[],"mappings":";;;;;;;AAUa;AACZ;AACD;;"}
|
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
export type SliceSimulatorProps = Omit<BaseSliceSimulatorProps, "state"> & {
|
|
4
|
-
children: React.ReactNode;
|
|
5
|
-
className?: string;
|
|
6
|
-
};
|
|
7
|
-
export declare const SliceSimulator: ({ children, background, zIndex, className, }: SliceSimulatorProps) => JSX.Element;
|
|
1
|
+
import { SliceSimulatorProps } from "../SliceSimulator";
|
|
2
|
+
export declare const SliceSimulator: (props: SliceSimulatorProps) => JSX.Element;
|
|
@@ -1,57 +1,10 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
'use client';
|
|
3
3
|
import { jsx } from "react/jsx-runtime";
|
|
4
|
-
import
|
|
5
|
-
import { SimulatorManager, getDefaultMessage, StateEventType } from "@prismicio/simulator/kit";
|
|
6
|
-
import { compressToEncodedURIComponent } from "lz-string";
|
|
7
|
-
import { SliceSimulatorWrapper } from "../SliceSimulatorWrapper.js";
|
|
4
|
+
import { SliceSimulator as SliceSimulator$1 } from "../SliceSimulator.js";
|
|
8
5
|
import { revalidatePath } from "./actions.js";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const throttle = (
|
|
12
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
|
-
(fn, wait) => {
|
|
14
|
-
let timeoutId;
|
|
15
|
-
let lastCallTime = 0;
|
|
16
|
-
return (...args) => {
|
|
17
|
-
clearTimeout(timeoutId);
|
|
18
|
-
const now = Date.now();
|
|
19
|
-
const timeSinceLastCall = now - lastCallTime;
|
|
20
|
-
const delayForNextCall = wait - timeSinceLastCall;
|
|
21
|
-
if (delayForNextCall <= 0) {
|
|
22
|
-
lastCallTime = now;
|
|
23
|
-
fn(...args);
|
|
24
|
-
} else {
|
|
25
|
-
timeoutId = setTimeout(() => {
|
|
26
|
-
lastCallTime = Date.now();
|
|
27
|
-
fn(...args);
|
|
28
|
-
}, delayForNextCall);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
);
|
|
33
|
-
const throttledRevalidatePath = throttle(revalidatePath, 300);
|
|
34
|
-
const simulatorManager = new SimulatorManager();
|
|
35
|
-
const SliceSimulator = ({ children, background, zIndex, className }) => {
|
|
36
|
-
const [message, setMessage] = React.useState(() => getDefaultMessage());
|
|
37
|
-
const state = typeof window !== "undefined" ? new URL(window.location.href).searchParams.get(STATE_PARAMS_KEY) : void 0;
|
|
38
|
-
const hasSlices = getSlices(state).length > 0;
|
|
39
|
-
React.useEffect(() => {
|
|
40
|
-
simulatorManager.state.on(StateEventType.Slices, (newSlices) => {
|
|
41
|
-
const url = new URL(window.location.href);
|
|
42
|
-
url.searchParams.set(STATE_PARAMS_KEY, compressToEncodedURIComponent(JSON.stringify(newSlices)));
|
|
43
|
-
window.history.pushState(null, "", url);
|
|
44
|
-
const path = window.location.pathname;
|
|
45
|
-
setTimeout(() => throttledRevalidatePath(path), 0);
|
|
46
|
-
}, "simulator-slices");
|
|
47
|
-
simulatorManager.state.on(StateEventType.Message, (newMessage) => setMessage(newMessage), "simulator-message");
|
|
48
|
-
simulatorManager.init();
|
|
49
|
-
return () => {
|
|
50
|
-
simulatorManager.state.off(StateEventType.Slices, "simulator-slices");
|
|
51
|
-
simulatorManager.state.off(StateEventType.Message, "simulator-message");
|
|
52
|
-
};
|
|
53
|
-
}, []);
|
|
54
|
-
return jsx(SliceSimulatorWrapper, { message, hasSlices, background, zIndex, className, children });
|
|
6
|
+
const SliceSimulator = (props) => {
|
|
7
|
+
return jsx(SliceSimulator$1, { ...props, __revalidatePath: revalidatePath });
|
|
55
8
|
};
|
|
56
9
|
export {
|
|
57
10
|
SliceSimulator
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SliceSimulator.js","sources":["../../../../src/simulator/react-server/SliceSimulator.tsx"],"sourcesContent":["// This `<SliceSimulator>` is only accessible from Server Components.\n\n\"use client\";\n\nimport
|
|
1
|
+
{"version":3,"file":"SliceSimulator.js","sources":["../../../../src/simulator/react-server/SliceSimulator.tsx"],"sourcesContent":["// This `<SliceSimulator>` is only accessible from Server Components.\n\n\"use client\";\n\nimport {\n\tSliceSimulator as BaseSliceSimulator,\n\tSliceSimulatorProps,\n} from \"../SliceSimulator\";\nimport { revalidatePath } from \"./actions\";\n\nexport const SliceSimulator = (props: SliceSimulatorProps): JSX.Element => {\n\treturn <BaseSliceSimulator {...props} __revalidatePath={revalidatePath} />;\n};\n"],"names":[],"mappings":";;;;;AAUa;AACZ;AACD;;;;"}
|
package/dist/simulator.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const SliceSimulator = require("./simulator/SliceSimulator.cjs");
|
|
4
|
-
const getSlices = require("./simulator/getSlices.cjs");
|
|
4
|
+
const getSlices = require("./simulator/react-server/getSlices.cjs");
|
|
5
5
|
exports.SliceSimulator = SliceSimulator.SliceSimulator;
|
|
6
6
|
exports.getSlices = getSlices.getSlices;
|
|
7
7
|
//# sourceMappingURL=simulator.cjs.map
|
package/dist/simulator.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slicemachine/adapter-next",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.39-alpha.aa-fix-nextjs-rsc-simulator.1",
|
|
4
4
|
"description": "Slice Machine adapter for Next.js.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"dependencies": {
|
|
69
69
|
"@prismicio/simulator": "^0.1.4",
|
|
70
70
|
"@prismicio/types-internal": "^2.2.0",
|
|
71
|
-
"@slicemachine/plugin-kit": "0.4.
|
|
71
|
+
"@slicemachine/plugin-kit": "0.4.39-alpha.aa-fix-nextjs-rsc-simulator.1",
|
|
72
72
|
"common-tags": "^1.8.2",
|
|
73
73
|
"fp-ts": "^2.13.1",
|
|
74
74
|
"io-ts": "^2.2.20",
|
|
@@ -113,5 +113,6 @@
|
|
|
113
113
|
},
|
|
114
114
|
"publishConfig": {
|
|
115
115
|
"access": "public"
|
|
116
|
-
}
|
|
116
|
+
},
|
|
117
|
+
"stableVersion": "0.3.38"
|
|
117
118
|
}
|
|
@@ -2,16 +2,48 @@
|
|
|
2
2
|
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
|
|
5
|
+
import { compressToEncodedURIComponent } from "lz-string";
|
|
5
6
|
import {
|
|
6
|
-
SliceSimulatorProps as BaseSliceSimulatorProps,
|
|
7
7
|
SimulatorManager,
|
|
8
|
-
SliceSimulatorState,
|
|
9
8
|
StateEventType,
|
|
10
9
|
getDefaultMessage,
|
|
10
|
+
SliceSimulatorProps as BaseSliceSimulatorProps,
|
|
11
|
+
SliceSimulatorState,
|
|
11
12
|
getDefaultSlices,
|
|
13
|
+
disableEventHandler,
|
|
14
|
+
getDefaultProps,
|
|
15
|
+
onClickHandler,
|
|
16
|
+
simulatorClass,
|
|
17
|
+
simulatorRootClass,
|
|
12
18
|
} from "@prismicio/simulator/kit";
|
|
13
19
|
|
|
14
|
-
|
|
20
|
+
const STATE_PARAMS_KEY = "state";
|
|
21
|
+
const SERVER_REVALIDATE_THROTTLE_DELAY = 300;
|
|
22
|
+
|
|
23
|
+
const throttle =
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
+
<TArgs extends [...any]>(fn: (...args: TArgs) => unknown, wait: number) => {
|
|
26
|
+
let timeoutId: ReturnType<typeof setTimeout>;
|
|
27
|
+
let lastCallTime = 0;
|
|
28
|
+
|
|
29
|
+
return async (...args: TArgs) => {
|
|
30
|
+
clearTimeout(timeoutId);
|
|
31
|
+
|
|
32
|
+
const now = Date.now();
|
|
33
|
+
const timeSinceLastCall = now - lastCallTime;
|
|
34
|
+
const delayForNextCall = wait - timeSinceLastCall;
|
|
35
|
+
|
|
36
|
+
if (delayForNextCall <= 0) {
|
|
37
|
+
lastCallTime = now;
|
|
38
|
+
fn(...args);
|
|
39
|
+
} else {
|
|
40
|
+
timeoutId = setTimeout(() => {
|
|
41
|
+
lastCallTime = Date.now();
|
|
42
|
+
fn(...args);
|
|
43
|
+
}, delayForNextCall);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
};
|
|
15
47
|
|
|
16
48
|
const simulatorManager = new SimulatorManager();
|
|
17
49
|
|
|
@@ -19,35 +51,33 @@ export type SliceSimulatorSliceZoneProps = {
|
|
|
19
51
|
slices: SliceSimulatorState["slices"];
|
|
20
52
|
};
|
|
21
53
|
|
|
22
|
-
export type SliceSimulatorProps = {
|
|
54
|
+
export type SliceSimulatorProps = Omit<BaseSliceSimulatorProps, "state"> & {
|
|
55
|
+
/**
|
|
56
|
+
* React component to render simulated Slices.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
*
|
|
60
|
+
* ```tsx
|
|
61
|
+
* import { SliceSimulator } from "@slicemachine/adapter-next/simulator";
|
|
62
|
+
* import { SliceZone } from "@prismicio/react";
|
|
63
|
+
*
|
|
64
|
+
* import { components } from "../slices";
|
|
65
|
+
*
|
|
66
|
+
* <SliceSimulator
|
|
67
|
+
* sliceZone={({ slices }) => (
|
|
68
|
+
* <SliceZone slices={slices} components={components} />
|
|
69
|
+
* )}
|
|
70
|
+
* />;
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
sliceZone?: (props: SliceSimulatorSliceZoneProps) => JSX.Element;
|
|
74
|
+
|
|
75
|
+
children: React.ReactNode;
|
|
23
76
|
className?: string;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
* React component to render simulated Slices.
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
*
|
|
32
|
-
* ```tsx
|
|
33
|
-
* import { SliceSimulator } from "@slicemachine/adapter-next/simulator";
|
|
34
|
-
* import { SliceZone } from "@prismicio/react";
|
|
35
|
-
*
|
|
36
|
-
* import { components } from "../slices";
|
|
37
|
-
*
|
|
38
|
-
* <SliceSimulator
|
|
39
|
-
* sliceZone={({ slices }) => (
|
|
40
|
-
* <SliceZone slices={slices} components={components} />
|
|
41
|
-
* )}
|
|
42
|
-
* />;
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
sliceZone: (props: SliceSimulatorSliceZoneProps) => JSX.Element;
|
|
46
|
-
}
|
|
47
|
-
| {
|
|
48
|
-
children: React.ReactNode;
|
|
49
|
-
}
|
|
50
|
-
);
|
|
77
|
+
|
|
78
|
+
/** @internal */
|
|
79
|
+
__revalidatePath?: (path: string) => Promise<void>;
|
|
80
|
+
};
|
|
51
81
|
|
|
52
82
|
/**
|
|
53
83
|
* Simulate slices in isolation. The slice simulator enables live slice
|
|
@@ -57,9 +87,19 @@ export const SliceSimulator = ({
|
|
|
57
87
|
background,
|
|
58
88
|
zIndex,
|
|
59
89
|
className,
|
|
60
|
-
|
|
90
|
+
sliceZone: SliceZoneComp,
|
|
91
|
+
children,
|
|
92
|
+
__revalidatePath,
|
|
61
93
|
}: SliceSimulatorProps): JSX.Element => {
|
|
62
|
-
|
|
94
|
+
const isServerComponent = Boolean(__revalidatePath);
|
|
95
|
+
|
|
96
|
+
if (isServerComponent && SliceZoneComp) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
"The sliceZone prop should not be used when <SliceSimulator> is rendered in a Server Component. Use the getSlices helper or convert your simulator to a Client Component.",
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!isServerComponent && !SliceZoneComp) {
|
|
63
103
|
throw new Error(
|
|
64
104
|
"A sliceZone prop must be provided when <SliceZone> is rendered in a Client Component. Add a sliceZone prop or convert your simulator to a Server Component with the getSlices helper.",
|
|
65
105
|
);
|
|
@@ -67,20 +107,42 @@ export const SliceSimulator = ({
|
|
|
67
107
|
|
|
68
108
|
const [slices, setSlices] = React.useState(() => getDefaultSlices());
|
|
69
109
|
const [message, setMessage] = React.useState(() => getDefaultMessage());
|
|
110
|
+
const throttledRevalidatePath = React.useRef(
|
|
111
|
+
__revalidatePath
|
|
112
|
+
? throttle(__revalidatePath, SERVER_REVALIDATE_THROTTLE_DELAY)
|
|
113
|
+
: undefined,
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const defaultProps = getDefaultProps();
|
|
117
|
+
const hasSlices = slices.length > 0;
|
|
70
118
|
|
|
71
119
|
React.useEffect(() => {
|
|
72
120
|
simulatorManager.state.on(
|
|
73
121
|
StateEventType.Slices,
|
|
74
|
-
(
|
|
75
|
-
|
|
122
|
+
(newSlices) => {
|
|
123
|
+
if (isServerComponent) {
|
|
124
|
+
const url = new URL(window.location.href);
|
|
125
|
+
url.searchParams.set(
|
|
126
|
+
STATE_PARAMS_KEY,
|
|
127
|
+
compressToEncodedURIComponent(JSON.stringify(newSlices)),
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
window.history.pushState(null, "", url);
|
|
131
|
+
|
|
132
|
+
// A 0 ms timeout is needed to prevent a bug
|
|
133
|
+
// where the path is revalidated before the URL
|
|
134
|
+
// is updated with the new state.
|
|
135
|
+
const path = window.location.pathname;
|
|
136
|
+
setTimeout(() => throttledRevalidatePath.current?.(path), 0);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
setSlices(newSlices);
|
|
76
140
|
},
|
|
77
141
|
"simulator-slices",
|
|
78
142
|
);
|
|
79
143
|
simulatorManager.state.on(
|
|
80
144
|
StateEventType.Message,
|
|
81
|
-
(
|
|
82
|
-
setMessage(_message);
|
|
83
|
-
},
|
|
145
|
+
(newMessage) => setMessage(newMessage),
|
|
84
146
|
"simulator-message",
|
|
85
147
|
);
|
|
86
148
|
|
|
@@ -88,22 +150,44 @@ export const SliceSimulator = ({
|
|
|
88
150
|
|
|
89
151
|
return () => {
|
|
90
152
|
simulatorManager.state.off(StateEventType.Slices, "simulator-slices");
|
|
91
|
-
|
|
92
153
|
simulatorManager.state.off(StateEventType.Message, "simulator-message");
|
|
93
154
|
};
|
|
94
155
|
}, []);
|
|
95
156
|
|
|
96
|
-
const SliceZoneComp = restProps.sliceZone;
|
|
97
|
-
|
|
98
157
|
return (
|
|
99
|
-
<
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
158
|
+
<div
|
|
159
|
+
className={[simulatorClass, className].filter(Boolean).join(" ")}
|
|
160
|
+
style={{
|
|
161
|
+
zIndex:
|
|
162
|
+
typeof zIndex === "undefined"
|
|
163
|
+
? defaultProps.zIndex
|
|
164
|
+
: zIndex ?? undefined,
|
|
165
|
+
position: "fixed",
|
|
166
|
+
top: 0,
|
|
167
|
+
left: 0,
|
|
168
|
+
width: "100%",
|
|
169
|
+
height: "100vh",
|
|
170
|
+
overflow: "auto",
|
|
171
|
+
background:
|
|
172
|
+
typeof background === "undefined"
|
|
173
|
+
? defaultProps.background
|
|
174
|
+
: background ?? undefined,
|
|
175
|
+
}}
|
|
105
176
|
>
|
|
106
|
-
|
|
107
|
-
|
|
177
|
+
{message ? (
|
|
178
|
+
<article dangerouslySetInnerHTML={{ __html: message }} />
|
|
179
|
+
) : hasSlices ? (
|
|
180
|
+
<div
|
|
181
|
+
id="root"
|
|
182
|
+
className={simulatorRootClass}
|
|
183
|
+
onClickCapture={onClickHandler as unknown as React.MouseEventHandler}
|
|
184
|
+
onSubmitCapture={
|
|
185
|
+
disableEventHandler as unknown as React.FormEventHandler
|
|
186
|
+
}
|
|
187
|
+
>
|
|
188
|
+
{SliceZoneComp ? <SliceZoneComp slices={slices} /> : children}
|
|
189
|
+
</div>
|
|
190
|
+
) : null}
|
|
191
|
+
</div>
|
|
108
192
|
);
|
|
109
193
|
};
|
package/src/simulator/index.ts
CHANGED
|
@@ -2,112 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
"use client";
|
|
4
4
|
|
|
5
|
-
import * as React from "react";
|
|
6
|
-
|
|
7
5
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
getDefaultMessage,
|
|
12
|
-
} from "@prismicio/simulator/kit";
|
|
13
|
-
import { compressToEncodedURIComponent } from "lz-string";
|
|
14
|
-
|
|
15
|
-
import { SliceSimulatorWrapper } from "../SliceSimulatorWrapper";
|
|
6
|
+
SliceSimulator as BaseSliceSimulator,
|
|
7
|
+
SliceSimulatorProps,
|
|
8
|
+
} from "../SliceSimulator";
|
|
16
9
|
import { revalidatePath } from "./actions";
|
|
17
|
-
import { getSlices } from "./getSlices";
|
|
18
|
-
|
|
19
|
-
const STATE_PARAMS_KEY = "state";
|
|
20
|
-
|
|
21
|
-
const throttle =
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
-
<TArgs extends [...any]>(fn: (...args: TArgs) => unknown, wait: number) => {
|
|
24
|
-
let timeoutId: ReturnType<typeof setTimeout>;
|
|
25
|
-
let lastCallTime = 0;
|
|
26
|
-
|
|
27
|
-
return (...args: TArgs) => {
|
|
28
|
-
clearTimeout(timeoutId);
|
|
29
|
-
|
|
30
|
-
const now = Date.now();
|
|
31
|
-
const timeSinceLastCall = now - lastCallTime;
|
|
32
|
-
const delayForNextCall = wait - timeSinceLastCall;
|
|
33
|
-
|
|
34
|
-
if (delayForNextCall <= 0) {
|
|
35
|
-
lastCallTime = now;
|
|
36
|
-
fn(...args);
|
|
37
|
-
} else {
|
|
38
|
-
timeoutId = setTimeout(() => {
|
|
39
|
-
lastCallTime = Date.now();
|
|
40
|
-
fn(...args);
|
|
41
|
-
}, delayForNextCall);
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
const throttledRevalidatePath = throttle(revalidatePath, 300);
|
|
46
|
-
|
|
47
|
-
const simulatorManager = new SimulatorManager();
|
|
48
|
-
|
|
49
|
-
export type SliceSimulatorProps = Omit<BaseSliceSimulatorProps, "state"> & {
|
|
50
|
-
children: React.ReactNode;
|
|
51
|
-
className?: string;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
export const SliceSimulator = ({
|
|
55
|
-
children,
|
|
56
|
-
background,
|
|
57
|
-
zIndex,
|
|
58
|
-
className,
|
|
59
|
-
}: SliceSimulatorProps): JSX.Element => {
|
|
60
|
-
const [message, setMessage] = React.useState(() => getDefaultMessage());
|
|
61
|
-
|
|
62
|
-
const state =
|
|
63
|
-
typeof window !== "undefined"
|
|
64
|
-
? new URL(window.location.href).searchParams.get(STATE_PARAMS_KEY)
|
|
65
|
-
: undefined;
|
|
66
|
-
const hasSlices = getSlices(state).length > 0;
|
|
67
|
-
|
|
68
|
-
React.useEffect(() => {
|
|
69
|
-
simulatorManager.state.on(
|
|
70
|
-
StateEventType.Slices,
|
|
71
|
-
(newSlices) => {
|
|
72
|
-
const url = new URL(window.location.href);
|
|
73
|
-
url.searchParams.set(
|
|
74
|
-
STATE_PARAMS_KEY,
|
|
75
|
-
compressToEncodedURIComponent(JSON.stringify(newSlices)),
|
|
76
|
-
);
|
|
77
|
-
window.history.pushState(null, "", url);
|
|
78
|
-
|
|
79
|
-
// A 0 ms timeout is needed to prevent a bug
|
|
80
|
-
// where the path is revalidated before the URL
|
|
81
|
-
// is updated with the new state.
|
|
82
|
-
const path = window.location.pathname;
|
|
83
|
-
setTimeout(() => throttledRevalidatePath(path), 0);
|
|
84
|
-
},
|
|
85
|
-
"simulator-slices",
|
|
86
|
-
);
|
|
87
|
-
simulatorManager.state.on(
|
|
88
|
-
StateEventType.Message,
|
|
89
|
-
(newMessage) => setMessage(newMessage),
|
|
90
|
-
"simulator-message",
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
simulatorManager.init();
|
|
94
|
-
|
|
95
|
-
return () => {
|
|
96
|
-
simulatorManager.state.off(StateEventType.Slices, "simulator-slices");
|
|
97
|
-
|
|
98
|
-
simulatorManager.state.off(StateEventType.Message, "simulator-message");
|
|
99
|
-
};
|
|
100
|
-
}, []);
|
|
101
10
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
message={message}
|
|
105
|
-
hasSlices={hasSlices}
|
|
106
|
-
background={background}
|
|
107
|
-
zIndex={zIndex}
|
|
108
|
-
className={className}
|
|
109
|
-
>
|
|
110
|
-
{children}
|
|
111
|
-
</SliceSimulatorWrapper>
|
|
112
|
-
);
|
|
11
|
+
export const SliceSimulator = (props: SliceSimulatorProps): JSX.Element => {
|
|
12
|
+
return <BaseSliceSimulator {...props} __revalidatePath={revalidatePath} />;
|
|
113
13
|
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const jsxRuntime = require("react/jsx-runtime");
|
|
4
|
-
const kit = require("@prismicio/simulator/kit");
|
|
5
|
-
const SliceSimulatorWrapper = ({ className, children, zIndex, background, message, hasSlices }) => {
|
|
6
|
-
const defaultProps = kit.getDefaultProps();
|
|
7
|
-
return jsxRuntime.jsx("div", { className: [kit.simulatorClass, className].filter(Boolean).join(" "), style: {
|
|
8
|
-
zIndex: typeof zIndex === "undefined" ? defaultProps.zIndex : zIndex ?? void 0,
|
|
9
|
-
position: "fixed",
|
|
10
|
-
top: 0,
|
|
11
|
-
left: 0,
|
|
12
|
-
width: "100%",
|
|
13
|
-
height: "100vh",
|
|
14
|
-
overflow: "auto",
|
|
15
|
-
background: typeof background === "undefined" ? defaultProps.background : background ?? void 0
|
|
16
|
-
}, children: message ? jsxRuntime.jsx("article", { dangerouslySetInnerHTML: { __html: message } }) : hasSlices ? jsxRuntime.jsx("div", { id: "root", className: kit.simulatorRootClass, onClickCapture: kit.onClickHandler, onSubmitCapture: kit.disableEventHandler, children }) : null });
|
|
17
|
-
};
|
|
18
|
-
exports.SliceSimulatorWrapper = SliceSimulatorWrapper;
|
|
19
|
-
//# sourceMappingURL=SliceSimulatorWrapper.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SliceSimulatorWrapper.cjs","sources":["../../../src/simulator/SliceSimulatorWrapper.tsx"],"sourcesContent":["import { ReactNode } from \"react\";\nimport {\n\tSliceSimulatorProps,\n\tdisableEventHandler,\n\tgetDefaultProps,\n\tonClickHandler,\n\tsimulatorClass,\n\tsimulatorRootClass,\n} from \"@prismicio/simulator/kit\";\n\ntype SliceSimulatorWrapperProps = {\n\tchildren: ReactNode;\n\tclassName?: string;\n\tmessage?: string;\n\thasSlices: boolean;\n} & Omit<SliceSimulatorProps, \"state\">;\n\n/**\n * A wrapper for the slice simulator that isolates the given children from the\n * page's layout.\n */\nexport const SliceSimulatorWrapper = ({\n\tclassName,\n\tchildren,\n\tzIndex,\n\tbackground,\n\tmessage,\n\thasSlices,\n}: SliceSimulatorWrapperProps): JSX.Element => {\n\tconst defaultProps = getDefaultProps();\n\n\treturn (\n\t\t<div\n\t\t\tclassName={[simulatorClass, className].filter(Boolean).join(\" \")}\n\t\t\tstyle={{\n\t\t\t\tzIndex:\n\t\t\t\t\ttypeof zIndex === \"undefined\"\n\t\t\t\t\t\t? defaultProps.zIndex\n\t\t\t\t\t\t: zIndex ?? undefined,\n\t\t\t\tposition: \"fixed\",\n\t\t\t\ttop: 0,\n\t\t\t\tleft: 0,\n\t\t\t\twidth: \"100%\",\n\t\t\t\theight: \"100vh\",\n\t\t\t\toverflow: \"auto\",\n\t\t\t\tbackground:\n\t\t\t\t\ttypeof background === \"undefined\"\n\t\t\t\t\t\t? defaultProps.background\n\t\t\t\t\t\t: background ?? undefined,\n\t\t\t}}\n\t\t>\n\t\t\t{message ? (\n\t\t\t\t<article dangerouslySetInnerHTML={{ __html: message }} />\n\t\t\t) : hasSlices ? (\n\t\t\t\t<div\n\t\t\t\t\tid=\"root\"\n\t\t\t\t\tclassName={simulatorRootClass}\n\t\t\t\t\tonClickCapture={onClickHandler as unknown as React.MouseEventHandler}\n\t\t\t\t\tonSubmitCapture={\n\t\t\t\t\t\tdisableEventHandler as unknown as React.FormEventHandler\n\t\t\t\t\t}\n\t\t\t\t>\n\t\t\t\t\t{children}\n\t\t\t\t</div>\n\t\t\t) : null}\n\t\t</div>\n\t);\n};\n"],"names":["getDefaultProps","_jsx","simulatorClass","simulatorRootClass","onClickHandler","disableEventHandler"],"mappings":";;;;AAqBa,MAAA,wBAAwB,CAAC,EACrC,WACA,UACA,QACA,YACA,SACA,gBAC6C;AAC7C,QAAM,eAAeA,IAAAA;AAErB,SACCC,wBACC,WAAW,CAACC,IAAAA,gBAAgB,SAAS,EAAE,OAAO,OAAO,EAAE,KAAK,GAAG,GAC/D,OAAO;AAAA,IACN,QACC,OAAO,WAAW,cACf,aAAa,SACb,UAAU;AAAA,IACd,UAAU;AAAA,IACV,KAAK;AAAA,IACL,MAAM;AAAA,IACN,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,YACC,OAAO,eAAe,cACnB,aAAa,aACb,cAAc;AAAA,EAGlB,GAAA,UAAA,UACAD,WAAA,IAAS,WAAA,EAAA,yBAAyB,EAAE,QAAQ,QAAS,EAAA,CAAA,IAClD,YACHA,WAAAA,IAAA,OAAA,EACC,IAAG,QACH,WAAWE,IAAAA,oBACX,gBAAgBC,IAAAA,gBAChB,iBACCC,IAAwD,qBAAA,SAAA,CAGhD,IAEP,KACC,CAAA;AAER;;"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { ReactNode } from "react";
|
|
2
|
-
import { SliceSimulatorProps } from "@prismicio/simulator/kit";
|
|
3
|
-
type SliceSimulatorWrapperProps = {
|
|
4
|
-
children: ReactNode;
|
|
5
|
-
className?: string;
|
|
6
|
-
message?: string;
|
|
7
|
-
hasSlices: boolean;
|
|
8
|
-
} & Omit<SliceSimulatorProps, "state">;
|
|
9
|
-
/**
|
|
10
|
-
* A wrapper for the slice simulator that isolates the given children from the
|
|
11
|
-
* page's layout.
|
|
12
|
-
*/
|
|
13
|
-
export declare const SliceSimulatorWrapper: ({ className, children, zIndex, background, message, hasSlices, }: SliceSimulatorWrapperProps) => JSX.Element;
|
|
14
|
-
export {};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import { getDefaultProps, simulatorClass, simulatorRootClass, onClickHandler, disableEventHandler } from "@prismicio/simulator/kit";
|
|
3
|
-
const SliceSimulatorWrapper = ({ className, children, zIndex, background, message, hasSlices }) => {
|
|
4
|
-
const defaultProps = getDefaultProps();
|
|
5
|
-
return jsx("div", { className: [simulatorClass, className].filter(Boolean).join(" "), style: {
|
|
6
|
-
zIndex: typeof zIndex === "undefined" ? defaultProps.zIndex : zIndex ?? void 0,
|
|
7
|
-
position: "fixed",
|
|
8
|
-
top: 0,
|
|
9
|
-
left: 0,
|
|
10
|
-
width: "100%",
|
|
11
|
-
height: "100vh",
|
|
12
|
-
overflow: "auto",
|
|
13
|
-
background: typeof background === "undefined" ? defaultProps.background : background ?? void 0
|
|
14
|
-
}, children: message ? jsx("article", { dangerouslySetInnerHTML: { __html: message } }) : hasSlices ? jsx("div", { id: "root", className: simulatorRootClass, onClickCapture: onClickHandler, onSubmitCapture: disableEventHandler, children }) : null });
|
|
15
|
-
};
|
|
16
|
-
export {
|
|
17
|
-
SliceSimulatorWrapper
|
|
18
|
-
};
|
|
19
|
-
//# sourceMappingURL=SliceSimulatorWrapper.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SliceSimulatorWrapper.js","sources":["../../../src/simulator/SliceSimulatorWrapper.tsx"],"sourcesContent":["import { ReactNode } from \"react\";\nimport {\n\tSliceSimulatorProps,\n\tdisableEventHandler,\n\tgetDefaultProps,\n\tonClickHandler,\n\tsimulatorClass,\n\tsimulatorRootClass,\n} from \"@prismicio/simulator/kit\";\n\ntype SliceSimulatorWrapperProps = {\n\tchildren: ReactNode;\n\tclassName?: string;\n\tmessage?: string;\n\thasSlices: boolean;\n} & Omit<SliceSimulatorProps, \"state\">;\n\n/**\n * A wrapper for the slice simulator that isolates the given children from the\n * page's layout.\n */\nexport const SliceSimulatorWrapper = ({\n\tclassName,\n\tchildren,\n\tzIndex,\n\tbackground,\n\tmessage,\n\thasSlices,\n}: SliceSimulatorWrapperProps): JSX.Element => {\n\tconst defaultProps = getDefaultProps();\n\n\treturn (\n\t\t<div\n\t\t\tclassName={[simulatorClass, className].filter(Boolean).join(\" \")}\n\t\t\tstyle={{\n\t\t\t\tzIndex:\n\t\t\t\t\ttypeof zIndex === \"undefined\"\n\t\t\t\t\t\t? defaultProps.zIndex\n\t\t\t\t\t\t: zIndex ?? undefined,\n\t\t\t\tposition: \"fixed\",\n\t\t\t\ttop: 0,\n\t\t\t\tleft: 0,\n\t\t\t\twidth: \"100%\",\n\t\t\t\theight: \"100vh\",\n\t\t\t\toverflow: \"auto\",\n\t\t\t\tbackground:\n\t\t\t\t\ttypeof background === \"undefined\"\n\t\t\t\t\t\t? defaultProps.background\n\t\t\t\t\t\t: background ?? undefined,\n\t\t\t}}\n\t\t>\n\t\t\t{message ? (\n\t\t\t\t<article dangerouslySetInnerHTML={{ __html: message }} />\n\t\t\t) : hasSlices ? (\n\t\t\t\t<div\n\t\t\t\t\tid=\"root\"\n\t\t\t\t\tclassName={simulatorRootClass}\n\t\t\t\t\tonClickCapture={onClickHandler as unknown as React.MouseEventHandler}\n\t\t\t\t\tonSubmitCapture={\n\t\t\t\t\t\tdisableEventHandler as unknown as React.FormEventHandler\n\t\t\t\t\t}\n\t\t\t\t>\n\t\t\t\t\t{children}\n\t\t\t\t</div>\n\t\t\t) : null}\n\t\t</div>\n\t);\n};\n"],"names":["_jsx"],"mappings":";;AAqBa,MAAA,wBAAwB,CAAC,EACrC,WACA,UACA,QACA,YACA,SACA,gBAC6C;AAC7C,QAAM,eAAe;AAErB,SACCA,aACC,WAAW,CAAC,gBAAgB,SAAS,EAAE,OAAO,OAAO,EAAE,KAAK,GAAG,GAC/D,OAAO;AAAA,IACN,QACC,OAAO,WAAW,cACf,aAAa,SACb,UAAU;AAAA,IACd,UAAU;AAAA,IACV,KAAK;AAAA,IACL,MAAM;AAAA,IACN,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,YACC,OAAO,eAAe,cACnB,aAAa,aACb,cAAc;AAAA,EAGlB,GAAA,UAAA,UACAA,IAAS,WAAA,EAAA,yBAAyB,EAAE,QAAQ,QAAS,EAAA,CAAA,IAClD,YACHA,IAAA,OAAA,EACC,IAAG,QACH,WAAW,oBACX,gBAAgB,gBAChB,iBACC,qBAAwD,SAAA,CAGhD,IAEP,KACC,CAAA;AAER;"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const getSlices = (state) => {
|
|
4
|
-
throw new Error("getSlices is designed only for Server Components. Convert your simulator page to a Server Component or remove the function from your Client Component.");
|
|
5
|
-
};
|
|
6
|
-
exports.getSlices = getSlices;
|
|
7
|
-
//# sourceMappingURL=getSlices.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getSlices.cjs","sources":["../../../src/simulator/getSlices.ts"],"sourcesContent":["import { StateEvents, StateEventType } from \"@prismicio/simulator/kit\";\n\n/**\n * Returns the simulator's slices from the page's `searchParams.state` value.\n * The `state` value is set by `<SliceSimulator>`.\n *\n * **Note**: `getSlices` should only be used in the App Router with a Server\n * Component.\n */\nexport const getSlices = (\n\tstate: string | null | undefined,\n): StateEvents[StateEventType.Slices] => {\n\t// Prevent tsserver + eslint warnings about unused vars.\n\tstate;\n\n\tthrow new Error(\n\t\t\"getSlices is designed only for Server Components. Convert your simulator page to a Server Component or remove the function from your Client Component.\",\n\t);\n};\n"],"names":[],"mappings":";;AASa,MAAA,YAAY,CACxB,UACuC;AAIjC,QAAA,IAAI,MACT,wJAAwJ;AAE1J;;"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
const getSlices = (state) => {
|
|
2
|
-
throw new Error("getSlices is designed only for Server Components. Convert your simulator page to a Server Component or remove the function from your Client Component.");
|
|
3
|
-
};
|
|
4
|
-
export {
|
|
5
|
-
getSlices
|
|
6
|
-
};
|
|
7
|
-
//# sourceMappingURL=getSlices.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getSlices.js","sources":["../../../src/simulator/getSlices.ts"],"sourcesContent":["import { StateEvents, StateEventType } from \"@prismicio/simulator/kit\";\n\n/**\n * Returns the simulator's slices from the page's `searchParams.state` value.\n * The `state` value is set by `<SliceSimulator>`.\n *\n * **Note**: `getSlices` should only be used in the App Router with a Server\n * Component.\n */\nexport const getSlices = (\n\tstate: string | null | undefined,\n): StateEvents[StateEventType.Slices] => {\n\t// Prevent tsserver + eslint warnings about unused vars.\n\tstate;\n\n\tthrow new Error(\n\t\t\"getSlices is designed only for Server Components. Convert your simulator page to a Server Component or remove the function from your Client Component.\",\n\t);\n};\n"],"names":[],"mappings":"AASa,MAAA,YAAY,CACxB,UACuC;AAIjC,QAAA,IAAI,MACT,wJAAwJ;AAE1J;"}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { ReactNode } from "react";
|
|
2
|
-
import {
|
|
3
|
-
SliceSimulatorProps,
|
|
4
|
-
disableEventHandler,
|
|
5
|
-
getDefaultProps,
|
|
6
|
-
onClickHandler,
|
|
7
|
-
simulatorClass,
|
|
8
|
-
simulatorRootClass,
|
|
9
|
-
} from "@prismicio/simulator/kit";
|
|
10
|
-
|
|
11
|
-
type SliceSimulatorWrapperProps = {
|
|
12
|
-
children: ReactNode;
|
|
13
|
-
className?: string;
|
|
14
|
-
message?: string;
|
|
15
|
-
hasSlices: boolean;
|
|
16
|
-
} & Omit<SliceSimulatorProps, "state">;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* A wrapper for the slice simulator that isolates the given children from the
|
|
20
|
-
* page's layout.
|
|
21
|
-
*/
|
|
22
|
-
export const SliceSimulatorWrapper = ({
|
|
23
|
-
className,
|
|
24
|
-
children,
|
|
25
|
-
zIndex,
|
|
26
|
-
background,
|
|
27
|
-
message,
|
|
28
|
-
hasSlices,
|
|
29
|
-
}: SliceSimulatorWrapperProps): JSX.Element => {
|
|
30
|
-
const defaultProps = getDefaultProps();
|
|
31
|
-
|
|
32
|
-
return (
|
|
33
|
-
<div
|
|
34
|
-
className={[simulatorClass, className].filter(Boolean).join(" ")}
|
|
35
|
-
style={{
|
|
36
|
-
zIndex:
|
|
37
|
-
typeof zIndex === "undefined"
|
|
38
|
-
? defaultProps.zIndex
|
|
39
|
-
: zIndex ?? undefined,
|
|
40
|
-
position: "fixed",
|
|
41
|
-
top: 0,
|
|
42
|
-
left: 0,
|
|
43
|
-
width: "100%",
|
|
44
|
-
height: "100vh",
|
|
45
|
-
overflow: "auto",
|
|
46
|
-
background:
|
|
47
|
-
typeof background === "undefined"
|
|
48
|
-
? defaultProps.background
|
|
49
|
-
: background ?? undefined,
|
|
50
|
-
}}
|
|
51
|
-
>
|
|
52
|
-
{message ? (
|
|
53
|
-
<article dangerouslySetInnerHTML={{ __html: message }} />
|
|
54
|
-
) : hasSlices ? (
|
|
55
|
-
<div
|
|
56
|
-
id="root"
|
|
57
|
-
className={simulatorRootClass}
|
|
58
|
-
onClickCapture={onClickHandler as unknown as React.MouseEventHandler}
|
|
59
|
-
onSubmitCapture={
|
|
60
|
-
disableEventHandler as unknown as React.FormEventHandler
|
|
61
|
-
}
|
|
62
|
-
>
|
|
63
|
-
{children}
|
|
64
|
-
</div>
|
|
65
|
-
) : null}
|
|
66
|
-
</div>
|
|
67
|
-
);
|
|
68
|
-
};
|