refactor: extract the chapter-field resolution into a pure helper

utils/chapterField owns the per-zone reading (header = the chapter at
the page top, footer = the last begun) so the probed LibreOffice
semantics is unit-tested; the DOM measurement feeding atTop stays in
Editor.svelte.
This commit is contained in:
Steffen Becker committed 2026-08-30 10:06:43 +00:00
1 parent 78e8b81bf7
commit 27a8f4a11e
4 files changed
+59 -19

No files matched your search

+4 -3
View File
@@ -10,6 +10,7 @@
import { isInTable, selectedRect } from '@tiptap/pm/tables';
import { currentCellFormat, currentCellFormula, currentCellName, guessFormula } from '../editor/extensions/tableFormula';
import type { CellFormat } from '../utils/cellFormat';
import type { ChapterStart } from '../utils/chapterField';
import TableToolbar from './TableToolbar.svelte';
import TableSplitDialog from './TableSplitDialog.svelte';
import TableSortDialog from './TableSortDialog.svelte';
@@ -95,17 +96,17 @@ import { EMPTY_PAGE_DECOR, type PageDecor } from '../storage/pageDecor';
let sectionStartPages = $state<number[]>([]);
// Where each heading starts, for the running head's chapter field. Only collected
// when a zone actually shows one — it costs a layout read per heading.
let chapterStarts = $state<{ page: number; level: number; text: string; atTop: boolean }[]>([]);
let chapterStarts = $state<ChapterStart[]>([]);
let wantsChapters = $derived(hfUsesChapterField([
{ header: headerDoc, footer: footerDoc, headerFirst: headerFirstDoc, footerFirst: footerFirstDoc,
differentFirstPage, headerEven: headerEvenDoc, footerEven: footerEvenDoc, differentOddEven },
...extraHfSections,
]));
function collectChapterStarts(): { page: number; level: number; text: string; atTop: boolean }[] {
function collectChapterStarts(): ChapterStart[] {
if (!editor || editor.isDestroyed) return [];
const vm = readVerticalMargins(editor.view.dom as HTMLElement);
const out: { page: number; level: number; text: string; atTop: boolean }[] = [];
const out: ChapterStart[] = [];
editor.state.doc.descendants((node, pos) => {
if (node.type.name !== 'heading') return;
const text = node.textContent.trim();
+4 -15
View File
@@ -8,6 +8,7 @@
import { pageDimsCm, type PageFormat } from '../storage/pageFormat';
import { DEFAULT_PAGE_NUMBERING, type PageNumbering } from '../storage/pageNumbering';
import { formatOrdinal } from '../utils/orderedListTypes';
import { chapterOn, type ChapterStart } from '../utils/chapterField';
import { t } from '../i18n/i18n.svelte';
let {
@@ -58,7 +59,7 @@
hfTick: number;
extraHfSections?: HfSet[];
sectionStartPages?: number[];
chapterStarts?: { page: number; level: number; text: string; atTop?: boolean }[];
chapterStarts?: ChapterStart[];
/** False in a split view's second pane: it draws the zones, it does not edit them. */
interactive?: boolean;
} = $props();
@@ -216,18 +217,6 @@
// actions below re-run on (the map's identity changes with each pagination pass).
type ZoneParams = [number, number, string, unknown, HfZone];
// The chapter a page runs under (ODF text:chapter, probed): the header shows the
// chapter in force at the page's top — a heading counts there only once it opens a
// page — while the footer shows the last one begun on the page.
function chapterOn(page: number, level: number, zone: HfZone): string {
let text = '';
for (const c of chapterStarts) {
if (c.page > page || (zone === 'header' && c.page === page && !c.atTop)) continue;
if (c.level <= level) text = c.text;
}
return text;
}
// The zone's tabs: static HTML no ProseMirror plugin reaches. The advances are layout
// px, so only a content change invalidates them — not the zoom transform.
function layOutTabs(node: HTMLElement, _params: ZoneParams) {
@@ -254,7 +243,7 @@
const apply = ([page, total, , , zone]: ZoneParams) => {
for (const el of Array.from(node.querySelectorAll('[data-page-field]'))) {
const kind = el.getAttribute('data-page-field');
if (kind === 'chapter') el.textContent = chapterOn(page, Number(el.getAttribute('data-level')) || 1, zone);
if (kind === 'chapter') el.textContent = chapterOn(chapterStarts, page, Number(el.getAttribute('data-level')) || 1, zone);
// The count stays decimal, as the field LibreOffice and Word write does.
else el.textContent = kind === 'count' ? String(total) : pageLabel(page);
}
@@ -368,7 +357,7 @@
if (!liveMount) return;
for (const el of Array.from(liveMount.querySelectorAll('[data-page-field]'))) {
const kind = el.getAttribute('data-page-field');
if (kind === 'chapter') el.textContent = chapterOn(editingPage, Number(el.getAttribute('data-level')) || 1, hfActive ?? 'header');
if (kind === 'chapter') el.textContent = chapterOn(chapterStarts, editingPage, Number(el.getAttribute('data-level')) || 1, hfActive ?? 'header');
else el.textContent = kind === 'count' ? String(numPages) : pageLabel(editingPage);
}
// Content height (unscaled by the zoom transform) drives the active zone's frame.
+15
View File
@@ -0,0 +1,15 @@
// The chapter a page runs under (ODF text:chapter, probed from LibreOffice): the
// header shows the chapter in force at the page's top — a heading counts there only
// once it opens a page — while the footer shows the last one begun on the page.
export type ChapterStart = { page: number; level: number; text: string; atTop?: boolean };
export function chapterOn(
starts: readonly ChapterStart[], page: number, level: number, zone: 'header' | 'footer',
): string {
let text = '';
for (const c of starts) {
if (c.page > page || (zone === 'header' && c.page === page && !c.atTop)) continue;
if (c.level <= level) text = c.text;
}
return text;
}
+36 -1
View File
@@ -1,11 +1,12 @@
// The running head's chapter field: ODF <text:chapter>, Word STYLEREF. Both keep the
// outline level; the shown name is resolved per page by HeaderFooterLayer.
// outline level; the shown name is resolved per page by utils/chapterField.
import { describe, it, expect } from 'vitest';
import { unzipSync, strFromU8 } from 'fflate';
import { buildOdt } from '../../src/lib/export/odt';
import { importOdt } from '../../src/lib/import/odt';
import { buildDocx } from '../../src/lib/export/docx';
import { importDocx } from '../../src/lib/import/docx';
import { chapterOn, type ChapterStart } from '../../src/lib/utils/chapterField';
type N = any;
@@ -46,3 +47,37 @@ describe('chapter field', () => {
expect(fieldOf(back.footer)?.attrs?.level).toBe(1);
});
});
const CS = (page: number, level: number, text: string, atTop = false): ChapterStart =>
({ page, level, text, atTop });
// LibreOffice's text:chapter, probed: the header reads the page top, the footer the
// last chapter begun on the page; a heading only tops a page it opens itself.
describe('chapter field resolution', () => {
const starts = [CS(1, 1, 'Intro', true), CS(2, 1, 'Tables'), CS(2, 1, 'Frames'), CS(4, 1, 'Notes', true)];
it('header shows the chapter in force at the page top', () => {
expect(chapterOn(starts, 2, 1, 'header')).toBe('Intro');
expect(chapterOn(starts, 3, 1, 'header')).toBe('Frames');
expect(chapterOn(starts, 4, 1, 'header')).toBe('Notes');
});
it('footer shows the last chapter begun on the page', () => {
expect(chapterOn(starts, 1, 1, 'footer')).toBe('Intro');
expect(chapterOn(starts, 2, 1, 'footer')).toBe('Frames');
expect(chapterOn(starts, 3, 1, 'footer')).toBe('Frames');
});
it('is empty on pages before the first chapter', () => {
expect(chapterOn([CS(2, 1, 'Late')], 1, 1, 'footer')).toBe('');
expect(chapterOn([CS(2, 1, 'Late')], 2, 1, 'header')).toBe('');
});
it('a level-N field follows headings of level N and above only', () => {
const mixed = [CS(1, 1, 'Part', true), CS(1, 2, 'Section'), CS(1, 3, 'Sub')];
expect(chapterOn(mixed, 1, 1, 'footer')).toBe('Part');
expect(chapterOn(mixed, 1, 2, 'footer')).toBe('Section');
expect(chapterOn(mixed, 1, 3, 'footer')).toBe('Sub');
expect(chapterOn(mixed, 2, 1, 'header')).toBe('Part');
});
});