From 6f57a8e7ed92d4174619adf50076b6b7fa7f84b8 Mon Sep 17 00:00:00 2001 From: Steffen Becker Date: Wed, 26 Aug 2026 09:58:33 +0000 Subject: [PATCH] feat: level-varied preset gallery for the list-style built-ins The uniform LibreOffice definitions duplicated the dropdown formats plus default nesting. The gallery now ships patterns the depth cycle cannot produce: the outline chains Outline I.A.1 / A.I.1, the kind-mixing Numbering with Bullets and Checklist, and the Diamond Bullets ladder, all on the plain 1.27cm step. STYLE_SHEET_VERSION 12 swaps stored built-ins while user styles survive. --- src/lib/styles/CLAUDE.md | 7 +++++-- src/lib/styles/listStyles.ts | 38 +++++++++++++++++++++-------------- src/lib/styles/styleSheet.ts | 2 +- tests/docx-roundtrip.test.ts | 8 ++++---- tests/lo-roundtrip.test.ts | 4 ++-- tests/roundtrip.test.ts | 12 +++++------ tests/unit/list-style.test.ts | 33 +++++++++++++++++++----------- 7 files changed, 62 insertions(+), 42 deletions(-) diff --git a/src/lib/styles/CLAUDE.md b/src/lib/styles/CLAUDE.md index b34f299..99313a6 100644 --- a/src/lib/styles/CLAUDE.md +++ b/src/lib/styles/CLAUDE.md @@ -17,8 +17,11 @@ plain lists) and both exporters share. **The style level decides the depth's kin ODF the list style alone says what a depth renders, the editor's node species doesn't travel. `setListStyle` retypes the subtree to the levels' kinds; a Tab-nested list of the other species still renders (and exports) what the level says, and both importers -build the species from the level, so a round trip normalizes it. The five built-ins are **probed LibreOffice -definitions** (List 1/2, Numbering 123/ABC/IVX; their margins are LO's own metric values). +build the species from the level, so a round trip normalizes it. The five built-ins are a **preset gallery of +patterns the depth cycle can't produce** — the outline chains Outline I.A.1 / A.I.1, the +kind-mixing Numbering with Bullets and Checklist, and the Diamond Bullets ladder — all on +the plain 1.27cm step (no indentCm), so a preset that only restates a dropdown format plus +the default nesting has no place in it. Export writes the full named `` into styles.xml and points the list at it **only when nothing overrides** — ODF list styles have no parent chain, so an overridden list keeps the fully resolved automatic clone and drops the name (marker formats count as diff --git a/src/lib/styles/listStyles.ts b/src/lib/styles/listStyles.ts index ddbd96d..639e747 100644 --- a/src/lib/styles/listStyles.ts +++ b/src/lib/styles/listStyles.ts @@ -115,21 +115,29 @@ export function builtinListStyles(): Record { return out; } -const levels = (n: number, make: (i: number) => ListLevelStyle): ListLevelStyle[] => - Array.from({ length: n }, (_, i) => make(i)); +const num = (numType: Exclude, markerAlign?: 'right'): ListLevelStyle => + markerAlign ? { kind: 'number', numType, markerAlign } : { kind: 'number', numType }; +const bul = (bulletChar: string): ListLevelStyle => ({ kind: 'bullet', bulletChar }); -// LibreOffice's own list styles (probed from a 25.2 save; margins are its round metric -// values — List 1/2 step 0.4/0.3cm, the numberings 1.33cm then 0.7cm per level, IVX -// 1.33cm throughout with a right-set label). indentCm = the step minus the 1.27 base. +// `head` levels first, then `tail` repeated until MAX_LIST_LEVELS are filled. +const ladder = (head: ListLevelStyle[], tail: ListLevelStyle[]): ListLevelStyle[] => + Array.from({ length: MAX_LIST_LEVELS }, (_, i) => + structuredClone(i < head.length ? head[i] : tail[(i - head.length) % tail.length])); + +// The gallery: presets the depth cycle can't produce on its own — uppercase outline +// chains, levels mixing numbers with bullets, a decorative bullet ladder. All keep the +// plain 1.27cm step per level (no indentCm), the geometry an unstyled list gets. const LIST_BUILTINS: ListStyle[] = [ - { name: 'List 1', builtin: true, - levels: levels(MAX_LIST_LEVELS, () => ({ kind: 'bullet', bulletChar: '•', indentCm: -0.87 })) }, - { name: 'List 2', builtin: true, - levels: levels(MAX_LIST_LEVELS, () => ({ kind: 'bullet', bulletChar: '–', indentCm: -0.97 })) }, - { name: 'Numbering 123', builtin: true, - levels: levels(MAX_LIST_LEVELS, (i) => ({ kind: 'number', numType: 'decimal', indentCm: i === 0 ? 0.06 : -0.57 })) }, - { name: 'Numbering ABC', builtin: true, - levels: levels(MAX_LIST_LEVELS, (i) => ({ kind: 'number', numType: 'upper-alpha', indentCm: i === 0 ? 0.06 : -0.57 })) }, - { name: 'Numbering IVX', builtin: true, - levels: levels(MAX_LIST_LEVELS, () => ({ kind: 'number', numType: 'upper-roman', markerAlign: 'right', indentCm: 0.06 })) }, + { name: 'Outline I.A.1', builtin: true, + levels: ladder([num('upper-roman', 'right'), num('upper-alpha')], + [num('decimal'), num('lower-alpha-paren'), num('lower-roman-paren')]) }, + { name: 'Outline A.I.1', builtin: true, + levels: ladder([num('upper-alpha'), num('upper-roman', 'right')], + [num('decimal'), num('lower-alpha-paren'), num('lower-roman-paren')]) }, + { name: 'Numbering with Bullets', builtin: true, + levels: ladder([num('decimal')], [bul('–'), bul('◦'), bul('▪')]) }, + { name: 'Diamond Bullets', builtin: true, + levels: ladder([], [bul('❖'), bul('➢'), bul('▪')]) }, + { name: 'Checklist', builtin: true, + levels: ladder([bul('✓')], [bul('–'), bul('◦')]) }, ]; diff --git a/src/lib/styles/styleSheet.ts b/src/lib/styles/styleSheet.ts index f4bc1a4..bb1da44 100644 --- a/src/lib/styles/styleSheet.ts +++ b/src/lib/styles/styleSheet.ts @@ -100,7 +100,7 @@ const CHAR_BUILTINS: Style[] = [ // Bumped whenever the built-in definitions change: a stored sheet from an older version // keeps its user styles but takes the new factory built-ins (see mergeStoredSheet). -export const STYLE_SHEET_VERSION = 11; +export const STYLE_SHEET_VERSION = 12; // A persisted sheet merged onto the current built-ins. Same version: stored entries win // (a document's own styles, and edits to built-ins). Older: only user styles survive. diff --git a/tests/docx-roundtrip.test.ts b/tests/docx-roundtrip.test.ts index bc4b3c6..bd7e477 100644 --- a/tests/docx-roundtrip.test.ts +++ b/tests/docx-roundtrip.test.ts @@ -1225,7 +1225,7 @@ describe('DOCX named list styles', () => { para('between'), // A second list in the same style: its own instance, so it restarts at 1. { type: 'orderedList', attrs: { listStyleName: 'Prüfliste' }, content: [li(para('restart'))] }, - { type: 'bulletList', attrs: { listStyleName: 'List 2' }, content: [li(para('dash'))] }, + { type: 'bulletList', attrs: { listStyleName: 'Diamond Bullets' }, content: [li(para('dash'))] }, ], }; @@ -1248,7 +1248,7 @@ describe('DOCX named list styles', () => { const style = styles.match(/[\s\S]*?<\/w:style>/)?.[0] ?? ''; expect(style).toContain(''); expect(//.test(style), 'placeholder numId resolved').toBe(true); - expect(styles).toContain('w:styleId="List2"'); + expect(styles).toContain('w:styleId="DiamondBullets"'); }); it('round-trips the assignment and the definition', async () => { @@ -1262,7 +1262,7 @@ describe('DOCX named list styles', () => { const nested = walk(lists[0], 'bulletList')[0]; expect(nested?.attrs ?? null, 'nested list stays attr-free').toBeNull(); expect(lists[1]?.attrs?.listStyleName).toBe('Prüfliste'); - expect(lists[2]?.attrs?.listStyleName).toBe('List 2'); + expect(lists[2]?.attrs?.listStyleName).toBe('Diamond Bullets'); const imported = result.styles.list['Prüfliste']; expect(imported?.levels[0]).toMatchObject({ kind: 'number', numType: 'upper-roman-paren', markerAlign: 'right', indentCm: 0.5 }); expect(imported?.levels[1]).toMatchObject({ kind: 'bullet', bulletChar: '✓' }); @@ -1301,7 +1301,7 @@ describe('DOCX named list styles', () => { it('an overridden list keeps a private, fully resolved numbering and drops the name', async () => { const doc = { type: 'doc', content: [ - { type: 'orderedList', attrs: { listStyleName: 'Numbering ABC', listStyleType: 'lower-roman' }, content: [li(para('broken out'))] }, + { type: 'orderedList', attrs: { listStyleName: 'Outline A.I.1', listStyleType: 'lower-roman' }, content: [li(para('broken out'))] }, ] }; const bytes = await buildDocx(doc as any, undefined, undefined, undefined, undefined, undefined, sheet); const numbering = strFromU8(unzipSync(bytes)['word/numbering.xml']); diff --git a/tests/lo-roundtrip.test.ts b/tests/lo-roundtrip.test.ts index def2126..cf80250 100644 --- a/tests/lo-roundtrip.test.ts +++ b/tests/lo-roundtrip.test.ts @@ -426,7 +426,7 @@ describe.skipIf(!SOFFICE)('LibreOffice round-trip (needs soffice on PATH)', () = LI(P(null, T('two'))), ] }, P(null, T('between')), - { type: 'bulletList', attrs: { listStyleName: 'List 2' }, content: [LI(P(null, T('dash')))] }, + { type: 'bulletList', attrs: { listStyleName: 'Diamond Bullets' }, content: [LI(P(null, T('dash')))] }, // A bullet tree under the same style: level 1 is a number level, so the style // decides the kind and LibreOffice must render (and keep) the numbering. { type: 'bulletList', attrs: { listStyleName: 'Prüfliste' }, content: [ @@ -450,7 +450,7 @@ describe.skipIf(!SOFFICE)('LibreOffice round-trip (needs soffice on PATH)', () = const res = importOdt(resaved); const lists = (res.content.content ?? []).filter((n: N) => n.type === 'orderedList' || n.type === 'bulletList'); check('LO lists: the assignment comes back', lists[0]?.attrs?.listStyleName === 'Prüfliste', lists[0]?.attrs); - check('LO lists: the built-in assignment comes back', lists[1]?.attrs?.listStyleName === 'List 2', lists[1]?.attrs); + check('LO lists: the built-in assignment comes back', lists[1]?.attrs?.listStyleName === 'Diamond Bullets', lists[1]?.attrs); check('LO lists: no per-level attrs accrete', !lists[0]?.attrs?.listStyleType && !lists[0]?.attrs?.indent && !lists[0]?.attrs?.markerAlign, lists[0]?.attrs); const imported = res.styles.list['Prüfliste']; diff --git a/tests/roundtrip.test.ts b/tests/roundtrip.test.ts index 056af20..1e3a688 100644 --- a/tests/roundtrip.test.ts +++ b/tests/roundtrip.test.ts @@ -1690,7 +1690,7 @@ describe('Leg 14: named list styles (ODF)', () => { LI(P(null, T('two'))), ] }, P(null, T('between')), - { type: 'bulletList', attrs: { listStyleName: 'List 2' }, content: [LI(P(null, T('dash')))] }, + { type: 'bulletList', attrs: { listStyleName: 'Diamond Bullets' }, content: [LI(P(null, T('dash')))] }, ] }; it('writes named text:list-style definitions and round-trips the assignment', async () => { @@ -1706,9 +1706,9 @@ describe('Leg 14: named list styles (ODF)', () => { check('level 2 is the bullet level', /text:level="2" text:bullet-char="✓"/.test(def), def); check('level 1 margin carries the extra step (1.77cm)', def.includes('fo:margin-left="1.770cm"'), def); check('all ten levels are written', (def.match(/text:level="/g) ?? []).length === 10, def); - check('the built-in List 2 is minted too', //.test(styles)); + check('the built-in Diamond Bullets is minted too', //.test(styles)); check('the list references the named style', content.includes(''), content.match(/]*>/g)); - check('the bullet list references List 2', content.includes('')); + check('the bullet list references Diamond Bullets', content.includes('')); check('nested lists stay bare (they inherit the style)', /\s*/.test(content), content.match(/]*>/g)); const res = importOdt(bytes); @@ -1721,19 +1721,19 @@ describe('Leg 14: named list styles (ODF)', () => { check('level 1 survives whole', imported?.levels[0]?.numType === 'upper-roman-paren' && imported?.levels[0]?.markerAlign === 'right' && imported?.levels[0]?.indentCm === 0.5, imported?.levels[0]); check('level 2 keeps its bullet', imported?.levels[1]?.bulletChar === '✓', imported?.levels[1]); - expect.soft(res.styles.list['List 2'], 'the built-in comes back as itself').toEqual(sheet.list['List 2']); + expect.soft(res.styles.list['Diamond Bullets'], 'the built-in comes back as itself').toEqual(sheet.list['Diamond Bullets']); }); it('an overridden list keeps the resolved automatic clone and drops the name', async () => { const doc: N = { type: 'doc', content: [ - { type: 'orderedList', attrs: { listStyleName: 'Numbering ABC', listStyleType: 'lower-roman' }, content: [LI(P(null, T('broken out')))] }, + { type: 'orderedList', attrs: { listStyleName: 'Outline A.I.1', listStyleType: 'lower-roman' }, content: [LI(P(null, T('broken out')))] }, ] }; const bytes = await buildOdt(doc, margins, 'portrait', undefined, null, 'A4', sheet); const files = unzipSync(bytes); const content = strFromU8(files['content.xml']); check('the list keeps its automatic style', content.includes(''), content.match(/]*>/g)); - check('no named definition is written', !strFromU8(files['styles.xml']).includes('Numbering_20_ABC')); + check('no named definition is written', !strFromU8(files['styles.xml']).includes('Outline_20_A.I.1')); check('the clone resolves the override', /[\s\S]*?text:level="1" style:num-format="i" style:num-suffix="\."/.test(content), content.match(//)?.[0]); const list = (importOdt(bytes).content.content ?? []).find((n: N) => n.type === 'orderedList'); diff --git a/tests/unit/list-style.test.ts b/tests/unit/list-style.test.ts index 077b30d..75175d4 100644 --- a/tests/unit/list-style.test.ts +++ b/tests/unit/list-style.test.ts @@ -85,20 +85,29 @@ describe('listStyleOverridden', () => { }); }); -describe('the built-ins (probed LibreOffice definitions)', () => { +describe('the built-ins (the preset gallery)', () => { const builtins = builtinListStyles(); - it('ship LibreOffice\'s five, ten levels each', () => { - expect(Object.keys(builtins).sort()).toEqual(['List 1', 'List 2', 'Numbering 123', 'Numbering ABC', 'Numbering IVX']); - for (const s of Object.values(builtins)) expect(s.levels).toHaveLength(MAX_LIST_LEVELS); + it('ship five presets, ten levels each, none uniform across its levels', () => { + expect(Object.keys(builtins).sort()).toEqual( + ['Checklist', 'Diamond Bullets', 'Numbering with Bullets', 'Outline A.I.1', 'Outline I.A.1']); + for (const s of Object.values(builtins)) { + expect(s.levels).toHaveLength(MAX_LIST_LEVELS); + const markers = new Set(s.levels.map((l) => `${l.kind}:${l.bulletChar ?? l.numType}`)); + expect(markers.size, `${s.name} must differ across levels`).toBeGreaterThan(1); + } }); - it('carry LibreOffice\'s level geometry', () => { - // List 1: 0.4cm per level; Numbering 123: 1.33cm then 0.7cm steps. - expect(listStyleMarginCm(builtins['List 1'], 1)).toBeCloseTo(0.4, 3); - expect(listStyleMarginCm(builtins['List 1'], 2)).toBeCloseTo(0.8, 3); - expect(listStyleMarginCm(builtins['Numbering 123'], 1)).toBeCloseTo(1.33, 3); - expect(listStyleMarginCm(builtins['Numbering 123'], 2)).toBeCloseTo(2.03, 3); + it('keep the plain 1.27cm step per level', () => { + expect(listStyleMarginCm(builtins['Outline I.A.1'], 1)).toBeCloseTo(1.27, 3); + expect(listStyleMarginCm(builtins['Diamond Bullets'], 3)).toBeCloseTo(3.81, 3); }); - it('Numbering IVX sets its wide labels against the right edge', () => { - expect(builtins['Numbering IVX'].levels[0]).toMatchObject({ kind: 'number', numType: 'upper-roman', markerAlign: 'right' }); + it('Outline I.A.1 runs I. A. 1. a) i) with the wide roman labels right-set', () => { + expect(builtins['Outline I.A.1'].levels.slice(0, 5).map((l) => l.numType)).toEqual( + ['upper-roman', 'upper-alpha', 'decimal', 'lower-alpha-paren', 'lower-roman-paren']); + expect(builtins['Outline I.A.1'].levels[0].markerAlign).toBe('right'); + }); + it('Numbering with Bullets numbers level 1 and dashes the levels below', () => { + expect(builtins['Numbering with Bullets'].levels[0]).toMatchObject({ kind: 'number', numType: 'decimal' }); + expect(builtins['Numbering with Bullets'].levels[1]).toMatchObject({ kind: 'bullet', bulletChar: '–' }); + expect(builtins['Checklist'].levels[0]).toMatchObject({ kind: 'bullet', bulletChar: '✓' }); }); });