@storyblok/js 2.2.2 → 2.2.4
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -2
- package/dist/storyblok-js.js +2 -2
- package/dist/storyblok-js.mjs +212 -206
- package/dist/types/types.d.ts +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
@@ -32,8 +32,6 @@
|
|
32
32
|
|
33
33
|
## 🚀 Usage
|
34
34
|
|
35
|
-
<!-- -->
|
36
|
-
|
37
35
|
> If you are first-time user of the Storyblok, read the [Getting Started](https://www.storyblok.com/docs/guide/getting-started?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js) guide to get a project ready in less than 5 minutes.
|
38
36
|
|
39
37
|
### Installation
|
@@ -145,6 +143,7 @@ You can pass [Bridge options](https://www.storyblok.com/docs/Guides/storyblok-la
|
|
145
143
|
```js
|
146
144
|
useStoryblokBridge(story.id, (story) => (state.story = story), {
|
147
145
|
resolveRelations: ["Article.author"],
|
146
|
+
resolveLinks: "url"
|
148
147
|
});
|
149
148
|
```
|
150
149
|
|
package/dist/storyblok-js.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
(function(d,
|
1
|
+
(function(d,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(d=typeof globalThis<"u"?globalThis:d||self,g(d.storyblok={}))})(this,function(d){"use strict";let g=!1;const O=[],v=n=>new Promise((e,t)=>{if(typeof window>"u"||(window.storyblokRegisterEvent=r=>{if(window.location===window.parent.location){console.warn("You are not in Draft Mode or in the Visual Editor.");return}g?r():O.push(r)},document.getElementById("storyblok-javascript-bridge")))return;const s=document.createElement("script");s.async=!0,s.src=n,s.id="storyblok-javascript-bridge",s.onerror=r=>t(r),s.onload=r=>{O.forEach(o=>o()),g=!0,e(r)},document.getElementsByTagName("head")[0].appendChild(s)});var G=Object.defineProperty,C=(n,e,t)=>e in n?G(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,h=(n,e,t)=>(C(n,typeof e!="symbol"?e+"":e,t),t);function N(n){return!(n!==n||n===1/0||n===-1/0)}function x(n,e,t){if(!N(e))throw new TypeError("Expected `limit` to be a finite number");if(!N(t))throw new TypeError("Expected `interval` to be a finite number");const s=[];let r=[],o=0;const i=function(){o++;const a=setTimeout(function(){o--,s.length>0&&i(),r=r.filter(function(u){return u!==a})},t);r.indexOf(a)<0&&r.push(a);const l=s.shift();l.resolve(n.apply(l.self,l.args))},c=function(...a){const l=this;return new Promise(function(u,p){s.push({resolve:u,reject:p,args:a,self:l}),o<e&&i()})};return c.abort=function(){r.forEach(clearTimeout),r=[],s.forEach(function(a){a.reject(function(){Error.call(this,"Throttled function aborted"),this.name="AbortError"})}),s.length=0},c}const B=function(n,e){const t={};for(const s in n){const r=n[s];e.indexOf(s)>-1&&r!==null&&(t[s]=r)}return t},$=n=>n==="email",D=()=>({singleTag:"hr"}),M=()=>({tag:"blockquote"}),L=()=>({tag:"ul"}),V=n=>({tag:["pre",{tag:"code",attrs:n.attrs}]}),F=()=>({singleTag:"br"}),W=n=>({tag:`h${n.attrs.level}`}),K=n=>({singleTag:[{tag:"img",attrs:B(n.attrs,["src","alt","title"])}]}),X=()=>({tag:"li"}),z=()=>({tag:"ol"}),q=()=>({tag:"p"}),J=n=>({tag:[{tag:"span",attrs:{["data-type"]:"emoji",["data-name"]:n.attrs.name,emoji:n.attrs.emoji}}]}),Y=()=>({tag:"b"}),Z=()=>({tag:"strike"}),Q=()=>({tag:"u"}),ee=()=>({tag:"strong"}),te=()=>({tag:"code"}),se=()=>({tag:"i"}),re=n=>{const e={...n.attrs},{linktype:t="url"}=n.attrs;if($(t)&&(e.href=`mailto:${e.href}`),e.anchor&&(e.href=`${e.href}#${e.anchor}`,delete e.anchor),e.custom){for(const s in e.custom)e[s]=e.custom[s];delete e.custom}return{tag:[{tag:"a",attrs:e}]}},oe=n=>({tag:[{tag:"span",attrs:n.attrs}]}),ne=()=>({tag:"sub"}),ie=()=>({tag:"sup"}),ae=n=>({tag:[{tag:"span",attrs:n.attrs}]}),le=n=>{var e;return(e=n.attrs)!=null&&e.color?{tag:[{tag:"span",attrs:{style:`background-color:${n.attrs.color};`}}]}:{tag:""}},ce=n=>{var e;return(e=n.attrs)!=null&&e.color?{tag:[{tag:"span",attrs:{style:`color:${n.attrs.color}`}}]}:{tag:""}},A={nodes:{horizontal_rule:D,blockquote:M,bullet_list:L,code_block:V,hard_break:F,heading:W,image:K,list_item:X,ordered_list:z,paragraph:q,emoji:J},marks:{bold:Y,strike:Z,underline:Q,strong:ee,code:te,italic:se,link:re,styled:oe,subscript:ne,superscript:ie,anchor:ae,highlight:le,textStyle:ce}},he=function(n){const e={"&":"&","<":"<",">":">",'"':""","'":"'"},t=/[&<>"']/g,s=RegExp(t.source);return n&&s.test(n)?n.replace(t,r=>e[r]):n};class b{constructor(e){h(this,"marks"),h(this,"nodes"),e||(e=A),this.marks=e.marks||[],this.nodes=e.nodes||[]}addNode(e,t){this.nodes[e]=t}addMark(e,t){this.marks[e]=t}render(e,t={optimizeImages:!1}){if(e&&e.content&&Array.isArray(e.content)){let s="";return e.content.forEach(r=>{s+=this.renderNode(r)}),t.optimizeImages?this.optimizeImages(s,t.optimizeImages):s}return console.warn(`The render method must receive an Object with a "content" field.
|
2
2
|
The "content" field must be an array of nodes as the type ISbRichtext.
|
3
3
|
ISbRichtext:
|
4
4
|
content?: ISbRichtext[]
|
@@ -21,4 +21,4 @@
|
|
21
21
|
}
|
22
22
|
],
|
23
23
|
type: 'doc'
|
24
|
-
}`),""}optimizeImages(e,t){let s=0,r=0,i="",o="";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()}`));const c=s>0||r>0||o.length>0?`${s}x${r}${o}`:"";return e=e.replace(/a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g,`a.storyblok.com/f/$1/$2.$3/m/${c}`),typeof t!="boolean"&&(t.sizes||t.srcset)&&(e=e.replace(/<img.*?src=["|'](.*?)["|']/g,a=>{var l,u;const p=a.match(/a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g);if(p&&p.length>0){const f={srcset:(l=t.srcset)==null?void 0:l.map(g=>{if(typeof g=="number")return`//${p}/m/${g}x0${o} ${g}w`;if(typeof g=="object"&&g.length===2){let $=0,C=0;return typeof g[0]=="number"&&($=g[0]),typeof g[1]=="number"&&(C=g[1]),`//${p}/m/${$}x${C}${o} ${$}w`}}).join(", "),sizes:(u=t.sizes)==null?void 0:u.map(g=>g).join(", ")};let T="";return f.srcset&&(T+=`srcset="${f.srcset}" `),f.sizes&&(T+=`sizes="${f.sizes}" `),a.replace(/<img/g,`<img ${T.trim()}`)}return a})),e}renderNode(e){const t=[];e.marks&&e.marks.forEach(r=>{const i=this.getMatchingMark(r);i&&i.tag!==""&&t.push(this.renderOpeningTag(i.tag))});const s=this.getMatchingNode(e);return s&&s.tag&&t.push(this.renderOpeningTag(s.tag)),e.content?e.content.forEach(r=>{t.push(this.renderNode(r))}):e.text?t.push(le(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=>{const i=this.getMatchingMark(r);i&&i.tag!==""&&t.push(this.renderClosingTag(i.tag))}),t.join("")}renderTag(e,t){return e.constructor===String?`<${e}${t}>`:e.map(s=>{if(s.constructor===String)return`<${s}${t}>`;{let r=`<${s.tag}`;if(s.attrs)for(const i in s.attrs){const o=s.attrs[i];o!==null&&(r+=` ${i}="${o}"`)}return`${r}${t}>`}}).join("")}renderOpeningTag(e){return this.renderTag(e,"")}renderClosingTag(e){return e.constructor===String?`</${e}>`:e.slice(0).reverse().map(t=>t.constructor===String?`</${t}>`:`</${t.tag}>`).join("")}getMatchingNode(e){const t=this.nodes[e.type];if(typeof t=="function")return t(e)}getMatchingMark(e){const t=this.marks[e.type];if(typeof t=="function")return t(e)}renderEmoji(e){if(e.attrs.emoji)return e.attrs.emoji;const t=[{tag:"img",attrs:{src:e.attrs.fallbackImage,draggable:"false",loading:"lazy",align:"absmiddle"}}];return this.renderTag(t," /")}}class v{constructor(){h(this,"isCDNUrl",(e="")=>e.indexOf("/cdn/")>-1),h(this,"getOptionsPage",(e,t=25,s=1)=>({...e,per_page:t,page:s})),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)=>{const s=Math.abs(t-e)||0,r=e<t?1:-1;return this.arrayFrom(s,(i,o)=>o*r+e)}),h(this,"asyncMap",async(e,t)=>Promise.all(e.map(t))),h(this,"flatMap",(e=[],t)=>e.map(t).reduce((s,r)=>[...s,...r],[]))}stringify(e,t,s){const r=[];for(const i in e){if(!Object.prototype.hasOwnProperty.call(e,i))continue;const o=e[i],c=s?"":encodeURIComponent(i);let a;typeof o=="object"?a=this.stringify(o,t?t+encodeURIComponent("["+c+"]"):c,Array.isArray(o)):a=(t?t+encodeURIComponent("["+c+"]"):c)+"="+encodeURIComponent(o),r.push(a)}return r.join("&")}getRegionURL(e){const t="api.storyblok.com",s="api-us.storyblok.com",r="app.storyblokchina.cn";switch(e){case"us":return s;case"cn":return r;default:return t}}}class ce{constructor(e){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=!1,this.url="",this.parameters={}}get(e,t){return this.url=e,this.parameters=t,this._methodHandler("get")}post(e,t){return this.url=e,this.parameters=t,this._methodHandler("post")}put(e,t){return this.url=e,this.parameters=t,this._methodHandler("put")}delete(e,t){return this.url=e,this.parameters=t,this._methodHandler("delete")}async _responseHandler(e){const t=[],s={data:{},headers:{},status:0,statusText:""};e.status!==204&&await e.json().then(r=>{s.data=r});for(const r of e.headers.entries())t[r[0]]=r[1];return s.headers={...t},s.status=e.status,s.statusText=e.statusText,s}async _methodHandler(e){let t=`${this.baseURL}${this.url}`,s=null;if(e==="get"){const a=new v;t=`${this.baseURL}${this.url}?${a.stringify(this.parameters)}`}else s=JSON.stringify(this.parameters);const r=new URL(t),i=new AbortController,{signal:o}=i;let c;this.timeout&&(c=setTimeout(()=>i.abort(),this.timeout));try{const a=await this.fetch(`${r}`,{method:e,headers:this.headers,body:s,signal:o});this.timeout&&clearTimeout(c);const l=await this._responseHandler(a);return this.responseInterceptor&&!this.ejectInterceptor?this._statusHandler(this.responseInterceptor(l)):this._statusHandler(l)}catch(a){return{message:a}}}eject(){this.ejectInterceptor=!0}_statusHandler(e){const t=/20[0-6]/g;return new Promise((s,r)=>{if(t.test(`${e.status}`))return s(e);const i={message:new Error(e.statusText),status:e.status,response:Array.isArray(e.data)?e.data[0]:e.data.error||e.data.slug};r(i)})}}let k={};const y={};class he{constructor(e,t){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");let s=e.endpoint||t;if(!s){const o=new v().getRegionURL,c=e.https===!1?"http":"https";e.oauthToken?s=`${c}://${o(e.region)}/v1`:s=`${c}://${o(e.region)}/v2`}const r=new Headers;r.set("Content-Type","application/json"),r.set("Accept","application/json"),r.forEach((o,c)=>{e.headers&&e.headers[c]&&r.set(c,e.headers[c])});let i=5;e.oauthToken&&(r.set("Authorization",e.oauthToken),i=3),e.rateLimit&&(i=e.rateLimit),e.richTextSchema?this.richTextResolver=new b(e.richTextSchema):this.richTextResolver=new b,e.componentResolver&&this.setComponentResolver(e.componentResolver),this.maxRetries=e.maxRetries||5,this.throttle=N(this.throttledRequest,i,1e3),this.accessToken=e.accessToken||"",this.relations={},this.links={},this.cache=e.cache||{clear:"manual"},this.helpers=new v,this.resolveCounter=0,this.resolveNestedRelations=e.resolveNestedRelations||!0,this.client=new ce({baseURL:s,timeout:e.timeout||0,headers:r,responseInterceptor:e.responseInterceptor,fetch:e.fetch})}setComponentResolver(e){this.richTextResolver.addNode("blok",t=>{let s="";return t.attrs.body&&t.attrs.body.forEach(r=>{s+=e(r.component,r)}),{html:s}})}parseParams(e){return e.version||(e.version="published"),e.token||(e.token=this.getToken()),e.cv||(e.cv=y[e.token]),Array.isArray(e.resolve_relations)&&(e.resolve_relations=e.resolve_relations.join(",")),e}factoryParamOptions(e,t){return this.helpers.isCDNUrl(e)?this.parseParams(t):t}makeRequest(e,t,s,r){const i=this.factoryParamOptions(e,this.helpers.getOptionsPage(t,s,r));return this.cacheResponse(e,i)}get(e,t){t||(t={});const s=`/${e}`,r=this.factoryParamOptions(s,t);return this.cacheResponse(s,r)}async getAll(e,t,s){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(this.helpers.range(a,u),f=>this.makeRequest(i,t,r,f+1));return this.helpers.flatMap([l,...p],f=>Object.values(f.data[c]))}post(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("post",s,t))}put(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("put",s,t))}delete(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("delete",s,t))}getStories(e){return this.get("cdn/stories",e)}getStory(e,t){return this.get(`cdn/stories/${e}`,t)}getToken(){return this.accessToken}ejectInterceptor(){this.client.eject()}_cleanCopy(e){return JSON.parse(JSON.stringify(e))}_insertLinks(e,t,s){const r=e[t];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]))}_insertRelations(e,t,s,r){if(s.indexOf(`${e.component}.${t}`)>-1){if(typeof e[t]=="string")this.relations[r][e[t]]&&(e[t]=this._cleanCopy(this.relations[r][e[t]]));else if(e[t]&&e[t].constructor===Array){const i=[];e[t].forEach(o=>{this.relations[r][o]&&i.push(this._cleanCopy(this.relations[r][o]))}),e[t]=i}}}iterateTree(e,t,s){const r=i=>{if(i!=null){if(i.constructor===Array)for(let o=0;o<i.length;o++)r(i[o]);else if(i.constructor===Object){if(i._stopResolving)return;for(const o in i)(i.component&&i._uid||i.type==="link")&&(this._insertRelations(i,o,t,s),this._insertLinks(i,o,s)),r(i[o])}}};r(e.content)}async resolveLinks(e,t,s){let r=[];if(e.link_uuids){const i=e.link_uuids.length,o=[],c=50;for(let a=0;a<i;a+=c){const l=Math.min(i,a+c);o.push(e.link_uuids.slice(a,l))}for(let a=0;a<o.length;a++)(await this.getStories({per_page:c,language:t.language,version:t.version,by_uuids:o[a].join(",")})).data.stories.forEach(l=>{r.push(l)})}else r=e.links;r.forEach(i=>{this.links[s][i.uuid]={...i,_stopResolving:!0}})}async resolveRelations(e,t,s){let r=[];if(e.rel_uuids){const i=e.rel_uuids.length,o=[],c=50;for(let a=0;a<i;a+=c){const l=Math.min(i,a+c);o.push(e.rel_uuids.slice(a,l))}for(let a=0;a<o.length;a++)(await this.getStories({per_page:c,language:t.language,version:t.version,by_uuids:o[a].join(",")})).data.stories.forEach(l=>{r.push(l)})}else r=e.rels;r&&r.length>0&&r.forEach(i=>{this.relations[s][i.uuid]={...i,_stopResolving:!0}})}async resolveStories(e,t,s){var r,i;let o=[];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)for(const c in this.relations[s])this.iterateTree(this.relations[s][c],o,s);e.story?this.iterateTree(e.story,o,s):e.stories.forEach(c=>{this.iterateTree(c,o,s)}),delete this.links[s],delete this.relations[s]}async cacheResponse(e,t,s){const r=this.helpers.stringify({url:e,params:t}),i=this.cacheProvider();if(this.cache.clear==="auto"&&t.version==="draft"&&await this.flushCache(),t.version==="published"&&e!="/cdn/spaces/me"){const o=await i.get(r);if(o)return Promise.resolve(o)}return new Promise((o,c)=>{try{(async()=>{var a;try{const l=await this.throttle("get",e,t);let u={data:l.data,headers:l.headers};if((a=l.headers)!=null&&a["per-page"]&&(u=Object.assign({},u,{perPage:l.headers["per-page"]?parseInt(l.headers["per-page"]):0,total:l.headers["per-page"]?parseInt(l.headers.total):0})),l.status!=200)return c(l);if(u.data.story||u.data.stories){const p=this.resolveCounter=++this.resolveCounter%1e3;await this.resolveStories(u.data,t,`${p}`)}return t.version==="published"&&e!="/cdn/spaces/me"&&await i.set(r,u),u.data.cv&&t.token&&(t.version=="draft"&&y[t.token]!=u.data.cv&&await this.flushCache(),y[t.token]=u.data.cv),o(u)}catch(l){return c(l)}})()}catch{}})}throttledRequest(e,t,s){return this.client[e](t,s)}cacheVersions(){return y}cacheVersion(){return y[this.accessToken]}setCacheVersion(e){this.accessToken&&(y[this.accessToken]=e)}cacheProvider(){switch(this.cache.type){case"memory":return{get(e){return Promise.resolve(k[e])},getAll(){return Promise.resolve(k)},set(e,t){return k[e]=t,Promise.resolve(void 0)},flush(){return k={},Promise.resolve(void 0)}};case"custom":if(this.cache.custom)return this.cache.custom;default:return{get(){return Promise.resolve(void 0)},getAll(){return Promise.resolve(void 0)},set(){return Promise.resolve(void 0)},flush(){return Promise.resolve(void 0)}}}}async flushCache(){return await this.cacheProvider().flush(),this}}const ue=(n={})=>{const{apiOptions:e}=n;if(!e.accessToken){console.error("You need to provide an access token to interact with Storyblok API. Read https://www.storyblok.com/docs/api/content-delivery#topics/authentication");return}return{storyblokApi:new he(e)}},de=n=>{if(typeof n!="object"||typeof n._editable>"u")return{};const e=JSON.parse(n._editable.replace(/^<!--#storyblok#/,"").replace(/-->$/,""));return{"data-blok-c":JSON.stringify(e),"data-blok-uid":e.id+"-"+e.uid}};let w;const E="https://app.storyblok.com/f/storyblok-v2-latest.js",x=(n,e,t={})=>{var c;const r=!(typeof window>"u")&&typeof window.storyblokRegisterEvent<"u",o=+new URL((c=window.location)==null?void 0:c.href).searchParams.get("_storyblok")===n;if(!(!r||!o)){if(!n){console.warn("Story ID is not defined. Please provide a valid ID.");return}window.storyblokRegisterEvent(()=>{new window.StoryblokBridge(t).on(["input","published","change"],l=>{l.action==="input"&&l.story.id===n?e(l.story):(l.action==="change"||l.action==="published")&&l.storyId===n&&window.location.reload()})})}},pe=(n={})=>{var u,p;const{bridge:e,accessToken:t,use:s=[],apiOptions:r={},richText:i={}}=n;r.accessToken=r.accessToken||t;const o={bridge:e,apiOptions:r};let c={};s.forEach(f=>{c={...c,...f(o)}});const l=!(typeof window>"u")&&((p=(u=window.location)==null?void 0:u.search)==null?void 0:p.includes("_storyblok_tk"));return e!==!1&&l&&_(E),w=new b(i.schema),i.resolver&&I(w,i.resolver),c},I=(n,e)=>{n.addNode("blok",t=>{let s="";return t.attrs.body.forEach(r=>{s+=e(r.component,r)}),{html:s}})},P=n=>{var e,t;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)},ge=(n,e,t)=>{let s=t||w;if(!s){console.error("Please initialize the Storyblok SDK before calling the renderRichText function");return}return P(n)?"":(e&&(s=new b(e.schema),e.resolver&&I(s,e.resolver)),s.render(n))},fe=()=>_(E);d.RichTextResolver=b,d.RichTextSchema=S,d.apiPlugin=ue,d.isRichTextEmpty=P,d.loadStoryblokBridge=fe,d.registerStoryblokBridge=x,d.renderRichText=ge,d.storyblokEditable=de,d.storyblokInit=pe,d.useStoryblokBridge=x,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
|
24
|
+
}`),""}optimizeImages(e,t){let s=0,r=0,o="",i="";typeof t!="boolean"&&(typeof t.width=="number"&&t.width>0&&(o+=`width="${t.width}" `,s=t.width),typeof t.height=="number"&&t.height>0&&(o+=`height="${t.height}" `,r=t.height),(t.loading==="lazy"||t.loading==="eager")&&(o+=`loading="${t.loading}" `),typeof t.class=="string"&&t.class.length>0&&(o+=`class="${t.class}" `),t.filters&&(typeof t.filters.blur=="number"&&t.filters.blur>=0&&t.filters.blur<=100&&(i+=`:blur(${t.filters.blur})`),typeof t.filters.brightness=="number"&&t.filters.brightness>=-100&&t.filters.brightness<=100&&(i+=`:brightness(${t.filters.brightness})`),t.filters.fill&&(t.filters.fill.match(/[0-9A-Fa-f]{6}/g)||t.filters.fill==="transparent")&&(i+=`:fill(${t.filters.fill})`),t.filters.format&&["webp","png","jpeg"].includes(t.filters.format)&&(i+=`:format(${t.filters.format})`),typeof t.filters.grayscale=="boolean"&&t.filters.grayscale&&(i+=":grayscale()"),typeof t.filters.quality=="number"&&t.filters.quality>=0&&t.filters.quality<=100&&(i+=`:quality(${t.filters.quality})`),t.filters.rotate&&[90,180,270].includes(t.filters.rotate)&&(i+=`:rotate(${t.filters.rotate})`),i.length>0&&(i="/filters"+i))),o.length>0&&(e=e.replace(/<img/g,`<img ${o.trim()}`));const c=s>0||r>0||i.length>0?`${s}x${r}${i}`:"";return e=e.replace(/a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g,`a.storyblok.com/f/$1/$2.$3/m/${c}`),typeof t!="boolean"&&(t.sizes||t.srcset)&&(e=e.replace(/<img.*?src=["|'](.*?)["|']/g,a=>{var l,u;const p=a.match(/a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g);if(p&&p.length>0){const _={srcset:(l=t.srcset)==null?void 0:l.map(m=>{if(typeof m=="number")return`//${p}/m/${m}x0${i} ${m}w`;if(typeof m=="object"&&m.length===2){let I=0,P=0;return typeof m[0]=="number"&&(I=m[0]),typeof m[1]=="number"&&(P=m[1]),`//${p}/m/${I}x${P}${i} ${I}w`}}).join(", "),sizes:(u=t.sizes)==null?void 0:u.map(m=>m).join(", ")};let E="";return _.srcset&&(E+=`srcset="${_.srcset}" `),_.sizes&&(E+=`sizes="${_.sizes}" `),a.replace(/<img/g,`<img ${E.trim()}`)}return a})),e}renderNode(e){const t=[];e.marks&&e.marks.forEach(r=>{const o=this.getMatchingMark(r);o&&o.tag!==""&&t.push(this.renderOpeningTag(o.tag))});const s=this.getMatchingNode(e);return s&&s.tag&&t.push(this.renderOpeningTag(s.tag)),e.content?e.content.forEach(r=>{t.push(this.renderNode(r))}):e.text?t.push(he(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=>{const o=this.getMatchingMark(r);o&&o.tag!==""&&t.push(this.renderClosingTag(o.tag))}),t.join("")}renderTag(e,t){return e.constructor===String?`<${e}${t}>`:e.map(s=>{if(s.constructor===String)return`<${s}${t}>`;{let r=`<${s.tag}`;if(s.attrs)for(const o in s.attrs){const i=s.attrs[o];i!==null&&(r+=` ${o}="${i}"`)}return`${r}${t}>`}}).join("")}renderOpeningTag(e){return this.renderTag(e,"")}renderClosingTag(e){return e.constructor===String?`</${e}>`:e.slice(0).reverse().map(t=>t.constructor===String?`</${t}>`:`</${t.tag}>`).join("")}getMatchingNode(e){const t=this.nodes[e.type];if(typeof t=="function")return t(e)}getMatchingMark(e){const t=this.marks[e.type];if(typeof t=="function")return t(e)}renderEmoji(e){if(e.attrs.emoji)return e.attrs.emoji;const t=[{tag:"img",attrs:{src:e.attrs.fallbackImage,draggable:"false",loading:"lazy",align:"absmiddle"}}];return this.renderTag(t," /")}}class y{constructor(){h(this,"isCDNUrl",(e="")=>e.indexOf("/cdn/")>-1),h(this,"getOptionsPage",(e,t=25,s=1)=>({...e,per_page:t,page:s})),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)=>{const s=Math.abs(t-e)||0,r=e<t?1:-1;return this.arrayFrom(s,(o,i)=>i*r+e)}),h(this,"asyncMap",async(e,t)=>Promise.all(e.map(t))),h(this,"flatMap",(e=[],t)=>e.map(t).reduce((s,r)=>[...s,...r],[]))}stringify(e,t,s){const r=[];for(const o in e){if(!Object.prototype.hasOwnProperty.call(e,o))continue;const i=e[o],c=s?"":encodeURIComponent(o);let a;typeof i=="object"?a=this.stringify(i,t?t+encodeURIComponent("["+c+"]"):c,Array.isArray(i)):a=(t?t+encodeURIComponent("["+c+"]"):c)+"="+encodeURIComponent(i),r.push(a)}return r.join("&")}getRegionURL(e){const t="api.storyblok.com",s="api-us.storyblok.com",r="app.storyblokchina.cn";switch(e){case"us":return s;case"cn":return r;default:return t}}}class ue{constructor(e){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=!1,this.url="",this.parameters={}}get(e,t){return this.url=e,this.parameters=t,this._methodHandler("get")}post(e,t){return this.url=e,this.parameters=t,this._methodHandler("post")}put(e,t){return this.url=e,this.parameters=t,this._methodHandler("put")}delete(e,t){return this.url=e,this.parameters=t,this._methodHandler("delete")}async _responseHandler(e){const t=[],s={data:{},headers:{},status:0,statusText:""};e.status!==204&&await e.json().then(r=>{s.data=r});for(const r of e.headers.entries())t[r[0]]=r[1];return s.headers={...t},s.status=e.status,s.statusText=e.statusText,s}async _methodHandler(e){let t=`${this.baseURL}${this.url}`,s=null;if(e==="get"){const a=new y;t=`${this.baseURL}${this.url}?${a.stringify(this.parameters)}`}else s=JSON.stringify(this.parameters);const r=new URL(t),o=new AbortController,{signal:i}=o;let c;this.timeout&&(c=setTimeout(()=>o.abort(),this.timeout));try{const a=await this.fetch(`${r}`,{method:e,headers:this.headers,body:s,signal:i});this.timeout&&clearTimeout(c);const l=await this._responseHandler(a);return this.responseInterceptor&&!this.ejectInterceptor?this._statusHandler(this.responseInterceptor(l)):this._statusHandler(l)}catch(a){return{message:a}}}eject(){this.ejectInterceptor=!0}_statusHandler(e){const t=/20[0-6]/g;return new Promise((s,r)=>{if(t.test(`${e.status}`))return s(e);const o={message:new Error(e.statusText),status:e.status,response:Array.isArray(e.data)?e.data[0]:e.data.error||e.data.slug};r(o)})}}const w="SB-Agent",k={defaultAgentName:"SB-JS-CLIENT",defaultAgentVersion:"SB-Agent-Version",packageVersion:"2.2.4"};let T={};const f={};class de{constructor(e,t){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");let s=e.endpoint||t;if(!s){const i=new y().getRegionURL,c=e.https===!1?"http":"https";e.oauthToken?s=`${c}://${i(e.region)}/v1`:s=`${c}://${i(e.region)}/v2`}const r=new Headers;if(r.set("Content-Type","application/json"),r.set("Accept","application/json"),e.headers)for(const i in e.headers)r.set(i,e.headers[i]);r.has(w)||(r.set(w,k.defaultAgentName),r.set(k.defaultAgentVersion,k.packageVersion));let o=5;e.oauthToken&&(r.set("Authorization",e.oauthToken),o=3),e.rateLimit&&(o=e.rateLimit),e.richTextSchema?this.richTextResolver=new b(e.richTextSchema):this.richTextResolver=new b,e.componentResolver&&this.setComponentResolver(e.componentResolver),this.maxRetries=e.maxRetries||5,this.throttle=x(this.throttledRequest,o,1e3),this.accessToken=e.accessToken||"",this.relations={},this.links={},this.cache=e.cache||{clear:"manual"},this.helpers=new y,this.resolveCounter=0,this.resolveNestedRelations=e.resolveNestedRelations||!0,this.client=new ue({baseURL:s,timeout:e.timeout||0,headers:r,responseInterceptor:e.responseInterceptor,fetch:e.fetch})}setComponentResolver(e){this.richTextResolver.addNode("blok",t=>{let s="";return t.attrs.body&&t.attrs.body.forEach(r=>{s+=e(r.component,r)}),{html:s}})}parseParams(e){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}factoryParamOptions(e,t){return this.helpers.isCDNUrl(e)?this.parseParams(t):t}makeRequest(e,t,s,r){const o=this.factoryParamOptions(e,this.helpers.getOptionsPage(t,s,r));return this.cacheResponse(e,o)}get(e,t){t||(t={});const s=`/${e}`,r=this.factoryParamOptions(s,t);return this.cacheResponse(s,r)}async getAll(e,t,s){const r=(t==null?void 0:t.per_page)||25,o=`/${e}`,i=o.split("/"),c=s||i[i.length-1],a=1,l=await this.makeRequest(o,t,r,a),u=l.total?Math.ceil(l.total/r):1,p=await this.helpers.asyncMap(this.helpers.range(a,u),_=>this.makeRequest(o,t,r,_+1));return this.helpers.flatMap([l,...p],_=>Object.values(_.data[c]))}post(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("post",s,t))}put(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("put",s,t))}delete(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("delete",s,t))}getStories(e){return this.get("cdn/stories",e)}getStory(e,t){return this.get(`cdn/stories/${e}`,t)}getToken(){return this.accessToken}ejectInterceptor(){this.client.eject()}_cleanCopy(e){return JSON.parse(JSON.stringify(e))}_insertLinks(e,t,s){const r=e[t];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]))}_insertRelations(e,t,s,r){if(s.indexOf(`${e.component}.${t}`)>-1){if(typeof e[t]=="string")this.relations[r][e[t]]&&(e[t]=this._cleanCopy(this.relations[r][e[t]]));else if(e[t]&&e[t].constructor===Array){const o=[];e[t].forEach(i=>{this.relations[r][i]&&o.push(this._cleanCopy(this.relations[r][i]))}),e[t]=o}}}iterateTree(e,t,s){const r=o=>{if(o!=null){if(o.constructor===Array)for(let i=0;i<o.length;i++)r(o[i]);else if(o.constructor===Object){if(o._stopResolving)return;for(const i in o)(o.component&&o._uid||o.type==="link")&&(this._insertRelations(o,i,t,s),this._insertLinks(o,i,s)),r(o[i])}}};r(e.content)}async resolveLinks(e,t,s){let r=[];if(e.link_uuids){const o=e.link_uuids.length,i=[],c=50;for(let a=0;a<o;a+=c){const l=Math.min(o,a+c);i.push(e.link_uuids.slice(a,l))}for(let a=0;a<i.length;a++)(await this.getStories({per_page:c,language:t.language,version:t.version,by_uuids:i[a].join(",")})).data.stories.forEach(l=>{r.push(l)})}else r=e.links;r.forEach(o=>{this.links[s][o.uuid]={...o,_stopResolving:!0}})}async resolveRelations(e,t,s){let r=[];if(e.rel_uuids){const o=e.rel_uuids.length,i=[],c=50;for(let a=0;a<o;a+=c){const l=Math.min(o,a+c);i.push(e.rel_uuids.slice(a,l))}for(let a=0;a<i.length;a++)(await this.getStories({per_page:c,language:t.language,version:t.version,by_uuids:i[a].join(",")})).data.stories.forEach(l=>{r.push(l)})}else r=e.rels;r&&r.length>0&&r.forEach(o=>{this.relations[s][o.uuid]={...o,_stopResolving:!0}})}async resolveStories(e,t,s){var r,o;let i=[];if(this.links[s]={},this.relations[s]={},typeof t.resolve_relations<"u"&&t.resolve_relations.length>0&&(typeof t.resolve_relations=="string"&&(i=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||(o=e.link_uuids)!=null&&o.length)&&await this.resolveLinks(e,t,s),this.resolveNestedRelations)for(const c in this.relations[s])this.iterateTree(this.relations[s][c],i,s);e.story?this.iterateTree(e.story,i,s):e.stories.forEach(c=>{this.iterateTree(c,i,s)}),delete this.links[s],delete this.relations[s]}async cacheResponse(e,t,s){const r=this.helpers.stringify({url:e,params:t}),o=this.cacheProvider();if(this.cache.clear==="auto"&&t.version==="draft"&&await this.flushCache(),t.version==="published"&&e!="/cdn/spaces/me"){const i=await o.get(r);if(i)return Promise.resolve(i)}return new Promise((i,c)=>{try{(async()=>{var a;try{const l=await this.throttle("get",e,t);let u={data:l.data,headers:l.headers};if((a=l.headers)!=null&&a["per-page"]&&(u=Object.assign({},u,{perPage:l.headers["per-page"]?parseInt(l.headers["per-page"]):0,total:l.headers["per-page"]?parseInt(l.headers.total):0})),l.status!=200)return c(l);if(u.data.story||u.data.stories){const p=this.resolveCounter=++this.resolveCounter%1e3;await this.resolveStories(u.data,t,`${p}`)}return t.version==="published"&&e!="/cdn/spaces/me"&&await o.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),i(u)}catch(l){return c(l)}})()}catch{}})}throttledRequest(e,t,s){return this.client[e](t,s)}cacheVersions(){return f}cacheVersion(){return f[this.accessToken]}setCacheVersion(e){this.accessToken&&(f[this.accessToken]=e)}cacheProvider(){switch(this.cache.type){case"memory":return{get(e){return Promise.resolve(T[e])},getAll(){return Promise.resolve(T)},set(e,t){return T[e]=t,Promise.resolve(void 0)},flush(){return T={},Promise.resolve(void 0)}};case"custom":if(this.cache.custom)return this.cache.custom;default:return{get(){return Promise.resolve(void 0)},getAll(){return Promise.resolve(void 0)},set(){return Promise.resolve(void 0)},flush(){return Promise.resolve(void 0)}}}}async flushCache(){return await this.cacheProvider().flush(),this}}const pe=(n={})=>{const{apiOptions:e}=n;if(!e.accessToken){console.error("You need to provide an access token to interact with Storyblok API. Read https://www.storyblok.com/docs/api/content-delivery#topics/authentication");return}return{storyblokApi:new de(e)}},me=n=>{if(typeof n!="object"||typeof n._editable>"u")return{};const e=JSON.parse(n._editable.replace(/^<!--#storyblok#/,"").replace(/-->$/,""));return{"data-blok-c":JSON.stringify(e),"data-blok-uid":e.id+"-"+e.uid}};let R;const S="https://app.storyblok.com/f/storyblok-v2-latest.js",j=(n,e,t={})=>{var c;const r=!(typeof window>"u")&&typeof window.storyblokRegisterEvent<"u",i=+new URL((c=window.location)==null?void 0:c.href).searchParams.get("_storyblok")===n;if(!(!r||!i)){if(!n){console.warn("Story ID is not defined. Please provide a valid ID.");return}window.storyblokRegisterEvent(()=>{new window.StoryblokBridge(t).on(["input","published","change"],l=>{l.action==="input"&&l.story.id===n?e(l.story):(l.action==="change"||l.action==="published")&&l.storyId===n&&window.location.reload()})})}},_e=(n={})=>{var u,p;const{bridge:e,accessToken:t,use:s=[],apiOptions:r={},richText:o={}}=n;r.accessToken=r.accessToken||t;const i={bridge:e,apiOptions:r};let c={};s.forEach(_=>{c={...c,..._(i)}});const l=!(typeof window>"u")&&((p=(u=window.location)==null?void 0:u.search)==null?void 0:p.includes("_storyblok_tk"));return e!==!1&&l&&v(S),R=new b(o.schema),o.resolver&&U(R,o.resolver),c},U=(n,e)=>{n.addNode("blok",t=>{let s="";return t.attrs.body.forEach(r=>{s+=e(r.component,r)}),{html:s}})},H=n=>{var e,t;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)},ge=(n,e,t)=>{let s=t||R;if(!s){console.error("Please initialize the Storyblok SDK before calling the renderRichText function");return}return H(n)?"":(e&&(s=new b(e.schema),e.resolver&&U(s,e.resolver)),s.render(n))},fe=()=>v(S);d.RichTextResolver=b,d.RichTextSchema=A,d.apiPlugin=pe,d.isRichTextEmpty=H,d.loadStoryblokBridge=fe,d.registerStoryblokBridge=j,d.renderRichText=ge,d.storyblokEditable=me,d.storyblokInit=_e,d.useStoryblokBridge=j,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
|
package/dist/storyblok-js.mjs
CHANGED
@@ -1,33 +1,33 @@
|
|
1
|
-
let
|
2
|
-
const
|
1
|
+
let I = !1;
|
2
|
+
const O = [], A = (n) => new Promise((e, t) => {
|
3
3
|
if (typeof window > "u" || (window.storyblokRegisterEvent = (r) => {
|
4
4
|
if (window.location === window.parent.location) {
|
5
5
|
console.warn("You are not in Draft Mode or in the Visual Editor.");
|
6
6
|
return;
|
7
7
|
}
|
8
|
-
|
8
|
+
I ? r() : O.push(r);
|
9
9
|
}, document.getElementById("storyblok-javascript-bridge")))
|
10
10
|
return;
|
11
11
|
const s = document.createElement("script");
|
12
12
|
s.async = !0, s.src = n, s.id = "storyblok-javascript-bridge", s.onerror = (r) => t(r), s.onload = (r) => {
|
13
|
-
|
13
|
+
O.forEach((o) => o()), I = !0, e(r);
|
14
14
|
}, document.getElementsByTagName("head")[0].appendChild(s);
|
15
15
|
});
|
16
|
-
var
|
17
|
-
function
|
16
|
+
var j = Object.defineProperty, U = (n, e, t) => e in n ? j(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t, h = (n, e, t) => (U(n, typeof e != "symbol" ? e + "" : e, t), t);
|
17
|
+
function v(n) {
|
18
18
|
return !(n !== n || n === 1 / 0 || n === -1 / 0);
|
19
19
|
}
|
20
|
-
function
|
21
|
-
if (!
|
20
|
+
function H(n, e, t) {
|
21
|
+
if (!v(e))
|
22
22
|
throw new TypeError("Expected `limit` to be a finite number");
|
23
|
-
if (!
|
23
|
+
if (!v(t))
|
24
24
|
throw new TypeError("Expected `interval` to be a finite number");
|
25
25
|
const s = [];
|
26
|
-
let r = [],
|
27
|
-
const
|
28
|
-
|
26
|
+
let r = [], o = 0;
|
27
|
+
const i = function() {
|
28
|
+
o++;
|
29
29
|
const a = setTimeout(function() {
|
30
|
-
|
30
|
+
o--, s.length > 0 && i(), r = r.filter(function(u) {
|
31
31
|
return u !== a;
|
32
32
|
});
|
33
33
|
}, t);
|
@@ -36,13 +36,13 @@ function S(n, e, t) {
|
|
36
36
|
l.resolve(n.apply(l.self, l.args));
|
37
37
|
}, c = function(...a) {
|
38
38
|
const l = this;
|
39
|
-
return new Promise(function(u,
|
39
|
+
return new Promise(function(u, d) {
|
40
40
|
s.push({
|
41
41
|
resolve: u,
|
42
|
-
reject:
|
42
|
+
reject: d,
|
43
43
|
args: a,
|
44
44
|
self: l
|
45
|
-
}),
|
45
|
+
}), o < e && i();
|
46
46
|
});
|
47
47
|
};
|
48
48
|
return c.abort = function() {
|
@@ -53,20 +53,20 @@ function S(n, e, t) {
|
|
53
53
|
}), s.length = 0;
|
54
54
|
}, c;
|
55
55
|
}
|
56
|
-
const
|
56
|
+
const P = function(n, e) {
|
57
57
|
const t = {};
|
58
58
|
for (const s in n) {
|
59
59
|
const r = n[s];
|
60
60
|
e.indexOf(s) > -1 && r !== null && (t[s] = r);
|
61
61
|
}
|
62
62
|
return t;
|
63
|
-
},
|
63
|
+
}, x = (n) => n === "email", G = () => ({
|
64
64
|
singleTag: "hr"
|
65
|
-
}),
|
65
|
+
}), C = () => ({
|
66
66
|
tag: "blockquote"
|
67
|
-
}),
|
67
|
+
}), $ = () => ({
|
68
68
|
tag: "ul"
|
69
|
-
}),
|
69
|
+
}), B = (n) => ({
|
70
70
|
tag: [
|
71
71
|
"pre",
|
72
72
|
{
|
@@ -74,24 +74,24 @@ const C = function(n, e) {
|
|
74
74
|
attrs: n.attrs
|
75
75
|
}
|
76
76
|
]
|
77
|
-
}),
|
77
|
+
}), D = () => ({
|
78
78
|
singleTag: "br"
|
79
|
-
}),
|
79
|
+
}), L = (n) => ({
|
80
80
|
tag: `h${n.attrs.level}`
|
81
|
-
}),
|
81
|
+
}), M = (n) => ({
|
82
82
|
singleTag: [
|
83
83
|
{
|
84
84
|
tag: "img",
|
85
|
-
attrs:
|
85
|
+
attrs: P(n.attrs, ["src", "alt", "title"])
|
86
86
|
}
|
87
87
|
]
|
88
|
-
}),
|
88
|
+
}), V = () => ({
|
89
89
|
tag: "li"
|
90
|
-
}),
|
90
|
+
}), F = () => ({
|
91
91
|
tag: "ol"
|
92
|
-
}),
|
92
|
+
}), W = () => ({
|
93
93
|
tag: "p"
|
94
|
-
}),
|
94
|
+
}), K = (n) => ({
|
95
95
|
tag: [
|
96
96
|
{
|
97
97
|
tag: "span",
|
@@ -102,21 +102,21 @@ const C = function(n, e) {
|
|
102
102
|
}
|
103
103
|
}
|
104
104
|
]
|
105
|
-
}),
|
105
|
+
}), X = () => ({
|
106
106
|
tag: "b"
|
107
|
-
}),
|
107
|
+
}), z = () => ({
|
108
108
|
tag: "strike"
|
109
|
-
}),
|
109
|
+
}), q = () => ({
|
110
110
|
tag: "u"
|
111
|
-
}),
|
111
|
+
}), J = () => ({
|
112
112
|
tag: "strong"
|
113
|
-
}),
|
113
|
+
}), Y = () => ({
|
114
114
|
tag: "code"
|
115
|
-
}),
|
115
|
+
}), Z = () => ({
|
116
116
|
tag: "i"
|
117
117
|
}), Q = (n) => {
|
118
118
|
const e = { ...n.attrs }, { linktype: t = "url" } = n.attrs;
|
119
|
-
if (
|
119
|
+
if (x(t) && (e.href = `mailto:${e.href}`), e.anchor && (e.href = `${e.href}#${e.anchor}`, delete e.anchor), e.custom) {
|
120
120
|
for (const s in e.custom)
|
121
121
|
e[s] = e.custom[s];
|
122
122
|
delete e.custom;
|
@@ -129,25 +129,25 @@ const C = function(n, e) {
|
|
129
129
|
}
|
130
130
|
]
|
131
131
|
};
|
132
|
-
},
|
132
|
+
}, ee = (n) => ({
|
133
133
|
tag: [
|
134
134
|
{
|
135
135
|
tag: "span",
|
136
136
|
attrs: n.attrs
|
137
137
|
}
|
138
138
|
]
|
139
|
-
}),
|
139
|
+
}), te = () => ({
|
140
140
|
tag: "sub"
|
141
|
-
}),
|
141
|
+
}), se = () => ({
|
142
142
|
tag: "sup"
|
143
|
-
}),
|
143
|
+
}), re = (n) => ({
|
144
144
|
tag: [
|
145
145
|
{
|
146
146
|
tag: "span",
|
147
147
|
attrs: n.attrs
|
148
148
|
}
|
149
149
|
]
|
150
|
-
}),
|
150
|
+
}), oe = (n) => {
|
151
151
|
var e;
|
152
152
|
return (e = n.attrs) != null && e.color ? {
|
153
153
|
tag: [
|
@@ -161,7 +161,7 @@ const C = function(n, e) {
|
|
161
161
|
} : {
|
162
162
|
tag: ""
|
163
163
|
};
|
164
|
-
},
|
164
|
+
}, ne = (n) => {
|
165
165
|
var e;
|
166
166
|
return (e = n.attrs) != null && e.color ? {
|
167
167
|
tag: [
|
@@ -177,34 +177,34 @@ const C = function(n, e) {
|
|
177
177
|
};
|
178
178
|
}, ie = {
|
179
179
|
nodes: {
|
180
|
-
horizontal_rule:
|
181
|
-
blockquote:
|
182
|
-
bullet_list:
|
183
|
-
code_block:
|
184
|
-
hard_break:
|
185
|
-
heading:
|
186
|
-
image:
|
187
|
-
list_item:
|
188
|
-
ordered_list:
|
189
|
-
paragraph:
|
190
|
-
emoji:
|
180
|
+
horizontal_rule: G,
|
181
|
+
blockquote: C,
|
182
|
+
bullet_list: $,
|
183
|
+
code_block: B,
|
184
|
+
hard_break: D,
|
185
|
+
heading: L,
|
186
|
+
image: M,
|
187
|
+
list_item: V,
|
188
|
+
ordered_list: F,
|
189
|
+
paragraph: W,
|
190
|
+
emoji: K
|
191
191
|
},
|
192
192
|
marks: {
|
193
|
-
bold:
|
194
|
-
strike:
|
195
|
-
underline:
|
196
|
-
strong:
|
197
|
-
code:
|
198
|
-
italic:
|
193
|
+
bold: X,
|
194
|
+
strike: z,
|
195
|
+
underline: q,
|
196
|
+
strong: J,
|
197
|
+
code: Y,
|
198
|
+
italic: Z,
|
199
199
|
link: Q,
|
200
|
-
styled:
|
201
|
-
subscript:
|
202
|
-
superscript:
|
203
|
-
anchor:
|
204
|
-
highlight:
|
205
|
-
textStyle:
|
206
|
-
}
|
207
|
-
},
|
200
|
+
styled: ee,
|
201
|
+
subscript: te,
|
202
|
+
superscript: se,
|
203
|
+
anchor: re,
|
204
|
+
highlight: oe,
|
205
|
+
textStyle: ne
|
206
|
+
}
|
207
|
+
}, ae = function(n) {
|
208
208
|
const e = {
|
209
209
|
"&": "&",
|
210
210
|
"<": "<",
|
@@ -214,7 +214,7 @@ const C = function(n, e) {
|
|
214
214
|
}, t = /[&<>"']/g, s = RegExp(t.source);
|
215
215
|
return n && s.test(n) ? n.replace(t, (r) => e[r]) : n;
|
216
216
|
};
|
217
|
-
class
|
217
|
+
class b {
|
218
218
|
constructor(e) {
|
219
219
|
h(this, "marks"), h(this, "nodes"), e || (e = ie), this.marks = e.marks || [], this.nodes = e.nodes || [];
|
220
220
|
}
|
@@ -259,31 +259,31 @@ class y {
|
|
259
259
|
), "";
|
260
260
|
}
|
261
261
|
optimizeImages(e, t) {
|
262
|
-
let s = 0, r = 0,
|
263
|
-
typeof t != "boolean" && (typeof t.width == "number" && t.width > 0 && (
|
264
|
-
const c = s > 0 || r > 0 ||
|
262
|
+
let s = 0, r = 0, o = "", i = "";
|
263
|
+
typeof t != "boolean" && (typeof t.width == "number" && t.width > 0 && (o += `width="${t.width}" `, s = t.width), typeof t.height == "number" && t.height > 0 && (o += `height="${t.height}" `, r = t.height), (t.loading === "lazy" || t.loading === "eager") && (o += `loading="${t.loading}" `), typeof t.class == "string" && t.class.length > 0 && (o += `class="${t.class}" `), t.filters && (typeof t.filters.blur == "number" && t.filters.blur >= 0 && t.filters.blur <= 100 && (i += `:blur(${t.filters.blur})`), typeof t.filters.brightness == "number" && t.filters.brightness >= -100 && t.filters.brightness <= 100 && (i += `:brightness(${t.filters.brightness})`), t.filters.fill && (t.filters.fill.match(/[0-9A-Fa-f]{6}/g) || t.filters.fill === "transparent") && (i += `:fill(${t.filters.fill})`), t.filters.format && ["webp", "png", "jpeg"].includes(t.filters.format) && (i += `:format(${t.filters.format})`), typeof t.filters.grayscale == "boolean" && t.filters.grayscale && (i += ":grayscale()"), typeof t.filters.quality == "number" && t.filters.quality >= 0 && t.filters.quality <= 100 && (i += `:quality(${t.filters.quality})`), t.filters.rotate && [90, 180, 270].includes(t.filters.rotate) && (i += `:rotate(${t.filters.rotate})`), i.length > 0 && (i = "/filters" + i))), o.length > 0 && (e = e.replace(/<img/g, `<img ${o.trim()}`));
|
264
|
+
const c = s > 0 || r > 0 || i.length > 0 ? `${s}x${r}${i}` : "";
|
265
265
|
return e = e.replace(
|
266
266
|
/a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g,
|
267
267
|
`a.storyblok.com/f/$1/$2.$3/m/${c}`
|
268
268
|
), typeof t != "boolean" && (t.sizes || t.srcset) && (e = e.replace(/<img.*?src=["|'](.*?)["|']/g, (a) => {
|
269
269
|
var l, u;
|
270
|
-
const
|
270
|
+
const d = a.match(
|
271
271
|
/a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g
|
272
272
|
);
|
273
|
-
if (
|
274
|
-
const
|
275
|
-
srcset: (l = t.srcset) == null ? void 0 : l.map((
|
276
|
-
if (typeof
|
277
|
-
return `//${
|
278
|
-
if (typeof
|
279
|
-
let
|
280
|
-
return typeof
|
273
|
+
if (d && d.length > 0) {
|
274
|
+
const m = {
|
275
|
+
srcset: (l = t.srcset) == null ? void 0 : l.map((p) => {
|
276
|
+
if (typeof p == "number")
|
277
|
+
return `//${d}/m/${p}x0${i} ${p}w`;
|
278
|
+
if (typeof p == "object" && p.length === 2) {
|
279
|
+
let T = 0, E = 0;
|
280
|
+
return typeof p[0] == "number" && (T = p[0]), typeof p[1] == "number" && (E = p[1]), `//${d}/m/${T}x${E}${i} ${T}w`;
|
281
281
|
}
|
282
282
|
}).join(", "),
|
283
|
-
sizes: (u = t.sizes) == null ? void 0 : u.map((
|
283
|
+
sizes: (u = t.sizes) == null ? void 0 : u.map((p) => p).join(", ")
|
284
284
|
};
|
285
|
-
let
|
286
|
-
return
|
285
|
+
let f = "";
|
286
|
+
return m.srcset && (f += `srcset="${m.srcset}" `), m.sizes && (f += `sizes="${m.sizes}" `), a.replace(/<img/g, `<img ${f.trim()}`);
|
287
287
|
}
|
288
288
|
return a;
|
289
289
|
})), e;
|
@@ -291,15 +291,15 @@ class y {
|
|
291
291
|
renderNode(e) {
|
292
292
|
const t = [];
|
293
293
|
e.marks && e.marks.forEach((r) => {
|
294
|
-
const
|
295
|
-
|
294
|
+
const o = this.getMatchingMark(r);
|
295
|
+
o && o.tag !== "" && t.push(this.renderOpeningTag(o.tag));
|
296
296
|
});
|
297
297
|
const s = this.getMatchingNode(e);
|
298
298
|
return s && s.tag && t.push(this.renderOpeningTag(s.tag)), e.content ? e.content.forEach((r) => {
|
299
299
|
t.push(this.renderNode(r));
|
300
|
-
}) : e.text ? t.push(
|
301
|
-
const
|
302
|
-
|
300
|
+
}) : e.text ? t.push(ae(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 o = this.getMatchingMark(r);
|
302
|
+
o && o.tag !== "" && t.push(this.renderClosingTag(o.tag));
|
303
303
|
}), t.join("");
|
304
304
|
}
|
305
305
|
renderTag(e, t) {
|
@@ -309,9 +309,9 @@ class y {
|
|
309
309
|
{
|
310
310
|
let r = `<${s.tag}`;
|
311
311
|
if (s.attrs)
|
312
|
-
for (const
|
313
|
-
const
|
314
|
-
|
312
|
+
for (const o in s.attrs) {
|
313
|
+
const i = s.attrs[o];
|
314
|
+
i !== null && (r += ` ${o}="${i}"`);
|
315
315
|
}
|
316
316
|
return `${r}${t}>`;
|
317
317
|
}
|
@@ -358,7 +358,7 @@ class k {
|
|
358
358
|
page: s
|
359
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
360
|
const s = Math.abs(t - e) || 0, r = e < t ? 1 : -1;
|
361
|
-
return this.arrayFrom(s, (
|
361
|
+
return this.arrayFrom(s, (o, i) => i * r + e);
|
362
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
363
|
}
|
364
364
|
/**
|
@@ -370,16 +370,16 @@ class k {
|
|
370
370
|
*/
|
371
371
|
stringify(e, t, s) {
|
372
372
|
const r = [];
|
373
|
-
for (const
|
374
|
-
if (!Object.prototype.hasOwnProperty.call(e,
|
373
|
+
for (const o in e) {
|
374
|
+
if (!Object.prototype.hasOwnProperty.call(e, o))
|
375
375
|
continue;
|
376
|
-
const
|
376
|
+
const i = e[o], c = s ? "" : encodeURIComponent(o);
|
377
377
|
let a;
|
378
|
-
typeof
|
379
|
-
|
378
|
+
typeof i == "object" ? a = this.stringify(
|
379
|
+
i,
|
380
380
|
t ? t + encodeURIComponent("[" + c + "]") : c,
|
381
|
-
Array.isArray(
|
382
|
-
) : a = (t ? t + encodeURIComponent("[" + c + "]") : c) + "=" + encodeURIComponent(
|
381
|
+
Array.isArray(i)
|
382
|
+
) : a = (t ? t + encodeURIComponent("[" + c + "]") : c) + "=" + encodeURIComponent(i), r.push(a);
|
383
383
|
}
|
384
384
|
return r.join("&");
|
385
385
|
}
|
@@ -400,7 +400,7 @@ class k {
|
|
400
400
|
}
|
401
401
|
}
|
402
402
|
}
|
403
|
-
class
|
403
|
+
class le {
|
404
404
|
constructor(e) {
|
405
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 = !1, this.url = "", this.parameters = {};
|
406
406
|
}
|
@@ -445,15 +445,15 @@ class oe {
|
|
445
445
|
)}`;
|
446
446
|
} else
|
447
447
|
s = JSON.stringify(this.parameters);
|
448
|
-
const r = new URL(t),
|
448
|
+
const r = new URL(t), o = new AbortController(), { signal: i } = o;
|
449
449
|
let c;
|
450
|
-
this.timeout && (c = setTimeout(() =>
|
450
|
+
this.timeout && (c = setTimeout(() => o.abort(), this.timeout));
|
451
451
|
try {
|
452
452
|
const a = await this.fetch(`${r}`, {
|
453
453
|
method: e,
|
454
454
|
headers: this.headers,
|
455
455
|
body: s,
|
456
|
-
signal:
|
456
|
+
signal: i
|
457
457
|
});
|
458
458
|
this.timeout && clearTimeout(c);
|
459
459
|
const l = await this._responseHandler(a);
|
@@ -472,18 +472,23 @@ class oe {
|
|
472
472
|
return new Promise((s, r) => {
|
473
473
|
if (t.test(`${e.status}`))
|
474
474
|
return s(e);
|
475
|
-
const
|
475
|
+
const o = {
|
476
476
|
message: new Error(e.statusText),
|
477
477
|
status: e.status,
|
478
478
|
response: Array.isArray(e.data) ? e.data[0] : e.data.error || e.data.slug
|
479
479
|
};
|
480
|
-
r(
|
480
|
+
r(o);
|
481
481
|
});
|
482
482
|
}
|
483
483
|
}
|
484
|
-
|
485
|
-
|
486
|
-
|
484
|
+
const N = "SB-Agent", y = {
|
485
|
+
defaultAgentName: "SB-JS-CLIENT",
|
486
|
+
defaultAgentVersion: "SB-Agent-Version",
|
487
|
+
packageVersion: "2.2.4"
|
488
|
+
};
|
489
|
+
let g = {};
|
490
|
+
const _ = {};
|
491
|
+
class ce {
|
487
492
|
/**
|
488
493
|
*
|
489
494
|
* @param config ISbConfig interface
|
@@ -493,15 +498,16 @@ class ae {
|
|
493
498
|
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
499
|
let s = e.endpoint || t;
|
495
500
|
if (!s) {
|
496
|
-
const
|
497
|
-
e.oauthToken ? s = `${c}://${
|
501
|
+
const i = new k().getRegionURL, c = e.https === !1 ? "http" : "https";
|
502
|
+
e.oauthToken ? s = `${c}://${i(e.region)}/v1` : s = `${c}://${i(e.region)}/v2`;
|
498
503
|
}
|
499
504
|
const r = new Headers();
|
500
|
-
r.set("Content-Type", "application/json"), r.set("Accept", "application/json"),
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
+
if (r.set("Content-Type", "application/json"), r.set("Accept", "application/json"), e.headers)
|
506
|
+
for (const i in e.headers)
|
507
|
+
r.set(i, e.headers[i]);
|
508
|
+
r.has(N) || (r.set(N, y.defaultAgentName), r.set(y.defaultAgentVersion, y.packageVersion));
|
509
|
+
let o = 5;
|
510
|
+
e.oauthToken && (r.set("Authorization", e.oauthToken), o = 3), e.rateLimit && (o = e.rateLimit), e.richTextSchema ? this.richTextResolver = new b(e.richTextSchema) : this.richTextResolver = new b(), e.componentResolver && this.setComponentResolver(e.componentResolver), this.maxRetries = e.maxRetries || 5, this.throttle = H(this.throttledRequest, o, 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 || !0, this.client = new le({
|
505
511
|
baseURL: s,
|
506
512
|
timeout: e.timeout || 0,
|
507
513
|
headers: r,
|
@@ -520,17 +526,17 @@ class ae {
|
|
520
526
|
});
|
521
527
|
}
|
522
528
|
parseParams(e) {
|
523
|
-
return e.version || (e.version = "published"), e.token || (e.token = this.getToken()), e.cv || (e.cv =
|
529
|
+
return e.version || (e.version = "published"), e.token || (e.token = this.getToken()), e.cv || (e.cv = _[e.token]), Array.isArray(e.resolve_relations) && (e.resolve_relations = e.resolve_relations.join(",")), e;
|
524
530
|
}
|
525
531
|
factoryParamOptions(e, t) {
|
526
532
|
return this.helpers.isCDNUrl(e) ? this.parseParams(t) : t;
|
527
533
|
}
|
528
534
|
makeRequest(e, t, s, r) {
|
529
|
-
const
|
535
|
+
const o = this.factoryParamOptions(
|
530
536
|
e,
|
531
537
|
this.helpers.getOptionsPage(t, s, r)
|
532
538
|
);
|
533
|
-
return this.cacheResponse(e,
|
539
|
+
return this.cacheResponse(e, o);
|
534
540
|
}
|
535
541
|
get(e, t) {
|
536
542
|
t || (t = {});
|
@@ -538,13 +544,13 @@ class ae {
|
|
538
544
|
return this.cacheResponse(s, r);
|
539
545
|
}
|
540
546
|
async getAll(e, t, s) {
|
541
|
-
const r = (t == null ? void 0 : t.per_page) || 25,
|
547
|
+
const r = (t == null ? void 0 : t.per_page) || 25, o = `/${e}`, i = o.split("/"), c = s || i[i.length - 1], a = 1, l = await this.makeRequest(o, t, r, a), u = l.total ? Math.ceil(l.total / r) : 1, d = await this.helpers.asyncMap(
|
542
548
|
this.helpers.range(a, u),
|
543
|
-
(
|
549
|
+
(m) => this.makeRequest(o, t, r, m + 1)
|
544
550
|
);
|
545
551
|
return this.helpers.flatMap(
|
546
|
-
[l, ...
|
547
|
-
(
|
552
|
+
[l, ...d],
|
553
|
+
(m) => Object.values(m.data[c])
|
548
554
|
);
|
549
555
|
}
|
550
556
|
post(e, t) {
|
@@ -585,33 +591,33 @@ class ae {
|
|
585
591
|
this.relations[r][e[t]]
|
586
592
|
));
|
587
593
|
else if (e[t] && e[t].constructor === Array) {
|
588
|
-
const
|
589
|
-
e[t].forEach((
|
590
|
-
this.relations[r][
|
591
|
-
}), e[t] =
|
594
|
+
const o = [];
|
595
|
+
e[t].forEach((i) => {
|
596
|
+
this.relations[r][i] && o.push(this._cleanCopy(this.relations[r][i]));
|
597
|
+
}), e[t] = o;
|
592
598
|
}
|
593
599
|
}
|
594
600
|
}
|
595
601
|
iterateTree(e, t, s) {
|
596
|
-
const r = (
|
597
|
-
if (
|
598
|
-
if (
|
599
|
-
for (let
|
600
|
-
r(i
|
601
|
-
else if (
|
602
|
-
if (
|
602
|
+
const r = (o) => {
|
603
|
+
if (o != null) {
|
604
|
+
if (o.constructor === Array)
|
605
|
+
for (let i = 0; i < o.length; i++)
|
606
|
+
r(o[i]);
|
607
|
+
else if (o.constructor === Object) {
|
608
|
+
if (o._stopResolving)
|
603
609
|
return;
|
604
|
-
for (const
|
605
|
-
(
|
606
|
-
i,
|
610
|
+
for (const i in o)
|
611
|
+
(o.component && o._uid || o.type === "link") && (this._insertRelations(
|
607
612
|
o,
|
613
|
+
i,
|
608
614
|
t,
|
609
615
|
s
|
610
616
|
), this._insertLinks(
|
611
|
-
i,
|
612
617
|
o,
|
618
|
+
i,
|
613
619
|
s
|
614
|
-
)), r(i
|
620
|
+
)), r(o[i]);
|
615
621
|
}
|
616
622
|
}
|
617
623
|
};
|
@@ -620,17 +626,17 @@ class ae {
|
|
620
626
|
async resolveLinks(e, t, s) {
|
621
627
|
let r = [];
|
622
628
|
if (e.link_uuids) {
|
623
|
-
const
|
624
|
-
for (let a = 0; a <
|
625
|
-
const l = Math.min(
|
626
|
-
|
629
|
+
const o = e.link_uuids.length, i = [], c = 50;
|
630
|
+
for (let a = 0; a < o; a += c) {
|
631
|
+
const l = Math.min(o, a + c);
|
632
|
+
i.push(e.link_uuids.slice(a, l));
|
627
633
|
}
|
628
|
-
for (let a = 0; a <
|
634
|
+
for (let a = 0; a < i.length; a++)
|
629
635
|
(await this.getStories({
|
630
636
|
per_page: c,
|
631
637
|
language: t.language,
|
632
638
|
version: t.version,
|
633
|
-
by_uuids:
|
639
|
+
by_uuids: i[a].join(",")
|
634
640
|
})).data.stories.forEach(
|
635
641
|
(l) => {
|
636
642
|
r.push(l);
|
@@ -638,9 +644,9 @@ class ae {
|
|
638
644
|
);
|
639
645
|
} else
|
640
646
|
r = e.links;
|
641
|
-
r.forEach((
|
642
|
-
this.links[s][
|
643
|
-
...
|
647
|
+
r.forEach((o) => {
|
648
|
+
this.links[s][o.uuid] = {
|
649
|
+
...o,
|
644
650
|
_stopResolving: !0
|
645
651
|
};
|
646
652
|
});
|
@@ -648,51 +654,51 @@ class ae {
|
|
648
654
|
async resolveRelations(e, t, s) {
|
649
655
|
let r = [];
|
650
656
|
if (e.rel_uuids) {
|
651
|
-
const
|
652
|
-
for (let a = 0; a <
|
653
|
-
const l = Math.min(
|
654
|
-
|
657
|
+
const o = e.rel_uuids.length, i = [], c = 50;
|
658
|
+
for (let a = 0; a < o; a += c) {
|
659
|
+
const l = Math.min(o, a + c);
|
660
|
+
i.push(e.rel_uuids.slice(a, l));
|
655
661
|
}
|
656
|
-
for (let a = 0; a <
|
662
|
+
for (let a = 0; a < i.length; a++)
|
657
663
|
(await this.getStories({
|
658
664
|
per_page: c,
|
659
665
|
language: t.language,
|
660
666
|
version: t.version,
|
661
|
-
by_uuids:
|
667
|
+
by_uuids: i[a].join(",")
|
662
668
|
})).data.stories.forEach((l) => {
|
663
669
|
r.push(l);
|
664
670
|
});
|
665
671
|
} else
|
666
672
|
r = e.rels;
|
667
|
-
r && r.length > 0 && r.forEach((
|
668
|
-
this.relations[s][
|
669
|
-
...
|
673
|
+
r && r.length > 0 && r.forEach((o) => {
|
674
|
+
this.relations[s][o.uuid] = {
|
675
|
+
...o,
|
670
676
|
_stopResolving: !0
|
671
677
|
};
|
672
678
|
});
|
673
679
|
}
|
674
680
|
async resolveStories(e, t, s) {
|
675
|
-
var r,
|
676
|
-
let
|
677
|
-
if (this.links[s] = {}, this.relations[s] = {}, typeof t.resolve_relations < "u" && t.resolve_relations.length > 0 && (typeof t.resolve_relations == "string" && (
|
681
|
+
var r, o;
|
682
|
+
let i = [];
|
683
|
+
if (this.links[s] = {}, this.relations[s] = {}, typeof t.resolve_relations < "u" && t.resolve_relations.length > 0 && (typeof t.resolve_relations == "string" && (i = 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 || (o = e.link_uuids) != null && o.length) && await this.resolveLinks(e, t, s), this.resolveNestedRelations)
|
678
684
|
for (const c in this.relations[s])
|
679
685
|
this.iterateTree(
|
680
686
|
this.relations[s][c],
|
681
|
-
|
687
|
+
i,
|
682
688
|
s
|
683
689
|
);
|
684
|
-
e.story ? this.iterateTree(e.story,
|
685
|
-
this.iterateTree(c,
|
690
|
+
e.story ? this.iterateTree(e.story, i, s) : e.stories.forEach((c) => {
|
691
|
+
this.iterateTree(c, i, s);
|
686
692
|
}), delete this.links[s], delete this.relations[s];
|
687
693
|
}
|
688
694
|
async cacheResponse(e, t, s) {
|
689
|
-
const r = this.helpers.stringify({ url: e, params: t }),
|
695
|
+
const r = this.helpers.stringify({ url: e, params: t }), o = this.cacheProvider();
|
690
696
|
if (this.cache.clear === "auto" && t.version === "draft" && await this.flushCache(), t.version === "published" && e != "/cdn/spaces/me") {
|
691
|
-
const
|
692
|
-
if (
|
693
|
-
return Promise.resolve(
|
697
|
+
const i = await o.get(r);
|
698
|
+
if (i)
|
699
|
+
return Promise.resolve(i);
|
694
700
|
}
|
695
|
-
return new Promise((
|
701
|
+
return new Promise((i, c) => {
|
696
702
|
try {
|
697
703
|
(async () => {
|
698
704
|
var a;
|
@@ -705,10 +711,10 @@ class ae {
|
|
705
711
|
})), l.status != 200)
|
706
712
|
return c(l);
|
707
713
|
if (u.data.story || u.data.stories) {
|
708
|
-
const
|
709
|
-
await this.resolveStories(u.data, t, `${
|
714
|
+
const d = this.resolveCounter = ++this.resolveCounter % 1e3;
|
715
|
+
await this.resolveStories(u.data, t, `${d}`);
|
710
716
|
}
|
711
|
-
return t.version === "published" && e != "/cdn/spaces/me" && await
|
717
|
+
return t.version === "published" && e != "/cdn/spaces/me" && await o.set(r, u), u.data.cv && t.token && (t.version == "draft" && _[t.token] != u.data.cv && await this.flushCache(), _[t.token] = u.data.cv), i(u);
|
712
718
|
} catch (l) {
|
713
719
|
return c(l);
|
714
720
|
}
|
@@ -721,29 +727,29 @@ class ae {
|
|
721
727
|
return this.client[e](t, s);
|
722
728
|
}
|
723
729
|
cacheVersions() {
|
724
|
-
return
|
730
|
+
return _;
|
725
731
|
}
|
726
732
|
cacheVersion() {
|
727
|
-
return
|
733
|
+
return _[this.accessToken];
|
728
734
|
}
|
729
735
|
setCacheVersion(e) {
|
730
|
-
this.accessToken && (
|
736
|
+
this.accessToken && (_[this.accessToken] = e);
|
731
737
|
}
|
732
738
|
cacheProvider() {
|
733
739
|
switch (this.cache.type) {
|
734
740
|
case "memory":
|
735
741
|
return {
|
736
742
|
get(e) {
|
737
|
-
return Promise.resolve(
|
743
|
+
return Promise.resolve(g[e]);
|
738
744
|
},
|
739
745
|
getAll() {
|
740
|
-
return Promise.resolve(
|
746
|
+
return Promise.resolve(g);
|
741
747
|
},
|
742
748
|
set(e, t) {
|
743
|
-
return
|
749
|
+
return g[e] = t, Promise.resolve(void 0);
|
744
750
|
},
|
745
751
|
flush() {
|
746
|
-
return
|
752
|
+
return g = {}, Promise.resolve(void 0);
|
747
753
|
}
|
748
754
|
};
|
749
755
|
case "custom":
|
@@ -770,7 +776,7 @@ class ae {
|
|
770
776
|
return await this.cacheProvider().flush(), this;
|
771
777
|
}
|
772
778
|
}
|
773
|
-
const
|
779
|
+
const ue = (n = {}) => {
|
774
780
|
const { apiOptions: e } = n;
|
775
781
|
if (!e.accessToken) {
|
776
782
|
console.error(
|
@@ -778,8 +784,8 @@ const ce = (n = {}) => {
|
|
778
784
|
);
|
779
785
|
return;
|
780
786
|
}
|
781
|
-
return { storyblokApi: new
|
782
|
-
},
|
787
|
+
return { storyblokApi: new ce(e) };
|
788
|
+
}, de = (n) => {
|
783
789
|
if (typeof n != "object" || typeof n._editable > "u")
|
784
790
|
return {};
|
785
791
|
const e = JSON.parse(
|
@@ -790,13 +796,13 @@ const ce = (n = {}) => {
|
|
790
796
|
"data-blok-uid": e.id + "-" + e.uid
|
791
797
|
};
|
792
798
|
};
|
793
|
-
let
|
794
|
-
const
|
799
|
+
let R;
|
800
|
+
const w = "https://app.storyblok.com/f/storyblok-v2-latest.js", pe = (n, e, t = {}) => {
|
795
801
|
var c;
|
796
|
-
const r = !(typeof window > "u") && typeof window.storyblokRegisterEvent < "u",
|
802
|
+
const r = !(typeof window > "u") && typeof window.storyblokRegisterEvent < "u", i = +new URL((c = window.location) == null ? void 0 : c.href).searchParams.get(
|
797
803
|
"_storyblok"
|
798
804
|
) === n;
|
799
|
-
if (!(!r || !
|
805
|
+
if (!(!r || !i)) {
|
800
806
|
if (!n) {
|
801
807
|
console.warn("Story ID is not defined. Please provide a valid ID.");
|
802
808
|
return;
|
@@ -807,24 +813,24 @@ const x = "https://app.storyblok.com/f/storyblok-v2-latest.js", ue = (n, e, t =
|
|
807
813
|
});
|
808
814
|
});
|
809
815
|
}
|
810
|
-
},
|
811
|
-
var u,
|
816
|
+
}, me = (n = {}) => {
|
817
|
+
var u, d;
|
812
818
|
const {
|
813
819
|
bridge: e,
|
814
820
|
accessToken: t,
|
815
821
|
use: s = [],
|
816
822
|
apiOptions: r = {},
|
817
|
-
richText:
|
823
|
+
richText: o = {}
|
818
824
|
} = n;
|
819
825
|
r.accessToken = r.accessToken || t;
|
820
|
-
const
|
826
|
+
const i = { bridge: e, apiOptions: r };
|
821
827
|
let c = {};
|
822
|
-
s.forEach((
|
823
|
-
c = { ...c, ...
|
828
|
+
s.forEach((m) => {
|
829
|
+
c = { ...c, ...m(i) };
|
824
830
|
});
|
825
|
-
const l = !(typeof window > "u") && ((
|
826
|
-
return e !== !1 && l &&
|
827
|
-
},
|
831
|
+
const l = !(typeof window > "u") && ((d = (u = window.location) == null ? void 0 : u.search) == null ? void 0 : d.includes("_storyblok_tk"));
|
832
|
+
return e !== !1 && l && A(w), R = new b(o.schema), o.resolver && S(R, o.resolver), c;
|
833
|
+
}, S = (n, e) => {
|
828
834
|
n.addNode("blok", (t) => {
|
829
835
|
let s = "";
|
830
836
|
return t.attrs.body.forEach((r) => {
|
@@ -833,28 +839,28 @@ const x = "https://app.storyblok.com/f/storyblok-v2-latest.js", ue = (n, e, t =
|
|
833
839
|
html: s
|
834
840
|
};
|
835
841
|
});
|
836
|
-
},
|
842
|
+
}, he = (n) => {
|
837
843
|
var e, t;
|
838
844
|
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
|
-
},
|
840
|
-
let s = t ||
|
845
|
+
}, _e = (n, e, t) => {
|
846
|
+
let s = t || R;
|
841
847
|
if (!s) {
|
842
848
|
console.error(
|
843
849
|
"Please initialize the Storyblok SDK before calling the renderRichText function"
|
844
850
|
);
|
845
851
|
return;
|
846
852
|
}
|
847
|
-
return
|
848
|
-
}, ge = () =>
|
853
|
+
return he(n) ? "" : (e && (s = new b(e.schema), e.resolver && S(s, e.resolver)), s.render(n));
|
854
|
+
}, ge = () => A(w);
|
849
855
|
export {
|
850
|
-
|
856
|
+
b as RichTextResolver,
|
851
857
|
ie as RichTextSchema,
|
852
|
-
|
853
|
-
|
858
|
+
ue as apiPlugin,
|
859
|
+
he as isRichTextEmpty,
|
854
860
|
ge as loadStoryblokBridge,
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
861
|
+
pe as registerStoryblokBridge,
|
862
|
+
_e as renderRichText,
|
863
|
+
de as storyblokEditable,
|
864
|
+
me as storyblokInit,
|
865
|
+
pe as useStoryblokBridge
|
860
866
|
};
|
package/dist/types/types.d.ts
CHANGED
@@ -37,7 +37,7 @@ export interface ISbEventPayload<S extends ISbComponentType<string> = any> {
|
|
37
37
|
reload?: boolean;
|
38
38
|
}
|
39
39
|
export interface StoryblokBridgeConfigV2 {
|
40
|
-
resolveRelations?: string[];
|
40
|
+
resolveRelations?: string | string[];
|
41
41
|
customParent?: string;
|
42
42
|
preventClicks?: boolean;
|
43
43
|
language?: string;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@storyblok/js",
|
3
|
-
"version": "2.2.
|
3
|
+
"version": "2.2.4",
|
4
4
|
"description": "SDK to integrate Storyblok into your project using JavaScript.",
|
5
5
|
"main": "./dist/storyblok-js.js",
|
6
6
|
"module": "./dist/storyblok-js.mjs",
|
@@ -28,7 +28,7 @@
|
|
28
28
|
"prepublishOnly": "npm run build && cp ../README.md ./"
|
29
29
|
},
|
30
30
|
"dependencies": {
|
31
|
-
"storyblok-js-client": "^5.
|
31
|
+
"storyblok-js-client": "^5.12.0"
|
32
32
|
},
|
33
33
|
"devDependencies": {
|
34
34
|
"@tsconfig/recommended": "^1.0.1",
|
@@ -38,7 +38,7 @@
|
|
38
38
|
"isomorphic-fetch": "^3.0.0",
|
39
39
|
"start-server-and-test": "^1.15.2",
|
40
40
|
"vite": "^4.0.4",
|
41
|
-
"vitest": "^0.
|
41
|
+
"vitest": "^0.33.0"
|
42
42
|
},
|
43
43
|
"babel": {
|
44
44
|
"presets": [
|