mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	refactor(collections/table): rework persistence
This commit is contained in:
		| @@ -16,9 +16,10 @@ import useColTableEditing from "./col_editing"; | |||||||
| import AttributeDetailWidget from "../../attribute_widgets/attribute_detail"; | import AttributeDetailWidget from "../../attribute_widgets/attribute_detail"; | ||||||
| import attributes from "../../../services/attributes"; | import attributes from "../../../services/attributes"; | ||||||
| import { RefObject } from "preact"; | import { RefObject } from "preact"; | ||||||
|  | import SpacedUpdate from "../../../services/spaced_update"; | ||||||
|  |  | ||||||
| interface TableConfig { | interface TableConfig { | ||||||
|     tableData?: { |     tableData: { | ||||||
|         columns?: ColumnDefinition[]; |         columns?: ColumnDefinition[]; | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| @@ -53,12 +54,12 @@ export default function TableView({ note, noteIds, notePath, viewConfig, saveCon | |||||||
|  |  | ||||||
|     return ( |     return ( | ||||||
|         <div className="table-view"> |         <div className="table-view"> | ||||||
|             {columnDefs && ( |             {persistenceProps &&  ( | ||||||
|                 <> |                 <> | ||||||
|                     <Tabulator |                     <Tabulator | ||||||
|                         tabulatorRef={tabulatorRef} |                         tabulatorRef={tabulatorRef} | ||||||
|                         className="table-view-container" |                         className="table-view-container" | ||||||
|                         columns={columnDefs} |                         columns={columnDefs ?? []} | ||||||
|                         data={rowData} |                         data={rowData} | ||||||
|                         modules={[ SortModule, FormatModule, InteractionModule, EditModule, ResizeColumnsModule, FrozenColumnsModule, PersistenceModule, MoveColumnsModule, MoveRowsModule, DataTreeModule ]} |                         modules={[ SortModule, FormatModule, InteractionModule, EditModule, ResizeColumnsModule, FrozenColumnsModule, PersistenceModule, MoveColumnsModule, MoveRowsModule, DataTreeModule ]} | ||||||
|                         footerElement={<TableFooter note={note} />} |                         footerElement={<TableFooter note={note} />} | ||||||
| @@ -94,23 +95,31 @@ function TableFooter({ note }: { note: FNote }) { | |||||||
|     ) |     ) | ||||||
| } | } | ||||||
|  |  | ||||||
| function usePersistence(initialConfig: TableConfig | null | undefined, saveConfig: (newConfig: TableConfig) => void) { | function usePersistence(viewConfig: TableConfig | null | undefined, saveConfig: (newConfig: TableConfig) => void) { | ||||||
|     const config = useRef<TableConfig | null | undefined>(initialConfig); |     const [ persistenceProps, setPersistenceProps ] = useState<Pick<Options, "persistenceReaderFunc" | "persistenceWriterFunc">>(); | ||||||
|     const spacedUpdate = useSpacedUpdate(() => { |  | ||||||
|         if (config.current) { |     useEffect(() => { | ||||||
|             saveConfig(config.current); |         const viewConfigLocal = viewConfig ?? { tableData: {} }; | ||||||
|         } |         const spacedUpdate = new SpacedUpdate(() => { | ||||||
|  |             saveConfig(viewConfigLocal); | ||||||
|         }, 5_000); |         }, 5_000); | ||||||
|     const persistenceWriterFunc = useCallback((_id, type: string, data: object) => { |  | ||||||
|         if (!config.current) config.current = {}; |         setPersistenceProps({ | ||||||
|         if (!config.current.tableData) config.current.tableData = {}; |             persistenceReaderFunc(_, type) { | ||||||
|         (config.current.tableData as Record<string, {}>)[type] = data; |                 return viewConfigLocal.tableData?.[type]; | ||||||
|  |             }, | ||||||
|  |             persistenceWriterFunc(_, type, data) { | ||||||
|  |                 (viewConfigLocal.tableData as Record<string, {}>)[type] = data; | ||||||
|                 spacedUpdate.scheduleUpdate(); |                 spacedUpdate.scheduleUpdate(); | ||||||
|     }, []); |             }, | ||||||
|     const persistenceReaderFunc = useCallback((_id, type: string) => { |         }); | ||||||
|         return config.current?.tableData?.[type]; |  | ||||||
|     }, []); |         return () => { | ||||||
|     return { persistenceReaderFunc, persistenceWriterFunc }; |             spacedUpdate.updateNowIfNecessary(); | ||||||
|  |         }; | ||||||
|  |     }, [ viewConfig, saveConfig ]) | ||||||
|  |  | ||||||
|  |     return persistenceProps; | ||||||
| } | } | ||||||
|  |  | ||||||
| function useData(note: FNote, noteIds: string[], viewConfig: TableConfig | undefined, newAttributePosition: RefObject<number | undefined>, resetNewAttributePosition: () => void) { | function useData(note: FNote, noteIds: string[], viewConfig: TableConfig | undefined, newAttributePosition: RefObject<number | undefined>, resetNewAttributePosition: () => void) { | ||||||
| @@ -125,6 +134,7 @@ function useData(note: FNote, noteIds: string[], viewConfig: TableConfig | undef | |||||||
|  |  | ||||||
|     function refresh() { |     function refresh() { | ||||||
|         const info = getAttributeDefinitionInformation(note); |         const info = getAttributeDefinitionInformation(note); | ||||||
|  |  | ||||||
|         buildRowDefinitions(note, info, includeArchived, maxDepth).then(({ definitions: rowData, hasSubtree: hasChildren, rowNumber }) => { |         buildRowDefinitions(note, info, includeArchived, maxDepth).then(({ definitions: rowData, hasSubtree: hasChildren, rowNumber }) => { | ||||||
|             const columnDefs = buildColumnDefinitions({ |             const columnDefs = buildColumnDefinitions({ | ||||||
|                 info, |                 info, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user