Skip to content

Commit a21ddff

Browse files
committed
switch to js visual editor
1 parent 66fe6b2 commit a21ddff

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

apps/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@sanity/client": "^7.17.0",
1717
"@sanity/image-url": "^2.0.3",
1818
"@sanity/preview-url-secret": "^4.0.3",
19+
"@sanity/visual-editing": "^5.3.0",
1920
"@tailwindcss/vite": "^4.2.1",
2021
"@types/react": "^19.2.14",
2122
"@types/react-dom": "^19.2.3",

apps/web/src/layouts/BaseLayout.astro

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import "../styles/global.css";
33
import ThemeScript from "../components/ThemeScript.astro";
44
import Header from "../components/Header.astro";
55
import Footer from "../components/Footer.astro";
6-
import { VisualEditing } from "@sanity/astro/visual-editing";
76
import SocialMeta from "../components/SocialMeta.astro";
87
98
interface Props {
@@ -63,6 +62,10 @@ const visualEditingEnabled =
6362

6463
<Footer />
6564

66-
<VisualEditing enabled={visualEditingEnabled} />
65+
{visualEditingEnabled && (
66+
<script>
67+
import("../scripts/visual-editing").then((m) => m.run());
68+
</script>
69+
)}
6770
</body>
6871
</html>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Sanity Visual Editing (Plain JS).
3+
* Loaded only when visual editing is enabled; integrates with History API for Presentation tool.
4+
*/
5+
import { enableVisualEditing } from "@sanity/visual-editing";
6+
7+
export function run(): () => void {
8+
return enableVisualEditing({
9+
history: {
10+
subscribe: (navigate) => {
11+
const handler = (_event: PopStateEvent) => {
12+
navigate({
13+
type: "push",
14+
url: `${location.pathname}${location.search}`,
15+
});
16+
};
17+
window.addEventListener("popstate", handler);
18+
return () => window.removeEventListener("popstate", handler);
19+
},
20+
update: (update) => {
21+
switch (update.type) {
22+
case "push":
23+
window.history.pushState(null, "", update.url);
24+
break;
25+
case "pop":
26+
window.history.back();
27+
break;
28+
case "replace":
29+
window.history.replaceState(null, "", update.url);
30+
break;
31+
default:
32+
throw new Error(`Unknown update type: ${(update as { type: string }).type}`);
33+
}
34+
},
35+
},
36+
zIndex: 1000,
37+
});
38+
}

pnpm-lock.yaml

Lines changed: 11 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)