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
@@ -0,0 +1,386 @@
1
+ /**
2
+ * The `data-sanity` attribute that marks an element as editable, and its path, without
3
+ * dependencies. Storefronts write it with `createDataAttribute({id, type, path})` or by hand:
4
+ *
5
+ * data-sanity="id=page-home;type=page;path=sections:k1.heading"
6
+ *
7
+ * `base=` and `workspace=` may follow; they're read but unused. `path` is Sanity's URL path form:
8
+ * `.` between segments, `field:key` for a keyed array item, and `field:3` (up to 9 digits) for an
9
+ * index, so `sections:k1.heading` is `sections[_key=="k1"].heading`.
10
+ */
11
+ declare const SANITY_NODE_ATTRIBUTE = "data-sanity";
12
+ /** An editable element's document and path (URL form, as written in the attribute). */
13
+ type SanityNode = {
14
+ id: string;
15
+ type: string;
16
+ path: string;
17
+ base?: string;
18
+ workspace?: string;
19
+ };
20
+ /** A Studio path: field names, keyed array items and indexes. */
21
+ type StudioPathSegment = string | number | {
22
+ _key: string;
23
+ };
24
+ type StudioPath = StudioPathSegment[];
25
+ /** Longest path the edit message carries. */
26
+ declare const MAX_EDIT_PATH_LENGTH = 1000;
27
+ /**
28
+ * Reads a `data-sanity` value: pairs split on `;`, each on its first `=`, values URI-decoded.
29
+ * Returns `null` unless `id`, `type` and `path` are all present and non-empty.
30
+ */
31
+ declare function parseSanityNode(value: string | null | undefined): SanityNode | null;
32
+ /**
33
+ * Sanity's URL path form to a Studio path: `sections:k1.heading` →
34
+ * `['sections', {_key: 'k1'}, 'heading']`, `items:3` → `['items', 3]`. Chained items
35
+ * (`rows:r1:c2`) are nested arrays. Returns `null` for an empty or malformed path.
36
+ */
37
+ declare function toStudioPath(urlPath: string): StudioPath | null;
38
+ /**
39
+ * A Studio path as the document pane's `path` param reads it (`@sanity/util/paths` `toString`):
40
+ * `sections[_key=="k1"].heading`, `items[3]`.
41
+ */
42
+ declare function studioPathToString(path: StudioPath): string;
43
+ /** The pane param for a URL-form path, or `null` when it doesn't parse. */
44
+ declare function toPanePath(urlPath: string): string | null;
45
+ /** `heroImage` → `Hero image`, `section_title` → `Section title`. */
46
+ declare function humanizeFieldName(name: string): string;
47
+ /**
48
+ * A label for a path's last segment: the field's name, humanized (`sections:k1.heading` →
49
+ * `Heading`), or `Sections item` when it ends in an array item.
50
+ */
51
+ declare function describeSanityPath(urlPath: string): string;
52
+ /** A document's published ID (`drafts.` and `versions.<release>.` prefixes removed). */
53
+ declare function publishedDocumentId(id: string): string;
54
+ /** A Studio path in Sanity's URL form: `['sections', {_key: 'k1'}, 'heading']` → `sections:k1.heading`. */
55
+ declare function toUrlPath(path: readonly StudioPathSegment[]): string;
56
+ type SanityAttrDocument = {
57
+ _id?: string | null;
58
+ _type?: string | null;
59
+ } | {
60
+ id?: string | null;
61
+ type?: string | null;
62
+ } | null | undefined;
63
+ type SanityAttrPath = string | readonly StudioPathSegment[];
64
+ type SanityAttrOptions = {
65
+ /** Only emit `data-sanity` when true (e.g. `data.preview`). Default `true`. */
66
+ enabled?: boolean;
67
+ base?: string;
68
+ workspace?: string;
69
+ };
70
+ type SanityDataAttribute = {
71
+ readonly 'data-sanity'?: string;
72
+ };
73
+ /** Formats a `SanityNode` into a `data-sanity` attribute value (`id=...;type=...;path=...`). */
74
+ declare function encodeSanityNode(node: SanityNode): string;
75
+ /**
76
+ * Returns `{ 'data-sanity': '...' }` for click-to-edit overlays, or `{}` when `enabled` is `false`
77
+ * or `doc`/`path` is incomplete. Spread directly onto any Svelte or JSX element:
78
+ *
79
+ * <h1 {...sanityAttr(page, 'heading', { enabled: preview })}>
80
+ * <section {...sanityAttr(page, ['sections', { _key: section._key }, 'title'])}>
81
+ */
82
+ declare function sanityAttr(doc: SanityAttrDocument, path: SanityAttrPath, options?: SanityAttrOptions): SanityDataAttribute;
83
+ /**
84
+ * Scoped `sanityAttr` bound to one document and option set:
85
+ *
86
+ * const attr = createSanityAttr(page, { enabled: preview })
87
+ * <h1 {...attr('heading')}>
88
+ */
89
+ declare function createSanityAttr(doc: SanityAttrDocument, options?: SanityAttrOptions): (path: SanityAttrPath) => SanityDataAttribute;
90
+ /**
91
+ * The nodes that show a focused field: those of the same document whose path is the longest
92
+ * prefix of the focused path (the field itself, or the nearest annotated element around it).
93
+ * A field can render more than once (a desktop and a mobile variant), so all of them match.
94
+ */
95
+ declare function matchFocusedNodes<T extends {
96
+ node: SanityNode;
97
+ }>(items: readonly T[], focus: {
98
+ id: string;
99
+ path: string;
100
+ }): T[];
101
+ /** The parts of a compiled Sanity schema type the labels read. */
102
+ type LabelSchemaType = {
103
+ name: string;
104
+ title?: string;
105
+ jsonType?: string;
106
+ fields?: readonly {
107
+ name: string;
108
+ type: LabelSchemaType;
109
+ }[];
110
+ of?: readonly LabelSchemaType[];
111
+ to?: readonly LabelSchemaType[];
112
+ options?: {
113
+ list?: readonly unknown[];
114
+ };
115
+ };
116
+ type LabelSchema = {
117
+ get(name: string): LabelSchemaType | undefined;
118
+ };
119
+ /** A node's label for the overlay, keyed by the node's `id` and `path` as the page sent them. */
120
+ type NodeLabel = {
121
+ id: string;
122
+ path: string;
123
+ label: string;
124
+ missing?: boolean;
125
+ };
126
+ /**
127
+ * The label of a path's last segment, and whether the path resolves. `document` is the document's
128
+ * value, `null` when it doesn't exist, or `undefined` when it wasn't loaded (then only the schema
129
+ * is checked).
130
+ */
131
+ declare function describePath(schema: LabelSchema, documentType: string, document: unknown, urlPath: string): {
132
+ label: string;
133
+ missing: boolean;
134
+ };
135
+ /** Fetches documents by published ID, in the editor's perspective; absent ones are left out. */
136
+ type DocumentFetcher = (ids: string[]) => Promise<Record<string, unknown>>;
137
+ /**
138
+ * Answers the overlay's `describe`: one fetch for the documents involved (cached briefly, since
139
+ * hovering asks a few at a time), then each node's label and drift.
140
+ */
141
+ declare function createNodeDescriber(schema: LabelSchema, fetchDocuments: DocumentFetcher, cacheMs?: number): (nodes: readonly SanityNode[]) => Promise<NodeLabel[]>;
142
+ type PerspectiveClient = {
143
+ withConfig(config: {
144
+ perspective: unknown;
145
+ }): {
146
+ fetch(query: string, params: Record<string, unknown>, options?: unknown): Promise<unknown>;
147
+ };
148
+ };
149
+ /** A `DocumentFetcher` over a Sanity client, in a perspective stack (`['drafts']`, a release…). */
150
+ declare function clientDocumentFetcher(client: PerspectiveClient, perspective: unknown): DocumentFetcher;
151
+ /** Most nodes one `describe` or `labels` message carries. */
152
+ declare const MAX_DESCRIBE_NODES = 500;
153
+ /** Default route for the framed bar; its endpoints live under it (`/__visual-editor/resolve`, …). */
154
+ declare const PREVIEW_BAR_ROUTE = "/__visual-editor";
155
+ /**
156
+ * Query parameter on storefront URLs loaded as Studio thumbnails: suppresses preview mode,
157
+ * overlays, and storefront analytics so editors see published content cleanly.
158
+ */
159
+ declare const STUDIO_PREVIEW_PARAM = "studio-preview";
160
+ /** Visible height of the bar in CSS pixels; the host reserves this much at the bottom of the page. */
161
+ declare const PREVIEW_BAR_HEIGHT = 50;
162
+ /**
163
+ * With the document pane open, the page (and the bar) keep at least this many CSS pixels beside
164
+ * it. On a narrower viewport the pane overlays the page instead, above a full-width bar.
165
+ */
166
+ declare const PREVIEW_PANEL_MIN_PAGE_WIDTH = 360;
167
+ /** Which edge of the viewport the document pane sits on. */
168
+ type PaneSide = 'left' | 'right';
169
+ /** Which edge of the viewport the bar sits on. */
170
+ type PreviewBarPosition = 'top' | 'bottom';
171
+ /**
172
+ * The host tells the framed bar its position in the frame's URL (`/__visual-editor?position=top`), so
173
+ * the bar renders in place from its first frame.
174
+ */
175
+ declare const PREVIEW_BAR_POSITION_PARAM = "position";
176
+ declare function readPreviewBarPosition(search: string): PreviewBarPosition;
177
+ /**
178
+ * Where the bar is framed: on the site (by `sanity-plugin-visual-editor/host`) or in the Studio's Visual
179
+ * Editor tool (`sanity-plugin-visual-editor/studio`), which passes `?context=tool` in the frame's URL.
180
+ */
181
+ type PreviewBarContext = 'site' | 'tool';
182
+ declare const PREVIEW_BAR_CONTEXT_PARAM = "context";
183
+ declare function readPreviewBarContext(search: string): PreviewBarContext;
184
+ /**
185
+ * The Visual Editor tool frames the site under this window name. It survives navigation inside
186
+ * the frame, so the site can tell it's in the tool (not another embed) on every page.
187
+ */
188
+ declare const PREVIEW_BAR_FRAME_NAME = "sanity-plugin-visual-editor-frame";
189
+ /**
190
+ * Whether this window is the site framed by the Visual Editor tool, where the page reads like a
191
+ * normal preview tab and mounts `mountPreviewBarFrame` instead of the host.
192
+ */
193
+ declare function isPreviewBarFrame(): boolean;
194
+ /** The Visual Editor's viewport sizes. */
195
+ type PreviewBarViewport = 'desktop' | 'tablet' | 'mobile';
196
+ /** A document to open in the Visual Editor's pane; a new one carries its template. */
197
+ type PreviewBarDocument = {
198
+ id: string;
199
+ type: string;
200
+ template?: string;
201
+ templateParameters?: Record<string, unknown>;
202
+ };
203
+ /** The bar's perspective as a menu selection: the chosen layer and the hidden ones. */
204
+ type PreviewBarSelection = {
205
+ selected: string;
206
+ excluded: string[];
207
+ };
208
+ type PreviewBarMessage =
209
+ /** Bar → host, once mounted: asks for the current storefront path. */
210
+ {
211
+ type: 'sanity-visual-editor:ready';
212
+ } |
213
+ /** Bar → host: hide the bar until the next full page load (preview mode stays on). */
214
+ {
215
+ type: 'sanity-visual-editor:hide';
216
+ } |
217
+ /** Bar → host: the perspective menu opened or closed; the host grows the frame so it fits. */
218
+ {
219
+ type: 'sanity-visual-editor:menu';
220
+ open: boolean;
221
+ } |
222
+ /** Bar → host: the perspective cookie changed; rerun every query and load in place. */
223
+ {
224
+ type: 'sanity-visual-editor:refresh';
225
+ } |
226
+ /** Host → bar: the storefront finished refreshing after `refresh`. */
227
+ {
228
+ type: 'sanity-visual-editor:refreshed';
229
+ } |
230
+ /** Host → bar: the storefront path and query, sent on `ready` and after every navigation. */
231
+ {
232
+ type: 'sanity-visual-editor:path';
233
+ path: string;
234
+ } |
235
+ /** Host → bar: ⌘K / Ctrl+K was pressed on the storefront page; open search. */
236
+ {
237
+ type: 'sanity-visual-editor:search';
238
+ } |
239
+ /** Host → bar: `E` was pressed on the storefront page (not while typing); toggle the pane. */
240
+ {
241
+ type: 'sanity-visual-editor:pane';
242
+ } |
243
+ /** Bar → host: a search result was chosen; navigate the storefront there (client-side). */
244
+ {
245
+ type: 'sanity-visual-editor:navigate';
246
+ href: string;
247
+ } |
248
+ /** Bar → Visual Editor tool: open this document in the tool's pane (Edit, Create new document). */
249
+ {
250
+ type: 'sanity-visual-editor:open-document';
251
+ document: PreviewBarDocument;
252
+ } |
253
+ /**
254
+ * Visual Editor tool → bar: the tool's pane opened or closed (Edit hides while it's open), and
255
+ * whether the page has a document to edit (Edit shows only then).
256
+ */
257
+ {
258
+ type: 'sanity-visual-editor:editor-state';
259
+ open: boolean;
260
+ available?: boolean;
261
+ /**
262
+ * The tool is too narrow for the pane beside the page: one shows at a time, and Edit stays,
263
+ * pressed while the document shows, and toggles between them (`close-editor` goes back).
264
+ */
265
+ toggle?: boolean;
266
+ } |
267
+ /** Visual Editor tool → bar: the current page's document (the page picker previews it), if any. */
268
+ {
269
+ type: 'sanity-visual-editor:page-document';
270
+ document: {
271
+ id: string;
272
+ type: string;
273
+ } | null;
274
+ } |
275
+ /** Bar → Visual Editor tool: Edit was clicked; open the page's document in the pane. */
276
+ {
277
+ type: 'sanity-visual-editor:open-editor';
278
+ } |
279
+ /** Bar → Visual Editor tool: Edit was clicked again (narrow, `toggle`); show the page. */
280
+ {
281
+ type: 'sanity-visual-editor:close-editor';
282
+ } |
283
+ /** Bar → Visual Editor tool: show the site at desktop width, as a 768px tablet or a 375px phone. */
284
+ {
285
+ type: 'sanity-visual-editor:viewport';
286
+ mode: PreviewBarViewport;
287
+ } |
288
+ /** Bar → Visual Editor tool: the bar's perspective changed; the Studio follows it. */
289
+ {
290
+ type: 'sanity-visual-editor:perspective';
291
+ selection: PreviewBarSelection;
292
+ } |
293
+ /** Visual Editor tool → bar: the Studio's perspective changed (pane chips, navbar); switch to it. */
294
+ {
295
+ type: 'sanity-visual-editor:set-perspective';
296
+ selection: PreviewBarSelection;
297
+ } |
298
+ /** Bar → Visual Editor tool: the logo was clicked; go to the Studio (its router, no reload). */
299
+ {
300
+ type: 'sanity-visual-editor:studio-home';
301
+ } |
302
+ /** Site framed by the Visual Editor tool → tool: its path and query, on load and navigation. */
303
+ {
304
+ type: 'sanity-visual-editor:frame-path';
305
+ path: string;
306
+ } |
307
+ /**
308
+ * Bar → host: the document pane opened (a full-height column `width` CSS pixels wide on `side`)
309
+ * or closed. The host clips the frame to that column plus the bar and reflows the page beside it.
310
+ */
311
+ {
312
+ type: 'sanity-visual-editor:panel';
313
+ open: boolean;
314
+ width: number;
315
+ side: PaneSide;
316
+ } |
317
+ /**
318
+ * Site framed by the Visual Editor tool → tool: an element marked with `data-sanity` was clicked
319
+ * while the inspector is on (click-to-edit, `sanity-plugin-visual-editor/host`'s overlay). Open `id` in the document pane
320
+ * at `path`, which is in Sanity's URL form as written in the attribute (`sections:k1.heading`);
321
+ * `toPanePath` turns it into the pane's `path` param. The page's own document opens at the path
322
+ * (an array item opens its editor, a field gets focus); any other document goes on the pane's
323
+ * stack.
324
+ */
325
+ {
326
+ type: 'sanity-visual-editor:edit';
327
+ id: string;
328
+ documentType: string;
329
+ path: string;
330
+ } |
331
+ /**
332
+ * Visual Editor tool → framed site: the document pane focused a field (`path` in
333
+ * URL form) of document `id`; the overlay outlines the elements that show it and scrolls one into
334
+ * view. Empty `id` and `path` clear it.
335
+ */
336
+ {
337
+ type: 'sanity-visual-editor:focus';
338
+ id: string;
339
+ path: string;
340
+ } |
341
+ /**
342
+ * Bar → Visual Editor tool → framed site: the inspector (the bar's "Activate inspector" button or
343
+ * ⌘⇧I) turned click-to-edit on or off. The tool resends it after every page load in the frame.
344
+ */
345
+ {
346
+ type: 'sanity-visual-editor:inspector';
347
+ active: boolean;
348
+ } |
349
+ /** Framed site → Visual Editor tool → bar: ⌘⇧I was pressed on the page; toggle the inspector. */
350
+ {
351
+ type: 'sanity-visual-editor:toggle-inspector';
352
+ } |
353
+ /**
354
+ * Framed site → Visual Editor tool: label these `data-sanity` nodes (field titles
355
+ * from the Studio's schema) and check that their paths resolve. Answered with `labels`.
356
+ */
357
+ {
358
+ type: 'sanity-visual-editor:describe';
359
+ nodes: SanityNode[];
360
+ } |
361
+ /** Visual Editor tool → framed site: the answer to `describe`. */
362
+ {
363
+ type: 'sanity-visual-editor:labels';
364
+ labels: NodeLabel[];
365
+ } |
366
+ /**
367
+ * Host → bar: how the storefront is following the preview hub. `connected` is a WebSocket;
368
+ * `polling` is the degraded long-poll fallback; `offline` means live updates are paused while
369
+ * retrying. The bar shows a toast on `polling` / `offline` (and on recovery to `connected`).
370
+ */
371
+ {
372
+ type: 'sanity-visual-editor:live-status';
373
+ status: 'connected' | 'polling' | 'offline';
374
+ };
375
+ /** ⌘⇧I / Ctrl+Shift+I: the inspector's shortcut, in the bar and on the framed page. */
376
+ declare function isInspectorShortcut(event: {
377
+ key: string;
378
+ code?: string;
379
+ metaKey: boolean;
380
+ ctrlKey: boolean;
381
+ shiftKey: boolean;
382
+ altKey: boolean;
383
+ repeat?: boolean;
384
+ }): boolean;
385
+ declare function isPreviewBarMessage(data: unknown): data is PreviewBarMessage;
386
+ export { SanityAttrDocument as A, humanizeFieldName as B, LabelSchemaType as C, describePath as D, createNodeDescriber as E, StudioPath as F, studioPathToString as G, parseSanityNode as H, StudioPathSegment as I, toUrlPath as J, toPanePath as K, createSanityAttr as L, SanityAttrPath as M, SanityDataAttribute as N, MAX_EDIT_PATH_LENGTH as O, SanityNode as P, describeSanityPath as R, LabelSchema as S, clientDocumentFetcher as T, publishedDocumentId as U, matchFocusedNodes as V, sanityAttr as W, isPreviewBarFrame as _, PREVIEW_BAR_POSITION_PARAM as a, readPreviewBarPosition as b, PaneSide as c, PreviewBarMessage as d, PreviewBarPosition as f, isInspectorShortcut as g, STUDIO_PREVIEW_PARAM as h, PREVIEW_BAR_HEIGHT as i, SanityAttrOptions as j, SANITY_NODE_ATTRIBUTE as k, PreviewBarContext as l, PreviewBarViewport as m, PREVIEW_BAR_CONTEXT_PARAM as n, PREVIEW_BAR_ROUTE as o, PreviewBarSelection as p, toStudioPath as q, PREVIEW_BAR_FRAME_NAME as r, PREVIEW_PANEL_MIN_PAGE_WIDTH as s, MAX_DESCRIBE_NODES as t, PreviewBarDocument as u, isPreviewBarMessage as v, NodeLabel as w, DocumentFetcher as x, readPreviewBarContext as y, encodeSanityNode as z };