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.
This commit is contained in:
1 parent
697eea4d86
commit
6f57a8e7ed
7 files changed
+62
-42
No files matched your search
@@ -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 `<text:list-style>` 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
|
||||
|
||||
@@ -115,21 +115,29 @@ export function builtinListStyles(): Record<string, ListStyle> {
|
||||
return out;
|
||||
}
|
||||
|
||||
const levels = (n: number, make: (i: number) => ListLevelStyle): ListLevelStyle[] =>
|
||||
Array.from({ length: n }, (_, i) => make(i));
|
||||
const num = (numType: Exclude<OrderedListType, 'multilevel'>, 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('◦')]) },
|
||||
];
|
||||
@@ -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.
|
||||
|
||||
@@ -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(/<w:style w:type="numbering" w:styleId="Prfliste">[\s\S]*?<\/w:style>/)?.[0] ?? '';
|
||||
expect(style).toContain('<w:name w:val="Prüfliste"/>');
|
||||
expect(/<w:numId w:val="[1-9]\d*"\/>/.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']);
|
||||
|
||||
@@ -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'];
|
||||
|
||||
@@ -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', /<text:list-style style:name="List_20_2" style:display-name="List 2">/.test(styles));
|
||||
check('the built-in Diamond Bullets is minted too', /<text:list-style style:name="Diamond_20_Bullets" style:display-name="Diamond Bullets">/.test(styles));
|
||||
check('the list references the named style', content.includes('<text:list text:style-name="Prüfliste">'), content.match(/<text:list [^>]*>/g));
|
||||
check('the bullet list references List 2', content.includes('<text:list text:style-name="List_20_2">'));
|
||||
check('the bullet list references Diamond Bullets', content.includes('<text:list text:style-name="Diamond_20_Bullets">'));
|
||||
check('nested lists stay bare (they inherit the style)', /<text:list>\s*<text:list-item>/.test(content), content.match(/<text:list[^>]*>/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('<text:list text:style-name="L1">'), content.match(/<text:list [^>]*>/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', /<text:list-style style:name="L1">[\s\S]*?text:level="1" style:num-format="i" style:num-suffix="\."/.test(content), content.match(/<text:list-style[\s\S]*?<\/text:list-style>/)?.[0]);
|
||||
|
||||
const list = (importOdt(bytes).content.content ?? []).find((n: N) => n.type === 'orderedList');
|
||||
|
||||
@@ -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: '✓' });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user