@macrostrat/data-sheet 3.0.4 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -1
- package/dist/cell-renderer.cjs +162 -0
- package/dist/cell-renderer.cjs.map +1 -0
- package/dist/cell-renderer.d.ts +8 -0
- package/dist/cell-renderer.js +162 -0
- package/dist/cell-renderer.js.map +1 -0
- package/dist/core.cjs +32 -159
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +4 -9
- package/dist/core.js +33 -158
- package/dist/core.js.map +1 -1
- package/dist/data-sheet.css +9 -9
- package/dist/index.cjs +13 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +13 -7
- package/dist/index.js.map +1 -1
- package/dist/postgrest-table/index.cjs.map +1 -1
- package/dist/postgrest-table/index.d.ts +1 -1
- package/dist/postgrest-table/index.js.map +1 -1
- package/dist/provider.cjs +59 -339
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.d.ts +11 -65
- package/dist/provider.js +58 -339
- package/dist/provider.js.map +1 -1
- package/dist/types.d.ts +54 -0
- package/dist/utils/key-handling.cjs +26 -1
- package/dist/utils/key-handling.cjs.map +1 -1
- package/dist/utils/key-handling.d.ts +3 -2
- package/dist/utils/key-handling.js +27 -2
- package/dist/utils/key-handling.js.map +1 -1
- package/dist/zustand-store.cjs +320 -0
- package/dist/zustand-store.cjs.map +1 -0
- package/dist/zustand-store.d.ts +6 -0
- package/dist/zustand-store.js +318 -0
- package/dist/zustand-store.js.map +1 -0
- package/package.json +5 -4
- package/src/cell-renderer.ts +217 -0
- package/src/core.ts +52 -212
- package/src/index.ts +3 -0
- package/src/postgrest-table/index.ts +1 -1
- package/src/provider.ts +66 -486
- package/src/types.ts +66 -0
- package/src/utils/key-handling.ts +31 -5
- package/src/zustand-store.ts +377 -0
package/dist/provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.js","sources":["../src/provider.ts"],"sourcesContent":["import { createContext, useContext, useEffect, useRef, useState } from \"react\";\nimport h from \"@macrostrat/hyper\";\nimport { createStore, StoreApi, useStore } from \"zustand\";\nimport { createComputed } from \"zustand-computed\";\nimport type {\n FocusedCellCoordinates,\n Region,\n Table2,\n} from \"@blueprintjs/table\";\nimport {\n ColumnSpec,\n ColumnSpecOptions,\n editorKeyHandler,\n generateColumnSpec,\n range,\n tableKeyHandler,\n} from \"./utils\";\nimport update, { Spec } from \"immutability-helper\";\n\nexport interface DataSheetCoreProps<T> {\n data: T[];\n columnSpec?: ColumnSpec[];\n editable?: boolean;\n enableColumnReordering?: boolean;\n defaultColumnWidth?: number;\n}\n\nexport interface DataSheetState<T> {\n selection: Region[];\n columnSpec: ColumnSpec[];\n fillValueBaseCell: FocusedCellCoordinates | null;\n focusedCell: FocusedCellCoordinates | null;\n topLeftCell: FocusedCellCoordinates | null;\n deletedRows: Set<number>;\n // Sparse data structure for updated data\n updatedData: T[];\n initialized: boolean;\n columnWidthsIndex: Map<string, number>;\n}\n\ntype DataSheetVals<T> = DataSheetState<T> & DataSheetCoreProps<T>;\n\ntype StateUpdater<T> = T[] | ((state: T[]) => T[]);\n\nexport interface DataSheetStoreMain<T> extends DataSheetVals<T> {\n setSelection(selection: Region[]): void;\n onDragValue(event: MouseEvent): void;\n setUpdatedData(data: StateUpdater<T>): void;\n onCellEdited(rowIndex: number, columnName: string, value: any): void;\n onSelectionEdited(value: any): void;\n onColumnsReordered(oldIndex: number, newIndex: number, length: number): void;\n onColumnWidthChanged(columnIndex: number, newWidth: number): void;\n moveFocusedCell(direction: \"up\" | \"down\" | \"left\" | \"right\"): void;\n deleteSelectedRows(): void;\n clearSelection(): void;\n resetChanges(): void;\n addRow(): void;\n initialize(props: DataSheetCoreProps<T>): void;\n onSelection(selection: Region[]): void;\n // Internal method used for infinite scrolling\n setVisibleCells(visibleCells: VisibleCells): void;\n scrollToRow(rowIndex: number): void;\n tableRef: React.MutableRefObject<Table2>;\n columnWidthsIndex: Map<string, number>;\n defaultColumnWidth: number;\n}\n\nexport type DataSheetProviderProps<T> = DataSheetCoreProps<T> & {\n children: React.ReactNode;\n columnSpecOptions?: ColumnSpecOptions<T>;\n};\n\nconst DataSheetContext = createContext<StoreApi<DataSheetStore<any>>>(null);\n\nexport interface VisibleCells {\n rowIndexStart: number;\n rowIndexEnd: number;\n}\n\ninterface DataSheetComputedVals {\n hasUpdates: boolean;\n /** State for column widths (if resized).\n * This will reset if the columnSpec prop changes\n */\n columnWidths: number[];\n tableKeyHandler: (evt: React.KeyboardEvent) => void;\n editorKeyHandler: (evt: React.KeyboardEvent) => void;\n isSingleCellSelection?: boolean;\n}\n\nexport type DataSheetStore<T> = DataSheetComputedVals & DataSheetStoreMain<T>;\n\nconst computed = createComputed(\n (state: DataSheetStoreMain<any>): DataSheetComputedVals => {\n const isSingleCellSelection = singleFocusedCell(state.selection) != null;\n return {\n hasUpdates: state.updatedData.length > 0 || state.deletedRows.size > 0,\n columnWidths: state.columnSpec.map(\n (col) =>\n state.columnWidthsIndex.get(col.key) ??\n col.width ??\n state.defaultColumnWidth,\n ),\n editorKeyHandler: (e: React.KeyboardEvent) =>\n editorKeyHandler(e, isSingleCellSelection),\n isSingleCellSelection,\n tableKeyHandler: (e: React.KeyboardEvent) => {\n tableKeyHandler(e, state);\n },\n };\n },\n) as any;\n\nexport function DataSheetProvider<T>({\n children,\n data,\n columnSpec,\n columnSpecOptions,\n editable,\n enableColumnReordering,\n defaultColumnWidth = 150,\n}: DataSheetProviderProps<T>) {\n const visibleCellsRef = useRef<VisibleCells>({\n rowIndexStart: 0,\n rowIndexEnd: 0,\n });\n\n const tableRef = useRef<Table2>(null);\n\n const [store] = useState(() => {\n const spec = columnSpec || generateColumnSpec(data, columnSpecOptions);\n return createStore<DataSheetStore<T>>(\n computed((set): DataSheetStore<T> => {\n return {\n data,\n columnSpec: spec,\n defaultColumnWidth,\n editable,\n deletedRows: new Set<number>(),\n selection: [],\n fillValueBaseCell: null,\n updatedData: [],\n focusedCell: null,\n topLeftCell: null,\n initialized: false,\n tableRef,\n // This is a placeholder\n enableColumnReordering: false,\n setSelection(selection: Region[]) {\n set(updateSelection(selection));\n },\n columnWidthsIndex: new Map<string, number>(),\n moveFocusedCell(direction: \"up\" | \"down\" | \"left\" | \"right\") {\n set((state) => {\n const { topLeftCell } = state;\n if (topLeftCell == null) return {};\n let { col, row } = topLeftCell;\n switch (direction) {\n case \"up\":\n row = Math.max(0, row - 1);\n break;\n case \"down\":\n row = Math.min(\n row + 1,\n Math.max(state.data.length, state.updatedData.length) - 1,\n );\n break;\n case \"left\":\n col = Math.max(0, col - 1);\n break;\n case \"right\":\n col = Math.min(col + 1, state.columnSpec.length - 1);\n break;\n }\n const region: Region = { cols: [col, col], rows: [row, row] };\n return updateSelection([region]);\n });\n },\n addRow(row: Partial<T> = {} as T) {\n /** Add a new row. If there is a selection, use the last row index to determine\n * where to insert the new row. Otherwise, append to the end of the data. */\n set((state) => {\n const { updatedData, data, selection } = state;\n const lastRowIndex =\n getLastRowIndex(selection) ??\n Math.max(data.length, updatedData.length) - 1;\n // If there is a selection, insert the new row after the last selected row\n const spec: Spec<any> = {\n $splice: [[lastRowIndex + 1, 0, row]],\n };\n console.log(spec);\n\n return {\n updatedData: update(updatedData, spec),\n };\n });\n },\n setUpdatedData(data: StateUpdater<T>) {\n if (Array.isArray(data)) {\n set({ updatedData: data });\n } else {\n set((state) => {\n return { updatedData: data(state.updatedData) };\n });\n }\n },\n deleteSelectedRows() {\n // Remove selected rows from the data and updatedData arrays\n set((state) => {\n const { selection, deletedRows } = state;\n const rowIndices = getRowIndices(selection);\n\n // Delete rows from both updatedData and data\n const newDeletedRows = new Set(deletedRows);\n for (const rowIndex of rowIndices) {\n newDeletedRows.add(rowIndex);\n }\n\n // Remove selected rows and reset selection\n return {\n deletedRows: newDeletedRows,\n selection: [],\n focusedCell: null,\n topLeftCell: null,\n fillValueBaseCell: null,\n };\n });\n },\n resetChanges() {\n // Reset the updated data to the initial data\n set((state) => ({\n updatedData: [],\n deletedRows: new Set<number>(),\n columnWidths: new Map<string, number>(),\n selection: [],\n focusedCell: null,\n topLeftCell: null,\n fillValueBaseCell: null,\n }));\n },\n onColumnWidthChanged(columnIx: number, newWidth: number) {\n set((state) => {\n const { columnSpec, columnWidthsIndex } = state;\n const colKey = columnSpec[columnIx].key;\n const newColumnWidths = new Map(columnWidthsIndex);\n newColumnWidths.set(colKey, newWidth);\n return { columnWidthsIndex: newColumnWidths };\n });\n },\n setVisibleCells(visibleCells: VisibleCells) {\n // Visible cells are used for infinite scrolling\n // Right now we don't store this in the state\n visibleCellsRef.current = visibleCells;\n },\n onDragValue(event: MouseEvent) {\n set((state) => {\n return { fillValueBaseCell: state.focusedCell };\n });\n event.preventDefault();\n },\n onCellEdited(rowIndex: number, columnName: string, value: any) {\n set((state) => {\n const { editable, updatedData, data } = state;\n if (!editable) return {};\n let rowSpec: any;\n\n // Check to see if the new value is the same as the old one\n if (value !== data[rowIndex]?.[columnName]) {\n const rowOp = updatedData[rowIndex] != null ? \"$merge\" : \"$set\";\n rowSpec = { [rowOp]: { [columnName]: value } };\n } else {\n rowSpec = { $unset: [columnName] };\n }\n const spec: Spec<T[]> = { [rowIndex]: rowSpec };\n return { updatedData: update(updatedData, spec) };\n });\n },\n initialize(props: DataSheetCoreProps<T>) {\n set({ ...props, initialized: true });\n },\n clearSelection() {\n set((state) => {\n // Delete all selected cells\n const { selection, updatedData, columnSpec, data } = state;\n let spec = {};\n for (const region of selection) {\n console.log(\"Clearing region\", region);\n const { cols, rows } = region;\n const rowRange = range(rows ?? [0, updatedData.length - 1]);\n const colRange = range(cols ?? [0, columnSpec.length - 1]);\n for (const row of rowRange) {\n let vals = {};\n for (const col of colRange) {\n const key = columnSpec[col].key;\n const currentValue =\n updatedData[row]?.[key] ?? data[row]?.[key];\n if (currentValue != null && currentValue !== \"\") {\n vals[key] = \"\";\n }\n }\n let op = updatedData[row] == null ? \"$set\" : \"$merge\";\n spec[row] = { [op]: vals };\n }\n }\n return {\n updatedData: update(updatedData, spec),\n };\n });\n },\n onSelection(selection: Region[]) {\n set((state) => {\n if (\n selectionEquals(selection, state.selection) &&\n singleFocusedCell(selection) == null // Only if we're in a multi-cell selection mode\n ) {\n // If the selection is the same as the current selection, remove the selection.\n // In practice this only happens for whole-row and whole-column selections\n return {\n selection: [],\n focusedCell: null,\n topLeftCell: null,\n fillValueBaseCell: null,\n };\n }\n\n let spec = updateSelection(selection);\n if (state.fillValueBaseCell != null) {\n spec.updatedData = fillValues(state, selection);\n }\n return spec;\n });\n },\n onSelectionEdited(value: any) {\n // Apply the same value to all selected cells\n set((state) => {\n const { selection, updatedData, columnSpec, editable } = state;\n if (!editable) return {};\n let spec = {};\n for (const region of selection) {\n const { cols, rows } = region;\n const rowRange = range(rows ?? [0, updatedData.length - 1]);\n const colRange = range(cols ?? [0, columnSpec.length - 1]);\n for (const row of rowRange) {\n let vals = {};\n for (const col of colRange) {\n const key = columnSpec[col].key;\n vals[key] = value;\n }\n let op = updatedData[row] == null ? \"$set\" : \"$merge\";\n spec[row] = { [op]: vals };\n }\n }\n return {\n updatedData: update(updatedData, spec),\n };\n });\n },\n onColumnsReordered(\n oldIndex: number,\n newIndex: number,\n length: number,\n ) {\n set((state) => {\n if (!state.enableColumnReordering) return {};\n const { columnSpec } = state;\n const newSpec = [...columnSpec];\n const removed = newSpec.splice(oldIndex, length);\n newSpec.splice(newIndex, 0, ...removed);\n return { columnSpec: newSpec };\n });\n },\n scrollToRow(rowIndex: number) {\n if (tableRef.current == null) return;\n tableRef.current.scrollToRegion({\n rows: [rowIndex, rowIndex],\n });\n },\n };\n }),\n );\n });\n\n // Not sure how required this initialization is\n useEffect(() => {\n const { initialize } = store.getState();\n initialize({\n data,\n columnSpec: columnSpec ?? generateColumnSpec(data, columnSpecOptions),\n editable,\n enableColumnReordering,\n });\n }, [data, editable, columnSpec, columnSpecOptions, enableColumnReordering]);\n\n return h(DataSheetContext.Provider, { value: store }, children);\n}\n\nexport function useStoreAPI<T>(): StoreApi<DataSheetStore<T>> {\n const store = useContext(DataSheetContext);\n if (!store) {\n throw new Error(\"Missing DataSheetProvider\");\n }\n return store;\n}\n\nexport function useSelector<T = any, A = any>(\n selector: (state: DataSheetStore<T> & DataSheetComputedVals) => A,\n): A {\n const store = useStoreAPI<T>();\n return useStore(store, selector);\n}\n\nfunction updateSelection<T>(selection: Region[]) {\n const focusedCell = singleFocusedCell(selection);\n let spec: Partial<DataSheetState<T>> = {\n selection,\n focusedCell,\n topLeftCell: topLeftCell(selection),\n };\n if (focusedCell != null) {\n spec.fillValueBaseCell = null;\n }\n return spec;\n}\n\nfunction getLastRowIndex(regions: Region[]): number | null {\n /** Get the last row index from a selection of regions */\n if (regions == null || regions.length === 0) return null;\n let lastRow = -1;\n for (const region of regions) {\n const { rows } = region;\n if (rows == null || rows.length !== 2) continue;\n lastRow = Math.max(lastRow, rows[1]);\n }\n if (lastRow < 0) {\n /** If no valid rows are found, return null */\n return null;\n }\n return lastRow;\n}\n\nfunction getRowIndices(regions: Region[]): number[] {\n /** Get the row indices from a selection of regions */\n if (regions == null || regions.length === 0) return [];\n const rowIndices = new Set<number>();\n for (const region of regions) {\n const { rows } = region;\n if (rows == null || rows.length !== 2) continue;\n for (let i = rows[0]; i <= rows[1]; i++) {\n rowIndices.add(i);\n }\n }\n return Array.from(rowIndices).sort((a, b) => a - b);\n}\n\nexport function topLeftCell(\n regions: Region[],\n requireSolitaryCell: boolean = false,\n): FocusedCellCoordinates | null {\n /** Top left cell of a ranged selection */\n if (regions == null) return null;\n const [region] = regions;\n if (region == null) return null;\n const { cols, rows } = region;\n if (cols == null || rows == null) return null;\n if (requireSolitaryCell && (cols[0] !== cols[1] || rows[0] !== rows[1]))\n return null;\n return { col: cols[0], row: rows[0], focusSelectionIndex: 0 };\n}\n\nfunction selectionEquals(a: Region[], b: Region[]): boolean {\n /** Check if two selections are equal */\n if (a.length !== b.length) return false;\n for (let i = 0; i < a.length; i++) {\n const regionA = a[i];\n const regionB = b[i];\n\n const colsA = regionA.cols ?? [];\n const colsB = regionB.cols ?? [];\n\n const rowsA = regionA.rows ?? [];\n const rowsB = regionB.rows ?? [];\n\n if (\n colsA.length !== colsB.length ||\n rowsA.length !== rowsB.length ||\n colsA[0] !== colsB[0] ||\n colsA[1] !== colsB[1] ||\n rowsA[0] !== rowsB[0] ||\n rowsA[1] !== rowsB[1]\n ) {\n return false;\n }\n }\n return true;\n}\n\nexport function singleFocusedCell(\n sel: Region[],\n): FocusedCellCoordinates | null {\n /** Derive a single focused cell from a selected region, if possible */\n if (sel?.length !== 1) return null;\n return topLeftCell(sel, true);\n}\n\nfunction fillValues<T>(state: DataSheetStore<T>, selection: Region[]) {\n const { updatedData, columnSpec, editable, fillValueBaseCell, data } = state;\n\n // Prepare regions by unnesting columns\n let regions = selection.map((region) => {\n const { cols, rows } = region;\n // Get the first column (maybe should be the last)\n const [col] = cols;\n return { cols: [col, col], rows };\n });\n\n // Fill values downwards\n if (!editable || fillValueBaseCell == null) return updatedData;\n const { col, row } = fillValueBaseCell;\n const key = columnSpec[col].key;\n const value = updatedData[row]?.[key] ?? data[row][key];\n const spec = {};\n for (const region of regions) {\n const { cols, rows } = region;\n for (const row of range(rows)) {\n let op = updatedData[row] == null ? \"$set\" : \"$merge\";\n spec[row] = { [op]: { [key]: value } };\n }\n }\n return update(updatedData, spec);\n}\n"],"names":["topLeftCell","data","spec","columnSpec","editable","h","col","row"],"mappings":";;;;;;;;AAwEA,MAAM,mBAAmB,cAA6C,IAAI;AAoB1E,MAAM,WAAW;AAAA,EACf,CAAC,UAA0D;AACzD,UAAM,wBAAwB,kBAAkB,MAAM,SAAS,KAAK;AACpE,WAAO;AAAA,MACL,YAAY,MAAM,YAAY,SAAS,KAAK,MAAM,YAAY,OAAO;AAAA,MACrE,cAAc,MAAM,WAAW;AAAA,QAC7B,CAAC,QACC,MAAM,kBAAkB,IAAI,IAAI,GAAG,KACnC,IAA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
|
1
|
+
{"version":3,"file":"provider.js","sources":["../src/provider.ts"],"sourcesContent":["import { SetStateAction, useEffect, useRef, useState } from \"react\";\nimport h from \"@macrostrat/hyper\";\nimport { createStore, StoreApi, useStore } from \"zustand\";\nimport type { Region, Table } from \"@blueprintjs/table\";\nimport { generateColumnSpec } from \"./utils\";\nimport { createScopedStore } from \"@macrostrat/data-components\";\nimport {\n DataSheetProviderProps,\n DataSheetStore,\n VisibleCells,\n} from \"./types.ts\";\nimport { createZustandStore } from \"./zustand-store.ts\";\nimport { atomWithStore } from \"jotai-zustand\";\nimport { atom } from \"jotai\";\n\n/** Create a Jotai scoped store */\nconst scope = createScopedStore();\nconst { useAtom, useAtomValue, useSetAtom } = scope;\nexport { useAtom, useAtomValue, useSetAtom };\nexport { atom };\n\nexport const storeAPIAtom = atom<StoreApi<DataSheetStore<any>>>();\n\nconst storeWrapperAtom = atom((get) => {\n return atomWithStore(get(storeAPIAtom));\n});\n\n/** This is the basis for all other atoms that manipulate the store. */\nexport const storeAtom = atom(\n (get) => {\n return get(get(storeWrapperAtom));\n },\n (get, set, action: SetStateAction<T>) => {\n return set(get(storeWrapperAtom), action);\n },\n);\n\nconst initializeStoreAtom = atom(\n null,\n (get, set, payload: Partial<DataSheetStore<T>>) => {\n set(storeAtom, (state) => {\n return {\n ...state,\n ...payload,\n };\n });\n },\n);\n\nexport function DataSheetProvider<T>(props: DataSheetProviderProps<T>) {\n const [store] = useState(() => {\n return createStore<DataSheetStore<T>>(createZustandStore);\n });\n return h(\n scope.Provider,\n {\n atoms: [[storeAPIAtom, store]],\n },\n h(DataSheetProviderInner, props),\n );\n}\n\nexport function DataSheetProviderInner<T>({\n children,\n data,\n columnSpec,\n columnSpecOptions,\n editable,\n enableColumnReordering,\n defaultColumnWidth = 150,\n}: DataSheetProviderProps<T>) {\n const visibleCellsRef = useRef<VisibleCells>({\n rowIndexStart: 0,\n rowIndexEnd: 0,\n });\n\n const tableRef = useRef<Table>(null);\n\n const initializeStore = scope.useSetAtom(initializeStoreAtom);\n\n // Not sure how required this initialization is\n useEffect(() => {\n initializeStore({\n columnSpec: columnSpec ?? generateColumnSpec(data, columnSpecOptions),\n editable,\n enableColumnReordering,\n data,\n defaultColumnWidth,\n tableRef,\n visibleCellsRef,\n });\n }, [data, editable, columnSpec, columnSpecOptions, enableColumnReordering]);\n\n return children;\n}\n\nexport function useStoreAPI<T>(): StoreApi<DataSheetStore<T>> {\n const store = scope.useAtomValue(storeAPIAtom);\n if (!store) {\n throw new Error(\"Missing DataSheetProvider\");\n }\n return store;\n}\n\nexport function useSelector<T = any, A = any>(\n selector: (state: DataSheetStore<T> & DataSheetComputedVals) => A,\n): A {\n const store = useStoreAPI<T>();\n return useStore(store, selector);\n}\n"],"names":["h"],"mappings":";;;;;;;;;AAgBA,MAAM,QAAQ,kBAAA;AACd,MAAM,EAAE,SAAS,cAAc,eAAe;AAIvC,MAAM,eAAe,KAAA;AAE5B,MAAM,mBAAmB,KAAK,CAAC,QAAQ;AACrC,SAAO,cAAc,IAAI,YAAY,CAAC;AACxC,CAAC;AAGM,MAAM,YAAY;AAAA,EACvB,CAAC,QAAQ;AACP,WAAO,IAAI,IAAI,gBAAgB,CAAC;AAAA,EAClC;AAAA,EACA,CAAC,KAAK,KAAK,WAA8B;AACvC,WAAO,IAAI,IAAI,gBAAgB,GAAG,MAAM;AAAA,EAC1C;AACF;AAEA,MAAM,sBAAsB;AAAA,EAC1B;AAAA,EACA,CAAC,KAAK,KAAK,YAAwC;AACjD,QAAI,WAAW,CAAC,UAAU;AACxB,aAAO;AAAA,QACL,GAAG;AAAA,QACH,GAAG;AAAA,MAAA;AAAA,IAEP,CAAC;AAAA,EACH;AACF;AAEO,SAAS,kBAAqB,OAAkC;AACrE,QAAM,CAAC,KAAK,IAAI,SAAS,MAAM;AAC7B,WAAO,YAA+B,kBAAkB;AAAA,EAC1D,CAAC;AACD,SAAOA;AAAAA,IACL,MAAM;AAAA,IACN;AAAA,MACE,OAAO,CAAC,CAAC,cAAc,KAAK,CAAC;AAAA,IAAA;AAAA,IAE/BA,MAAE,wBAAwB,KAAK;AAAA,EAAA;AAEnC;AAEO,SAAS,uBAA0B;AAAA,EACxC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,qBAAqB;AACvB,GAA8B;AAC5B,QAAM,kBAAkB,OAAqB;AAAA,IAC3C,eAAe;AAAA,IACf,aAAa;AAAA,EAAA,CACd;AAED,QAAM,WAAW,OAAc,IAAI;AAEnC,QAAM,kBAAkB,MAAM,WAAW,mBAAmB;AAG5D,YAAU,MAAM;AACd,oBAAgB;AAAA,MACd,YAAY,cAAc,mBAAmB,MAAM,iBAAiB;AAAA,MACpE;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA,CACD;AAAA,EACH,GAAG,CAAC,MAAM,UAAU,YAAY,mBAAmB,sBAAsB,CAAC;AAE1E,SAAO;AACT;AAEO,SAAS,cAA8C;AAC5D,QAAM,QAAQ,MAAM,aAAa,YAAY;AAC7C,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,MAAM,2BAA2B;AAAA,EAC7C;AACA,SAAO;AACT;AAEO,SAAS,YACd,UACG;AACH,QAAM,QAAQ,YAAA;AACd,SAAO,SAAS,OAAO,QAAQ;AACjC;"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { FocusedCellCoordinates, Region, Table2 } from '@blueprintjs/table';
|
|
2
|
+
import { ColumnSpec, ColumnSpecOptions } from './utils';
|
|
3
|
+
export interface DataSheetCoreProps<T> {
|
|
4
|
+
data: T[];
|
|
5
|
+
columnSpec?: ColumnSpec[];
|
|
6
|
+
editable?: boolean;
|
|
7
|
+
enableColumnReordering?: boolean;
|
|
8
|
+
defaultColumnWidth?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface DataSheetState<T> {
|
|
11
|
+
selection: Region[];
|
|
12
|
+
columnSpec: ColumnSpec[];
|
|
13
|
+
fillValueBaseCell: FocusedCellCoordinates | null;
|
|
14
|
+
focusedCell: FocusedCellCoordinates | null;
|
|
15
|
+
topLeftCell: FocusedCellCoordinates | null;
|
|
16
|
+
deletedRows: Set<number>;
|
|
17
|
+
updatedData: T[];
|
|
18
|
+
initialized: boolean;
|
|
19
|
+
columnWidthsIndex: Map<string, number>;
|
|
20
|
+
}
|
|
21
|
+
type DataSheetVals<T> = DataSheetState<T> & DataSheetCoreProps<T>;
|
|
22
|
+
export type StateUpdater<T> = T[] | ((state: T[]) => T[]);
|
|
23
|
+
export interface DataSheetStoreMain<T> extends DataSheetVals<T> {
|
|
24
|
+
setSelection(selection: Region[]): void;
|
|
25
|
+
onDragValue(event: MouseEvent): void;
|
|
26
|
+
setUpdatedData(data: StateUpdater<T>): void;
|
|
27
|
+
onCellEdited(rowIndex: number, columnName: string, value: any): void;
|
|
28
|
+
onSelectionEdited(value: any): void;
|
|
29
|
+
onColumnsReordered(oldIndex: number, newIndex: number, length: number): void;
|
|
30
|
+
onColumnWidthChanged(columnIndex: number, newWidth: number): void;
|
|
31
|
+
moveFocusedCell(direction: "up" | "down" | "left" | "right"): void;
|
|
32
|
+
deleteSelectedRows(): void;
|
|
33
|
+
clearSelection(): void;
|
|
34
|
+
resetChanges(): void;
|
|
35
|
+
addRow(): void;
|
|
36
|
+
initialize(props: Partial<DataSheetStoreMain<T>>): void;
|
|
37
|
+
onSelection(selection: Region[]): void;
|
|
38
|
+
setVisibleCells(visibleCells: VisibleCells): void;
|
|
39
|
+
scrollToRow(rowIndex: number): void;
|
|
40
|
+
tableRef: React.MutableRefObject<Table2>;
|
|
41
|
+
visibleCellsRef: React.MutableRefObject<VisibleCells>;
|
|
42
|
+
columnWidthsIndex: Map<string, number>;
|
|
43
|
+
defaultColumnWidth: number;
|
|
44
|
+
}
|
|
45
|
+
export type DataSheetProviderProps<T> = DataSheetCoreProps<T> & {
|
|
46
|
+
children: React.ReactNode;
|
|
47
|
+
columnSpecOptions?: ColumnSpecOptions<T>;
|
|
48
|
+
};
|
|
49
|
+
export interface VisibleCells {
|
|
50
|
+
rowIndexStart: number;
|
|
51
|
+
rowIndexEnd: number;
|
|
52
|
+
}
|
|
53
|
+
export type DataSheetStore<T> = DataSheetStoreMain<T>;
|
|
54
|
+
export {};
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const provider = require("../provider.cjs");
|
|
4
|
+
const jotai = require("jotai");
|
|
5
|
+
const zustandStore = require("../zustand-store.cjs");
|
|
6
|
+
const tableKeyHandlerAtom = jotai.atom((get) => {
|
|
7
|
+
const store = get(provider.storeAPIAtom);
|
|
8
|
+
return (e) => {
|
|
9
|
+
const state = store.getState();
|
|
10
|
+
tableKeyHandler(e, state);
|
|
11
|
+
};
|
|
12
|
+
});
|
|
3
13
|
function tableKeyHandler(e, state) {
|
|
4
14
|
if (e.key === "Escape") {
|
|
5
15
|
state.setSelection([]);
|
|
@@ -30,6 +40,20 @@ const directionMap = {
|
|
|
30
40
|
ArrowLeft: "left",
|
|
31
41
|
ArrowRight: "right"
|
|
32
42
|
};
|
|
43
|
+
const selectionAtom = jotai.atom((get) => {
|
|
44
|
+
const store = get(provider.storeAtom);
|
|
45
|
+
return store.selection;
|
|
46
|
+
});
|
|
47
|
+
const isSingleCellSelectionAtom = jotai.atom((get) => {
|
|
48
|
+
const selection = get(selectionAtom);
|
|
49
|
+
return zustandStore.singleFocusedCell(selection) != null;
|
|
50
|
+
});
|
|
51
|
+
const editorKeyHandlerAtom = jotai.atom((get) => {
|
|
52
|
+
return (e) => {
|
|
53
|
+
const isSingleCellSelection = get(isSingleCellSelectionAtom);
|
|
54
|
+
editorKeyHandler(e, isSingleCellSelection);
|
|
55
|
+
};
|
|
56
|
+
});
|
|
33
57
|
function editorKeyHandler(e, isSingleCellSelection) {
|
|
34
58
|
let shouldCaptureEventForInlineEditing = true;
|
|
35
59
|
if (e.key == "Enter") {
|
|
@@ -79,7 +103,8 @@ function handleSpecialKeys(evt) {
|
|
|
79
103
|
}
|
|
80
104
|
return false;
|
|
81
105
|
}
|
|
82
|
-
exports.
|
|
106
|
+
exports.editorKeyHandlerAtom = editorKeyHandlerAtom;
|
|
83
107
|
exports.multiLineTextKeyHandler = multiLineTextKeyHandler;
|
|
84
108
|
exports.tableKeyHandler = tableKeyHandler;
|
|
109
|
+
exports.tableKeyHandlerAtom = tableKeyHandlerAtom;
|
|
85
110
|
//# sourceMappingURL=key-handling.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key-handling.cjs","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport { DataSheetStoreMain } from \"
|
|
1
|
+
{"version":3,"file":"key-handling.cjs","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport { storeAPIAtom, storeAtom } from \"../provider\";\nimport type { DataSheetStoreMain } from \"../types\";\nimport { atom } from \"jotai\";\nimport { singleFocusedCell } from \"../zustand-store.ts\";\n\nexport const tableKeyHandlerAtom = atom((get) => {\n const store = get(storeAPIAtom);\n return (e: KeyboardEvent) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n tableKeyHandler(e, state);\n };\n});\n\nexport function tableKeyHandler<T>(\n e: KeyboardEvent,\n state: DataSheetStoreMain<T>,\n) {\n // General key event\n if (e.key === \"Escape\") {\n // Clear selection on Escape\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }\n // Clear selection on Backspace or Delete\n if (e.key === \"Backspace\" || e.key === \"Delete\") {\n // Clear selection on Backspace or Delete\n state.clearSelection();\n e.preventDefault();\n }\n // Handle arrow keys for navigation\n if (e.key in directionMap) {\n // Handle arrow key navigation\n const direction = directionMap[e.key];\n state.moveFocusedCell(direction);\n // Prevent default scrolling behavior\n e.preventDefault();\n }\n if (e.key === \"Tab\") {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n e.preventDefault();\n }\n if (e.key === \"Enter\") {\n state.moveFocusedCell(\"down\");\n e.preventDefault();\n }\n}\n\nconst directionMap = {\n ArrowUp: \"up\",\n ArrowDown: \"down\",\n ArrowLeft: \"left\",\n ArrowRight: \"right\",\n};\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store.selection;\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n return (e: KeyboardEvent) => {\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(e: KeyboardEvent, isSingleCellSelection: boolean) {\n /** Get a key handler for inline cell editing */\n\n // if (e.key == \"Enter\" || e.key == \"Tab\") {\n // e.target.blur();\n // return;\n // }\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n e.target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = handleSpecialKeys(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n } else {\n e.target.blur();\n //e.target.parentNode.dispatchEvent(new KeyboardEvent(\"keydown\", e));\n }\n}\n\nexport function multiLineTextKeyHandler(evt: InputEvent) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = handleSpecialKeys(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n }\n}\n\nfunction handleSpecialKeys(evt: KeyboardEvent): boolean {\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n target.blur();\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n target.blur();\n return true;\n }\n\n return false;\n}\n"],"names":["atom","storeAPIAtom","storeAtom","singleFocusedCell"],"mappings":";;;;;AAMO,MAAM,sBAAsBA,MAAAA,KAAK,CAAC,QAAQ;AAC/C,QAAM,QAAQ,IAAIC,qBAAY;AAC9B,SAAO,CAAC,MAAqB;AAE3B,UAAM,QAAQ,MAAM,SAAA;AACpB,oBAAgB,GAAG,KAAK;AAAA,EAC1B;AACF,CAAC;AAEM,SAAS,gBACd,GACA,OACA;AAEA,MAAI,EAAE,QAAQ,UAAU;AAEtB,UAAM,aAAa,EAAE;AACrB,MAAE,eAAA;AACF,MAAE,gBAAA;AAAA,EACJ;AAEA,MAAI,EAAE,QAAQ,eAAe,EAAE,QAAQ,UAAU;AAE/C,UAAM,eAAA;AACN,MAAE,eAAA;AAAA,EACJ;AAEA,MAAI,EAAE,OAAO,cAAc;AAEzB,UAAM,YAAY,aAAa,EAAE,GAAG;AACpC,UAAM,gBAAgB,SAAS;AAE/B,MAAE,eAAA;AAAA,EACJ;AACA,MAAI,EAAE,QAAQ,OAAO;AACnB,UAAM,gBAAgB,EAAE,WAAW,SAAS,OAAO;AACnD,MAAE,eAAA;AAAA,EACJ;AACA,MAAI,EAAE,QAAQ,SAAS;AACrB,UAAM,gBAAgB,MAAM;AAC5B,MAAE,eAAA;AAAA,EACJ;AACF;AAEA,MAAM,eAAe;AAAA,EACnB,SAAS;AAAA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AAEA,SAAS,kBAAkB,KAA6B;AACtD,QAAM,SAAS,IAAI;AACnB,OACG,IAAI,QAAQ,eACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,GAC1B;AACA,WAAO,KAAA;AACP,WAAO;AAAA,EACT;AAEA,OACG,IAAI,QAAQ,gBACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,SACZ,IAAI,QAAQ,WACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,IAAI,OAAO,MAAM,QAC3C;AACA,WAAO,KAAA;AACP,WAAO;AAAA,EACT;AAEA,SAAO;AACT;;;;;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { KeyboardEvent } from 'react';
|
|
2
|
-
import { DataSheetStoreMain } from '
|
|
2
|
+
import { DataSheetStoreMain } from '../types';
|
|
3
|
+
export declare const tableKeyHandlerAtom: import('jotai').Atom<(e: KeyboardEvent) => void>;
|
|
3
4
|
export declare function tableKeyHandler<T>(e: KeyboardEvent, state: DataSheetStoreMain<T>): void;
|
|
4
|
-
export declare
|
|
5
|
+
export declare const editorKeyHandlerAtom: import('jotai').Atom<(e: KeyboardEvent) => void>;
|
|
5
6
|
export declare function multiLineTextKeyHandler(evt: InputEvent): void;
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import { storeAPIAtom, storeAtom } from "../provider.js";
|
|
2
|
+
import { atom } from "jotai";
|
|
3
|
+
import { singleFocusedCell } from "../zustand-store.js";
|
|
4
|
+
const tableKeyHandlerAtom = atom((get) => {
|
|
5
|
+
const store = get(storeAPIAtom);
|
|
6
|
+
return (e) => {
|
|
7
|
+
const state = store.getState();
|
|
8
|
+
tableKeyHandler(e, state);
|
|
9
|
+
};
|
|
10
|
+
});
|
|
1
11
|
function tableKeyHandler(e, state) {
|
|
2
12
|
if (e.key === "Escape") {
|
|
3
13
|
state.setSelection([]);
|
|
@@ -28,6 +38,20 @@ const directionMap = {
|
|
|
28
38
|
ArrowLeft: "left",
|
|
29
39
|
ArrowRight: "right"
|
|
30
40
|
};
|
|
41
|
+
const selectionAtom = atom((get) => {
|
|
42
|
+
const store = get(storeAtom);
|
|
43
|
+
return store.selection;
|
|
44
|
+
});
|
|
45
|
+
const isSingleCellSelectionAtom = atom((get) => {
|
|
46
|
+
const selection = get(selectionAtom);
|
|
47
|
+
return singleFocusedCell(selection) != null;
|
|
48
|
+
});
|
|
49
|
+
const editorKeyHandlerAtom = atom((get) => {
|
|
50
|
+
return (e) => {
|
|
51
|
+
const isSingleCellSelection = get(isSingleCellSelectionAtom);
|
|
52
|
+
editorKeyHandler(e, isSingleCellSelection);
|
|
53
|
+
};
|
|
54
|
+
});
|
|
31
55
|
function editorKeyHandler(e, isSingleCellSelection) {
|
|
32
56
|
let shouldCaptureEventForInlineEditing = true;
|
|
33
57
|
if (e.key == "Enter") {
|
|
@@ -78,8 +102,9 @@ function handleSpecialKeys(evt) {
|
|
|
78
102
|
return false;
|
|
79
103
|
}
|
|
80
104
|
export {
|
|
81
|
-
|
|
105
|
+
editorKeyHandlerAtom,
|
|
82
106
|
multiLineTextKeyHandler,
|
|
83
|
-
tableKeyHandler
|
|
107
|
+
tableKeyHandler,
|
|
108
|
+
tableKeyHandlerAtom
|
|
84
109
|
};
|
|
85
110
|
//# sourceMappingURL=key-handling.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key-handling.js","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport { DataSheetStoreMain } from \"
|
|
1
|
+
{"version":3,"file":"key-handling.js","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport { storeAPIAtom, storeAtom } from \"../provider\";\nimport type { DataSheetStoreMain } from \"../types\";\nimport { atom } from \"jotai\";\nimport { singleFocusedCell } from \"../zustand-store.ts\";\n\nexport const tableKeyHandlerAtom = atom((get) => {\n const store = get(storeAPIAtom);\n return (e: KeyboardEvent) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n tableKeyHandler(e, state);\n };\n});\n\nexport function tableKeyHandler<T>(\n e: KeyboardEvent,\n state: DataSheetStoreMain<T>,\n) {\n // General key event\n if (e.key === \"Escape\") {\n // Clear selection on Escape\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }\n // Clear selection on Backspace or Delete\n if (e.key === \"Backspace\" || e.key === \"Delete\") {\n // Clear selection on Backspace or Delete\n state.clearSelection();\n e.preventDefault();\n }\n // Handle arrow keys for navigation\n if (e.key in directionMap) {\n // Handle arrow key navigation\n const direction = directionMap[e.key];\n state.moveFocusedCell(direction);\n // Prevent default scrolling behavior\n e.preventDefault();\n }\n if (e.key === \"Tab\") {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n e.preventDefault();\n }\n if (e.key === \"Enter\") {\n state.moveFocusedCell(\"down\");\n e.preventDefault();\n }\n}\n\nconst directionMap = {\n ArrowUp: \"up\",\n ArrowDown: \"down\",\n ArrowLeft: \"left\",\n ArrowRight: \"right\",\n};\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store.selection;\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n return (e: KeyboardEvent) => {\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(e: KeyboardEvent, isSingleCellSelection: boolean) {\n /** Get a key handler for inline cell editing */\n\n // if (e.key == \"Enter\" || e.key == \"Tab\") {\n // e.target.blur();\n // return;\n // }\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n e.target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = handleSpecialKeys(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n } else {\n e.target.blur();\n //e.target.parentNode.dispatchEvent(new KeyboardEvent(\"keydown\", e));\n }\n}\n\nexport function multiLineTextKeyHandler(evt: InputEvent) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = handleSpecialKeys(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n }\n}\n\nfunction handleSpecialKeys(evt: KeyboardEvent): boolean {\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n target.blur();\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n target.blur();\n return true;\n }\n\n return false;\n}\n"],"names":[],"mappings":";;;AAMO,MAAM,sBAAsB,KAAK,CAAC,QAAQ;AAC/C,QAAM,QAAQ,IAAI,YAAY;AAC9B,SAAO,CAAC,MAAqB;AAE3B,UAAM,QAAQ,MAAM,SAAA;AACpB,oBAAgB,GAAG,KAAK;AAAA,EAC1B;AACF,CAAC;AAEM,SAAS,gBACd,GACA,OACA;AAEA,MAAI,EAAE,QAAQ,UAAU;AAEtB,UAAM,aAAa,EAAE;AACrB,MAAE,eAAA;AACF,MAAE,gBAAA;AAAA,EACJ;AAEA,MAAI,EAAE,QAAQ,eAAe,EAAE,QAAQ,UAAU;AAE/C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AAC;AAC3C,MAAI,CAAC,iBAAiB;AACpB,MAAE,gBAAA;AAAA,EACJ,OAAO;AACL,MAAE,OAAO,KAAA;AAAA,EAEX;AACF;AAEO,SAAS,wBAAwB,KAAiB;AACvD,MAAI,IAAI,QAAQ,SAAS;AAEvB,QAAI,gBAAA;AACJ;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,UAAU;AACxB,QAAI,eAAA;AACJ;AAAA,EACF;AAEA,QAAM,kBAAkB,kBAAkB,GAAG;AAC7C,MAAI,CAAC,iBAAiB;AACpB,QAAI,gBAAA;AAAA,EACN;AACF;AAEA,SAAS,kBAAkB,KAA6B;AACtD,QAAM,SAAS,IAAI;AACnB,OACG,IAAI,QAAQ,eACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,GAC1B;AACA,WAAO,KAAA;AACP,WAAO;AAAA,EACT;AAEA,OACG,IAAI,QAAQ,gBACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,SACZ,IAAI,QAAQ,WACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,IAAI,OAAO,MAAM,QAC3C;AACA,WAAO,KAAA;AACP,WAAO;AAAA,EACT;AAEA,SAAO;AACT;"}
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const index = require("./utils/index.cjs");
|
|
4
|
+
const update = require("immutability-helper");
|
|
5
|
+
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
|
|
6
|
+
const update__default = /* @__PURE__ */ _interopDefault(update);
|
|
7
|
+
function createZustandStore(set, get) {
|
|
8
|
+
return {
|
|
9
|
+
data: [],
|
|
10
|
+
columnSpec: [],
|
|
11
|
+
defaultColumnWidth: 150,
|
|
12
|
+
editable: false,
|
|
13
|
+
deletedRows: /* @__PURE__ */ new Set(),
|
|
14
|
+
selection: [],
|
|
15
|
+
fillValueBaseCell: null,
|
|
16
|
+
updatedData: [],
|
|
17
|
+
focusedCell: null,
|
|
18
|
+
topLeftCell: null,
|
|
19
|
+
initialized: false,
|
|
20
|
+
tableRef: null,
|
|
21
|
+
visibleCellsRef: null,
|
|
22
|
+
// This is a placeholder
|
|
23
|
+
enableColumnReordering: false,
|
|
24
|
+
setSelection(selection) {
|
|
25
|
+
set(updateSelection(selection));
|
|
26
|
+
},
|
|
27
|
+
columnWidthsIndex: /* @__PURE__ */ new Map(),
|
|
28
|
+
moveFocusedCell(direction) {
|
|
29
|
+
set((state) => {
|
|
30
|
+
const { topLeftCell: topLeftCell2 } = state;
|
|
31
|
+
if (topLeftCell2 == null) return {};
|
|
32
|
+
let { col, row } = topLeftCell2;
|
|
33
|
+
switch (direction) {
|
|
34
|
+
case "up":
|
|
35
|
+
row = Math.max(0, row - 1);
|
|
36
|
+
break;
|
|
37
|
+
case "down":
|
|
38
|
+
row = Math.min(
|
|
39
|
+
row + 1,
|
|
40
|
+
Math.max(state.data.length, state.updatedData.length) - 1
|
|
41
|
+
);
|
|
42
|
+
break;
|
|
43
|
+
case "left":
|
|
44
|
+
col = Math.max(0, col - 1);
|
|
45
|
+
break;
|
|
46
|
+
case "right":
|
|
47
|
+
col = Math.min(col + 1, state.columnSpec.length - 1);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
const region = { cols: [col, col], rows: [row, row] };
|
|
51
|
+
return updateSelection([region]);
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
addRow(row = {}) {
|
|
55
|
+
set((state) => {
|
|
56
|
+
const { updatedData, data, selection } = state;
|
|
57
|
+
const lastRowIndex = getLastRowIndex(selection) ?? Math.max(data.length, updatedData.length) - 1;
|
|
58
|
+
const spec = {
|
|
59
|
+
$splice: [[lastRowIndex + 1, 0, row]]
|
|
60
|
+
};
|
|
61
|
+
console.log(spec);
|
|
62
|
+
return {
|
|
63
|
+
updatedData: update__default.default(updatedData, spec)
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
setUpdatedData(data) {
|
|
68
|
+
if (Array.isArray(data)) {
|
|
69
|
+
set({ updatedData: data });
|
|
70
|
+
} else {
|
|
71
|
+
set((state) => {
|
|
72
|
+
return { updatedData: data(state.updatedData) };
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
deleteSelectedRows() {
|
|
77
|
+
set((state) => {
|
|
78
|
+
const { selection, deletedRows } = state;
|
|
79
|
+
const rowIndices = getRowIndices(selection);
|
|
80
|
+
const newDeletedRows = new Set(deletedRows);
|
|
81
|
+
for (const rowIndex of rowIndices) {
|
|
82
|
+
newDeletedRows.add(rowIndex);
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
deletedRows: newDeletedRows,
|
|
86
|
+
selection: [],
|
|
87
|
+
focusedCell: null,
|
|
88
|
+
topLeftCell: null,
|
|
89
|
+
fillValueBaseCell: null
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
resetChanges() {
|
|
94
|
+
set((state) => ({
|
|
95
|
+
updatedData: [],
|
|
96
|
+
deletedRows: /* @__PURE__ */ new Set(),
|
|
97
|
+
columnWidths: /* @__PURE__ */ new Map(),
|
|
98
|
+
selection: [],
|
|
99
|
+
focusedCell: null,
|
|
100
|
+
topLeftCell: null,
|
|
101
|
+
fillValueBaseCell: null
|
|
102
|
+
}));
|
|
103
|
+
},
|
|
104
|
+
onColumnWidthChanged(columnIx, newWidth) {
|
|
105
|
+
set((state) => {
|
|
106
|
+
const { columnSpec, columnWidthsIndex } = state;
|
|
107
|
+
const colKey = columnSpec[columnIx].key;
|
|
108
|
+
const newColumnWidths = new Map(columnWidthsIndex);
|
|
109
|
+
newColumnWidths.set(colKey, newWidth);
|
|
110
|
+
return { columnWidthsIndex: newColumnWidths };
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
setVisibleCells(visibleCells) {
|
|
114
|
+
const ref = get().visibleCellsRef;
|
|
115
|
+
if (ref == null) return;
|
|
116
|
+
ref.current = visibleCells;
|
|
117
|
+
},
|
|
118
|
+
onDragValue(event) {
|
|
119
|
+
set((state) => {
|
|
120
|
+
return { fillValueBaseCell: state.focusedCell };
|
|
121
|
+
});
|
|
122
|
+
event.preventDefault();
|
|
123
|
+
},
|
|
124
|
+
onCellEdited(rowIndex, columnName, value) {
|
|
125
|
+
set((state) => {
|
|
126
|
+
const { editable, updatedData, data } = state;
|
|
127
|
+
if (!editable) return {};
|
|
128
|
+
let rowSpec;
|
|
129
|
+
if (value !== data[rowIndex]?.[columnName]) {
|
|
130
|
+
const rowOp = updatedData[rowIndex] != null ? "$merge" : "$set";
|
|
131
|
+
rowSpec = { [rowOp]: { [columnName]: value } };
|
|
132
|
+
} else {
|
|
133
|
+
rowSpec = { $unset: [columnName] };
|
|
134
|
+
}
|
|
135
|
+
const spec = { [rowIndex]: rowSpec };
|
|
136
|
+
return { updatedData: update__default.default(updatedData, spec) };
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
initialize(props) {
|
|
140
|
+
set({ ...props, initialized: true });
|
|
141
|
+
},
|
|
142
|
+
clearSelection() {
|
|
143
|
+
set((state) => {
|
|
144
|
+
const { selection, updatedData, columnSpec, data } = state;
|
|
145
|
+
let spec = {};
|
|
146
|
+
for (const region of selection) {
|
|
147
|
+
console.log("Clearing region", region);
|
|
148
|
+
const { cols, rows } = region;
|
|
149
|
+
const rowRange = index.range(rows ?? [0, updatedData.length - 1]);
|
|
150
|
+
const colRange = index.range(cols ?? [0, columnSpec.length - 1]);
|
|
151
|
+
for (const row of rowRange) {
|
|
152
|
+
let vals = {};
|
|
153
|
+
for (const col of colRange) {
|
|
154
|
+
const key = columnSpec[col].key;
|
|
155
|
+
const currentValue = updatedData[row]?.[key] ?? data[row]?.[key];
|
|
156
|
+
if (currentValue != null && currentValue !== "") {
|
|
157
|
+
vals[key] = "";
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
let op = updatedData[row] == null ? "$set" : "$merge";
|
|
161
|
+
spec[row] = { [op]: vals };
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
updatedData: update__default.default(updatedData, spec)
|
|
166
|
+
};
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
onSelection(selection) {
|
|
170
|
+
set((state) => {
|
|
171
|
+
if (selectionEquals(selection, state.selection) && singleFocusedCell(selection) == null) {
|
|
172
|
+
return {
|
|
173
|
+
selection: [],
|
|
174
|
+
focusedCell: null,
|
|
175
|
+
topLeftCell: null,
|
|
176
|
+
fillValueBaseCell: null
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
let spec = updateSelection(selection);
|
|
180
|
+
if (state.fillValueBaseCell != null) {
|
|
181
|
+
spec.updatedData = fillValues(state, selection);
|
|
182
|
+
}
|
|
183
|
+
return spec;
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
onSelectionEdited(value) {
|
|
187
|
+
set((state) => {
|
|
188
|
+
const { selection, updatedData, columnSpec, editable } = state;
|
|
189
|
+
if (!editable) return {};
|
|
190
|
+
let spec = {};
|
|
191
|
+
for (const region of selection) {
|
|
192
|
+
const { cols, rows } = region;
|
|
193
|
+
const rowRange = index.range(rows ?? [0, updatedData.length - 1]);
|
|
194
|
+
const colRange = index.range(cols ?? [0, columnSpec.length - 1]);
|
|
195
|
+
for (const row of rowRange) {
|
|
196
|
+
let vals = {};
|
|
197
|
+
for (const col of colRange) {
|
|
198
|
+
const key = columnSpec[col].key;
|
|
199
|
+
vals[key] = value;
|
|
200
|
+
}
|
|
201
|
+
let op = updatedData[row] == null ? "$set" : "$merge";
|
|
202
|
+
spec[row] = { [op]: vals };
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
updatedData: update__default.default(updatedData, spec)
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
onColumnsReordered(oldIndex, newIndex, length) {
|
|
211
|
+
set((state) => {
|
|
212
|
+
if (!state.enableColumnReordering) return {};
|
|
213
|
+
const { columnSpec } = state;
|
|
214
|
+
const newSpec = [...columnSpec];
|
|
215
|
+
const removed = newSpec.splice(oldIndex, length);
|
|
216
|
+
newSpec.splice(newIndex, 0, ...removed);
|
|
217
|
+
return { columnSpec: newSpec };
|
|
218
|
+
});
|
|
219
|
+
},
|
|
220
|
+
scrollToRow(rowIndex) {
|
|
221
|
+
const tableRef = get().tableRef;
|
|
222
|
+
if (tableRef.current == null) return;
|
|
223
|
+
tableRef.current.scrollToRegion({
|
|
224
|
+
rows: [rowIndex, rowIndex]
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function updateSelection(selection) {
|
|
230
|
+
const focusedCell = singleFocusedCell(selection);
|
|
231
|
+
let spec = {
|
|
232
|
+
selection,
|
|
233
|
+
focusedCell,
|
|
234
|
+
topLeftCell: topLeftCell(selection)
|
|
235
|
+
};
|
|
236
|
+
if (focusedCell != null) {
|
|
237
|
+
spec.fillValueBaseCell = null;
|
|
238
|
+
}
|
|
239
|
+
return spec;
|
|
240
|
+
}
|
|
241
|
+
function getLastRowIndex(regions) {
|
|
242
|
+
if (regions == null || regions.length === 0) return null;
|
|
243
|
+
let lastRow = -1;
|
|
244
|
+
for (const region of regions) {
|
|
245
|
+
const { rows } = region;
|
|
246
|
+
if (rows == null || rows.length !== 2) continue;
|
|
247
|
+
lastRow = Math.max(lastRow, rows[1]);
|
|
248
|
+
}
|
|
249
|
+
if (lastRow < 0) {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
return lastRow;
|
|
253
|
+
}
|
|
254
|
+
function getRowIndices(regions) {
|
|
255
|
+
if (regions == null || regions.length === 0) return [];
|
|
256
|
+
const rowIndices = /* @__PURE__ */ new Set();
|
|
257
|
+
for (const region of regions) {
|
|
258
|
+
const { rows } = region;
|
|
259
|
+
if (rows == null || rows.length !== 2) continue;
|
|
260
|
+
for (let i = rows[0]; i <= rows[1]; i++) {
|
|
261
|
+
rowIndices.add(i);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return Array.from(rowIndices).sort((a, b) => a - b);
|
|
265
|
+
}
|
|
266
|
+
function topLeftCell(regions, requireSolitaryCell = false) {
|
|
267
|
+
if (regions == null) return null;
|
|
268
|
+
const [region] = regions;
|
|
269
|
+
if (region == null) return null;
|
|
270
|
+
const { cols, rows } = region;
|
|
271
|
+
if (cols == null || rows == null) return null;
|
|
272
|
+
if (requireSolitaryCell && (cols[0] !== cols[1] || rows[0] !== rows[1]))
|
|
273
|
+
return null;
|
|
274
|
+
return { col: cols[0], row: rows[0], focusSelectionIndex: 0 };
|
|
275
|
+
}
|
|
276
|
+
function selectionEquals(a, b) {
|
|
277
|
+
if (a.length !== b.length) return false;
|
|
278
|
+
for (let i = 0; i < a.length; i++) {
|
|
279
|
+
const regionA = a[i];
|
|
280
|
+
const regionB = b[i];
|
|
281
|
+
const colsA = regionA.cols ?? [];
|
|
282
|
+
const colsB = regionB.cols ?? [];
|
|
283
|
+
const rowsA = regionA.rows ?? [];
|
|
284
|
+
const rowsB = regionB.rows ?? [];
|
|
285
|
+
if (colsA.length !== colsB.length || rowsA.length !== rowsB.length || colsA[0] !== colsB[0] || colsA[1] !== colsB[1] || rowsA[0] !== rowsB[0] || rowsA[1] !== rowsB[1]) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
function singleFocusedCell(sel) {
|
|
292
|
+
if (sel?.length !== 1) return null;
|
|
293
|
+
return topLeftCell(sel, true);
|
|
294
|
+
}
|
|
295
|
+
function fillValues(state, selection) {
|
|
296
|
+
const { updatedData, columnSpec, editable, fillValueBaseCell, data } = state;
|
|
297
|
+
let regions = selection.map((region) => {
|
|
298
|
+
const { cols, rows } = region;
|
|
299
|
+
const [col2] = cols;
|
|
300
|
+
return { cols: [col2, col2], rows };
|
|
301
|
+
});
|
|
302
|
+
if (!editable || fillValueBaseCell == null) return updatedData;
|
|
303
|
+
const { col, row } = fillValueBaseCell;
|
|
304
|
+
const key = columnSpec[col].key;
|
|
305
|
+
const value = updatedData[row]?.[key] ?? data[row][key];
|
|
306
|
+
const spec = {};
|
|
307
|
+
for (const region of regions) {
|
|
308
|
+
const { cols, rows } = region;
|
|
309
|
+
for (const row2 of index.range(rows)) {
|
|
310
|
+
let op = updatedData[row2] == null ? "$set" : "$merge";
|
|
311
|
+
spec[row2] = { [op]: { [key]: value } };
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return update__default.default(updatedData, spec);
|
|
315
|
+
}
|
|
316
|
+
exports.createZustandStore = createZustandStore;
|
|
317
|
+
exports.singleFocusedCell = singleFocusedCell;
|
|
318
|
+
exports.topLeftCell = topLeftCell;
|
|
319
|
+
exports.updateSelection = updateSelection;
|
|
320
|
+
//# sourceMappingURL=zustand-store.cjs.map
|