mirror of
https://github.com/aculix/negotium.git
synced 2026-09-11 07:28:17 +00:00
feat: installable offline app, plus export and import
Two things that were deliberately left out of 2.0.0. Export writes every stored day to a JSON file. Import reads one back, and only ever adds: a task whose id is already there is left alone, so importing the same file twice does nothing and importing into a list you're using can't lose work. That is also why it needs no confirmation dialog. The trade is that import restores rather than reverts. Both sit in a quiet line under the task list rather than the header, since they get used about twice a year and the header is what you look at all day. For the PWA half, the service worker is about fifty lines with no dependency, because the strategy falls out of how Vite builds. Documents go network first and fall back to cache, so a deploy is picked up as soon as you're online and nobody ends up stuck on an old build. Fingerprinted assets go cache first and are kept, since their names change when their contents do. No build-time asset manifest needed. Icons are SVG in the manifest, which stays sharp at any size and costs about a kilobyte, plus one 180px PNG because iOS wants a raster apple-touch-icon. Two theme-color metas so the phone status bar follows the theme, and safe-area padding on the header, without which the header sits under the clock once installed on an iPhone. Verified against the production build: worker registers and claims the page, shell and assets land in cache, and with the server stopped the app still loads, adds a task and persists it.
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
import * as taskOps from './lib/tasks.js';
|
||||
import { createUndoStack, applyUndo } from './lib/undo.js';
|
||||
import { shouldHandleUndo } from './lib/shortcuts.js';
|
||||
import { buildExport, serialize, parseImport, mergeImport } from './lib/backup.js';
|
||||
|
||||
const storage = createStorage();
|
||||
const undoStack = createUndoStack();
|
||||
@@ -83,6 +84,63 @@
|
||||
darkMode = !darkMode;
|
||||
}
|
||||
|
||||
let fileInput;
|
||||
let status = $state('');
|
||||
let statusIsError = $state(false);
|
||||
let statusTimer = null;
|
||||
|
||||
function showStatus(message, isError = false) {
|
||||
status = message;
|
||||
statusIsError = isError;
|
||||
clearTimeout(statusTimer);
|
||||
statusTimer = setTimeout(() => { status = ''; }, STATUS_MS);
|
||||
}
|
||||
|
||||
function exportTasks() {
|
||||
const text = serialize(buildExport(storage));
|
||||
const url = URL.createObjectURL(new Blob([text], { type: 'application/json' }));
|
||||
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.download = `negotium-${todayKey}.json`;
|
||||
link.click();
|
||||
URL.revokeObjectURL(url);
|
||||
|
||||
showStatus('Exported.');
|
||||
}
|
||||
|
||||
async function importTasks(event) {
|
||||
const file = event.target.files?.[0];
|
||||
// Reset first, so picking the same file twice still fires a change event.
|
||||
event.target.value = '';
|
||||
if (!file) return;
|
||||
|
||||
let parsed;
|
||||
try {
|
||||
parsed = parseImport(await file.text());
|
||||
} catch {
|
||||
showStatus("That file couldn't be read.", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!parsed.ok) {
|
||||
showStatus(parsed.error, true);
|
||||
return;
|
||||
}
|
||||
|
||||
const { imported, duplicates, days } = mergeImport(storage, parsed);
|
||||
tasks = storage.loadTasks(selectedKey);
|
||||
|
||||
if (imported === 0) {
|
||||
showStatus(duplicates > 0 ? 'Already up to date.' : 'Nothing to import.');
|
||||
return;
|
||||
}
|
||||
|
||||
const taskWord = imported === 1 ? 'task' : 'tasks';
|
||||
const dayWord = days === 1 ? 'day' : 'days';
|
||||
showStatus(`Imported ${imported} ${taskWord} across ${days} ${dayWord}.`);
|
||||
}
|
||||
|
||||
/** Scoped to the input. Previously this also sat on window, so Enter while a
|
||||
* task was focused would toggle that task *and* add whatever was in the
|
||||
* input. */
|
||||
@@ -126,6 +184,7 @@
|
||||
const DRAG_THRESHOLD_PX = 8;
|
||||
const LONG_PRESS_MS = 400;
|
||||
const SETTLE_MS = 240;
|
||||
const STATUS_MS = 4000;
|
||||
|
||||
let drag = null;
|
||||
|
||||
@@ -534,6 +593,22 @@
|
||||
{/if}
|
||||
{/key}
|
||||
</div>
|
||||
|
||||
<footer class="data-footer">
|
||||
<button class="data-link" onclick={exportTasks}>Export</button>
|
||||
<span class="data-sep" aria-hidden="true">·</span>
|
||||
<button class="data-link" onclick={() => fileInput.click()}>Import</button>
|
||||
<input
|
||||
bind:this={fileInput}
|
||||
type="file"
|
||||
accept="application/json,.json"
|
||||
class="visually-hidden"
|
||||
onchange={importTasks}
|
||||
/>
|
||||
<span class="data-status" class:error={statusIsError} role="status" aria-live="polite">
|
||||
{status}
|
||||
</span>
|
||||
</footer>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user