@weaverse/next 0.1.0-alpha.0 → 0.1.0-alpha.2
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/README.md +165 -0
- package/dist/chunk-SS6QUKYY.mjs +149 -0
- package/dist/chunk-SS6QUKYY.mjs.map +1 -0
- package/dist/index.d.mts +145 -160
- package/dist/index.d.ts +145 -160
- package/dist/index.js +477 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +407 -108
- package/dist/index.mjs.map +1 -1
- package/dist/server.d.mts +60 -0
- package/dist/server.d.ts +60 -0
- package/dist/server.js +662 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +513 -0
- package/dist/server.mjs.map +1 -0
- package/dist/types-DvEeLX6L.d.mts +321 -0
- package/dist/types-DvEeLX6L.d.ts +321 -0
- package/package.json +22 -3
package/dist/index.mjs
CHANGED
|
@@ -1,31 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { buildWeaverseNextRequestInfo, generateDataFromSchema } from './chunk-SS6QUKYY.mjs';
|
|
2
|
+
export { buildWeaverseNextRequestInfo, runWeaverseComponentLoaders } from './chunk-SS6QUKYY.mjs';
|
|
3
|
+
import { WeaverseRoot, Weaverse, WeaverseItemStore } from '@weaverse/react';
|
|
2
4
|
export { isBrowser, isIframe, useChildInstances, useItemInstance, useParentInstance, useWeaverse } from '@weaverse/react';
|
|
3
5
|
import { createSchema } from '@weaverse/schema';
|
|
4
6
|
export { createSchema } from '@weaverse/schema';
|
|
5
|
-
import { jsx } from 'react/jsx-runtime';
|
|
6
|
-
import { createContext, memo, useContext, useMemo } from 'react';
|
|
7
|
+
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
8
|
+
import { createContext, memo, useContext, useMemo, useRef, useEffect, useSyncExternalStore } from 'react';
|
|
9
|
+
import { useRouter } from 'next/navigation';
|
|
7
10
|
|
|
8
|
-
// src/index.ts
|
|
9
|
-
|
|
10
|
-
// src/utils.ts
|
|
11
|
-
function generateDataFromSchema(schema) {
|
|
12
|
-
let data = {};
|
|
13
|
-
if (!schema) {
|
|
14
|
-
return data;
|
|
15
|
-
}
|
|
16
|
-
let groups = schema.settings ?? schema.inspector ?? [];
|
|
17
|
-
for (let group of groups) {
|
|
18
|
-
for (let input of group?.inputs ?? []) {
|
|
19
|
-
let { name, defaultValue } = input;
|
|
20
|
-
if (name && defaultValue !== null && defaultValue !== void 0) {
|
|
21
|
-
data[name] = defaultValue;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return data;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// src/item.ts
|
|
29
11
|
var WeaverseNextItem = class extends WeaverseItemStore {
|
|
30
12
|
constructor(initialData, weaverse) {
|
|
31
13
|
super(initialData, weaverse);
|
|
@@ -132,18 +114,53 @@ var NextClient = class {
|
|
|
132
114
|
function createWeaverseNextClient(config) {
|
|
133
115
|
return new NextClient(config);
|
|
134
116
|
}
|
|
117
|
+
|
|
118
|
+
// src/theme-settings-store.ts
|
|
119
|
+
function createWeaverseNextThemeSettingsStore(options = {}) {
|
|
120
|
+
let listeners = /* @__PURE__ */ new Set();
|
|
121
|
+
let store = {
|
|
122
|
+
publicEnv: options.publicEnv,
|
|
123
|
+
schema: options.schema,
|
|
124
|
+
settings: { ...options.settings ?? {} },
|
|
125
|
+
getServerSnapshot: () => store.settings,
|
|
126
|
+
getSnapshot: () => store.settings,
|
|
127
|
+
subscribe: (listener) => {
|
|
128
|
+
listeners.add(listener);
|
|
129
|
+
return () => {
|
|
130
|
+
listeners.delete(listener);
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
updateThemeSettings: (next) => {
|
|
134
|
+
store.settings = { ...store.settings, ...next };
|
|
135
|
+
for (let listener of listeners) {
|
|
136
|
+
listener();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
return store;
|
|
141
|
+
}
|
|
142
|
+
var EMPTY_THEME_SETTINGS = {};
|
|
135
143
|
var WeaverseNextContext = createContext(null);
|
|
136
144
|
function WeaverseNextProvider(props) {
|
|
137
145
|
let { children, client, rootData, pageData, commerce, themeSettings } = props;
|
|
146
|
+
let themeSettingsValue = themeSettings ?? client?.themeSettings ?? EMPTY_THEME_SETTINGS;
|
|
147
|
+
let themeSettingsStore = useMemo(
|
|
148
|
+
() => createWeaverseNextThemeSettingsStore({
|
|
149
|
+
schema: client?.themeSchema,
|
|
150
|
+
settings: themeSettingsValue
|
|
151
|
+
}),
|
|
152
|
+
[client?.themeSchema, themeSettingsValue]
|
|
153
|
+
);
|
|
138
154
|
let value = useMemo(
|
|
139
155
|
() => ({
|
|
140
156
|
client,
|
|
141
157
|
rootData,
|
|
142
158
|
pageData,
|
|
143
159
|
commerce: commerce ?? client?.commerce,
|
|
144
|
-
themeSettings:
|
|
160
|
+
themeSettings: themeSettingsStore.getSnapshot(),
|
|
161
|
+
themeSettingsStore
|
|
145
162
|
}),
|
|
146
|
-
[client, rootData, pageData, commerce,
|
|
163
|
+
[client, rootData, pageData, commerce, themeSettingsStore]
|
|
147
164
|
);
|
|
148
165
|
return /* @__PURE__ */ jsx(WeaverseNextContext.Provider, { value, children });
|
|
149
166
|
}
|
|
@@ -167,87 +184,263 @@ function useWeaverseCommerce() {
|
|
|
167
184
|
return useWeaverseNextContext("useWeaverseCommerce").commerce;
|
|
168
185
|
}
|
|
169
186
|
function useThemeSettings() {
|
|
170
|
-
|
|
187
|
+
let { themeSettingsStore } = useWeaverseNextContext("useThemeSettings");
|
|
188
|
+
return useSyncExternalStore(
|
|
189
|
+
themeSettingsStore.subscribe,
|
|
190
|
+
themeSettingsStore.getSnapshot,
|
|
191
|
+
themeSettingsStore.getServerSnapshot
|
|
192
|
+
);
|
|
171
193
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
function isItemNode(node) {
|
|
175
|
-
return typeof node === "object" && node !== null && typeof node.type === "string";
|
|
194
|
+
function getRuntimeWindow() {
|
|
195
|
+
return typeof window === "undefined" ? void 0 : window;
|
|
176
196
|
}
|
|
177
|
-
function
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
(child) => isItemNode(child) ? cloneItem(child) : { ...child }
|
|
182
|
-
) : item.children
|
|
183
|
-
};
|
|
197
|
+
function getRenderablePage(data) {
|
|
198
|
+
let page = data.page;
|
|
199
|
+
let rootId = page.rootId ?? page.items.find((item) => item.type === "main")?.id ?? page.items[0]?.id ?? page.id;
|
|
200
|
+
return { ...page, rootId };
|
|
184
201
|
}
|
|
185
|
-
|
|
186
|
-
let
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
202
|
+
function getConfigString(configs, key) {
|
|
203
|
+
let value = configs?.[key];
|
|
204
|
+
return typeof value === "string" ? value : void 0;
|
|
205
|
+
}
|
|
206
|
+
function getRecordString(record, key) {
|
|
207
|
+
let value = record?.[key];
|
|
208
|
+
return typeof value === "string" ? value : void 0;
|
|
209
|
+
}
|
|
210
|
+
function resolveProjectId(client, data, fallback) {
|
|
211
|
+
let projectId = client?.projectId;
|
|
212
|
+
if (projectId) {
|
|
213
|
+
return projectId;
|
|
190
214
|
}
|
|
191
|
-
|
|
192
|
-
|
|
215
|
+
let configProjectId = getConfigString(data.configs, "projectId");
|
|
216
|
+
if (configProjectId) {
|
|
217
|
+
return configProjectId;
|
|
193
218
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
219
|
+
return getRecordString(data.project, "id") ?? fallback;
|
|
220
|
+
}
|
|
221
|
+
function resolveDataContext(config) {
|
|
222
|
+
if (config.dataContext) {
|
|
223
|
+
return config.dataContext;
|
|
224
|
+
}
|
|
225
|
+
if (config.client?.dataContext) {
|
|
226
|
+
return config.client.dataContext;
|
|
227
|
+
}
|
|
228
|
+
return {};
|
|
229
|
+
}
|
|
230
|
+
function getRuntimeKey(pageId, requestInfo) {
|
|
231
|
+
return `${pageId}:${requestInfo.pathname}:${requestInfo.search}`;
|
|
232
|
+
}
|
|
233
|
+
function registerRuntime(runtime) {
|
|
234
|
+
let runtimeWindow = getRuntimeWindow();
|
|
235
|
+
if (!runtimeWindow) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
runtimeWindow.__weaverses ??= {};
|
|
239
|
+
runtimeWindow.__weaverses[runtime.pageId] = runtime;
|
|
240
|
+
runtimeWindow.__weaverse = runtime;
|
|
241
|
+
}
|
|
242
|
+
var WeaverseNextRuntime = class extends Weaverse {
|
|
243
|
+
pageId;
|
|
244
|
+
internal;
|
|
245
|
+
requestInfo;
|
|
246
|
+
isPreviewMode;
|
|
247
|
+
isRevisionPreview;
|
|
248
|
+
sectionType;
|
|
249
|
+
translationMap = {};
|
|
250
|
+
translationLocale = "";
|
|
251
|
+
translationLanguageId = "";
|
|
252
|
+
constructor(config) {
|
|
253
|
+
let { client, data } = config;
|
|
254
|
+
let page = getRenderablePage(data);
|
|
255
|
+
let configs = data.configs;
|
|
256
|
+
let requestContext = client?.requestContext;
|
|
257
|
+
let projectId = resolveProjectId(client, data, page.id);
|
|
258
|
+
ensureNextItemConstructor();
|
|
259
|
+
super({
|
|
260
|
+
projectId,
|
|
261
|
+
data: page,
|
|
262
|
+
isDesignMode: requestContext?.isDesignMode ?? false,
|
|
263
|
+
weaverseHost: getConfigString(configs, "weaverseHost"),
|
|
264
|
+
weaverseVersion: getConfigString(configs, "weaverseVersion")
|
|
265
|
+
});
|
|
266
|
+
this.pageId = page.id;
|
|
267
|
+
this.dataContext = resolveDataContext(config);
|
|
268
|
+
this.requestInfo = buildWeaverseNextRequestInfo(requestContext);
|
|
269
|
+
this.isDesignMode = requestContext?.isDesignMode ?? false;
|
|
270
|
+
this.isPreviewMode = requestContext?.isPreviewMode ?? false;
|
|
271
|
+
this.isRevisionPreview = requestContext?.isRevisionPreview ?? false;
|
|
272
|
+
this.sectionType = requestContext?.sectionType;
|
|
273
|
+
this.internal = {
|
|
274
|
+
navigate: config.navigate,
|
|
275
|
+
pageAssignment: data.pageAssignment,
|
|
276
|
+
project: data.project,
|
|
277
|
+
revalidate: config.revalidate,
|
|
278
|
+
themeSettingsStore: config.themeSettingsStore ?? createWeaverseNextThemeSettingsStore({
|
|
279
|
+
schema: client?.themeSchema,
|
|
280
|
+
settings: client?.themeSettings
|
|
281
|
+
})
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
extractTranslationSidecar = () => void 0;
|
|
285
|
+
setTranslationSidecar = (map = {}, locale = "", languageId = "") => {
|
|
286
|
+
this.translationMap = map;
|
|
287
|
+
this.translationLocale = locale;
|
|
288
|
+
this.translationLanguageId = languageId;
|
|
200
289
|
};
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
290
|
+
updateTranslation = () => void 0;
|
|
291
|
+
getTranslationChanges = () => void 0;
|
|
292
|
+
};
|
|
293
|
+
function createWeaverseNextRuntime(config) {
|
|
294
|
+
let requestInfo = buildWeaverseNextRequestInfo(config.client?.requestContext);
|
|
295
|
+
let page = getRenderablePage(config.data);
|
|
296
|
+
let runtimeWindow = getRuntimeWindow();
|
|
297
|
+
let existing = runtimeWindow?.__weaverses?.[page.id];
|
|
298
|
+
let requestKey = getRuntimeKey(page.id, requestInfo);
|
|
299
|
+
if (existing?.__weaverseNextRequestKey === requestKey) {
|
|
300
|
+
let nextIsDesignMode = config.client?.requestContext?.isDesignMode ?? false;
|
|
301
|
+
if (!(existing.isDesignMode || nextIsDesignMode)) {
|
|
302
|
+
existing.setProjectData(page);
|
|
206
303
|
}
|
|
304
|
+
existing.dataContext = resolveDataContext(config);
|
|
305
|
+
existing.requestInfo = requestInfo;
|
|
306
|
+
existing.projectId = resolveProjectId(config.client, config.data, page.id);
|
|
307
|
+
existing.isDesignMode = nextIsDesignMode;
|
|
308
|
+
existing.isPreviewMode = config.client?.requestContext?.isPreviewMode ?? false;
|
|
309
|
+
existing.isRevisionPreview = config.client?.requestContext?.isRevisionPreview ?? false;
|
|
310
|
+
existing.sectionType = config.client?.requestContext?.sectionType;
|
|
311
|
+
existing.weaverseHost = getConfigString(config.data.configs, "weaverseHost") ?? existing.weaverseHost;
|
|
312
|
+
existing.weaverseVersion = getConfigString(config.data.configs, "weaverseVersion") ?? existing.weaverseVersion;
|
|
313
|
+
existing.internal.pageAssignment = config.data.pageAssignment;
|
|
314
|
+
existing.internal.project = config.data.project;
|
|
315
|
+
if (config.navigate) {
|
|
316
|
+
existing.internal.navigate = config.navigate;
|
|
317
|
+
}
|
|
318
|
+
if (config.revalidate) {
|
|
319
|
+
existing.internal.revalidate = config.revalidate;
|
|
320
|
+
}
|
|
321
|
+
if (config.themeSettingsStore) {
|
|
322
|
+
existing.internal.themeSettingsStore = config.themeSettingsStore;
|
|
323
|
+
}
|
|
324
|
+
registerRuntime(existing);
|
|
325
|
+
return existing;
|
|
207
326
|
}
|
|
208
|
-
let
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
327
|
+
let runtime = new WeaverseNextRuntime(config);
|
|
328
|
+
runtime.__weaverseNextRequestKey = requestKey;
|
|
329
|
+
registerRuntime(runtime);
|
|
330
|
+
return runtime;
|
|
331
|
+
}
|
|
332
|
+
function bindWeaverseNextStudioRuntime(runtime) {
|
|
333
|
+
if (!runtime.isDesignMode) {
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
let studio = getRuntimeWindow()?.weaverseStudio;
|
|
337
|
+
if (!studio) {
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
let boundRuntime = runtime;
|
|
341
|
+
if (!boundRuntime.__weaverseNextStudioBound) {
|
|
342
|
+
studio.init?.(runtime);
|
|
343
|
+
boundRuntime.__weaverseNextStudioBound = true;
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
studio.refreshStudio?.({
|
|
347
|
+
data: runtime.data,
|
|
348
|
+
pageId: runtime.pageId,
|
|
349
|
+
requestInfo: runtime.requestInfo
|
|
350
|
+
});
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
var STUDIO_BIND_RETRY_INTERVAL_MS = 50;
|
|
354
|
+
var STUDIO_BIND_MAX_ATTEMPTS = 100;
|
|
355
|
+
function WeaverseNextStudioBridge(props) {
|
|
356
|
+
let { runtime, navigate, revalidate } = props;
|
|
357
|
+
let lastRuntimeRef = useRef(runtime);
|
|
358
|
+
let lastRuntimeDataRef = useRef(runtime.data);
|
|
359
|
+
let lastRequestInfoRef = useRef(runtime.requestInfo);
|
|
360
|
+
useEffect(() => {
|
|
361
|
+
if (navigate) {
|
|
362
|
+
runtime.internal.navigate = navigate;
|
|
363
|
+
}
|
|
364
|
+
if (revalidate) {
|
|
365
|
+
runtime.internal.revalidate = revalidate;
|
|
366
|
+
}
|
|
367
|
+
if (bindWeaverseNextStudioRuntime(runtime) || !runtime.isDesignMode) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
let attempts = 0;
|
|
371
|
+
let interval = window.setInterval(() => {
|
|
372
|
+
attempts += 1;
|
|
373
|
+
if (bindWeaverseNextStudioRuntime(runtime) || attempts >= STUDIO_BIND_MAX_ATTEMPTS) {
|
|
374
|
+
window.clearInterval(interval);
|
|
234
375
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
376
|
+
}, STUDIO_BIND_RETRY_INTERVAL_MS);
|
|
377
|
+
return () => window.clearInterval(interval);
|
|
378
|
+
}, [runtime, navigate, revalidate]);
|
|
379
|
+
useEffect(() => {
|
|
380
|
+
if (lastRuntimeRef.current !== runtime) {
|
|
381
|
+
lastRuntimeRef.current = runtime;
|
|
382
|
+
lastRuntimeDataRef.current = runtime.data;
|
|
383
|
+
lastRequestInfoRef.current = runtime.requestInfo;
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (lastRuntimeDataRef.current === runtime.data && lastRequestInfoRef.current === runtime.requestInfo) {
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
lastRuntimeDataRef.current = runtime.data;
|
|
390
|
+
lastRequestInfoRef.current = runtime.requestInfo;
|
|
391
|
+
bindWeaverseNextStudioRuntime(runtime);
|
|
392
|
+
}, [runtime, runtime.data, runtime.requestInfo]);
|
|
393
|
+
return null;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// src/studio-router.ts
|
|
397
|
+
function shouldResetScroll(options) {
|
|
398
|
+
if (!options || typeof options !== "object") {
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
return !options.preventScrollReset;
|
|
402
|
+
}
|
|
403
|
+
function createWeaverseNextStudioInternals(router, options = {}) {
|
|
404
|
+
return {
|
|
405
|
+
navigate: (to, navigateOptions) => {
|
|
406
|
+
let scroll = shouldResetScroll(navigateOptions);
|
|
407
|
+
if (options.replace) {
|
|
408
|
+
router.replace(to, { scroll });
|
|
409
|
+
} else {
|
|
410
|
+
router.push(to, { scroll });
|
|
243
411
|
}
|
|
412
|
+
},
|
|
413
|
+
revalidate: () => {
|
|
414
|
+
router.refresh();
|
|
244
415
|
}
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
function useWeaverseNextStudioInternals(options) {
|
|
419
|
+
let router = useRouter();
|
|
420
|
+
let ref = useRef(null);
|
|
421
|
+
if (!ref.current || ref.current.router !== router || ref.current.replace !== options?.replace) {
|
|
422
|
+
ref.current = {
|
|
423
|
+
internals: createWeaverseNextStudioInternals(router, options),
|
|
424
|
+
replace: options?.replace,
|
|
425
|
+
router
|
|
426
|
+
};
|
|
245
427
|
}
|
|
246
|
-
|
|
247
|
-
|
|
428
|
+
return ref.current.internals;
|
|
429
|
+
}
|
|
430
|
+
function WeaverseNextStudio(props) {
|
|
431
|
+
let { runtime, replace } = props;
|
|
432
|
+
let { navigate, revalidate } = useWeaverseNextStudioInternals({ replace });
|
|
433
|
+
return /* @__PURE__ */ jsx(
|
|
434
|
+
WeaverseNextStudioBridge,
|
|
435
|
+
{
|
|
436
|
+
navigate,
|
|
437
|
+
revalidate,
|
|
438
|
+
runtime
|
|
439
|
+
}
|
|
440
|
+
);
|
|
248
441
|
}
|
|
249
442
|
var EMPTY_DATA_CONTEXT = {};
|
|
250
|
-
function
|
|
443
|
+
function getRenderablePage2(data) {
|
|
251
444
|
let page = data?.page;
|
|
252
445
|
if (!page) {
|
|
253
446
|
return null;
|
|
@@ -260,30 +453,136 @@ var WeaverseNextRenderer = memo(function WeaverseNextRendererComponent(props) {
|
|
|
260
453
|
let client = props.client ?? context?.client;
|
|
261
454
|
let data = props.data ?? client?.data ?? null;
|
|
262
455
|
let dataContext = props.dataContext ?? client?.dataContext ?? EMPTY_DATA_CONTEXT;
|
|
263
|
-
let page = useMemo(() =>
|
|
456
|
+
let page = useMemo(() => getRenderablePage2(data), [data]);
|
|
264
457
|
let weaverse = useMemo(() => {
|
|
265
458
|
if (!page) {
|
|
266
459
|
return null;
|
|
267
460
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
data: page
|
|
461
|
+
return createWeaverseNextRuntime({
|
|
462
|
+
client,
|
|
463
|
+
data: { ...data, page },
|
|
464
|
+
dataContext,
|
|
465
|
+
themeSettingsStore: context?.themeSettingsStore
|
|
271
466
|
});
|
|
272
|
-
|
|
273
|
-
instance.isDesignMode = client?.requestContext?.isDesignMode ?? false;
|
|
274
|
-
return instance;
|
|
275
|
-
}, [
|
|
276
|
-
client?.projectId,
|
|
277
|
-
client?.requestContext?.isDesignMode,
|
|
278
|
-
page,
|
|
279
|
-
dataContext
|
|
280
|
-
]);
|
|
467
|
+
}, [client, page, data, dataContext, context?.themeSettingsStore]);
|
|
281
468
|
if (!weaverse) {
|
|
282
469
|
return null;
|
|
283
470
|
}
|
|
284
|
-
return /* @__PURE__ */
|
|
471
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
472
|
+
/* @__PURE__ */ jsx(WeaverseRoot, { context: weaverse }),
|
|
473
|
+
/* @__PURE__ */ jsx(WeaverseNextStudio, { runtime: weaverse })
|
|
474
|
+
] });
|
|
285
475
|
});
|
|
286
476
|
|
|
287
|
-
|
|
477
|
+
// src/studio-script-src.ts
|
|
478
|
+
var TRUSTED_STUDIO_DOMAINS = ["weaverse.io", "weaverse.dev"];
|
|
479
|
+
function isLoopbackHostname(hostname) {
|
|
480
|
+
return hostname === "localhost" || hostname === "127.0.0.1" || hostname.endsWith(".localhost");
|
|
481
|
+
}
|
|
482
|
+
function normalizeOrigin(value) {
|
|
483
|
+
if (!value) {
|
|
484
|
+
return null;
|
|
485
|
+
}
|
|
486
|
+
try {
|
|
487
|
+
return new URL(value).origin;
|
|
488
|
+
} catch {
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
function isTrustedStudioOrigin(origin, storefrontHostname) {
|
|
493
|
+
let parsed;
|
|
494
|
+
try {
|
|
495
|
+
parsed = new URL(origin);
|
|
496
|
+
} catch {
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
let { hostname, protocol } = parsed;
|
|
500
|
+
if (isLoopbackHostname(storefrontHostname) && isLoopbackHostname(hostname)) {
|
|
501
|
+
return protocol === "http:" || protocol === "https:";
|
|
502
|
+
}
|
|
503
|
+
if (protocol !== "https:") {
|
|
504
|
+
return false;
|
|
505
|
+
}
|
|
506
|
+
return TRUSTED_STUDIO_DOMAINS.some(
|
|
507
|
+
(domain) => hostname === domain || hostname.endsWith(`.${domain}`)
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
function toSearchParams(context) {
|
|
511
|
+
if (context?.searchParams) {
|
|
512
|
+
return new URLSearchParams(context.searchParams);
|
|
513
|
+
}
|
|
514
|
+
if (context?.url) {
|
|
515
|
+
let url = typeof context.url === "string" ? new URL(context.url, "http://localhost") : context.url;
|
|
516
|
+
return new URLSearchParams(url.searchParams);
|
|
517
|
+
}
|
|
518
|
+
return new URLSearchParams();
|
|
519
|
+
}
|
|
520
|
+
function last(searchParams, key) {
|
|
521
|
+
let values = searchParams.getAll(key);
|
|
522
|
+
return values.length ? values.at(-1) ?? null : null;
|
|
523
|
+
}
|
|
524
|
+
function resolveWeaverseNextStudioScriptSrc(context, options = {}) {
|
|
525
|
+
let searchParams = toSearchParams(context);
|
|
526
|
+
let isDesignMode = context?.isDesignMode ?? last(searchParams, "isDesignMode") === "true";
|
|
527
|
+
let isPreviewMode = context?.isPreviewMode ?? last(searchParams, "isPreviewMode") === "true";
|
|
528
|
+
let isRevisionPreview = context?.isRevisionPreview ?? Boolean(last(searchParams, "__revisionId"));
|
|
529
|
+
if (!(isDesignMode || isPreviewMode || isRevisionPreview)) {
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
let origin = normalizeOrigin(last(searchParams, "weaverseHost"));
|
|
533
|
+
if (!origin) {
|
|
534
|
+
return null;
|
|
535
|
+
}
|
|
536
|
+
let storefrontHostname = options.storefrontHostname ?? "";
|
|
537
|
+
if (!isTrustedStudioOrigin(origin, storefrontHostname)) {
|
|
538
|
+
return null;
|
|
539
|
+
}
|
|
540
|
+
let framework = options.framework ?? "hydrogen";
|
|
541
|
+
let bundle = isDesignMode ? "index.js" : "preview.js";
|
|
542
|
+
let version = last(searchParams, "weaverseVersion");
|
|
543
|
+
let url = `${origin}/static/studio/${framework}/${bundle}`;
|
|
544
|
+
return version ? `${url}?v=${encodeURIComponent(version)}` : url;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// src/studio-connect.tsx
|
|
548
|
+
var loadedScriptSrcs = /* @__PURE__ */ new Set();
|
|
549
|
+
function loadScript(src) {
|
|
550
|
+
if (typeof document === "undefined") {
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
if (loadedScriptSrcs.has(src)) {
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
let existing = document.querySelector(`script[src="${src}"]`);
|
|
557
|
+
if (existing) {
|
|
558
|
+
loadedScriptSrcs.add(src);
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
let script = document.createElement("script");
|
|
562
|
+
script.async = true;
|
|
563
|
+
script.src = src;
|
|
564
|
+
document.head.appendChild(script);
|
|
565
|
+
loadedScriptSrcs.add(src);
|
|
566
|
+
}
|
|
567
|
+
function WeaverseNextStudioConnect(props) {
|
|
568
|
+
useEffect(() => {
|
|
569
|
+
let context = props.context;
|
|
570
|
+
if (!context && typeof window !== "undefined") {
|
|
571
|
+
context = {
|
|
572
|
+
searchParams: new URLSearchParams(window.location.search),
|
|
573
|
+
url: window.location.href
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
let src = resolveWeaverseNextStudioScriptSrc(context, {
|
|
577
|
+
storefrontHostname: props.storefrontHostname ?? (typeof window === "undefined" ? void 0 : window.location.hostname)
|
|
578
|
+
});
|
|
579
|
+
if (src) {
|
|
580
|
+
loadScript(src);
|
|
581
|
+
}
|
|
582
|
+
}, [props.context, props.storefrontHostname]);
|
|
583
|
+
return null;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
export { WeaverseNextProvider, WeaverseNextRenderer, WeaverseNextRuntime, WeaverseNextStudio, WeaverseNextStudioBridge, WeaverseNextStudioConnect, bindWeaverseNextStudioRuntime, createWeaverseNextClient, createWeaverseNextRuntime, createWeaverseNextStudioInternals, createWeaverseNextThemeSettingsStore, resolveWeaverseNextStudioScriptSrc, useThemeSettings, useWeaverseCommerce, useWeaverseNextStudioInternals, useWeaversePageData, useWeaverseRootData };
|
|
288
587
|
//# sourceMappingURL=index.mjs.map
|
|
289
588
|
//# sourceMappingURL=index.mjs.map
|