@orunelle/theme-runtime 0.0.0-stage → 2.0.0-alpha.14
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/application.d.ts +7 -0
- package/dist/application.d.ts.map +1 -0
- package/dist/application.js +58 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +429 -0
- package/dist/commerce.d.ts +235 -0
- package/dist/commerce.d.ts.map +1 -0
- package/dist/commerce.js +316 -0
- package/dist/credentials.d.ts +11 -0
- package/dist/credentials.d.ts.map +1 -0
- package/dist/credentials.js +79 -0
- package/dist/fixtures.d.ts +23 -0
- package/dist/fixtures.d.ts.map +1 -0
- package/dist/fixtures.js +131 -0
- package/dist/host/package.json +1 -0
- package/dist/host/preview/editor.css +116 -0
- package/dist/host/preview/editor.tsx +193 -0
- package/dist/host/preview/src/bridge.tsx +371 -0
- package/dist/host/preview/src/client.tsx +15 -0
- package/dist/host/preview/src/env.d.ts +5 -0
- package/dist/host/preview/src/preview.css +40 -0
- package/dist/host/preview/src/server.tsx +95 -0
- package/dist/host/preview/src/surface.tsx +264 -0
- package/dist/host/serve-node.mjs +133 -0
- package/dist/host/storefront/public/fonts/9da48a48bf6500f8-s.p.woff2 +0 -0
- package/dist/host/storefront/public/fonts/e4af272ccee01ff0-s.p.woff2 +0 -0
- package/dist/host/storefront/public/fonts/e4efb0298547fad0-s.p.woff2 +0 -0
- package/dist/host/storefront/public/fonts/f022b1629605b85c-s.p.woff2 +0 -0
- package/dist/host/storefront/public/fonts/fb66eb17bf011c3b-s.p.woff2 +0 -0
- package/dist/host/storefront/public/fonts.css +7 -0
- package/dist/host/storefront/public/samples/pouch-navy.svg +4 -0
- package/dist/host/storefront/public/samples/throw-oat.svg +2 -0
- package/dist/host/storefront/public/samples/throw-sage.svg +2 -0
- package/dist/host/storefront/public/samples/tote-charcoal.svg +5 -0
- package/dist/host/storefront/public/samples/tote-detail.svg +1 -0
- package/dist/host/storefront/public/samples/tote-sand.svg +5 -0
- package/dist/host/storefront/src/binding.server.ts +1 -0
- package/dist/host/storefront/src/data.ts +2 -0
- package/dist/host/storefront/src/env.d.ts +5 -0
- package/dist/host/storefront/src/queries.ts +1 -0
- package/dist/host/storefront/src/routeTree.gen.ts +155 -0
- package/dist/host/storefront/src/router.tsx +31 -0
- package/dist/host/storefront/src/routes/$locale.$.tsx +135 -0
- package/dist/host/storefront/src/routes/$locale.index.tsx +19 -0
- package/dist/host/storefront/src/routes/$locale.products.$handle.tsx +57 -0
- package/dist/host/storefront/src/routes/$locale.tsx +36 -0
- package/dist/host/storefront/src/routes/__root.tsx +103 -0
- package/dist/host/storefront/src/routes/index.tsx +6 -0
- package/dist/host/storefront/src/server.ts +1 -0
- package/dist/host.d.ts +45 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +380 -0
- package/dist/local-editor.d.ts +2 -0
- package/dist/local-editor.d.ts.map +1 -0
- package/dist/local-editor.js +111 -0
- package/dist/managed-worker.d.ts +2 -0
- package/dist/managed-worker.d.ts.map +1 -0
- package/dist/managed-worker.js +22 -0
- package/dist/server.d.ts +5 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +98 -0
- package/dist/source.d.ts +19 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +131 -0
- package/dist/tanstack/binding.server.d.ts +4 -0
- package/dist/tanstack/binding.server.d.ts.map +1 -0
- package/dist/tanstack/binding.server.js +9 -0
- package/dist/tanstack/data.d.ts +44 -0
- package/dist/tanstack/data.d.ts.map +1 -0
- package/dist/tanstack/data.js +320 -0
- package/dist/tanstack/index.d.ts +4 -0
- package/dist/tanstack/index.d.ts.map +1 -0
- package/dist/tanstack/index.js +2 -0
- package/dist/tanstack/queries.d.ts +6 -0
- package/dist/tanstack/queries.d.ts.map +1 -0
- package/dist/tanstack/queries.js +5 -0
- package/dist/template/app/client.tsx +3 -0
- package/dist/template/app/env.d.ts +1 -0
- package/dist/template/app/router.tsx +40 -0
- package/dist/template/app/routes/$locale.$.tsx +135 -0
- package/dist/template/app/routes/$locale.index.tsx +19 -0
- package/dist/template/app/routes/$locale.products.$handle.tsx +57 -0
- package/dist/template/app/routes/$locale.tsx +46 -0
- package/dist/template/app/routes/__root.tsx +103 -0
- package/dist/template/app/routes/index.tsx +6 -0
- package/dist/template/app/server.ts +1 -0
- package/dist/template/app/start.ts +2 -0
- package/dist/template/app/theme.ts +12 -0
- package/dist/template/tests/router-isolation.test.ts +14 -0
- package/dist/template/vite.config.ts +5 -0
- package/dist/template/vitest.config.ts +9 -0
- package/dist/vite.d.ts +7 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +22 -0
- package/package.json +88 -3
- package/README.md +0 -3
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
2
|
+
import {
|
|
3
|
+
boundedPreviewMessage,
|
|
4
|
+
previewConnectSchema,
|
|
5
|
+
previewHostMessageSchema,
|
|
6
|
+
samePreviewIdentity,
|
|
7
|
+
validPreviewSnapshot,
|
|
8
|
+
type PreviewBootstrap,
|
|
9
|
+
type PreviewFrameMessage,
|
|
10
|
+
type PreviewRect,
|
|
11
|
+
type PreviewView,
|
|
12
|
+
} from "@orunelle/theme-contract/preview";
|
|
13
|
+
|
|
14
|
+
export function usePreviewBridge(
|
|
15
|
+
bootstrap: PreviewBootstrap,
|
|
16
|
+
initialView: PreviewView,
|
|
17
|
+
) {
|
|
18
|
+
const [snapshot, setSnapshot] = useState({
|
|
19
|
+
configuration: bootstrap.configuration,
|
|
20
|
+
view: initialView,
|
|
21
|
+
sequence: 0,
|
|
22
|
+
inspect: true,
|
|
23
|
+
});
|
|
24
|
+
const current = useRef(snapshot);
|
|
25
|
+
current.current = snapshot;
|
|
26
|
+
const port = useRef<MessagePort | undefined>(undefined);
|
|
27
|
+
const channel = useRef({
|
|
28
|
+
sessionId: bootstrap.identity.sessionId,
|
|
29
|
+
documentId: "",
|
|
30
|
+
generation: 0,
|
|
31
|
+
});
|
|
32
|
+
const send = useRef((message: Record<string, unknown>) => {
|
|
33
|
+
port.current?.postMessage({
|
|
34
|
+
...message,
|
|
35
|
+
...channel.current,
|
|
36
|
+
sequence: current.current.sequence,
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
const [hover, setHover] = useState<{
|
|
40
|
+
nodeId: string;
|
|
41
|
+
rect: PreviewRect;
|
|
42
|
+
} | null>(null),
|
|
43
|
+
[selection, setSelection] = useState<string | null>(null),
|
|
44
|
+
[selectedRect, setSelectedRect] = useState<PreviewRect | null>(null),
|
|
45
|
+
[drop, setDrop] = useState<{ id: string; valid: boolean } | null>(null);
|
|
46
|
+
const hoverId = useRef<string | null>(null),
|
|
47
|
+
selectedId = useRef<string | null>(null),
|
|
48
|
+
dropId = useRef<string | null>(null);
|
|
49
|
+
hoverId.current = hover?.nodeId ?? null;
|
|
50
|
+
selectedId.current = selection;
|
|
51
|
+
dropId.current = drop?.id ?? null;
|
|
52
|
+
const measureRef = useRef(() => {}),
|
|
53
|
+
failedSequence = useRef(-1);
|
|
54
|
+
function renderFailed() {
|
|
55
|
+
failedSequence.current = current.current.sequence;
|
|
56
|
+
send.current({ type: "error", code: "render-error" });
|
|
57
|
+
}
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
const identity = bootstrap.identity;
|
|
60
|
+
function connect(event: MessageEvent) {
|
|
61
|
+
if (
|
|
62
|
+
!event.isTrusted ||
|
|
63
|
+
event.source !== window.parent ||
|
|
64
|
+
event.origin !== identity.editorOrigin ||
|
|
65
|
+
!boundedPreviewMessage(event.data)
|
|
66
|
+
)
|
|
67
|
+
return;
|
|
68
|
+
const parsed = previewConnectSchema.safeParse(event.data);
|
|
69
|
+
if (
|
|
70
|
+
!parsed.success ||
|
|
71
|
+
!samePreviewIdentity(parsed.data.identity, identity) ||
|
|
72
|
+
Date.now() >= identity.expiresAt ||
|
|
73
|
+
event.ports.length !== 1
|
|
74
|
+
)
|
|
75
|
+
return;
|
|
76
|
+
port.current?.close();
|
|
77
|
+
port.current = event.ports[0];
|
|
78
|
+
channel.current = {
|
|
79
|
+
sessionId: identity.sessionId,
|
|
80
|
+
documentId: parsed.data.documentId,
|
|
81
|
+
generation: parsed.data.generation,
|
|
82
|
+
};
|
|
83
|
+
current.current = { ...current.current, sequence: 0 };
|
|
84
|
+
port.current!.onmessage = (event) => {
|
|
85
|
+
if (Date.now() >= identity.expiresAt) {
|
|
86
|
+
send.current({ type: "error", code: "expired" });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (!boundedPreviewMessage(event.data)) return;
|
|
90
|
+
const result = previewHostMessageSchema.safeParse(event.data);
|
|
91
|
+
if (!result.success) return;
|
|
92
|
+
const message = result.data;
|
|
93
|
+
if (
|
|
94
|
+
message.sessionId !== channel.current.sessionId ||
|
|
95
|
+
message.documentId !== channel.current.documentId ||
|
|
96
|
+
message.generation !== channel.current.generation
|
|
97
|
+
)
|
|
98
|
+
return;
|
|
99
|
+
if (message.type === "snapshot") {
|
|
100
|
+
if (
|
|
101
|
+
message.sequence <= current.current.sequence ||
|
|
102
|
+
!validPreviewSnapshot(
|
|
103
|
+
bootstrap.manifest,
|
|
104
|
+
message.configuration,
|
|
105
|
+
identity.installationId,
|
|
106
|
+
) ||
|
|
107
|
+
!Object.hasOwn(
|
|
108
|
+
bootstrap.manifest.editor?.templates ?? {},
|
|
109
|
+
message.view.template,
|
|
110
|
+
)
|
|
111
|
+
) {
|
|
112
|
+
send.current({ type: "error", code: "invalid-snapshot" });
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const next = {
|
|
116
|
+
configuration: message.configuration,
|
|
117
|
+
view: message.view,
|
|
118
|
+
sequence: message.sequence,
|
|
119
|
+
inspect: message.inspect,
|
|
120
|
+
};
|
|
121
|
+
current.current = next;
|
|
122
|
+
setSnapshot(next);
|
|
123
|
+
} else if (
|
|
124
|
+
message.sequence === current.current.sequence &&
|
|
125
|
+
message.type === "selection"
|
|
126
|
+
) {
|
|
127
|
+
const known =
|
|
128
|
+
message.nodeId === null ||
|
|
129
|
+
current.current.configuration.nodes.some(
|
|
130
|
+
(node) => node.id === message.nodeId,
|
|
131
|
+
);
|
|
132
|
+
if (!known) return;
|
|
133
|
+
setSelection(message.nodeId);
|
|
134
|
+
if (message.scroll && message.nodeId)
|
|
135
|
+
requestAnimationFrame(() =>
|
|
136
|
+
[
|
|
137
|
+
...document.querySelectorAll<HTMLElement>(
|
|
138
|
+
"[data-orunelle-node-id]",
|
|
139
|
+
),
|
|
140
|
+
]
|
|
141
|
+
.find((node) => node.dataset.orunelleNodeId === message.nodeId)
|
|
142
|
+
?.scrollIntoView({ block: "center", behavior: "instant" }),
|
|
143
|
+
);
|
|
144
|
+
measureRef.current();
|
|
145
|
+
} else if (
|
|
146
|
+
message.sequence === current.current.sequence &&
|
|
147
|
+
message.type === "drop-target"
|
|
148
|
+
) {
|
|
149
|
+
setDrop(
|
|
150
|
+
message.nodeId
|
|
151
|
+
? { id: message.nodeId, valid: message.valid }
|
|
152
|
+
: null,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
send.current({
|
|
157
|
+
type: "ready",
|
|
158
|
+
capabilities: ["inspect", "geometry", "snapshots", "drag", "navigate"],
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
window.addEventListener("message", connect);
|
|
162
|
+
window.parent.postMessage(
|
|
163
|
+
{ type: "orunelle.preview.boot", identity },
|
|
164
|
+
identity.editorOrigin,
|
|
165
|
+
);
|
|
166
|
+
const expiry = setTimeout(
|
|
167
|
+
() => send.current({ type: "error", code: "expired" }),
|
|
168
|
+
Math.max(0, identity.expiresAt - Date.now()),
|
|
169
|
+
);
|
|
170
|
+
return () => {
|
|
171
|
+
window.removeEventListener("message", connect);
|
|
172
|
+
clearTimeout(expiry);
|
|
173
|
+
port.current?.close();
|
|
174
|
+
};
|
|
175
|
+
}, [bootstrap]);
|
|
176
|
+
useLayoutEffect(() => {
|
|
177
|
+
const frame = requestAnimationFrame(() => {
|
|
178
|
+
if (snapshot.sequence && failedSequence.current !== snapshot.sequence)
|
|
179
|
+
send.current({ type: "ack" });
|
|
180
|
+
measureRef.current();
|
|
181
|
+
});
|
|
182
|
+
return () => cancelAnimationFrame(frame);
|
|
183
|
+
}, [snapshot]);
|
|
184
|
+
useEffect(() => {
|
|
185
|
+
let pending = 0,
|
|
186
|
+
hoverPending = 0,
|
|
187
|
+
lastHover: string | null = null,
|
|
188
|
+
hoverTarget: EventTarget | null = null;
|
|
189
|
+
const registered = (target: EventTarget | null) =>
|
|
190
|
+
target instanceof Element
|
|
191
|
+
? target.closest<HTMLElement>("[data-orunelle-node-id]")
|
|
192
|
+
: null;
|
|
193
|
+
const rect = (node: Element): PreviewRect => {
|
|
194
|
+
const r = node.getBoundingClientRect();
|
|
195
|
+
return { x: r.x, y: r.y, width: r.width, height: r.height };
|
|
196
|
+
};
|
|
197
|
+
function measure() {
|
|
198
|
+
if (pending) return;
|
|
199
|
+
pending = requestAnimationFrame(() => {
|
|
200
|
+
pending = 0;
|
|
201
|
+
const known = new Set(
|
|
202
|
+
current.current.configuration.nodes.map((node) => node.id),
|
|
203
|
+
);
|
|
204
|
+
const nodes = [
|
|
205
|
+
...document.querySelectorAll<HTMLElement>("[data-orunelle-node-id]"),
|
|
206
|
+
].filter((node) => known.has(node.dataset.orunelleNodeId!));
|
|
207
|
+
const geometry = nodes
|
|
208
|
+
.map((node) => ({
|
|
209
|
+
nodeId: node.dataset.orunelleNodeId!,
|
|
210
|
+
rect: rect(node),
|
|
211
|
+
}))
|
|
212
|
+
.filter(
|
|
213
|
+
(item) =>
|
|
214
|
+
item.rect.width > 0 &&
|
|
215
|
+
item.rect.height > 0 &&
|
|
216
|
+
Math.abs(item.rect.x) < 1000000 &&
|
|
217
|
+
Math.abs(item.rect.y) < 1000000 &&
|
|
218
|
+
item.rect.width < 100000 &&
|
|
219
|
+
item.rect.height < 100000,
|
|
220
|
+
);
|
|
221
|
+
send.current({ type: "geometry", nodes: geometry });
|
|
222
|
+
const selected = geometry.find(
|
|
223
|
+
(item) => item.nodeId === selectedId.current,
|
|
224
|
+
);
|
|
225
|
+
setSelectedRect(selected?.rect ?? null);
|
|
226
|
+
const hovered = geometry.find(
|
|
227
|
+
(item) => item.nodeId === hoverId.current,
|
|
228
|
+
);
|
|
229
|
+
if (hovered) setHover(hovered);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
measureRef.current = measure;
|
|
233
|
+
function hoverEvent(event: MouseEvent) {
|
|
234
|
+
hoverTarget = event.target;
|
|
235
|
+
if (hoverPending) return;
|
|
236
|
+
hoverPending = requestAnimationFrame(() => {
|
|
237
|
+
hoverPending = 0;
|
|
238
|
+
if (!current.current.inspect) return;
|
|
239
|
+
const node = registered(hoverTarget);
|
|
240
|
+
const id = node?.dataset.orunelleNodeId ?? null;
|
|
241
|
+
if (id === lastHover) return;
|
|
242
|
+
lastHover = id;
|
|
243
|
+
const value = node && id ? { nodeId: id, rect: rect(node) } : null;
|
|
244
|
+
setHover(value);
|
|
245
|
+
send.current({ type: "hover", nodeId: id, rect: value?.rect ?? null });
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
function click(event: MouseEvent) {
|
|
249
|
+
if (current.current.inspect) {
|
|
250
|
+
event.preventDefault();
|
|
251
|
+
event.stopImmediatePropagation();
|
|
252
|
+
const node = registered(event.target);
|
|
253
|
+
const id = node?.dataset.orunelleNodeId;
|
|
254
|
+
setSelection(id ?? null);
|
|
255
|
+
send.current({ type: "select", nodeId: id ?? null });
|
|
256
|
+
measure();
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const anchor =
|
|
260
|
+
event.target instanceof Element ? event.target.closest("a") : null;
|
|
261
|
+
if (anchor) {
|
|
262
|
+
event.preventDefault();
|
|
263
|
+
event.stopImmediatePropagation();
|
|
264
|
+
const url = new URL(anchor.href, bootstrap.storefront.origin);
|
|
265
|
+
if (url.origin !== bootstrap.storefront.origin) {
|
|
266
|
+
send.current({ type: "error", code: "unsupported-navigation" });
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
send.current({ type: "navigate", path: url.pathname + url.search });
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const prevent = (event: Event) => {
|
|
273
|
+
event.preventDefault();
|
|
274
|
+
event.stopImmediatePropagation();
|
|
275
|
+
};
|
|
276
|
+
document.addEventListener("mousemove", hoverEvent, { passive: true });
|
|
277
|
+
document.addEventListener("click", click, true);
|
|
278
|
+
document.addEventListener("submit", prevent, true);
|
|
279
|
+
window.addEventListener("scroll", measure, {
|
|
280
|
+
passive: true,
|
|
281
|
+
capture: true,
|
|
282
|
+
});
|
|
283
|
+
window.addEventListener("resize", measure);
|
|
284
|
+
document.addEventListener("load", measure, true);
|
|
285
|
+
const observer = new MutationObserver((records) => {
|
|
286
|
+
if (
|
|
287
|
+
records.some(
|
|
288
|
+
(record) =>
|
|
289
|
+
!(
|
|
290
|
+
record.target instanceof Element &&
|
|
291
|
+
record.target.closest("[data-preview-overlay]")
|
|
292
|
+
),
|
|
293
|
+
)
|
|
294
|
+
)
|
|
295
|
+
measure();
|
|
296
|
+
});
|
|
297
|
+
observer.observe(document.getElementById("root")!, {
|
|
298
|
+
subtree: true,
|
|
299
|
+
attributes: true,
|
|
300
|
+
childList: true,
|
|
301
|
+
characterData: true,
|
|
302
|
+
});
|
|
303
|
+
const resize = new ResizeObserver(measure);
|
|
304
|
+
resize.observe(document.body);
|
|
305
|
+
void document.fonts.ready.then(measure);
|
|
306
|
+
measure();
|
|
307
|
+
return () => {
|
|
308
|
+
if (pending) cancelAnimationFrame(pending);
|
|
309
|
+
if (hoverPending) cancelAnimationFrame(hoverPending);
|
|
310
|
+
observer.disconnect();
|
|
311
|
+
resize.disconnect();
|
|
312
|
+
document.removeEventListener("mousemove", hoverEvent);
|
|
313
|
+
document.removeEventListener("click", click, true);
|
|
314
|
+
document.removeEventListener("submit", prevent, true);
|
|
315
|
+
window.removeEventListener("scroll", measure, true);
|
|
316
|
+
window.removeEventListener("resize", measure);
|
|
317
|
+
document.removeEventListener("load", measure, true);
|
|
318
|
+
};
|
|
319
|
+
}, [bootstrap]);
|
|
320
|
+
function outline(id: string, rectangle: PreviewRect, kind: string) {
|
|
321
|
+
const type = current.current.configuration.nodes.find(
|
|
322
|
+
(node) => node.id === id,
|
|
323
|
+
)?.type;
|
|
324
|
+
const label = type
|
|
325
|
+
? (bootstrap.manifest.editor?.nodeTypes[type]?.label[
|
|
326
|
+
current.current.view.locale
|
|
327
|
+
] ?? type)
|
|
328
|
+
: id;
|
|
329
|
+
return (
|
|
330
|
+
<div
|
|
331
|
+
key={kind}
|
|
332
|
+
className={`preview-outline preview-outline-${kind}`}
|
|
333
|
+
style={{
|
|
334
|
+
left: rectangle.x,
|
|
335
|
+
top: rectangle.y,
|
|
336
|
+
width: rectangle.width,
|
|
337
|
+
height: rectangle.height,
|
|
338
|
+
}}
|
|
339
|
+
>
|
|
340
|
+
<span>{label}</span>
|
|
341
|
+
</div>
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
const dropRect = drop
|
|
345
|
+
? [...document.querySelectorAll<HTMLElement>("[data-orunelle-node-id]")]
|
|
346
|
+
.find((node) => node.dataset.orunelleNodeId === drop.id)
|
|
347
|
+
?.getBoundingClientRect()
|
|
348
|
+
: undefined;
|
|
349
|
+
const overlay = (
|
|
350
|
+
<div data-preview-overlay aria-hidden="true">
|
|
351
|
+
{snapshot.inspect && hover && outline(hover.nodeId, hover.rect, "hover")}
|
|
352
|
+
{snapshot.inspect &&
|
|
353
|
+
selection &&
|
|
354
|
+
selectedRect &&
|
|
355
|
+
outline(selection, selectedRect, "selected")}
|
|
356
|
+
{drop &&
|
|
357
|
+
dropRect &&
|
|
358
|
+
outline(
|
|
359
|
+
drop.id,
|
|
360
|
+
{
|
|
361
|
+
x: dropRect.x,
|
|
362
|
+
y: dropRect.y,
|
|
363
|
+
width: dropRect.width,
|
|
364
|
+
height: dropRect.height,
|
|
365
|
+
},
|
|
366
|
+
drop.valid ? "drop" : "rejected",
|
|
367
|
+
)}
|
|
368
|
+
</div>
|
|
369
|
+
);
|
|
370
|
+
return { snapshot, overlay, renderFailed };
|
|
371
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { hydrateRoot } from "react-dom/client";
|
|
2
|
+
import { makePreviewRouter, PreviewDocument } from "./surface";
|
|
3
|
+
import type { PreviewBootstrap } from "@orunelle/theme-contract/preview";
|
|
4
|
+
import "@orunelle/theme-engine/styles.css";
|
|
5
|
+
import "../../storefront/public/fonts.css";
|
|
6
|
+
import "./preview.css";
|
|
7
|
+
const bootstrap: PreviewBootstrap = JSON.parse(
|
|
8
|
+
document.getElementById("orunelle-preview-data")!.textContent!,
|
|
9
|
+
);
|
|
10
|
+
const router = makePreviewRouter(bootstrap);
|
|
11
|
+
await router.load();
|
|
12
|
+
hydrateRoot(
|
|
13
|
+
document.getElementById("root")!,
|
|
14
|
+
<PreviewDocument router={router} bootstrap={bootstrap} />,
|
|
15
|
+
);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
.preview-outline {
|
|
2
|
+
position: fixed;
|
|
3
|
+
z-index: 2147483640;
|
|
4
|
+
border: 2px solid #7042b0;
|
|
5
|
+
pointer-events: none;
|
|
6
|
+
box-sizing: border-box;
|
|
7
|
+
}
|
|
8
|
+
.preview-outline span {
|
|
9
|
+
position: absolute;
|
|
10
|
+
inset-inline-start: -2px;
|
|
11
|
+
top: 0;
|
|
12
|
+
transform: translateY(-100%);
|
|
13
|
+
background: #7042b0;
|
|
14
|
+
color: #fff;
|
|
15
|
+
padding: 0.25rem 0.5rem;
|
|
16
|
+
font:
|
|
17
|
+
500 12px/1.4 Inter,
|
|
18
|
+
"IBM Plex Sans Arabic",
|
|
19
|
+
sans-serif;
|
|
20
|
+
max-width: min(240px, 90vw);
|
|
21
|
+
overflow: hidden;
|
|
22
|
+
text-overflow: ellipsis;
|
|
23
|
+
white-space: nowrap;
|
|
24
|
+
}
|
|
25
|
+
.preview-outline-selected {
|
|
26
|
+
border-width: 3px;
|
|
27
|
+
}
|
|
28
|
+
.preview-outline-hover {
|
|
29
|
+
border-style: dashed;
|
|
30
|
+
}
|
|
31
|
+
.preview-outline-rejected {
|
|
32
|
+
border-color: #6a6074;
|
|
33
|
+
border-style: dashed;
|
|
34
|
+
}
|
|
35
|
+
.theme-preview-surface[data-font="display"] .theme-copy {
|
|
36
|
+
font-weight: 600;
|
|
37
|
+
}
|
|
38
|
+
[data-orunelle-node-id]:empty {
|
|
39
|
+
min-height: 1px;
|
|
40
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { renderToString } from "react-dom/server";
|
|
3
|
+
import {
|
|
4
|
+
validateConfiguration,
|
|
5
|
+
validateManifest,
|
|
6
|
+
} from "@orunelle/theme-contract";
|
|
7
|
+
import {
|
|
8
|
+
previewIdentitySchema,
|
|
9
|
+
canonicalPreviewJson,
|
|
10
|
+
type PreviewBootstrap,
|
|
11
|
+
} from "@orunelle/theme-contract/preview";
|
|
12
|
+
import definition from "virtual:orunelle/theme";
|
|
13
|
+
import { makePreviewRouter, PreviewDocument } from "./surface";
|
|
14
|
+
|
|
15
|
+
export const manifest = definition.manifest;
|
|
16
|
+
export const defaults = definition.defaults;
|
|
17
|
+
function matches(a: string, b: string) {
|
|
18
|
+
return (
|
|
19
|
+
a.length === b.length && timingSafeEqual(Buffer.from(a), Buffer.from(b))
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
export default {
|
|
23
|
+
async fetch(request: Request) {
|
|
24
|
+
const deny = () =>
|
|
25
|
+
new Response("Preview unavailable", {
|
|
26
|
+
status: 403,
|
|
27
|
+
headers: { "cache-control": "private, no-store" },
|
|
28
|
+
});
|
|
29
|
+
if (
|
|
30
|
+
request.method !== "POST" ||
|
|
31
|
+
new URL(request.url).pathname !== "/__theme-preview/document" ||
|
|
32
|
+
!process.env.ORUNELLE_PREVIEW_GATEWAY_SECRET ||
|
|
33
|
+
!matches(
|
|
34
|
+
request.headers.get("x-orunelle-preview-key") ?? "",
|
|
35
|
+
process.env.ORUNELLE_PREVIEW_GATEWAY_SECRET,
|
|
36
|
+
)
|
|
37
|
+
)
|
|
38
|
+
return deny();
|
|
39
|
+
const bytes = await request.arrayBuffer();
|
|
40
|
+
if (bytes.byteLength > 2 * 1024 * 1024) return deny();
|
|
41
|
+
let bootstrap: PreviewBootstrap;
|
|
42
|
+
try {
|
|
43
|
+
bootstrap = JSON.parse(new TextDecoder().decode(bytes));
|
|
44
|
+
} catch {
|
|
45
|
+
return deny();
|
|
46
|
+
}
|
|
47
|
+
if (
|
|
48
|
+
!previewIdentitySchema.safeParse(bootstrap.identity).success ||
|
|
49
|
+
Date.now() >= bootstrap.identity.expiresAt ||
|
|
50
|
+
validateManifest(bootstrap.manifest) ||
|
|
51
|
+
canonicalPreviewJson(bootstrap.manifest) !==
|
|
52
|
+
canonicalPreviewJson(manifest) ||
|
|
53
|
+
validateConfiguration(
|
|
54
|
+
manifest,
|
|
55
|
+
bootstrap.configuration,
|
|
56
|
+
bootstrap.identity.installationId,
|
|
57
|
+
) ||
|
|
58
|
+
bootstrap.identity.artifactDigest !==
|
|
59
|
+
request.headers.get("x-orunelle-artifact-digest")
|
|
60
|
+
)
|
|
61
|
+
return deny();
|
|
62
|
+
const router = makePreviewRouter(bootstrap);
|
|
63
|
+
await router.load();
|
|
64
|
+
const rendered = renderToString(
|
|
65
|
+
<PreviewDocument router={router} bootstrap={bootstrap} />,
|
|
66
|
+
);
|
|
67
|
+
const metadata: {
|
|
68
|
+
entry: string;
|
|
69
|
+
styles: string[];
|
|
70
|
+
artifactDigest: string;
|
|
71
|
+
} = JSON.parse(process.env.ORUNELLE_PREVIEW_ARTIFACT!);
|
|
72
|
+
if (metadata.artifactDigest !== bootstrap.identity.artifactDigest)
|
|
73
|
+
return deny();
|
|
74
|
+
const json = JSON.stringify(bootstrap)
|
|
75
|
+
.replace(/</g, "\\u003c")
|
|
76
|
+
.replace(/\u2028/g, "\\u2028")
|
|
77
|
+
.replace(/\u2029/g, "\\u2029");
|
|
78
|
+
const styles = metadata.styles
|
|
79
|
+
.map(
|
|
80
|
+
(path) =>
|
|
81
|
+
`<link rel="stylesheet" href="${bootstrap.assetBase + path}">`,
|
|
82
|
+
)
|
|
83
|
+
.join("");
|
|
84
|
+
return new Response(
|
|
85
|
+
`<!doctype html><html lang="en" dir="ltr"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">${styles}</head><body><div id="root">${rendered}</div><script type="application/json" id="orunelle-preview-data">${json}</script><script type="module" crossorigin="anonymous" src="${bootstrap.assetBase + metadata.entry}"></script></body></html>`,
|
|
86
|
+
{
|
|
87
|
+
headers: {
|
|
88
|
+
"content-type": "text/html; charset=utf-8",
|
|
89
|
+
"cache-control": "private, no-store",
|
|
90
|
+
"x-orunelle-artifact-digest": metadata.artifactDigest,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
);
|
|
94
|
+
},
|
|
95
|
+
};
|