@prismicio/react 3.3.0 → 3.4.0-canary.e2a0168
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/PrismicToolbar.d.ts
CHANGED
|
@@ -8,6 +8,26 @@ type PrismicToolbarProps = {
|
|
|
8
8
|
* repository URL is `my-repo.prismic.io`.
|
|
9
9
|
*/
|
|
10
10
|
repositoryName: string;
|
|
11
|
+
/**
|
|
12
|
+
* Called when the Prismic toolbar triggers a preview update. This happens
|
|
13
|
+
* when the previewed content changes.
|
|
14
|
+
*
|
|
15
|
+
* The new ref can be read from `event.detail.ref`.
|
|
16
|
+
*
|
|
17
|
+
* The default page reload behavior can be cancelled with
|
|
18
|
+
* `event.preventDefault()`.
|
|
19
|
+
*/
|
|
20
|
+
onPreviewUpdate?: (event: CustomEvent<{
|
|
21
|
+
ref: string;
|
|
22
|
+
}>) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Called when the Prismic toolbar triggers a preview end. This happens when
|
|
25
|
+
* a preview session is closed.
|
|
26
|
+
*
|
|
27
|
+
* The default page reload behavior can be cancelled with
|
|
28
|
+
* `event.preventDefault()`.
|
|
29
|
+
*/
|
|
30
|
+
onPreviewEnd?: (event: CustomEvent<null>) => void;
|
|
11
31
|
};
|
|
12
32
|
/**
|
|
13
33
|
* Renders the Prismic Toolbar script to support draft previews.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicToolbar.d.ts","names":[],"sources":["../src/PrismicToolbar.tsx"],"sourcesContent":[],"mappings":";;;;KAMY,mBAAA;EAAA;
|
|
1
|
+
{"version":3,"file":"PrismicToolbar.d.ts","names":[],"sources":["../src/PrismicToolbar.tsx"],"sourcesContent":[],"mappings":";;;;KAMY,mBAAA;EAAA;AAuCZ;;;;;;;;;;;;;4BAvB2B;;;;;;;;;;yBASH;;;;;;;;;;;;;cAcX,gBAAgB,GAAG"}
|
package/dist/PrismicToolbar.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { useEffect } from "react";
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
4
|
import { getToolbarSrc } from "@prismicio/client";
|
|
5
5
|
|
|
6
6
|
//#region src/PrismicToolbar.tsx
|
|
@@ -16,8 +16,12 @@ import { getToolbarSrc } from "@prismicio/client";
|
|
|
16
16
|
* @see Learn how to set up preview functionality and the toolbar's role in preview sessions: {@link https://prismic.io/docs/previews}
|
|
17
17
|
*/
|
|
18
18
|
const PrismicToolbar = (props) => {
|
|
19
|
-
const { repositoryName } = props;
|
|
19
|
+
const { repositoryName, onPreviewUpdate, onPreviewEnd } = props;
|
|
20
20
|
const src = getToolbarSrc(repositoryName);
|
|
21
|
+
const onPreviewUpdateRef = useRef(onPreviewUpdate);
|
|
22
|
+
onPreviewUpdateRef.current = onPreviewUpdate;
|
|
23
|
+
const onPreviewEndRef = useRef(onPreviewEnd);
|
|
24
|
+
onPreviewEndRef.current = onPreviewEnd;
|
|
21
25
|
useEffect(() => {
|
|
22
26
|
if (!document.querySelector(`script[src="${src}"]`)) {
|
|
23
27
|
const script = document.createElement("script");
|
|
@@ -29,6 +33,12 @@ const PrismicToolbar = (props) => {
|
|
|
29
33
|
document.body.appendChild(script);
|
|
30
34
|
}
|
|
31
35
|
}, [repositoryName, src]);
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
const controller = new AbortController();
|
|
38
|
+
window.addEventListener("prismicPreviewUpdate", (event) => onPreviewUpdateRef.current?.(event), { signal: controller.signal });
|
|
39
|
+
window.addEventListener("prismicPreviewEnd", (event) => onPreviewEndRef.current?.(event), { signal: controller.signal });
|
|
40
|
+
return () => controller.abort();
|
|
41
|
+
}, []);
|
|
32
42
|
return null;
|
|
33
43
|
};
|
|
34
44
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicToolbar.js","names":[],"sources":["../src/PrismicToolbar.tsx"],"sourcesContent":["\"use client\";\n\nimport {
|
|
1
|
+
{"version":3,"file":"PrismicToolbar.js","names":[],"sources":["../src/PrismicToolbar.tsx"],"sourcesContent":["\"use client\";\n\nimport { getToolbarSrc } from \"@prismicio/client\";\nimport { type FC, useEffect, useRef } from \"react\";\n\n/** Props for `<PrismicToolbar>`. */\nexport type PrismicToolbarProps = {\n\t/**\n\t * The name of the Prismic repository. For example, `\"my-repo\"` if the\n\t * repository URL is `my-repo.prismic.io`.\n\t */\n\trepositoryName: string;\n\n\t/**\n\t * Called when the Prismic toolbar triggers a preview update. This happens\n\t * when the previewed content changes.\n\t *\n\t * The new ref can be read from `event.detail.ref`.\n\t *\n\t * The default page reload behavior can be cancelled with\n\t * `event.preventDefault()`.\n\t */\n\tonPreviewUpdate?: (event: CustomEvent<{ ref: string }>) => void;\n\n\t/**\n\t * Called when the Prismic toolbar triggers a preview end. This happens when\n\t * a preview session is closed.\n\t *\n\t * The default page reload behavior can be cancelled with\n\t * `event.preventDefault()`.\n\t */\n\tonPreviewEnd?: (event: CustomEvent<null>) => void;\n};\n\n/**\n * Renders the Prismic Toolbar script to support draft previews.\n *\n * @example\n *\n * ```tsx\n * <PrismicToolbar repositoryName=\"my-repo\" />;\n * ```\n *\n * @see Learn how to set up preview functionality and the toolbar's role in preview sessions: {@link https://prismic.io/docs/previews}\n */\nexport const PrismicToolbar: FC<PrismicToolbarProps> = (props) => {\n\tconst { repositoryName, onPreviewUpdate, onPreviewEnd } = props;\n\n\tconst src = getToolbarSrc(repositoryName);\n\n\tconst onPreviewUpdateRef = useRef(onPreviewUpdate);\n\tonPreviewUpdateRef.current = onPreviewUpdate;\n\n\tconst onPreviewEndRef = useRef(onPreviewEnd);\n\tonPreviewEndRef.current = onPreviewEnd;\n\n\tuseEffect(() => {\n\t\tconst existingScript = document.querySelector(`script[src=\"${src}\"]`);\n\n\t\tif (!existingScript) {\n\t\t\tconst script = document.createElement(\"script\");\n\t\t\tscript.src = src;\n\t\t\tscript.defer = true;\n\n\t\t\t// Used to distinguish the toolbar element from other elements.\n\t\t\tscript.dataset.prismicToolbar = \"\";\n\t\t\tscript.dataset.repositoryName = repositoryName;\n\n\t\t\t// Disable Happy DOM `<script>` evaluation during tests.\n\t\t\t// @ts-expect-error - `_evaluateScript` is a Happy DOM-specific property.\n\t\t\tscript._evaluateScript = false;\n\n\t\t\tdocument.body.appendChild(script);\n\t\t}\n\t}, [repositoryName, src]);\n\n\tuseEffect(() => {\n\t\tconst controller = new AbortController();\n\n\t\twindow.addEventListener(\n\t\t\t\"prismicPreviewUpdate\",\n\t\t\t(event) =>\n\t\t\t\tonPreviewUpdateRef.current?.(event as CustomEvent<{ ref: string }>),\n\t\t\t{ signal: controller.signal },\n\t\t);\n\n\t\twindow.addEventListener(\n\t\t\t\"prismicPreviewEnd\",\n\t\t\t(event) => onPreviewEndRef.current?.(event as CustomEvent<null>),\n\t\t\t{ signal: controller.signal },\n\t\t);\n\n\t\treturn () => controller.abort();\n\t}, []);\n\n\treturn null;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AA6CA,MAAa,kBAA2C,UAAU;CACjE,MAAM,EAAE,gBAAgB,iBAAiB,iBAAiB;CAE1D,MAAM,MAAM,cAAc,eAAe;CAEzC,MAAM,qBAAqB,OAAO,gBAAgB;AAClD,oBAAmB,UAAU;CAE7B,MAAM,kBAAkB,OAAO,aAAa;AAC5C,iBAAgB,UAAU;AAE1B,iBAAgB;AAGf,MAAI,CAFmB,SAAS,cAAc,eAAe,IAAI,IAAI,EAEhD;GACpB,MAAM,SAAS,SAAS,cAAc,SAAS;AAC/C,UAAO,MAAM;AACb,UAAO,QAAQ;AAGf,UAAO,QAAQ,iBAAiB;AAChC,UAAO,QAAQ,iBAAiB;AAIhC,UAAO,kBAAkB;AAEzB,YAAS,KAAK,YAAY,OAAO;;IAEhC,CAAC,gBAAgB,IAAI,CAAC;AAEzB,iBAAgB;EACf,MAAM,aAAa,IAAI,iBAAiB;AAExC,SAAO,iBACN,yBACC,UACA,mBAAmB,UAAU,MAAsC,EACpE,EAAE,QAAQ,WAAW,QAAQ,CAC7B;AAED,SAAO,iBACN,sBACC,UAAU,gBAAgB,UAAU,MAA2B,EAChE,EAAE,QAAQ,WAAW,QAAQ,CAC7B;AAED,eAAa,WAAW,OAAO;IAC7B,EAAE,CAAC;AAEN,QAAO"}
|
package/dist/package.js
CHANGED
package/package.json
CHANGED
package/src/PrismicToolbar.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { type FC, useEffect } from "react";
|
|
4
3
|
import { getToolbarSrc } from "@prismicio/client";
|
|
4
|
+
import { type FC, useEffect, useRef } from "react";
|
|
5
5
|
|
|
6
6
|
/** Props for `<PrismicToolbar>`. */
|
|
7
7
|
export type PrismicToolbarProps = {
|
|
@@ -10,6 +10,26 @@ export type PrismicToolbarProps = {
|
|
|
10
10
|
* repository URL is `my-repo.prismic.io`.
|
|
11
11
|
*/
|
|
12
12
|
repositoryName: string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Called when the Prismic toolbar triggers a preview update. This happens
|
|
16
|
+
* when the previewed content changes.
|
|
17
|
+
*
|
|
18
|
+
* The new ref can be read from `event.detail.ref`.
|
|
19
|
+
*
|
|
20
|
+
* The default page reload behavior can be cancelled with
|
|
21
|
+
* `event.preventDefault()`.
|
|
22
|
+
*/
|
|
23
|
+
onPreviewUpdate?: (event: CustomEvent<{ ref: string }>) => void;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Called when the Prismic toolbar triggers a preview end. This happens when
|
|
27
|
+
* a preview session is closed.
|
|
28
|
+
*
|
|
29
|
+
* The default page reload behavior can be cancelled with
|
|
30
|
+
* `event.preventDefault()`.
|
|
31
|
+
*/
|
|
32
|
+
onPreviewEnd?: (event: CustomEvent<null>) => void;
|
|
13
33
|
};
|
|
14
34
|
|
|
15
35
|
/**
|
|
@@ -24,10 +44,16 @@ export type PrismicToolbarProps = {
|
|
|
24
44
|
* @see Learn how to set up preview functionality and the toolbar's role in preview sessions: {@link https://prismic.io/docs/previews}
|
|
25
45
|
*/
|
|
26
46
|
export const PrismicToolbar: FC<PrismicToolbarProps> = (props) => {
|
|
27
|
-
const { repositoryName } = props;
|
|
47
|
+
const { repositoryName, onPreviewUpdate, onPreviewEnd } = props;
|
|
28
48
|
|
|
29
49
|
const src = getToolbarSrc(repositoryName);
|
|
30
50
|
|
|
51
|
+
const onPreviewUpdateRef = useRef(onPreviewUpdate);
|
|
52
|
+
onPreviewUpdateRef.current = onPreviewUpdate;
|
|
53
|
+
|
|
54
|
+
const onPreviewEndRef = useRef(onPreviewEnd);
|
|
55
|
+
onPreviewEndRef.current = onPreviewEnd;
|
|
56
|
+
|
|
31
57
|
useEffect(() => {
|
|
32
58
|
const existingScript = document.querySelector(`script[src="${src}"]`);
|
|
33
59
|
|
|
@@ -48,5 +74,24 @@ export const PrismicToolbar: FC<PrismicToolbarProps> = (props) => {
|
|
|
48
74
|
}
|
|
49
75
|
}, [repositoryName, src]);
|
|
50
76
|
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
const controller = new AbortController();
|
|
79
|
+
|
|
80
|
+
window.addEventListener(
|
|
81
|
+
"prismicPreviewUpdate",
|
|
82
|
+
(event) =>
|
|
83
|
+
onPreviewUpdateRef.current?.(event as CustomEvent<{ ref: string }>),
|
|
84
|
+
{ signal: controller.signal },
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
window.addEventListener(
|
|
88
|
+
"prismicPreviewEnd",
|
|
89
|
+
(event) => onPreviewEndRef.current?.(event as CustomEvent<null>),
|
|
90
|
+
{ signal: controller.signal },
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
return () => controller.abort();
|
|
94
|
+
}, []);
|
|
95
|
+
|
|
51
96
|
return null;
|
|
52
97
|
};
|