Add calibration backup and restore from file

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-07 08:59:18 +03:30
parent b979332084
commit 0046b49009
2 changed files with 60 additions and 0 deletions

View File

@@ -218,8 +218,10 @@
saved = structuredClone(state);
applyLive();
setStatus('Saved to server', 'ok');
return true;
} catch (e) {
setStatus('Save failed: ' + e.message, 'err');
return false;
} finally {
btn.disabled = false;
}
@@ -239,6 +241,50 @@
}
}
function backup() {
const blob = new Blob([JSON.stringify(payloadFromState(), null, 2) + '\n'], {
type: 'application/json',
});
const a = document.createElement('a');
const d = new Date();
const stamp = [
d.getFullYear(),
String(d.getMonth() + 1).padStart(2, '0'),
String(d.getDate()).padStart(2, '0'),
].join('');
a.href = URL.createObjectURL(blob);
a.download = `label-print-calibration-${stamp}.json`;
a.click();
URL.revokeObjectURL(a.href);
setStatus('Backup downloaded', 'ok');
}
function parseCalibrationFile(raw) {
const data = typeof raw === 'string' ? JSON.parse(raw) : raw;
if (!data || typeof data !== 'object') throw new Error('invalid file');
const required = ['top', 'left', 'font', 'bottom_top'];
for (const field of FIELDS) {
const v = data[field];
if (!v || typeof v !== 'object') throw new Error('missing ' + field);
for (const key of required) {
if (!Number.isFinite(Number(v[key]))) throw new Error(field + '.' + key);
}
}
return data;
}
async function restoreFromFile(file) {
try {
const data = parseCalibrationFile(await file.text());
if (!confirm('Replace current calibration with this file and save to server?')) return;
state = data;
applyLive();
if (await save()) setStatus('Restored from file', 'ok');
} catch (e) {
setStatus('Restore failed: ' + e.message, 'err');
}
}
document.querySelectorAll('.var-field').forEach((el) => {
el.addEventListener('click', () => {
const q = el.dataset.scope;
@@ -283,9 +329,20 @@
nudge(field, key, dir);
});
const restoreFileEl = document.getElementById('restore-file');
document.getElementById('btn-save').addEventListener('click', save);
document.getElementById('btn-reset').addEventListener('click', reset);
document.getElementById('btn-print').addEventListener('click', () => window.print());
document.getElementById('btn-backup').addEventListener('click', backup);
document.getElementById('btn-restore').addEventListener('click', () => {
restoreFileEl.value = '';
restoreFileEl.click();
});
restoreFileEl.addEventListener('change', () => {
const file = restoreFileEl.files && restoreFileEl.files[0];
if (file) restoreFromFile(file);
});
buildPanel();
applyLive();

View File

@@ -238,6 +238,9 @@
<div class="toolbar">
<button type="button" class="primary" id="btn-save">Save</button>
<button type="button" id="btn-print">Print</button>
<button type="button" id="btn-backup">Backup</button>
<button type="button" id="btn-restore">Restore</button>
<input type="file" id="restore-file" accept="application/json,.json" hidden>
<button type="button" class="danger" id="btn-reset">Reset defaults</button>
<a href="{% url 'entry' %}" style="align-self:center;color:#93c5fd;font-size:0.85rem;margin-left:0.25rem;">← Back</a>
</div>