Add sub-tabs to records sections (Refs #18)
Records sections can be split into user-named sub-tabs with a General catch-all. Records and visual sequences move between tabs via a Sub-tab drop-down; up/down, collapse all and print work per tab. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
106
src/app.js
106
src/app.js
@@ -11,6 +11,7 @@
|
||||
dirty: false,
|
||||
baseline: null, // deep copy of the document at open/save; Cancel reverts to it
|
||||
pwChanged: false, // passphrase changed since the last save
|
||||
tabs: {}, // section id -> id of the sub-tab being shown ('' = General); not saved
|
||||
collapsed: new Set(), // ids of records folded to just their title (view mode only)
|
||||
revealed: new WeakSet(), // secret field objects currently shown
|
||||
blob: null, // encrypted payload taken from a file the user chose to open
|
||||
@@ -230,16 +231,32 @@
|
||||
return [h('dt', {}, f.label), h('dd', {}, value)];
|
||||
}
|
||||
|
||||
function viewRecord(r, arr, i) {
|
||||
const currentTab = (s) => validTab(s, state.tabs[s.id]);
|
||||
const sameTab = (s) => { const t = currentTab(s); return (e) => entryTab(s, e) === t; };
|
||||
|
||||
// The row of sub-tab buttons (nothing when the section has no sub-tabs).
|
||||
function tabBar(s) {
|
||||
const tabs = tabsOf(s);
|
||||
if (!tabs.length) return null;
|
||||
const cur = currentTab(s);
|
||||
return h('div', { class: 'tabs', role: 'tablist', 'aria-label': s.title + ' sub-tabs' }, tabs.map((t) => h('button',
|
||||
{ type: 'button', role: 'tab', class: 'tab' + (t.id === cur ? ' on' : ''), 'aria-selected': String(t.id === cur),
|
||||
onclick: () => { state.tabs[s.id] = t.id; render(); } },
|
||||
t.title, ' ', h('span', { class: 'muted small' }, '(' + entriesInTab(s, t.id).length + ')'))));
|
||||
}
|
||||
|
||||
function viewRecord(s, r, i) {
|
||||
const arr = s.records;
|
||||
const same = sameTab(s);
|
||||
const closed = state.collapsed.has(r.id);
|
||||
const toggle = () => { state.collapsed[closed ? 'delete' : 'add'](r.id); render(); };
|
||||
const shift = (dir) => { move(arr, i, dir); changed(r); render(); };
|
||||
const shift = (dir) => { moveWithin(arr, i, dir, same); changed(r); render(); };
|
||||
return h('article', { class: 'card' + (closed ? ' collapsed' : '') },
|
||||
h('div', { class: 'rechead' },
|
||||
h('h3', {}, r.title, ' ', statusBadge(r.status)),
|
||||
h('span', { class: 'tools' },
|
||||
closed && i > 0 ? btn('↑', () => shift(-1), 'icon', { 'aria-label': 'Move ' + r.title + ' up' }) : null,
|
||||
closed && i < arr.length - 1 ? btn('↓', () => shift(1), 'icon', { 'aria-label': 'Move ' + r.title + ' down' }) : null,
|
||||
closed && siblingIndex(arr, i, -1, same) >= 0 ? btn('↑', () => shift(-1), 'icon', { 'aria-label': 'Move ' + r.title + ' up' }) : null,
|
||||
closed && siblingIndex(arr, i, 1, same) >= 0 ? btn('↓', () => shift(1), 'icon', { 'aria-label': 'Move ' + r.title + ' down' }) : null,
|
||||
btn(closed ? 'Expand' : 'Collapse', toggle, 'small', { 'aria-expanded': String(!closed), 'aria-label': (closed ? 'Expand ' : 'Collapse ') + r.title }))),
|
||||
closed ? null : [
|
||||
r.fields.length ? h('dl', {}, r.fields.map(viewField)) : null,
|
||||
@@ -247,8 +264,8 @@
|
||||
h('p', { class: 'muted small' }, 'Updated ' + r.updated)]);
|
||||
}
|
||||
|
||||
function foldBar(s) {
|
||||
const recs = s.records.filter((r) => !isSequence(r));
|
||||
function foldBar(entries) {
|
||||
const recs = entries.filter((r) => !isSequence(r));
|
||||
if (!recs.length) return null;
|
||||
const setAll = (fold) => { recs.forEach((r) => state.collapsed[fold ? 'add' : 'delete'](r.id)); render(); };
|
||||
return h('div', { class: 'row foldbar' },
|
||||
@@ -285,11 +302,19 @@
|
||||
it.detail ? h('p', { class: 'notes' }, it.detail) : null);
|
||||
}
|
||||
|
||||
function viewRecords(s) {
|
||||
const entries = entriesInTab(s, currentTab(s));
|
||||
return [tabBar(s),
|
||||
hasTabs(s) && !entries.length ? h('p', { class: 'muted' }, 'Nothing in this sub-tab yet. Choose Edit to add or move records here.') : null,
|
||||
foldBar(entries.map((x) => x.e)),
|
||||
entries.map(({ e, i }) => (isSequence(e) ? viewSequence(e) : viewRecord(s, e, i)))];
|
||||
}
|
||||
|
||||
function viewSection(s) {
|
||||
return [
|
||||
s.intro ? h('p', { class: 'intro' }, s.intro) : null,
|
||||
s.kind === 'text' ? [h('p', { class: 'body' }, s.body), (s.sequences || []).map(viewSequence)] : null,
|
||||
s.kind === 'records' ? [foldBar(s), s.records.map((r, i) => (isSequence(r) ? viewSequence(r) : viewRecord(r, s.records, i)))] : null,
|
||||
s.kind === 'records' ? viewRecords(s) : null,
|
||||
s.kind === 'steps' ? h('ol', { class: 'items' }, s.items.map((i) => (isSequence(i) ? seqItem(i) : viewItem(i)))) : null,
|
||||
s.kind === 'checklist' ? h('ul', { class: 'items' }, s.items.map((i) => (isSequence(i) ? seqItem(i) : viewItem(i)))) : null,
|
||||
];
|
||||
@@ -309,12 +334,13 @@
|
||||
}
|
||||
// Up / down / delete controls for element i of arr.
|
||||
// `message` (string or function) replaces the default delete confirmation.
|
||||
function rowTools(arr, i, what, record, quiet = false, message = null) {
|
||||
// `same` (optional) limits up/down to elements it accepts, e.g. the ones in the same sub-tab.
|
||||
function rowTools(arr, i, what, record, quiet = false, message = null, same = () => true) {
|
||||
const note = () => (quiet ? markDirty() : changed(record));
|
||||
const act = (fn) => () => { fn(); note(); render(); };
|
||||
return h('span', { class: 'tools' },
|
||||
btn('↑', act(() => move(arr, i, -1)), 'icon', { 'aria-label': 'Move ' + what + ' up', disabled: i === 0 }),
|
||||
btn('↓', act(() => move(arr, i, 1)), 'icon', { 'aria-label': 'Move ' + what + ' down', disabled: i === arr.length - 1 }),
|
||||
btn('↑', act(() => moveWithin(arr, i, -1, same)), 'icon', { 'aria-label': 'Move ' + what + ' up', disabled: siblingIndex(arr, i, -1, same) < 0 }),
|
||||
btn('↓', act(() => moveWithin(arr, i, 1, same)), 'icon', { 'aria-label': 'Move ' + what + ' down', disabled: siblingIndex(arr, i, 1, same) < 0 }),
|
||||
btn('✕', () => { if (confirm(typeof message === 'function' ? message() : message || 'Delete this ' + what + '?')) { removeAt(arr, i); note(); render(); } }, 'icon danger', { 'aria-label': 'Delete ' + what }));
|
||||
}
|
||||
|
||||
@@ -356,13 +382,56 @@
|
||||
h('label', { class: 'check' }, secret, ' secret'), rowTools(r.fields, i, 'field', r));
|
||||
}
|
||||
|
||||
// "Sub-tab: [select]" for a record or sequence; changing it moves the entry to that tab.
|
||||
function moveToRow(s, e) {
|
||||
if (!hasTabs(s)) return null;
|
||||
const sel = h('select', { 'aria-label': 'Sub-tab of ' + e.title, onchange: () => {
|
||||
const t = tabsOf(s).find((x) => x.id === sel.value);
|
||||
moveToTab(s, e, sel.value); changed(e);
|
||||
render(); setStatus('Moved "' + e.title + '" to ' + t.title + '.');
|
||||
} }, tabsOf(s).map((t) => h('option', { value: t.id, selected: t.id === entryTab(s, e) }, t.title)));
|
||||
return h('div', { class: 'row' }, h('label', { class: 'check' }, 'Sub-tab ', sel));
|
||||
}
|
||||
|
||||
// Add, rename, reorder and delete the sub-tabs of a records section.
|
||||
function tabPanel(s) {
|
||||
const cur = currentTab(s);
|
||||
const name = h('input', { placeholder: 'New sub-tab name (e.g. Online)', 'aria-label': 'New sub-tab name',
|
||||
onkeydown: (e) => { if (e.key === 'Enter') { e.preventDefault(); add(); } } });
|
||||
const add = () => {
|
||||
if (!name.value.trim()) return name.focus();
|
||||
state.tabs[s.id] = addSubsection(state.doc, s, name.value).id;
|
||||
markDirty(); render();
|
||||
};
|
||||
let manage = null;
|
||||
if (hasTabs(s)) {
|
||||
const tab = tabsOf(s).find((t) => t.id === cur);
|
||||
const idx = s.subsections.findIndex((t) => t.id === cur);
|
||||
const rename = h('input', { value: tab.title, 'aria-label': 'Name of the selected sub-tab',
|
||||
onchange: () => { if (renameTab(s, cur, rename.value)) { changed(); render(); } else rename.value = tab.title; } });
|
||||
const shift = (dir) => () => { moveSubsection(s, cur, dir); changed(); render(); };
|
||||
manage = h('div', { class: 'row' }, rename, cur ? h('span', { class: 'tools' },
|
||||
btn('←', shift(-1), 'icon', { 'aria-label': 'Move sub-tab left', disabled: idx <= 0 }),
|
||||
btn('→', shift(1), 'icon', { 'aria-label': 'Move sub-tab right', disabled: idx >= s.subsections.length - 1 }),
|
||||
btn('✕', () => {
|
||||
if (!confirm(deleteSubsectionMessage(s, cur))) return;
|
||||
removeSubsection(s, cur); state.tabs[s.id] = ''; changed(); render();
|
||||
}, 'icon danger', { 'aria-label': 'Delete sub-tab' })) : h('span', { class: 'muted small' }, 'Catch-all for records not placed in a sub-tab'));
|
||||
}
|
||||
return h('div', { class: 'card editing' },
|
||||
h('strong', {}, 'Sub-tabs'),
|
||||
h('div', { class: 'row' }, name, btn('+ Add sub-tab', add, 'small')),
|
||||
manage);
|
||||
}
|
||||
|
||||
function editRecord(s, r, i) {
|
||||
const picker = h('input', { type: 'file', multiple: true, accept: 'image/png,image/jpeg', hidden: true, 'aria-label': 'Choose images to add',
|
||||
onchange: () => { importFiles(r, [...picker.files]); picker.value = ''; } });
|
||||
return h('article', { class: 'card editing', 'data-rec': r.id,
|
||||
ondragover: (e) => { if (hasFiles(e)) e.preventDefault(); },
|
||||
ondrop: (e) => { e.preventDefault(); importFiles(r, [...e.dataTransfer.files]); } },
|
||||
h('div', { class: 'row' }, input(r, 'title', { label: 'Record title' }, r), select(r, 'status', ['ok', 'tbd'], 'Record status', r), rowTools(s.records, i, 'record')),
|
||||
h('div', { class: 'row' }, input(r, 'title', { label: 'Record title' }, r), select(r, 'status', ['ok', 'tbd'], 'Record status', r), rowTools(s.records, i, 'record', undefined, false, null, sameTab(s))),
|
||||
moveToRow(s, r),
|
||||
r.fields.map((f, k) => editField(r, f, k)),
|
||||
h('div', { class: 'row' },
|
||||
btn('+ Add field', () => { addField(r); changed(r); render(); }, 'small'),
|
||||
@@ -384,19 +453,20 @@
|
||||
f.layout === 'none' ? null : h('label', {}, 'Text', input(f, 'text', { multiline: true, label: 'Text for step ' + (k + 1) }, q)));
|
||||
}
|
||||
|
||||
function editSequence(list, q, i) {
|
||||
function editSequence(list, q, i, s) {
|
||||
const picker = h('input', { type: 'file', multiple: true, accept: 'image/png,image/jpeg', hidden: true, 'aria-label': 'Choose images for new steps',
|
||||
onchange: () => { importFiles(q, [...picker.files], { add: addFrameFrom(q) }); picker.value = ''; } });
|
||||
return h('article', { class: 'card editing seq', 'data-seq': q.id,
|
||||
ondragover: (e) => { if (hasFiles(e)) e.preventDefault(); },
|
||||
ondrop: (e) => { e.preventDefault(); importFiles(q, [...e.dataTransfer.files], { add: addFrameFrom(q) }); } },
|
||||
h('div', { class: 'row' }, input(q, 'title', { label: 'Visual sequence title' }, q), rowTools(list, i, 'visual sequence', q)),
|
||||
h('div', { class: 'row' }, input(q, 'title', { label: 'Visual sequence title' }, q), rowTools(list, i, 'visual sequence', q, false, null, s ? sameTab(s) : undefined)),
|
||||
s ? moveToRow(s, q) : null,
|
||||
q.frames.map((f, k) => editFrame(q, f, k)),
|
||||
h('div', { class: 'row' },
|
||||
btn('+ Add step', () => { addFrame(q); changed(q); render(); }, 'small'),
|
||||
btn('+ Add images\u2026', () => picker.click(), 'small'), picker));
|
||||
}
|
||||
const addSeqBtn = (s) => btn('+ Add visual sequence', () => { addSequence(state.doc, s); markDirty(); render(); }, 'primary');
|
||||
const addSeqBtn = (s) => btn('+ Add visual sequence', () => { addSequence(state.doc, s, s.kind === 'records' ? currentTab(s) : ''); markDirty(); render(); }, 'primary');
|
||||
|
||||
function editItem(s, it, i) {
|
||||
return h('li', { class: 'card editing' },
|
||||
@@ -412,8 +482,9 @@
|
||||
h('label', {}, 'Intro', input(s, 'intro', { multiline: true, label: 'Section intro' }))),
|
||||
s.kind === 'text' ? [h('label', {}, 'Text', input(s, 'body', { multiline: true, label: 'Body' })),
|
||||
(s.sequences || []).map((q, i) => editSequence(s.sequences, q, i)), h('div', { class: 'row' }, addSeqBtn(s))] : null,
|
||||
s.kind === 'records' ? [s.records.map((r, i) => (isSequence(r) ? editSequence(s.records, r, i) : editRecord(s, r, i))),
|
||||
h('div', { class: 'row' }, btn('+ Add record', () => { addRecord(state.doc, s); markDirty(); render(); }, 'primary'), addSeqBtn(s))] : null,
|
||||
s.kind === 'records' ? [tabPanel(s), tabBar(s),
|
||||
entriesInTab(s, currentTab(s)).map(({ e, i }) => (isSequence(e) ? editSequence(s.records, e, i, s) : editRecord(s, e, i))),
|
||||
h('div', { class: 'row' }, btn('+ Add record', () => { addRecord(state.doc, s, currentTab(s)); markDirty(); render(); }, 'primary'), addSeqBtn(s))] : null,
|
||||
s.kind === 'steps' || s.kind === 'checklist' ? [h('ol', { class: 'items' }, s.items.map((it, i) => (isSequence(it)
|
||||
? h('li', { class: 'seqitem' }, editSequence(s.items, it, i)) : editItem(s, it, i)))),
|
||||
h('div', { class: 'row' }, btn('+ Add item', () => { addItem(state.doc, s); markDirty(); render(); }, 'primary'), addSeqBtn(s))] : null,
|
||||
@@ -518,7 +589,8 @@
|
||||
h('h2', {}, s.title),
|
||||
s.intro ? h('p', { class: 'pintro' }, s.intro) : null,
|
||||
s.kind === 'text' ? [h('p', { class: 'pbody' }, s.body), (s.sequences || []).map((q) => printSequence(q, includeSecrets))] : null,
|
||||
s.kind === 'records' ? s.records.map((r) => (isSequence(r) ? printSequence(r, includeSecrets) : printRecord(r, includeSecrets))) : null,
|
||||
s.kind === 'records' ? printGroups(s).map((g) => [g.title == null ? null : h('h3', { class: 'psubhead' }, g.title),
|
||||
g.entries.map((r) => (isSequence(r) ? printSequence(r, includeSecrets) : printRecord(r, includeSecrets)))]) : null,
|
||||
s.kind === 'steps' ? h('ol', { class: 'pitems' }, s.items.map((i) => (isSequence(i) ? h('li', { class: 'pseqitem' }, printSequence(i, includeSecrets)) : printItem(i)))) : null,
|
||||
s.kind === 'checklist' ? h('ul', { class: 'pitems' }, s.items.map((i) => (isSequence(i) ? h('li', { class: 'pseqitem' }, printSequence(i, includeSecrets)) : printItem(i)))) : null)),
|
||||
].flat();
|
||||
|
||||
Reference in New Issue
Block a user