@b10cks/client 1.4.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,438 +1,643 @@
1
- function j(s) {
2
- if (!s) return;
3
- if (s.type === "email") {
4
- const n = new URLSearchParams();
5
- s.subject && n.set("subject", s.subject), s.body && n.set("body", s.body), s.cc && n.set("cc", s.cc), s.bcc && n.set("bcc", s.bcc);
6
- const i = n.toString();
7
- return { href: `mailto:${s.email}${i ? `?${i}` : ""}`, target: "_self" };
8
- }
9
- if (s.type === "asset")
10
- return;
11
- const t = s.target ?? "_self";
12
- let e = s.url ?? "";
13
- return "anchor" in s && s.anchor && (e = `${e.trimEnd()}#${s.anchor}`), { href: e, target: t };
1
+ //#region \0rolldown/runtime.js
2
+ var e = Object.defineProperty, t = (t, n) => {
3
+ let r = {};
4
+ for (var i in t) e(r, i, {
5
+ get: t[i],
6
+ enumerable: !0
7
+ });
8
+ return n || e(r, Symbol.toStringTag, { value: "Module" }), r;
9
+ };
10
+ //#endregion
11
+ //#region src/content.ts
12
+ function n(e) {
13
+ if (!e) return;
14
+ if (e.type === "email") {
15
+ let t = new URLSearchParams();
16
+ e.subject && t.set("subject", e.subject), e.body && t.set("body", e.body), e.cc && t.set("cc", e.cc), e.bcc && t.set("bcc", e.bcc);
17
+ let n = t.toString();
18
+ return {
19
+ href: `mailto:${e.email}${n ? `?${n}` : ""}`,
20
+ target: "_self"
21
+ };
22
+ }
23
+ if (e.type === "asset") return;
24
+ let t = e.target ?? "_self", n = e.url ?? "";
25
+ return "anchor" in e && e.anchor && (n = `${n.trimEnd()}#${e.anchor}`), {
26
+ href: n,
27
+ target: t
28
+ };
14
29
  }
15
- function v(s) {
16
- if (s != null) {
17
- if (typeof s == "boolean" || typeof s == "number") return String(s);
18
- if (typeof s == "string") return s;
19
- if (typeof s == "object" && !Array.isArray(s)) {
20
- const t = s;
21
- if ("null" in t) return "null:";
22
- if ("!null" in t) return "!null:";
23
- if ("between" in t) {
24
- const [n, i] = t.between;
25
- return `${n}...${i}`;
26
- }
27
- const e = [
28
- "eq",
29
- "neq",
30
- "like",
31
- "!like",
32
- "^like",
33
- "like$",
34
- "lt",
35
- "gt",
36
- "lte",
37
- "gte",
38
- "in",
39
- "!in",
40
- "empty",
41
- "!empty"
42
- ];
43
- for (const n of e)
44
- if (n in t) {
45
- const i = t[n];
46
- return Array.isArray(i) ? `${n}:${i.join(",")}` : `${n}:${String(i)}`;
47
- }
48
- }
49
- }
30
+ //#endregion
31
+ //#region src/data-api.ts
32
+ function r(e) {
33
+ if (e != null) {
34
+ if (typeof e == "boolean" || typeof e == "number") return String(e);
35
+ if (typeof e == "string") return e;
36
+ if (typeof e == "object" && !Array.isArray(e)) {
37
+ let t = e;
38
+ if ("null" in t) return "null:";
39
+ if ("!null" in t) return "!null:";
40
+ if ("between" in t) {
41
+ let e = t.between;
42
+ if (!Array.isArray(e) || e.length !== 2) return;
43
+ let [n, r] = e;
44
+ return `${n}...${r}`;
45
+ }
46
+ for (let e of [
47
+ "eq",
48
+ "neq",
49
+ "like",
50
+ "!like",
51
+ "^like",
52
+ "like$",
53
+ "lt",
54
+ "gt",
55
+ "lte",
56
+ "gte",
57
+ "in",
58
+ "!in",
59
+ "empty",
60
+ "!empty"
61
+ ]) if (e in t) {
62
+ let n = t[e];
63
+ return Array.isArray(n) ? `${e}:${n.join(",")}` : `${e}:${String(n)}`;
64
+ }
65
+ }
66
+ }
67
+ }
68
+ function i(e) {
69
+ let t = {};
70
+ for (let [n, i] of Object.entries(e)) {
71
+ let e = r(i);
72
+ e !== void 0 && (t[n] = e);
73
+ }
74
+ return t;
50
75
  }
51
- function R(s) {
52
- const t = {};
53
- for (const [e, n] of Object.entries(s)) {
54
- const i = v(n);
55
- i !== void 0 && (t[e] = i);
56
- }
57
- return t;
76
+ function a(e, t, n) {
77
+ let r = t && typeof t == "object" ? i(t) : {}, a = Array.isArray(n) ? { sort: n.join(",") } : n == null ? {} : { sort: n };
78
+ return {
79
+ ...e,
80
+ ...r,
81
+ ...a
82
+ };
58
83
  }
59
- function f(s, t, e) {
60
- const n = t && typeof t == "object" ? R(t) : {}, i = Array.isArray(e) ? { sort: e.join(",") } : e != null ? { sort: e } : {};
61
- return { ...s, ...n, ...i };
84
+ var o = 128, s = class {
85
+ client;
86
+ redirectsCache = /* @__PURE__ */ new Map();
87
+ configCache = /* @__PURE__ */ new Map();
88
+ configInflight = /* @__PURE__ */ new Map();
89
+ constructor(e) {
90
+ this.client = e;
91
+ }
92
+ async getResource(e, t = {}) {
93
+ let n = await this.client.get(e, t);
94
+ return this.unwrapResource(n);
95
+ }
96
+ async getCollection(e, t = {}, n = {}) {
97
+ if (n.allPages) return this.client.getAll(e, t);
98
+ let r = await this.client.get(e, t);
99
+ return this.unwrapCollection(r);
100
+ }
101
+ async getContent(e, t = {}) {
102
+ return this.getResource(`contents/${e}`, t);
103
+ }
104
+ async getContents(e = {}, t = {}) {
105
+ let { filter: n, sort: r, ...i } = e;
106
+ return this.getCollection("contents", a(i, n, r), t);
107
+ }
108
+ async getBlock(e, t = {}) {
109
+ return this.getResource(`blocks/${e}`, t);
110
+ }
111
+ async getBlocks(e = {}, t = {}) {
112
+ let { filter: n, ...r } = e;
113
+ return this.getCollection("blocks", a(r, n, void 0), t);
114
+ }
115
+ async search(e) {
116
+ let t = await this.client.get("search", e);
117
+ if (typeof t == "object" && t && "data" in t && "meta" in t) return t;
118
+ if (typeof t == "object" && t && "data" in t) {
119
+ let e = t.data;
120
+ if (typeof e == "object" && e && "meta" in e && "data" in e) return e;
121
+ }
122
+ return t;
123
+ }
124
+ async getSitemap(e = {}, t = {}) {
125
+ return this.getCollection("sitemap", e, t);
126
+ }
127
+ async getDataEntries(e, t = {}, n = {}) {
128
+ return this.getCollection(`datasources/${e}/entries`, t, n);
129
+ }
130
+ async getDataSources(e = {}, t = {}) {
131
+ return this.getCollection("datasources", e, t);
132
+ }
133
+ async getSpace(e = {}) {
134
+ return this.getResource("spaces/me", e);
135
+ }
136
+ async lookupRedirect(e) {
137
+ if (!this.client.post) throw Error("lookupRedirect requires a client that supports POST. Use ApiClient from @b10cks/client.");
138
+ return this.client.post("redirects/lookup", { source: e });
139
+ }
140
+ async getRedirects(e = {}, t = !1) {
141
+ let { allPages: n = !1, forceRefresh: r = !1 } = typeof t == "boolean" ? {
142
+ allPages: !0,
143
+ forceRefresh: t
144
+ } : t, i = JSON.stringify({
145
+ params: e,
146
+ rv: this.currentRv()
147
+ });
148
+ if (n && !r) {
149
+ let e = this.redirectsCache.get(i);
150
+ if (e) return e;
151
+ }
152
+ let { filter: o, ...s } = e, c = a(s, o, void 0), l = await this.getCollection("redirects", c, { allPages: n }), u = Object.fromEntries(l.map(({ source: e, target: t, status_code: n }) => [e, {
153
+ target: t,
154
+ status_code: n
155
+ }]));
156
+ return n && this.redirectsCache.set(i, u), u;
157
+ }
158
+ currentRv() {
159
+ return typeof this.client.getRv == "function" ? this.client.getRv() : "";
160
+ }
161
+ async getConfig({ slug: e = "_config", language_iso: t, language: n, bypassCache: r = !1, ...i } = {}) {
162
+ let a = t ?? n, o = JSON.stringify({
163
+ slug: e,
164
+ language_iso: a,
165
+ rv: this.currentRv(),
166
+ ...i
167
+ });
168
+ if (!r && this.configCache.has(o)) return this.configCache.get(o);
169
+ let s = this.configInflight.get(o);
170
+ if (!r && s) return s;
171
+ let c = this.getContent(e, {
172
+ ...i,
173
+ language_iso: a
174
+ }).then((e) => {
175
+ let t = e.content ?? {};
176
+ return r || this.setConfigCache(o, t), t;
177
+ }).finally(() => {
178
+ this.configInflight.delete(o);
179
+ });
180
+ return r || this.configInflight.set(o, c), c;
181
+ }
182
+ setConfigCache(e, t) {
183
+ if (this.configCache.delete(e), this.configCache.set(e, t), this.configCache.size > o) {
184
+ let e = this.configCache.keys().next().value;
185
+ e !== void 0 && this.configCache.delete(e);
186
+ }
187
+ }
188
+ async syncRevision(e = 426713400) {
189
+ let t = (await this.getSpace()).rv || e;
190
+ return this.client.setRv(t), t;
191
+ }
192
+ clearCache() {
193
+ this.redirectsCache.clear(), this.configCache.clear(), this.configInflight.clear();
194
+ }
195
+ unwrapResource(e) {
196
+ return typeof e == "object" && e && "data" in e ? e.data : e;
197
+ }
198
+ unwrapCollection(e) {
199
+ if (Array.isArray(e)) return e;
200
+ if (typeof e == "object" && e && "data" in e) {
201
+ if (Array.isArray(e.data)) return e.data;
202
+ if (typeof e.data == "object" && e.data !== null && "data" in e.data && Array.isArray(e.data.data)) return e.data.data;
203
+ }
204
+ return [];
205
+ }
206
+ };
207
+ function c(e) {
208
+ return new s(e);
62
209
  }
63
- class C {
64
- constructor(t) {
65
- this.client = t;
66
- }
67
- redirectsCache = null;
68
- configCache = /* @__PURE__ */ new Map();
69
- async getResource(t, e = {}) {
70
- const n = await this.client.get(t, e);
71
- return this.unwrapResource(n);
72
- }
73
- async getCollection(t, e = {}, n = {}) {
74
- if (n.allPages)
75
- return this.client.getAll(t, e);
76
- const i = await this.client.get(t, e);
77
- return this.unwrapCollection(i);
78
- }
79
- async getContent(t, e = {}) {
80
- return this.getResource(`contents/${t}`, e);
81
- }
82
- async getContents(t = {}, e = {}) {
83
- const { filter: n, sort: i, ...r } = t;
84
- return this.getCollection(
85
- "contents",
86
- f(r, n, i),
87
- e
88
- );
89
- }
90
- async getBlock(t, e = {}) {
91
- return this.getResource(`blocks/${t}`, e);
92
- }
93
- async getBlocks(t = {}, e = {}) {
94
- const { filter: n, ...i } = t;
95
- return this.getCollection(
96
- "blocks",
97
- f(i, n, void 0),
98
- e
99
- );
100
- }
101
- async search(t) {
102
- const e = await this.client.get(
103
- "search",
104
- t
105
- );
106
- if (typeof e == "object" && e !== null && "data" in e && "meta" in e)
107
- return e;
108
- if (typeof e == "object" && e !== null && "data" in e) {
109
- const n = e.data;
110
- if (typeof n == "object" && n !== null && "meta" in n && "data" in n)
111
- return n;
210
+ //#endregion
211
+ //#region src/preview-bridge.ts
212
+ var l = /* @__PURE__ */ new Set([
213
+ "CONTENT_UPDATE",
214
+ "CONTENT_PATCH",
215
+ "SELECT_UPDATE",
216
+ "HOVER_UPDATE",
217
+ "FIELD_UPDATE",
218
+ "FIELD_SELECT"
219
+ ]), u = "B10CKS_BRIDGE_READY", d = class e {
220
+ static instance;
221
+ listeners = {};
222
+ initialized = !1;
223
+ allowedOrigins = null;
224
+ parentOrigin = null;
225
+ constructor() {}
226
+ static getInstance() {
227
+ return e.instance ||= new e(), e.instance;
228
+ }
229
+ init(e = {}) {
230
+ !this.isInPreviewMode() || this.initialized || (this.allowedOrigins = e.allowedOrigins ?? null, window.addEventListener("message", this.handleMessage), this.initialized = !0, window.parent.postMessage({ type: u }, "*"));
231
+ }
232
+ destroy() {
233
+ this.initialized && typeof window < "u" && window.removeEventListener("message", this.handleMessage), this.initialized = !1, this.parentOrigin = null;
234
+ for (let e of Object.keys(this.listeners)) this.listeners[e]?.clear();
235
+ }
236
+ isInPreviewMode() {
237
+ return typeof window < "u" && window.self !== window.top;
238
+ }
239
+ on(e, t) {
240
+ if (!this.isInPreviewMode()) return () => {};
241
+ let n = this.listeners[e] || /* @__PURE__ */ new Set();
242
+ return this.listeners[e] = n, n.add(t), () => {
243
+ n.delete(t);
244
+ };
245
+ }
246
+ selectItem(e) {
247
+ this.post("SELECT_UPDATE", { selectedItem: e });
248
+ }
249
+ selectField(e, t) {
250
+ this.post("FIELD_SELECT", {
251
+ itemId: e,
252
+ path: t
253
+ });
254
+ }
255
+ updateField(e, t, n) {
256
+ this.post("FIELD_UPDATE", {
257
+ itemId: e,
258
+ field: t,
259
+ value: n
260
+ });
261
+ }
262
+ updateFieldAt(e, t, n) {
263
+ this.post("FIELD_UPDATE", {
264
+ itemId: e,
265
+ path: t,
266
+ value: n
267
+ });
268
+ }
269
+ post(e, t) {
270
+ this.isInPreviewMode() && window.parent.postMessage({
271
+ type: e,
272
+ payload: t
273
+ }, this.parentOrigin ?? "*");
274
+ }
275
+ handleMessage = (e) => {
276
+ if (!this.isOriginTrusted(e.origin) || !e.data || typeof e.data != "object") return;
277
+ let { type: t, payload: n } = e.data;
278
+ !t || !l.has(t) || !n || (!this.parentOrigin && e.origin && e.origin !== "null" && (this.parentOrigin = e.origin), this.notify(t, n));
279
+ };
280
+ isOriginTrusted(e) {
281
+ return this.parentOrigin ? e === this.parentOrigin : this.allowedOrigins ? this.allowedOrigins.includes(e) : !0;
282
+ }
283
+ notify(e, t) {
284
+ let n = this.listeners[e];
285
+ if (n) for (let e of n) e(t);
286
+ }
287
+ }, f = d.getInstance(), p = "b10cks-preview-style", m = "--b10cks-scroll-offset";
288
+ function h() {
289
+ if (typeof document > "u" || document.getElementById(p)) return;
290
+ let e = document.createElement("style");
291
+ e.id = p, e.textContent = `
292
+ .b10cks-preview { scroll-margin-top: var(${m}, 0px); }
293
+ .b10cks-hover,
294
+ .b10cks-preview:hover {
295
+ outline: 2px dashed rgba(59, 130, 246, 0.5);
296
+ outline-offset: 2px;
112
297
  }
113
- return e;
114
- }
115
- async getSitemap(t = {}, e = {}) {
116
- return this.getCollection("sitemap", t, e);
117
- }
118
- async getDataEntries(t, e = {}, n = {}) {
119
- return this.getCollection(`datasources/${t}/entries`, e, n);
120
- }
121
- async getDataSources(t = {}, e = {}) {
122
- return this.getCollection("datasources", t, e);
123
- }
124
- async getSpace(t = {}) {
125
- return this.getResource("spaces/me", t);
126
- }
127
- async lookupRedirect(t) {
128
- if (!this.client.post)
129
- throw new Error(
130
- "lookupRedirect requires a client that supports POST. Use ApiClient from @b10cks/client."
131
- );
132
- return this.client.post("redirects/lookup", { source: t });
133
- }
134
- async getRedirects(t = {}, e = !1) {
135
- const { allPages: n = !1, forceRefresh: i = !1 } = typeof e == "boolean" ? { allPages: !0, forceRefresh: e } : e;
136
- if (n && this.redirectsCache && !i)
137
- return this.redirectsCache;
138
- const { filter: r, ...a } = t, o = f(a, r, void 0), u = await this.getCollection("redirects", o, { allPages: n }), l = Object.fromEntries(
139
- u.map(({ source: m, target: b, status_code: w }) => [m, { target: b, status_code: w }])
140
- );
141
- return n && (this.redirectsCache = l), l;
142
- }
143
- async getConfig({
144
- slug: t = "_config",
145
- language_iso: e,
146
- language: n,
147
- bypassCache: i = !1,
148
- ...r
149
- } = {}) {
150
- const a = e ?? n, o = JSON.stringify({ slug: t, language_iso: a, ...r });
151
- if (!i && this.configCache.has(o))
152
- return this.configCache.get(o);
153
- const l = (await this.getContent(t, {
154
- ...r,
155
- language_iso: a
156
- })).content ?? {};
157
- return i || this.configCache.set(o, l), l;
158
- }
159
- async syncRevision(t = 426713400) {
160
- const n = (await this.getSpace()).rv || t;
161
- return this.client.setRv(n), n;
162
- }
163
- clearCache() {
164
- this.redirectsCache = null, this.configCache.clear();
165
- }
166
- unwrapResource(t) {
167
- return typeof t == "object" && t !== null && "data" in t ? t.data : t;
168
- }
169
- unwrapCollection(t) {
170
- if (Array.isArray(t))
171
- return t;
172
- if (typeof t == "object" && t !== null && "data" in t) {
173
- if (Array.isArray(t.data))
174
- return t.data;
175
- if (typeof t.data == "object" && t.data !== null && "data" in t.data && Array.isArray(t.data.data))
176
- return t.data.data;
298
+ .b10cks-selected {
299
+ outline: 2px solid rgb(59, 130, 246) !important;
300
+ outline-offset: 2px;
177
301
  }
178
- return [];
179
- }
302
+ `, document.head.appendChild(e);
180
303
  }
181
- function $(s) {
182
- return new C(s);
304
+ function g(e) {
305
+ if (typeof document > "u") return;
306
+ let t = typeof e == "number" ? `${e}px` : e;
307
+ document.documentElement.style.setProperty(m, t);
183
308
  }
184
- class c {
185
- static instance;
186
- listeners = {};
187
- initialized = !1;
188
- constructor() {
189
- }
190
- static getInstance() {
191
- return c.instance || (c.instance = new c()), c.instance;
192
- }
193
- init() {
194
- !this.isInPreviewMode() || this.initialized || (window.addEventListener("message", this.handleMessage), this.initialized = !0);
195
- }
196
- destroy() {
197
- this.initialized && typeof window < "u" && window.removeEventListener("message", this.handleMessage), this.initialized = !1;
198
- for (const t of Object.keys(this.listeners))
199
- this.listeners[t]?.clear();
200
- }
201
- isInPreviewMode() {
202
- return typeof window < "u" && window.self !== window.top;
203
- }
204
- on(t, e) {
205
- if (!this.isInPreviewMode())
206
- return () => {
207
- };
208
- const n = this.listeners[t] || /* @__PURE__ */ new Set();
209
- return this.listeners[t] = n, n.add(e), () => {
210
- n.delete(e);
211
- };
212
- }
213
- selectItem(t) {
214
- this.isInPreviewMode() && window.parent.postMessage(
215
- {
216
- type: "SELECT_UPDATE",
217
- payload: { selectedItem: t }
218
- },
219
- "*"
220
- );
221
- }
222
- updateField(t, e, n) {
223
- this.isInPreviewMode() && window.parent.postMessage(
224
- {
225
- type: "FIELD_UPDATE",
226
- payload: { itemId: t, field: e, value: n }
227
- },
228
- "*"
229
- );
230
- }
231
- handleMessage = (t) => {
232
- if (!t.data || typeof t.data != "object")
233
- return;
234
- const { type: e, payload: n } = t.data;
235
- !e || !n || this.notify(e, n);
236
- };
237
- notify(t, e) {
238
- const n = this.listeners[t];
239
- if (n)
240
- for (const i of n)
241
- i(e);
242
- }
309
+ function _(e, t) {
310
+ if (!f.isInPreviewMode() || !t.id) return () => {};
311
+ h();
312
+ let { id: n, onSelectChange: r, onHoverChange: i, scrollOnSelect: a = !0 } = t;
313
+ e.classList.add("b10cks-preview");
314
+ let o = (e) => {
315
+ e.preventDefault(), e.stopPropagation(), f.selectItem(n);
316
+ }, s = ({ selectedItem: t }) => {
317
+ let i = t === n;
318
+ e.classList.toggle("b10cks-selected", i), i && a && y(e), r?.(i);
319
+ }, c = ({ selectedItem: t }) => {
320
+ let r = t === n;
321
+ e.classList.toggle("b10cks-hover", r), i?.(r);
322
+ };
323
+ e.addEventListener("click", o);
324
+ let l = f.on("SELECT_UPDATE", s), u = f.on("HOVER_UPDATE", c);
325
+ return () => {
326
+ e.removeEventListener("click", o), l(), u(), e.classList.remove("b10cks-preview", "b10cks-selected", "b10cks-hover");
327
+ };
243
328
  }
244
- const A = c.getInstance();
245
- function p(s) {
246
- return (s ?? "").trim().replace(/^\/+|\/+$/g, "");
329
+ function v(e, t) {
330
+ if (!f.isInPreviewMode() || !t.id) return () => {};
331
+ let { id: n, field: r, path: i, mode: a = "inline" } = t;
332
+ if (a === "select") {
333
+ h(), e.classList.add("b10cks-preview");
334
+ let t = (e) => {
335
+ e.preventDefault(), e.stopPropagation(), i ? f.selectField(n, i) : f.selectItem(n);
336
+ };
337
+ return e.addEventListener("click", t), () => {
338
+ e.removeEventListener("click", t), e.classList.remove("b10cks-preview");
339
+ };
340
+ }
341
+ e.setAttribute("contenteditable", "true");
342
+ let o = (e) => {
343
+ let t = e.target.innerText;
344
+ i ? f.updateFieldAt(n, i, t) : f.updateField(n, r ?? "", t);
345
+ };
346
+ return e.addEventListener("input", o), () => {
347
+ e.removeEventListener("input", o), e.removeAttribute("contenteditable");
348
+ };
247
349
  }
248
- function y(s, t) {
249
- const e = p(s), n = p(t);
250
- if (!n)
251
- return e && e !== "home" ? `/${e}` : "/";
252
- if (!e || e === "home")
253
- return `/${n}`;
254
- const i = `${n}/`;
255
- return e === n || e.startsWith(i) ? `/${e}` : `/${n}/${e}`;
350
+ function y(e) {
351
+ typeof e.scrollIntoView == "function" && e.scrollIntoView({
352
+ block: "nearest",
353
+ behavior: "smooth"
354
+ });
256
355
  }
257
- function g(s, t) {
258
- if (!t) return null;
259
- try {
260
- return new URL(s, t).toString();
261
- } catch {
262
- return null;
263
- }
356
+ //#endregion
357
+ //#region src/preview-store.ts
358
+ function b(e, t) {
359
+ let n = e;
360
+ for (let e of t) {
361
+ if (typeof n != "object" || !n) return;
362
+ n = n[e];
363
+ }
364
+ return n;
264
365
  }
265
- function d(s) {
266
- return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
366
+ function x(e, t, n) {
367
+ if (t.length === 0) return n;
368
+ let [r, ...i] = t;
369
+ if (r === void 0) return n;
370
+ if (typeof r == "number") {
371
+ let t = Array.isArray(e) ? e.slice() : [];
372
+ return t[r] = x(t[r], i, n), t;
373
+ }
374
+ let a = e && typeof e == "object" && !Array.isArray(e) ? { ...e } : {};
375
+ return a[r] = x(a[r], i, n), a;
267
376
  }
268
- function S(s, t = {}) {
269
- const { siteUrl: e, locale: n } = t, i = /* @__PURE__ */ new Set();
270
- return s.filter((r) => {
271
- if (n && r.language_iso !== n || r.meta?.robots?.toLowerCase().includes("noindex")) return !1;
272
- const a = y(r.full_slug, r.language_iso), o = e ? g(a, e) ?? a : a;
273
- return i.has(o) ? !1 : (i.add(o), !0);
274
- });
377
+ var S = class {
378
+ content;
379
+ listeners = /* @__PURE__ */ new Set();
380
+ constructor(e) {
381
+ this.content = e;
382
+ }
383
+ subscribe = (e) => (this.listeners.add(e), () => {
384
+ this.listeners.delete(e);
385
+ });
386
+ getSnapshot = () => this.content;
387
+ setContent(e) {
388
+ this.content = e, this.emit();
389
+ }
390
+ patch(e, t) {
391
+ this.content = x(this.content, e, t), this.emit();
392
+ }
393
+ emit() {
394
+ for (let e of this.listeners) e();
395
+ }
396
+ };
397
+ function C(e) {
398
+ let t = f.on("CONTENT_UPDATE", ({ content: t }) => {
399
+ e.setContent(t);
400
+ }), n = f.on("CONTENT_PATCH", ({ path: t, value: n }) => {
401
+ e.patch(t, n);
402
+ });
403
+ return () => {
404
+ t(), n();
405
+ };
275
406
  }
276
- function P(s, t) {
277
- return [
278
- '<?xml version="1.0" encoding="UTF-8"?>',
279
- '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
280
- s.map((n) => {
281
- const i = y(n.full_slug, n.language_iso), r = t ? g(i, t) : i;
282
- if (!r) return "";
283
- const a = [" <url>", ` <loc>${d(r)}</loc>`];
284
- return n.published_at && a.push(` <lastmod>${d(n.published_at)}</lastmod>`), a.push(" </url>"), a.join(`
285
- `);
286
- }).filter(Boolean).join(`
287
- `),
288
- "</urlset>"
289
- ].join(`
290
- `);
407
+ //#endregion
408
+ //#region src/sitemap.ts
409
+ function w(e) {
410
+ return (e ?? "").trim().replace(/^\/+|\/+$/g, "");
291
411
  }
292
- function F(s, t) {
293
- return [
294
- '<?xml version="1.0" encoding="UTF-8"?>',
295
- '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
296
- s.map((n) => {
297
- const i = t ? g(n, t) : n;
298
- return i ? [" <sitemap>", ` <loc>${d(i)}</loc>`, " </sitemap>"].join(`
299
- `) : "";
300
- }).filter(Boolean).join(`
301
- `),
302
- "</sitemapindex>"
303
- ].join(`
304
- `);
412
+ function T(e, t) {
413
+ let n = w(e), r = w(t);
414
+ if (!r) return n && n !== "home" ? `/${n}` : "/";
415
+ if (!n || n === "home") return `/${r}`;
416
+ let i = `${r}/`;
417
+ return n === r || n.startsWith(i) ? `/${n}` : `/${r}/${n}`;
305
418
  }
306
- const _ = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
307
- __proto__: null
308
- }, Symbol.toStringTag, { value: "Module" }));
309
- let h = 0;
310
- class E {
311
- baseUrl;
312
- token;
313
- vid;
314
- fetchClient;
315
- getRvFn;
316
- setRvFn;
317
- constructor(t, e) {
318
- this.baseUrl = t.baseUrl.endsWith("/") ? t.baseUrl.slice(0, -1) : t.baseUrl, this.token = t.token;
319
- const n = typeof globalThis.fetch == "function" ? globalThis.fetch.bind(globalThis) : null;
320
- if (t.fetchClient)
321
- this.fetchClient = t.fetchClient;
322
- else if (n)
323
- this.fetchClient = n;
324
- else
325
- throw new Error(
326
- "No fetch implementation available. Provide `fetchClient` in ApiClient options."
327
- );
328
- this.getRvFn = t.getRv, this.setRvFn = t.setRv;
329
- const i = this.resolveRequestUrl(e);
330
- this.vid = t.version || "published", this.setRv(i?.searchParams.get("b10cks_rv") || t.rv || h);
331
- }
332
- async get(t, e = {}) {
333
- const n = this.buildUrl(t, {
334
- vid: this.vid,
335
- rv: this.getRv(),
336
- ...e,
337
- token: this.token
338
- }), i = await this.fetchClient(n), r = await this.parseResponse(i);
339
- return this.hasRevision(r) && this.setRv(r.rv), r;
340
- }
341
- async post(t, e, n = {}) {
342
- const i = this.buildUrl(t, {
343
- vid: this.vid,
344
- rv: this.getRv(),
345
- ...n,
346
- token: this.token
347
- }), r = await this.fetchClient(i, {
348
- method: "POST",
349
- headers: { "Content-Type": "application/json" },
350
- body: e !== void 0 ? JSON.stringify(e) : void 0
351
- });
352
- return this.parseResponse(r);
353
- }
354
- async getAll(t, e = {}) {
355
- const n = await this.get(t, {
356
- ...e,
357
- page: 1
358
- }), i = this.normalizeCollectionResponse(n);
359
- if (i.rv && this.setRv(i.rv), !i.meta || i.meta.last_page <= 1)
360
- return i.data;
361
- const r = Array.from(
362
- { length: i.meta.last_page - 1 },
363
- (o, u) => this.get(t, { ...e, page: u + 2 })
364
- ), a = await Promise.all(r);
365
- return i.data.concat(
366
- a.flatMap((o) => this.normalizeCollectionResponse(o).data)
367
- );
368
- }
369
- setRv(t) {
370
- this.setRvFn ? this.setRvFn(t) : h = t;
371
- }
372
- getRv() {
373
- return this.getRvFn ? this.getRvFn() : h;
374
- }
375
- async parseResponse(t) {
376
- if (this.isFetchResponse(t)) {
377
- if (!t.ok)
378
- throw new Error(`Request failed with status ${t.status}`);
379
- return await t.json();
380
- }
381
- return t;
382
- }
383
- normalizeCollectionResponse(t) {
384
- if (this.isCollectionResponseEnvelope(t)) {
385
- const e = t.data;
386
- return {
387
- data: e.data,
388
- meta: e.meta,
389
- rv: t.rv || e.rv
390
- };
391
- }
392
- if (Array.isArray(t))
393
- return { data: t, meta: void 0, rv: void 0 };
394
- if (typeof t == "object" && t !== null && "data" in t) {
395
- const {
396
- data: e,
397
- meta: n,
398
- rv: i
399
- } = t;
400
- return { data: e, meta: n, rv: i };
401
- }
402
- return { data: [], meta: void 0, rv: void 0 };
403
- }
404
- buildUrl(t, e) {
405
- const n = new URL(`${this.baseUrl}/v1/${t}`);
406
- return Object.entries(e).forEach(([i, r]) => {
407
- r != null && n.searchParams.append(i, String(r));
408
- }), n.toString();
409
- }
410
- resolveRequestUrl(t) {
411
- return t ? t instanceof URL ? t : new URL(t, "http://localhost") : null;
412
- }
413
- isFetchResponse(t) {
414
- return typeof t == "object" && t !== null && "json" in t && typeof t.json == "function" && "ok" in t;
415
- }
416
- hasRevision(t) {
417
- return typeof t == "object" && t !== null && "rv" in t;
418
- }
419
- isCollectionResponseEnvelope(t) {
420
- return typeof t == "object" && t !== null && "data" in t && typeof t.data == "object" && t.data !== null && "data" in t.data && Array.isArray(t.data.data);
421
- }
419
+ function E(e, t) {
420
+ if (!t) return null;
421
+ try {
422
+ return new URL(e, t).toString();
423
+ } catch {
424
+ return null;
425
+ }
426
+ }
427
+ function D(e) {
428
+ return e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
429
+ }
430
+ function O(e, t = {}) {
431
+ let { siteUrl: n, locale: r } = t, i = /* @__PURE__ */ new Set();
432
+ return e.filter((e) => {
433
+ if (r && e.language_iso !== r || e.meta?.robots?.toLowerCase().includes("noindex")) return !1;
434
+ let t = T(e.full_slug, e.language_iso), a = n ? E(t, n) ?? t : t;
435
+ return i.has(a) ? !1 : (i.add(a), !0);
436
+ });
437
+ }
438
+ function k(e, t) {
439
+ return [
440
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
441
+ "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">",
442
+ e.map((e) => {
443
+ let n = T(e.full_slug, e.language_iso), r = t ? E(n, t) : n;
444
+ if (!r) return "";
445
+ let i = [" <url>", ` <loc>${D(r)}</loc>`];
446
+ return e.published_at && i.push(` <lastmod>${D(e.published_at)}</lastmod>`), i.push(" </url>"), i.join("\n");
447
+ }).filter(Boolean).join("\n"),
448
+ "</urlset>"
449
+ ].join("\n");
450
+ }
451
+ function A(e, t) {
452
+ return [
453
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
454
+ "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">",
455
+ e.map((e) => {
456
+ let n = t ? E(e, t) : e;
457
+ return n ? [
458
+ " <sitemap>",
459
+ ` <loc>${D(n)}</loc>`,
460
+ " </sitemap>"
461
+ ].join("\n") : "";
462
+ }).filter(Boolean).join("\n"),
463
+ "</sitemapindex>"
464
+ ].join("\n");
422
465
  }
423
- export {
424
- E as ApiClient,
425
- C as B10cksDataApi,
426
- c as PreviewBridge,
427
- y as buildLocalizedPath,
428
- $ as createB10cksDataApi,
429
- S as filterSitemapEntries,
430
- p as normalizePathSegment,
431
- A as previewBridge,
432
- F as renderSitemapIndex,
433
- P as renderSitemapXml,
434
- j as resolveB10cksLink,
435
- R as serializeFilter,
436
- _ as types
466
+ //#endregion
467
+ //#region src/types.ts
468
+ var j = /* @__PURE__ */ t({}), M = class extends Error {
469
+ status;
470
+ endpoint;
471
+ body;
472
+ constructor(e, t, n, r) {
473
+ super(e), this.status = t, this.endpoint = n, this.body = r, this.name = "ApiError";
474
+ }
475
+ }, N = (e) => e === 429 || e >= 500 && e <= 599, P = (e) => new Promise((t) => setTimeout(t, e)), F = class {
476
+ baseUrl;
477
+ token;
478
+ vid;
479
+ fetchClient;
480
+ getRvFn;
481
+ setRvFn;
482
+ timeoutMs;
483
+ retries;
484
+ maxConcurrency;
485
+ rvValue = 0;
486
+ constructor(e, t) {
487
+ this.baseUrl = e.baseUrl.endsWith("/") ? e.baseUrl.slice(0, -1) : e.baseUrl, this.token = e.token;
488
+ let n = typeof globalThis.fetch == "function" ? globalThis.fetch.bind(globalThis) : null;
489
+ if (e.fetchClient) this.fetchClient = e.fetchClient;
490
+ else if (n) this.fetchClient = n;
491
+ else throw Error("No fetch implementation available. Provide `fetchClient` in ApiClient options.");
492
+ this.getRvFn = e.getRv, this.setRvFn = e.setRv, this.timeoutMs = e.timeoutMs, this.retries = Math.max(0, e.retries ?? 0), this.maxConcurrency = Math.max(1, e.maxConcurrency ?? 6);
493
+ let r = this.resolveRequestUrl(t);
494
+ this.vid = e.version || "published", this.setRv(r?.searchParams.get("b10cks_rv") || e.rv || 0);
495
+ }
496
+ async get(e, t = {}) {
497
+ let n = this.buildUrl(e, {
498
+ vid: this.vid,
499
+ rv: this.getRv(),
500
+ ...t,
501
+ token: this.token
502
+ }), r = await this.requestWithRetry(n, e);
503
+ return this.hasRevision(r) && this.setRv(r.rv), r;
504
+ }
505
+ async post(e, t, n = {}) {
506
+ let r = this.buildUrl(e, {
507
+ vid: this.vid,
508
+ rv: this.getRv(),
509
+ ...n,
510
+ token: this.token
511
+ }), i = await this.fetchWithTimeout(r, {
512
+ method: "POST",
513
+ headers: { "Content-Type": "application/json" },
514
+ body: t === void 0 ? void 0 : JSON.stringify(t)
515
+ });
516
+ return this.parseResponse(i, e);
517
+ }
518
+ async fetchWithTimeout(e, t) {
519
+ if (!this.timeoutMs) return this.fetchClient(e, t);
520
+ let n = new AbortController(), r = setTimeout(() => n.abort(), this.timeoutMs);
521
+ try {
522
+ return await this.fetchClient(e, {
523
+ ...t,
524
+ signal: n.signal
525
+ });
526
+ } catch (t) {
527
+ throw t instanceof Error && t.name === "AbortError" ? new M(`Request timed out after ${this.timeoutMs}ms`, 0, e) : t;
528
+ } finally {
529
+ clearTimeout(r);
530
+ }
531
+ }
532
+ async requestWithRetry(e, t) {
533
+ let n;
534
+ for (let r = 0; r <= this.retries; r++) try {
535
+ let n = await this.fetchWithTimeout(e);
536
+ return await this.parseResponse(n, t);
537
+ } catch (e) {
538
+ if (n = e, !(r < this.retries && (!(e instanceof M) || e.status === 0 || N(e.status)))) throw e;
539
+ await P(200 * 2 ** r);
540
+ }
541
+ throw n;
542
+ }
543
+ async getAll(e, t = {}) {
544
+ let n = await this.get(e, {
545
+ ...t,
546
+ page: 1
547
+ }), r = this.normalizeCollectionResponse(n);
548
+ if (r.rv && this.setRv(r.rv), !r.meta || r.meta.last_page <= 1) return r.data;
549
+ let i = Array.from({ length: r.meta.last_page - 1 }, (e, t) => t + 2), a = await this.mapWithConcurrency(i, (n) => this.get(e, {
550
+ ...t,
551
+ page: n
552
+ }));
553
+ return r.data.concat(a.flatMap((e) => this.normalizeCollectionResponse(e).data));
554
+ }
555
+ async mapWithConcurrency(e, t) {
556
+ let n = Array.from({ length: e.length }), r = 0, i = async () => {
557
+ for (; r < e.length;) {
558
+ let i = r++;
559
+ n[i] = await t(e[i]);
560
+ }
561
+ }, a = Array.from({ length: Math.min(this.maxConcurrency, e.length) }, () => i());
562
+ return await Promise.all(a), n;
563
+ }
564
+ setRv(e) {
565
+ this.setRvFn ? this.setRvFn(e) : this.rvValue = e;
566
+ }
567
+ getRv() {
568
+ return this.getRvFn ? this.getRvFn() : this.rvValue;
569
+ }
570
+ async parseResponse(e, t) {
571
+ if (this.isFetchResponse(e)) {
572
+ if (!e.ok) {
573
+ let n = await this.readErrorBody(e);
574
+ throw new M(`Request to "${t}" failed with status ${e.status}`, e.status, t, n);
575
+ }
576
+ return await e.json();
577
+ }
578
+ return e;
579
+ }
580
+ async readErrorBody(e) {
581
+ try {
582
+ let t = await e.text();
583
+ if (!t) return;
584
+ try {
585
+ return JSON.parse(t);
586
+ } catch {
587
+ return t;
588
+ }
589
+ } catch {
590
+ return;
591
+ }
592
+ }
593
+ normalizeCollectionResponse(e) {
594
+ if (this.isCollectionResponseEnvelope(e)) {
595
+ let t = e.data;
596
+ return {
597
+ data: t.data,
598
+ meta: t.meta,
599
+ rv: e.rv || t.rv
600
+ };
601
+ }
602
+ if (Array.isArray(e)) return {
603
+ data: e,
604
+ meta: void 0,
605
+ rv: void 0
606
+ };
607
+ if (typeof e == "object" && e && "data" in e) {
608
+ let { data: t, meta: n, rv: r } = e;
609
+ return {
610
+ data: t,
611
+ meta: n,
612
+ rv: r
613
+ };
614
+ }
615
+ return {
616
+ data: [],
617
+ meta: void 0,
618
+ rv: void 0
619
+ };
620
+ }
621
+ buildUrl(e, t) {
622
+ let n = new URL(`${this.baseUrl}/v1/${e}`);
623
+ return Object.entries(t).forEach(([e, t]) => {
624
+ t != null && n.searchParams.append(e, String(t));
625
+ }), n.toString();
626
+ }
627
+ resolveRequestUrl(e) {
628
+ return e ? e instanceof URL ? e : new URL(e, "http://localhost") : null;
629
+ }
630
+ isFetchResponse(e) {
631
+ return typeof e == "object" && !!e && "json" in e && typeof e.json == "function" && "ok" in e;
632
+ }
633
+ hasRevision(e) {
634
+ return typeof e == "object" && !!e && "rv" in e && e.rv != null;
635
+ }
636
+ isCollectionResponseEnvelope(e) {
637
+ return typeof e == "object" && !!e && "data" in e && typeof e.data == "object" && e.data !== null && "data" in e.data && Array.isArray(e.data.data);
638
+ }
437
639
  };
438
- //# sourceMappingURL=index.mjs.map
640
+ //#endregion
641
+ export { F as ApiClient, M as ApiError, s as B10cksDataApi, d as PreviewBridge, S as PreviewStore, _ as attachEditable, v as attachEditableField, C as bindPreviewStore, T as buildLocalizedPath, c as createB10cksDataApi, h as ensurePreviewStyles, O as filterSitemapEntries, b as getAtPath, w as normalizePathSegment, f as previewBridge, A as renderSitemapIndex, k as renderSitemapXml, n as resolveB10cksLink, i as serializeFilter, x as setAtPath, g as setPreviewScrollOffset, j as types };
642
+
643
+ //# sourceMappingURL=index.mjs.map