diff --git a/CHANGELOG.md b/CHANGELOG.md index 1795a49..47216a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,6 +74,7 @@ - Table of contents: generated from headings (H1–H5) with live page numbers and dot leaders, click an entry to jump to its heading; round-trips to ODF `text:table-of-content` and a Word TOC field - Alphabetical index (References ▸ Index entry, then the Index in the same menu): mark a word — under a key of its own where the index should file it elsewhere — and the index lists every term once, sorted case-insensitively, with all the pages it was marked on. Round-trips as ODF `text:alphabetical-index-mark` + `text:alphabetical-index` (LibreOffice reads both back unchanged) and as Word's `XE` and `INDEX` fields - Bibliography (References ▸ Citation, then the Bibliography in the index menu): a citation carries its whole source record — short name, type and its fields — so the document needs no database beside it, and the list prints one row per source cited, in document order and without page numbers, as both word processors do. Round-trips as ODF `text:bibliography-mark` + `text:bibliography` (LibreOffice reads every field back and regenerates the list from our entry templates) and as Word's `CITATION` and `BIBLIOGRAPHY` fields over a `b:Sources` custom-XML part — which LibreOffice's own Word export drops, so the record survives our leg and not its +- Section page setup: a section carries its own paper (Layout ▸ Orientation ▸ This section), so a wide table gets its landscape page amid portrait ones. The editor lays it out — the page grid is a table of per-page boxes rather than one repeating cycle, and every page below a section moves with it — and it round-trips as an ODF page layout of its own per master page and a `w:pgSz` per `w:sectPr` - Comments: annotate a selection (Review tab or the context menu) and work through them in the reviewing pane — edit, resolve, remove, click one to jump to the text it marks. Round-trips to ODF `office:annotation`/`office:annotation-end` (with LibreOffice's `loext:resolved`) and to Word's `w:commentRangeStart`/`-End` + `word/comments.xml`; a LibreOffice point comment, which has no range, attaches to the run before it - Track changes (Review ▸ Record changes): while recording, typed text is marked as an insertion and deleted text stays in the document struck through instead of going away, each carrying its author and date; consecutive typing is one change, and accept / reject work on the change at the cursor or on all of them. Round-trips to ODF's `text:tracked-changes` registry — `text:change-start`/`-end` around an insertion, `text:change` where a deletion was, its text kept in the registry — and to Word's `w:ins`/`w:del` + `w:delText` - Footnotes and endnotes (Ctrl+Alt+F / Ctrl+Alt+D): a footnote is drawn at the foot of the page its anchor sits on, with the separator line above it and the body text moved up to make room; endnotes are collected on their own page at the document end. Notes renumber themselves as anchors move, and deleting an anchor deletes its note. An options dialog covers numbering format, start value, restart, position, prefix/suffix, the two styles and the separator's length, thickness, spacing, alignment and colour. Round-trips to ODF `text:note` + `text:notes-configuration` and to Word's `word/footnotes.xml`/`endnotes.xml` + `w:footnotePr` @@ -146,7 +147,7 @@ The gap against Word/LibreOffice, most valuable first. Reviewed 2026-08-08. - Restarting the page numbering **per section** (only the document's own format and start value exist) - Heading levels 7–10 (Word and LibreOffice go that far; HTML stops at `h6`) - A bibliography's citation style: the list is set the one way ("key: author, title, year"), where both word processors offer APA, MLA, Chicago and the rest, and numbered citations (`[1]`) instead of the source's short name -- Section-level page **size and orientation**: only the margins are per section. A file whose sections disagree on the page size keeps the last one's +- A section's own **page format** is set only by the file it came from — the ribbon offers the orientation per section, the paper size document-wide. Pages of different widths are left-aligned in the scroll, where both word processors centre each - Table extras: sort, sum/formula in a cell, number recognition - A list level's own hanging indent: the marker sits at the 0.635 cm both exports write (`LIST_HANGING_CM`), so a wider *left*-set marker overflows where Word moves the text to the next list tab (a **right**-set one — `w:lvlJc`, which is what the built-in Roman numberings use — grows into the margin and is fine). Reading the value back naively also moves the markers of our own ODT exports — LibreOffice draws its own flat hanging at exactly the value odf-kit writes for level 2; see `tests/render-parity/README.md` before building on it - Linked / chained text frames diff --git a/docs/architecture/pagination.md b/docs/architecture/pagination.md index d0379c0..ba2ec1b 100644 --- a/docs/architecture/pagination.md +++ b/docs/architecture/pagination.md @@ -76,8 +76,20 @@ Horizontally `.tiptap`'s padding draws one pair for every page, so the differenc everything that writes a block margin (`editor.css`, `indent.ts`, `styleSheet.ts`, `tableView.ts`) and cleared on descendants so a nested indent can't count it twice. The page a block lands on is read *after* its own spacer, so a forced break onto the section's second -page takes the "rest" pair. Not carried: the ruler, the header/footer layer and a frame's -`COLUMN_WIDTH_CSS` stay document-wide, and both exports write one page geometry. +page takes the "rest" pair. Not carried: the ruler and a frame's `COLUMN_WIDTH_CSS` stay document-wide. + +**Per-section paper.** A section's own format/orientation (`HfSet.format`/`.orientation`, +null = the document's) makes the pages differ in size, which a repeating background +cannot express — so the page grid is a **`PageGrid`** of height runs ("every page from +here on is this tall") instead of one cycle, and the sheets are one box per page +(`PageSheetLayer`). `placeLeaves` builds its own grid as each section's first page +becomes known, so a pass never measures its own last answer; it reports the runs, and +`Editor.svelte` publishes them as `--pb-page-runs` for every other consumer (the TOC, a +cross-reference, a page-anchored frame) plus `--pb-section-page` for the next pass. +`.paper` reserves the **widest** section's paper (`--pb-paper-width`) or a landscape page +would be cut at the sheet edge, and a narrower section takes the difference as an extra +right inset on top of its own margins. The sheets are left-aligned in that box, where +both word processors centre each page. **Tables across page breaks:** when a single continuous table box crosses a page boundary, the plugin reports `TableBreakBand`s (doc-px geometry). `Editor.svelte` renders an overlay (`.band-layer` inside `.paper`) that masks the table borders bleeding through the page margins and paints the dark page gap as one seam-free stripe. diff --git a/src/App.svelte b/src/App.svelte index 52c5b0a..9de9ae6 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -984,6 +984,7 @@ bind:pageMargins bind:pageOrientation bind:pageFormat + bind:extraHfSections bind:hyphenate bind:pageNumbering bind:pageDecor diff --git a/src/lib/components/Editor.svelte b/src/lib/components/Editor.svelte index 9e7d508..4d29f25 100644 --- a/src/lib/components/Editor.svelte +++ b/src/lib/components/Editor.svelte @@ -17,12 +17,13 @@ import ContextMenu from './ContextMenu.svelte'; import HeaderFooterLayer from './HeaderFooterLayer.svelte'; import PageDecorLayer from './PageDecorLayer.svelte'; +import PageSheetLayer from './PageSheetLayer.svelte'; import LineNumberLayer from './LineNumberLayer.svelte'; import { DEFAULT_LINE_NUMBERING, type LineNumbering } from '../storage/lineNumbering'; import { EMPTY_PAGE_DECOR, type PageDecor } from '../storage/pageDecor'; import Ruler from './Ruler.svelte'; import { saveDocument, loadDocument, markDocumentLoaded } from '../storage/autosave'; - import { applyMarginVars, cmToPx, DEFAULT_MARGINS, type PageMargins } from '../storage/pageMargins'; + import { applyMarginVars, cmToPx, PX_PER_CM, DEFAULT_MARGINS, type PageMargins } from '../storage/pageMargins'; import { DEFAULT_TAB_INTERVAL_CM } from '../storage/tabInterval'; import { type Orientation } from '../storage/pageOrientation'; import { type SpacingModel } from '../storage/spacingModel'; @@ -31,9 +32,9 @@ import { EMPTY_PAGE_DECOR, type PageDecor } from '../storage/pageDecor'; import { applyNoteVars } from '../storage/noteSettings'; import { noteSettings } from '../storage/notes.svelte'; import { RESYNC_NOTES } from '../editor/extensions/notes'; - import { applyPageSizeVars, type PageFormat } from '../storage/pageFormat'; + import { applyPageSizeVars, pageDimsCm, type PageFormat } from '../storage/pageFormat'; import { DEFAULT_HF_DISTANCES, hfIsEmpty, hfUsesChapterField, type HfDoc, type HfZone, type HfDistances, type HfSet } from '../storage/headerFooter'; - import { FORCE_PAGE_RECALC, pageOfElement, readVerticalMargins, type TableBreakBand } from '../editor/extensions/pageBreaks'; + import { FORCE_PAGE_RECALC, PAGE_GAP, pageOfElement, readVerticalMargins, type TableBreakBand } from '../editor/extensions/pageBreaks'; import { findBookmark } from '../editor/extensions/bookmark'; import { recordTransaction, resetHistoryLog } from '../utils/historyLog.svelte'; import { wheelZoomFactor } from '../utils/zoom'; @@ -91,14 +92,14 @@ import { EMPTY_PAGE_DECOR, type PageDecor } from '../storage/pageDecor'; function collectChapterStarts(): { page: number; level: number; text: string }[] { if (!editor || editor.isDestroyed) return []; - const cycle = readVerticalMargins(editor.view.dom as HTMLElement).cycle; + const grid = readVerticalMargins(editor.view.dom as HTMLElement).grid; const out: { page: number; level: number; text: string }[] = []; editor.state.doc.descendants((node, pos) => { if (node.type.name !== 'heading') return; const text = node.textContent.trim(); const el = editor!.view.nodeDOM(pos) as HTMLElement | null; if (!text || !el || el.nodeType !== 1) return; - out.push({ page: pageOfElement(editor!.view, el, cycle), level: (node.attrs.level as number) ?? 1, text }); + out.push({ page: pageOfElement(editor!.view, el, grid), level: (node.attrs.level as number) ?? 1, text }); }); return out; } @@ -186,23 +187,58 @@ import { EMPTY_PAGE_DECOR, type PageDecor } from '../storage/pageDecor'; }), ].map((g) => g.map((n) => Math.round(n)).join('|')).join(',')); + // Each section's own paper (px). A section that names neither format nor orientation + // is on the document's, so its entry is the document's box. + let sectionPaper = $derived([ + pageDimsCm(pageFormat, orientation), + ...extraHfSections.map((s) => pageDimsCm(s.format ?? pageFormat, s.orientation ?? orientation)), + ].map((d) => ({ w: Math.round(d.w * PX_PER_CM), h: Math.round(d.h * PX_PER_CM) }))); + // The sheet .paper reserves is the widest of them: a landscape section is wider than + // the portrait ones around it and would otherwise be cut at the page edge. + let paperWidth = $derived(Math.max(...sectionPaper.map((p) => p.w))); + + // Which section each page belongs to (sectionStartPages holds the first page of every + // section past the first), and from it the box every per-page layer paints. + let pageBoxes = $derived.by(() => { + const out: { top: number; height: number; width: number; section: number }[] = []; + let top = 0; + let section = 0; + for (let p = 1; p <= Math.max(1, numPages); p++) { + while (section < sectionStartPages.length && sectionStartPages[section] <= p) section++; + const paper = sectionPaper[Math.min(section, sectionPaper.length - 1)]; + out.push({ top, height: paper.h, width: paper.w, section }); + top += paper.h + PAGE_GAP; + } + return out; + }); + // The same per section for the side margins, as a delta against the document's own: // .tiptap's padding draws those for every page, so a section that wants others has // its blocks inset by the difference ("leftFirst|rightFirst|leftRest|rightRest"). + // A section narrower than the sheet (.paper reserves the widest paper) takes the whole + // difference on its right, so its text still ends at its own page's right margin. let sectionInset = $derived([ - [0, 0, 0, 0], - ...extraHfSections.map((s) => { + [0, paperWidth - sectionPaper[0].w, 0, paperWidth - sectionPaper[0].w], + ...extraHfSections.map((s, i) => { const rest = s.margins ?? null; const first = s.marginsFirst ?? rest; const d = (m: PageMargins | null, side: 'left' | 'right') => m ? Math.round(cmToPx(m[side]) - cmToPx(pageMargins[side])) : 0; - return [d(first, 'left'), d(first, 'right'), d(rest, 'left'), d(rest, 'right')]; + const narrow = paperWidth - (sectionPaper[i + 1]?.w ?? paperWidth); + return [d(first, 'left'), d(first, 'right') + narrow, d(rest, 'left'), d(rest, 'right') + narrow]; }), ].map((g) => g.join('|')).join(',')); $effect(() => { const s = document.documentElement.style; s.setProperty('--pb-section-inset', sectionInset); + s.setProperty('--pb-section-page', sectionPaper.map((p) => p.h).join(',')); + s.setProperty('--pb-paper-width', `${paperWidth}px`); + // The grid the last pass laid out, as "fromPage|height" runs, so every consumer + // resolves a page number against the same one pageBreaks placed against. + s.setProperty('--pb-page-runs', sectionStartPages + .map((page, i) => `${page}|${sectionPaper[Math.min(i + 1, sectionPaper.length - 1)].h}`) + .join(',')); s.setProperty('--pb-content-top-rest', `${effTopRest}px`); s.setProperty('--pb-content-top-first', `${effTopFirst}px`); s.setProperty('--pb-content-bottom-rest', `${effBottomRest}px`); @@ -1014,9 +1050,10 @@ import { EMPTY_PAGE_DECOR, type PageDecor } from '../storage/pageDecor'; {/each} {/if} - + + {#if lineNumbering.on} - + {/if} pageBoxes[page - 1] + ?? { top: (page - 1) * (pageHeightPx + PAGE_GAP), height: pageHeightPx, width: pageWidthPx }; let mTop = $derived(cmToPx(pageMargins.top)); let mBottom = $derived(cmToPx(pageMargins.bottom)); let mLeft = $derived(cmToPx(pageMargins.left)); let mRight = $derived(cmToPx(pageMargins.right)); - let contentWidth = $derived(Math.max(0, pageWidthPx - mLeft - mRight)); + const contentWidthOf = (page: number) => Math.max(0, boxOf(page).width - mLeft - mRight); // Edge→zone distance in px. The footer may sit farther from the edge than the body // bottom margin (Word's w:footer > w:bottom); the zone then grows up into the margin. let headerDistPx = $derived(Math.min(cmToPx(hfDistances.header), pageHeightPx)); @@ -84,14 +90,15 @@ function zoneBox(zone: HfZone, page: number) { // Mirrored margins: an even page is the left-hand one, so the pair is swapped. const left = pageMargins.mirrored && page % 2 === 0 ? mRight : mLeft; - const width = contentWidth; + const width = contentWidthOf(page); + const box = boxOf(page); if (zone === 'header') { - const top = (page - 1) * cycle + headerDistPx; + const top = box.top + headerDistPx; return { top, left, width, height: Math.max(MIN_ZONE_PX, mTop - headerDistPx) }; } // Footer anchors its bottom edge at footerDistPx from the page bottom and grows up. const height = Math.max(MIN_ZONE_PX, mBottom - footerDistPx); - const top = (page - 1) * cycle + pageHeightPx - footerDistPx - height; + const top = box.top + box.height - footerDistPx - height; return { top, left, width, height }; } // The active (edited) zone grows to fit its content, keeping the anchored edge fixed @@ -374,7 +381,7 @@ class="hf-page-bg" src={bg.src} alt="" - style="top: {(p - 1) * cycle + bg.y}px; left: {bg.x}px; width: {bg.width}px; height: {bg.height}px;" + style="top: {boxOf(p).top + bg.y}px; left: {bg.x}px; width: {bg.width}px; height: {bg.height}px;" /> {/each} {/each} diff --git a/src/lib/components/LineNumberLayer.svelte b/src/lib/components/LineNumberLayer.svelte index afebf11..f8bee28 100644 --- a/src/lib/components/LineNumberLayer.svelte +++ b/src/lib/components/LineNumberLayer.svelte @@ -1,8 +1,6 @@