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

@@ -191,3 +191,132 @@ test('deleteSectionMessage: items, singular forms, and text sections', () => {
addSequence(d, t);
assert.match(deleteSectionMessage(t), /\(1 visual sequence, its text\)/);
});
// ---- sub-tabs ----
import { hasTabs, tabsOf, entryTab, validTab, entriesInTab, siblingIndex, moveWithin, addSubsection, moveSubsection, renameTab, moveToTab, removeSubsection, deleteSubsectionMessage, printGroups } from '../src/ops.js';
function bank() {
const doc = emptyDocument('Me');
const s = addSection(doc, 'records', 'Banking');
const a = addRecord(doc, s); a.title = 'A';
const b = addRecord(doc, s); b.title = 'B';
const c = addRecord(doc, s); c.title = 'C';
return { doc, s, a, b, c };
}
test('a section has no tabs until one is added; then General comes first', () => {
const { doc, s } = bank();
assert.equal(hasTabs(s), false);
assert.deepEqual(tabsOf(s), []);
const t = addSubsection(doc, s, ' Online ');
assert.equal(t.title, 'Online');
assert.deepEqual(tabsOf(s), [{ id: '', title: 'General' }, { id: t.id, title: 'Online' }]);
assert.deepEqual(validate(doc), []);
});
test('records without a sub-tab, or with a dangling one, are in General', () => {
const { doc, s, a, b } = bank();
const t = addSubsection(doc, s, 'Online');
moveToTab(s, b, t.id);
assert.equal(entryTab(s, a), '');
assert.equal(entryTab(s, b), t.id);
b.subsection = 'gone';
assert.equal(entryTab(s, b), '');
assert.equal(validTab(s, 'gone'), '');
assert.equal(validTab(s, t.id), t.id);
});
test('moveToTab changes the tab, refuses an unknown one, and General clears it', () => {
const { doc, s, a } = bank();
const t = addSubsection(doc, s, 'Online');
assert.equal(moveToTab(s, a, t.id), true);
assert.equal(a.subsection, t.id);
assert.equal(moveToTab(s, a, 'nope'), false);
assert.equal(a.subsection, t.id);
assert.equal(moveToTab(s, a, ''), true);
assert.equal('subsection' in a, false);
assert.deepEqual(validate(doc), []);
});
test('up/down moves within a tab, stepping over records in other tabs', () => {
const { doc, s, a, b, c } = bank();
const t = addSubsection(doc, s, 'Online');
moveToTab(s, b, t.id); // list: A(General) B(Online) C(General)
const same = (e) => entryTab(s, e) === '';
assert.equal(siblingIndex(s.records, 0, 1, same), 2);
assert.equal(moveWithin(s.records, 0, 1, same), true);
assert.deepEqual(s.records.map((r) => r.title), ['C', 'B', 'A']);
assert.equal(moveWithin(s.records, 2, 1, same), false);
assert.deepEqual(entriesInTab(s, '').map((x) => x.e.title), ['C', 'A']);
assert.deepEqual(entriesInTab(s, t.id).map((x) => x.i), [1]);
});
test('new records and sequences go into the given tab', () => {
const { doc, s } = bank();
const t = addSubsection(doc, s, 'Online');
const r = addRecord(doc, s, t.id);
const q = addSequence(doc, s, t.id);
assert.equal(r.subsection, t.id);
assert.equal(q.subsection, t.id);
assert.equal('subsection' in addRecord(doc, s), false);
assert.deepEqual(validate(doc), []);
});
test('renameTab renames General or a tab, and refuses an empty name', () => {
const { doc, s } = bank();
const t = addSubsection(doc, s, 'Online');
assert.equal(renameTab(s, '', 'Everyday'), true);
assert.equal(renameTab(s, t.id, 'Web'), true);
assert.deepEqual(tabsOf(s).map((x) => x.title), ['Everyday', 'Web']);
assert.equal(renameTab(s, t.id, ' '), false);
assert.equal(renameTab(s, 'nope', 'x'), false);
assert.deepEqual(validate(doc), []);
});
test('moveSubsection reorders tabs', () => {
const { doc, s } = bank();
const x = addSubsection(doc, s, 'X');
const y = addSubsection(doc, s, 'Y');
assert.equal(moveSubsection(s, y.id, -1), true);
assert.deepEqual(s.subsections.map((t) => t.title), ['Y', 'X']);
assert.equal(moveSubsection(s, y.id, -1), false);
assert.equal(moveSubsection(s, 'nope', 1), false);
void x;
});
test('removing a tab sends its records to General; removing the last tab restores a plain section', () => {
const { doc, s, a, b } = bank();
const t = addSubsection(doc, s, 'Online');
renameTab(s, '', 'Everyday');
moveToTab(s, a, t.id);
assert.match(deleteSubsectionMessage(s, t.id), /Delete the sub-tab "Online"\? Its 1 entry moves to "Everyday"\./);
assert.equal(removeSubsection(s, t.id), true);
assert.equal('subsection' in a, false);
assert.equal(s.records.length, 3);
assert.equal(hasTabs(s), false);
assert.equal('subsections' in s, false);
assert.equal('generalTitle' in s, false);
assert.equal(removeSubsection(s, t.id), false);
void b;
assert.deepEqual(validate(doc), []);
});
test('printGroups: one untitled group without tabs, else one per non-empty tab', () => {
const { doc, s, a, b } = bank();
assert.equal(printGroups(s).length, 1);
assert.equal(printGroups(s)[0].title, null);
const t = addSubsection(doc, s, 'Online');
addSubsection(doc, s, 'Empty');
moveToTab(s, b, t.id);
const g = printGroups(s);
assert.deepEqual(g.map((x) => x.title), ['General', 'Online']);
assert.deepEqual(g[1].entries, [b]);
void a;
});
test('sub-tab ids are unique across the document', () => {
const { doc, s } = bank();
const t = addSubsection(doc, s, 'Online');
const ids = new Set([t.id, ...s.records.map((r) => r.id), s.id]);
assert.equal(ids.size, 5);
});