sanity-plugin-visual-editor 1.0.0 → 1.2.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.
Files changed (59) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +263 -16
  3. package/TESTING.md +133 -0
  4. package/dist/PreviewBar-D_rw0bhP.js +3503 -0
  5. package/dist/PreviewBar-D_rw0bhP.js.map +1 -0
  6. package/dist/bar.d.ts +22 -0
  7. package/dist/bar.js +67 -0
  8. package/dist/bar.js.map +1 -0
  9. package/dist/cloudflare.d.ts +87 -0
  10. package/dist/cloudflare.js +1134 -0
  11. package/dist/cloudflare.js.map +1 -0
  12. package/dist/data-D0qotTtt.d.ts +177 -0
  13. package/dist/host-x2eXr-LJ.js +619 -0
  14. package/dist/host-x2eXr-LJ.js.map +1 -0
  15. package/dist/host.d.ts +162 -0
  16. package/dist/host.js +3 -0
  17. package/dist/index-hDzatlg7.d.ts +276 -0
  18. package/dist/index.d.ts +18 -18
  19. package/dist/index.js +7 -8
  20. package/dist/link-search-JtZkxz4k.js +14 -0
  21. package/dist/link-search-JtZkxz4k.js.map +1 -0
  22. package/dist/next-client.d.ts +22 -0
  23. package/dist/next-client.js +112 -0
  24. package/dist/next-client.js.map +1 -0
  25. package/dist/next.d.ts +135 -0
  26. package/dist/next.js +176 -0
  27. package/dist/next.js.map +1 -0
  28. package/dist/node-labels-CDiW5rMq.js +122 -0
  29. package/dist/node-labels-CDiW5rMq.js.map +1 -0
  30. package/dist/page-documents-BFFcH8lD.d.ts +54 -0
  31. package/dist/page-documents-mGfw_flR.js +67 -0
  32. package/dist/page-documents-mGfw_flR.js.map +1 -0
  33. package/dist/perspective-BmS9TVtr.js +106 -0
  34. package/dist/perspective-BmS9TVtr.js.map +1 -0
  35. package/dist/preview-links-BZDZyIey.js +119 -0
  36. package/dist/preview-links-BZDZyIey.js.map +1 -0
  37. package/dist/preview-links-DM6BCiLV.d.ts +87 -0
  38. package/dist/protocol-BNO38gYy.js +280 -0
  39. package/dist/protocol-BNO38gYy.js.map +1 -0
  40. package/dist/protocol-BnRCy2wJ.d.ts +386 -0
  41. package/dist/server-B8MqMyaN.js +748 -0
  42. package/dist/server-B8MqMyaN.js.map +1 -0
  43. package/dist/server.d.ts +4 -0
  44. package/dist/server.js +3 -0
  45. package/dist/shell-DZotxRXX.d.ts +37 -0
  46. package/dist/studio.d.ts +93 -0
  47. package/dist/studio.js +1263 -0
  48. package/dist/studio.js.map +1 -0
  49. package/dist/sveltekit.d.ts +281 -0
  50. package/dist/sveltekit.js +394 -0
  51. package/dist/sveltekit.js.map +1 -0
  52. package/package.json +120 -39
  53. package/dist/index.d.mts +0 -21
  54. package/dist/index.js.map +0 -1
  55. package/dist/index.mjs +0 -8
  56. package/dist/index.mjs.map +0 -1
  57. package/sanity.json +0 -8
  58. package/src/index.ts +0 -27
  59. package/v2-incompatible.js +0 -11
