Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/tables: fix an issue with the touch selection overlay

Adorian Doran0c99c5e5058d
diff --git a/packages/ckeditor5/src/plugins/table_sticky_header_read_only.ts b/packages/ckeditor5/src/plugins/table_sticky_header_read_only.ts
index 1da5bfda15..8c68c698ba 100644
--- a/packages/ckeditor5/src/plugins/table_sticky_header_read_only.ts
+++ b/packages/ckeditor5/src/plugins/table_sticky_header_read_only.ts
@@ -93,6 +93,15 @@ export class StickyTableHeaders {
}
+/**
+ * The bottom of the header rows of `table` in the viewport while they stick, or `null` while they
+ * rest at the top of the table.
+ */
+export function getStuckHeaderBottom(table: Element) {
+ const thead = table.querySelector<HTMLElement>(":scope > thead");
+ return thead && getShift(thead) > 0 ? thead.getBoundingClientRect().bottom : null;
+}
+
/** Sets the start and the travel of the header rows, measured without the shift of sticking. */
function measure(figure: Element) {
const table = figure.querySelector(":scope > table");
diff --git a/packages/ckeditor5/src/plugins/table_touch_selection/table_touch_selection.spec.ts b/packages/ckeditor5/src/plugins/table_touch_selection/table_touch_selection.spec.ts
index ff22dfa82f..143e23b0d5 100644
--- a/packages/ckeditor5/src/plugins/table_touch_selection/table_touch_selection.spec.ts
+++ b/packages/ckeditor5/src/plugins/table_touch_selection/table_touch_selection.spec.ts
@@ -1,3 +1,5 @@
+import "../../theme/table_sticky_header.css";
+
import {
_setModelData as setModelData,
ClassicEditor,
@@ -15,6 +17,7 @@ import { cdp } from "vitest/browser";
import { createTestEditor } from "../../../test/editor-kit.js";
import { modelTable, scrollFigure } from "../../../test/table-kit.js";
import TriliumTableScroll from "../table_scroll.js";
+import TableStickyHeader from "../table_sticky_header.js";
import TableTouchSelection from "./table_touch_selection.js";
const PLUGINS = [
@@ -23,6 +26,7 @@ const PLUGINS = [
Table,
TableColumnResize,
TriliumTableScroll,
+ TableStickyHeader,
TableTouchSelection
];
const TOUCH_ID = 1;
@@ -219,6 +223,41 @@ describe("TableTouchSelection", () => {
expect(overlay(editor).isVisible).toBe(true);
});
+ it("hides the cells under the stuck header rows, unless the selection includes them",
+ async () => {
+ const scroller = wrapInScroller(editor, 200);
+ // The client unsets the `overflow` of the editable, so the header rows stick to
+ // `scroller` instead of the editable.
+ editor.editing.view.change((writer) => {
+ const root = editor.editing.view.document.getRoot();
+ if (root) {
+ writer.setStyle("overflow", "visible", root);
+ }
+ });
+ const rows = Array.from({ length: 12 }, (_, row) => [`${row}`, "x"]);
+ rows[3][0] = "3[]";
+ await showHandles(editor, modelTable(rows, 'headingRows="1"'));
+ await frames();
+ const thead = domCell(editor, cell(editor, 0, 0)).closest("thead");
+ if (!thead) {
+ throw new Error("The table has no header rows.");
+ }
+
+ const scrollerTop = scroller.getBoundingClientRect().top;
+ // Leaves the top 10px of the cell with the caret under the stuck header rows.
+ const headerBottom = scrollerTop + thead.getBoundingClientRect().height;
+ await scrollBy(scroller, cellRect(editor, 3, 0).top - headerBottom + 10);
+ await expect.poll(() => overlay(editor).isVisible).toBe(true);
+ expectNear(thead.getBoundingClientRect().top, scrollerTop);
+ expect(shownHandles(editor)).toEqual([BOTTOM_LEFT, BOTTOM_RIGHT]);
+ expectNear(clipRectOf(editor).top, thead.getBoundingClientRect().bottom);
+
+ selectCells(editor, [0, 0], [0, 1]);
+ await expect.poll(() => shownHandles(editor))
+ .toEqual([TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT]);
+ expectAround(editor, [0, 0], [0, 1]);
+ });
+
it("follows the cells when the table moves and into another table", async () => {
// A table without a width stays centered, so it moves when the editable resizes.
const second = modelTable([["c", "d"], ["e", "f"]]);
@@ -569,6 +608,11 @@ function outlineRectOf(editor: Editor) {
return outline?.getBoundingClientRect() ?? new DOMRect();
}
+function clipRectOf(editor: Editor) {
+ const clip = overlay(editor).element?.querySelector(".ck-table-touch-selection__clip");
+ return clip?.getBoundingClientRect() ?? new DOMRect();
+}
+
/** The indexes in `CORNERS` of the handles that are shown. */
function shownHandles(editor: Editor) {
return overlay(editor).handles.flatMap((handle, index) =>
diff --git a/packages/ckeditor5/src/plugins/table_touch_selection/table_touch_selection.ts b/packages/ckeditor5/src/plugins/table_touch_selection/table_touch_selection.ts
index 512f5f200b..bd0e5d59a9 100644
--- a/packages/ckeditor5/src/plugins/table_touch_selection/table_touch_selection.ts
+++ b/packages/ckeditor5/src/plugins/table_touch_selection/table_touch_selection.ts
@@ -15,6 +15,7 @@ import type {
ViewDocumentTouchStartEvent
} from "ckeditor5";
+import { getStuckHeaderBottom } from "../table_sticky_header_read_only.js";
import {
type ClientBox,
findScroller,
@@ -225,23 +226,25 @@ export default class TableTouchSelection extends Plugin {
* of the cells is visible.
*/
private layOut({ cells, domTable, domRoot }: SelectionTarget): SelectionLayout | null {
- const cellBoxes: ClientBox[] = [];
+ const domCells: HTMLElement[] = [];
for (const cell of cells) {
const domCell = this.getDomElement(cell);
/* v8 ignore next 3 -- defensive: a cell in the model is rendered in its table */
if (!domCell) {
return null;
}
- cellBoxes.push(domCell.getBoundingClientRect());
+ domCells.push(domCell);
}
+ const cellBoxes: ClientBox[] = domCells.map((domCell) => domCell.getBoundingClientRect());
+
const range = cellBoxes.reduce<ClientBox>((union, box) => ({
left: Math.min(union.left, box.left),
top: Math.min(union.top, box.top),
right: Math.max(union.right, box.right),
bottom: Math.max(union.bottom, box.bottom)
}), cellBoxes[0]);
- const visible = this.getVisibleArea(domTable, domRoot);
+ const visible = this.getVisibleArea(domTable, domRoot, domCells);
if (isEmptyBox(intersectBoxes(range, visible))) {
return null;
}
@@ -251,14 +254,20 @@ export default class TableTouchSelection extends Plugin {
}
/**
- * The part of the viewport where the table can be seen: inside the element that clips it
- * horizontally, the scroll container of the editable and the viewport offset of the editor.
+ * The part of the viewport where `domCells` of the table can be seen: inside the element that
+ * clips the table horizontally, the scroll container of the editable and the viewport offset
+ * of the editor, and below the stuck header rows unless `domCells` includes one of them.
*/
- private getVisibleArea(domTable: HTMLElement, domRoot: HTMLElement): ClientBox {
+ private getVisibleArea(
+ domTable: HTMLElement,
+ domRoot: HTMLElement,
+ domCells: readonly HTMLElement[]
+ ): ClientBox {
const offset = this.editor.ui.viewportOffset;
+ const headerBottom = domCells.some(isHeaderCell) ? null : getStuckHeaderBottom(domTable);
const viewport = {
left: offset.left ?? 0,
- top: offset.top ?? 0,
+ top: Math.max(offset.top ?? 0, headerBottom ?? -Infinity),
right: document.documentElement.clientWidth - (offset.right ?? 0),
bottom: Math.min(getViewportBottom(), window.innerHeight - (offset.bottom ?? 0))
};
@@ -466,6 +475,11 @@ function findCorner({ range, visible }: SelectionLayout, x: number, y: number):
return closest;
}
+/** Whether `domCell` is in the header rows of its table. */
+function isHeaderCell(domCell: HTMLElement) {
+ return domCell.parentElement?.parentElement?.localName === "thead";
+}
+
/** Whether a scroll of `scrolled`, an element or the document, moves `element`. */
function isScrolledAround(scrolled: EventTarget | null, element: Element) {
return scrolled === document || (scrolled instanceof Node && scrolled.contains(element));