@aphexcms/visual-editing 0.1.1 → 0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Benjamin Sinidol
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -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,CAqNhF"}
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,CA+OhF"}
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
@@ -125,14 +173,22 @@ export function enableAphexPreview(options = {}) {
125
173
  if (!editMode)
126
174
  return;
127
175
  const el = e.target.closest('[data-aphex-field]');
128
- el ? showOn(el) : hide();
176
+ if (el)
177
+ showOn(el);
178
+ else
179
+ hide();
129
180
  };
130
181
  const onClick = (e) => {
131
- // Always prevent default — links/buttons must not navigate in preview
132
- e.preventDefault();
182
+ // Only cancel defaults that would take the preview somewhere else. A blanket
183
+ // preventDefault() here also cancels benign in-page defaults — <details>/<summary>
184
+ // toggling, checkboxes, radios — which authors expect to work while previewing
185
+ // (a collapsed toggle can't be read if it can never open).
186
+ const target = e.target;
187
+ if (navigatesAway(target))
188
+ e.preventDefault();
133
189
  if (!editMode)
134
190
  return;
135
- const el = e.target.closest('[data-aphex-field]');
191
+ const el = target.closest('[data-aphex-field]');
136
192
  if (el?.dataset.aphexField) {
137
193
  e.stopPropagation();
138
194
  const d = el.dataset;
@@ -148,7 +204,9 @@ export function enableAphexPreview(options = {}) {
148
204
  blockKey: d.aphexBlockKey ?? undefined,
149
205
  arrayIndex: d.aphexArrayIndex != null ? parseInt(d.aphexArrayIndex, 10) : undefined,
150
206
  objectPath: d.aphexObjectPath ?? undefined,
151
- linkHref
207
+ linkHref,
208
+ documentId: d.aphexDocumentId ?? undefined,
209
+ documentType: d.aphexDocumentType ?? undefined
152
210
  }, '*');
153
211
  }
154
212
  };
