Files
outline/shared/editor/nodes/TableView.ts
T
Tom MoorandClaude Opus 5 7c78773dc6 feat: Sticky first column in tables with a header column (#13444)
* feat: Sticky first column in tables with a header column

When a table is wider than the available space, the first column now stays
in place while the table is scrolled horizontally, matching the existing
behaviour of the header row. Only applies when every cell in that column is
a header cell.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* perf: Cache header column lookup per node

updateClassList runs on every horizontal scroll event, so the first column
check is kept until the table node itself changes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-15 17:07:28 -04:00

254 lines
6.8 KiB
TypeScript

import type { Node } from "prosemirror-model";
import {
TableMap,
TableView as ProsemirrorTableView,
} from "prosemirror-tables";
import { EditorStyleHelper } from "../styles/EditorStyleHelper";
import { TableLayout } from "../types";
import { HEADER_HEIGHT } from "../../constants";
import { isBrowser } from "../../utils/browser";
export class TableView extends ProsemirrorTableView {
public constructor(
public node: Node,
public cellMinWidth: number
) {
super(node, cellMinWidth);
this.dom.removeChild(this.table);
this.dom.classList.add(EditorStyleHelper.table);
// Add an extra wrapper to enable scrolling
this.scrollable = this.dom.appendChild(document.createElement("div"));
this.scrollable.appendChild(this.table);
this.scrollable.classList.add(EditorStyleHelper.tableScrollable);
if (isBrowser) {
this.scrollable.addEventListener(
"scroll",
() => {
this.updateClassList(this.node);
},
{
passive: true,
}
);
}
this.updateClassList(node);
// We need to wait for the next tick to ensure dom is rendered and scroll shadows are correct.
if (isBrowser) {
setTimeout(() => {
if (this.dom) {
this.updateClassList(node);
}
}, 0);
}
// Set up sticky header handling
this.setupStickyHeader();
}
public destroy() {
this.cleanupStickyHeader();
}
public override update(node: Node) {
this.updateClassList(node);
return super.update(node);
}
public override ignoreMutation(record: MutationRecord): boolean {
if (
record.type === "attributes" &&
record.target === this.dom &&
(record.attributeName === "class" || record.attributeName === "style")
) {
return true;
}
return (
record.type === "attributes" &&
(record.target === this.table || this.colgroup.contains(record.target))
);
}
private updateClassList(node: Node) {
if (!isBrowser) {
return;
}
this.dom.classList.toggle(
EditorStyleHelper.tableFullWidth,
node.attrs.layout === TableLayout.fullWidth
);
this.dom.classList.toggle(
EditorStyleHelper.tableStickyColumn,
this.hasHeaderColumn(node)
);
const shadowLeft = !!(this.scrollable && this.scrollable.scrollLeft > 0);
const shadowRight = !!(
this.scrollable &&
this.scrollable.scrollWidth > this.scrollable.clientWidth &&
this.scrollable.scrollLeft + this.scrollable.clientWidth <
this.scrollable.scrollWidth - 1
);
this.dom.classList.toggle(EditorStyleHelper.tableShadowLeft, shadowLeft);
this.dom.classList.toggle(EditorStyleHelper.tableShadowRight, shadowRight);
if (this.scrollable) {
this.dom.style.setProperty(
"--table-height",
`${this.scrollable?.clientHeight}px`
);
this.dom.style.setProperty(
"--table-width",
`${this.scrollable?.clientWidth}px`
);
} else {
this.dom.style.removeProperty("--table-height");
this.dom.style.removeProperty("--table-width");
}
}
/**
* Whether every cell in the first column of the table is a header cell. Nodes
* are immutable, so the answer is cached until the node itself changes.
*
* @param node the table node.
* @returns true if the first column is a header column.
*/
private hasHeaderColumn(node: Node) {
if (node !== this.headerColumnNode) {
const map = TableMap.get(node);
this.headerColumnNode = node;
this.headerColumn =
map.width > 0 &&
map.height > 0 &&
map
.cellsInRect({ left: 0, top: 0, right: 1, bottom: map.height })
.every(
(pos) => node.nodeAt(pos)?.type.spec.tableRole === "header_cell"
);
}
return this.headerColumn;
}
private headerColumnNode: Node | null = null;
private headerColumn = false;
private scrollable: HTMLDivElement | null = null;
private scrollHandler: (() => void) | null = null;
/**
* Sets up the scroll listener for sticky header behavior. Nested tables
* (tables within another table) are excluded from sticky header behavior.
*/
private setupStickyHeader() {
if (!isBrowser) {
return;
}
// Defer setup to ensure DOM is fully rendered
setTimeout(() => {
// Skip sticky header for nested tables
if (this.dom.closest(`table .${EditorStyleHelper.table}`)) {
return;
}
this.scrollHandler = () => {
this.updateStickyHeader();
};
// Use capture phase on document to catch all scroll events
document.addEventListener("scroll", this.scrollHandler, {
passive: true,
capture: true,
});
// Initial update
this.updateStickyHeader();
}, 0);
}
/**
* Cleans up the scroll listener and resets header styles.
*/
private cleanupStickyHeader() {
if (!isBrowser) {
return;
}
if (this.scrollHandler) {
document.removeEventListener("scroll", this.scrollHandler, {
capture: true,
});
this.scrollHandler = null;
}
// Reset sticky header state
this.dom.classList.remove(EditorStyleHelper.tableStickyHeader);
this.dom.style.removeProperty("--sticky-scroll-offset");
}
/**
* Updates the header row transform to create a sticky effect.
*/
private updateStickyHeader() {
if (!isBrowser) {
return;
}
const headerRow = this.table.querySelector("tr") as HTMLElement | null;
if (!headerRow) {
return;
}
const tableRect = this.table.getBoundingClientRect();
const headerRowHeight = headerRow.getBoundingClientRect().height;
const headerOffset = this.getHeaderOffset();
// Check if the table top is above the header area but the table extends below it
const shouldStick =
tableRect.top < headerOffset &&
tableRect.bottom > headerOffset + headerRowHeight;
if (shouldStick) {
// Set the raw scroll offset - CSS will add the header offset
const scrollOffset = Math.min(
-tableRect.top,
tableRect.height - headerRowHeight
);
this.dom.classList.add(EditorStyleHelper.tableStickyHeader);
this.dom.style.setProperty("--sticky-scroll-offset", `${scrollOffset}px`);
} else {
this.dom.classList.remove(EditorStyleHelper.tableStickyHeader);
this.dom.style.removeProperty("--sticky-scroll-offset");
}
}
/**
* Gets the current header offset from the CSS variable.
*
* @returns the offset in pixels from the top of the viewport.
*/
private getHeaderOffset(): number {
if (!isBrowser) {
return HEADER_HEIGHT;
}
const value = getComputedStyle(document.documentElement).getPropertyValue(
"--header-offset"
);
return value ? parseFloat(value) : HEADER_HEIGHT;
}
}