Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(collections/table): drag rows only by their handle (closes #12225)
Elian Doran94670fc26a15
diff --git a/apps/client/src/widgets/collections/table/data.spec.tsx b/apps/client/src/widgets/collections/table/data.spec.tsxindex cf5409a5c4..7d1017af76 100644--- a/apps/client/src/widgets/collections/table/data.spec.tsx+++ b/apps/client/src/widgets/collections/table/data.spec.tsx@@ -28,6 +28,7 @@ describe("useData", () => {afterEach(() => {render(null, container);container.remove();+ vi.restoreAllMocks();});/** Runs the hook as the table does, under a parent that can hand it a reload. */@@ -53,9 +54,22 @@ describe("useData", () => {await settle();}- function settle() {+ function settle(ms = 0) {return act(async () => {- await new Promise((resolve) => setTimeout(resolve, 0));+ await new Promise((resolve) => setTimeout(resolve, ms));+ });+ }++ /** Holds back the next `froca.getNotes()` call, as a cold cache does the refresh that fetches. */+ function delayNextGetNotes() {+ const getNotes = froca.getNotes.bind(froca);+ let delayed = false;+ vi.spyOn(froca, "getNotes").mockImplementation(async (...args) => {+ if (!delayed) {+ delayed = true;+ await new Promise((resolve) => setTimeout(resolve, 20));+ }+ return getNotes(...args);});}@@ -113,6 +127,31 @@ describe("useData", () => {expect(captured?.movableRows).toBe(false);});+ it("gives movable rows a handle even when the first refresh finishes last", async () => {+ const { note, noteIds } = collection();+ delayNextGetNotes();+ await mount(note, noteIds);+ await settle(50);++ // Without the handle column, Tabulator drags a row by any cell, and opening a cell's+ // editor on mousedown keeps the mouseup from cancelling the drag (#12225).+ expect(captured?.movableRows).toBe(true);+ expect(captured?.columnDefs?.[0]).toMatchObject({ title: "#", rowHandle: true });+ });++ it("keeps an older, slower refresh from overwriting a newer one", async () => {+ const { note, noteIds } = collection();+ await mount(note, noteIds);++ const other = buildNote({ title: "Other", children: [ { title: "Other task" } ] });+ delayNextGetNotes();+ await mount(other, []);+ await mount(note, noteIds);+ await settle(50);++ expect(captured?.rowData?.map((row) => row.title)).toEqual([ "Task 1", "Task 2" ]);+ });+it("refreshes only the rows for a row change, leaving the columns be", async () => {const { note, noteIds } = collection();await mount(note, noteIds);diff --git a/apps/client/src/widgets/collections/table/data.tsx b/apps/client/src/widgets/collections/table/data.tsxindex 8767e01149..9fcef23413 100644--- a/apps/client/src/widgets/collections/table/data.tsx+++ b/apps/client/src/widgets/collections/table/data.tsx@@ -2,7 +2,7 @@ import type { ColumnDefinition } from "tabulator-tables";import FNote from "../../../entities/fnote";import { useNoteLabelBoolean, useNoteLabelInt, useTriliumEvent } from "../../react/hooks";import { ParentComponent } from "../../react/react_utils";-import { useContext, useEffect, useState } from "preact/hooks";+import { useContext, useEffect, useRef, useState } from "preact/hooks";import getAttributeDefinitionInformation, { buildRowDefinitions, TableData } from "./rows";import froca from "../../../services/froca";import { buildColumnDefinitions } from "./columns";@@ -27,19 +27,29 @@ export default function useData(note: FNote, noteIds: string[], viewConfig: Tablconst [ rowData, setRowData ] = useState<TableData[]>();const [ hasChildren, setHasChildren ] = useState<boolean>();const [ isSorted ] = useNoteLabelBoolean(note, "sorted");- const [ movableRows, setMovableRows ] = useState(false);+ // Derived during render, so that the refresh started on mount already builds the row handle.+ const movableRows = !isReadOnly && !isSorted && note.type !== "search" && !hasChildren;+ const latestRefreshId = useRef(0);+ const columnsPending = useRef(false);// Rows change far more often than the columns do, and rebuilding the columns is not free: they// are replaced wholesale in Tabulator, which resets the scroll — so a refresh only rebuilds// them when asked, and a change that can only touch rows leaves them be.+ //+ // Only the latest refresh commits its result, as an older one holds stale values such as+ // `movableRows`; it takes over the column rebuild of any refresh it supersedes.async function refresh(rebuildColumns = true) {+ const refreshId = ++latestRefreshId.current;+ columnsPending.current ||= rebuildColumns;const info = getAttributeDefinitionInformation(note);// Ensure all note IDs are loaded.await froca.getNotes(noteIds);const { definitions: rowData, hasSubtree: hasChildren, rowNumber } = await buildRowDefinitions(note, info, includeArchived, maxDepth);- if (rebuildColumns) {+ if (refreshId !== latestRefreshId.current) return;+ if (columnsPending.current) {+ columnsPending.current = false;const columnDefs = buildColumnDefinitions({info,movableRows,@@ -90,11 +100,6 @@ export default function useData(note: FNote, noteIds: string[], viewConfig: Tabl}});- // Identify if movable rows.- useEffect(() => {- setMovableRows(!isReadOnly && !isSorted && note.type !== "search" && !hasChildren);- }, [ isReadOnly, isSorted, note, hasChildren ]);-return { columnDefs, rowData, movableRows, hasChildren };}