diff --git a/docs/architecture/tables.md b/docs/architecture/tables.md index 0587804..9810c08 100644 --- a/docs/architecture/tables.md +++ b/docs/architecture/tables.md @@ -93,7 +93,9 @@ We deliberately keep TipTap's `resizable: false` (so its own columnResizing plug ones) — ODF `fo:margin-top`/`-bottom` on the table style, which LibreOffice honours like a paragraph's. Rendered on the `.tableWrapper` (`TableView`, plus the inline style for the static paths). Word has no table-level spacing, so it round-trips through ODF only. the Math Guide's callout - tables carry 0.109/0.409cm and lost half a line each without it. + tables carry 0.109/0.409cm and lost half a line each without it. The space **above** rides + `--space-before`, so the document's spacing model adds it to the block above instead of + collapsing against it, exactly as a paragraph's does. ## Cell metrics measured against LibreOffice @@ -102,6 +104,12 @@ We deliberately keep TipTap's `resizable: false` (so its own columnResizing plug model only. Measured on the Math Guide: the table row grew 1mm per row without it. `pageBreaks.ts` excludes cell leaves from its page-top-drop detection, or that missing padding would read as a decoration to add back. +- **A painted border is always a whole pixel**, whatever width the file declares — Chromium + floors it, LibreOffice reserves the declared width (the Math Guide's cells ask for a 0.05pt hairline). + The excess comes off the cell's `padding-bottom` (`--border-over`, `tableCellBorders.ts`; + the bottom side only, since `border-collapse` draws one line per boundary), which is why the + cell padding rides four custom properties rather than one shorthand. 0.25mm a row, a page + over the Math Guide's length. - **`` carries no UA defaults.** A repeating ODF header row (`table:table-header-rows`) becomes a `tableHeader`, and the browser centres and bolds it — neither Word nor LibreOffice does. `editor.css` resets both, so the cell follows its own style (the "header row" preset's diff --git a/src/lib/editor/extensions/tableCellBorders.ts b/src/lib/editor/extensions/tableCellBorders.ts index 18cba83..43316d3 100644 --- a/src/lib/editor/extensions/tableCellBorders.ts +++ b/src/lib/editor/extensions/tableCellBorders.ts @@ -57,6 +57,16 @@ export function cssBorder(value: string): string | null { return `${px}px solid ${b.color}`; } +// What the painted border costs the row beyond the width the file declares: Chromium +// floors every border to a whole pixel, LibreOffice reserves the declared width (a +// 0.05pt hairline is next to nothing). editor.css takes it off the cell's padding. +export function borderOvershootPx(value: string): number { + const b = parseBorderAttr(value); + if (b === null || b === 'none') return 0; + const declared = (b.widthPt * 96) / 72; + return Math.round((Math.max(1, Math.round(declared)) - declared) * 1000) / 1000; +} + const SIDE_META: Record = { borderTop: { data: 'data-border-top', css: 'border-top' }, borderRight: { data: 'data-border-right', css: 'border-right' }, @@ -216,7 +226,11 @@ export const TableCellBorders = Extension.create({ if (!v) return {}; const css = cssBorder(v); if (!css) return {}; - return { [SIDE_META[side].data]: v, style: `${SIDE_META[side].css}: ${css}` }; + // Only the bottom one: with border-collapse a boundary draws a single + // line, so compensating both sides of it would take the excess twice. + const over = side === 'borderBottom' ? borderOvershootPx(v) : 0; + const style = `${SIDE_META[side].css}: ${css}` + (over ? `; --border-over: ${over}px` : ''); + return { [SIDE_META[side].data]: v, style }; }, }, ]), diff --git a/src/lib/editor/extensions/tableCellPadding.ts b/src/lib/editor/extensions/tableCellPadding.ts index c279e6d..ba28fe7 100644 --- a/src/lib/editor/extensions/tableCellPadding.ts +++ b/src/lib/editor/extensions/tableCellPadding.ts @@ -32,11 +32,14 @@ export function parseCellPadding(value: unknown): CellPadding | null { return nums.length === 4 && nums.every((n) => Number.isFinite(n)) ? cellPaddingAttr(nums) : null; } -// One custom property carrying the whole shorthand; editor.css reads it with the -// default as its fallback, so nothing to write for an ordinary table. +// One custom property per side; editor.css reads them with the default as their +// fallback, so nothing to write for an ordinary table. Per side rather than one +// shorthand because the bottom one is what the border overshoot comes off (editor.css). +export const CELL_PAD_VARS = ['--cell-pad-t', '--cell-pad-r', '--cell-pad-b', '--cell-pad-l']; + export function cellPaddingStyle(value: unknown): string | null { const p = parseCellPadding(value); - return p ? `--cell-pad:${p.map((n) => `${n}cm`).join(' ')}` : null; + return p ? CELL_PAD_VARS.map((v, i) => `${v}:${p[i]}cm`).join(';') : null; } export const TableCellPadding = Extension.create({ @@ -51,7 +54,8 @@ export const TableCellPadding = Extension.create({ attributes: { cellPadding: { default: null, - parseHTML: (element: HTMLElement) => parseCellPadding(element.style.getPropertyValue('--cell-pad')), + parseHTML: (element: HTMLElement) => + parseCellPadding(CELL_PAD_VARS.map((v) => element.style.getPropertyValue(v)).join(' ')), renderHTML: (attributes: Record) => { const style = cellPaddingStyle(attributes.cellPadding); return style ? { style } : {}; diff --git a/src/lib/editor/extensions/tableView.ts b/src/lib/editor/extensions/tableView.ts index 943a23d..70a8038 100644 --- a/src/lib/editor/extensions/tableView.ts +++ b/src/lib/editor/extensions/tableView.ts @@ -1,5 +1,5 @@ import type { Node as PMNode } from '@tiptap/pm/model'; -import { parseCellPadding } from './tableCellPadding'; +import { CELL_PAD_VARS, parseCellPadding } from './tableCellPadding'; import type { ViewMutationRecord } from '@tiptap/pm/view'; // Custom table node view: uses percentage widths and the table stays at @@ -46,8 +46,11 @@ function applyMargins(node: PMNode, wrapper: HTMLElement): void { const mb = (node.attrs.marginBottom as number) || 0; wrapper.style.marginLeft = ml ? `${ml}cm` : ''; wrapper.style.marginRight = mr ? `${mr}cm` : ''; - wrapper.style.marginTop = mt ? `${mt}cm` : ''; wrapper.style.marginBottom = mb ? `${mb}cm` : ''; + // Space above rides --space-before so editor.css can add it to the block above + // instead of collapsing against it (storage/spacingModel.ts). + if (mt) wrapper.style.setProperty('--space-before', `${mt}cm`); + else wrapper.style.removeProperty('--space-before'); } // The node view never calls the node's renderHTML, so the table-style hook the generated @@ -64,8 +67,10 @@ function applyTableStyleAttr(node: PMNode, table: HTMLElement): void { // Also bypassed by the node view: the cell margins editor.css reads off the table. function applyCellPadding(node: PMNode, table: HTMLElement): void { const p = parseCellPadding(node.attrs.cellPadding); - if (p) table.style.setProperty('--cell-pad', p.map((n) => `${n}cm`).join(' ')); - else table.style.removeProperty('--cell-pad'); + CELL_PAD_VARS.forEach((v, i) => { + if (p) table.style.setProperty(v, `${p[i]}cm`); + else table.style.removeProperty(v); + }); } function buildColgroup(node: PMNode, colgroup: HTMLElement): void { diff --git a/src/styles/editor.css b/src/styles/editor.css index 3efab67..fa704a3 100644 --- a/src/styles/editor.css +++ b/src/styles/editor.css @@ -625,6 +625,12 @@ margin-left/-right here (tableView.ts), which is what the export writes. */ margin: 0; overflow-x: auto; + /* The table's space above adds to the block above it, as a paragraph's does. */ + padding-top: var(--space-before); +} +.paper[data-spacing-model='max'] .tiptap .tableWrapper { + padding-top: 0; + margin-top: var(--space-before); } .paper .tiptap table { @@ -640,7 +646,11 @@ .paper .tiptap td, .paper .tiptap th { border: 1px solid #000; - padding: var(--cell-pad, 0 0.19cm); + padding: var(--cell-pad-t, 0) var(--cell-pad-r, 0.19cm) var(--cell-pad-b, 0) var(--cell-pad-l, 0.19cm); + /* A painted border is a whole pixel wide whatever the file declares, so the excess + comes off here — else a long table grows a line every few rows + (tableCellBorders.ts). */ + padding-bottom: max(0px, calc(var(--cell-pad-b, 0px) - var(--border-over, 0px))); vertical-align: top; /* A repeating header row is a , and no word processor centres or bolds its text for that reason — drop the two UA defaults so the cell follows its own style. */