@@ -159,7 +217,7 @@ export function enableAphexPreview(options = {}) {
159
217
  const onMessage = (e) => {
160
218
  if (!e.data || typeof e.data !== 'object')
161
219
  return;
162
- const { type, fieldPath, document: doc } = e.data;
220
+ const { type, fieldPath, document: doc, documentType, documentId } = e.data;
163
221
  if (type === 'aphex:edit-mode') {
164
222
  editMode = e.data.enabled;
165
223
  if (!editMode) {
@@ -171,10 +229,18 @@ export function enableAphexPreview(options = {}) {
171
229
  }
172
230
  }
173
231
  else if (type === 'aphex:data' && doc) {
174
- onData?.(doc);
232
+ onData?.(doc, { documentType, documentId });
175
233
  // Re-scan after the framework re-renders with the new stega-encoded values
176
234
  requestAnimationFrame(scanStega);
177
235
  }
236
+ else if (type === 'aphex:refresh') {
237
+ // Re-fetch server-loaded data (a different document changed). Prefer the
238
+ // app's invalidate hook; otherwise hard-reload the preview.
239
+ if (onRefresh)
240
+ onRefresh();
241
+ else
242
+ location.reload();
243
+ }
178
244
  else if (type === 'aphex:field-focus' && fieldPath) {
179
245
  const el = document.querySelector(`[data-aphex-field="${fieldPath}"]`);
180
246
  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,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,oEAAoE;IACpE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAGL,MAAM,MAAM,mBAAmB,GAC5B;IAAE,IAAI,EAAE,mBAAmB,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAChD;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,GACzD;IAAE,IAAI,EAAE,iBAAiB,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,GAC7C;IAAE,IAAI,EAAE,eAAe,CAAA;CAAE,CAAC"}
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,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,oEAAoE;IACpE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;+EAC2E;IAC3E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAGL,MAAM,MAAM,mBAAmB,GAC5B;IAAE,IAAI,EAAE,mBAAmB,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAChD;IACA,IAAI,EAAE,YAAY,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,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"}
@@ -30,13 +30,31 @@ export interface PreviewApi {
30
30
  /** The live document pushed by the editor, or `null` outside preview. */
31
31
  readonly document: Record<string, unknown> | null;
32
32
  /** The live document merged over your server fallback: `const post = $derived(ve.live(data.post))`. */
33
- live<T>(fallback: T): T;
33
+ live<T>(fallback: T, options?: {
34
+ type?: string;
35
+ id?: string;
36
+ }): T;
34
37
  /**
35
38
  * Make a value click-to-edit. In preview it returns the value stega-encoded with the
36
39
  * navigation payload; outside preview it returns the value unchanged. `field` defaults to
37
40
  * the Portable Text field context when omitted (for inline blocks).
38
41
  */
39
42
  encode(value: string | null | undefined, payload?: EncodePayload): string;
43
+ /**
44
+ * Make any element click-to-edit a *specific* document — for app-level references
45
+ * that aren't stored in the current document (e.g. an app-queried "list of posts"
46
+ * block). Spread the returned attributes onto the element; clicking it in preview
47
+ * opens that document in the studio. Returns `{}` outside preview.
48
+ *
49
+ * @example
50
+ * // {#each posts as post}
51
+ * // <a href="/blog/{post.slug}" {...ve.edit({ id: post.id, type: 'blog_post' })}>…</a>
52
+ */
53
+ edit(target: {
54
+ id: string;
55
+ type: string;
56
+ field?: string;
57
+ }): Record<string, string>;
40
58
  /**
41
59
  * Resolve an image field to `{ src, alt }` in one call — destructure it:
42
60
  * `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,CAAC,GAAG,CAAC,CAAC;IACxB;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC;IAC1E;;;;;OAKG;IACH,KAAK,CAAC,GAAG,EAAE,SAAS,GAAG,aAAa,CAAC;CACrC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,IAAI,UAAU,CA4BvC"}
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,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;;;;;;;;;OASG;IACH,IAAI,CAAC,MAAM,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACnF;;;;;OAKG;IACH,KAAK,CAAC,GAAG,EAAE,SAAS,GAAG,aAAa,CAAC;CACrC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,IAAI,UAAU,CAwCvC"}
@@ -32,7 +32,11 @@ export function usePreview() {
32
32
  get document() {
33
33
  return ctx.current;
34
34
  },
35
- live(fallback) {
35
+ live(fallback, options = {}) {
36
+ if (options.type && ctx.currentType !== options.type)
37
+ return fallback;
38
+ if (options.id && ctx.currentId !== options.id)
39
+ return fallback;
36
40
  return ctx.current ?? fallback;
37
41
  },
38
42
  encode(value, payload = {}) {
@@ -44,6 +48,17 @@ export function usePreview() {
44
48
  return raw; // nothing to navigate to
45
49
  return stegaEncode(raw || ' ', { ...payload, field });
46
50
  },
51
+ edit(target) {
52
+ const attrs = {};
53
+ if (ctx.current == null)
54
+ return attrs; // not in preview — no attributes
55
+ // `data-aphex-field` is what the overlay keys on to make an element clickable;
56
+ // the id/type route the click to that document (the studio opens it).
57
+ attrs['data-aphex-field'] = target.field ?? 'title';
58
+ attrs['data-aphex-document-id'] = target.id;
59
+ attrs['data-aphex-document-type'] = target.type;
60
+ return attrs;
61
+ },
47
62
  image(img) {
48
63
  return {
49
64
  src: img?.asset?.url ?? null, // injected at load (server) or into the live doc (editor)
package/package.json CHANGED
@@ -1,49 +1,49 @@
1
1
  {
2
- "name": "@aphexcms/visual-editing",
3
- "version": "0.1.1",
4
- "description": "Visual editing overlay for AphexCMS — add click-to-edit overlays to your frontend",
5
- "license": "MIT",
6
- "publishConfig": {
7
- "access": "public",
8
- "provenance": true
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "https://github.com/IcelandicIcecream/aphex.git",
13
- "directory": "packages/visual-editing"
14
- },
15
- "type": "module",
16
- "main": "./dist/index.js",
17
- "types": "./dist/index.d.ts",
18
- "svelte": "./dist/index.js",
19
- "exports": {
20
- ".": {
21
- "types": "./dist/index.d.ts",
22
- "svelte": "./dist/index.js",
23
- "default": "./dist/index.js"
24
- }
25
- },
26
- "files": [
27
- "dist"
28
- ],
29
- "scripts": {
30
- "dev": "tsc --watch --preserveWatchOutput",
31
- "build": "svelte-package",
32
- "check": "svelte-check --tsconfig ./tsconfig.json",
33
- "dev:local": "node ../../scripts/swap-package-paths.js ./package.json src lib",
34
- "prepack": "node ../../scripts/swap-package-paths.js ./package.json dist lib",
35
- "postpack": "node ../../scripts/swap-package-paths.js ./package.json src lib"
36
- },
37
- "devDependencies": {
38
- "@sveltejs/package": "^2.5.6",
39
- "svelte": "^5.0.0",
40
- "svelte-check": "^4.0.0",
41
- "typescript": "^5.9.3"
42
- },
43
- "peerDependencies": {
44
- "svelte": "^5.0.0"
45
- },
46
- "dependencies": {
47
- "@vercel/stega": "^1.1.0"
48
- }
49
- }
2
+ "name": "@aphexcms/visual-editing",
3
+ "version": "0.2.0",
4
+ "description": "Visual editing overlay for AphexCMS — add click-to-edit overlays to your frontend",
5
+ "license": "MIT",
6
+ "publishConfig": {
7
+ "access": "public",
8
+ "provenance": true
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/IcelandicIcecream/aphex.git",
13
+ "directory": "packages/visual-editing"
14
+ },
15
+ "type": "module",
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "svelte": "./dist/index.js",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "svelte": "./dist/index.js",
23
+ "default": "./dist/index.js"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "devDependencies": {
30
+ "@sveltejs/kit": "^2.59.1",
31
+ "@sveltejs/package": "^2.5.6",
32
+ "svelte": "^5.0.0",
33
+ "svelte-check": "^4.0.0",
34
+ "typescript": "^5.9.3"
35
+ },
36
+ "peerDependencies": {
37
+ "@sveltejs/kit": "^2.0.0",
38
+ "svelte": "^5.0.0"
39
+ },
40
+ "dependencies": {
41
+ "@vercel/stega": "^1.1.0"
42
+ },
43
+ "scripts": {
44
+ "dev": "tsc --watch --preserveWatchOutput",
45
+ "build": "svelte-package",
46
+ "check": "svelte-check --tsconfig ./tsconfig.json",
47
+ "dev:local": "node ../../scripts/swap-package-paths.js ./package.json src lib"
48
+ }
49
+ }