UNPKG

@drincs/pixi-vn-json

Version:

Pixi'VN can be integrated with JSON files to create a visual novel.

1 lines 3.75 kB
'use strict';var core=require('@drincs/pixi-vn-json/core');var P=Object.defineProperty;var V=(c,e,i)=>e in c?P(c,e,{enumerable:true,configurable:true,writable:true,value:i}):c[e]=i;var m=(c,e,i)=>V(c,typeof e!="symbol"?e+"":e,i);function E(c,e={}){let i="";return c.values.forEach(n=>{if(typeof n=="string")i+=n;else {let s=core.JsonUnifier.getLogichValue(n,e);i+=`${s}`;}}),i}var t=class t{static t(e){return Array.isArray(e)?e.map(i=>t.translate(`${i}`)):t.translate(`${e}`)}static set beforeToTranslate(e){t._beforeToTranslate=e;}static set translate(e){t._translate=e;}static get translate(){return e=>{let i=e;return t._beforeToTranslate&&(i=t._beforeToTranslate(i)),i=t._translate(i),t._afterToTranslate&&(i=t._afterToTranslate(i)),i}}static set afterToTranslate(e){t._afterToTranslate=e;}static addKey(e,i,n){let s=n.defaultValue||"empty_string";typeof i=="string"&&(i=[i]),Array.isArray(i)&&i.forEach(l=>{e[l]===void 0&&(s==="empty_string"?e[l]="":s==="copy_key"&&(t._beforeToTranslate?e[l]=t._beforeToTranslate(l):e[l]=l));});}static getConditionalsThenElse(e,i=[]){if(typeof e=="object"&&e&&"type"in e)if(e.type==="ifelse")e.then&&t.getConditionalsThenElse(e.then,i),e.else&&t.getConditionalsThenElse(e.else,i);else if(e.type==="stepswitch"){if(e.elements)if(Array.isArray(e.elements))e.elements.forEach(n=>{t.getConditionalsThenElse(n,i);});else if(e.elements.type==="ifelse"){let n=[];t.getConditionalsThenElse(e.elements,n),n.forEach(s=>{i.push(...s);});}else if(e.elements.type==="stepswitch"){let n=[];t.getConditionalsThenElse(e.elements,n),n.forEach(s=>{i.push(...s);});}else t.getConditionalsThenElse(e.elements,i);}else e.type==="resulttocombine"?(e.firstItem&&t.getConditionalsThenElse(e.firstItem,i),e.secondConditionalItem&&e.secondConditionalItem.forEach(n=>{t.getConditionalsThenElse(n,i);})):i.push(e);else e&&i.push(e);return i}static async generateJsonTranslation(e,i={},n={}){let{defaultValue:s="copy_key",operationStringConvert:l}=n,x=e.map(async r=>{if(r.choices){let o=[];Array.isArray(r.choices)?o=[r.choices]:o=t.getConditionalsThenElse(r.choices),o.forEach(f=>f.forEach(a=>{if("type"in a){let g=[];a.type==="ifelse"||a.type==="stepswitch"?t.getConditionalsThenElse(a,g):g=[a],g.map(p=>t.getConditionalsThenElse(p.text)).forEach(p=>{Array.isArray(p)&&p.forEach(y=>{Array.isArray(y)?y.forEach(T=>{typeof T=="string"?t.addKey(i,T,{defaultValue:s}):t.getConditionalsThenElse(T).forEach(d=>{Array.isArray(d)?d.forEach(u=>{typeof u=="string"&&t.addKey(i,u,{defaultValue:s});}):typeof d=="string"&&t.addKey(i,d,{defaultValue:s});});}):typeof y=="string"&&t.addKey(i,y,{defaultValue:s});});});}}));}if(r.dialogue){let o=[];Array.isArray(r.dialogue)?o=[r.dialogue]:o=t.getConditionalsThenElse(r.dialogue),o.forEach(f=>{if(typeof f=="string")t.addKey(i,f,{defaultValue:s});else if("text"in f){let a=t.getConditionalsThenElse(f.text);typeof a=="string"&&(a=[a]),a.forEach(g=>{typeof g=="string"?t.addKey(i,g,{defaultValue:s}):t.getConditionalsThenElse(g).forEach(p=>{typeof p=="string"?t.addKey(i,p,{defaultValue:s}):Array.isArray(p)&&p.forEach(y=>{typeof y=="string"?t.addKey(i,y,{defaultValue:s}):t.getConditionalsThenElse(y).forEach(T=>{typeof T=="string"&&t.addKey(i,T,{defaultValue:s});});});});});}});}if(r.operations)for(let o of r.operations){if(o.type==="operationtoconvert"&&l){let f=E(o),a=await l(f,r,{});if(!a)return;o=a;}o.type==="text"&&o.operationType==="show"&&t.addKey(i,o.text,{defaultValue:s});}r.conditionalStep&&t.getConditionalsThenElse(r.conditionalStep).forEach(f=>{Array.isArray(f)?t.generateJsonTranslation(f,i,n):t.generateJsonTranslation([f],i,n);});});return await Promise.all(x),i}};m(t,"_beforeToTranslate"),m(t,"_translate",e=>e),m(t,"_afterToTranslate");var h=t;var L=h;exports.translator=L;