@aphexcms/visual-editing 0.1.2 → 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/dist/AphexVisualOverlay.svelte +8 -2
- package/dist/AphexVisualOverlay.svelte.d.ts.map +1 -1
- package/dist/core.d.ts +11 -14
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +74 -11
- package/dist/live-preview.svelte.d.ts +2 -0
- package/dist/live-preview.svelte.d.ts.map +1 -1
- package/dist/live-preview.svelte.js +2 -0
- package/dist/stega.d.ts +8 -0
- package/dist/stega.d.ts.map +1 -1
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/use-preview.svelte.d.ts +19 -1
- package/dist/use-preview.svelte.d.ts.map +1 -1
- package/dist/use-preview.svelte.js +16 -1
- package/package.json +3 -1
|
@@ -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;
|
|
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
|
|
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
|
package/dist/core.d.ts.map
CHANGED
|
@@ -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,
|
|
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
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
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
|
|
@@ -131,11 +179,16 @@ export function enableAphexPreview(options = {}) {
|
|
|
131
179
|
hide();
|
|
132
180
|
};
|
|
133
181
|
const onClick = (e) => {
|
|
134
|
-
//
|
|
135
|
-
|
|
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();
|
|
136
189
|
if (!editMode)
|
|
137
190
|
return;
|
|
138
|
-
const el =
|
|
191
|
+
const el = target.closest('[data-aphex-field]');
|
|
139
192
|
if (el?.dataset.aphexField) {
|
|
140
193
|
e.stopPropagation();
|
|
141
194
|
const d = el.dataset;
|
|
@@ -151,7 +204,9 @@ export function enableAphexPreview(options = {}) {
|
|
|
151
204
|
blockKey: d.aphexBlockKey ?? undefined,
|
|
152
205
|
arrayIndex: d.aphexArrayIndex != null ? parseInt(d.aphexArrayIndex, 10) : undefined,
|
|
153
206
|
objectPath: d.aphexObjectPath ?? undefined,
|
|
154
|
-
linkHref
|
|
207
|
+
linkHref,
|
|
208
|
+
documentId: d.aphexDocumentId ?? undefined,
|
|
209
|
+
documentType: d.aphexDocumentType ?? undefined
|
|
155
210
|
}, '*');
|
|
156
211
|
}
|
|
157
212
|
};
|
|
@@ -162,7 +217,7 @@ export function enableAphexPreview(options = {}) {
|
|
|
162
217
|
const onMessage = (e) => {
|
|
163
218
|
if (!e.data || typeof e.data !== 'object')
|
|
164
219
|
return;
|
|
165
|
-
const { type, fieldPath, document: doc } = e.data;
|
|
220
|
+
const { type, fieldPath, document: doc, documentType, documentId } = e.data;
|
|
166
221
|
if (type === 'aphex:edit-mode') {
|
|
167
222
|
editMode = e.data.enabled;
|
|
168
223
|
if (!editMode) {
|
|
@@ -174,10 +229,18 @@ export function enableAphexPreview(options = {}) {
|
|
|
174
229
|
}
|
|
175
230
|
}
|
|
176
231
|
else if (type === 'aphex:data' && doc) {
|
|
177
|
-
onData?.(doc);
|
|
232
|
+
onData?.(doc, { documentType, documentId });
|
|
178
233
|
// Re-scan after the framework re-renders with the new stega-encoded values
|
|
179
234
|
requestAnimationFrame(scanStega);
|
|
180
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
|
+
}
|
|
181
244
|
else if (type === 'aphex:field-focus' && fieldPath) {
|
|
182
245
|
const el = document.querySelector(`[data-aphex-field="${fieldPath}"]`);
|
|
183
246
|
if (!el)
|
|
@@ -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;
|
|
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.
|
package/dist/stega.d.ts.map
CHANGED
|
@@ -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;
|
|
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;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -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;
|
|
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
|
|
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;
|
|
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aphexcms/visual-editing",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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": {
|