@storyblok/react 2.2.0 → 2.3.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.
@@ -1,858 +0,0 @@
1
- let R = false;
2
- const T = [], j = (n) => new Promise((e, t) => {
3
- if (typeof window > "u" || (window.storyblokRegisterEvent = (r) => {
4
- if (window.location === window.parent.location) {
5
- console.warn("You are not in Draft Mode or in the Visual Editor.");
6
- return;
7
- }
8
- R ? r() : T.push(r);
9
- }, document.getElementById("storyblok-javascript-bridge")))
10
- return;
11
- const s = document.createElement("script");
12
- s.async = true, s.src = n, s.id = "storyblok-javascript-bridge", s.onerror = (r) => t(r), s.onload = (r) => {
13
- T.forEach((i) => i()), R = true, e(r);
14
- }, document.getElementsByTagName("head")[0].appendChild(s);
15
- });
16
- var I = Object.defineProperty, P = (n, e, t) => e in n ? I(n, e, { enumerable: true, configurable: true, writable: true, value: t }) : n[e] = t, h = (n, e, t) => (P(n, typeof e != "symbol" ? e + "" : e, t), t);
17
- function _(n) {
18
- return !(n !== n || n === 1 / 0 || n === -1 / 0);
19
- }
20
- function S(n, e, t) {
21
- if (!_(e))
22
- throw new TypeError("Expected `limit` to be a finite number");
23
- if (!_(t))
24
- throw new TypeError("Expected `interval` to be a finite number");
25
- const s = [];
26
- let r = [], i = 0;
27
- const o = function() {
28
- i++;
29
- const a = setTimeout(function() {
30
- i--, s.length > 0 && o(), r = r.filter(function(u) {
31
- return u !== a;
32
- });
33
- }, t);
34
- r.indexOf(a) < 0 && r.push(a);
35
- const l = s.shift();
36
- l.resolve(n.apply(l.self, l.args));
37
- }, c = function(...a) {
38
- const l = this;
39
- return new Promise(function(u, p) {
40
- s.push({
41
- resolve: u,
42
- reject: p,
43
- args: a,
44
- self: l
45
- }), i < e && o();
46
- });
47
- };
48
- return c.abort = function() {
49
- r.forEach(clearTimeout), r = [], s.forEach(function(a) {
50
- a.reject(function() {
51
- Error.call(this, "Throttled function aborted"), this.name = "AbortError";
52
- });
53
- }), s.length = 0;
54
- }, c;
55
- }
56
- const C = function(n, e) {
57
- const t = {};
58
- for (const s in n) {
59
- const r = n[s];
60
- e.indexOf(s) > -1 && r !== null && (t[s] = r);
61
- }
62
- return t;
63
- }, O = (n) => n === "email", A = () => ({
64
- singleTag: "hr"
65
- }), N = () => ({
66
- tag: "blockquote"
67
- }), L = () => ({
68
- tag: "ul"
69
- }), M = (n) => ({
70
- tag: [
71
- "pre",
72
- {
73
- tag: "code",
74
- attrs: n.attrs
75
- }
76
- ]
77
- }), z = () => ({
78
- singleTag: "br"
79
- }), U = (n) => ({
80
- tag: `h${n.attrs.level}`
81
- }), q = (n) => ({
82
- singleTag: [
83
- {
84
- tag: "img",
85
- attrs: C(n.attrs, ["src", "alt", "title"])
86
- }
87
- ]
88
- }), H = () => ({
89
- tag: "li"
90
- }), B = () => ({
91
- tag: "ol"
92
- }), D = () => ({
93
- tag: "p"
94
- }), J = (n) => ({
95
- tag: [
96
- {
97
- tag: "span",
98
- attrs: {
99
- ["data-type"]: "emoji",
100
- ["data-name"]: n.attrs.name,
101
- emoji: n.attrs.emoji
102
- }
103
- }
104
- ]
105
- }), F = () => ({
106
- tag: "b"
107
- }), V = () => ({
108
- tag: "strike"
109
- }), Y = () => ({
110
- tag: "u"
111
- }), K = () => ({
112
- tag: "strong"
113
- }), W = () => ({
114
- tag: "code"
115
- }), G = () => ({
116
- tag: "i"
117
- }), Q = (n) => {
118
- const e = { ...n.attrs }, { linktype: t = "url" } = n.attrs;
119
- if (O(t) && (e.href = `mailto:${e.href}`), e.anchor && (e.href = `${e.href}#${e.anchor}`, delete e.anchor), e.custom) {
120
- for (const s in e.custom)
121
- e[s] = e.custom[s];
122
- delete e.custom;
123
- }
124
- return {
125
- tag: [
126
- {
127
- tag: "a",
128
- attrs: e
129
- }
130
- ]
131
- };
132
- }, X = (n) => ({
133
- tag: [
134
- {
135
- tag: "span",
136
- attrs: n.attrs
137
- }
138
- ]
139
- }), Z = () => ({
140
- tag: "sub"
141
- }), ee = () => ({
142
- tag: "sup"
143
- }), te = (n) => ({
144
- tag: [
145
- {
146
- tag: "span",
147
- attrs: n.attrs
148
- }
149
- ]
150
- }), se = (n) => {
151
- var e;
152
- return (e = n.attrs) != null && e.color ? {
153
- tag: [
154
- {
155
- tag: "span",
156
- attrs: {
157
- style: `background-color:${n.attrs.color};`
158
- }
159
- }
160
- ]
161
- } : {
162
- tag: ""
163
- };
164
- }, re = (n) => {
165
- var e;
166
- return (e = n.attrs) != null && e.color ? {
167
- tag: [
168
- {
169
- tag: "span",
170
- attrs: {
171
- style: `color:${n.attrs.color}`
172
- }
173
- }
174
- ]
175
- } : {
176
- tag: ""
177
- };
178
- }, ie = {
179
- nodes: {
180
- horizontal_rule: A,
181
- blockquote: N,
182
- bullet_list: L,
183
- code_block: M,
184
- hard_break: z,
185
- heading: U,
186
- image: q,
187
- list_item: H,
188
- ordered_list: B,
189
- paragraph: D,
190
- emoji: J
191
- },
192
- marks: {
193
- bold: F,
194
- strike: V,
195
- underline: Y,
196
- strong: K,
197
- code: W,
198
- italic: G,
199
- link: Q,
200
- styled: X,
201
- subscript: Z,
202
- superscript: ee,
203
- anchor: te,
204
- highlight: se,
205
- textStyle: re
206
- }
207
- }, ne = function(n) {
208
- const e = {
209
- "&": "&amp;",
210
- "<": "&lt;",
211
- ">": "&gt;",
212
- '"': "&quot;",
213
- "'": "&#39;"
214
- }, t = /[&<>"']/g, s = RegExp(t.source);
215
- return n && s.test(n) ? n.replace(t, (r) => e[r]) : n;
216
- };
217
- class y {
218
- constructor(e) {
219
- h(this, "marks"), h(this, "nodes"), e || (e = ie), this.marks = e.marks || [], this.nodes = e.nodes || [];
220
- }
221
- addNode(e, t) {
222
- this.nodes[e] = t;
223
- }
224
- addMark(e, t) {
225
- this.marks[e] = t;
226
- }
227
- render(e, t = { optimizeImages: false }) {
228
- if (e && e.content && Array.isArray(e.content)) {
229
- let s = "";
230
- return e.content.forEach((r) => {
231
- s += this.renderNode(r);
232
- }), t.optimizeImages ? this.optimizeImages(s, t.optimizeImages) : s;
233
- }
234
- return console.warn(
235
- `The render method must receive an Object with a "content" field.
236
- The "content" field must be an array of nodes as the type ISbRichtext.
237
- ISbRichtext:
238
- content?: ISbRichtext[]
239
- marks?: ISbRichtext[]
240
- attrs?: any
241
- text?: string
242
- type: string
243
-
244
- Example:
245
- {
246
- content: [
247
- {
248
- content: [
249
- {
250
- text: 'Hello World',
251
- type: 'text'
252
- }
253
- ],
254
- type: 'paragraph'
255
- }
256
- ],
257
- type: 'doc'
258
- }`
259
- ), "";
260
- }
261
- optimizeImages(e, t) {
262
- let s = 0, r = 0, i = "", o = "";
263
- typeof t != "boolean" && (typeof t.width == "number" && t.width > 0 && (i += `width="${t.width}" `, s = t.width), typeof t.height == "number" && t.height > 0 && (i += `height="${t.height}" `, r = t.height), (t.loading === "lazy" || t.loading === "eager") && (i += `loading="${t.loading}" `), typeof t.class == "string" && t.class.length > 0 && (i += `class="${t.class}" `), t.filters && (typeof t.filters.blur == "number" && t.filters.blur >= 0 && t.filters.blur <= 100 && (o += `:blur(${t.filters.blur})`), typeof t.filters.brightness == "number" && t.filters.brightness >= -100 && t.filters.brightness <= 100 && (o += `:brightness(${t.filters.brightness})`), t.filters.fill && (t.filters.fill.match(/[0-9A-Fa-f]{6}/g) || t.filters.fill === "transparent") && (o += `:fill(${t.filters.fill})`), t.filters.format && ["webp", "png", "jpeg"].includes(t.filters.format) && (o += `:format(${t.filters.format})`), typeof t.filters.grayscale == "boolean" && t.filters.grayscale && (o += ":grayscale()"), typeof t.filters.quality == "number" && t.filters.quality >= 0 && t.filters.quality <= 100 && (o += `:quality(${t.filters.quality})`), t.filters.rotate && [90, 180, 270].includes(t.filters.rotate) && (o += `:rotate(${t.filters.rotate})`), o.length > 0 && (o = "/filters" + o))), i.length > 0 && (e = e.replace(/<img/g, `<img ${i.trim()}`));
264
- const c = s > 0 || r > 0 || o.length > 0 ? `${s}x${r}${o}` : "";
265
- return e = e.replace(
266
- /a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g,
267
- `a.storyblok.com/f/$1/$2.$3/m/${c}`
268
- ), typeof t != "boolean" && (t.sizes || t.srcset) && (e = e.replace(/<img.*?src=["|'](.*?)["|']/g, (a) => {
269
- var l, u;
270
- const p = a.match(
271
- /a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g
272
- );
273
- if (p && p.length > 0) {
274
- const g = {
275
- srcset: (l = t.srcset) == null ? void 0 : l.map((d) => {
276
- if (typeof d == "number")
277
- return `//${p}/m/${d}x0${o} ${d}w`;
278
- if (typeof d == "object" && d.length === 2) {
279
- let v = 0, $ = 0;
280
- return typeof d[0] == "number" && (v = d[0]), typeof d[1] == "number" && ($ = d[1]), `//${p}/m/${v}x${$}${o} ${v}w`;
281
- }
282
- }).join(", "),
283
- sizes: (u = t.sizes) == null ? void 0 : u.map((d) => d).join(", ")
284
- };
285
- let b = "";
286
- return g.srcset && (b += `srcset="${g.srcset}" `), g.sizes && (b += `sizes="${g.sizes}" `), a.replace(/<img/g, `<img ${b.trim()}`);
287
- }
288
- return a;
289
- })), e;
290
- }
291
- renderNode(e) {
292
- const t = [];
293
- e.marks && e.marks.forEach((r) => {
294
- const i = this.getMatchingMark(r);
295
- i && i.tag !== "" && t.push(this.renderOpeningTag(i.tag));
296
- });
297
- const s = this.getMatchingNode(e);
298
- return s && s.tag && t.push(this.renderOpeningTag(s.tag)), e.content ? e.content.forEach((r) => {
299
- t.push(this.renderNode(r));
300
- }) : e.text ? t.push(ne(e.text)) : s && s.singleTag ? t.push(this.renderTag(s.singleTag, " /")) : s && s.html ? t.push(s.html) : e.type === "emoji" && t.push(this.renderEmoji(e)), s && s.tag && t.push(this.renderClosingTag(s.tag)), e.marks && e.marks.slice(0).reverse().forEach((r) => {
301
- const i = this.getMatchingMark(r);
302
- i && i.tag !== "" && t.push(this.renderClosingTag(i.tag));
303
- }), t.join("");
304
- }
305
- renderTag(e, t) {
306
- return e.constructor === String ? `<${e}${t}>` : e.map((s) => {
307
- if (s.constructor === String)
308
- return `<${s}${t}>`;
309
- {
310
- let r = `<${s.tag}`;
311
- if (s.attrs)
312
- for (const i in s.attrs) {
313
- const o = s.attrs[i];
314
- o !== null && (r += ` ${i}="${o}"`);
315
- }
316
- return `${r}${t}>`;
317
- }
318
- }).join("");
319
- }
320
- renderOpeningTag(e) {
321
- return this.renderTag(e, "");
322
- }
323
- renderClosingTag(e) {
324
- return e.constructor === String ? `</${e}>` : e.slice(0).reverse().map((t) => t.constructor === String ? `</${t}>` : `</${t.tag}>`).join("");
325
- }
326
- getMatchingNode(e) {
327
- const t = this.nodes[e.type];
328
- if (typeof t == "function")
329
- return t(e);
330
- }
331
- getMatchingMark(e) {
332
- const t = this.marks[e.type];
333
- if (typeof t == "function")
334
- return t(e);
335
- }
336
- renderEmoji(e) {
337
- if (e.attrs.emoji)
338
- return e.attrs.emoji;
339
- const t = [
340
- {
341
- tag: "img",
342
- attrs: {
343
- src: e.attrs.fallbackImage,
344
- draggable: "false",
345
- loading: "lazy",
346
- align: "absmiddle"
347
- }
348
- }
349
- ];
350
- return this.renderTag(t, " /");
351
- }
352
- }
353
- class k {
354
- constructor() {
355
- h(this, "isCDNUrl", (e = "") => e.indexOf("/cdn/") > -1), h(this, "getOptionsPage", (e, t = 25, s = 1) => ({
356
- ...e,
357
- per_page: t,
358
- page: s
359
- })), h(this, "delay", (e) => new Promise((t) => setTimeout(t, e))), h(this, "arrayFrom", (e = 0, t) => [...Array(e)].map(t)), h(this, "range", (e = 0, t = e) => {
360
- const s = Math.abs(t - e) || 0, r = e < t ? 1 : -1;
361
- return this.arrayFrom(s, (i, o) => o * r + e);
362
- }), h(this, "asyncMap", async (e, t) => Promise.all(e.map(t))), h(this, "flatMap", (e = [], t) => e.map(t).reduce((s, r) => [...s, ...r], []));
363
- }
364
- /**
365
- * @method stringify
366
- * @param {Object} params
367
- * @param {String} prefix
368
- * @param {Boolean} isArray
369
- * @return {String} Stringified object
370
- */
371
- stringify(e, t, s) {
372
- const r = [];
373
- for (const i in e) {
374
- if (!Object.prototype.hasOwnProperty.call(e, i))
375
- continue;
376
- const o = e[i], c = s ? "" : encodeURIComponent(i);
377
- let a;
378
- typeof o == "object" ? a = this.stringify(
379
- o,
380
- t ? t + encodeURIComponent("[" + c + "]") : c,
381
- Array.isArray(o)
382
- ) : a = (t ? t + encodeURIComponent("[" + c + "]") : c) + "=" + encodeURIComponent(o), r.push(a);
383
- }
384
- return r.join("&");
385
- }
386
- /**
387
- * @method getRegionURL
388
- * @param {String} regionCode region code, could be eu, us or cn
389
- * @return {String} The base URL of the region
390
- */
391
- getRegionURL(e) {
392
- const t = "api.storyblok.com", s = "api-us.storyblok.com", r = "app.storyblokchina.cn";
393
- switch (e) {
394
- case "us":
395
- return s;
396
- case "cn":
397
- return r;
398
- default:
399
- return t;
400
- }
401
- }
402
- }
403
- class oe {
404
- constructor(e) {
405
- h(this, "baseURL"), h(this, "timeout"), h(this, "headers"), h(this, "responseInterceptor"), h(this, "fetch"), h(this, "ejectInterceptor"), h(this, "url"), h(this, "parameters"), this.baseURL = e.baseURL, this.headers = e.headers || new Headers(), this.timeout = e != null && e.timeout ? e.timeout * 1e3 : 0, this.responseInterceptor = e.responseInterceptor, this.fetch = (...t) => e.fetch ? e.fetch(...t) : fetch(...t), this.ejectInterceptor = false, this.url = "", this.parameters = {};
406
- }
407
- /**
408
- *
409
- * @param url string
410
- * @param params ISbStoriesParams
411
- * @returns Promise<ISbResponse | Error>
412
- */
413
- get(e, t) {
414
- return this.url = e, this.parameters = t, this._methodHandler("get");
415
- }
416
- post(e, t) {
417
- return this.url = e, this.parameters = t, this._methodHandler("post");
418
- }
419
- put(e, t) {
420
- return this.url = e, this.parameters = t, this._methodHandler("put");
421
- }
422
- delete(e, t) {
423
- return this.url = e, this.parameters = t, this._methodHandler("delete");
424
- }
425
- async _responseHandler(e) {
426
- const t = [], s = {
427
- data: {},
428
- headers: {},
429
- status: 0,
430
- statusText: ""
431
- };
432
- e.status !== 204 && await e.json().then((r) => {
433
- s.data = r;
434
- });
435
- for (const r of e.headers.entries())
436
- t[r[0]] = r[1];
437
- return s.headers = { ...t }, s.status = e.status, s.statusText = e.statusText, s;
438
- }
439
- async _methodHandler(e) {
440
- let t = `${this.baseURL}${this.url}`, s = null;
441
- if (e === "get") {
442
- const a = new k();
443
- t = `${this.baseURL}${this.url}?${a.stringify(
444
- this.parameters
445
- )}`;
446
- } else
447
- s = JSON.stringify(this.parameters);
448
- const r = new URL(t), i = new AbortController(), { signal: o } = i;
449
- let c;
450
- this.timeout && (c = setTimeout(() => i.abort(), this.timeout));
451
- try {
452
- const a = await this.fetch(`${r}`, {
453
- method: e,
454
- headers: this.headers,
455
- body: s,
456
- signal: o
457
- });
458
- this.timeout && clearTimeout(c);
459
- const l = await this._responseHandler(a);
460
- return this.responseInterceptor && !this.ejectInterceptor ? this._statusHandler(this.responseInterceptor(l)) : this._statusHandler(l);
461
- } catch (a) {
462
- return {
463
- message: a
464
- };
465
- }
466
- }
467
- eject() {
468
- this.ejectInterceptor = true;
469
- }
470
- _statusHandler(e) {
471
- const t = /20[0-6]/g;
472
- return new Promise((s, r) => {
473
- if (t.test(`${e.status}`))
474
- return s(e);
475
- const i = {
476
- message: new Error(e.statusText),
477
- status: e.status,
478
- response: Array.isArray(e.data) ? e.data[0] : e.data.error || e.data.slug
479
- };
480
- r(i);
481
- });
482
- }
483
- }
484
- let m = {};
485
- const f = {};
486
- class ae {
487
- /**
488
- *
489
- * @param config ISbConfig interface
490
- * @param endpoint string, optional
491
- */
492
- constructor(e, t) {
493
- h(this, "client"), h(this, "maxRetries"), h(this, "throttle"), h(this, "accessToken"), h(this, "cache"), h(this, "helpers"), h(this, "resolveCounter"), h(this, "relations"), h(this, "links"), h(this, "richTextResolver"), h(this, "resolveNestedRelations");
494
- let s = e.endpoint || t;
495
- if (!s) {
496
- const o = new k().getRegionURL, c = e.https === false ? "http" : "https";
497
- e.oauthToken ? s = `${c}://${o(e.region)}/v1` : s = `${c}://${o(e.region)}/v2`;
498
- }
499
- const r = new Headers();
500
- r.set("Content-Type", "application/json"), r.set("Accept", "application/json"), r.forEach((o, c) => {
501
- e.headers && e.headers[c] && r.set(c, e.headers[c]);
502
- });
503
- let i = 5;
504
- e.oauthToken && (r.set("Authorization", e.oauthToken), i = 3), e.rateLimit && (i = e.rateLimit), e.richTextSchema ? this.richTextResolver = new y(e.richTextSchema) : this.richTextResolver = new y(), e.componentResolver && this.setComponentResolver(e.componentResolver), this.maxRetries = e.maxRetries || 5, this.throttle = S(this.throttledRequest, i, 1e3), this.accessToken = e.accessToken || "", this.relations = {}, this.links = {}, this.cache = e.cache || { clear: "manual" }, this.helpers = new k(), this.resolveCounter = 0, this.resolveNestedRelations = e.resolveNestedRelations || true, this.client = new oe({
505
- baseURL: s,
506
- timeout: e.timeout || 0,
507
- headers: r,
508
- responseInterceptor: e.responseInterceptor,
509
- fetch: e.fetch
510
- });
511
- }
512
- setComponentResolver(e) {
513
- this.richTextResolver.addNode("blok", (t) => {
514
- let s = "";
515
- return t.attrs.body && t.attrs.body.forEach((r) => {
516
- s += e(r.component, r);
517
- }), {
518
- html: s
519
- };
520
- });
521
- }
522
- parseParams(e) {
523
- return e.version || (e.version = "published"), e.token || (e.token = this.getToken()), e.cv || (e.cv = f[e.token]), Array.isArray(e.resolve_relations) && (e.resolve_relations = e.resolve_relations.join(",")), e;
524
- }
525
- factoryParamOptions(e, t) {
526
- return this.helpers.isCDNUrl(e) ? this.parseParams(t) : t;
527
- }
528
- makeRequest(e, t, s, r) {
529
- const i = this.factoryParamOptions(
530
- e,
531
- this.helpers.getOptionsPage(t, s, r)
532
- );
533
- return this.cacheResponse(e, i);
534
- }
535
- get(e, t) {
536
- t || (t = {});
537
- const s = `/${e}`, r = this.factoryParamOptions(s, t);
538
- return this.cacheResponse(s, r);
539
- }
540
- async getAll(e, t, s) {
541
- const r = (t == null ? void 0 : t.per_page) || 25, i = `/${e}`, o = i.split("/"), c = s || o[o.length - 1], a = 1, l = await this.makeRequest(i, t, r, a), u = l.total ? Math.ceil(l.total / r) : 1, p = await this.helpers.asyncMap(
542
- this.helpers.range(a, u),
543
- (g) => this.makeRequest(i, t, r, g + 1)
544
- );
545
- return this.helpers.flatMap(
546
- [l, ...p],
547
- (g) => Object.values(g.data[c])
548
- );
549
- }
550
- post(e, t) {
551
- const s = `/${e}`;
552
- return Promise.resolve(this.throttle("post", s, t));
553
- }
554
- put(e, t) {
555
- const s = `/${e}`;
556
- return Promise.resolve(this.throttle("put", s, t));
557
- }
558
- delete(e, t) {
559
- const s = `/${e}`;
560
- return Promise.resolve(this.throttle("delete", s, t));
561
- }
562
- getStories(e) {
563
- return this.get("cdn/stories", e);
564
- }
565
- getStory(e, t) {
566
- return this.get(`cdn/stories/${e}`, t);
567
- }
568
- getToken() {
569
- return this.accessToken;
570
- }
571
- ejectInterceptor() {
572
- this.client.eject();
573
- }
574
- _cleanCopy(e) {
575
- return JSON.parse(JSON.stringify(e));
576
- }
577
- _insertLinks(e, t, s) {
578
- const r = e[t];
579
- r && r.fieldtype == "multilink" && r.linktype == "story" && typeof r.id == "string" && this.links[s][r.id] ? r.story = this._cleanCopy(this.links[s][r.id]) : r && r.linktype === "story" && typeof r.uuid == "string" && this.links[s][r.uuid] && (r.story = this._cleanCopy(this.links[s][r.uuid]));
580
- }
581
- _insertRelations(e, t, s, r) {
582
- if (s.indexOf(`${e.component}.${t}`) > -1) {
583
- if (typeof e[t] == "string")
584
- this.relations[r][e[t]] && (e[t] = this._cleanCopy(
585
- this.relations[r][e[t]]
586
- ));
587
- else if (e[t] && e[t].constructor === Array) {
588
- const i = [];
589
- e[t].forEach((o) => {
590
- this.relations[r][o] && i.push(this._cleanCopy(this.relations[r][o]));
591
- }), e[t] = i;
592
- }
593
- }
594
- }
595
- iterateTree(e, t, s) {
596
- const r = (i) => {
597
- if (i != null) {
598
- if (i.constructor === Array)
599
- for (let o = 0; o < i.length; o++)
600
- r(i[o]);
601
- else if (i.constructor === Object) {
602
- if (i._stopResolving)
603
- return;
604
- for (const o in i)
605
- (i.component && i._uid || i.type === "link") && (this._insertRelations(
606
- i,
607
- o,
608
- t,
609
- s
610
- ), this._insertLinks(
611
- i,
612
- o,
613
- s
614
- )), r(i[o]);
615
- }
616
- }
617
- };
618
- r(e.content);
619
- }
620
- async resolveLinks(e, t, s) {
621
- let r = [];
622
- if (e.link_uuids) {
623
- const i = e.link_uuids.length, o = [], c = 50;
624
- for (let a = 0; a < i; a += c) {
625
- const l = Math.min(i, a + c);
626
- o.push(e.link_uuids.slice(a, l));
627
- }
628
- for (let a = 0; a < o.length; a++)
629
- (await this.getStories({
630
- per_page: c,
631
- language: t.language,
632
- version: t.version,
633
- by_uuids: o[a].join(",")
634
- })).data.stories.forEach(
635
- (l) => {
636
- r.push(l);
637
- }
638
- );
639
- } else
640
- r = e.links;
641
- r.forEach((i) => {
642
- this.links[s][i.uuid] = {
643
- ...i,
644
- _stopResolving: true
645
- };
646
- });
647
- }
648
- async resolveRelations(e, t, s) {
649
- let r = [];
650
- if (e.rel_uuids) {
651
- const i = e.rel_uuids.length, o = [], c = 50;
652
- for (let a = 0; a < i; a += c) {
653
- const l = Math.min(i, a + c);
654
- o.push(e.rel_uuids.slice(a, l));
655
- }
656
- for (let a = 0; a < o.length; a++)
657
- (await this.getStories({
658
- per_page: c,
659
- language: t.language,
660
- version: t.version,
661
- by_uuids: o[a].join(",")
662
- })).data.stories.forEach((l) => {
663
- r.push(l);
664
- });
665
- } else
666
- r = e.rels;
667
- r && r.length > 0 && r.forEach((i) => {
668
- this.relations[s][i.uuid] = {
669
- ...i,
670
- _stopResolving: true
671
- };
672
- });
673
- }
674
- async resolveStories(e, t, s) {
675
- var r, i;
676
- let o = [];
677
- if (this.links[s] = {}, this.relations[s] = {}, typeof t.resolve_relations < "u" && t.resolve_relations.length > 0 && (typeof t.resolve_relations == "string" && (o = t.resolve_relations.split(",")), await this.resolveRelations(e, t, s)), t.resolve_links && ["1", "story", "url"].indexOf(t.resolve_links) > -1 && ((r = e.links) != null && r.length || (i = e.link_uuids) != null && i.length) && await this.resolveLinks(e, t, s), this.resolveNestedRelations)
678
- for (const c in this.relations[s])
679
- this.iterateTree(
680
- this.relations[s][c],
681
- o,
682
- s
683
- );
684
- e.story ? this.iterateTree(e.story, o, s) : e.stories.forEach((c) => {
685
- this.iterateTree(c, o, s);
686
- }), delete this.links[s], delete this.relations[s];
687
- }
688
- async cacheResponse(e, t, s) {
689
- const r = this.helpers.stringify({ url: e, params: t }), i = this.cacheProvider();
690
- if (this.cache.clear === "auto" && t.version === "draft" && await this.flushCache(), t.version === "published" && e != "/cdn/spaces/me") {
691
- const o = await i.get(r);
692
- if (o)
693
- return Promise.resolve(o);
694
- }
695
- return new Promise((o, c) => {
696
- try {
697
- (async () => {
698
- var a;
699
- try {
700
- const l = await this.throttle("get", e, t);
701
- let u = { data: l.data, headers: l.headers };
702
- if ((a = l.headers) != null && a["per-page"] && (u = Object.assign({}, u, {
703
- perPage: l.headers["per-page"] ? parseInt(l.headers["per-page"]) : 0,
704
- total: l.headers["per-page"] ? parseInt(l.headers.total) : 0
705
- })), l.status != 200)
706
- return c(l);
707
- if (u.data.story || u.data.stories) {
708
- const p = this.resolveCounter = ++this.resolveCounter % 1e3;
709
- await this.resolveStories(u.data, t, `${p}`);
710
- }
711
- return t.version === "published" && e != "/cdn/spaces/me" && await i.set(r, u), u.data.cv && t.token && (t.version == "draft" && f[t.token] != u.data.cv && await this.flushCache(), f[t.token] = u.data.cv), o(u);
712
- } catch (l) {
713
- return c(l);
714
- }
715
- })();
716
- } catch {
717
- }
718
- });
719
- }
720
- throttledRequest(e, t, s) {
721
- return this.client[e](t, s);
722
- }
723
- cacheVersions() {
724
- return f;
725
- }
726
- cacheVersion() {
727
- return f[this.accessToken];
728
- }
729
- setCacheVersion(e) {
730
- this.accessToken && (f[this.accessToken] = e);
731
- }
732
- cacheProvider() {
733
- switch (this.cache.type) {
734
- case "memory":
735
- return {
736
- get(e) {
737
- return Promise.resolve(m[e]);
738
- },
739
- getAll() {
740
- return Promise.resolve(m);
741
- },
742
- set(e, t) {
743
- return m[e] = t, Promise.resolve(void 0);
744
- },
745
- flush() {
746
- return m = {}, Promise.resolve(void 0);
747
- }
748
- };
749
- case "custom":
750
- if (this.cache.custom)
751
- return this.cache.custom;
752
- default:
753
- return {
754
- get() {
755
- return Promise.resolve(void 0);
756
- },
757
- getAll() {
758
- return Promise.resolve(void 0);
759
- },
760
- set() {
761
- return Promise.resolve(void 0);
762
- },
763
- flush() {
764
- return Promise.resolve(void 0);
765
- }
766
- };
767
- }
768
- }
769
- async flushCache() {
770
- return await this.cacheProvider().flush(), this;
771
- }
772
- }
773
- const ce = (n = {}) => {
774
- const { apiOptions: e } = n;
775
- if (!e.accessToken) {
776
- console.error(
777
- "You need to provide an access token to interact with Storyblok API. Read https://www.storyblok.com/docs/api/content-delivery#topics/authentication"
778
- );
779
- return;
780
- }
781
- return { storyblokApi: new ae(e) };
782
- }, he = (n) => {
783
- if (typeof n != "object" || typeof n._editable > "u")
784
- return {};
785
- const e = JSON.parse(
786
- n._editable.replace(/^<!--#storyblok#/, "").replace(/-->$/, "")
787
- );
788
- return {
789
- "data-blok-c": JSON.stringify(e),
790
- "data-blok-uid": e.id + "-" + e.uid
791
- };
792
- };
793
- let w;
794
- const x = "https://app.storyblok.com/f/storyblok-v2-latest.js", ue = (n, e, t = {}) => {
795
- var c;
796
- const r = !(typeof window > "u") && typeof window.storyblokRegisterEvent < "u", o = +new URL((c = window.location) == null ? void 0 : c.href).searchParams.get(
797
- "_storyblok"
798
- ) === n;
799
- if (!(!r || !o)) {
800
- if (!n) {
801
- console.warn("Story ID is not defined. Please provide a valid ID.");
802
- return;
803
- }
804
- window.storyblokRegisterEvent(() => {
805
- new window.StoryblokBridge(t).on(["input", "published", "change"], (l) => {
806
- l.action === "input" && l.story.id === n ? e(l.story) : (l.action === "change" || l.action === "published") && l.storyId === n && window.location.reload();
807
- });
808
- });
809
- }
810
- }, pe = (n = {}) => {
811
- var u, p;
812
- const {
813
- bridge: e,
814
- accessToken: t,
815
- use: s = [],
816
- apiOptions: r = {},
817
- richText: i = {}
818
- } = n;
819
- r.accessToken = r.accessToken || t;
820
- const o = { bridge: e, apiOptions: r };
821
- let c = {};
822
- s.forEach((g) => {
823
- c = { ...c, ...g(o) };
824
- });
825
- const l = !(typeof window > "u") && ((p = (u = window.location) == null ? void 0 : u.search) == null ? void 0 : p.includes("_storyblok_tk"));
826
- return e !== false && l && j(x), w = new y(i.schema), i.resolver && E(w, i.resolver), c;
827
- }, E = (n, e) => {
828
- n.addNode("blok", (t) => {
829
- let s = "";
830
- return t.attrs.body.forEach((r) => {
831
- s += e(r.component, r);
832
- }), {
833
- html: s
834
- };
835
- });
836
- }, le = (n) => {
837
- var e, t;
838
- return !n || ((e = n == null ? void 0 : n.content) == null ? void 0 : e[0].type) !== "blok" && !((t = n == null ? void 0 : n.content) != null && t[0].content);
839
- }, de = (n, e, t) => {
840
- let s = t || w;
841
- if (!s) {
842
- console.error(
843
- "Please initialize the Storyblok SDK before calling the renderRichText function"
844
- );
845
- return;
846
- }
847
- return le(n) ? "" : (e && (s = new y(e.schema), e.resolver && E(s, e.resolver)), s.render(n));
848
- }, ge = () => j(x);
849
- export {
850
- ce as c,
851
- de as d,
852
- ge as g,
853
- he as h,
854
- ie as i,
855
- pe as p,
856
- ue as u,
857
- y
858
- };