feat: Add import/export buttons
This commit is contained in:
@@ -598,6 +598,11 @@
|
||||
.btn-save:hover { background: var(--gold-dim); color: var(--text-bright); }
|
||||
.btn-load { border-color: var(--border-bright); color: var(--text-dim); }
|
||||
.btn-load:hover { border-color: var(--teal-dim); color: var(--teal); }
|
||||
.btn-export { border-color: var(--teal-dim); color: var(--teal); }
|
||||
.btn-export:hover { background: var(--teal-dim); color: var(--text-bright); }
|
||||
.btn-import { border-color: var(--border-bright); color: var(--text-dim); }
|
||||
.btn-import:hover { border-color: var(--gold-dim); color: var(--gold); }
|
||||
.toolbar-sep { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }
|
||||
|
||||
.save-status {
|
||||
font-family: var(--font-mono);
|
||||
@@ -662,6 +667,10 @@
|
||||
<div class="toolbar">
|
||||
<button class="btn-save" onclick="saveSheet()">✦ Save</button>
|
||||
<button class="btn-load" onclick="loadSheet()">↑ Load</button>
|
||||
<div class="toolbar-sep"></div>
|
||||
<button class="btn-save btn-export" onclick="exportSheet()">↓ Export JSON</button>
|
||||
<button class="btn-load btn-import" onclick="importSheet()">↑ Import JSON</button>
|
||||
<input type="file" id="importFileInput" accept=".json,application/json" style="display:none" onchange="handleImportFile(this)">
|
||||
<span class="save-status" id="saveStatus">Saved!</span>
|
||||
</div>
|
||||
</header>
|
||||
@@ -1349,6 +1358,45 @@ function applyData(d) {
|
||||
updateCrisis();
|
||||
}
|
||||
|
||||
// ── EXPORT / IMPORT JSON ───────────────────────────
|
||||
function exportSheet() {
|
||||
const data = collectData();
|
||||
const json = JSON.stringify(data, null, 2);
|
||||
const blob = new Blob([json], { type: 'application/json' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
const charName = (data.name || 'character').replace(/[^a-z0-9_\- ]/gi, '').trim() || 'character';
|
||||
a.href = url;
|
||||
a.download = charName + '-fabula-ultima.json';
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
function importSheet() {
|
||||
document.getElementById('importFileInput').value = '';
|
||||
document.getElementById('importFileInput').click();
|
||||
}
|
||||
|
||||
function handleImportFile(input) {
|
||||
const file = input.files[0];
|
||||
if (!file) return;
|
||||
const reader = new FileReader();
|
||||
reader.onload = e => {
|
||||
try {
|
||||
const data = JSON.parse(e.target.result);
|
||||
applyData(data);
|
||||
saveSheet();
|
||||
const st = document.getElementById('saveStatus');
|
||||
st.textContent = 'Imported!';
|
||||
st.classList.add('show');
|
||||
setTimeout(() => { st.classList.remove('show'); st.textContent = 'Saved!'; }, 2500);
|
||||
} catch (err) {
|
||||
alert('Could not import: invalid JSON file.');
|
||||
}
|
||||
};
|
||||
reader.readAsText(file);
|
||||
}
|
||||
|
||||
// Auto-save every 30s
|
||||
setInterval(saveSheet, 30000);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user