支持指定路径保存关卡数据包并移除旧中台导出
This commit is contained in:
+36
-13
@@ -16,7 +16,7 @@
|
||||
helpButton.onclick = () => { const help = $('help-dialog'); if (!help.open) help.showModal(); };
|
||||
const exportDialog = document.createElement('dialog'); exportDialog.id = 'project-export-dialog'; exportDialog.setAttribute('aria-labelledby', 'project-export-title');
|
||||
exportDialog.innerHTML = '<form method="dialog"><div class="mainline-heading"><h2 id="project-export-title">导出项目 JSON</h2><button value="cancel" title="关闭" aria-label="关闭"><i data-lucide="x"></i></button></div>' +
|
||||
'<div class="mainline-row"><button id="project-export-changes" value="changes"><i data-lucide="file-diff"></i>导出变更 JSON</button><button id="project-export-all" class="primary" value="all"><i data-lucide="files"></i>导出全部 JSON</button><button id="project-export-staging" value="staging"><i data-lucide="folder-down"></i>导出中台测试包</button></div></form>';
|
||||
'<div class="mainline-row"><button id="project-export-changes" value="changes"><i data-lucide="file-diff"></i>导出变更 JSON</button><button id="project-export-all" class="primary" value="all"><i data-lucide="files"></i>导出全部 JSON</button></div></form>';
|
||||
document.body.append(exportDialog);
|
||||
const input = document.createElement('input'); input.type = 'file'; input.accept = '.json'; input.hidden = true; document.body.append(input);
|
||||
const dialog = document.createElement('dialog'); dialog.id = 'mainline-dialog'; dialog.setAttribute('aria-labelledby', 'mainline-title');
|
||||
@@ -181,20 +181,44 @@
|
||||
} catch (error) { $('mainline-error').textContent = error.message; }
|
||||
finally { setSubmitting(false); setBusy(false); }
|
||||
};
|
||||
function download(bundle) {
|
||||
const url = URL.createObjectURL(new Blob([JSON.stringify(bundle, null, 2)], { type: 'application/json' }));
|
||||
const link = document.createElement('a'); link.href = url; link.download = '主线关卡数据包.json'; link.click(); setTimeout(() => URL.revokeObjectURL(url), 1000);
|
||||
}
|
||||
exportButton.onclick = guarded(async () => {
|
||||
exportButton.onclick = async () => {
|
||||
if (state.busy || !state.level) return;
|
||||
setBusy(true);
|
||||
try { const result = await snapshot(); download(result.package); message('已下载完整数据包 JSON,包含主线顺序、新增关卡和当前布局;可用“导入完整数据包 JSON”直接导入'); }
|
||||
finally { setBusy(false); }
|
||||
});
|
||||
const name = '主线关卡数据包.json';
|
||||
try {
|
||||
let handle;
|
||||
// 保存窗口必须在点击事件内打开,不能等异步校验结束后再请求权限。
|
||||
if (typeof window.showSaveFilePicker === 'function') {
|
||||
try {
|
||||
handle = await window.showSaveFilePicker({ id: 'mainline-package', suggestedName: name,
|
||||
types: [{ description: '关卡数据包 JSON', accept: { 'application/json': ['.json'] } }] });
|
||||
} catch (error) {
|
||||
if (error.name === 'AbortError') return;
|
||||
if (error.name !== 'SecurityError') throw error;
|
||||
}
|
||||
}
|
||||
if (!handle && !window.confirm('当前环境无法打开保存窗口。是否改用浏览器下载?同名文件可能会另存为副本。')) return;
|
||||
const result = await snapshot();
|
||||
const blob = new Blob([JSON.stringify(result.package, null, 2)], { type: 'application/json' });
|
||||
if (handle) {
|
||||
const stream = await handle.createWritable();
|
||||
try { await stream.write(blob); await stream.close(); }
|
||||
catch (error) { await stream.abort().catch(() => {}); throw error; }
|
||||
message('已保存完整数据包:' + handle.name);
|
||||
} else {
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement('a'); link.href = url; link.download = name; link.click();
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
||||
message('已下载完整数据包 JSON');
|
||||
}
|
||||
} catch (error) {
|
||||
message('导出数据包失败:' + (error.name === 'NotAllowedError' ? '没有文件写入权限' : error.message), true);
|
||||
} finally { setBusy(false); }
|
||||
};
|
||||
projectExport.onclick = () => { if (!state.busy && state.level) { exportDialog.returnValue = 'cancel'; exportDialog.showModal(); } };
|
||||
exportDialog.addEventListener('close', guarded(async () => {
|
||||
const mode = exportDialog.returnValue;
|
||||
if (!['changes', 'all', 'staging'].includes(mode)) return;
|
||||
if (!['changes', 'all'].includes(mode)) return;
|
||||
if (state.busy || !state.level) return;
|
||||
setBusy(true); projectExport.disabled = true;
|
||||
try {
|
||||
@@ -204,10 +228,9 @@
|
||||
const result = await ProjectExport.create(prepared, mode);
|
||||
const url = URL.createObjectURL(new Blob([result.bytes], { type: 'application/zip' }));
|
||||
const link = document.createElement('a'); link.href = url;
|
||||
link.download = mode === 'staging' ? '中台关卡测试包-' + result.revision + '.zip'
|
||||
: '关卡项目文件包-' + (mode === 'all' ? '全部-' : '变更-') + new Date().toISOString().replace(/[:.]/g, '-') + '.zip'; link.click();
|
||||
link.download = '关卡项目文件包-' + (mode === 'all' ? '全部-' : '变更-') + new Date().toISOString().replace(/[:.]/g, '-') + '.zip'; link.click();
|
||||
setTimeout(() => URL.revokeObjectURL(url), 60000);
|
||||
message(mode === 'staging' ? '已下载中台关卡测试包' : '已下载' + (mode === 'all' ? '全部' : '变更') + '项目 JSON 包,共 ' + result.manifest.files.length + ' 个文件;请在浏览器下载记录中查看,覆盖前先备份');
|
||||
message('已下载' + (mode === 'all' ? '全部' : '变更') + '项目 JSON 包,共 ' + result.manifest.files.length + ' 个文件;请在浏览器下载记录中查看,覆盖前先备份');
|
||||
} finally { projectExport.disabled = false; setBusy(false); }
|
||||
}));
|
||||
importButton.onclick = () => { if (!state.busy) input.click(); };
|
||||
|
||||
Reference in New Issue
Block a user