package/dist/bar.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { n as PreviewBarData, r as PreviewLinkErrorData } from "./data-D0qotTtt.js";
2
+ import { a as StudioConfigLoader, i as ToolEditorState, r as PreviewBarOptions, t as DocumentPaneOptions } from "./shell-DZotxRXX.js";
3
+ import "@sanity/ui/styles.css";
4
+ export declare function SitePreviewBar({ data, options }: {
5
+ data: PreviewBarData;
6
+ options: PreviewBarOptions;
7
+ }): import("react").JSX.Element;
8
+ export declare function VisualEditorBar({ data, options, native, editorState, initialPageDocument }: {
9
+ data: PreviewBarData;
10
+ options: PreviewBarOptions;
11
+ native: unknown;
12
+ editorState?: ToolEditorState;
13
+ initialPageDocument?: {
14
+ id: string;
15
+ type: string;
16
+ } | null;
17
+ }): import("react").JSX.Element;
18
+ /** Renders the bar into `element` and returns its unmount function. */
19
+ export declare function mountPreviewBar(element: HTMLElement, data: PreviewBarData, options?: PreviewBarOptions): () => void;
20
+ /** Renders the message into `element` and returns its unmount function. */
21
+ export declare function mountPreviewLinkError(element: HTMLElement, data: PreviewLinkErrorData): () => void;
22
+ export type { DocumentPaneOptions, PreviewBarData, PreviewBarOptions, PreviewLinkErrorData, StudioConfigLoader };
package/dist/bar.js ADDED
@@ -0,0 +1,67 @@
1
+ import { d as injectStudioDocumentCss, i as SitePreviewBar, n as mountPreviewBar, r as VisualEditorBar } from "./PreviewBar-D_rw0bhP.js";
2
+ import { a as PREVIEW_LINK_TTL_SECONDS } from "./preview-links-BZDZyIey.js";
3
+ import { StrictMode } from "react";
4
+ import { createRoot } from "react-dom/client";
5
+ import { Box, Button, Card, Container, Flex, Stack, Text, ThemeProvider, usePrefersDark } from "@sanity/ui";
6
+ import { jsx, jsxs } from "react/jsx-runtime";
7
+ import "@sanity/ui/styles.css";
8
+ import { buildTheme } from "@sanity/ui/theme";
9
+ const theme = buildTheme(), TTL_DAYS = PREVIEW_LINK_TTL_SECONDS / 86400;
10
+ function Message({ expired, studioPath = "/studio" }) {
11
+ let title = expired ? "This preview link has expired" : "This preview link couldn’t be opened", message = expired ? `Preview links open for ${TTL_DAYS} days. Ask the person who shared it for a new link.` : "Something went wrong on our side. Try the link again in a moment.";
12
+ return /* @__PURE__ */ jsx(Card, {
13
+ as: "main",
14
+ height: "fill",
15
+ style: { minHeight: "100dvh" },
16
+ children: /* @__PURE__ */ jsx(Flex, {
17
+ align: "center",
18
+ justify: "center",
19
+ style: { minHeight: "100dvh" },
20
+ children: /* @__PURE__ */ jsx(Container, {
21
+ width: 0,
22
+ padding: 4,
23
+ children: /* @__PURE__ */ jsxs(Stack, {
24
+ gap: 4,
25
+ children: [/* @__PURE__ */ jsxs(Stack, {
26
+ gap: 3,
27
+ children: [/* @__PURE__ */ jsx(Text, {
28
+ as: "h1",
29
+ size: 1,
30
+ weight: "semibold",
31
+ children: title
32
+ }), /* @__PURE__ */ jsx(Text, {
33
+ as: "p",
34
+ size: 1,
35
+ muted: !0,
36
+ children: message
37
+ })]
38
+ }), /* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsx(Button, {
39
+ as: "a",
40
+ href: studioPath,
41
+ mode: "ghost",
42
+ padding: 2,
43
+ gap: 2,
44
+ text: "Open Studio"
45
+ }) })]
46
+ })
47
+ })
48
+ })
49
+ });
50
+ }
51
+ function App(props) {
52
+ let scheme = usePrefersDark() ? "dark" : "light";
53
+ return /* @__PURE__ */ jsx(ThemeProvider, {
54
+ theme,
55
+ scheme,
56
+ children: /* @__PURE__ */ jsx(Message, { ...props })
57
+ });
58
+ }
59
+ /** Renders the message into `element` and returns its unmount function. */
60
+ function mountPreviewLinkError(element, data) {
61
+ injectStudioDocumentCss(element.ownerDocument);
62
+ let root = createRoot(element);
63
+ return root.render(/* @__PURE__ */ jsx(StrictMode, { children: /* @__PURE__ */ jsx(App, { ...data }) })), () => root.unmount();
64
+ }
65
+ export { SitePreviewBar, VisualEditorBar, mountPreviewBar, mountPreviewLinkError };
66
+
67
+ //# sourceMappingURL=bar.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bar.js","names":[],"sources":["../src/bar/PreviewLinkError.tsx"],"sourcesContent":["// The page an unknown or expired short preview link shows: pure Sanity UI, like the bar, so it\n// reads as Sanity editor chrome rather than the site. Render it outside the site's layout.\n//\n// The layout is Presentation's `ErrorCard` (`sanity@6.23`, \"An error occurred / Could not connect\n// to the preview\"): a full-height card centring a `Container size={0} padding={4}`, a gap-4 stack\n// of the semibold title and muted message (gap 3) over one ghost button with the Studio `Button`\n// defaults (`padding={2}`, `gap={2}`). Its Retry becomes \"Open Studio\"; there's no \"Continue anyway\".\nimport '@sanity/ui/styles.css'\nimport {\n Box,\n Button,\n Card,\n Container,\n Flex,\n Stack,\n Text,\n ThemeProvider,\n usePrefersDark,\n} from '@sanity/ui'\nimport {buildTheme} from '@sanity/ui/theme'\nimport {StrictMode} from 'react'\nimport {createRoot} from 'react-dom/client'\nimport type {PreviewLinkErrorData} from '../core/data'\nimport {PREVIEW_LINK_TTL_SECONDS} from '../core/preview-links'\nimport {injectStudioDocumentCss} from './document-css'\n\nexport type {PreviewLinkErrorData}\n\nconst theme = buildTheme()\nconst TTL_DAYS = PREVIEW_LINK_TTL_SECONDS / (24 * 60 * 60)\n\nfunction Message({expired, studioPath = '/studio'}: PreviewLinkErrorData) {\n const title = expired ? 'This preview link has expired' : 'This preview link couldn’t be opened'\n const message = expired\n ? `Preview links open for ${TTL_DAYS} days. Ask the person who shared it for a new link.`\n : 'Something went wrong on our side. Try the link again in a moment.'\n\n return (\n <Card as=\"main\" height=\"fill\" style={{minHeight: '100dvh'}}>\n <Flex align=\"center\" justify=\"center\" style={{minHeight: '100dvh'}}>\n <Container width={0} padding={4}>\n <Stack gap={4}>\n <Stack gap={3}>\n <Text as=\"h1\" size={1} weight=\"semibold\">\n {title}\n </Text>\n <Text as=\"p\" size={1} muted>\n {message}\n </Text>\n </Stack>\n <Box>\n <Button\n as=\"a\"\n href={studioPath}\n mode=\"ghost\"\n padding={2}\n gap={2}\n text=\"Open Studio\"\n />\n </Box>\n </Stack>\n </Container>\n </Flex>\n </Card>\n )\n}\n\nfunction App(props: PreviewLinkErrorData) {\n const scheme = usePrefersDark() ? 'dark' : 'light'\n return (\n <ThemeProvider theme={theme} scheme={scheme}>\n <Message {...props} />\n </ThemeProvider>\n )\n}\n\n/** Renders the message into `element` and returns its unmount function. */\nexport function mountPreviewLinkError(element: HTMLElement, data: PreviewLinkErrorData) {\n injectStudioDocumentCss(element.ownerDocument)\n const root = createRoot(element)\n root.render(\n <StrictMode>\n <App {...data} />\n </StrictMode>,\n )\n return () => root.unmount()\n}\n"],"mappings":";;;;;;;;AA4BA,MAAM,QAAQ,WAAW,GACnB,WAAW,2BAA4B;AAE7C,SAAS,QAAQ,EAAC,SAAS,aAAa,aAAkC;CACxE,IAAM,QAAQ,UAAU,kCAAkC,wCACpD,UAAU,UACZ,0BAA0B,SAAS,uDACnC;CAEJ,OACE,oBAAC,MAAD;EAAM,IAAG;EAAO,QAAO;EAAO,OAAO,EAAC,WAAW,SAAQ;EACvD,UAAA,oBAAC,MAAD;GAAM,OAAM;GAAS,SAAQ;GAAS,OAAO,EAAC,WAAW,SAAQ;GAC/D,UAAA,oBAAC,WAAD;IAAW,OAAO;IAAG,SAAS;IAC5B,UAAA,qBAAC,OAAD;KAAO,KAAK;KAAZ,UAAA,CACE,qBAAC,OAAD;MAAO,KAAK;MAAZ,UAAA,CACE,oBAAC,MAAD;OAAM,IAAG;OAAK,MAAM;OAAG,QAAO;OAC3B,UAAA;MACG,CAAA,GACN,oBAAC,MAAD;OAAM,IAAG;OAAI,MAAM;OAAG,OAAA;OACnB,UAAA;MACG,CAAA,CACD;KACP,CAAA,GAAA,oBAAC,KAAD,EAAA,UACE,oBAAC,QAAD;MACE,IAAG;MACH,MAAM;MACN,MAAK;MACL,SAAS;MACT,KAAK;MACL,MAAK;KACN,CAAA,EACE,CAAA,CACA;;GACE,CAAA;EACP,CAAA;CACF,CAAA;AAEV;AAEA,SAAS,IAAI,OAA6B;CACxC,IAAM,SAAS,eAAe,IAAI,SAAS;CAC3C,OACE,oBAAC,eAAD;EAAsB;EAAe;EACnC,UAAA,oBAAC,SAAD,EAAS,GAAI,MAAQ,CAAA;CACR,CAAA;AAEnB;;AAGA,SAAgB,sBAAsB,SAAsB,MAA4B;CACtF,wBAAwB,QAAQ,aAAa;CAC7C,IAAM,OAAO,WAAW,OAAO;CAM/B,OALA,KAAK,OACH,oBAAC,YAAD,EAAA,UACE,oBAAC,KAAD,EAAK,GAAI,KAAO,CAAA,EACN,CAAA,CACd,SACa,KAAK,QAAQ;AAC5B"}
@@ -0,0 +1,87 @@
1
+ import { DurableObject } from "cloudflare:workers";
2
+ /**
3
+ * Realtime preview hub (LF-INIT-20). One instance per preview perspective, addressed by name
4
+ * (`drafts`, or a release stack such as `rABC,drafts`).
5
+ *
6
+ * - Reads: the storefront calls `POST /query` instead of Sanity. Results are held in memory with
7
+ * their sync tags, so editors on the same page share one Sanity fetch.
8
+ * - Fast path: a local mirror of the documents (`mirror.ts`) follows `/listen` with
9
+ * `includeResult`, ~50 ms after each write, and re-evaluates held queries with `groq-js`.
10
+ * A query uses this path only after its local result matched Sanity's exactly.
11
+ * Refetching instead would not help: Sanity's query API returns a change only ~1 s after the
12
+ * write, whatever the request (tested 2026-11-23).
13
+ * - Authority: one Live Content API stream (`includeDrafts`) reports changed sync tags ~1 s after
14
+ * each write. Matching results are refetched from Sanity, which corrects any local result.
15
+ * (The `invalidate-tags` Sanity Function never fires for draft edits, tested 2026-11-23.)
16
+ * - Downstream: when a result's hash changes, its key is appended to an in-memory log and sent at
17
+ * once to every tab's WebSocket (`/socket`); tabs that cannot open one long-poll (`/events`).
18
+ * A tab resumes from its cursor. Nothing here touches Durable Object storage: on Workers, the
19
+ * output gate holds a Durable Object's outgoing messages until its storage writes are
20
+ * confirmed, and with a durable log (`agents/streams`) every update reached the tab ~3 s late
21
+ * (2026-11-23).
22
+ */
23
+ type QueryParams = Record<string, unknown>;
24
+ /** Payload of each invalidation event sent to tabs. */
25
+ export interface PreviewInvalidation {
26
+ keys: string[];
27
+ at: number;
28
+ /** Which path found the change: local evaluation, the delayed re-evaluation or a refetch. */
29
+ source: 'local' | 'rest' | 'sweep';
30
+ /**
31
+ * The changed results, by key, so a tab can show them without asking again. Only for results
32
+ * under {@link MAX_PUSH_BYTES}. Events a tab catches up on (a long poll, or a reconnecting
33
+ * WebSocket) carry each key's current result, on the last event that names the key.
34
+ */
35
+ results?: Record<string, unknown>;
36
+ /** The storefront remote queries that read each changed key. */
37
+ labels?: Record<string, string[]>;
38
+ }
39
+ /** Cloudflare Worker bindings and vars for the preview hub worker. */
40
+ export interface PreviewHubEnv {
41
+ CF_VERSION_METADATA: {
42
+ id: string;
43
+ };
44
+ SANITY_PROJECT_ID: string;
45
+ SANITY_DATASET: string;
46
+ SANITY_API_VERSION: string;
47
+ SANITY_API_READ_TOKEN: string;
48
+ /**
49
+ * Comma-separated document types the mirror does not bulk-load; one is fetched only when a held
50
+ * result references it. Default `product,productVariant`; set to `""` to bulk-load every type.
51
+ */
52
+ PREVIEW_HUB_ON_DEMAND_TYPES?: string;
53
+ /**
54
+ * Comma-separated document types left out of the `/listen` stream (and so also on-demand).
55
+ * Default `productVariant`; set to `""` to listen to every type.
56
+ */
57
+ PREVIEW_HUB_LISTEN_EXCLUDED_TYPES?: string;
58
+ PREVIEW_HUB: {
59
+ getByName(name: string): {
60
+ fetch(request: Request): Promise<Response>;
61
+ };
62
+ };
63
+ }
64
+ export declare class PreviewHub extends DurableObject<PreviewHubEnv> {
65
+ #private;
66
+ /** Run a GROQ query in this hub's perspective, from memory when the hub already holds it. */
67
+ query<T = unknown>(query: string, params?: QueryParams, label?: string): Promise<T>;
68
+ /**
69
+ * - `POST /query` `{ query, params, label? }` → `{ result }`. `label` names the storefront's
70
+ * remote query reading it; events for the query carry it back. The storefront uses this rather than the
71
+ * `query()` RPC method because Vite dev's platform proxy cannot carry Durable Object RPC.
72
+ * - `GET /socket` (WebSocket upgrade) → invalidation events, pushed as they happen.
73
+ * - `GET /events` → the same events by long poll, for tabs without a WebSocket.
74
+ * - `GET /stats` → entry counts by local state and timings, for debugging.
75
+ */
76
+ fetch(request: Request): Promise<Response>;
77
+ }
78
+ /**
79
+ * Entry point for the storefront's service binding (this Worker has no public URL). It picks the
80
+ * hub for the request's perspective with `getByName()` here, inside this Worker, so the instance
81
+ * can read its perspective from `ctx.id.name`. Through Vite dev's platform proxy, a Durable
82
+ * Object stub reached directly from the storefront arrives without its name.
83
+ */
84
+ declare const _default: {
85
+ fetch(request: Request, env: PreviewHubEnv): Response | Promise<Response>;
86
+ };
87
+ export { _default as default };