mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
Move the header row with a CSS scroll-driven animation so the browser updates it in the same frame as the scroll. Browsers without support keep the scroll listener.
305 lines
8.5 KiB
TypeScript
305 lines
8.5 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;
|
|
|
|
private resizeObserver: ResizeObserver | null = null;
|
|
|
|
private supportsViewTimeline = false;
|
|
|
|
/**
|
|
* 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.supportsViewTimeline =
|
|
typeof CSS !== "undefined" &&
|
|
CSS.supports("animation-timeline", "view()");
|
|
|
|
// The animation in CSS needs the range in which the header row moves.
|
|
if (this.supportsViewTimeline) {
|
|
this.resizeObserver = new ResizeObserver(() => {
|
|
this.updateStickyHeaderRange();
|
|
});
|
|
this.resizeObserver.observe(this.table);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
this.resizeObserver?.disconnect();
|
|
this.resizeObserver = null;
|
|
|
|
// Reset sticky header state
|
|
this.dom.classList.remove(EditorStyleHelper.tableStickyHeader);
|
|
this.dom.style.removeProperty("--sticky-scroll-offset");
|
|
this.dom.style.removeProperty("--sticky-header-start");
|
|
this.dom.style.removeProperty("--sticky-header-travel");
|
|
}
|
|
|
|
/**
|
|
* Stores where the sticky animation starts, which is the top of the table in
|
|
* the wrapper, and how far the header row can move down before it reaches
|
|
* the bottom of the table.
|
|
*/
|
|
private updateStickyHeaderRange() {
|
|
const headerRow = this.table.querySelector("tr");
|
|
const start =
|
|
this.table.getBoundingClientRect().top -
|
|
this.dom.getBoundingClientRect().top;
|
|
const travel = headerRow
|
|
? this.table.offsetHeight - headerRow.offsetHeight
|
|
: 0;
|
|
|
|
this.dom.style.setProperty("--sticky-header-start", `${start}px`);
|
|
this.dom.style.setProperty("--sticky-header-travel", `${travel}px`);
|
|
}
|
|
|
|
/**
|
|
* Updates the sticky header state. When the browser supports scroll-driven
|
|
* animations CSS moves the header row, otherwise the transform is set here.
|
|
*/
|
|
private updateStickyHeader() {
|
|
if (!isBrowser) {
|
|
return;
|
|
}
|
|
|
|
const headerRow = this.table.querySelector("tr") as HTMLElement | null;
|
|
if (!headerRow) {
|
|
return;
|
|
}
|
|
|
|
const tableRect = this.table.getBoundingClientRect();
|
|
const headerOffset = this.getHeaderOffset();
|
|
|
|
// The animated header row stays at the bottom of the table when the table
|
|
// leaves the view, so it keeps the sticky styles until then.
|
|
if (this.supportsViewTimeline) {
|
|
this.dom.classList.toggle(
|
|
EditorStyleHelper.tableStickyHeader,
|
|
tableRect.top < headerOffset && tableRect.bottom > headerOffset
|
|
);
|
|
return;
|
|
}
|
|
|
|
const headerRowHeight = headerRow.getBoundingClientRect().height;
|
|
|
|
// 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;
|
|
}
|
|
}
|