diff --git a/slips/static/slips/js/calibrate.js b/slips/static/slips/js/calibrate.js index 00e992e..10818d5 100644 --- a/slips/static/slips/js/calibrate.js +++ b/slips/static/slips/js/calibrate.js @@ -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(); diff --git a/slips/templates/slips/calibrate.html b/slips/templates/slips/calibrate.html index b5b107b..4437868 100644 --- a/slips/templates/slips/calibrate.html +++ b/slips/templates/slips/calibrate.html @@ -238,6 +238,9 @@