import { Extension } from '@tiptap/core'; import type { CommandProps } from '@tiptap/core'; import type { EditorState } from '@tiptap/pm/state'; import { selectedRect, isInTable } from '@tiptap/pm/tables'; // Word/LibreOffice-style "header row" as a styling preset: the first row gets bold text // + a light grey fill. This is real formatting (bold mark + the backgroundColor cell // attr from tableCellBackground.ts), both of which already round-trip to/from ODF — so // the editor and the exported .odt look identical, with no page repetition (we // deliberately don't emit , which is what makes Word repeat it). // The shading doubles as the toggle's marker, so the active state survives save/reopen. export const HEADER_SHADE = '#F2F2F2'; declare module '@tiptap/core' { interface Commands { tableHeaderRow: { toggleHeaderRowStyle: () => ReturnType; }; } } // Distinct cell positions (relative to tableStart) of the table's first row. function firstRowCellPositions(state: EditorState): { positions: number[]; tableStart: number } | null { if (!isInTable(state)) return null; const { map, tableStart } = selectedRect(state); const seen = new Set(); const positions: number[] = []; for (let c = 0; c < map.width; c++) { const pos = map.map[c]; if (!seen.has(pos)) { seen.add(pos); positions.push(pos); } } return { positions, tableStart }; } // True when the cursor sits in a header-shaded cell (so the Bold button should drive the // fontWeight override, like a heading, instead of the bold mark — see Toolbar.svelte). export function isInHeaderCell(state: EditorState): boolean { const { $from } = state.selection; for (let d = $from.depth; d > 0; d--) { const role = $from.node(d).type.spec.tableRole; if (role === 'cell' || role === 'header_cell') { return $from.node(d).attrs.backgroundColor === HEADER_SHADE; } } return false; } // True when every cell of the first row carries the header fill. export function isHeaderRowStyled(state: EditorState): boolean { const info = firstRowCellPositions(state); if (!info || info.positions.length === 0) return false; return info.positions.every( (p) => state.doc.nodeAt(info.tableStart + p)?.attrs.backgroundColor === HEADER_SHADE, ); } export function toggleHeaderRowStyle() { return ({ state, tr, dispatch }: CommandProps): boolean => { const info = firstRowCellPositions(state); if (!info) return false; if (!dispatch) return true; const isHeader = isHeaderRowStyled(state); const bold = state.schema.marks.bold; // The fill is the header marker; bold is presentational (CSS on `.cell-header`, keyed // off this fill) so it covers existing, typed, and pasted text alike. setNodeMarkup/ // removeMark don't shift positions, so the original cell positions stay valid. for (const p of info.positions) { const cellPos = info.tableStart + p; const cell = tr.doc.nodeAt(cellPos); if (!cell) continue; tr.setNodeMarkup(cellPos, undefined, { ...cell.attrs, backgroundColor: isHeader ? null : HEADER_SHADE }); // Turning off: strip any bold marks (incl. ones baked into the runs on export and // read back on import) so the row truly returns to normal. if (isHeader && bold) tr.removeMark(cellPos + 1, cellPos + cell.nodeSize - 1, bold); } dispatch(tr); return true; }; } export const TableHeaderRow = Extension.create({ name: 'tableHeaderRow', addCommands() { return { toggleHeaderRowStyle: () => toggleHeaderRowStyle(), }; }, });