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:
2026-11-21 19:32:05 -05:00
parent 37bdb62baf
commit d7dc098e79
9 changed files with 411 additions and 24 deletions

View File

@@ -1,4 +1,4 @@
import { defaultHeaderFields, isSequence } from './model.js';
import { defaultHeaderFields, isSequence, GENERAL_TITLE } from './model.js';
// Pure editing operations on the document model. No DOM; tested in Node.
// build.js strips `export` when bundling.
@@ -9,6 +9,7 @@ export function allIds(doc) {
const ids = new Set();
for (const s of doc.sections) {
ids.add(s.id);
(s.subsections || []).forEach((t) => ids.add(t.id));
(s.records || []).forEach((r) => ids.add(r.id));
(s.items || []).forEach((i) => ids.add(i.id));
(s.sequences || []).forEach((q) => ids.add(q.id));
@@ -51,8 +52,9 @@ export function addSection(doc, kind, title) {
return s;
}
export function addRecord(doc, section) {
export function addRecord(doc, section, tabId = '') {
const r = { id: newId(doc, 'rec'), title: 'New record', status: 'tbd', updated: today(), fields: [] };
if (tabId) r.subsection = tabId;
section.records.push(r);
touch(doc, r);
return r;
@@ -185,8 +187,9 @@ export function sequenceList(section) {
return section.items;
}
export function addSequence(doc, section) {
export function addSequence(doc, section, tabId = '') {
const q = { id: newId(doc, 'seq'), kind: 'sequence', title: 'New visual sequence', updated: today(), frames: [] };
if (tabId && section.kind === 'records') q.subsection = tabId;
sequenceList(section).push(q);
touch(doc, q);
return q;
@@ -223,3 +226,99 @@ export function deleteSectionMessage(section) {
if (!parts.length) return 'Delete the empty section ' + name + '?';
return 'Delete the section ' + name + ' and everything in it (' + parts.join(', ') + ')? This cannot be undone with Cancel.';
}
// ---- sub-tabs of a records section ----
// A tab id of '' is the General catch-all; the rest are section.subsections ids.
export const hasTabs = (section) => !!(section.subsections && section.subsections.length);
// [{id, title}] in display order, General first; [] when the section has no sub-tabs.
export function tabsOf(section) {
if (!hasTabs(section)) return [];
return [{ id: '', title: section.generalTitle || GENERAL_TITLE }, ...section.subsections.map((t) => ({ id: t.id, title: t.title }))];
}
// Which tab an entry (record or sequence) is in. A missing or dangling id means General.
export function entryTab(section, e) {
return e.subsection && (section.subsections || []).some((t) => t.id === e.subsection) ? e.subsection : '';
}
// The tab to show: `wanted` if it still exists, else General.
export function validTab(section, wanted) {
return wanted && (section.subsections || []).some((t) => t.id === wanted) ? wanted : '';
}
// Entries of the section's list that are in a tab, as [{e, i}] (i = index in the list).
export function entriesInTab(section, tabId) {
const out = [];
section.records.forEach((e, i) => { if (entryTab(section, e) === tabId) out.push({ e, i }); });
return out;
}
// Index of the nearest element before (dir -1) / after (+1) arr[i] satisfying `same`, or -1.
export function siblingIndex(arr, i, dir, same = () => true) {
for (let j = i + dir; j >= 0 && j < arr.length; j += dir) if (same(arr[j])) return j;
return -1;
}
// Move arr[i] past its nearest neighbour for which `same` holds. Returns true if it moved.
export function moveWithin(arr, i, dir, same) {
const j = siblingIndex(arr, i, dir, same);
if (j < 0) return false;
[arr[i], arr[j]] = [arr[j], arr[i]];
return true;
}
export function addSubsection(doc, section, title) {
const t = { id: newId(doc, 'sub'), title: String(title).trim() };
(section.subsections || (section.subsections = [])).push(t);
touch(doc);
return t;
}
export function moveSubsection(section, id, dir) {
const i = (section.subsections || []).findIndex((t) => t.id === id);
return i < 0 ? false : move(section.subsections, i, dir);
}
// Rename a tab; '' is the General tab. Refuses an empty name.
export function renameTab(section, id, title) {
const name = String(title).trim();
if (!name) return false;
if (id === '') section.generalTitle = name;
else { const t = (section.subsections || []).find((x) => x.id === id); if (!t) return false; t.title = name; }
return true;
}
// Put a record or sequence in a tab ('' = General).
export function moveToTab(section, e, tabId) {
if (tabId && !(section.subsections || []).some((t) => t.id === tabId)) return false;
if (tabId) e.subsection = tabId; else delete e.subsection;
return true;
}
// Delete a tab. Its records and sequences fall back to General (nothing is lost).
// With no sub-tabs left, the section goes back to a plain list.
export function removeSubsection(section, id) {
const i = (section.subsections || []).findIndex((t) => t.id === id);
if (i < 0) return false;
for (const e of section.records) if (e.subsection === id) delete e.subsection;
section.subsections.splice(i, 1);
if (!section.subsections.length) { delete section.subsections; delete section.generalTitle; }
return true;
}
export function deleteSubsectionMessage(section, id) {
const t = section.subsections.find((x) => x.id === id);
const n = entriesInTab(section, id).length;
const where = '"' + (section.generalTitle || GENERAL_TITLE) + '"';
return 'Delete the sub-tab "' + t.title + '"?' + (n ? ' Its ' + n + (n === 1 ? ' entry moves' : ' entries move') + ' to ' + where + '.' : '');
}
// What to print for a records section: [{title, entries}]. Without sub-tabs one group with
// a null title; with them, one group per tab that has entries.
export function printGroups(section) {
if (!hasTabs(section)) return [{ title: null, entries: section.records }];
return tabsOf(section)
.map((t) => ({ title: t.title, entries: entriesInTab(section, t.id).map((x) => x.e) }))
.filter((g) => g.entries.length);
}