Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/tables: optimize the performance of the selection overlay on touch screen while scrolling

Adorian Doran5762789d587e
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 9c8ddf645b..ff22dfa82f 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
@@ -10,6 +10,7 @@ import {
import type { Editor, ModelElement } from "ckeditor5";
import editorStylesheetUrl from "ckeditor5/ckeditor5.css?url";
import { afterAll, beforeAll, beforeEach, describe, expect, it, onTestFinished } from "vitest";
+import { cdp } from "vitest/browser";
import { createTestEditor } from "../../../test/editor-kit.js";
import { modelTable, scrollFigure } from "../../../test/table-kit.js";
@@ -108,6 +109,31 @@ describe("TableTouchSelection", () => {
await expect.poll(() => overlay(editor).isVisible).toBe(false);
});
+ it("fades in each time it shows, unless motion is reduced", async () => {
+ await showHandles(editor, GRID);
+ const element = overlayElement(editor);
+ const [fadeIn] = element.getAnimations();
+ const style = getComputedStyle(element);
+ expect(style.animationName).toBe("ck-table-touch-selection-fade-in");
+ expect(style.animationDuration).toBe("0.3s");
+ await fadeIn.finished;
+ expect(element.getAnimations()).toHaveLength(0);
+
+ touch("mouse");
+ await expect.poll(() => overlay(editor).isVisible).toBe(false);
+ touch();
+ await expect.poll(() => overlay(editor).isVisible).toBe(true);
+ expect(element.getAnimations()).toHaveLength(1);
+
+ const session = cdp() as { send(method: string, params: object): Promise<unknown> };
+ const setReducedMotion = (value: string) => session.send("Emulation.setEmulatedMedia", {
+ features: [{ name: "prefers-reduced-motion", value }]
+ });
+ await setReducedMotion("reduce");
+ onTestFinished(() => setReducedMotion(""));
+ expect(getComputedStyle(element).animationName).toBe("none");
+ });
+
it("hides while the cells are out of view", async () => {
await showHandles(editor, GRID);
const editorElement = editor.ui.element as HTMLElement;
@@ -128,6 +154,31 @@ describe("TableTouchSelection", () => {
await expect.poll(() => overlay(editor).isVisible).toBe(true);
});
+ it("hides while the table scrolls and shows again once the scroll settles", async () => {
+ const scroller = wrapInScroller(editor, 200);
+ await showHandles(editor, `${GRID}${"<paragraph>x</paragraph>".repeat(12)}`);
+ const elsewhere = document.createElement("div");
+ elsewhere.style.cssText = "height: 50px; overflow: auto";
+ elsewhere.append(document.createElement("div"));
+ elsewhere.firstElementChild?.setAttribute("style", "height: 200px");
+ document.body.append(elsewhere);
+ onTestFinished(() => elsewhere.remove());
+ await scrollBy(elsewhere, 20);
+ expect(overlay(editor).isVisible).toBe(true);
+
+ const handle = handleCenter(editor, BOTTOM_RIGHT);
+ touchStart(handle.x, handle.y);
+ await scrollBy(scroller, 10);
+ expect(overlay(editor).isVisible).toBe(false);
+ expect(touchMove(handle.x + 30, handle.y + 30).defaultPrevented).toBe(false);
+ touchEnd();
+
+ await scrollBy(scroller, 10);
+ expect(overlay(editor).isVisible).toBe(false);
+ await expect.poll(() => overlay(editor).isVisible).toBe(true);
+ expectAround(editor, [0, 0], [0, 0]);
+ });
+
it("hides the handles beyond either side of a table that scrolls sideways", async () => {
await showHandles(editor, modelTable(
[["a[]", "b", "c", "d"], ["e", "f", "g", "h"]],
@@ -384,6 +435,7 @@ describe("TableTouchSelection", () => {
touchMove(handle.x, scrollerRect.top + scroller.clientHeight - 5);
await expect.poll(() => scroller.scrollTop).toBeGreaterThan(100);
await expect.poll(() => selectedCells(editor).length).toBeGreaterThan(14);
+ expect(overlay(editor).isVisible).toBe(true);
touchEnd();
const scrollTop = scroller.scrollTop;
@@ -504,6 +556,14 @@ function overlay(editor: Editor) {
return editor.plugins.get(TableTouchSelection).view;
}
+function overlayElement(editor: Editor) {
+ const element = overlay(editor).element;
+ if (!element) {
+ throw new Error("The overlay is not rendered.");
+ }
+ return element;
+}
+
function outlineRectOf(editor: Editor) {
const outline = overlay(editor).element?.querySelector(".ck-table-touch-selection__outline");
return outline?.getBoundingClientRect() ?? new DOMRect();
@@ -632,6 +692,15 @@ function rootOf(editor: Editor) {
return root;
}
+/** Scrolls `element` down by `distance` and waits for its `scroll` event. */
+async function scrollBy(element: HTMLElement, distance: number) {
+ const scrolled = new Promise((resolve) => {
+ element.addEventListener("scroll", resolve, { once: true });
+ });
+ element.scrollTop += distance;
+ await scrolled;
+}
+
/** Puts the editor in a scroll container `height` pixels tall. */
function wrapInScroller(editor: Editor, height: number) {
const scroller = document.createElement("div");
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 0afe0a8915..512f5f200b 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
@@ -33,6 +33,8 @@ import TableTouchSelectionView, {
/** The distance from a handle within which a touch drags it, in pixels. */
const HANDLE_REACH = 24;
+/** The time after the last scroll at which the overlay shows again, in milliseconds. */
+const SCROLL_SETTLE_MS = 150;
/**
* Handles at the corners of the selected table cells, or of the cell with the caret, shown after a
@@ -47,6 +49,8 @@ export default class TableTouchSelection extends Plugin {
private layout: SelectionLayout | null = null;
private gesture: Gesture | null = null;
private frame = 0;
+ private isScrolling = false;
+ private scrollTimer: ReturnType<typeof setTimeout> | undefined;
private readonly domEmitter = new (DomEmitterMixin())();
private readonly resizeObserver = new ResizeObserver(() => this.scheduleUpdate());
@@ -86,7 +90,7 @@ export default class TableTouchSelection extends Plugin {
this.domEmitter.listenTo(document, "pointerdown", (_evt, event: PointerEvent) => {
this.detectTouch(event);
}, { useCapture: true });
- this.domEmitter.listenTo(document, "scroll", () => this.scheduleUpdate(), {
+ this.domEmitter.listenTo(document, "scroll", (_evt, event: Event) => this.onScroll(event), {
useCapture: true,
usePassive: true
});
@@ -108,6 +112,7 @@ export default class TableTouchSelection extends Plugin {
override destroy() {
this.endGesture();
cancelAnimationFrame(this.frame);
+ clearTimeout(this.scrollTimer);
this.domEmitter.stopListening();
this.resizeObserver.disconnect();
this.view.destroy();
@@ -128,7 +133,7 @@ export default class TableTouchSelection extends Plugin {
}
this.target = target;
- this.layout = target ? this.layOut(target) : null;
+ this.layout = target && !this.isScrolling ? this.layOut(target) : null;
this.view.isVisible = !!this.layout;
if (!this.layout) {
this.endGesture();
@@ -146,6 +151,32 @@ export default class TableTouchSelection extends Plugin {
});
}
+ /**
+ * Hides the overlay while an element around the table scrolls, and shows it again once the
+ * scroll settles. The auto-scroll of a drag keeps the overlay shown.
+ */
+ private onScroll(event: Event) {
+ const domTable = this.target?.domTable;
+ if (!domTable || !isScrolledAround(event.target, domTable)) {
+ return;
+ }
+
+ if (this.gesture?.isDragging) {
+ this.scheduleUpdate();
+ return;
+ }
+
+ clearTimeout(this.scrollTimer);
+ this.scrollTimer = setTimeout(() => {
+ this.isScrolling = false;
+ this.scheduleUpdate();
+ }, SCROLL_SETTLE_MS);
+ if (!this.isScrolling) {
+ this.isScrolling = true;
+ this.update();
+ }
+ }
+
/** Shows the handles after a touch and hides them after a press of a mouse or pen. */
private detectTouch(event: PointerEvent) {
const isTouch = event.pointerType === "touch";
@@ -435,6 +466,11 @@ function findCorner({ range, visible }: SelectionLayout, x: number, y: number):
return closest;
}
+/** 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));
+}
+
function getOppositeCorner({ x, y }: Corner): Corner {
return { x: x === "left" ? "right" : "left", y: y === "top" ? "bottom" : "top" };
}
diff --git a/packages/ckeditor5/src/theme/table_touch_selection.css b/packages/ckeditor5/src/theme/table_touch_selection.css
index 9340fbaa61..d3a4b9a3fa 100644
--- a/packages/ckeditor5/src/theme/table_touch_selection.css
+++ b/packages/ckeditor5/src/theme/table_touch_selection.css
@@ -6,6 +6,7 @@
--ck-table-touch-selection-line-width: 2px;
--ck-table-touch-selection-handle-size: 14px;
--ck-table-touch-selection-handle-dragged-scale: 1.3;
+ --ck-table-touch-selection-fade-in-duration: 300ms;
}
/* Outranks the `.ck-reset_all` rules of the body collection. */
@@ -14,6 +15,13 @@
inset: 0;
z-index: calc(var(--ck-z-panel) - 1);
pointer-events: none;
+ animation-name: ck-table-touch-selection-fade-in;
+ animation-duration: var(--ck-table-touch-selection-fade-in-duration);
+ animation-timing-function: ease-out;
+
+ @media (prefers-reduced-motion: reduce) {
+ animation-name: none;
+ }
.ck-table-touch-selection__clip {
position: fixed;
@@ -43,3 +51,9 @@
scale: var(--ck-table-touch-selection-handle-dragged-scale);
}
}
+
+@keyframes ck-table-touch-selection-fade-in {
+ from {
+ opacity: 0;
+ }
+}