next-sanity
Version:
Sanity.io toolkit for Next.js
106 lines (105 loc) • 4.59 kB
JavaScript
;
var jsxRuntime = require("react/jsx-runtime"), react$1 = require("@sanity/visual-editing/react"), navigation_js = require("next/navigation.js"), serverActions = require("next-sanity/visual-editing/server-actions"), react = require("react");
function pathHasPrefix(path, prefix) {
if (typeof path != "string")
return !1;
const { pathname } = parsePath(path);
return pathname === prefix || pathname.startsWith(`${prefix}/`);
}
function parsePath(path) {
const hashIndex = path.indexOf("#"), queryIndex = path.indexOf("?"), hasQuery = queryIndex > -1 && (hashIndex < 0 || queryIndex < hashIndex);
return hasQuery || hashIndex > -1 ? {
pathname: path.substring(0, hasQuery ? queryIndex : hashIndex),
query: hasQuery ? path.substring(queryIndex, hashIndex > -1 ? hashIndex : void 0) : "",
hash: hashIndex > -1 ? path.slice(hashIndex) : ""
} : { pathname: path, query: "", hash: "" };
}
function addPathPrefix(path, prefix) {
if (!path.startsWith("/") || !prefix)
return path;
if (path === "/" && prefix)
return prefix;
const { pathname, query, hash } = parsePath(path);
return `${prefix}${pathname}${query}${hash}`;
}
function removePathPrefix(path, prefix) {
if (!pathHasPrefix(path, prefix))
return path;
const withoutPrefix = path.slice(prefix.length);
return withoutPrefix.startsWith("/") ? withoutPrefix : `/${withoutPrefix}`;
}
const normalizePathTrailingSlash = (path, trailingSlash) => {
const { pathname, query, hash } = parsePath(path);
return trailingSlash ? pathname.endsWith("/") ? `${pathname}${query}${hash}` : `${pathname}/${query}${hash}` : `${removeTrailingSlash(pathname)}${query}${hash}`;
};
function removeTrailingSlash(route) {
return route.replace(/\/$/, "") || "/";
}
function VisualEditing(props) {
const { basePath = "", components, refresh, trailingSlash = !1, zIndex } = props, router = navigation_js.useRouter(), routerRef = react.useRef(router), [navigate, setNavigate] = react.useState();
react.useEffect(() => {
routerRef.current = router;
}, [router]);
const history = react.useMemo(
() => ({
subscribe: (_navigate) => (setNavigate(() => _navigate), () => setNavigate(void 0)),
update: (update) => {
switch (update.type) {
case "push":
return routerRef.current.push(removePathPrefix(update.url, basePath));
case "pop":
return routerRef.current.back();
case "replace":
return routerRef.current.replace(removePathPrefix(update.url, basePath));
default:
throw new Error(`Unknown update type: ${update.type}`);
}
}
}),
[basePath]
), pathname = navigation_js.usePathname(), searchParams = navigation_js.useSearchParams();
react.useEffect(() => {
navigate && navigate({
type: "push",
url: normalizePathTrailingSlash(
addPathPrefix(`${pathname}${searchParams?.size ? `?${searchParams}` : ""}`, basePath),
trailingSlash
)
});
}, [basePath, navigate, pathname, searchParams, trailingSlash]);
const handleRefresh = react.useCallback(
(payload) => {
if (refresh) return refresh(payload);
const manualFastRefresh = () => (console.debug(
"Live preview is setup, calling router.refresh() to refresh the server components without refetching cached data"
), routerRef.current.refresh(), Promise.resolve()), manualFallbackRefresh = () => (console.debug(
"No loaders in live mode detected, or preview kit setup, revalidating root layout"
), serverActions.revalidateRootLayout()), mutationFastRefresh = () => (console.debug(
"Live preview is setup, mutation is skipped assuming its handled by the live preview"
), !1), mutationFallbackRefresh = () => (console.debug(
"No loaders in live mode detected, or preview kit setup, revalidating root layout"
), serverActions.revalidateRootLayout());
switch (payload.source) {
case "manual":
return payload.livePreviewEnabled ? manualFastRefresh() : manualFallbackRefresh();
case "mutation":
return payload.livePreviewEnabled ? mutationFastRefresh() : mutationFallbackRefresh();
default:
throw new Error("Unknown refresh source", { cause: payload });
}
},
[refresh]
);
return /* @__PURE__ */ jsxRuntime.jsx(
react$1.VisualEditing,
{
components,
history,
portal: !0,
refresh: handleRefresh,
zIndex
}
);
}
exports.default = VisualEditing;
//# sourceMappingURL=VisualEditing.cjs.map