Files
nixingnishang_level-editor/抓猪关卡转换/piggy-converter/转换工具.html
T

38 lines
3.8 KiB
HTML

<!doctype html>
<html lang="zh-CN"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>关卡 JSON 转换</title>
<style>
*{box-sizing:border-box}body{font:14px system-ui,sans-serif;color:#243238;background:#f5f7f8;margin:0}main{max-width:900px;margin:32px auto;padding:24px}h1{font-size:24px}section{padding:20px 0;border-top:1px solid #d3dce0}label{display:block;margin:12px 0}select,input,button{font:inherit;max-width:100%}select,button{padding:8px 12px;border:1px solid #aabcc2;border-radius:4px;background:white}button{cursor:pointer}button:disabled{opacity:.4;cursor:default}pre{white-space:pre-wrap;overflow-wrap:anywhere;line-height:1.6}textarea{width:100%;height:140px;font:13px monospace;border:1px solid #aabcc2;border-radius:4px;padding:12px}#download{background:#087d83;color:white}small{color:#586971}footer{padding-top:16px}a{color:#087d83}
</style>
<main><h1>关卡 JSON 转换</h1><section>
<label>源关卡 JSON <input id="files" type="file" accept=".json,application/json" multiple></label>
<label>待查看文件 <select id="selected"></select></label>
<label>源角度 0° 朝向 <select id="zero"><option value="north">上</option><option value="east">右</option><option value="south">下</option><option value="west">左</option></select></label>
<label>对象映射 JSON <textarea id="mapping" spellcheck="false" aria-label="对象映射 JSON"></textarea></label>
<button id="convert">转换</button> <button id="download" disabled>下载当前单关 JSON</button> <button id="report" disabled>下载转换报告</button>
</section><section><h2>转换结果</h2><pre id="result" role="status">未选择文件</pre></section><footer><a href="使用说明.md" target="_blank">使用说明</a></footer></main>
<script src="converter.cjs"></script><script>
'use strict';
const $=id=>document.getElementById(id);let results=[],generation=0;
$('mapping').value=JSON.stringify(PiggyConverter.defaults,null,2);
function invalidate(){generation++;results=[];$('download').disabled=$('report').disabled=true;$('result').textContent='请点击转换';}
for(const id of ['files','zero','mapping'])$(id).addEventListener('change',invalidate);
function show(){const item=results[$('selected').value];$('download').disabled=!item?.ok;$('result').textContent=item?JSON.stringify(item,null,2):'没有可查看结果';}
$('selected').onchange=show;
$('convert').onclick=async()=>{
const current=++generation;$('convert').disabled=true;$('download').disabled=$('report').disabled=true;results=[];
try{
const mapping=JSON.parse($('mapping').value),zero=$('zero').value,files=Array.from($('files').files);
if(!files.length)throw Error('请选择关卡 JSON');
const converted=[];
for(const file of files){
let item;try{if(file.size>2*1024*1024)throw Error('文件不能超过 2MB');item=PiggyConverter.convert(JSON.parse((await file.text()).replace(/^\uFEFF/,'')),mapping,zero);}catch(error){item={ok:false,errors:['转换失败:'+error.message]};}
converted.push({file:file.name,...item});
}
if(current!==generation)return;results=converted;$('selected').replaceChildren(...results.map((item,index)=>new Option((item.ok?'通过 · ':'未转换 · ')+item.file,index)));$('report').disabled=false;show();
}catch(error){$('result').textContent='转换失败:'+error.message;}finally{$('convert').disabled=false;}
};
function download(name,data){const url=URL.createObjectURL(new Blob([JSON.stringify(data,null,2)],{type:'application/json'})),a=document.createElement('a');a.href=url;a.download=name;a.click();setTimeout(()=>URL.revokeObjectURL(url),1000);}
$('download').onclick=()=>{const item=results[$('selected').value];if(item?.ok)download('转换-'+item.file,item.layout);};
$('report').onclick=()=>download('转换报告.json',results.map(({layout,...report})=>report));
</script></html>