@bagelink/blox 1.13.5 → 1.14.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.
Files changed (47) hide show
  1. package/dist/CmsPageView.vue.d.ts.map +1 -1
  2. package/dist/PreviewApp-BrthGx2F.js +4 -0
  3. package/dist/PreviewApp-CV_Uh9-d.cjs +4 -0
  4. package/dist/PreviewApp.vue.d.ts.map +1 -1
  5. package/dist/PreviewApp.vue_vue_type_style_index_0_lang-BkGWMbsE.js +185 -0
  6. package/dist/PreviewApp.vue_vue_type_style_index_0_lang-CFS05Itn.cjs +184 -0
  7. package/dist/PreviewRenderer.d.ts.map +1 -1
  8. package/dist/bridge.d.ts +1 -0
  9. package/dist/bridge.d.ts.map +1 -1
  10. package/dist/core-CPXNfJ-Z.js +460 -0
  11. package/dist/core-D1FiZJtz.cjs +459 -0
  12. package/dist/core.d.ts.map +1 -1
  13. package/dist/createBloxApp.d.ts +107 -0
  14. package/dist/createBloxApp.d.ts.map +1 -0
  15. package/dist/defineBlock.d.ts +19 -4
  16. package/dist/defineBlock.d.ts.map +1 -1
  17. package/dist/index.cjs +87 -604
  18. package/dist/index.d.ts +2 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.mjs +86 -602
  21. package/dist/{prerender-CjJwDXkC.cjs → prerender-Bi7YtzSp.cjs} +48 -46
  22. package/dist/{prerender-oMLxrKGs.js → prerender-D3Q4jKXm.js} +51 -49
  23. package/dist/schema.d.ts +2 -0
  24. package/dist/schema.d.ts.map +1 -1
  25. package/dist/ssg/cli.cjs +2 -2
  26. package/dist/ssg/cli.mjs +2 -2
  27. package/dist/ssg/client.cjs +9 -7
  28. package/dist/ssg/client.d.ts +1 -1
  29. package/dist/ssg/client.d.ts.map +1 -1
  30. package/dist/ssg/client.mjs +3 -1
  31. package/dist/ssg/createSSREntry.d.ts +73 -0
  32. package/dist/ssg/createSSREntry.d.ts.map +1 -0
  33. package/dist/ssg/index.cjs +116 -8
  34. package/dist/ssg/index.d.ts +2 -0
  35. package/dist/ssg/index.d.ts.map +1 -1
  36. package/dist/ssg/index.mjs +93 -6
  37. package/dist/ssg/prerender.d.ts.map +1 -1
  38. package/dist/style.css +5 -46
  39. package/dist/vite-plugin.cjs +142 -3
  40. package/dist/vite-plugin.d.ts +22 -21
  41. package/dist/vite-plugin.d.ts.map +1 -1
  42. package/dist/vite-plugin.mjs +142 -3
  43. package/package.json +4 -1
  44. package/dist/PreviewApp-BZNzZkit.js +0 -4
  45. package/dist/PreviewApp-C1WvJWI4.cjs +0 -4
  46. package/dist/constants-BjitNk-W.js +0 -8
  47. package/dist/constants-CFB_pMvT.cjs +0 -7
package/dist/index.cjs CHANGED
@@ -1,239 +1,24 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
- const constants = require("./constants-CFB_pMvT.cjs");
3
+ const core = require("./core-D1FiZJtz.cjs");
4
+ const PreviewApp_vue_vue_type_style_index_0_lang = require("./PreviewApp.vue_vue_type_style_index_0_lang-CFS05Itn.cjs");
7
5
  const vue = require("vue");
8
6
  const vueRouter = require("vue-router");
