From 7b8c7e21f59d4074199b1677b344c23972e1da19 Mon Sep 17 00:00:00 2001 From: Steffen Becker Date: Mon, 10 Aug 2026 13:12:17 +0000 Subject: [PATCH] fix: an index's entries follow the styles its own file names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The TOC rendered every level with our 0.5cm-per-level indent, an editor-only 1.5 line height and no space above — so a fixture's contents drifted 7.6mm off LibreOffice down one page and pushed entries onto the wrong one. The per-level entry template names a paragraph style; the rows now carry it as data-style and the document stylesheet does the rest. Our own indents stay the fallback for an index whose file names none. --- src/lib/editor/extensions/CLAUDE.md | 2 +- src/lib/editor/extensions/tableOfContents.ts | 14 ++++++++++++++ src/lib/export/odt.ts | 14 +++++++++++--- src/lib/import/odt.ts | 17 +++++++++++++++-- src/styles/editor.css | 18 +++++++++--------- 5 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/lib/editor/extensions/CLAUDE.md b/src/lib/editor/extensions/CLAUDE.md index d2d3d1a..7287a2f 100644 --- a/src/lib/editor/extensions/CLAUDE.md +++ b/src/lib/editor/extensions/CLAUDE.md @@ -30,7 +30,7 @@ Five topics are large enough to have their own deep-dive — read the file befor - **`bookmark.ts`** / **`crossReference.ts`** — a named range of text (a mark) and the live references to it (`REF`/`PAGEREF`). `ToolbarExpanded.svelte` owns both dialogs, as it does the link one. - **`searchReplace.ts`** — Find & Replace: decorations highlight all matches, commands navigate/replace. Touches the document only for actual replacements. - **`spellCheck.ts`** — decorations from `spell/controller.ts`; `spellErrorAt` feeds the context menu. -- **`tableOfContents.ts`** — a generated TOC: block atom listing every heading down to `maxLevel` (ODF `text:outline-level`, Word's `TOC \o` range, default 5 — listing deeper than the file asks inflates the block by pages). `title: ''` is an index that deliberately has none (its heading is a separate paragraph, as Word and LibreOffice write it); only a missing title falls back to "Table of Contents". +- **`tableOfContents.ts`** — a generated TOC: block atom listing every heading down to `maxLevel` (ODF `text:outline-level`, Word's `TOC \o` range, default 5 — listing deeper than the file asks inflates the block by pages). `title: ''` is an index that deliberately has none (its heading is a separate paragraph, as Word and LibreOffice write it); only a missing title falls back to "Table of Contents". `levelStyles` is the named paragraph style each level's entries use (ODF's per-level entry template, which is what LibreOffice regenerates from — the cached body paragraphs point at automatic styles derived from it): the rows carry it as `data-style`, so the document stylesheet gives them the file's own indent, space-before and font, and `editor.css`'s 0.5cm-per-level fallback applies only to an index that names none. - **`trailingNode.ts`** — keeps an empty paragraph after a trailing table, text box or columns section (they are isolating, so the caret would otherwise have nowhere to go). - **`headerFooter.ts`** (`hfExtensions`) — the header/footer schema: `Document` constrained to one paragraph, the body's marks, `HardBreak`, `TextAlign`, and the `pageField.ts` atoms. No image, no date field, no table. - **`pageField.ts`** — inline atoms `pageNumber`/`pageCount`/`chapterField`, rendered as ``; `HeaderFooterLayer.svelte` patches each span's text to the real per-page value. `chapterField` is the running head (ODF `text:chapter`, Word `STYLEREF`): its `level` is the outline level to show, `text` the file's cached name, and the page's own chapter is the last heading at or above that level to have started (`Editor.svelte` collects the heading→page map on each pagination pass, only when a zone shows one). diff --git a/src/lib/editor/extensions/tableOfContents.ts b/src/lib/editor/extensions/tableOfContents.ts index 208fe70..8e6c794 100644 --- a/src/lib/editor/extensions/tableOfContents.ts +++ b/src/lib/editor/extensions/tableOfContents.ts @@ -62,6 +62,17 @@ export const TableOfContents = Node.create({ parseHTML: el => Number((el as HTMLElement).getAttribute('data-toc-tab')) || null, renderHTML: attrs => (attrs.tabPosCm ? { 'data-toc-tab': String(attrs.tabPosCm) } : {}), }, + // The named paragraph style of each level's entries (ODF's per-level entry + // template). The rows carry it as data-style, so the document stylesheet gives + // them the file's own indent, spacing and font. + levelStyles: { + default: null as (string | null)[] | null, + parseHTML: el => { + try { return JSON.parse((el as HTMLElement).getAttribute('data-toc-styles') ?? 'null'); } + catch { return null; } + }, + renderHTML: attrs => (attrs.levelStyles ? { 'data-toc-styles': JSON.stringify(attrs.levelStyles) } : {}), + }, entries: { default: [] as TocEntry[], parseHTML: el => { @@ -227,9 +238,12 @@ class TocView { } const fill = typeof this.node()?.attrs?.leader === 'string' ? String(this.node()!.attrs.leader) : ''; + const levelStyles = this.node()?.attrs?.levelStyles as (string | null)[] | null | undefined; entries.forEach((e, i) => { const row = document.createElement('div'); row.className = `toc-entry toc-level-${e.level}`; + const levelStyle = levelStyles?.[e.level - 1]; + if (levelStyle) row.dataset.style = levelStyle; const text = document.createElement('span'); text.className = 'toc-text'; text.textContent = e.text; diff --git a/src/lib/export/odt.ts b/src/lib/export/odt.ts index bebe4ac..13f3dca 100644 --- a/src/lib/export/odt.ts +++ b/src/lib/export/odt.ts @@ -683,7 +683,7 @@ function replaceColumns(doc: TiptapNode, cols: ColumnsExport[]): TiptapNode { // One generated table of contents, collected by replaceTableOfContents and emitted by // applyToc. Entries are the cached heading→page rows (the node view keeps them current). type TocEntry = { text: string; level: number; page: number }; -type TocExport = { entries: TocEntry[]; title: string | null; maxLevel: number; leader: string | null; tabPosCm: number | null }; +type TocExport = { entries: TocEntry[]; title: string | null; maxLevel: number; leader: string | null; tabPosCm: number | null; levelStyles: (string | null)[] | null }; // Swap each top-level tableOfContents node for a marker paragraph carrying the TOC // sentinel and collect its cached entries. Top-level only (like replacePageBreaks): a @@ -709,6 +709,7 @@ function replaceTableOfContents(doc: TiptapNode, tocs: TocExport[]): TiptapNode maxLevel: depth >= 1 ? Math.min(MAX_HEADING_LEVEL, depth) : MAX_HEADING_LEVEL, leader: normalizeLeader(child.attrs?.leader), tabPosCm: typeof child.attrs?.tabPosCm === 'number' ? child.attrs.tabPosCm : null, + levelStyles: Array.isArray(child.attrs?.levelStyles) ? (child.attrs!.levelStyles as (string | null)[]) : null, }); content.push({ type: 'paragraph', content: [{ type: 'text', text: `${TOC_SENT}${tocs.length - 1}${TOC_SENT}` }] }); continue; @@ -3175,6 +3176,13 @@ function contentsHeadingStyle(): string { // The full : a source (title + per-level entry templates that // carry a right tab stop, page number, and link markers so LibreOffice rebuilds it as a // linking TOC) plus a cached index-body (title + one entry paragraph per heading). +// A level's entry style: the file's own named one where the index came with it, else +// the automatic Contents_20_N this export mints. +function tocLevelStyle(toc: TocExport, level: number): string { + const own = toc.levelStyles?.[level - 1]; + return own ? odfStyleName(own) : `Contents_20_${level}`; +} + function tocXml(toc: TocExport, index: number): string { const title = toc.title ?? 'Table of Contents'; const name = `Table of Contents${index + 1}`; @@ -3184,7 +3192,7 @@ function tocXml(toc: TocExport, index: number): string { HEADING_LEVELS.filter(l => l <= toc.maxLevel) .map( l => - `` + + `` + `` + `` + `` : '') + toc.entries - .map(e => `${escapeXml(e.text)}${e.page}`) + .map(e => `${escapeXml(e.text)}${e.page}`) .join('') + ``; return `${source}${body}`; diff --git a/src/lib/import/odt.ts b/src/lib/import/odt.ts index f6e5856..f0c3a33 100644 --- a/src/lib/import/odt.ts +++ b/src/lib/import/odt.ts @@ -928,11 +928,24 @@ function convertToc(el: Element, ctx: Ctx): Node { // Where the page number ends: the entry template's own stop, else the right stop of // the paragraph style its entries use. Word and LibreOffice both put it short of the // text width in some templates, and the number then hangs 45mm out of place. - const template = source?.getElementsByTagNameNS(NS.text, 'table-of-content-entry-template')[0]; + const templates = Array.from(source?.getElementsByTagNameNS(NS.text, 'table-of-content-entry-template') ?? []); + const template = templates[0]; const styled = ctx.resolver.tabStops(template?.getAttributeNS(NS.text, 'style-name') ?? null); const tabPosCm = lengthToCm(stop?.getAttributeNS(NS.style, 'position')) ?? [...styled].reverse().find(t => t.align === 'right')?.pos ?? null; - return { type: 'tableOfContents', attrs: { entries, title, maxLevel, leader, tabPosCm } }; + // Each level's own paragraph style: the entry rows carry its name, so the document + // stylesheet gives them the file's indent, spacing and font instead of our defaults. + const levelStyles: (string | null)[] = []; + for (const tpl of templates) { + const level = Number(tpl.getAttributeNS(NS.text, 'outline-level')); + const named = ctx.resolver.namedAncestor(tpl.getAttributeNS(NS.text, 'style-name')); + if (!(level >= 1) || !named) continue; + ctx.usedStyles.add(named); + levelStyles[level - 1] = ctx.styleNames.get(named) ?? named; + } + const attrs: Record = { entries, title, maxLevel, leader, tabPosCm }; + if (levelStyles.some(Boolean)) attrs.levelStyles = Array.from(levelStyles, (s) => s ?? null); + return { type: 'tableOfContents', attrs }; } // Split a TOC entry paragraph around its last : the text before it is the diff --git a/src/styles/editor.css b/src/styles/editor.css index bf967f7..24ad546 100644 --- a/src/styles/editor.css +++ b/src/styles/editor.css @@ -214,16 +214,16 @@ display: flex; align-items: baseline; cursor: pointer; - line-height: 1.5; } .paper .tiptap .toc-entry:hover .toc-text { text-decoration: underline; } -/* One level = 0.5cm, the indent LibreOffice's own Contents 1…5 styles carry. */ -.paper .tiptap .toc-level-2 { padding-left: 0.5cm; } -.paper .tiptap .toc-level-3 { padding-left: 1cm; } -.paper .tiptap .toc-level-4 { padding-left: 1.5cm; } -.paper .tiptap .toc-level-5 { padding-left: 2cm; } +/* One level = 0.5cm, the indent LibreOffice's own Contents 1…5 styles carry — the + fallback for an index whose file names no per-level style (that one brings its own). */ +.paper .tiptap .toc-level-2:not([data-style]) { padding-left: 0.5cm; } +.paper .tiptap .toc-level-3:not([data-style]) { padding-left: 1cm; } +.paper .tiptap .toc-level-4:not([data-style]) { padding-left: 1.5cm; } +.paper .tiptap .toc-level-5:not([data-style]) { padding-left: 2cm; } .paper .tiptap .toc-text { white-space: nowrap; overflow: hidden; @@ -480,10 +480,10 @@ inherits: false; initial-value: 0; } -.paper .tiptap :is(p, h1, h2, h3, h4, h5, .toc, .toc-title) { +.paper .tiptap :is(p, h1, h2, h3, h4, h5, .toc, .toc-title, .toc-entry) { padding-top: var(--space-before); } -.paper[data-spacing-model='max'] .tiptap :is(p, h1, h2, h3, h4, h5, .toc, .toc-title) { +.paper[data-spacing-model='max'] .tiptap :is(p, h1, h2, h3, h4, h5, .toc, .toc-title, .toc-entry) { padding-top: 0; margin-top: var(--space-before); } @@ -491,7 +491,7 @@ /* The line box: the paragraph's spacing factor times its font's natural line height. Either half may come from the style rule or, nearer, from the block's own attrs (lineHeight.ts / blockFontSize.ts) — multiplying them here is what keeps both. */ -.paper .tiptap :is(p, h1, h2, h3, h4, h5, li) { +.paper .tiptap :is(p, h1, h2, h3, h4, h5, li, .toc-entry) { line-height: calc(var(--line-factor) * var(--natural-line)); }