diff --git a/src/lib/editor/extensions/tableOfContents.ts b/src/lib/editor/extensions/tableOfContents.ts index d4de404..208fe70 100644 --- a/src/lib/editor/extensions/tableOfContents.ts +++ b/src/lib/editor/extensions/tableOfContents.ts @@ -55,6 +55,13 @@ export const TableOfContents = Node.create({ parseHTML: el => (el as HTMLElement).getAttribute('data-toc-leader') || null, renderHTML: attrs => (attrs.leader ? { 'data-toc-leader': String(attrs.leader) } : {}), }, + // Where the page number ends, in cm from the text margin — the file's own stop. + // null = the end of the column, which is where a fresh index puts it. + tabPosCm: { + default: null, + parseHTML: el => Number((el as HTMLElement).getAttribute('data-toc-tab')) || null, + renderHTML: attrs => (attrs.tabPosCm ? { 'data-toc-tab': String(attrs.tabPosCm) } : {}), + }, entries: { default: [] as TocEntry[], parseHTML: el => { @@ -245,9 +252,20 @@ class TocView { } this.dom.appendChild(row); }); + this.stopAtTab(); this.fillLeaders(); } + // Pull the rows' right edge in to the index's own tab stop, so the page numbers end + // where the file puts them rather than at the column's edge. + private stopAtTab(): void { + const cm = Number(this.node()?.attrs?.tabPosCm); + this.dom.style.paddingRight = ''; + if (!(cm > 0)) return; + const inset = this.dom.clientWidth - (cm * 96) / 2.54; + if (inset > 1) this.dom.style.paddingRight = `${Math.round(inset)}px`; + } + // As many leader dots as the gap holds. The row clips the rest on screen, but nothing // else does: 200 of them reach the PDF, the clipboard and every measurement as text. private fillLeaders(): void { diff --git a/src/lib/export/odt.ts b/src/lib/export/odt.ts index 7012051..550d2eb 100644 --- a/src/lib/export/odt.ts +++ b/src/lib/export/odt.ts @@ -679,7 +679,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 }; +type TocExport = { entries: TocEntry[]; title: string | null; maxLevel: number; leader: string | null; tabPosCm: number | 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 @@ -704,6 +704,7 @@ function replaceTableOfContents(doc: TiptapNode, tocs: TocExport[]): TiptapNode title: typeof rawTitle === 'string' ? rawTitle : null, 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, }); content.push({ type: 'paragraph', content: [{ type: 'text', text: `${TOC_SENT}${tocs.length - 1}${TOC_SENT}` }] }); continue; @@ -3171,7 +3172,9 @@ function tocXml(toc: TocExport, index: number): string { `` + `` + `` + - `` + + `` + `` + `` + ``, diff --git a/src/lib/import/odt.ts b/src/lib/import/odt.ts index 022fbbe..e0456bb 100644 --- a/src/lib/import/odt.ts +++ b/src/lib/import/odt.ts @@ -855,7 +855,7 @@ function convertBlocks(elements: Element[], ctx: Ctx, kind: BlockKind, boldByDef else out.push(...inner); } else if (el.localName === 'table-of-content' && kind === 'body') { // Generated table of contents → a tableOfContents node (regenerated live). - out.push(convertToc(el)); + out.push(convertToc(el, ctx)); } else if (/-index$|^table-of-content$|^bibliography$/.test(el.localName)) { // Other generated indexes (bibliography, …) or a TOC nested in a cell: keep the // rendered text from index-body. @@ -899,7 +899,7 @@ function convertBlocks(elements: Element[], ctx: Ctx, kind: BlockKind, boldByDef // A → a tableOfContents node. Entries (text + level + page) are // parsed from the cached index-body as a starting cache; the node view recomputes page // numbers live after mount, so parse fidelity isn't critical. -function convertToc(el: Element): Node { +function convertToc(el: Element, ctx: Ctx): Node { const indexBody = el.getElementsByTagNameNS(NS.text, 'index-body')[0]; const entries: { text: string; level: number; page: number }[] = []; if (indexBody) { @@ -925,7 +925,14 @@ function convertToc(el: Element): Node { const stop = source?.getElementsByTagNameNS(NS.style, 'index-entry-tab-stop')[0] ?? source?.getElementsByTagNameNS(NS.text, 'index-entry-tab-stop')[0]; const leader = source ? normalizeLeader(stop?.getAttributeNS(NS.style, 'leader-char')) : '.'; - return { type: 'tableOfContents', attrs: { entries, title, maxLevel, leader } }; + // 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 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 } }; } // Split a TOC entry paragraph around its last : the text before it is the