9
- const vue$1 = require("@bagelink/vue");
10
- const BASE = "/api";
11
- let _websiteName = "";
12
- let _store = "";
13
- let _websiteId = null;
14
- function configureApi(websiteName, store) {
15
- _websiteName = websiteName;
16
- _store = store;
17
- _websiteId = null;
18
- }
19
- async function request(path, { locale: locale2, params, ...opts } = {}) {
20
- const url = new URL(`${BASE}${path}`, window.location.origin);
21
- if (locale2) url.searchParams.set("locale", locale2);
22
- if (params) {
23
- for (const [k, v] of Object.entries(params)) url.searchParams.set(k, String(v));
24
- }
25
- const res = await fetch(url.toString(), opts);
26
- if (!res.ok) throw new Error(`API ${res.status}: ${path}`);
27
- if (res.status === 204) return null;
28
- return res.json();
29
- }
30
- async function getWebsiteId() {
31
- if (_websiteId) return _websiteId;
32
- if (typeof window !== "undefined") {
33
- const embedded = window[constants.BLOX_WEBSITE_ID_WINDOW_KEY];
34
- if (typeof embedded === "string" && embedded !== "") {
35
- _websiteId = embedded;
36
- return _websiteId;
37
- }
38
- }
39
- const websites = await request("/cms/websites");
40
- const website = websites.find((w) => w.name === _websiteName);
41
- if (!website) throw new Error(`Website "${_websiteName}" not found. Run: npm run seed`);
42
- _websiteId = website.id;
43
- return _websiteId;
44
- }
45
- async function resolvePath(path, locale2) {
46
- const websiteId = await getWebsiteId();
47
- return request(`/cms/websites/${websiteId}/resolve-path`, {
48
- locale: locale2,
49
- params: { path }
50
- });
51
- }
52
- async function listItems(collection, { locale: locale2, q, offset = 0, limit = 100 } = {}) {
53
- return request(`/datastore/${_store}/collections/${collection}`, {
54
- locale: locale2,
55
- params: { ...q ? { q } : {}, offset, limit }
56
- });
57
- }
58
- function sendToEditor(msg) {
59
- window.parent.postMessage(msg, "*");
60
- }
61
- function sendToPreview(iframe, msg) {
62
- var _a;
63
- (_a = iframe.contentWindow) == null ? void 0 : _a.postMessage(msg, "*");
64
- }
65
- const locale = vue.ref(localStorage.getItem("blox:locale") || "en");
66
- const currentAlternates = vue.ref({});
67
- function useLocale() {
68
- const dir = vue.computed(() => locale.value === "he" || locale.value === "ar" ? "rtl" : "ltr");
69
- function setLocale(l) {
70
- locale.value = l;
71
- localStorage.setItem("blox:locale", l);
72
- document.documentElement.lang = l;
73
- document.documentElement.dir = dir.value;
74
- try {
75
- const i18n = vue$1.getI18n();
76
- if (i18n == null ? void 0 : i18n.global) i18n.global.locale.value = l;
77
- } catch {
78
- }
79
- }
80
- function setAlternates(alternates) {
81
- currentAlternates.value = alternates;
82
- }
83
- return { locale, dir, setLocale, currentAlternates, setAlternates };
84
- }
85
- const BLOX_REGISTRY_KEY = Symbol("blox:registry");
86
- const BLOX_CONFIG_KEY = Symbol("blox:config");
87
- const BLOX_LOCALE_STRATEGY_KEY = Symbol("blox:locale-strategy");
88
- const PageRenderer = vue.defineComponent({
89
- name: "PageRenderer",
90
- props: {
91
- blocks: {
92
- type: Array,
93
- default: () => []
94
- }
95
- },
96
- setup(props) {
97
- const registry = vue.inject(BLOX_REGISTRY_KEY, {});
98
- return () => vue.h(
99
- "div",
100
- props.blocks.flatMap((block, i) => {
101
- const definition = registry[block.type];
102
- if (!definition) return [];
103
- const props2 = { ...definition.schema.defaults, ...block.props };
104
- return [vue.h(definition.component, { key: i, ...props2 })];
105
- })
106
- );
107
- }
108
- });
109
- const _hoisted_1$1 = {
110
- key: 0,
111
- class: "blox-loading"
112
- };
113
- const _hoisted_2 = {
114
- key: 1,
115
- class: "blox-not-found"
116
- };
117
- const _hoisted_3 = {
118
- key: 2,
119
- class: "blox-error"
120
- };
121
- const _hoisted_4 = {
122
- key: 0,
123
- class: "blox-nav-progress"
124
- };
125
- const _sfc_main$2 = /* @__PURE__ */ vue.defineComponent({
126
- __name: "CmsPageView",
127
- setup(__props) {
128
- const route = vueRouter.useRoute();
129
- const router = vueRouter.useRouter();
130
- const { locale: locale2, setLocale, setAlternates } = useLocale();
131
- const strategy = vue.inject(BLOX_LOCALE_STRATEGY_KEY, null);
132
- const blocks = vue.ref([]);
133
- const contexts = vue.ref({});
134
- vue.provide("contexts", contexts);
135
- const notFound = vue.ref(false);
136
- const error = vue.ref(null);
137
- const initialLoading = vue.ref(true);
138
- const navigating = vue.ref(false);
139
- function tryHydrateFromState(path) {
140
- var _a, _b;
141
- const win = typeof window !== "undefined" ? window : typeof globalThis !== "undefined" ? globalThis : void 0;
142
- if (!win) return false;
143
- const state = win[constants.BLOX_STATE_WINDOW_KEY];
144
- if (!state || typeof state !== "object") return false;
145
- const entry = state[path];
146
- if (!entry) return false;
147
- blocks.value = ((_b = (_a = entry.page) == null ? void 0 : _a.content) == null ? void 0 : _b.blocks) ?? [];
148
- contexts.value = entry.contexts ?? {};
149
- setAlternates(entry.alternates ?? {});
150
- return true;
151
- }
152
- function updateDocumentTitle(resolved) {
153
- if (typeof document === "undefined") return;
154
- const page = resolved.page;
155
- const ctx = resolved.contexts ? Object.values(resolved.contexts).find((c) => c != null) : null;
156
- const ctxTitle = ctx && typeof ctx.title === "string" ? ctx.title : null;
157
- const title = page.meta_title || ctxTitle || page.title;
158
- if (title) document.title = title;
159
- }
160
- const initialSlug = (() => {
161
- var _a;
162
- if (strategy) {
163
- const hostname = typeof window !== "undefined" ? ((_a = window.location) == null ? void 0 : _a.hostname) ?? "" : "";
164
- return strategy.detect(hostname, route.path).slug;
165
- }
166
- return route.path || "/";
167
- })();
168
- const ssrHydrated = tryHydrateFromState(initialSlug);
169
- if (ssrHydrated) initialLoading.value = false;
170
- async function load() {
171
- var _a;
172
- notFound.value = false;
173
- error.value = null;
174
- const { locale: urlLocale, slug } = strategy ? strategy.detect(typeof window !== "undefined" ? window.location.hostname : "", route.path) : { locale: locale2.value, slug: route.path || "/" };
175
- if (urlLocale !== locale2.value) setLocale(urlLocale);
176
- if (tryHydrateFromState(slug)) {
177
- initialLoading.value = false;
178
- return;
179
- }
180
- navigating.value = true;
181
- try {
182
- const resolved = await resolvePath(slug, urlLocale);
183
- blocks.value = ((_a = resolved.page.content) == null ? void 0 : _a.blocks) ?? [];
184
- contexts.value = resolved.contexts ?? {};
185
- setAlternates(resolved.alternates ?? {});
186
- updateDocumentTitle(resolved);
187
- } catch (e) {
188
- const msg = e instanceof Error ? e.message : String(e);
189
- if (msg.includes("404")) {
190
- const home = strategy ? strategy.toPath("/") : "/";
191
- router.replace(home);
192
- } else {
193
- error.value = msg;
194
- }
195
- } finally {
196
- initialLoading.value = false;
197
- navigating.value = false;
198
- }
199
- }
200
- if (!ssrHydrated) {
201
- load();
202
- }
203
- vue.watch(() => route.path, load);
204
- return (_ctx, _cache) => {
205
- const _component_RouterLink = vue.resolveComponent("RouterLink");
206
- return vue.openBlock(), vue.createElementBlock("div", null, [
207
- initialLoading.value && blocks.value.length === 0 ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_1$1, " Loading… ")) : notFound.value ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_2, [
208
- _cache[0] || (_cache[0] = vue.createElementVNode("h2", null, "404", -1)),
209
- vue.createElementVNode("p", null, vue.toDisplayString(vue.unref(locale2) === "he" ? "הדף לא נמצא" : "Page not found"), 1),
210
- vue.createVNode(_component_RouterLink, { to: "/" }, {
211
- default: vue.withCtx(() => [
212
- vue.createTextVNode(vue.toDisplayString(vue.unref(locale2) === "he" ? "חזרה הביתה" : "Go home"), 1)
213
- ]),
214
- _: 1
215
- })
216
- ])) : error.value ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_3, vue.toDisplayString(error.value), 1)) : (vue.openBlock(), vue.createElementBlock(vue.Fragment, { key: 3 }, [
217
- navigating.value ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_4)) : vue.createCommentVNode("", true),
218
- vue.createVNode(vue.unref(PageRenderer), { blocks: blocks.value }, null, 8, ["blocks"])
219
- ], 64))
220
- ]);
221
- };
222
- }
223
- });
7
+ const pinia = require("pinia");
8
+ const ssg_client = require("./ssg/client.cjs");
224
9
  function useLocaleNav() {
225
- const strategy = vue.inject(BLOX_LOCALE_STRATEGY_KEY, null);
10
+ const strategy = vue.inject(core.BLOX_LOCALE_STRATEGY_KEY, null);
226
11
  const router = vueRouter.useRouter();
227
- const { locale: locale2, currentAlternates: currentAlternates2 } = useLocale();
12
+ const { locale, currentAlternates } = core.useLocale();
228
13
  function switchLocale(targetLocale) {
229
14
  if (!strategy) return;
230
- const alternateSlug = currentAlternates2.value[targetLocale];
15
+ const alternateSlug = currentAlternates.value[targetLocale];
231
16
  strategy.switchTo(targetLocale, alternateSlug ?? "/", router);
232
17
  }
233
18
  function toPath(slug) {
234
19
  return strategy ? strategy.toPath(slug) : slug;
235
20
  }
236
- return { switchLocale, toPath, locale: locale2 };
21
+ return { switchLocale, toPath, locale };
237
22
  }
