Add viewer/editor UI
Passphrase unlock (or open a plain JSON file while no payload is embedded), sectioned navigation, read-only cards with masked secret fields and a reveal toggle, and an edit mode with add/remove/reorder for sections, records, fields and items. Pure edit operations live in ops.js with tests. DOM is built with textContent only. Closes #4 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
69
src/ops.js
Normal file
69
src/ops.js
Normal file
@@ -0,0 +1,69 @@
|
||||
// Pure editing operations on the document model. No DOM; tested in Node.
|
||||
// build.js strips `export` when bundling.
|
||||
|
||||
const today = () => new Date().toISOString().slice(0, 10);
|
||||
|
||||
export function allIds(doc) {
|
||||
const ids = new Set();
|
||||
for (const s of doc.sections) {
|
||||
ids.add(s.id);
|
||||
(s.records || []).forEach((r) => ids.add(r.id));
|
||||
(s.items || []).forEach((i) => ids.add(i.id));
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
export function newId(doc, prefix) {
|
||||
const ids = allIds(doc);
|
||||
let n = ids.size + 1;
|
||||
while (ids.has(`${prefix}-${n}`)) n++;
|
||||
return `${prefix}-${n}`;
|
||||
}
|
||||
|
||||
// Move arr[i] one place up (dir -1) or down (+1). Returns true if it moved.
|
||||
export function move(arr, i, dir) {
|
||||
const j = i + dir;
|
||||
if (i < 0 || i >= arr.length || j < 0 || j >= arr.length) return false;
|
||||
[arr[i], arr[j]] = [arr[j], arr[i]];
|
||||
return true;
|
||||
}
|
||||
|
||||
export function removeAt(arr, i) {
|
||||
return i >= 0 && i < arr.length ? arr.splice(i, 1)[0] : undefined;
|
||||
}
|
||||
|
||||
export function touch(doc, record) {
|
||||
const d = today();
|
||||
doc.meta.lastUpdated = d;
|
||||
if (record) record.updated = d;
|
||||
}
|
||||
|
||||
export function addSection(doc, kind, title) {
|
||||
const s = { id: newId(doc, 'sec'), title, kind };
|
||||
if (kind === 'text') s.body = '';
|
||||
else if (kind === 'records') s.records = [];
|
||||
else s.items = [];
|
||||
doc.sections.push(s);
|
||||
touch(doc);
|
||||
return s;
|
||||
}
|
||||
|
||||
export function addRecord(doc, section) {
|
||||
const r = { id: newId(doc, 'rec'), title: 'New record', status: 'tbd', updated: today(), fields: [] };
|
||||
section.records.push(r);
|
||||
touch(doc, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
export function addField(record) {
|
||||
const f = { label: 'Label', value: '' };
|
||||
record.fields.push(f);
|
||||
return f;
|
||||
}
|
||||
|
||||
export function addItem(doc, section) {
|
||||
const it = { id: newId(doc, 'item'), text: 'New item', status: 'open' };
|
||||
section.items.push(it);
|
||||
touch(doc);
|
||||
return it;
|
||||
}
|
||||
Reference in New Issue
Block a user