@aphexcms/visual-editing 0.1.2 → 0.3.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.
@@ -1,5 +1,6 @@
1
1
  <script lang="ts">
2
2
  import type { Snippet } from 'svelte';
3
+ import { invalidateAll } from '$app/navigation';
3
4
  import { setLivePreviewContext } from './live-preview.svelte.js';
4
5
  import { enableAphexPreview } from './core.js';
5
6
 
@@ -22,9 +23,14 @@
22
23
 
23
24
  return enableAphexPreview({
24
25
  stega,
25
- onData: (doc) => {
26
+ onData: (doc, meta) => {
26
27
  preview.current = doc;
27
- }
28
+ preview.currentType = meta?.documentType ?? null;
29
+ preview.currentId = meta?.documentId ?? null;
30
+ },
31
+ // Re-run load functions (no full reload) when the editor signals a change to a
32
+ // document this page loaded server-side — e.g. after editing a referenced doc.
33
+ onRefresh: () => void invalidateAll()
28
34
  });
29
35
  });
30
36
  </script>
@@ -1 +1 @@
1
- {"version":3,"file":"AphexVisualOverlay.svelte.d.ts","sourceRoot":"","sources":["../src/lib/AphexVisualOverlay.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAKrC,UAAU,KAAK;IACd;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB;AA4BF,QAAA,MAAM,kBAAkB,2CAAwC,CAAC;AACjE,KAAK,kBAAkB,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAChE,eAAe,kBAAkB,CAAC"}
1
+ {"version":3,"file":"AphexVisualOverlay.svelte.d.ts","sourceRoot":"","sources":["../src/lib/AphexVisualOverlay.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAMrC,UAAU,KAAK;IACd;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB;AAkCF,QAAA,MAAM,kBAAkB,2CAAwC,CAAC;AACjE,KAAK,kBAAkB,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAChE,eAAe,kBAAkB,CAAC"}
package/dist/core.d.ts CHANGED
@@ -5,20 +5,17 @@ export interface AphexPreviewOptions {
5
5
  */
6
6
  stega?: boolean;
7
7
  /** Called whenever the CMS pushes a new document snapshot via postMessage. */
8
- onData?: (doc: Record<string, unknown>) => void;
8
+ onData?: (doc: Record<string, unknown>, meta?: {
9
+ documentType?: string;
10
+ documentId?: string;
11
+ }) => void;
12
+ /**
13
+ * Called when the CMS asks the preview to re-fetch (`aphex:refresh`) — e.g. after a
14
+ * *different* document was edited, whose data this page loaded server-side and can't
15
+ * receive via `onData`. Wire it to SvelteKit's `invalidateAll()` for a smooth reload;
16
+ * if omitted, the overlay falls back to a full `location.reload()`.
17
+ */
18
+ onRefresh?: () => void;
9
19
  }
10
- /**
11
- * Vanilla JS entry point for Aphex live preview.
12
- * Call this once in your frontend (framework-agnostic).
13
- * Returns a cleanup function — call it on unmount.
14
- *
15
- * @example
16
- * // SvelteKit
17
- * $effect(() => enableAphexPreview({ onData: (doc) => { preview.current = doc; } }));
18
- *
19
- * // Vanilla / React useEffect
20
- * const cleanup = enableAphexPreview({ onData: setPost });
21
- * return cleanup;
22
- */
23
20
  export declare function enableAphexPreview(options?: AphexPreviewOptions): () => void;
24
21
  //# sourceMappingURL=core.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/lib/core.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,8EAA8E;IAC9E,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;CAChD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,mBAAwB,GAAG,MAAM,IAAI,CAsNhF"}
1
+ {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/lib/core.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,8EAA8E;IAC9E,MAAM,CAAC,EAAE,CACR,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,IAAI,CAAC,EAAE;QAAE,YAAY,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,KACjD,IAAI,CAAC;IACV;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,IAAI,CAAC;CACvB;AAuDD,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,mBAAwB,GAAG,MAAM,IAAI,CAkPhF"}
package/dist/core.js CHANGED
@@ -12,14 +12,56 @@ import { stegaDecode } from './stega.js';
12
12
  * const cleanup = enableAphexPreview({ onData: setPost });
13
13
  * return cleanup;
14
14
  */
15
+ /**
16
+ * Read the admin theme's `--primary` (shadcn token) from the parent frame. The preview
17
+ * iframe is the same-origin app, so this reflects the current admin theme + light/dark.
18
+ * Returns null if the parent is cross-origin (a separate public-site URL) or unset.
19
+ */
20
+ function readParentPrimary() {
21
+ try {
22
+ const doc = window.parent?.document;
23
+ if (!doc)
24
+ return null;
25
+ const v = getComputedStyle(doc.documentElement).getPropertyValue('--primary').trim();
26
+ return v || null;
27
+ }
28
+ catch {
29
+ return null;
30
+ }
31
+ }
32
+ /**
33
+ * Would this element's default click action leave or reload the preview?
34
+ *
35
+ * Used to keep the preview iframe put without cancelling every default: only
36
+ * navigation and form submission are suppressed, so in-page defaults the author
37
+ * relies on (opening a <details> toggle, ticking a checkbox) still happen.
38
+ *
39
+ * `button.type` is checked as a property rather than an attribute selector
40
+ * because a bare <button> has no type attribute but still defaults to "submit".
41
+ */
42
+ function navigatesAway(target) {
43
+ if (target.closest('a[href]'))
44
+ return true;
45
+ const button = target.closest('button');
46
+ if (button && button.type !== 'button')
47
+ return true;
48
+ const input = target.closest('input');
49
+ if (input && (input.type === 'submit' || input.type === 'reset' || input.type === 'image')) {
50
+ return true;
51
+ }
52
+ return false;
53
+ }
15
54
  export function enableAphexPreview(options = {}) {
16
- const { stega = true, onData } = options;
55
+ const { stega = true, onData, onRefresh } = options;
17
56
  if (typeof window === 'undefined' || window.parent === window)
18
57
  return () => { };
19
- // Accent matches the studio's --primary (orange) so the preview overlay and the
20
- // editor's field-focus ring read as the same highlight.
21
- const ACCENT = 'oklch(0.62 0.14 39.04)';
22
- const ACCENT_FADE = 'oklch(0.62 0.14 39.04 / 0)';
58
+ // Accent matches the studio's theme. The preview iframe is the same-origin app, so
59
+ // read the admin's `--primary` (shadcn token, light/dark-aware) straight from the
60
+ // parent frame — the overlay then tracks the theme instead of a hardcoded hue. Falls
61
+ // back to a default if the parent is cross-origin (a separate public-site URL) or the
62
+ // token is missing.
63
+ const ACCENT = readParentPrimary() ?? 'oklch(0.62 0.14 39.04)';
64
+ const ACCENT_FADE = `color-mix(in oklab, ${ACCENT} 10%, transparent)`;
23
65
  // Cursor style for all field elements
24
66
  const style = document.createElement('style');
25
67
  style.textContent = '[data-aphex-field] { cursor: pointer !important; }';
@@ -73,6 +115,12 @@ export function enableAphexPreview(options = {}) {
73
115
  if (decoded.objectPath && !el.dataset.aphexObjectPath) {
74
116
  el.dataset.aphexObjectPath = decoded.objectPath;
75
117
  }
118
+ if (decoded.documentId && !el.dataset.aphexDocumentId) {
119
+ el.dataset.aphexDocumentId = decoded.documentId;
120
+ }
121
+ if (decoded.documentType && !el.dataset.aphexDocumentType) {
122
+ el.dataset.aphexDocumentType = decoded.documentType;
123
+ }
76
124
  }
77
125
  // Stega rides in two places: text node content, and a few attributes carrying human
78
126
  // text. Images have no text node, so they become click-to-edit via their `alt`; dates
@@ -113,7 +161,10 @@ export function enableAphexPreview(options = {}) {
113
161
  }
114
162
  function showOn(el) {
115
163
  activeEl = el;
116
- label.textContent = el.dataset.aphexField ?? '';
164
+ // Show the row number for a list entry — several rows of one list otherwise all
165
+ // hover as the same bare field name, with nothing to say which slot is which.
166
+ const idx = el.dataset.aphexArrayIndex;
167
+ label.textContent = (el.dataset.aphexField ?? '') + (idx != null ? `[${idx}]` : '');
117
168
  positionOn(el);
118
169
  overlay.style.display = 'block';
119
170
  }
@@ -131,11 +182,16 @@ export function enableAphexPreview(options = {}) {
131
182
  hide();
132
183
  };
133
184
  const onClick = (e) => {
134
- // Always prevent default — links/buttons must not navigate in preview
135
- e.preventDefault();
185
+ // Only cancel defaults that would take the preview somewhere else. A blanket
186
+ // preventDefault() here also cancels benign in-page defaults — <details>/<summary>
187
+ // toggling, checkboxes, radios — which authors expect to work while previewing
188
+ // (a collapsed toggle can't be read if it can never open).
189
+ const target = e.target;
190
+ if (navigatesAway(target))
191
+ e.preventDefault();
136
192
  if (!editMode)
137
193
  return;
138
- const el = e.target.closest('[data-aphex-field]');
194
+ const el = target.closest('[data-aphex-field]');
139
195
  if (el?.dataset.aphexField) {
140
196
  e.stopPropagation();
141
197
  const d = el.dataset;
@@ -151,7 +207,9 @@ export function enableAphexPreview(options = {}) {
151
207
  blockKey: d.aphexBlockKey ?? undefined,
152
208
  arrayIndex: d.aphexArrayIndex != null ? parseInt(d.aphexArrayIndex, 10) : undefined,
153
209
  objectPath: d.aphexObjectPath ?? undefined,
154
- linkHref
210
+ linkHref,
211
+ documentId: d.aphexDocumentId ?? undefined,
212
+ documentType: d.aphexDocumentType ?? undefined
155
213
  }, '*');
156
214
  }
157
215
  };
@@ -162,7 +220,7 @@ export function enableAphexPreview(options = {}) {
162
220
  const onMessage = (e) => {
163
221
  if (!e.data || typeof e.data !== 'object')
164
222
  return;
165
- const { type, fieldPath, document: doc } = e.data;
223
+ const { type, fieldPath, document: doc, documentType, documentId } = e.data;
166
224
  if (type === 'aphex:edit-mode') {
167
225
  editMode = e.data.enabled;
168
226
  if (!editMode) {
@@ -174,10 +232,18 @@ export function enableAphexPreview(options = {}) {
174
232
  }
175
233
  }
176
234
  else if (type === 'aphex:data' && doc) {
177
- onData?.(doc);
235
+ onData?.(doc, { documentType, documentId });
178
236
  // Re-scan after the framework re-renders with the new stega-encoded values
179
237
  requestAnimationFrame(scanStega);
180
238
  }
239
+ else if (type === 'aphex:refresh') {
240
+ // Re-fetch server-loaded data (a different document changed). Prefer the
241
+ // app's invalidate hook; otherwise hard-reload the preview.
242
+ if (onRefresh)
243
+ onRefresh();
244
+ else
245
+ location.reload();
246
+ }
181
247
  else if (type === 'aphex:field-focus' && fieldPath) {
182
248
  const el = document.querySelector(`[data-aphex-field="${fieldPath}"]`);
183
249
  if (!el)
@@ -1,5 +1,7 @@
1
1
  export declare class LivePreviewContext {
2
2
  current: Record<string, unknown> | null;
3
+ currentType: string | null;
4
+ currentId: string | null;
3
5
  }
4
6
  export declare function setLivePreviewContext(): LivePreviewContext;
5
7
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"live-preview.svelte.d.ts","sourceRoot":"","sources":["../src/lib/live-preview.svelte.ts"],"names":[],"mappings":"AAIA,qBAAa,kBAAkB;IAC9B,OAAO,iCAAgD;CACvD;AAED,wBAAgB,qBAAqB,IAAI,kBAAkB,CAI1D;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,IAAI,kBAAkB,CAE3D"}
1
+ {"version":3,"file":"live-preview.svelte.d.ts","sourceRoot":"","sources":["../src/lib/live-preview.svelte.ts"],"names":[],"mappings":"AAIA,qBAAa,kBAAkB;IAC9B,OAAO,iCAAgD;IACvD,WAAW,gBAA+B;IAC1C,SAAS,gBAA+B;CACxC;AAED,wBAAgB,qBAAqB,IAAI,kBAAkB,CAI1D;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,IAAI,kBAAkB,CAE3D"}
@@ -2,6 +2,8 @@ import { getContext, setContext } from 'svelte';
2
2
  const KEY = Symbol('aphex:live-preview');
3
3
  export class LivePreviewContext {
4
4
  current = $state(null);
5
+ currentType = $state(null);
6
+ currentId = $state(null);
5
7
  }
6
8
  export function setLivePreviewContext() {
7
9
  const ctx = new LivePreviewContext();
package/dist/stega.d.ts CHANGED
@@ -9,6 +9,14 @@ export type StegaPayload = {
9
9
  blockKey?: string;
10
10
  arrayIndex?: number;
11
11
  objectPath?: string;
12
+ /**
13
+ * Target a *different* document than the one being previewed. Use for content
14
+ * whose reference lives at the app level, not in the document — e.g. an
15
+ * app-queried "list of posts" block: stamp each card with the post's own id/type
16
+ * and clicking it opens that post, even though the page never stored the link.
17
+ */
18
+ documentId?: string;
19
+ documentType?: string;
12
20
  };
13
21
  /**
14
22
  * Stamp a navigation payload onto a string as invisible stega characters.
@@ -1 +1 @@
1
- {"version":3,"file":"stega.d.ts","sourceRoot":"","sources":["../src/lib/stega.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAEzC;AAED,MAAM,MAAM,YAAY,GAAG;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,MAAM,CAGxE;AAED,2EAA2E;AAC3E,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CAE9D"}
1
+ {"version":3,"file":"stega.d.ts","sourceRoot":"","sources":["../src/lib/stega.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAEzC;AAED,MAAM,MAAM,YAAY,GAAG;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;OAKG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,MAAM,CAGxE;AAED,2EAA2E;AAC3E,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CAE9D"}
package/dist/types.d.ts CHANGED
@@ -10,7 +10,10 @@ export type AphexOutboundMessage = {
10
10
  objectPath?: string;
11
11
  /** href of the clicked link, when the click landed on an anchor. */
12
12
  linkHref?: string;
13
+ /** When set, the click targets a different document (app-level reference);
14
+ * the studio opens it instead of resolving a field in the current doc. */
13
15
  documentId?: string;
16
+ documentType?: string;
14
17
  };
15
18
  export type AphexInboundMessage = {
16
19
  type: 'aphex:field-focus';
@@ -18,6 +21,8 @@ export type AphexInboundMessage = {
18
21
  } | {
19
22
  type: 'aphex:data';
20
23
  document: Record<string, unknown>;
24
+ documentType?: string;
25
+ documentId?: string;
21
26
  } | {
22
27
  type: 'aphex:edit-mode';
23
28
  enabled: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/lib/types.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,oBAAoB,GAC7B;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GACvC;IACA,IAAI,EAAE,mBAAmB,CAAC;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MA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,CAAC,CAAC;IAClC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;CACnB,GACD;IAAE,IAAI,EAAE,iBAAiB,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,GAC7C;IAAE,IAAI,EAAE,eAAe,CAAA;CAAE,CAAC"}
@@ -29,14 +29,47 @@ export interface PreviewApi {
29
29
  readonly inPreview: boolean;
30
30
  /** The live document pushed by the editor, or `null` outside preview. */
31
31
  readonly document: Record<string, unknown> | null;
32
+ /**
33
+ * Schema type of the document currently open in the editor, or `null` outside preview.
34
+ * Use it when one page is reachable from several document types and an element should
35
+ * behave differently depending on which one is being edited — e.g. a menu row that
36
+ * reveals its slot in the open menu's list, but opens the dish when a dish is open.
37
+ */
38
+ readonly documentType: string | null;
32
39
  /** The live document merged over your server fallback: `const post = $derived(ve.live(data.post))`. */
33
- live<T>(fallback: T): T;
40
+ live<T>(fallback: T, options?: {
41
+ type?: string;
42
+ id?: string;
43
+ }): T;
34
44
  /**
35
45
  * Make a value click-to-edit. In preview it returns the value stega-encoded with the
36
46
  * navigation payload; outside preview it returns the value unchanged. `field` defaults to
37
47
  * the Portable Text field context when omitted (for inline blocks).
38
48
  */
39
49
  encode(value: string | null | undefined, payload?: EncodePayload): string;
50
+ /**
51
+ * Make any element click-to-edit, for content the current document's own stega can't
52
+ * mark up. Spread the returned attributes onto the element; returns `{}` outside preview.
53
+ *
54
+ * Two targets, chosen by whether `id`/`type` are given:
55
+ * - **Another document** (`{ id, type }`) — an app-level reference not stored in the
56
+ * document being edited (e.g. an app-queried "list of posts" block). Clicking opens
57
+ * that document in the studio.
58
+ * - **A field of the open document** (`{ field, arrayIndex }`) — clicking reveals that
59
+ * field, and with `arrayIndex` the specific row, in the form pane. Use this for a
60
+ * list whose entries are references: revealing the row is what lets the author
61
+ * reorder or remove it, which opening the referenced document does not.
62
+ *
63
+ * @example
64
+ * // {...ve.edit({ id: post.id, type: 'blog_post' })} → opens that post
65
+ * // {...ve.edit({ field: 'items', arrayIndex: i })} → reveals row i of `items`
66
+ */
67
+ edit(target: {
68
+ id?: string;
69
+ type?: string;
70
+ field?: string;
71
+ arrayIndex?: number;
72
+ }): Record<string, string>;
40
73
  /**
41
74
  * Resolve an image field to `{ src, alt }` in one call — destructure it:
42
75
  * `const { src, alt } = $derived(ve.image(post.coverImage))`. Reads `asset.url`/`asset.alt`,
@@ -1 +1 @@
1
- {"version":3,"file":"use-preview.svelte.d.ts","sourceRoot":"","sources":["../src/lib/use-preview.svelte.ts"],"names":[],"mappings":"AAEA,OAAO,EAAe,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAI5D;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,GAAG,IAAI,CAEzE;AAED;gEACgE;AAChE,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;AAElD,KAAK,SAAS,GACX;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,GACvE,IAAI,GACJ,SAAS,CAAC;AAEb,6CAA6C;AAC7C,MAAM,WAAW,aAAa;IAC7B,yDAAyD;IACzD,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,yEAAyE;IACzE,GAAG,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,WAAW,UAAU;IAC1B,6FAA6F;IAC7F,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,yEAAyE;IACzE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAClD,uGAAuG;IACvG,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,CAA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M,GAAG,IAAI,CAAC;IACrC,uGAAuG;IACvG,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,EAAE,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,CAAC,CAAC;IAClE;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC;IAC1E;;;;;;;;;;;;;;;;OAgBG;IACH,IAAI,CAAC,MAAM,EAAE;QACZ,EAAE,CAAC,EAAE,MAAM,CAAC;QACZ,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,UAAU,CAAC,EAAE,MAAM,CAAC;KACpB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3B;;;;;OAKG;IACH,KAAK,CAAC,GAAG,EAAE,SAAS,GAAG,aAAa,CAAC;CACrC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,IAAI,UAAU,CAmDvC"}
@@ -32,7 +32,14 @@ export function usePreview() {
32
32
  get document() {
33
33
  return ctx.current;
34
34
  },
35
- live(fallback) {
35
+ get documentType() {
36
+ return ctx.currentType;
37
+ },
38
+ live(fallback, options = {}) {
39
+ if (options.type && ctx.currentType !== options.type)
40
+ return fallback;
41
+ if (options.id && ctx.currentId !== options.id)
42
+ return fallback;
36
43
  return ctx.current ?? fallback;
37
44
  },
38
45
  encode(value, payload = {}) {
@@ -44,6 +51,25 @@ export function usePreview() {
44
51
  return raw; // nothing to navigate to
45
52
  return stegaEncode(raw || ' ', { ...payload, field });
46
53
  },
54
+ edit(target) {
55
+ const attrs = {};
56
+ if (ctx.current == null)
57
+ return attrs; // not in preview — no attributes
58
+ // `data-aphex-field` is what the overlay keys on to make an element clickable,
59
+ // so it is always set. Adding id/type routes the click to *that* document (the
60
+ // studio opens it); leaving them off keeps the click on the open document, where
61
+ // `arrayIndex` picks out a single row. Both are only emitted together — the
62
+ // studio treats a cross-document click as such only when it has each half.
63
+ attrs['data-aphex-field'] = target.field ?? 'title';
64
+ if (target.id && target.type) {
65
+ attrs['data-aphex-document-id'] = target.id;
66
+ attrs['data-aphex-document-type'] = target.type;
67
+ }
68
+ if (target.arrayIndex != null) {
69
+ attrs['data-aphex-array-index'] = String(target.arrayIndex);
70
+ }
71
+ return attrs;
72
+ },
47
73
  image(img) {
48
74
  return {
49
75
  src: img?.asset?.url ?? null, // injected at load (server) or into the live doc (editor)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aphexcms/visual-editing",
3
- "version": "0.1.2",
3
+ "version": "0.3.0",
4
4
  "description": "Visual editing overlay for AphexCMS — add click-to-edit overlays to your frontend",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -27,12 +27,14 @@
27
27
  "dist"
28
28
  ],
29
29
  "devDependencies": {
30
+ "@sveltejs/kit": "^2.59.1",
30
31
  "@sveltejs/package": "^2.5.6",
31
32
  "svelte": "^5.0.0",
32
33
  "svelte-check": "^4.0.0",
33
34
  "typescript": "^5.9.3"
34
35
  },
35
36
  "peerDependencies": {
37
+ "@sveltejs/kit": "^2.0.0",
36
38
  "svelte": "^5.0.0"
37
39
  },
38
40
  "dependencies": {