238
23
  function usePageContext(key) {
239
24
  const contexts = vue.inject("contexts");
@@ -243,396 +28,94 @@ function usePageContext(key) {
243
28
  return value;
244
29
  });
245
30
  }
246
- const _sfc_main$1 = /* @__PURE__ */ vue.defineComponent({
247
- __name: "LocaleRouterLink",
248
- props: {
249
- to: {},
250
- replace: { type: Boolean },
251
- activeClass: {},
252
- exactActiveClass: {},
253
- ariaCurrentValue: {}
254
- },
255
- setup(__props) {
256
- const props = __props;
257
- const strategy = vue.inject(BLOX_LOCALE_STRATEGY_KEY, null);
258
- const localizedTo = vue.computed(() => {
259
- if (!strategy || typeof props.to !== "string") return props.to;
260
- return strategy.toPath(props.to);
261
- });
262
- return (_ctx, _cache) => {
263
- return vue.openBlock(), vue.createBlock(vue.unref(vueRouter.RouterLink), vue.normalizeProps(vue.guardReactiveProps({ ...props, to: localizedTo.value })), {
264
- default: vue.withCtx(() => [
265
- vue.renderSlot(_ctx.$slots, "default")
266
- ]),
267
- _: 3
268
- }, 16);
269
- };
270
- }
271
- });
272
- function createPathStrategy(options) {
273
- const { defaultLocale, supportedLocales } = options;
274
- let _currentLocale = defaultLocale;
275
- return {
276
- get locales() {
277
- return supportedLocales;
278
- },
279
- get currentLocale() {
280
- return _currentLocale;
281
- },
282
- detect(_hostname, path) {
283
- const segments = path.replace(/^\//, "").split("/");
284
- const first = segments[0];
285
- if (supportedLocales.includes(first)) {
286
- _currentLocale = first;
287
- return {
288
- locale: first,
289
- slug: `/${segments.slice(1).join("/")}` || "/"
290
- };
291
- }
292
- _currentLocale = defaultLocale;
293
- return { locale: defaultLocale, slug: path || "/" };
294
- },
295
- toPath(slug) {
296
- if (_currentLocale === defaultLocale) return slug || "/";
297
- const s = slug || "/";
298
- return s === "/" ? `/${_currentLocale}` : `/${_currentLocale}${s}`;
299
- },
300
- switchTo(locale2, slug, router) {
301
- const s = slug || "/";
302
- const target = locale2 === defaultLocale ? s : s === "/" ? `/${locale2}` : `/${locale2}${s}`;
303
- router.push(target);
304
- }
305
- };
306
- }
307
- function createDomainStrategy(options) {
308
- const { localeDomains } = options;
309
- const locales = Object.keys(localeDomains);
310
- const _currentLocale = locales.find((l) => localeDomains[l] === window.location.hostname) ?? locales[0] ?? "en";
311
- return {
312
- get locales() {
313
- return locales;
314
- },
315
- get currentLocale() {
316
- return _currentLocale;
317
- },
318
- detect(_hostname, path) {
319
- return { locale: _currentLocale, slug: path || "/" };
320
- },
321
- toPath(slug) {
322
- return slug || "/";
323
- },
324
- switchTo(locale2, slug, _router) {
325
- const domain = localeDomains[locale2];
326
- if (!domain) return;
327
- if (locale2 === _currentLocale) {
328
- _router.push(slug || "/");
329
- return;
330
- }
331
- const { protocol } = window.location;
332
- const s = slug || "/";
333
- window.location.href = `${protocol}//${domain}${s}`;
334
- }
31
+ function createBloxApp(options) {
32
+ const {
33
+ rootComponent,
34
+ router,
35
+ modules,
36
+ websiteName,
37
+ store,
38
+ locale = "en",
39
+ supportedLocales = [locale],
40
+ apiBaseURL = "/api",
41
+ mount: mountTarget = "#app",
42
+ plugins = [],
43
+ globalComponents = {},
44
+ setup: userSetup
45
+ } = options;
46
+ ssg_client.installBloxStateCache();
47
+ let axiosInstance;
48
+ if (options.createApi) {
49
+ const result = options.createApi({ baseURL: apiBaseURL });
50
+ axiosInstance = result.axios;
51
+ }
52
+ const collectionInterceptor = ssg_client.installCollectionCache();
53
+ if (collectionInterceptor && axiosInstance) {
54
+ axiosInstance.interceptors.request.use(collectionInterceptor);
55
+ }
56
+ const app = vue.createApp(rootComponent);
57
+ const bloxOpts = {
58
+ modules,
59
+ websiteName,
60
+ store,
61
+ defaultLocale: locale,
62
+ supportedLocales,
63
+ router
335
64
  };
336
- }
337
- function createLocaleStrategy(options) {
338
- if (options.localeStrategy === "domain" && options.localeDomains) {
339
- return createDomainStrategy({ localeDomains: options.localeDomains });
340
- }
341
- return createPathStrategy({
342
- defaultLocale: options.defaultLocale ?? "en",
343
- supportedLocales: options.supportedLocales ?? []
65
+ const blox = core.createBlox(bloxOpts);
66
+ const pinia$1 = pinia.createPinia();
67
+ pinia$1.use(({ store: s }) => {
68
+ s.router = vue.markRaw(router);
69
+ });
70
+ blox.install(app, {});
71
+ app.use(pinia$1);
72
+ app.use(router);
73
+ for (const plugin of plugins) {
74
+ if (Array.isArray(plugin)) {
75
+ const [p, ...args] = plugin;
76
+ app.use(p, ...args);
77
+ } else {
78
+ app.use(plugin);
79
+ }
80
+ }
81
+ for (const [name, comp] of Object.entries(globalComponents)) {
82
+ app.component(name, comp);
83
+ }
84
+ if (userSetup) userSetup(app);
85
+ router.isReady().then(() => {
86
+ app.mount(mountTarget);
344
87
  });
88
+ return { app, router, blox };
345
89
  }
346
- function applyDefaultsToFields(schema, defaults) {
90
+ function defineBlock(meta) {
347
91
  var _a;
348
- if (!schema || !defaults) return;
349
- for (const [key, value] of Object.entries(defaults)) {
350
- const field = (_a = schema._fields) == null ? void 0 : _a[key];
351
- if (field && field._config.default === void 0) {
352
- field._config.default = value;
353
- }
354
- }
355
- }
356
- class BloxInstance {
357
- constructor(options) {
358
- __publicField(this, "_registry", {});
359
- __publicField(this, "_routeRegistered", false);
360
- __publicField(this, "_config");
361
- __publicField(this, "_strategy");
362
- this._config = {
363
- defaultLocale: (options == null ? void 0 : options.defaultLocale) ?? "en",
364
- supportedLocales: (options == null ? void 0 : options.supportedLocales) ?? []
365
- };
366
- this._strategy = createLocaleStrategy(options ?? {});
367
- }
368
- registerModules(modules) {
369
- const list = Array.isArray(modules) ? modules : Object.values(modules);
370
- for (const mod of list) {
371
- if (!(mod == null ? void 0 : mod.default)) continue;
372
- for (const [key, value] of Object.entries(mod)) {
373
- if (key === "default") continue;
374
- if (value && typeof value === "object" && "label" in value) {
375
- const meta = value;
376
- applyDefaultsToFields(meta.schema, meta.defaults);
377
- this._registry[meta.name ?? key] = {
378
- component: mod.default,
379
- schema: {
380
- label: meta.label,
381
- description: meta.description,
382
- icon: meta.icon,
383
- fields: meta.schema,
384
- defaults: meta.defaults
385
- }
386
- };
387
- break;
388
- }
389
- }
390
- }
391
- return this;
392
- }
393
- registerComponents(components) {
394
- for (const [key, value] of Object.entries(components)) {
395
- if (!value || typeof value !== "object" && typeof value !== "function") continue;
396
- if (isBlockDefinition(value)) {
397
- this._registry[key] = value;
398
- } else if (value.__blox) {
399
- const meta = value.__blox;
400
- applyDefaultsToFields(meta.schema, meta.defaults);
401
- this._registry[meta.name ?? key] = {
402
- component: value,
403
- schema: {
404
- label: meta.label,
405
- description: meta.description,
406
- icon: meta.icon,
407
- fields: meta.schema,
408
- defaults: meta.defaults
409
- }
410
- };
411
- }
412
- }
413
- return this;
414
- }
415
- registerRoutes(router) {
416
- if (this._routeRegistered) return this;
417
- if (this._config.supportedLocales.length > 0) {
418
- const { defaultLocale } = this._config;
419
- router.beforeEach((to) => {
420
- const segments = to.path.replace(/^\//, "").split("/");
421
- if (segments[0] === defaultLocale) {
422
- const rest = segments.slice(1).join("/");
423
- return { path: `/${rest}`, replace: true };
424
- }
425
- });
426
- }
427
- router.addRoute({
428
- path: "/_blox_preview",
429
- component: () => Promise.resolve().then(() => require("./PreviewApp-C1WvJWI4.cjs")),
430
- beforeEnter: () => window.parent !== window ? true : "/"
431
- });
432
- router.addRoute({ path: "/:pathMatch(.*)*", component: _sfc_main$2 });
433
- this._routeRegistered = true;
434
- return this;
435
- }
436
- install(app, options) {
437
- if (options) {
438
- this._config = {
439
- defaultLocale: options.defaultLocale ?? this._config.defaultLocale,
440
- supportedLocales: options.supportedLocales ?? this._config.supportedLocales
441
- };
442
- this._strategy = createLocaleStrategy({
443
- ...options,
444
- defaultLocale: this._config.defaultLocale,
445
- supportedLocales: this._config.supportedLocales
446
- });
447
- }
448
- if (options == null ? void 0 : options.modules) this.registerModules(options.modules);
449
- if (options == null ? void 0 : options.components) this.registerComponents(options.components);
450
- if (options == null ? void 0 : options.router) this.registerRoutes(options.router);
451
- if ((options == null ? void 0 : options.websiteName) || (options == null ? void 0 : options.store)) {
452
- configureApi(options.websiteName ?? "", options.store ?? "");
92
+ if (meta.schema && !meta.defaults) {
93
+ const extracted = {};
94
+ for (const [key, field] of Object.entries(meta.schema._fields ?? {})) {
95
+ const val = (_a = field == null ? void 0 : field._config) == null ? void 0 : _a.default;
96
+ if (val !== void 0) extracted[key] = val;
453
97
  }
454
- if (options == null ? void 0 : options.defaultLocale) {
455
- const { setLocale } = useLocale();
456
- if (!localStorage.getItem("blox:locale")) setLocale(options.defaultLocale);
98
+ if (Object.keys(extracted).length > 0) {
99
+ meta.defaults = extracted;
457
100
  }
458
- app.provide(BLOX_REGISTRY_KEY, this._registry);
459
- app.provide(BLOX_CONFIG_KEY, this._config);
460
- app.provide(BLOX_LOCALE_STRATEGY_KEY, this._strategy);
461
- app.component("RouterLink", _sfc_main$1);
462
101
  }
463
- }
464
- function isBlockDefinition(value) {
465
- return typeof value === "object" && "component" in value && "schema" in value;
466
- }
467
- function createBlox(options) {
468
- const instance = new BloxInstance(options);
469
- if ((options == null ? void 0 : options.websiteName) || (options == null ? void 0 : options.store)) {
470
- configureApi(options.websiteName ?? "", options.store ?? "");
471
- }
472
- if (options == null ? void 0 : options.modules) instance.registerModules(options.modules);
473
- if (options == null ? void 0 : options.components) instance.registerComponents(options.components);
474
- if (options == null ? void 0 : options.router) instance.registerRoutes(options.router);
475
- return instance;
476
- }
477
- function defineBlock(meta) {
478
102
  return meta;
479
103
  }
480
- const PreviewRenderer = vue.defineComponent({
481
- name: "PreviewRenderer",
482
- props: {
483
- blocks: {
484
- type: Array,
485
- default: () => []
486
- },
487
- selectedIndex: {
488
- type: Number,
489
- default: null
490
- },
491
- hoveredIndex: {
492
- type: Number,
493
- default: null
494
- }
495
- },
496
- setup(props) {
497
- const registry = vue.inject(BLOX_REGISTRY_KEY, {});
498
- const localHovered = vue.ref(null);
499
- function onBlockClick(index) {
500
- sendToEditor({ type: "BLOX_BLOCK_CLICK", index });
501
- }
502
- function onBlockEnter(index) {
503
- localHovered.value = index;
504
- sendToEditor({ type: "BLOX_BLOCK_HOVER", index });
505
- }
506
- function onBlockLeave() {
507
- localHovered.value = null;
508
- sendToEditor({ type: "BLOX_BLOCK_HOVER", index: null });
509
- }
510
- return () => vue.h(
511
- "div",
512
- { class: "blox-preview-canvas" },
513
- props.blocks.flatMap((block, i) => {
514
- const definition = registry[block.type];
515
- if (!definition) return [];
516
- const isSelected = props.selectedIndex === i;
517
- const isHovered = (props.hoveredIndex === i || localHovered.value === i) && !isSelected;
518
- const { label } = definition.schema;
519
- return [
520
- vue.h(
521
- "div",
522
- {
523
- "key": i,
524
- "data-blox-index": i,
525
- "class": [
526
- "blox-block-wrapper",
527
- isSelected && "blox-block--selected",
528
- isHovered && "blox-block--hovered"
529
- ],
530
- "onClick": () => {
531
- onBlockClick(i);
532
- },
533
- "onMouseenter": () => {
534
- onBlockEnter(i);
535
- },
536
- "onMouseleave": onBlockLeave
537
- },
538
- [
539
- vue.h(definition.component, { ...definition.schema.defaults, ...block.props }),
540
- vue.h("div", { class: "blox-block-overlay" }),
541
- (isSelected || isHovered) && vue.h(
542
- "div",
543
- {
544
- class: ["blox-block-label", isSelected && "blox-block-label--selected"]
545
- },
546
- label
547
- )
548
- ]
549
- )
550
- ];
551
- })
552
- );
553
- }
554
- });
555
- const _hoisted_1 = { class: "blox-preview-root" };
556
- const _sfc_main = /* @__PURE__ */ vue.defineComponent({
557
- __name: "PreviewApp",
558
- setup(__props) {
559
- const registry = vue.inject(BLOX_REGISTRY_KEY, {});
560
- const { setLocale } = useLocale();
561
- const blocks = vue.ref([]);
562
- const contexts = vue.ref({});
563
- const selectedIndex = vue.ref(null);
564
- const hoveredIndex = vue.ref(null);
565
- vue.provide("contexts", contexts);
566
- let heightObserver = null;
567
- function onMessage(event) {
568
- var _a;
569
- const msg = event.data;
570
- switch (msg.type) {
571
- case "BLOX_INIT":
572
- blocks.value = ((_a = msg.page.page.content) == null ? void 0 : _a.blocks) ?? [];
573
- contexts.value = msg.page.contexts ?? {};
574
- setLocale(msg.locale);
575
- break;
576
- case "BLOX_SET_LOCALE":
577
- setLocale(msg.locale);
578
- break;
579
- case "BLOX_SET_BLOCKS":
580
- blocks.value = msg.blocks;
581
- break;
582
- case "BLOX_SELECT_BLOCK":
583
- selectedIndex.value = msg.index;
584
- if (msg.index !== null) {
585
- vue.nextTick(() => {
586
- const el = document.querySelector(`[data-blox-index="${msg.index}"]`);
587
- el == null ? void 0 : el.scrollIntoView({ behavior: "smooth", block: "center" });
588
- });
589
- }
590
- break;
591
- case "BLOX_HOVER_BLOCK":
592
- hoveredIndex.value = msg.index;
593
- break;
594
- }
595
- }
596
- vue.onMounted(() => {
597
- window.addEventListener("message", onMessage);
598
- heightObserver = new ResizeObserver(() => {
599
- sendToEditor({ type: "BLOX_HEIGHT", height: document.body.scrollHeight });
600
- });
601
- heightObserver.observe(document.body);
602
- sendToEditor({
603
- type: "BLOX_READY",
604
- registry: Object.fromEntries(Object.entries(registry).map(([key, def]) => [key, def.schema]))
605
- });
606
- });
607
- vue.onUnmounted(() => {
608
- window.removeEventListener("message", onMessage);
609
- heightObserver == null ? void 0 : heightObserver.disconnect();
610
- });
611
- return (_ctx, _cache) => {
612
- return vue.openBlock(), vue.createElementBlock("div", _hoisted_1, [
613
- vue.createVNode(vue.unref(PreviewRenderer), {
614
- blocks: blocks.value,
615
- "selected-index": selectedIndex.value,
616
- "hovered-index": hoveredIndex.value
617
- }, null, 8, ["blocks", "selected-index", "hovered-index"])
618
- ]);
619
- };
620
- }
621
- });
622
- exports.BLOX_CONFIG_KEY = BLOX_CONFIG_KEY;
623
- exports.BLOX_LOCALE_STRATEGY_KEY = BLOX_LOCALE_STRATEGY_KEY;
624
- exports.BloxInstance = BloxInstance;
625
- exports.CmsPageView = _sfc_main$2;
626
- exports.PageRenderer = PageRenderer;
627
- exports.PreviewApp = _sfc_main;
628
- exports.PreviewRenderer = PreviewRenderer;
629
- exports.configureApi = configureApi;
630
- exports.createBlox = createBlox;
104
+ exports.BLOX_CONFIG_KEY = core.BLOX_CONFIG_KEY;
105
+ exports.BLOX_LOCALE_STRATEGY_KEY = core.BLOX_LOCALE_STRATEGY_KEY;
106
+ exports.BloxInstance = core.BloxInstance;
107
+ exports.CmsPageView = core._sfc_main;
108
+ exports.PageRenderer = core.PageRenderer;
109
+ exports.configureApi = core.configureApi;
110
+ exports.createBlox = core.createBlox;
111
+ exports.listItems = core.listItems;
112
+ exports.resolvePath = core.resolvePath;
113
+ exports.useLocale = core.useLocale;
114
+ exports.PreviewApp = PreviewApp_vue_vue_type_style_index_0_lang._sfc_main;
115
+ exports.PreviewRenderer = PreviewApp_vue_vue_type_style_index_0_lang.PreviewRenderer;
116
+ exports.sendToEditor = PreviewApp_vue_vue_type_style_index_0_lang.sendToEditor;
117
+ exports.sendToPreview = PreviewApp_vue_vue_type_style_index_0_lang.sendToPreview;
118
+ exports.createBloxApp = createBloxApp;
631
119
  exports.defineBlock = defineBlock;
632
- exports.listItems = listItems;
633
- exports.resolvePath = resolvePath;
634
- exports.sendToEditor = sendToEditor;
635
- exports.sendToPreview = sendToPreview;
636
- exports.useLocale = useLocale;
637
120
  exports.useLocaleNav = useLocaleNav;
638
121
  exports.usePageContext = usePageContext;