@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
@@ -0,0 +1,460 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ import { BLOX_WEBSITE_ID_WINDOW_KEY, BLOX_STATE_WINDOW_KEY } from "./ssg/client.mjs";
5
+ import { computed, ref, defineComponent, inject, h, provide, watch, resolveComponent, createElementBlock, openBlock, createElementVNode, createVNode, toDisplayString, unref, withCtx, createTextVNode, Fragment, createCommentVNode, createBlock, normalizeProps, guardReactiveProps, renderSlot } from "vue";
6
+ import { useRoute, useRouter, RouterLink } from "vue-router";
7
+ import { getI18n } from "@bagelink/vue";
8
+ const BASE = "/api";
9
+ let _websiteName = "";
10
+ let _store = "";
11
+ let _websiteId = null;
12
+ function configureApi(websiteName, store) {
13
+ _websiteName = websiteName;
14
+ _store = store;
15
+ _websiteId = null;
16
+ }
17
+ async function request(path, { locale: locale2, params, ...opts } = {}) {
18
+ const url = new URL(`${BASE}${path}`, window.location.origin);
19
+ if (locale2) url.searchParams.set("locale", locale2);
20
+ if (params) {
21
+ for (const [k, v] of Object.entries(params)) url.searchParams.set(k, String(v));
22
+ }
23
+ const res = await fetch(url.toString(), opts);
24
+ if (!res.ok) throw new Error(`API ${res.status}: ${path}`);
25
+ if (res.status === 204) return null;
26
+ return res.json();
27
+ }
28
+ async function getWebsiteId() {
29
+ if (_websiteId) return _websiteId;
30
+ if (typeof window !== "undefined") {
31
+ const embedded = window[BLOX_WEBSITE_ID_WINDOW_KEY];
32
+ if (typeof embedded === "string" && embedded !== "") {
33
+ _websiteId = embedded;
34
+ return _websiteId;
35
+ }
36
+ }
37
+ const websites = await request("/cms/websites");
38
+ const website = websites.find((w) => w.name === _websiteName);
39
+ if (!website) throw new Error(`Website "${_websiteName}" not found. Run: npm run seed`);
40
+ _websiteId = website.id;
41
+ return _websiteId;
42
+ }
43
+ async function resolvePath(path, locale2) {
44
+ const websiteId = await getWebsiteId();
45
+ return request(`/cms/websites/${websiteId}/resolve-path`, {
46
+ locale: locale2,
47
+ params: { path }
48
+ });
49
+ }
50
+ async function listItems(collection, { locale: locale2, q, offset = 0, limit = 100 } = {}) {
51
+ return request(`/datastore/${_store}/collections/${collection}`, {
52
+ locale: locale2,
53
+ params: { ...q ? { q } : {}, offset, limit }
54
+ });
55
+ }
56
+ const locale = ref(localStorage.getItem("blox:locale") || "en");
57
+ const currentAlternates = ref({});
58
+ function useLocale() {
59
+ const dir = computed(() => locale.value === "he" || locale.value === "ar" ? "rtl" : "ltr");
60
+ function setLocale(l) {
61
+ locale.value = l;
62
+ localStorage.setItem("blox:locale", l);
63
+ document.documentElement.lang = l;
64
+ document.documentElement.dir = dir.value;
65
+ try {
66
+ const i18n = getI18n();
67
+ if (i18n == null ? void 0 : i18n.global) i18n.global.locale.value = l;
68
+ } catch {
69
+ }
70
+ }
71
+ function setAlternates(alternates) {
72
+ currentAlternates.value = alternates;
73
+ }
74
+ return { locale, dir, setLocale, currentAlternates, setAlternates };
75
+ }
76
+ const BLOX_REGISTRY_KEY = Symbol("blox:registry");
77
+ const BLOX_CONFIG_KEY = Symbol("blox:config");
78
+ const BLOX_LOCALE_STRATEGY_KEY = Symbol("blox:locale-strategy");
79
+ const PageRenderer = defineComponent({
80
+ name: "PageRenderer",
81
+ props: {
82
+ blocks: {
83
+ type: Array,
84
+ default: () => []
85
+ }
86
+ },
87
+ setup(props) {
88
+ const registry = inject(BLOX_REGISTRY_KEY, {});
89
+ return () => h(
90
+ "div",
91
+ props.blocks.flatMap((block, i) => {
92
+ const definition = registry[block.type];
93
+ if (!definition) return [];
94
+ const props2 = { ...definition.schema.defaults, ...block.props };
95
+ return [h(definition.component, { key: i, ...props2 })];
96
+ })
97
+ );
98
+ }
99
+ });
100
+ const _hoisted_1 = {
101
+ key: 0,
102
+ class: "blox-loading"
103
+ };
104
+ const _hoisted_2 = {
105
+ key: 1,
106
+ class: "blox-not-found"
107
+ };
108
+ const _hoisted_3 = {
109
+ key: 2,
110
+ class: "blox-error color-red"
111
+ };
112
+ const _hoisted_4 = {
113
+ key: 0,
114
+ class: "blox-nav-progress"
115
+ };
116
+ const _sfc_main$1 = /* @__PURE__ */ defineComponent({
117
+ __name: "CmsPageView",
118
+ setup(__props) {
119
+ const route = useRoute();
120
+ const router = useRouter();
121
+ const { locale: locale2, setLocale, setAlternates } = useLocale();
122
+ const strategy = inject(BLOX_LOCALE_STRATEGY_KEY, null);
123
+ const blocks = ref([]);
124
+ const contexts = ref({});
125
+ provide("contexts", contexts);
126
+ const notFound = ref(false);
127
+ const error = ref(null);
128
+ const initialLoading = ref(true);
129
+ const navigating = ref(false);
130
+ function tryHydrateFromState(path) {
131
+ var _a, _b;
132
+ const win = typeof window !== "undefined" ? window : typeof globalThis !== "undefined" ? globalThis : void 0;
133
+ if (!win) return false;
134
+ const state = win[BLOX_STATE_WINDOW_KEY];
135
+ if (!state || typeof state !== "object") return false;
136
+ const entry = state[path];
137
+ if (!entry) return false;
138
+ blocks.value = ((_b = (_a = entry.page) == null ? void 0 : _a.content) == null ? void 0 : _b.blocks) ?? [];
139
+ contexts.value = entry.contexts ?? {};
140
+ setAlternates(entry.alternates ?? {});
141
+ return true;
142
+ }
143
+ function updateDocumentTitle(resolved) {
144
+ if (typeof document === "undefined") return;
145
+ const { page } = resolved;
146
+ const ctx = resolved.contexts ? Object.values(resolved.contexts).find((c) => c != null) : null;
147
+ const ctxTitle = ctx && typeof ctx.title === "string" ? ctx.title : null;
148
+ const title = page.meta_title || ctxTitle || page.title;
149
+ if (title) document.title = title;
150
+ }
151
+ const initialSlug = (() => {
152
+ var _a;
153
+ if (strategy) {
154
+ const hostname = typeof window !== "undefined" ? ((_a = window.location) == null ? void 0 : _a.hostname) ?? "" : "";
155
+ return strategy.detect(hostname, route.path).slug;
156
+ }
157
+ return route.path || "/";
158
+ })();
159
+ const ssrHydrated = tryHydrateFromState(initialSlug);
160
+ if (ssrHydrated) initialLoading.value = false;
161
+ async function load() {
162
+ var _a;
163
+ notFound.value = false;
164
+ error.value = null;
165
+ const { locale: urlLocale, slug } = strategy ? strategy.detect(typeof window !== "undefined" ? window.location.hostname : "", route.path) : { locale: locale2.value, slug: route.path || "/" };
166
+ if (urlLocale !== locale2.value) setLocale(urlLocale);
167
+ if (tryHydrateFromState(slug)) {
168
+ initialLoading.value = false;
169
+ return;
170
+ }
171
+ navigating.value = true;
172
+ try {
173
+ const resolved = await resolvePath(slug, urlLocale);
174
+ blocks.value = ((_a = resolved.page.content) == null ? void 0 : _a.blocks) ?? [];
175
+ contexts.value = resolved.contexts ?? {};
176
+ setAlternates(resolved.alternates ?? {});
177
+ updateDocumentTitle(resolved);
178
+ } catch (e) {
179
+ const msg = e instanceof Error ? e.message : String(e);
180
+ if (msg.includes("404")) {
181
+ const home = strategy ? strategy.toPath("/") : "/";
182
+ router.replace(home);
183
+ } else {
184
+ error.value = msg;
185
+ }
186
+ } finally {
187
+ initialLoading.value = false;
188
+ navigating.value = false;
189
+ }
190
+ }
191
+ if (!ssrHydrated) {
192
+ load();
193
+ }
194
+ watch(() => route.path, load);
195
+ return (_ctx, _cache) => {
196
+ const _component_RouterLink = resolveComponent("RouterLink");
197
+ return openBlock(), createElementBlock("div", null, [
198
+ initialLoading.value && blocks.value.length === 0 ? (openBlock(), createElementBlock("div", _hoisted_1, " Loading… ")) : notFound.value ? (openBlock(), createElementBlock("div", _hoisted_2, [
199
+ _cache[0] || (_cache[0] = createElementVNode("h2", null, "404", -1)),
200
+ createElementVNode("p", null, toDisplayString(unref(locale2) === "he" ? "הדף לא נמצא" : "Page not found"), 1),
201
+ createVNode(_component_RouterLink, { to: "/" }, {
202
+ default: withCtx(() => [
203
+ createTextVNode(toDisplayString(unref(locale2) === "he" ? "חזרה הביתה" : "Go home"), 1)
204
+ ]),
205
+ _: 1
206
+ })
207
+ ])) : error.value ? (openBlock(), createElementBlock("div", _hoisted_3, toDisplayString(error.value), 1)) : (openBlock(), createElementBlock(Fragment, { key: 3 }, [
208
+ navigating.value ? (openBlock(), createElementBlock("div", _hoisted_4)) : createCommentVNode("", true),
209
+ createVNode(unref(PageRenderer), { blocks: blocks.value }, null, 8, ["blocks"])
210
+ ], 64))
211
+ ]);
212
+ };
213
+ }
214
+ });
215
+ const _sfc_main = /* @__PURE__ */ defineComponent({
216
+ __name: "LocaleRouterLink",
217
+ props: {
218
+ to: {},
219
+ replace: { type: Boolean },
220
+ activeClass: {},
221
+ exactActiveClass: {},
222
+ ariaCurrentValue: {}
223
+ },
224
+ setup(__props) {
225
+ const props = __props;
226
+ const strategy = inject(BLOX_LOCALE_STRATEGY_KEY, null);
227
+ const localizedTo = computed(() => {
228
+ if (!strategy || typeof props.to !== "string") return props.to;
229
+ return strategy.toPath(props.to);
230
+ });
231
+ return (_ctx, _cache) => {
232
+ return openBlock(), createBlock(unref(RouterLink), normalizeProps(guardReactiveProps({ ...props, to: localizedTo.value })), {
233
+ default: withCtx(() => [
234
+ renderSlot(_ctx.$slots, "default")
235
+ ]),
236
+ _: 3
237
+ }, 16);
238
+ };
239
+ }
240
+ });
241
+ function createPathStrategy(options) {
242
+ const { defaultLocale, supportedLocales } = options;
243
+ let _currentLocale = defaultLocale;
244
+ return {
245
+ get locales() {
246
+ return supportedLocales;
247
+ },
248
+ get currentLocale() {
249
+ return _currentLocale;
250
+ },
251
+ detect(_hostname, path) {
252
+ const segments = path.replace(/^\//, "").split("/");
253
+ const first = segments[0];
254
+ if (supportedLocales.includes(first)) {
255
+ _currentLocale = first;
256
+ return {
257
+ locale: first,
258
+ slug: `/${segments.slice(1).join("/")}` || "/"
259
+ };
260
+ }
261
+ _currentLocale = defaultLocale;
262
+ return { locale: defaultLocale, slug: path || "/" };
263
+ },
264
+ toPath(slug) {
265
+ if (_currentLocale === defaultLocale) return slug || "/";
266
+ const s = slug || "/";
267
+ return s === "/" ? `/${_currentLocale}` : `/${_currentLocale}${s}`;
268
+ },
269
+ switchTo(locale2, slug, router) {
270
+ const s = slug || "/";
271
+ const target = locale2 === defaultLocale ? s : s === "/" ? `/${locale2}` : `/${locale2}${s}`;
272
+ router.push(target);
273
+ }
274
+ };
275
+ }
276
+ function createDomainStrategy(options) {
277
+ const { localeDomains } = options;
278
+ const locales = Object.keys(localeDomains);
279
+ const _currentLocale = locales.find((l) => localeDomains[l] === window.location.hostname) ?? locales[0] ?? "en";
280
+ return {
281
+ get locales() {
282
+ return locales;
283
+ },
284
+ get currentLocale() {
285
+ return _currentLocale;
286
+ },
287
+ detect(_hostname, path) {
288
+ return { locale: _currentLocale, slug: path || "/" };
289
+ },
290
+ toPath(slug) {
291
+ return slug || "/";
292
+ },
293
+ switchTo(locale2, slug, _router) {
294
+ const domain = localeDomains[locale2];
295
+ if (!domain) return;
296
+ if (locale2 === _currentLocale) {
297
+ _router.push(slug || "/");
298
+ return;
299
+ }
300
+ const { protocol } = window.location;
301
+ const s = slug || "/";
302
+ window.location.href = `${protocol}//${domain}${s}`;
303
+ }
304
+ };
305
+ }
306
+ function createLocaleStrategy(options) {
307
+ if (options.localeStrategy === "domain" && options.localeDomains) {
308
+ return createDomainStrategy({ localeDomains: options.localeDomains });
309
+ }
310
+ return createPathStrategy({
311
+ defaultLocale: options.defaultLocale ?? "en",
312
+ supportedLocales: options.supportedLocales ?? []
313
+ });
314
+ }
315
+ function applyDefaultsToFields(schema, defaults) {
316
+ var _a;
317
+ if (!schema || !defaults) return;
318
+ for (const [key, value] of Object.entries(defaults)) {
319
+ const field = (_a = schema._fields) == null ? void 0 : _a[key];
320
+ if (field && field._config.default === void 0) {
321
+ field._config.default = value;
322
+ }
323
+ }
324
+ }
325
+ class BloxInstance {
326
+ constructor(options) {
327
+ __publicField(this, "_registry", {});
328
+ __publicField(this, "_routeRegistered", false);
329
+ __publicField(this, "_config");
330
+ __publicField(this, "_strategy");
331
+ this._config = {
332
+ defaultLocale: (options == null ? void 0 : options.defaultLocale) ?? "en",
333
+ supportedLocales: (options == null ? void 0 : options.supportedLocales) ?? []
334
+ };
335
+ this._strategy = createLocaleStrategy(options ?? {});
336
+ }
337
+ registerModules(modules) {
338
+ const list = Array.isArray(modules) ? modules : Object.values(modules);
339
+ for (const mod of list) {
340
+ if (!(mod == null ? void 0 : mod.default)) continue;
341
+ for (const [key, value] of Object.entries(mod)) {
342
+ if (key === "default") continue;
343
+ if (value && typeof value === "object" && "label" in value) {
344
+ const meta = value;
345
+ applyDefaultsToFields(meta.schema, meta.defaults);
346
+ this._registry[meta.name ?? key] = {
347
+ component: mod.default,
348
+ schema: {
349
+ label: meta.label,
350
+ description: meta.description,
351
+ icon: meta.icon,
352
+ fields: meta.schema,
353
+ defaults: meta.defaults,
354
+ dataSource: meta.dataSource
355
+ }
356
+ };
357
+ break;
358
+ }
359
+ }
360
+ }
361
+ return this;
362
+ }
363
+ registerComponents(components) {
364
+ for (const [key, value] of Object.entries(components)) {
365
+ if (!value || typeof value !== "object" && typeof value !== "function") continue;
366
+ if (isBlockDefinition(value)) {
367
+ this._registry[key] = value;
368
+ } else if (value.__blox) {
369
+ const meta = value.__blox;
370
+ applyDefaultsToFields(meta.schema, meta.defaults);
371
+ this._registry[meta.name ?? key] = {
372
+ component: value,
373
+ schema: {
374
+ label: meta.label,
375
+ description: meta.description,
376
+ icon: meta.icon,
377
+ fields: meta.schema,
378
+ defaults: meta.defaults,
379
+ dataSource: meta.dataSource
380
+ }
381
+ };
382
+ }
383
+ }
384
+ return this;
385
+ }
386
+ registerRoutes(router) {
387
+ if (this._routeRegistered) return this;
388
+ if (this._config.supportedLocales.length > 0) {
389
+ const { defaultLocale } = this._config;
390
+ router.beforeEach((to) => {
391
+ const segments = to.path.replace(/^\//, "").split("/");
392
+ if (segments[0] === defaultLocale) {
393
+ const rest = segments.slice(1).join("/");
394
+ return { path: `/${rest}`, replace: true };
395
+ }
396
+ });
397
+ }
398
+ router.addRoute({
399
+ path: "/_blox_preview",
400
+ component: () => import("./PreviewApp-BrthGx2F.js"),
401
+ beforeEnter: () => window.parent !== window ? true : "/"
402
+ });
403
+ router.addRoute({ path: "/:pathMatch(.*)*", component: _sfc_main$1 });
404
+ this._routeRegistered = true;
405
+ return this;
406
+ }
407
+ install(app, options) {
408
+ if (options) {
409
+ this._config = {
410
+ defaultLocale: options.defaultLocale ?? this._config.defaultLocale,
411
+ supportedLocales: options.supportedLocales ?? this._config.supportedLocales
412
+ };
413
+ this._strategy = createLocaleStrategy({
414
+ ...options,
415
+ defaultLocale: this._config.defaultLocale,
416
+ supportedLocales: this._config.supportedLocales
417
+ });
418
+ }
419
+ if (options == null ? void 0 : options.modules) this.registerModules(options.modules);
420
+ if (options == null ? void 0 : options.components) this.registerComponents(options.components);
421
+ if (options == null ? void 0 : options.router) this.registerRoutes(options.router);
422
+ if ((options == null ? void 0 : options.websiteName) || (options == null ? void 0 : options.store)) {
423
+ configureApi(options.websiteName ?? "", options.store ?? "");
424
+ }
425
+ if (options == null ? void 0 : options.defaultLocale) {
426
+ const { setLocale } = useLocale();
427
+ if (!localStorage.getItem("blox:locale")) setLocale(options.defaultLocale);
428
+ }
429
+ app.provide(BLOX_REGISTRY_KEY, this._registry);
430
+ app.provide(BLOX_CONFIG_KEY, this._config);
431
+ app.provide(BLOX_LOCALE_STRATEGY_KEY, this._strategy);
432
+ app.component("RouterLink", _sfc_main);
433
+ }
434
+ }
435
+ function isBlockDefinition(value) {
436
+ return typeof value === "object" && "component" in value && "schema" in value;
437
+ }
438
+ function createBlox(options) {
439
+ const instance = new BloxInstance(options);
440
+ if ((options == null ? void 0 : options.websiteName) || (options == null ? void 0 : options.store)) {
441
+ configureApi(options.websiteName ?? "", options.store ?? "");
442
+ }
443
+ if (options == null ? void 0 : options.modules) instance.registerModules(options.modules);
444
+ if (options == null ? void 0 : options.components) instance.registerComponents(options.components);
445
+ if (options == null ? void 0 : options.router) instance.registerRoutes(options.router);
446
+ return instance;
447
+ }
448
+ export {
449
+ BLOX_LOCALE_STRATEGY_KEY as B,
450
+ PageRenderer as P,
451
+ _sfc_main$1 as _,
452
+ configureApi as a,
453
+ BloxInstance as b,
454
+ createBlox as c,
455
+ BLOX_CONFIG_KEY as d,
456
+ BLOX_REGISTRY_KEY as e,
457
+ listItems as l,
458
+ resolvePath as r,
459
+ useLocale as u
460
+ };