This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
1 lines (1 loc) · 11 KB
/
index.js
1
(function(){"use strict";const n=window.Vue;function j(){return window.panel}function E(){return j().api}function U(){const s=E();return{load:({parent:t,name:e})=>s.get(`${t}/sections/${e}`)}}function z(){var o;return((o=n.getCurrentInstance())==null?void 0:o.proxy).$store}const D=n.computed;n.customRef,n.defineAsyncComponent,n.defineComponent,n.effectScope,n.getCurrentInstance,n.getCurrentScope,n.h,n.inject,n.isProxy,n.isReactive,n.isReadonly,n.isRef,n.isShallow,n.markRaw,n.nextTick,n.onActivated,n.onBeforeMount;const K=n.onBeforeUnmount;n.onBeforeUpdate,n.onDeactivated,n.onErrorCaptured,n.onMounted,n.onRenderTracked,n.onRenderTriggered,n.onScopeDispose,n.onServerPrefetch,n.onUnmounted,n.onUpdated,n.provide,n.proxyRefs,n.reactive,n.readonly;const g=n.ref;n.shallowReactive,n.shallowReadonly,n.shallowRef,n.toRaw,n.toRef,n.toRefs,n.triggerRef,n.unref,n.useAttrs,n.useCssModule,n.useCssVars,n.useListeners,n.useSlots,n.watch,n.watchEffect,n.watchPostEffect,n.watchSyncEffect;const V={blueprint:String,lock:[Boolean,Object],help:String,name:String,parent:String,timestamp:Number},A="__content-translator__/translate";class Y extends Error{constructor(o){super(),this.name="AbortError",this.message=o}}const N=s=>globalThis.DOMException===void 0?new Y(s):new DOMException(s),$=s=>{const o=s.reason===void 0?N("This operation was aborted."):s.reason;return o instanceof Error?o:N(o)};async function q(s,o,{concurrency:t=Number.POSITIVE_INFINITY,stopOnError:e=!0,signal:p}={}){return new Promise((u,k)=>{if(s[Symbol.iterator]===void 0&&s[Symbol.asyncIterator]===void 0)throw new TypeError(`Expected \`input\` to be either an \`Iterable\` or \`AsyncIterable\`, got (${typeof s})`);if(typeof o!="function")throw new TypeError("Mapper function is required");if(!((Number.isSafeInteger(t)||t===Number.POSITIVE_INFINITY)&&t>=1))throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${t}\` (${typeof t})`);const m=[],d=[],h=new Map;let x=!1,y=!1,w=!1,b=0,S=0;const F=s[Symbol.iterator]===void 0?s[Symbol.asyncIterator]():s[Symbol.iterator](),v=r=>{x=!0,y=!0,k(r)};p&&(p.aborted&&v($(p)),p.addEventListener("abort",()=>{v($(p))}));const a=async()=>{if(y)return;const r=await F.next(),i=S;if(S++,r.done){if(w=!0,b===0&&!y){if(!e&&d.length>0){v(new AggregateError(d));return}if(y=!0,h.size===0){u(m);return}const l=[];for(const[_,C]of m.entries())h.get(_)!==B&&l.push(C);u(l)}return}b++,(async()=>{try{const l=await r.value;if(y)return;const _=await o(l,i);_===B&&h.set(i,_),m[i]=_,b--,await a()}catch(l){if(e)v(l);else{d.push(l),b--;try{await a()}catch(_){v(_)}}}})()};(async()=>{for(let r=0;r<t;r++){try{await a()}catch(i){v(i);break}if(w||x)break}})()})}const B=Symbol("skip");async function J(s,o){return q(s,t=>t(),o)}function G(){const s=E();return{recursiveTranslateContent:async(t,{sourceLanguage:e,targetLanguage:p,fields:u={},translatableStructureFields:k=[],translatableObjectFields:m=[],translatableBlocks:d={}})=>{var b,S,F,v;const h=[],x=(a,r)=>{for(const i in a)r.includes(i)&&a[i]&&(typeof a[i]=="string"?h.push(async()=>{const l=await s.post(A,{sourceLanguage:e,targetLanguage:p,text:a[i]});a[i]=l.text}):Array.isArray(a[i])&&a[i].every(l=>typeof l=="string")&&h.push(async()=>{for(const l in a[i]){if(!a[i][l])continue;const _=await s.post(A,{sourceLanguage:e,targetLanguage:p,text:a[i][l]});a[i][l]=_.text}}))},y=a=>{for(const r of a)x(r,k)},w=a=>{for(const r of a)if(!(!O(r.content)||!r.id||r.isHidden===!0)&&Object.keys(d).includes(r.type)){for(const i of Object.keys(r.content))if(H(d[r.type]).includes(i)&&r.content[i]){if(Array.isArray(r.content[i])&&r.content[i].every(l=>O(l)&&l.content)){w(r.content[i]);continue}if(Array.isArray(r.content[i])&&r.content[i].every(l=>O(l))){y(r.content[i]);continue}if(O(r.content[i])){x(r.content[i],m);continue}h.push(async()=>{const l=await s.post(A,{sourceLanguage:e,targetLanguage:p,text:r.content[i]});r.content[i]=l.text})}}};for(const a in t)if(t[a]){if(typeof t[a]=="string")h.push(async()=>{const r=await s.post(A,{sourceLanguage:e,targetLanguage:p,text:t[a]});t[a]=r.text});else if(Array.isArray(t[a])&&t[a].length>0){if(t[a].every(r=>typeof r=="string")){t[a]=await Promise.all(t[a].filter(Boolean).map(async r=>(await s.post(A,{sourceLanguage:e,targetLanguage:p,text:r})).text));continue}if(((b=u[a])==null?void 0:b.type)==="layout"){for(const r of t[a])for(const i of r.columns)w(i.blocks);continue}if(((S=u[a])==null?void 0:S.type)==="blocks"&&w(t[a]),((F=u[a])==null?void 0:F.type)==="structure"){y(t[a]);continue}}if(((v=u[a])==null?void 0:v.type)==="object"&&O(t[a])){x(t[a],m);continue}}try{await J(h,{concurrency:5})}catch(a){throw console.error(a),a}return t}}}function H(s){return s=s??[],Array.isArray(s)?s:[s]}function O(s){return typeof s=="object"&&s!==null}function L(s,o,t,e,p,u,k,m){var d=typeof s=="function"?s.options:s;return o&&(d.render=o,d.staticRenderFns=t,d._compiled=!0),{exports:s,options:d}}const P={...V},Q=Object.assign({inheritAttrs:!1},{__name:"ContentTranslator",props:P,setup(s){const o=s,t=j(),e=z(),{recursiveTranslateContent:p}=G(),u=g(),k=g(!0),m=g([]),d=g([]),h=g([]),x=g([]),y=g([]),w=g(!1),b=g(),S=g(),F=g({}),v=t.languages.find(f=>f.default),a=D(()=>e.getters["content/values"]()),r=D(()=>Object.fromEntries(Object.entries(a.value).filter(([f])=>d.value.includes(f))));(async()=>{const{load:f}=U(),c=await f({parent:o.parent,name:o.name});u.value=i(c.label)||t.t("johannschopplich.content-translator.label"),k.value=c.confirm??c.config.confirm??!0,m.value=c.syncableFields??c.config.syncableFields??[],d.value=c.translatableFields??c.config.translatableFields??[],h.value=c.translatableStructureFields??c.config.translatableStructureFields??[],x.value=c.translatableObjectFields??c.config.translatableObjectFields??[],y.value=c.translatableBlocks??c.config.translatableBlocks??[],w.value=c.title??c.config.title??!1,b.value=c.fields??{},S.value=c.config??{},t.events.on("model.update",C),t.events.on("page.changeTitle",C),C()})(),K(()=>{t.events.off("model.update",C),t.events.off("page.changeTitle",C)});function i(f){return!f||typeof f=="string"?f:f[t.translation.code]??Object.values(f)[0]}async function l(f){let{title:c,content:M}=F.value;if(f){const I=await R(f);c=I.title,M=I.content}const T=Object.fromEntries(Object.entries(M).filter(([I])=>m.value.includes(I)));for(const[I,nt]of Object.entries(T))e.dispatch("content/update",[I,nt]);w.value&&(await t.api.patch(`${t.view.path}/title`,{title:c}),await t.view.reload()),t.notification.success(t.t("johannschopplich.content-translator.notification.synced"))}async function _(f,c){t.view.isLoading=!0;const M=JSON.parse(JSON.stringify(r.value));try{await p(M,{sourceLanguage:c==null?void 0:c.code,targetLanguage:f.code,fields:b.value,translatableStructureFields:h.value,translatableObjectFields:x.value,translatableBlocks:y.value})}catch(T){console.error(T),t.notification.error(t.t("error"));return}for(const[T,I]of Object.entries(M))e.dispatch("content/update",[T,I]);if(w.value){const{text:T}=await t.api.post(A,{sourceLanguage:c==null?void 0:c.code,targetLanguage:f.code,text:t.view.title});await t.api.patch(`${t.view.path}/title`,{title:T}),await t.view.reload()}else t.view.isLoading=!1;t.notification.success(t.t("johannschopplich.content-translator.notification.translated"))}async function C(){F.value=await R(v)}function R(f){return t.api.get(t.view.path,{language:f.code})}function et(f,c){if(!k.value){c==null||c();return}t.dialog.open({component:"k-text-dialog",props:{text:f},on:{submit:()=>{t.dialog.close(),c==null||c()}}})}return{__sfc:!0,propsDefinition:P,props:o,panel:t,store:e,recursiveTranslateContent:p,label:u,confirm:k,syncableFields:m,translatableFields:d,translatableStructureFields:h,translatableObjectFields:x,translatableBlocks:y,translateTitle:w,fields:b,config:S,defaultLanguageData:F,defaultLanguage:v,currentContent:a,translatableContent:r,t:i,syncModelContent:l,translateModelContent:_,updateModelDefaultLanguageData:C,getModelData:R,openModal:et}}});var W=function(){var p;var o=this,t=o._self._c,e=o._self._setupProxy;return e.config?t("k-section",{attrs:{label:e.label}},[e.panel.multilang?!e.config.translateFn&&!((p=e.config.DeepL)!=null&&p.apiKey)?t("k-box",{attrs:{theme:"empty"}},[t("k-text",[o._v(" You need to set the either a custom "),t("code",[o._v("translateFn")]),o._v(" or the "),t("code",[o._v("DeepL.apiKey")]),o._v(" option for the "),t("code",[o._v("johannschopplich.content-translator")]),o._v(" namespace in your Kirby configuration. ")])],1):e.translatableFields.length?e.config.allowDefaultLanguageOverwrite?t("k-box",{attrs:{theme:"none"}},[t("k-button-group",{attrs:{layout:"collapsed"}},[o._l(e.panel.languages.filter(u=>u.code!==e.panel.language.code),function(u){return t("k-button",{directives:[{name:"show",rawName:"v-show",value:e.syncableFields.length,expression:"syncableFields.length"}],key:u.code,attrs:{size:"sm",variant:"filled"},on:{click:function(k){e.openModal(e.panel.t("johannschopplich.content-translator.dialog.syncFrom",{language:u.name}),()=>e.syncModelContent(u))}}},[o._v(" "+o._s(e.panel.t("johannschopplich.content-translator.importFrom",{language:u.code.toUpperCase()}))+" ")])}),t("k-button",{attrs:{icon:"translate",size:"sm",variant:"filled",theme:"notice"},on:{click:function(u){e.openModal(e.panel.t("johannschopplich.content-translator.dialog.translate",{language:e.panel.language.name}),()=>e.translateModelContent(e.panel.language))}}},[o._v(" "+o._s(e.panel.t("johannschopplich.content-translator.translate",{language:e.panel.language.code.toUpperCase()}))+" ")])],2)],1):[t("k-box",{attrs:{theme:"none"}},[t("k-button-group",{attrs:{layout:"collapsed"}},[t("k-button",{directives:[{name:"show",rawName:"v-show",value:e.syncableFields.length,expression:"syncableFields.length"}],attrs:{disabled:e.panel.language.default,size:"sm",variant:"filled"},on:{click:function(u){e.openModal(e.panel.t("johannschopplich.content-translator.dialog.sync",{language:e.defaultLanguage.name}),()=>e.syncModelContent())}}},[o._v(" "+o._s(e.panel.t("johannschopplich.content-translator.sync"))+" ")]),t("k-button",{attrs:{disabled:e.panel.language.default,icon:"translate",size:"sm",variant:"filled",theme:"notice"},on:{click:function(u){e.openModal(e.panel.t("johannschopplich.content-translator.dialog.translate",{language:e.panel.language.name}),()=>e.translateModelContent(e.panel.language,e.defaultLanguage))}}},[o._v(" "+o._s(e.panel.t("johannschopplich.content-translator.translate",{language:e.panel.language.code.toUpperCase()}))+" ")])],1)],1),t("k-box",{directives:[{name:"show",rawName:"v-show",value:e.panel.language.default,expression:"panel.language.default"}],staticClass:"kct-mt-1",attrs:{theme:"none",text:e.panel.t("johannschopplich.content-translator.help.disallowDefaultLanguage")}})]:t("k-box",{attrs:{theme:"info"}},[t("k-text",[o._v(" You have to define at least one translatable field for the "),t("code",[o._v("translatableFields")]),o._v(" blueprint or in your Kirby configuration. ")])],1):t("k-box",{attrs:{theme:"info"}},[t("k-text",[o._v(" This section requires multi-language support to be enabled. ")])],1)],2):o._e()},X=[],Z=L(Q,W,X);const tt=Z.exports;window.panel.plugin("johannschopplich/content-translator",{sections:{"content-translator":tt}})})();