sanity-plugin-iframe-pane 1.1.0 → 2.0.0
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/LICENSE +1 -1
- package/README.md +10 -1
- package/lib/cjs/index.js +204 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/esm/index.js +195 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/types/index.d.ts +21 -0
- package/lib/types/index.d.ts.map +1 -0
- package/package.json +47 -24
- package/sanity.json +7 -6
- package/src/Iframe.tsx +97 -55
- package/src/index.ts +5 -0
- package/v2-incompatible.js +11 -0
- package/.babelrc +0 -3
- package/lib/Iframe.js +0 -215
- package/lib/Iframe.js.map +0 -1
- package/lib/hooks/useCopytoClipboard.js +0 -64
- package/lib/hooks/useCopytoClipboard.js.map +0 -1
- package/src/hooks/useCopytoClipboard.ts +0 -30
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# sanity-plugin-iframe-pane
|
|
2
2
|
|
|
3
|
+
> NOTE
|
|
4
|
+
> This is for the Studio v3 version of the plugin
|
|
5
|
+
|
|
3
6
|
Display any URL in a View Pane, along with helpful buttons to Copy the URL or open in a new tab.
|
|
4
7
|
|
|
5
8
|
Accepts either a string or an async function to resolve a URL based on the current document.
|
|
@@ -9,7 +12,13 @@ Accepts either a string or an async function to resolve a URL based on the curre
|
|
|
9
12
|
## Installation
|
|
10
13
|
|
|
11
14
|
```
|
|
12
|
-
|
|
15
|
+
npm install --save sanity-plugin-iframe-pane-v3
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
or
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
yarn add sanity-plugin-iframe-pane-v3
|
|
13
22
|
```
|
|
14
23
|
|
|
15
24
|
This is designed to be used as a [Component inside of a View](https://www.sanity.io/docs/structure-builder-reference#c0c8284844b7).
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
var $k7rGe$reactjsxruntime = require("react/jsx-runtime");
|
|
2
|
+
var $k7rGe$react = require("react");
|
|
3
|
+
var $k7rGe$sanityui = require("@sanity/ui");
|
|
4
|
+
var $k7rGe$sanityicons = require("@sanity/icons");
|
|
5
|
+
var $k7rGe$usehooksts = require("usehooks-ts");
|
|
6
|
+
|
|
7
|
+
function $parcel$defineInteropFlag(a) {
|
|
8
|
+
Object.defineProperty(a, '__esModule', {value: true, configurable: true});
|
|
9
|
+
}
|
|
10
|
+
function $parcel$export(e, n, v, s) {
|
|
11
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
$parcel$defineInteropFlag(module.exports);
|
|
15
|
+
|
|
16
|
+
$parcel$export(module.exports, "default", () => $244e63ca53592e4d$export$2e2bcd8739ae039);
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
const $f4b318483f84cb22$var$sizes = {
|
|
23
|
+
desktop: {
|
|
24
|
+
width: `100%`,
|
|
25
|
+
height: `100%`,
|
|
26
|
+
maxHeight: `100%`
|
|
27
|
+
},
|
|
28
|
+
mobile: {
|
|
29
|
+
width: 414,
|
|
30
|
+
height: `100%`,
|
|
31
|
+
maxHeight: 736
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const $f4b318483f84cb22$var$DEFAULT_SIZE = `desktop`;
|
|
35
|
+
function $f4b318483f84cb22$var$Iframe(props) {
|
|
36
|
+
const { document: sanityDocument , options: options } = props;
|
|
37
|
+
const { url: url , defaultSize: defaultSize = $f4b318483f84cb22$var$DEFAULT_SIZE , reload: reload } = options;
|
|
38
|
+
const [displayUrl, setDisplayUrl] = (0, $k7rGe$react.useState)(url && typeof url === "string" ? url : ``);
|
|
39
|
+
const [iframeSize, setIframeSize] = (0, $k7rGe$react.useState)($f4b318483f84cb22$var$sizes?.[defaultSize] ? defaultSize : $f4b318483f84cb22$var$DEFAULT_SIZE);
|
|
40
|
+
const input = (0, $k7rGe$react.useRef)(null);
|
|
41
|
+
const iframe = (0, $k7rGe$react.useRef)(null);
|
|
42
|
+
const { displayed: displayed } = sanityDocument;
|
|
43
|
+
const [, copy] = (0, $k7rGe$usehooksts.useCopyToClipboard)();
|
|
44
|
+
function handleCopy() {
|
|
45
|
+
if (!input?.current?.value) return;
|
|
46
|
+
copy(input.current.value);
|
|
47
|
+
}
|
|
48
|
+
function handleReload() {
|
|
49
|
+
if (!iframe?.current) return;
|
|
50
|
+
// Funky way to reload an iframe without CORS issuies
|
|
51
|
+
// eslint-disable-next-line no-self-assign
|
|
52
|
+
iframe.current.src = iframe.current.src;
|
|
53
|
+
}
|
|
54
|
+
// Reload on new revisions
|
|
55
|
+
(0, $k7rGe$react.useEffect)(()=>{
|
|
56
|
+
if (reload?.revision) handleReload();
|
|
57
|
+
}, [
|
|
58
|
+
displayed._rev,
|
|
59
|
+
reload?.revision
|
|
60
|
+
]);
|
|
61
|
+
// Set initial URL and refresh on new revisions
|
|
62
|
+
(0, $k7rGe$react.useEffect)(()=>{
|
|
63
|
+
const getUrl = async ()=>{
|
|
64
|
+
const resolveUrl = typeof url === "function" ? await url(displayed) : ``;
|
|
65
|
+
// Only update state if URL has changed
|
|
66
|
+
if (resolveUrl !== displayUrl && resolveUrl && typeof resolveUrl === "string") setDisplayUrl(resolveUrl);
|
|
67
|
+
};
|
|
68
|
+
if (typeof url === "function") getUrl();
|
|
69
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
70
|
+
}, [
|
|
71
|
+
displayed._rev
|
|
72
|
+
]);
|
|
73
|
+
if (!displayUrl || typeof displayUrl !== "string") return /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.ThemeProvider), {
|
|
74
|
+
children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Flex), {
|
|
75
|
+
padding: 5,
|
|
76
|
+
align: "center",
|
|
77
|
+
justify: "center",
|
|
78
|
+
children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Spinner), {})
|
|
79
|
+
})
|
|
80
|
+
});
|
|
81
|
+
return /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsxs)((0, $k7rGe$sanityui.ThemeProvider), {
|
|
82
|
+
children: [
|
|
83
|
+
/*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)("textarea", {
|
|
84
|
+
style: {
|
|
85
|
+
position: `absolute`,
|
|
86
|
+
pointerEvents: `none`,
|
|
87
|
+
opacity: 0
|
|
88
|
+
},
|
|
89
|
+
ref: input,
|
|
90
|
+
value: displayUrl,
|
|
91
|
+
readOnly: true,
|
|
92
|
+
tabIndex: -1
|
|
93
|
+
}),
|
|
94
|
+
/*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsxs)((0, $k7rGe$sanityui.Flex), {
|
|
95
|
+
direction: "column",
|
|
96
|
+
style: {
|
|
97
|
+
height: `100%`
|
|
98
|
+
},
|
|
99
|
+
children: [
|
|
100
|
+
/*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Card), {
|
|
101
|
+
padding: 2,
|
|
102
|
+
borderBottom: true,
|
|
103
|
+
children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsxs)((0, $k7rGe$sanityui.Flex), {
|
|
104
|
+
align: "center",
|
|
105
|
+
gap: 2,
|
|
106
|
+
children: [
|
|
107
|
+
/*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Flex), {
|
|
108
|
+
align: "center",
|
|
109
|
+
gap: 1,
|
|
110
|
+
children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Button), {
|
|
111
|
+
fontSize: [
|
|
112
|
+
1
|
|
113
|
+
],
|
|
114
|
+
padding: 2,
|
|
115
|
+
tone: "primary",
|
|
116
|
+
mode: iframeSize === "mobile" ? "default" : "ghost",
|
|
117
|
+
icon: (0, $k7rGe$sanityicons.MobileDeviceIcon),
|
|
118
|
+
onClick: ()=>setIframeSize(iframeSize === "mobile" ? "desktop" : "mobile")
|
|
119
|
+
})
|
|
120
|
+
}),
|
|
121
|
+
/*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Box), {
|
|
122
|
+
flex: 1,
|
|
123
|
+
children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Text), {
|
|
124
|
+
size: 0,
|
|
125
|
+
textOverflow: "ellipsis",
|
|
126
|
+
children: displayUrl
|
|
127
|
+
})
|
|
128
|
+
}),
|
|
129
|
+
/*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsxs)((0, $k7rGe$sanityui.Flex), {
|
|
130
|
+
align: "center",
|
|
131
|
+
gap: 1,
|
|
132
|
+
children: [
|
|
133
|
+
reload?.button ? /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Button), {
|
|
134
|
+
fontSize: [
|
|
135
|
+
1
|
|
136
|
+
],
|
|
137
|
+
padding: 2,
|
|
138
|
+
icon: (0, $k7rGe$sanityicons.UndoIcon),
|
|
139
|
+
title: "Reload",
|
|
140
|
+
"aria-label": "Reload",
|
|
141
|
+
onClick: ()=>handleReload()
|
|
142
|
+
}) : null,
|
|
143
|
+
/*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Button), {
|
|
144
|
+
fontSize: [
|
|
145
|
+
1
|
|
146
|
+
],
|
|
147
|
+
icon: (0, $k7rGe$sanityicons.CopyIcon),
|
|
148
|
+
padding: [
|
|
149
|
+
2
|
|
150
|
+
],
|
|
151
|
+
title: "Copy",
|
|
152
|
+
"aria-label": "Copy",
|
|
153
|
+
onClick: ()=>handleCopy()
|
|
154
|
+
}),
|
|
155
|
+
/*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Button), {
|
|
156
|
+
fontSize: [
|
|
157
|
+
1
|
|
158
|
+
],
|
|
159
|
+
icon: (0, $k7rGe$sanityicons.LeaveIcon),
|
|
160
|
+
padding: [
|
|
161
|
+
2
|
|
162
|
+
],
|
|
163
|
+
text: "Open",
|
|
164
|
+
tone: "primary",
|
|
165
|
+
onClick: ()=>window.open(displayUrl)
|
|
166
|
+
})
|
|
167
|
+
]
|
|
168
|
+
})
|
|
169
|
+
]
|
|
170
|
+
})
|
|
171
|
+
}),
|
|
172
|
+
/*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Card), {
|
|
173
|
+
tone: "transparent",
|
|
174
|
+
padding: iframeSize === "mobile" ? 2 : 0,
|
|
175
|
+
style: {
|
|
176
|
+
height: `100%`
|
|
177
|
+
},
|
|
178
|
+
children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Flex), {
|
|
179
|
+
align: "center",
|
|
180
|
+
justify: "center",
|
|
181
|
+
style: {
|
|
182
|
+
height: `100%`
|
|
183
|
+
},
|
|
184
|
+
children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)("iframe", {
|
|
185
|
+
ref: iframe,
|
|
186
|
+
title: "preview",
|
|
187
|
+
style: $f4b318483f84cb22$var$sizes[iframeSize],
|
|
188
|
+
frameBorder: "0",
|
|
189
|
+
src: displayUrl
|
|
190
|
+
})
|
|
191
|
+
})
|
|
192
|
+
})
|
|
193
|
+
]
|
|
194
|
+
})
|
|
195
|
+
]
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
var $f4b318483f84cb22$export$2e2bcd8739ae039 = $f4b318483f84cb22$var$Iframe;
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
var $244e63ca53592e4d$export$2e2bcd8739ae039 = (0, $f4b318483f84cb22$export$2e2bcd8739ae039);
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
//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AAC,QAAQ;4BAAC,KAAK,EAAE;gCAAE,MAAM,EAAE,CAAC,IAAI,CAAC;6BAAE;sCAC7D,cAAA,gCAAC,QAAM;gCACL,GAAG,EAAE,MAAM;gCACX,KAAK,EAAC,SAAS;gCACf,KAAK,EAAE,2BAAK,CAAC,UAAU,CAAC;gCACxB,WAAW,EAAC,GAAG;gCACf,GAAG,EAAE,UAAU;8BACf;0BACG;sBACF;;cACF;;MACO,CAChB;CACH;IAED,wCAAsB,GAAP,4BAAM;;AD3MrB;IAEA,wCAA+B,GAAhB,CAAA,GAAA,wCAAe,CAAA","sources":["src/index.ts","src/Iframe.tsx"],"sourcesContent":["import IframeComponent, { IframeOptions as IframeOptionsType } from \"./Iframe\";\n\nexport default IframeComponent;\n\nexport type IframeOptions = IframeOptionsType;\n","import React, { useEffect, useState, useRef } from \"react\";\nimport { SanityDocumentLike } from \"sanity\";\nimport {\n Box,\n Flex,\n Text,\n Button,\n ThemeProvider,\n Card,\n Spinner,\n} from \"@sanity/ui\";\nimport { UndoIcon, CopyIcon, LeaveIcon, MobileDeviceIcon } from \"@sanity/icons\";\n\nimport { useCopyToClipboard } from \"usehooks-ts\";\n\ntype Size = \"desktop\" | \"mobile\";\n\ntype SizeProps = {\n [key in Size]: {\n width: string | number;\n height: string | number;\n maxHeight: string | number;\n };\n};\n\nconst sizes: SizeProps = {\n desktop: {\n width: `100%`,\n height: `100%`,\n maxHeight: `100%`,\n },\n mobile: {\n width: 414,\n height: `100%`,\n maxHeight: 736,\n },\n};\n\nexport type IframeOptions = {\n url: string | ((document: SanityDocumentLike) => unknown);\n defaultSize?: Size;\n reload?: {\n revision?: boolean;\n button?: boolean;\n };\n};\n\nexport type IframeProps = {\n document: {\n displayed: SanityDocumentLike;\n };\n options: IframeOptions;\n};\n\nconst DEFAULT_SIZE = `desktop`;\n\nfunction Iframe(props: IframeProps) {\n const { document: sanityDocument, options } = props;\n const { url, defaultSize = DEFAULT_SIZE, reload } = options;\n const [displayUrl, setDisplayUrl] = useState(\n url && typeof url === \"string\" ? url : ``\n );\n const [iframeSize, setIframeSize] = useState(\n sizes?.[defaultSize] ? defaultSize : DEFAULT_SIZE\n );\n const input = useRef<HTMLTextAreaElement>(null);\n const iframe = useRef<HTMLIFrameElement>(null);\n const { displayed } = sanityDocument;\n const [, copy] = useCopyToClipboard();\n\n function handleCopy() {\n if (!input?.current?.value) return;\n\n copy(input.current.value);\n }\n\n function handleReload() {\n if (!iframe?.current) {\n return;\n }\n\n // Funky way to reload an iframe without CORS issuies\n // eslint-disable-next-line no-self-assign\n iframe.current.src = iframe.current.src;\n }\n\n // Reload on new revisions\n useEffect(() => {\n if (reload?.revision) {\n handleReload();\n }\n }, [displayed._rev, reload?.revision]);\n\n // Set initial URL and refresh on new revisions\n useEffect(() => {\n const getUrl = async () => {\n const resolveUrl = typeof url === \"function\" ? await url(displayed) : ``;\n\n // Only update state if URL has changed\n if (\n resolveUrl !== displayUrl &&\n resolveUrl &&\n typeof resolveUrl === \"string\"\n ) {\n setDisplayUrl(resolveUrl);\n }\n };\n\n if (typeof url === \"function\") {\n getUrl();\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [displayed._rev]);\n\n if (!displayUrl || typeof displayUrl !== \"string\") {\n return (\n <ThemeProvider>\n <Flex padding={5} align=\"center\" justify=\"center\">\n <Spinner />\n </Flex>\n </ThemeProvider>\n );\n }\n\n return (\n <ThemeProvider>\n <textarea\n style={{ position: `absolute`, pointerEvents: `none`, opacity: 0 }}\n ref={input}\n value={displayUrl}\n readOnly\n tabIndex={-1}\n />\n <Flex direction=\"column\" style={{ height: `100%` }}>\n <Card padding={2} borderBottom>\n <Flex align=\"center\" gap={2}>\n <Flex align=\"center\" gap={1}>\n <Button\n fontSize={[1]}\n padding={2}\n tone=\"primary\"\n mode={iframeSize === \"mobile\" ? \"default\" : \"ghost\"}\n icon={MobileDeviceIcon}\n onClick={() =>\n setIframeSize(iframeSize === \"mobile\" ? \"desktop\" : \"mobile\")\n }\n />\n </Flex>\n <Box flex={1}>\n <Text size={0} textOverflow=\"ellipsis\">\n {displayUrl}\n </Text>\n </Box>\n <Flex align=\"center\" gap={1}>\n {reload?.button ? (\n <Button\n fontSize={[1]}\n padding={2}\n icon={UndoIcon}\n title=\"Reload\"\n aria-label=\"Reload\"\n onClick={() => handleReload()}\n />\n ) : null}\n <Button\n fontSize={[1]}\n icon={CopyIcon}\n padding={[2]}\n title=\"Copy\"\n aria-label=\"Copy\"\n onClick={() => handleCopy()}\n />\n <Button\n fontSize={[1]}\n icon={LeaveIcon}\n padding={[2]}\n text=\"Open\"\n tone=\"primary\"\n onClick={() => window.open(displayUrl)}\n />\n </Flex>\n </Flex>\n </Card>\n <Card\n tone=\"transparent\"\n padding={iframeSize === \"mobile\" ? 2 : 0}\n style={{ height: `100%` }}\n >\n <Flex align=\"center\" justify=\"center\" style={{ height: `100%` }}>\n <iframe\n ref={iframe}\n title=\"preview\"\n style={sizes[iframeSize]}\n frameBorder=\"0\"\n src={displayUrl}\n />\n </Flex>\n </Card>\n </Flex>\n </ThemeProvider>\n );\n}\n\nexport default Iframe;\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../../"}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import {jsx as $gGrEF$jsx, jsxs as $gGrEF$jsxs} from "react/jsx-runtime";
|
|
2
|
+
import {useState as $gGrEF$useState, useRef as $gGrEF$useRef, useEffect as $gGrEF$useEffect} from "react";
|
|
3
|
+
import {ThemeProvider as $gGrEF$ThemeProvider, Flex as $gGrEF$Flex, Spinner as $gGrEF$Spinner, Card as $gGrEF$Card, Button as $gGrEF$Button, Box as $gGrEF$Box, Text as $gGrEF$Text} from "@sanity/ui";
|
|
4
|
+
import {MobileDeviceIcon as $gGrEF$MobileDeviceIcon, UndoIcon as $gGrEF$UndoIcon, CopyIcon as $gGrEF$CopyIcon, LeaveIcon as $gGrEF$LeaveIcon} from "@sanity/icons";
|
|
5
|
+
import {useCopyToClipboard as $gGrEF$useCopyToClipboard} from "usehooks-ts";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
const $b1fbe8f05b2a0ffa$var$sizes = {
|
|
13
|
+
desktop: {
|
|
14
|
+
width: `100%`,
|
|
15
|
+
height: `100%`,
|
|
16
|
+
maxHeight: `100%`
|
|
17
|
+
},
|
|
18
|
+
mobile: {
|
|
19
|
+
width: 414,
|
|
20
|
+
height: `100%`,
|
|
21
|
+
maxHeight: 736
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const $b1fbe8f05b2a0ffa$var$DEFAULT_SIZE = `desktop`;
|
|
25
|
+
function $b1fbe8f05b2a0ffa$var$Iframe(props) {
|
|
26
|
+
const { document: sanityDocument , options: options } = props;
|
|
27
|
+
const { url: url , defaultSize: defaultSize = $b1fbe8f05b2a0ffa$var$DEFAULT_SIZE , reload: reload } = options;
|
|
28
|
+
const [displayUrl, setDisplayUrl] = (0, $gGrEF$useState)(url && typeof url === "string" ? url : ``);
|
|
29
|
+
const [iframeSize, setIframeSize] = (0, $gGrEF$useState)($b1fbe8f05b2a0ffa$var$sizes?.[defaultSize] ? defaultSize : $b1fbe8f05b2a0ffa$var$DEFAULT_SIZE);
|
|
30
|
+
const input = (0, $gGrEF$useRef)(null);
|
|
31
|
+
const iframe = (0, $gGrEF$useRef)(null);
|
|
32
|
+
const { displayed: displayed } = sanityDocument;
|
|
33
|
+
const [, copy] = (0, $gGrEF$useCopyToClipboard)();
|
|
34
|
+
function handleCopy() {
|
|
35
|
+
if (!input?.current?.value) return;
|
|
36
|
+
copy(input.current.value);
|
|
37
|
+
}
|
|
38
|
+
function handleReload() {
|
|
39
|
+
if (!iframe?.current) return;
|
|
40
|
+
// Funky way to reload an iframe without CORS issuies
|
|
41
|
+
// eslint-disable-next-line no-self-assign
|
|
42
|
+
iframe.current.src = iframe.current.src;
|
|
43
|
+
}
|
|
44
|
+
// Reload on new revisions
|
|
45
|
+
(0, $gGrEF$useEffect)(()=>{
|
|
46
|
+
if (reload?.revision) handleReload();
|
|
47
|
+
}, [
|
|
48
|
+
displayed._rev,
|
|
49
|
+
reload?.revision
|
|
50
|
+
]);
|
|
51
|
+
// Set initial URL and refresh on new revisions
|
|
52
|
+
(0, $gGrEF$useEffect)(()=>{
|
|
53
|
+
const getUrl = async ()=>{
|
|
54
|
+
const resolveUrl = typeof url === "function" ? await url(displayed) : ``;
|
|
55
|
+
// Only update state if URL has changed
|
|
56
|
+
if (resolveUrl !== displayUrl && resolveUrl && typeof resolveUrl === "string") setDisplayUrl(resolveUrl);
|
|
57
|
+
};
|
|
58
|
+
if (typeof url === "function") getUrl();
|
|
59
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
60
|
+
}, [
|
|
61
|
+
displayed._rev
|
|
62
|
+
]);
|
|
63
|
+
if (!displayUrl || typeof displayUrl !== "string") return /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$ThemeProvider), {
|
|
64
|
+
children: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Flex), {
|
|
65
|
+
padding: 5,
|
|
66
|
+
align: "center",
|
|
67
|
+
justify: "center",
|
|
68
|
+
children: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Spinner), {})
|
|
69
|
+
})
|
|
70
|
+
});
|
|
71
|
+
return /*#__PURE__*/ (0, $gGrEF$jsxs)((0, $gGrEF$ThemeProvider), {
|
|
72
|
+
children: [
|
|
73
|
+
/*#__PURE__*/ (0, $gGrEF$jsx)("textarea", {
|
|
74
|
+
style: {
|
|
75
|
+
position: `absolute`,
|
|
76
|
+
pointerEvents: `none`,
|
|
77
|
+
opacity: 0
|
|
78
|
+
},
|
|
79
|
+
ref: input,
|
|
80
|
+
value: displayUrl,
|
|
81
|
+
readOnly: true,
|
|
82
|
+
tabIndex: -1
|
|
83
|
+
}),
|
|
84
|
+
/*#__PURE__*/ (0, $gGrEF$jsxs)((0, $gGrEF$Flex), {
|
|
85
|
+
direction: "column",
|
|
86
|
+
style: {
|
|
87
|
+
height: `100%`
|
|
88
|
+
},
|
|
89
|
+
children: [
|
|
90
|
+
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Card), {
|
|
91
|
+
padding: 2,
|
|
92
|
+
borderBottom: true,
|
|
93
|
+
children: /*#__PURE__*/ (0, $gGrEF$jsxs)((0, $gGrEF$Flex), {
|
|
94
|
+
align: "center",
|
|
95
|
+
gap: 2,
|
|
96
|
+
children: [
|
|
97
|
+
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Flex), {
|
|
98
|
+
align: "center",
|
|
99
|
+
gap: 1,
|
|
100
|
+
children: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Button), {
|
|
101
|
+
fontSize: [
|
|
102
|
+
1
|
|
103
|
+
],
|
|
104
|
+
padding: 2,
|
|
105
|
+
tone: "primary",
|
|
106
|
+
mode: iframeSize === "mobile" ? "default" : "ghost",
|
|
107
|
+
icon: (0, $gGrEF$MobileDeviceIcon),
|
|
108
|
+
onClick: ()=>setIframeSize(iframeSize === "mobile" ? "desktop" : "mobile")
|
|
109
|
+
})
|
|
110
|
+
}),
|
|
111
|
+
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Box), {
|
|
112
|
+
flex: 1,
|
|
113
|
+
children: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Text), {
|
|
114
|
+
size: 0,
|
|
115
|
+
textOverflow: "ellipsis",
|
|
116
|
+
children: displayUrl
|
|
117
|
+
})
|
|
118
|
+
}),
|
|
119
|
+
/*#__PURE__*/ (0, $gGrEF$jsxs)((0, $gGrEF$Flex), {
|
|
120
|
+
align: "center",
|
|
121
|
+
gap: 1,
|
|
122
|
+
children: [
|
|
123
|
+
reload?.button ? /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Button), {
|
|
124
|
+
fontSize: [
|
|
125
|
+
1
|
|
126
|
+
],
|
|
127
|
+
padding: 2,
|
|
128
|
+
icon: (0, $gGrEF$UndoIcon),
|
|
129
|
+
title: "Reload",
|
|
130
|
+
"aria-label": "Reload",
|
|
131
|
+
onClick: ()=>handleReload()
|
|
132
|
+
}) : null,
|
|
133
|
+
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Button), {
|
|
134
|
+
fontSize: [
|
|
135
|
+
1
|
|
136
|
+
],
|
|
137
|
+
icon: (0, $gGrEF$CopyIcon),
|
|
138
|
+
padding: [
|
|
139
|
+
2
|
|
140
|
+
],
|
|
141
|
+
title: "Copy",
|
|
142
|
+
"aria-label": "Copy",
|
|
143
|
+
onClick: ()=>handleCopy()
|
|
144
|
+
}),
|
|
145
|
+
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Button), {
|
|
146
|
+
fontSize: [
|
|
147
|
+
1
|
|
148
|
+
],
|
|
149
|
+
icon: (0, $gGrEF$LeaveIcon),
|
|
150
|
+
padding: [
|
|
151
|
+
2
|
|
152
|
+
],
|
|
153
|
+
text: "Open",
|
|
154
|
+
tone: "primary",
|
|
155
|
+
onClick: ()=>window.open(displayUrl)
|
|
156
|
+
})
|
|
157
|
+
]
|
|
158
|
+
})
|
|
159
|
+
]
|
|
160
|
+
})
|
|
161
|
+
}),
|
|
162
|
+
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Card), {
|
|
163
|
+
tone: "transparent",
|
|
164
|
+
padding: iframeSize === "mobile" ? 2 : 0,
|
|
165
|
+
style: {
|
|
166
|
+
height: `100%`
|
|
167
|
+
},
|
|
168
|
+
children: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Flex), {
|
|
169
|
+
align: "center",
|
|
170
|
+
justify: "center",
|
|
171
|
+
style: {
|
|
172
|
+
height: `100%`
|
|
173
|
+
},
|
|
174
|
+
children: /*#__PURE__*/ (0, $gGrEF$jsx)("iframe", {
|
|
175
|
+
ref: iframe,
|
|
176
|
+
title: "preview",
|
|
177
|
+
style: $b1fbe8f05b2a0ffa$var$sizes[iframeSize],
|
|
178
|
+
frameBorder: "0",
|
|
179
|
+
src: displayUrl
|
|
180
|
+
})
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
]
|
|
184
|
+
})
|
|
185
|
+
]
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
var $b1fbe8f05b2a0ffa$export$2e2bcd8739ae039 = $b1fbe8f05b2a0ffa$var$Iframe;
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
var $df9eabe9bda49ea8$export$2e2bcd8739ae039 = (0, $b1fbe8f05b2a0ffa$export$2e2bcd8739ae039);
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
export {$df9eabe9bda49ea8$export$2e2bcd8739ae039 as default};
|
|
195
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;ACAA;;;;;AAyBA,MAAM,2BAAK,GAAc;IACvB,OAAO,EAAE;QACP,KAAK,EAAE,CAAC,IAAI,CAAC;QACb,MAAM,EAAE,CAAC,IAAI,CAAC;QACd,SAAS,EAAE,CAAC,IAAI,CAAC;KAClB;IACD,MAAM,EAAE;QACN,KAAK,EAAE,GAAG;QACV,MAAM,EAAE,CAAC,IAAI,CAAC;QACd,SAAS,EAAE,GAAG;KACf;CACF,AAAC;AAkBF,MAAM,kCAAY,GAAG,CAAC,OAAO,CAAC,AAAC;AAE/B,SAAS,4BAAM,CAAC,KAAkB,EAAE;IAClC,MAAM,EAAE,QAAQ,EAAE,cAAc,CAAA,WAAE,OAAO,CAAA,EAAE,GAAG,KAAK,AAAC;IACpD,MAAM,OAAE,GAAG,CAAA,eAAE,WAAW,GAAG,kCAAY,WAAE,MAAM,CAAA,EAAE,GAAG,OAAO,AAAC;IAC5D,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,CAAA,GAAA,eAAQ,CAAA,CAC1C,GAAG,IAAI,OAAO,GAA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GAAA,gBAAS,CAAA;4CACf,OAAO,EAAE;AAAC,iDAAC;6CAAC;4CACZ,IAAI,EAAC,MAAM;4CACX,IAAI,EAAC,SAAS;4CACd,OAAO,EAAE,IAAM,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC;0CACtC;;kCACG;;0BACF;sBACF;kCACP,gBAAC,CAAA,GAAA,WAAI,CAAA;wBACH,IAAI,EAAC,aAAa;wBAClB,OAAO,EAAE,UAAU,KAAK,QAAQ,GAAG,CAAC,GAAG,CAAC;wBACxC,KAAK,EAAE;4BAAE,MAAM,EAAE,CAAC,IAAI,CAAC;yBAAE;kCAEzB,cAAA,gBAAC,CAAA,GAAA,WAAI,CAAA;4BAAC,KAAK,EAAC,QAAQ;4BAAC,OAAO,EAAC,QAAQ;4BAAC,KAAK,EAAE;gCAAE,MAAM,EAAE,CAAC,IAAI,CAAC;6BAAE;sCAC7D,cAAA,gBAAC,QAAM;gCACL,GAAG,EAAE,MAAM;gCACX,KAAK,EAAC,SAAS;gCACf,KAAK,EAAE,2BAAK,CAAC,UAAU,CAAC;gCACxB,WAAW,EAAC,GAAG;gCACf,GAAG,EAAE,UAAU;8BACf;0BACG;sBACF;;cACF;;MACO,CAChB;CACH;IAED,wCAAsB,GAAP,4BAAM;;AD3MrB;IAEA,wCAA+B,GAAhB,CAAA,GAAA,wCAAe,CAAA","sources":["src/index.ts","src/Iframe.tsx"],"sourcesContent":["import IframeComponent, { IframeOptions as IframeOptionsType } from \"./Iframe\";\n\nexport default IframeComponent;\n\nexport type IframeOptions = IframeOptionsType;\n","import React, { useEffect, useState, useRef } from \"react\";\nimport { SanityDocumentLike } from \"sanity\";\nimport {\n Box,\n Flex,\n Text,\n Button,\n ThemeProvider,\n Card,\n Spinner,\n} from \"@sanity/ui\";\nimport { UndoIcon, CopyIcon, LeaveIcon, MobileDeviceIcon } from \"@sanity/icons\";\n\nimport { useCopyToClipboard } from \"usehooks-ts\";\n\ntype Size = \"desktop\" | \"mobile\";\n\ntype SizeProps = {\n [key in Size]: {\n width: string | number;\n height: string | number;\n maxHeight: string | number;\n };\n};\n\nconst sizes: SizeProps = {\n desktop: {\n width: `100%`,\n height: `100%`,\n maxHeight: `100%`,\n },\n mobile: {\n width: 414,\n height: `100%`,\n maxHeight: 736,\n },\n};\n\nexport type IframeOptions = {\n url: string | ((document: SanityDocumentLike) => unknown);\n defaultSize?: Size;\n reload?: {\n revision?: boolean;\n button?: boolean;\n };\n};\n\nexport type IframeProps = {\n document: {\n displayed: SanityDocumentLike;\n };\n options: IframeOptions;\n};\n\nconst DEFAULT_SIZE = `desktop`;\n\nfunction Iframe(props: IframeProps) {\n const { document: sanityDocument, options } = props;\n const { url, defaultSize = DEFAULT_SIZE, reload } = options;\n const [displayUrl, setDisplayUrl] = useState(\n url && typeof url === \"string\" ? url : ``\n );\n const [iframeSize, setIframeSize] = useState(\n sizes?.[defaultSize] ? defaultSize : DEFAULT_SIZE\n );\n const input = useRef<HTMLTextAreaElement>(null);\n const iframe = useRef<HTMLIFrameElement>(null);\n const { displayed } = sanityDocument;\n const [, copy] = useCopyToClipboard();\n\n function handleCopy() {\n if (!input?.current?.value) return;\n\n copy(input.current.value);\n }\n\n function handleReload() {\n if (!iframe?.current) {\n return;\n }\n\n // Funky way to reload an iframe without CORS issuies\n // eslint-disable-next-line no-self-assign\n iframe.current.src = iframe.current.src;\n }\n\n // Reload on new revisions\n useEffect(() => {\n if (reload?.revision) {\n handleReload();\n }\n }, [displayed._rev, reload?.revision]);\n\n // Set initial URL and refresh on new revisions\n useEffect(() => {\n const getUrl = async () => {\n const resolveUrl = typeof url === \"function\" ? await url(displayed) : ``;\n\n // Only update state if URL has changed\n if (\n resolveUrl !== displayUrl &&\n resolveUrl &&\n typeof resolveUrl === \"string\"\n ) {\n setDisplayUrl(resolveUrl);\n }\n };\n\n if (typeof url === \"function\") {\n getUrl();\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [displayed._rev]);\n\n if (!displayUrl || typeof displayUrl !== \"string\") {\n return (\n <ThemeProvider>\n <Flex padding={5} align=\"center\" justify=\"center\">\n <Spinner />\n </Flex>\n </ThemeProvider>\n );\n }\n\n return (\n <ThemeProvider>\n <textarea\n style={{ position: `absolute`, pointerEvents: `none`, opacity: 0 }}\n ref={input}\n value={displayUrl}\n readOnly\n tabIndex={-1}\n />\n <Flex direction=\"column\" style={{ height: `100%` }}>\n <Card padding={2} borderBottom>\n <Flex align=\"center\" gap={2}>\n <Flex align=\"center\" gap={1}>\n <Button\n fontSize={[1]}\n padding={2}\n tone=\"primary\"\n mode={iframeSize === \"mobile\" ? \"default\" : \"ghost\"}\n icon={MobileDeviceIcon}\n onClick={() =>\n setIframeSize(iframeSize === \"mobile\" ? \"desktop\" : \"mobile\")\n }\n />\n </Flex>\n <Box flex={1}>\n <Text size={0} textOverflow=\"ellipsis\">\n {displayUrl}\n </Text>\n </Box>\n <Flex align=\"center\" gap={1}>\n {reload?.button ? (\n <Button\n fontSize={[1]}\n padding={2}\n icon={UndoIcon}\n title=\"Reload\"\n aria-label=\"Reload\"\n onClick={() => handleReload()}\n />\n ) : null}\n <Button\n fontSize={[1]}\n icon={CopyIcon}\n padding={[2]}\n title=\"Copy\"\n aria-label=\"Copy\"\n onClick={() => handleCopy()}\n />\n <Button\n fontSize={[1]}\n icon={LeaveIcon}\n padding={[2]}\n text=\"Open\"\n tone=\"primary\"\n onClick={() => window.open(displayUrl)}\n />\n </Flex>\n </Flex>\n </Card>\n <Card\n tone=\"transparent\"\n padding={iframeSize === \"mobile\" ? 2 : 0}\n style={{ height: `100%` }}\n >\n <Flex align=\"center\" justify=\"center\" style={{ height: `100%` }}>\n <iframe\n ref={iframe}\n title=\"preview\"\n style={sizes[iframeSize]}\n frameBorder=\"0\"\n src={displayUrl}\n />\n </Flex>\n </Card>\n </Flex>\n </ThemeProvider>\n );\n}\n\nexport default Iframe;\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../../"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { SanityDocumentLike } from "sanity";
|
|
2
|
+
type Size = "desktop" | "mobile";
|
|
3
|
+
type _IframeOptions1 = {
|
|
4
|
+
url: string | ((document: SanityDocumentLike) => unknown);
|
|
5
|
+
defaultSize?: Size;
|
|
6
|
+
reload?: {
|
|
7
|
+
revision?: boolean;
|
|
8
|
+
button?: boolean;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
type IframeProps = {
|
|
12
|
+
document: {
|
|
13
|
+
displayed: SanityDocumentLike;
|
|
14
|
+
};
|
|
15
|
+
options: _IframeOptions1;
|
|
16
|
+
};
|
|
17
|
+
declare function Iframe(props: IframeProps): JSX.Element;
|
|
18
|
+
export default IframeComponent;
|
|
19
|
+
export type IframeOptions = _IframeOptions1;
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";AAeA,YAAY,SAAS,GAAG,QAAQ,CAAC;AAuBjC,uBAA4B;IAC1B,GAAG,EAAE,MAAM,GAAG,CAAC,CAAC,QAAQ,EAAE,kBAAkB,KAAK,OAAO,CAAC,CAAC;IAC1D,WAAW,CAAC,EAAE,IAAI,CAAC;IACnB,MAAM,CAAC,EAAE;QACP,QAAQ,CAAC,EAAE,OAAO,CAAC;QACnB,MAAM,CAAC,EAAE,OAAO,CAAC;KAClB,CAAC;CACH,CAAC;AAEF,mBAA0B;IACxB,QAAQ,EAAE;QACR,SAAS,EAAE,kBAAkB,CAAC;KAC/B,CAAC;IACF,OAAO,EAAE,eAAa,CAAC;CACxB,CAAC;AAIF,wBAAgB,KAAK,EAAE,WAAW,eAiJjC;ACvMD,eAAe,eAAe,CAAC;AAE/B,4BAA4B,eAAiB,CAAC","sources":["src/src/Iframe.tsx","src/src/index.ts","src/index.ts"],"sourcesContent":[null,null,"import IframeComponent, { IframeOptions as IframeOptionsType } from \"./Iframe\";\n\nexport default IframeComponent;\n\nexport type IframeOptions = IframeOptionsType;\n"],"names":[],"version":3,"file":"index.d.ts.map","sourceRoot":"../../"}
|
package/package.json
CHANGED
|
@@ -1,42 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sanity-plugin-iframe-pane",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Display any URL in a View Pane, along with helpful buttons to Copy the URL or open in a new tab",
|
|
5
|
-
"
|
|
5
|
+
"author": "Simeon Griggs <simeon@sanity.io>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"source": "./src/index.ts",
|
|
8
|
+
"main": "./lib/cjs/index.js",
|
|
9
|
+
"module": "./lib/esm/index.js",
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"require": "./lib/cjs/index.js",
|
|
14
|
+
"default": "./lib/esm/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"src",
|
|
19
|
+
"lib",
|
|
20
|
+
"v2-incompatible.js",
|
|
21
|
+
"sanity.json"
|
|
22
|
+
],
|
|
6
23
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
"watch": "sanipack build --watch",
|
|
9
|
-
"prepublishOnly": "sanipack build && sanipack verify",
|
|
24
|
+
"clean": "rimraf lib",
|
|
10
25
|
"lint": "eslint .",
|
|
11
|
-
"
|
|
26
|
+
"prebuild": "npm run clean && plugin-kit verify-package --silent",
|
|
27
|
+
"build": "parcel build --no-cache",
|
|
28
|
+
"watch": "parcel watch",
|
|
29
|
+
"link-watch": "plugin-kit link-watch",
|
|
30
|
+
"prepublishOnly": "npm run build"
|
|
12
31
|
},
|
|
13
|
-
"repository": {
|
|
14
|
-
|
|
15
|
-
"
|
|
32
|
+
"repository": {},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=14.0.0"
|
|
16
35
|
},
|
|
17
|
-
"keywords": [
|
|
18
|
-
"sanity",
|
|
19
|
-
"sanity-plugin"
|
|
20
|
-
],
|
|
21
|
-
"author": "Simeon Griggs <simeon@sanity.io>",
|
|
22
|
-
"license": "MIT",
|
|
23
36
|
"dependencies": {
|
|
24
|
-
"@sanity/icons": "^1.
|
|
25
|
-
"@sanity/
|
|
37
|
+
"@sanity/icons": "^1.3.1",
|
|
38
|
+
"@sanity/incompatible-plugin": "^0.0.1-studio-v3.1",
|
|
39
|
+
"@sanity/ui": "^0.37.12",
|
|
40
|
+
"usehooks-ts": "^2.6.0"
|
|
26
41
|
},
|
|
27
42
|
"devDependencies": {
|
|
28
|
-
"@
|
|
29
|
-
"
|
|
43
|
+
"@parcel/packager-ts": "^2.6.2",
|
|
44
|
+
"@parcel/transformer-typescript-types": "^2.6.2",
|
|
45
|
+
"@sanity/plugin-kit": "^0.1.0-v3-studio.1",
|
|
46
|
+
"@typescript-eslint/eslint-plugin": "^5.30.7",
|
|
47
|
+
"@typescript-eslint/parser": "^5.30.7",
|
|
48
|
+
"eslint": "^8.20.0",
|
|
30
49
|
"eslint-config-prettier": "^8.5.0",
|
|
31
|
-
"eslint-config-sanity": "6.0.0",
|
|
50
|
+
"eslint-config-sanity": "^6.0.0",
|
|
32
51
|
"eslint-plugin-prettier": "^4.2.1",
|
|
33
52
|
"eslint-plugin-react": "^7.30.1",
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
53
|
+
"eslint-plugin-react-hooks": "^4.6.0",
|
|
54
|
+
"parcel": "^2.6.2",
|
|
55
|
+
"prettier": "^2.7.1",
|
|
56
|
+
"react": "^17.0.0 || ^18.0.0",
|
|
57
|
+
"rimraf": "^3.0.2",
|
|
58
|
+
"sanity": "2.29.5-purple-unicorn.856",
|
|
37
59
|
"typescript": "^4.7.4"
|
|
38
60
|
},
|
|
39
61
|
"peerDependencies": {
|
|
40
|
-
"react": "^17.0.
|
|
62
|
+
"react": "^17.0.0 || ^18.0.0",
|
|
63
|
+
"sanity": "purple-unicorn"
|
|
41
64
|
}
|
|
42
65
|
}
|
package/sanity.json
CHANGED
package/src/Iframe.tsx
CHANGED
|
@@ -1,111 +1,149 @@
|
|
|
1
|
-
import React, {useEffect, useState, useRef} from
|
|
2
|
-
import {SanityDocumentLike} from
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
1
|
+
import React, { useEffect, useState, useRef } from "react";
|
|
2
|
+
import { SanityDocumentLike } from "sanity";
|
|
3
|
+
import {
|
|
4
|
+
Box,
|
|
5
|
+
Flex,
|
|
6
|
+
Text,
|
|
7
|
+
Button,
|
|
8
|
+
ThemeProvider,
|
|
9
|
+
Card,
|
|
10
|
+
Spinner,
|
|
11
|
+
} from "@sanity/ui";
|
|
12
|
+
import { UndoIcon, CopyIcon, LeaveIcon, MobileDeviceIcon } from "@sanity/icons";
|
|
13
|
+
|
|
14
|
+
import { useCopyToClipboard } from "usehooks-ts";
|
|
15
|
+
|
|
16
|
+
type Size = "desktop" | "mobile";
|
|
17
|
+
|
|
18
|
+
type SizeProps = {
|
|
19
|
+
[key in Size]: {
|
|
20
|
+
width: string | number;
|
|
21
|
+
height: string | number;
|
|
22
|
+
maxHeight: string | number;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const sizes: SizeProps = {
|
|
27
|
+
desktop: {
|
|
28
|
+
width: `100%`,
|
|
29
|
+
height: `100%`,
|
|
30
|
+
maxHeight: `100%`,
|
|
31
|
+
},
|
|
32
|
+
mobile: {
|
|
33
|
+
width: 414,
|
|
34
|
+
height: `100%`,
|
|
35
|
+
maxHeight: 736,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
12
38
|
|
|
13
39
|
export type IframeOptions = {
|
|
14
|
-
url: string | ((document: SanityDocumentLike) => unknown)
|
|
15
|
-
defaultSize?:
|
|
16
|
-
reload
|
|
17
|
-
revision
|
|
18
|
-
button
|
|
19
|
-
}
|
|
20
|
-
}
|
|
40
|
+
url: string | ((document: SanityDocumentLike) => unknown);
|
|
41
|
+
defaultSize?: Size;
|
|
42
|
+
reload?: {
|
|
43
|
+
revision?: boolean;
|
|
44
|
+
button?: boolean;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
21
47
|
|
|
22
48
|
export type IframeProps = {
|
|
23
49
|
document: {
|
|
24
|
-
displayed: SanityDocumentLike
|
|
25
|
-
}
|
|
26
|
-
options: IframeOptions
|
|
27
|
-
}
|
|
50
|
+
displayed: SanityDocumentLike;
|
|
51
|
+
};
|
|
52
|
+
options: IframeOptions;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const DEFAULT_SIZE = `desktop`;
|
|
28
56
|
|
|
29
57
|
function Iframe(props: IframeProps) {
|
|
30
|
-
const {document: sanityDocument, options} = props
|
|
31
|
-
const {url, defaultSize =
|
|
32
|
-
const [displayUrl, setDisplayUrl] = useState(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
58
|
+
const { document: sanityDocument, options } = props;
|
|
59
|
+
const { url, defaultSize = DEFAULT_SIZE, reload } = options;
|
|
60
|
+
const [displayUrl, setDisplayUrl] = useState(
|
|
61
|
+
url && typeof url === "string" ? url : ``
|
|
62
|
+
);
|
|
63
|
+
const [iframeSize, setIframeSize] = useState(
|
|
64
|
+
sizes?.[defaultSize] ? defaultSize : DEFAULT_SIZE
|
|
65
|
+
);
|
|
66
|
+
const input = useRef<HTMLTextAreaElement>(null);
|
|
67
|
+
const iframe = useRef<HTMLIFrameElement>(null);
|
|
68
|
+
const { displayed } = sanityDocument;
|
|
69
|
+
const [, copy] = useCopyToClipboard();
|
|
38
70
|
|
|
39
71
|
function handleCopy() {
|
|
40
|
-
if (!input?.current?.value) return
|
|
72
|
+
if (!input?.current?.value) return;
|
|
41
73
|
|
|
42
|
-
copy(input.current.value)
|
|
74
|
+
copy(input.current.value);
|
|
43
75
|
}
|
|
44
76
|
|
|
45
77
|
function handleReload() {
|
|
46
78
|
if (!iframe?.current) {
|
|
47
|
-
return
|
|
79
|
+
return;
|
|
48
80
|
}
|
|
49
81
|
|
|
50
82
|
// Funky way to reload an iframe without CORS issuies
|
|
51
83
|
// eslint-disable-next-line no-self-assign
|
|
52
|
-
iframe.current.src = iframe.current.src
|
|
84
|
+
iframe.current.src = iframe.current.src;
|
|
53
85
|
}
|
|
54
86
|
|
|
55
87
|
// Reload on new revisions
|
|
56
88
|
useEffect(() => {
|
|
57
89
|
if (reload?.revision) {
|
|
58
|
-
handleReload()
|
|
90
|
+
handleReload();
|
|
59
91
|
}
|
|
60
|
-
}, [displayed._rev, reload?.revision])
|
|
92
|
+
}, [displayed._rev, reload?.revision]);
|
|
61
93
|
|
|
62
94
|
// Set initial URL and refresh on new revisions
|
|
63
95
|
useEffect(() => {
|
|
64
96
|
const getUrl = async () => {
|
|
65
|
-
const resolveUrl = typeof url ===
|
|
97
|
+
const resolveUrl = typeof url === "function" ? await url(displayed) : ``;
|
|
66
98
|
|
|
67
99
|
// Only update state if URL has changed
|
|
68
|
-
if (
|
|
69
|
-
|
|
100
|
+
if (
|
|
101
|
+
resolveUrl !== displayUrl &&
|
|
102
|
+
resolveUrl &&
|
|
103
|
+
typeof resolveUrl === "string"
|
|
104
|
+
) {
|
|
105
|
+
setDisplayUrl(resolveUrl);
|
|
70
106
|
}
|
|
71
|
-
}
|
|
107
|
+
};
|
|
72
108
|
|
|
73
|
-
if (typeof url ===
|
|
74
|
-
getUrl()
|
|
109
|
+
if (typeof url === "function") {
|
|
110
|
+
getUrl();
|
|
75
111
|
}
|
|
76
112
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
77
|
-
}, [displayed._rev])
|
|
113
|
+
}, [displayed._rev]);
|
|
78
114
|
|
|
79
|
-
if (!displayUrl || typeof displayUrl !==
|
|
115
|
+
if (!displayUrl || typeof displayUrl !== "string") {
|
|
80
116
|
return (
|
|
81
117
|
<ThemeProvider>
|
|
82
|
-
<Flex padding={5}
|
|
118
|
+
<Flex padding={5} align="center" justify="center">
|
|
83
119
|
<Spinner />
|
|
84
120
|
</Flex>
|
|
85
121
|
</ThemeProvider>
|
|
86
|
-
)
|
|
122
|
+
);
|
|
87
123
|
}
|
|
88
124
|
|
|
89
125
|
return (
|
|
90
126
|
<ThemeProvider>
|
|
91
127
|
<textarea
|
|
92
|
-
style={{position: `absolute`, pointerEvents: `none`, opacity: 0}}
|
|
128
|
+
style={{ position: `absolute`, pointerEvents: `none`, opacity: 0 }}
|
|
93
129
|
ref={input}
|
|
94
130
|
value={displayUrl}
|
|
95
131
|
readOnly
|
|
96
132
|
tabIndex={-1}
|
|
97
133
|
/>
|
|
98
|
-
<Flex direction="column" style={{height: `100%`}}>
|
|
99
|
-
<Card padding={2} borderBottom
|
|
134
|
+
<Flex direction="column" style={{ height: `100%` }}>
|
|
135
|
+
<Card padding={2} borderBottom>
|
|
100
136
|
<Flex align="center" gap={2}>
|
|
101
137
|
<Flex align="center" gap={1}>
|
|
102
138
|
<Button
|
|
103
139
|
fontSize={[1]}
|
|
104
140
|
padding={2}
|
|
105
141
|
tone="primary"
|
|
106
|
-
mode={iframeSize ===
|
|
142
|
+
mode={iframeSize === "mobile" ? "default" : "ghost"}
|
|
107
143
|
icon={MobileDeviceIcon}
|
|
108
|
-
onClick={() =>
|
|
144
|
+
onClick={() =>
|
|
145
|
+
setIframeSize(iframeSize === "mobile" ? "desktop" : "mobile")
|
|
146
|
+
}
|
|
109
147
|
/>
|
|
110
148
|
</Flex>
|
|
111
149
|
<Box flex={1}>
|
|
@@ -143,8 +181,12 @@ function Iframe(props: IframeProps) {
|
|
|
143
181
|
</Flex>
|
|
144
182
|
</Flex>
|
|
145
183
|
</Card>
|
|
146
|
-
<Card
|
|
147
|
-
|
|
184
|
+
<Card
|
|
185
|
+
tone="transparent"
|
|
186
|
+
padding={iframeSize === "mobile" ? 2 : 0}
|
|
187
|
+
style={{ height: `100%` }}
|
|
188
|
+
>
|
|
189
|
+
<Flex align="center" justify="center" style={{ height: `100%` }}>
|
|
148
190
|
<iframe
|
|
149
191
|
ref={iframe}
|
|
150
192
|
title="preview"
|
|
@@ -156,7 +198,7 @@ function Iframe(props: IframeProps) {
|
|
|
156
198
|
</Card>
|
|
157
199
|
</Flex>
|
|
158
200
|
</ThemeProvider>
|
|
159
|
-
)
|
|
201
|
+
);
|
|
160
202
|
}
|
|
161
203
|
|
|
162
|
-
export default Iframe
|
|
204
|
+
export default Iframe;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const {showIncompatiblePluginDialog} = require('@sanity/incompatible-plugin')
|
|
2
|
+
const {name, version, sanityExchangeUrl} = require('./package.json')
|
|
3
|
+
|
|
4
|
+
export default showIncompatiblePluginDialog({
|
|
5
|
+
name: name,
|
|
6
|
+
versions: {
|
|
7
|
+
v3: version,
|
|
8
|
+
v2: undefined,
|
|
9
|
+
},
|
|
10
|
+
sanityExchangeUrl,
|
|
11
|
+
})
|
package/.babelrc
DELETED
package/lib/Iframe.js
DELETED
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _react = _interopRequireWildcard(require("react"));
|
|
9
|
-
|
|
10
|
-
var _ui = require("@sanity/ui");
|
|
11
|
-
|
|
12
|
-
var _icons = require("@sanity/icons");
|
|
13
|
-
|
|
14
|
-
var _useCopytoClipboard = _interopRequireDefault(require("./hooks/useCopytoClipboard"));
|
|
15
|
-
|
|
16
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
-
|
|
18
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
19
|
-
|
|
20
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
21
|
-
|
|
22
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
23
|
-
|
|
24
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
25
|
-
|
|
26
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
27
|
-
|
|
28
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
29
|
-
|
|
30
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
31
|
-
|
|
32
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
33
|
-
|
|
34
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
35
|
-
|
|
36
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
37
|
-
|
|
38
|
-
var sizes = {
|
|
39
|
-
desktop: {
|
|
40
|
-
backgroundColor: "white",
|
|
41
|
-
width: "100%",
|
|
42
|
-
height: "100%",
|
|
43
|
-
maxHeight: "100%"
|
|
44
|
-
},
|
|
45
|
-
mobile: {
|
|
46
|
-
backgroundColor: "white",
|
|
47
|
-
width: 414,
|
|
48
|
-
height: "100%",
|
|
49
|
-
maxHeight: 736
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
function Iframe(props) {
|
|
54
|
-
var sanityDocument = props.document,
|
|
55
|
-
options = props.options;
|
|
56
|
-
var url = options.url,
|
|
57
|
-
_options$defaultSize = options.defaultSize,
|
|
58
|
-
defaultSize = _options$defaultSize === void 0 ? "desktop" : _options$defaultSize,
|
|
59
|
-
reload = options.reload;
|
|
60
|
-
|
|
61
|
-
var _useState = (0, _react.useState)(typeof url === 'string' ? url : ""),
|
|
62
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
63
|
-
displayUrl = _useState2[0],
|
|
64
|
-
setDisplayUrl = _useState2[1];
|
|
65
|
-
|
|
66
|
-
var _useState3 = (0, _react.useState)(sizes === null || sizes === void 0 ? void 0 : sizes[defaultSize]),
|
|
67
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
68
|
-
iframeSize = _useState4[0],
|
|
69
|
-
setIframeSize = _useState4[1];
|
|
70
|
-
|
|
71
|
-
var input = (0, _react.useRef)();
|
|
72
|
-
var iframe = (0, _react.useRef)();
|
|
73
|
-
var displayed = sanityDocument.displayed;
|
|
74
|
-
|
|
75
|
-
var _useCopyToClipboard = (0, _useCopytoClipboard.default)(),
|
|
76
|
-
_useCopyToClipboard2 = _slicedToArray(_useCopyToClipboard, 2),
|
|
77
|
-
copy = _useCopyToClipboard2[1];
|
|
78
|
-
|
|
79
|
-
function handleCopy() {
|
|
80
|
-
var _input$current;
|
|
81
|
-
|
|
82
|
-
if (!(input !== null && input !== void 0 && (_input$current = input.current) !== null && _input$current !== void 0 && _input$current.value)) return;
|
|
83
|
-
copy(input.current.value);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function handleReload() {
|
|
87
|
-
if (!(iframe !== null && iframe !== void 0 && iframe.current)) {
|
|
88
|
-
return;
|
|
89
|
-
} // Funky way to reload an iframe without CORS issuies
|
|
90
|
-
// eslint-disable-next-line no-self-assign
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
iframe.current.src = iframe.current.src;
|
|
94
|
-
} // Reload on new revisions
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
(0, _react.useEffect)(() => {
|
|
98
|
-
if (reload !== null && reload !== void 0 && reload.revision) {
|
|
99
|
-
handleReload();
|
|
100
|
-
}
|
|
101
|
-
}, [displayed._rev, reload === null || reload === void 0 ? void 0 : reload.revision]); // Set initial URL and refresh on new revisions
|
|
102
|
-
|
|
103
|
-
(0, _react.useEffect)(() => {
|
|
104
|
-
var getUrl = /*#__PURE__*/function () {
|
|
105
|
-
var _ref = _asyncToGenerator(function* () {
|
|
106
|
-
var resolveUrl = typeof url === 'function' ? yield url(displayed) : ""; // Only update state if URL has changed
|
|
107
|
-
|
|
108
|
-
if (resolveUrl !== displayUrl) {
|
|
109
|
-
setDisplayUrl(resolveUrl);
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
return function getUrl() {
|
|
114
|
-
return _ref.apply(this, arguments);
|
|
115
|
-
};
|
|
116
|
-
}();
|
|
117
|
-
|
|
118
|
-
if (typeof url === 'function') {
|
|
119
|
-
getUrl();
|
|
120
|
-
} // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
121
|
-
|
|
122
|
-
}, [displayed._rev]);
|
|
123
|
-
|
|
124
|
-
if (!displayUrl || typeof displayUrl !== 'string') {
|
|
125
|
-
return /*#__PURE__*/_react.default.createElement(_ui.ThemeProvider, null, /*#__PURE__*/_react.default.createElement(_ui.Flex, {
|
|
126
|
-
padding: 5,
|
|
127
|
-
items: "center",
|
|
128
|
-
justify: "center"
|
|
129
|
-
}, /*#__PURE__*/_react.default.createElement(_ui.Spinner, null)));
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return /*#__PURE__*/_react.default.createElement(_ui.ThemeProvider, null, /*#__PURE__*/_react.default.createElement("textarea", {
|
|
133
|
-
style: {
|
|
134
|
-
position: "absolute",
|
|
135
|
-
pointerEvents: "none",
|
|
136
|
-
opacity: 0
|
|
137
|
-
},
|
|
138
|
-
ref: input,
|
|
139
|
-
value: displayUrl,
|
|
140
|
-
readOnly: true,
|
|
141
|
-
tabIndex: -1
|
|
142
|
-
}), /*#__PURE__*/_react.default.createElement(_ui.Flex, {
|
|
143
|
-
direction: "column",
|
|
144
|
-
style: {
|
|
145
|
-
height: "100%"
|
|
146
|
-
}
|
|
147
|
-
}, /*#__PURE__*/_react.default.createElement(_ui.Card, {
|
|
148
|
-
padding: 2,
|
|
149
|
-
borderBottom: 1
|
|
150
|
-
}, /*#__PURE__*/_react.default.createElement(_ui.Flex, {
|
|
151
|
-
align: "center",
|
|
152
|
-
gap: 2
|
|
153
|
-
}, /*#__PURE__*/_react.default.createElement(_ui.Flex, {
|
|
154
|
-
align: "center",
|
|
155
|
-
gap: 1
|
|
156
|
-
}, /*#__PURE__*/_react.default.createElement(_ui.Button, {
|
|
157
|
-
fontSize: [1],
|
|
158
|
-
padding: 2,
|
|
159
|
-
tone: "primary",
|
|
160
|
-
mode: iframeSize === 'mobile' ? 'default' : 'ghost',
|
|
161
|
-
icon: _icons.MobileDeviceIcon,
|
|
162
|
-
onClick: () => setIframeSize(iframeSize === 'mobile' ? 'desktop' : 'mobile')
|
|
163
|
-
})), /*#__PURE__*/_react.default.createElement(_ui.Box, {
|
|
164
|
-
flex: 1
|
|
165
|
-
}, /*#__PURE__*/_react.default.createElement(_ui.Text, {
|
|
166
|
-
size: 0,
|
|
167
|
-
textOverflow: "ellipsis"
|
|
168
|
-
}, displayUrl)), /*#__PURE__*/_react.default.createElement(_ui.Flex, {
|
|
169
|
-
align: "center",
|
|
170
|
-
gap: 1
|
|
171
|
-
}, reload !== null && reload !== void 0 && reload.button ? /*#__PURE__*/_react.default.createElement(_ui.Button, {
|
|
172
|
-
fontSize: [1],
|
|
173
|
-
padding: 2,
|
|
174
|
-
icon: _icons.UndoIcon,
|
|
175
|
-
title: "Reload",
|
|
176
|
-
"aria-label": "Reload",
|
|
177
|
-
onClick: () => handleReload()
|
|
178
|
-
}) : null, /*#__PURE__*/_react.default.createElement(_ui.Button, {
|
|
179
|
-
fontSize: [1],
|
|
180
|
-
icon: _icons.CopyIcon,
|
|
181
|
-
padding: [2],
|
|
182
|
-
title: "Copy",
|
|
183
|
-
"aria-label": "Copy",
|
|
184
|
-
onClick: () => handleCopy()
|
|
185
|
-
}), /*#__PURE__*/_react.default.createElement(_ui.Button, {
|
|
186
|
-
fontSize: [1],
|
|
187
|
-
icon: _icons.LeaveIcon,
|
|
188
|
-
padding: [2],
|
|
189
|
-
text: "Open",
|
|
190
|
-
tone: "primary",
|
|
191
|
-
onClick: () => window.open(displayUrl)
|
|
192
|
-
})))), /*#__PURE__*/_react.default.createElement(_ui.Card, {
|
|
193
|
-
tone: "transparent",
|
|
194
|
-
padding: iframeSize === 'mobile' ? 2 : 0,
|
|
195
|
-
style: {
|
|
196
|
-
height: "100%"
|
|
197
|
-
}
|
|
198
|
-
}, /*#__PURE__*/_react.default.createElement(_ui.Flex, {
|
|
199
|
-
align: "center",
|
|
200
|
-
justify: "center",
|
|
201
|
-
style: {
|
|
202
|
-
height: "100%"
|
|
203
|
-
}
|
|
204
|
-
}, /*#__PURE__*/_react.default.createElement("iframe", {
|
|
205
|
-
ref: iframe,
|
|
206
|
-
title: "preview",
|
|
207
|
-
style: sizes[iframeSize],
|
|
208
|
-
frameBorder: "0",
|
|
209
|
-
src: displayUrl
|
|
210
|
-
})))));
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
var _default = Iframe;
|
|
214
|
-
exports.default = _default;
|
|
215
|
-
//# sourceMappingURL=Iframe.js.map
|
package/lib/Iframe.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Iframe.js","names":["sizes","desktop","backgroundColor","width","height","maxHeight","mobile","Iframe","props","sanityDocument","document","options","url","defaultSize","reload","useState","displayUrl","setDisplayUrl","iframeSize","setIframeSize","input","useRef","iframe","displayed","useCopyToClipboard","copy","handleCopy","current","value","handleReload","src","useEffect","revision","_rev","getUrl","resolveUrl","position","pointerEvents","opacity","MobileDeviceIcon","button","UndoIcon","CopyIcon","LeaveIcon","window","open"],"sources":["../src/Iframe.tsx"],"sourcesContent":["import React, {useEffect, useState, useRef} from 'react'\nimport {SanityDocumentLike} from 'sanity'\nimport {Box, Flex, Text, Button, ThemeProvider, Card, Spinner} from '@sanity/ui'\nimport {UndoIcon, CopyIcon, LeaveIcon, MobileDeviceIcon} from '@sanity/icons'\n\nimport useCopyToClipboard from './hooks/useCopytoClipboard'\n\nconst sizes = {\n desktop: {backgroundColor: `white`, width: `100%`, height: `100%`, maxHeight: `100%`},\n mobile: {backgroundColor: `white`, width: 414, height: `100%`, maxHeight: 736},\n}\n\nexport type IframeOptions = {\n url: string | ((document: SanityDocumentLike) => unknown)\n defaultSize?: 'desktop' | 'mobile'\n reload: {\n revision: boolean\n button: boolean\n }\n}\n\nexport type IframeProps = {\n document: {\n displayed: SanityDocumentLike\n }\n options: IframeOptions\n}\n\nfunction Iframe(props: IframeProps) {\n const {document: sanityDocument, options} = props\n const {url, defaultSize = `desktop`, reload} = options\n const [displayUrl, setDisplayUrl] = useState(typeof url === 'string' ? url : ``)\n const [iframeSize, setIframeSize] = useState(sizes?.[defaultSize])\n const input = useRef()\n const iframe = useRef()\n const {displayed} = sanityDocument\n const [, copy] = useCopyToClipboard()\n\n function handleCopy() {\n if (!input?.current?.value) return\n\n copy(input.current.value)\n }\n\n function handleReload() {\n if (!iframe?.current) {\n return\n }\n\n // Funky way to reload an iframe without CORS issuies\n // eslint-disable-next-line no-self-assign\n iframe.current.src = iframe.current.src\n }\n\n // Reload on new revisions\n useEffect(() => {\n if (reload?.revision) {\n handleReload()\n }\n }, [displayed._rev, reload?.revision])\n\n // Set initial URL and refresh on new revisions\n useEffect(() => {\n const getUrl = async () => {\n const resolveUrl = typeof url === 'function' ? await url(displayed) : ``\n\n // Only update state if URL has changed\n if (resolveUrl !== displayUrl) {\n setDisplayUrl(resolveUrl)\n }\n }\n\n if (typeof url === 'function') {\n getUrl()\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [displayed._rev])\n\n if (!displayUrl || typeof displayUrl !== 'string') {\n return (\n <ThemeProvider>\n <Flex padding={5} items=\"center\" justify=\"center\">\n <Spinner />\n </Flex>\n </ThemeProvider>\n )\n }\n\n return (\n <ThemeProvider>\n <textarea\n style={{position: `absolute`, pointerEvents: `none`, opacity: 0}}\n ref={input}\n value={displayUrl}\n readOnly\n tabIndex={-1}\n />\n <Flex direction=\"column\" style={{height: `100%`}}>\n <Card padding={2} borderBottom={1}>\n <Flex align=\"center\" gap={2}>\n <Flex align=\"center\" gap={1}>\n <Button\n fontSize={[1]}\n padding={2}\n tone=\"primary\"\n mode={iframeSize === 'mobile' ? 'default' : 'ghost'}\n icon={MobileDeviceIcon}\n onClick={() => setIframeSize(iframeSize === 'mobile' ? 'desktop' : 'mobile')}\n />\n </Flex>\n <Box flex={1}>\n <Text size={0} textOverflow=\"ellipsis\">\n {displayUrl}\n </Text>\n </Box>\n <Flex align=\"center\" gap={1}>\n {reload?.button ? (\n <Button\n fontSize={[1]}\n padding={2}\n icon={UndoIcon}\n title=\"Reload\"\n aria-label=\"Reload\"\n onClick={() => handleReload()}\n />\n ) : null}\n <Button\n fontSize={[1]}\n icon={CopyIcon}\n padding={[2]}\n title=\"Copy\"\n aria-label=\"Copy\"\n onClick={() => handleCopy()}\n />\n <Button\n fontSize={[1]}\n icon={LeaveIcon}\n padding={[2]}\n text=\"Open\"\n tone=\"primary\"\n onClick={() => window.open(displayUrl)}\n />\n </Flex>\n </Flex>\n </Card>\n <Card tone=\"transparent\" padding={iframeSize === 'mobile' ? 2 : 0} style={{height: `100%`}}>\n <Flex align=\"center\" justify=\"center\" style={{height: `100%`}}>\n <iframe\n ref={iframe}\n title=\"preview\"\n style={sizes[iframeSize]}\n frameBorder=\"0\"\n src={displayUrl}\n />\n </Flex>\n </Card>\n </Flex>\n </ThemeProvider>\n )\n}\n\nexport default Iframe\n"],"mappings":";;;;;;;AAAA;;AAEA;;AACA;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;AAEA,IAAMA,KAAK,GAAG;EACZC,OAAO,EAAE;IAACC,eAAe,SAAhB;IAA2BC,KAAK,QAAhC;IAA0CC,MAAM,QAAhD;IAA0DC,SAAS;EAAnE,CADG;EAEZC,MAAM,EAAE;IAACJ,eAAe,SAAhB;IAA2BC,KAAK,EAAE,GAAlC;IAAuCC,MAAM,QAA7C;IAAuDC,SAAS,EAAE;EAAlE;AAFI,CAAd;;AAqBA,SAASE,MAAT,CAAg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AC,CAAD,CAHX;IAIE,IAAI,EAAC,MAJP;IAKE,IAAI,EAAC,SALP;IAME,OAAO,EAAE,MAAMC,MAAM,CAACC,IAAP,CAAY7B,UAAZ;EANjB,EAnBF,CAhBF,CADF,CADF,eAgDE,6BAAC,QAAD;IAAM,IAAI,EAAC,aAAX;IAAyB,OAAO,EAAEE,UAAU,KAAK,QAAf,GAA0B,CAA1B,GAA8B,CAAhE;IAAmE,KAAK,EAAE;MAACd,MAAM;IAAP;EAA1E,gBACE,6BAAC,QAAD;IAAM,KAAK,EAAC,QAAZ;IAAqB,OAAO,EAAC,QAA7B;IAAsC,KAAK,EAAE;MAACA,MAAM;IAAP;EAA7C,gBACE;IACE,GAAG,EAAEkB,MADP;IAEE,KAAK,EAAC,SAFR;IAGE,KAAK,EAAEtB,KAAK,CAACkB,UAAD,CAHd;IAIE,WAAW,EAAC,GAJd;IAKE,GAAG,EAAEF;EALP,EADF,CADF,CAhDF,CARF,CADF;AAuED;;eAEcT,M"}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _react = require("react");
|
|
9
|
-
|
|
10
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
11
|
-
|
|
12
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
13
|
-
|
|
14
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
15
|
-
|
|
16
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
17
|
-
|
|
18
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
19
|
-
|
|
20
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
21
|
-
|
|
22
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
23
|
-
|
|
24
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
25
|
-
|
|
26
|
-
// Return success
|
|
27
|
-
function useCopyToClipboard() {
|
|
28
|
-
var _useState = (0, _react.useState)(null),
|
|
29
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
30
|
-
copiedText = _useState2[0],
|
|
31
|
-
setCopiedText = _useState2[1];
|
|
32
|
-
|
|
33
|
-
var copy = /*#__PURE__*/function () {
|
|
34
|
-
var _ref = _asyncToGenerator(function* (text) {
|
|
35
|
-
var _navigator;
|
|
36
|
-
|
|
37
|
-
if (!((_navigator = navigator) !== null && _navigator !== void 0 && _navigator.clipboard)) {
|
|
38
|
-
console.warn('Clipboard not supported');
|
|
39
|
-
return false;
|
|
40
|
-
} // Try to save to clipboard then save it in the state if worked
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
try {
|
|
44
|
-
yield navigator.clipboard.writeText(text);
|
|
45
|
-
setCopiedText(text);
|
|
46
|
-
return true;
|
|
47
|
-
} catch (error) {
|
|
48
|
-
console.warn('Copy failed', error);
|
|
49
|
-
setCopiedText(null);
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
return function copy(_x) {
|
|
55
|
-
return _ref.apply(this, arguments);
|
|
56
|
-
};
|
|
57
|
-
}();
|
|
58
|
-
|
|
59
|
-
return [copiedText, copy];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
var _default = useCopyToClipboard;
|
|
63
|
-
exports.default = _default;
|
|
64
|
-
//# sourceMappingURL=useCopytoClipboard.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useCopytoClipboard.js","names":["useCopyToClipboard","useState","copiedText","setCopiedText","copy","text","navigator","clipboard","console","warn","writeText","error"],"sources":["../../src/hooks/useCopytoClipboard.ts"],"sourcesContent":["import {useState} from 'react'\n\ntype CopiedValue = string | null\ntype CopyFn = (text: string) => Promise<boolean> // Return success\n\nfunction useCopyToClipboard(): [CopiedValue, CopyFn] {\n const [copiedText, setCopiedText] = useState<CopiedValue>(null)\n\n const copy: CopyFn = async (text) => {\n if (!navigator?.clipboard) {\n console.warn('Clipboard not supported')\n return false\n }\n\n // Try to save to clipboard then save it in the state if worked\n try {\n await navigator.clipboard.writeText(text)\n setCopiedText(text)\n return true\n } catch (error) {\n console.warn('Copy failed', error)\n setCopiedText(null)\n return false\n }\n }\n\n return [copiedText, copy]\n}\n\nexport default useCopyToClipboard\n"],"mappings":";;;;;;;AAAA;;;;;;;;;;;;;;;;;;AAGiD;AAEjD,SAASA,kBAAT,GAAqD;EACnD,gBAAoC,IAAAC,eAAA,EAAsB,IAAtB,CAApC;EAAA;EAAA,IAAOC,UAAP;EAAA,IAAmBC,aAAnB;;EAEA,IAAMC,IAAY;IAAA,6BAAG,WAAOC,IAAP,EAAgB;MAAA;;MACnC,IAAI,gBAACC,SAAD,uCAAC,WAAWC,SAAZ,CAAJ,EAA2B;QACzBC,OAAO,CAACC,IAAR,CAAa,yBAAb;QACA,OAAO,KAAP;MACD,CAJkC,CAMnC;;;MACA,IAAI;QACF,MAAMH,SAAS,CAACC,SAAV,CAAoBG,SAApB,CAA8BL,IAA9B,CAAN;QACAF,aAAa,CAACE,IAAD,CAAb;QACA,OAAO,IAAP;MACD,CAJD,CAIE,OAAOM,KAAP,EAAc;QACdH,OAAO,CAACC,IAAR,CAAa,aAAb,EAA4BE,KAA5B;QACAR,aAAa,CAAC,IAAD,CAAb;QACA,OAAO,KAAP;MACD;IACF,CAhBiB;;IAAA,gBAAZC,IAAY;MAAA;IAAA;EAAA,GAAlB;;EAkBA,OAAO,CAACF,UAAD,EAAaE,IAAb,CAAP;AACD;;eAEcJ,kB"}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import {useState} from 'react'
|
|
2
|
-
|
|
3
|
-
type CopiedValue = string | null
|
|
4
|
-
type CopyFn = (text: string) => Promise<boolean> // Return success
|
|
5
|
-
|
|
6
|
-
function useCopyToClipboard(): [CopiedValue, CopyFn] {
|
|
7
|
-
const [copiedText, setCopiedText] = useState<CopiedValue>(null)
|
|
8
|
-
|
|
9
|
-
const copy: CopyFn = async (text) => {
|
|
10
|
-
if (!navigator?.clipboard) {
|
|
11
|
-
console.warn('Clipboard not supported')
|
|
12
|
-
return false
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Try to save to clipboard then save it in the state if worked
|
|
16
|
-
try {
|
|
17
|
-
await navigator.clipboard.writeText(text)
|
|
18
|
-
setCopiedText(text)
|
|
19
|
-
return true
|
|
20
|
-
} catch (error) {
|
|
21
|
-
console.warn('Copy failed', error)
|
|
22
|
-
setCopiedText(null)
|
|
23
|
-
return false
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return [copiedText, copy]
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export default useCopyToClipboard
|