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
chore(react/collections/table): react to sorted change
Elian Doran9758632bf0ff
diff --git a/apps/client/src/widgets/collections/table/index.tsx b/apps/client/src/widgets/collections/table/index.tsxindex 4b60a4abd6..7d7048a553 100644--- a/apps/client/src/widgets/collections/table/index.tsx+++ b/apps/client/src/widgets/collections/table/index.tsx@@ -2,7 +2,7 @@ import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "pimport { ViewModeProps } from "../interface";import { buildColumnDefinitions } from "./columns";import getAttributeDefinitionInformation, { buildRowDefinitions, TableData } from "./rows";-import { useLegacyWidget, useNoteLabelInt, useSpacedUpdate, useTriliumEvent } from "../../react/hooks";+import { useLegacyWidget, useNoteLabel, useNoteLabelBoolean, useNoteLabelInt, useSpacedUpdate, useTriliumEvent } from "../../react/hooks";import Tabulator from "./tabulator";import { Tabulator as VanillaTabulator, SortModule, FormatModule, InteractionModule, EditModule, ResizeColumnsModule, FrozenColumnsModule, PersistenceModule, MoveColumnsModule, MoveRowsModule, ColumnDefinition, DataTreeModule, Options} from 'tabulator-tables';import { useContextMenu } from "./context_menu";@@ -11,7 +11,7 @@ import FNote from "../../../entities/fnote";import { t } from "../../../services/i18n";import Button from "../../react/Button";import "./index.css";-import useRowTableEditing, { canReorderRows } from "./row_editing";+import useRowTableEditing from "./row_editing";import useColTableEditing from "./col_editing";import AttributeDetailWidget from "../../attribute_widgets/attribute_detail";import attributes from "../../../services/attributes";@@ -113,13 +113,13 @@ function useData(note: FNote, noteIds: string[], viewConfig: TableConfig | undefconst [ columnDefs, setColumnDefs ] = useState<ColumnDefinition[]>();const [ rowData, setRowData ] = useState<TableData[]>();- const [ movableRows, setMovableRows ] = useState<boolean>();const [ hasChildren, setHasChildren ] = useState<boolean>();+ const [ isSorted ] = useNoteLabelBoolean(note, "sorted");+ const [ movableRows, setMovableRows ] = useState(false);function refresh() {const info = getAttributeDefinitionInformation(note);buildRowDefinitions(note, info, maxDepth).then(({ definitions: rowData, hasSubtree: hasChildren, rowNumber }) => {- const movableRows = canReorderRows(note) && !hasChildren;const columnDefs = buildColumnDefinitions({info,movableRows,@@ -129,12 +129,11 @@ function useData(note: FNote, noteIds: string[], viewConfig: TableConfig | undef});setColumnDefs(columnDefs);setRowData(rowData);- setMovableRows(movableRows);setHasChildren(hasChildren);});}- useEffect(refresh, [ note, noteIds, maxDepth ]);+ useEffect(refresh, [ note, noteIds, maxDepth, movableRows ]);// React to column changes.useTriliumEvent("entitiesReloaded", ({ loadResults}) => {@@ -146,5 +145,10 @@ function useData(note: FNote, noteIds: string[], viewConfig: TableConfig | undef}});+ // Identify if movable rows.+ useEffect(() => {+ setMovableRows(!isSorted && note.type !== "search" && !hasChildren);+ }, [ isSorted, note, hasChildren ]);+return { columnDefs, rowData, movableRows, hasChildren };}diff --git a/apps/client/src/widgets/collections/table/row_editing.ts b/apps/client/src/widgets/collections/table/row_editing.tsindex 2e5ecca144..af92b86d31 100644--- a/apps/client/src/widgets/collections/table/row_editing.ts+++ b/apps/client/src/widgets/collections/table/row_editing.ts@@ -104,8 +104,3 @@ function findRowDataById(rows: RowComponent[], branchId: string): RowComponent |}return null;}--export function canReorderRows(parentNote: FNote) {- return !parentNote.hasLabel("sorted")- && parentNote.type !== "search";-}diff --git a/apps/client/src/widgets/view_widgets/table_view/index.ts b/apps/client/src/widgets/view_widgets/table_view/index.tsindex f25430f852..2011a7456e 100644--- a/apps/client/src/widgets/view_widgets/table_view/index.ts+++ b/apps/client/src/widgets/view_widgets/table_view/index.ts@@ -61,15 +61,6 @@ export default class TableView extends ViewMode<StateInfo> {}async onEntitiesReloaded({ loadResults }: EventData<"entitiesReloaded">) {- if (!this.api) {- return;- }-- // Force a refresh if sorted is changed since we need to disable reordering.- if (loadResults.getAttributeRows().find(a => a.name === "sorted" && attributes.isAffecting(a, this.parentNote))) {- return true;- }-if (loadResults.getBranchRows().some(branch => branch.parentNoteId === this.parentNote.noteId || this.noteIds.includes(branch.parentNoteId ?? ""))|| loadResults.getNoteIds().some(noteId => this.noteIds.includes(noteId))|| loadResults.getAttributeRows().some(attr => this.noteIds.includes(attr.noteId!))) {