Merge pull request #15271 from Budibase/typing/stores-grid-reorder
Typing grid reorder store
This commit is contained in:
commit
5019b0eb38
|
@ -14,7 +14,9 @@ export const getCellID = (rowId: string, fieldName: string) => {
|
||||||
return `${rowId}${CellIDSeparator}${fieldName}`
|
return `${rowId}${CellIDSeparator}${fieldName}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export const parseEventLocation = (e: MouseEvent & TouchEvent) => {
|
export const parseEventLocation = (event: MouseEvent | TouchEvent) => {
|
||||||
|
const e = event as MouseEvent & TouchEvent
|
||||||
|
|
||||||
return {
|
return {
|
||||||
x: e.clientX ?? e.touches?.[0]?.clientX,
|
x: e.clientX ?? e.touches?.[0]?.clientX,
|
||||||
y: e.clientY ?? e.touches?.[0]?.clientY,
|
y: e.clientY ?? e.touches?.[0]?.clientY,
|
||||||
|
|
|
@ -190,6 +190,7 @@ export const initialise = (context: StoreContext) => {
|
||||||
related: fieldSchema.related,
|
related: fieldSchema.related,
|
||||||
calculationType: fieldSchema.calculationType,
|
calculationType: fieldSchema.calculationType,
|
||||||
__left: undefined as any, // TODO
|
__left: undefined as any, // TODO
|
||||||
|
__idx: undefined as any, // TODO
|
||||||
}
|
}
|
||||||
// Override a few properties for primary display
|
// Override a few properties for primary display
|
||||||
if (field === primaryDisplay) {
|
if (field === primaryDisplay) {
|
||||||
|
|
|
@ -84,7 +84,8 @@ export type Store = BaseStore &
|
||||||
fixedRowHeight: Writable<number>
|
fixedRowHeight: Writable<number>
|
||||||
bounds: Readable<any>
|
bounds: Readable<any>
|
||||||
height: Readable<number>
|
height: Readable<number>
|
||||||
} & Rows.Store
|
} & Rows.Store &
|
||||||
|
Reorder.Store
|
||||||
|
|
||||||
export const attachStores = (context: Store): Store => {
|
export const attachStores = (context: Store): Store => {
|
||||||
// Atomic store creation
|
// Atomic store creation
|
||||||
|
|
|
@ -1,7 +1,24 @@
|
||||||
import { get, writable, derived } from "svelte/store"
|
import { get, writable, derived, Writable, Readable } from "svelte/store"
|
||||||
import { parseEventLocation } from "../lib/utils"
|
import { parseEventLocation } from "../lib/utils"
|
||||||
|
import { Store as StoreContext } from "."
|
||||||
|
|
||||||
const reorderInitialState = {
|
interface Breakpoint {
|
||||||
|
x: number
|
||||||
|
column: string
|
||||||
|
insertAfter: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ReorderInitialStoreData {
|
||||||
|
sourceColumn: string | null
|
||||||
|
targetColumn: string | null
|
||||||
|
insertAfter?: boolean
|
||||||
|
breakpoints: Breakpoint[]
|
||||||
|
gridLeft: number
|
||||||
|
width: number
|
||||||
|
increment?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
const reorderInitialState: ReorderInitialStoreData = {
|
||||||
sourceColumn: null,
|
sourceColumn: null,
|
||||||
targetColumn: null,
|
targetColumn: null,
|
||||||
insertAfter: false,
|
insertAfter: false,
|
||||||
|
@ -11,7 +28,14 @@ const reorderInitialState = {
|
||||||
increment: 0,
|
increment: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createStores = () => {
|
interface ReorderInitialStore {
|
||||||
|
reorder: Writable<ReorderInitialStoreData>
|
||||||
|
isReordering: Readable<boolean>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Store = ReorderInitialStore
|
||||||
|
|
||||||
|
export const createStores = (): ReorderInitialStore => {
|
||||||
const reorder = writable(reorderInitialState)
|
const reorder = writable(reorderInitialState)
|
||||||
const isReordering = derived(
|
const isReordering = derived(
|
||||||
reorder,
|
reorder,
|
||||||
|
@ -24,7 +48,7 @@ export const createStores = () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createActions = context => {
|
export const createActions = (context: StoreContext) => {
|
||||||
const {
|
const {
|
||||||
reorder,
|
reorder,
|
||||||
columns,
|
columns,
|
||||||
|
@ -40,11 +64,11 @@ export const createActions = context => {
|
||||||
maxScrollLeft,
|
maxScrollLeft,
|
||||||
} = context
|
} = context
|
||||||
let latestX = 0
|
let latestX = 0
|
||||||
let autoScrollInterval
|
let autoScrollInterval: NodeJS.Timeout
|
||||||
let isAutoScrolling
|
let isAutoScrolling: boolean
|
||||||
|
|
||||||
// Callback when dragging on a colum header and starting reordering
|
// Callback when dragging on a colum header and starting reordering
|
||||||
const startReordering = (column, e) => {
|
const startReordering = (column: string, e: MouseEvent | TouchEvent) => {
|
||||||
const $scrollableColumns = get(scrollableColumns)
|
const $scrollableColumns = get(scrollableColumns)
|
||||||
const $bounds = get(bounds)
|
const $bounds = get(bounds)
|
||||||
const $stickyWidth = get(stickyWidth)
|
const $stickyWidth = get(stickyWidth)
|
||||||
|
@ -87,7 +111,7 @@ export const createActions = context => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Callback when moving the mouse when reordering columns
|
// Callback when moving the mouse when reordering columns
|
||||||
const onReorderMouseMove = e => {
|
const onReorderMouseMove = (e: MouseEvent | TouchEvent) => {
|
||||||
// Immediately handle the current position
|
// Immediately handle the current position
|
||||||
const { x } = parseEventLocation(e)
|
const { x } = parseEventLocation(e)
|
||||||
latestX = x
|
latestX = x
|
||||||
|
@ -122,7 +146,7 @@ export const createActions = context => {
|
||||||
const $scrollLeft = get(scrollLeft)
|
const $scrollLeft = get(scrollLeft)
|
||||||
|
|
||||||
// Compute the closest breakpoint to the current position
|
// Compute the closest breakpoint to the current position
|
||||||
let breakpoint
|
let breakpoint: Breakpoint | undefined
|
||||||
let minDistance = Number.MAX_SAFE_INTEGER
|
let minDistance = Number.MAX_SAFE_INTEGER
|
||||||
const mouseX = latestX - $reorder.gridLeft + $scrollLeft
|
const mouseX = latestX - $reorder.gridLeft + $scrollLeft
|
||||||
$reorder.breakpoints.forEach(point => {
|
$reorder.breakpoints.forEach(point => {
|
||||||
|
@ -139,8 +163,8 @@ export const createActions = context => {
|
||||||
) {
|
) {
|
||||||
reorder.update(state => ({
|
reorder.update(state => ({
|
||||||
...state,
|
...state,
|
||||||
targetColumn: breakpoint.column,
|
targetColumn: breakpoint!.column,
|
||||||
insertAfter: breakpoint.insertAfter,
|
insertAfter: breakpoint!.insertAfter,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -157,7 +181,7 @@ export const createActions = context => {
|
||||||
const { increment } = get(reorder)
|
const { increment } = get(reorder)
|
||||||
scroll.update(state => ({
|
scroll.update(state => ({
|
||||||
...state,
|
...state,
|
||||||
left: Math.max(0, Math.min($maxLeft, state.left + increment)),
|
left: Math.max(0, Math.min($maxLeft, state.left + increment!)),
|
||||||
}))
|
}))
|
||||||
considerReorderPosition()
|
considerReorderPosition()
|
||||||
}, 10)
|
}, 10)
|
||||||
|
@ -185,7 +209,11 @@ export const createActions = context => {
|
||||||
const { sourceColumn, targetColumn, insertAfter } = get(reorder)
|
const { sourceColumn, targetColumn, insertAfter } = get(reorder)
|
||||||
reorder.set(reorderInitialState)
|
reorder.set(reorderInitialState)
|
||||||
if (sourceColumn !== targetColumn) {
|
if (sourceColumn !== targetColumn) {
|
||||||
await moveColumn({ sourceColumn, targetColumn, insertAfter })
|
await moveColumn({
|
||||||
|
sourceColumn: sourceColumn!,
|
||||||
|
targetColumn: targetColumn!,
|
||||||
|
insertAfter,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,6 +223,10 @@ export const createActions = context => {
|
||||||
sourceColumn,
|
sourceColumn,
|
||||||
targetColumn,
|
targetColumn,
|
||||||
insertAfter = false,
|
insertAfter = false,
|
||||||
|
}: {
|
||||||
|
sourceColumn: string
|
||||||
|
targetColumn: string
|
||||||
|
insertAfter?: boolean
|
||||||
}) => {
|
}) => {
|
||||||
// Find the indices in the overall columns array
|
// Find the indices in the overall columns array
|
||||||
const $columns = get(columns)
|
const $columns = get(columns)
|
||||||
|
@ -232,7 +264,7 @@ export const createActions = context => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Moves a column one place left (as appears visually)
|
// Moves a column one place left (as appears visually)
|
||||||
const moveColumnLeft = async column => {
|
const moveColumnLeft = async (column: string) => {
|
||||||
const $visibleColumns = get(visibleColumns)
|
const $visibleColumns = get(visibleColumns)
|
||||||
const $columnLookupMap = get(columnLookupMap)
|
const $columnLookupMap = get(columnLookupMap)
|
||||||
const sourceIdx = $columnLookupMap[column].__idx
|
const sourceIdx = $columnLookupMap[column].__idx
|
||||||
|
@ -243,7 +275,7 @@ export const createActions = context => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Moves a column one place right (as appears visually)
|
// Moves a column one place right (as appears visually)
|
||||||
const moveColumnRight = async column => {
|
const moveColumnRight = async (column: string) => {
|
||||||
const $visibleColumns = get(visibleColumns)
|
const $visibleColumns = get(visibleColumns)
|
||||||
const $columnLookupMap = get(columnLookupMap)
|
const $columnLookupMap = get(columnLookupMap)
|
||||||
const sourceIdx = $columnLookupMap[column].__idx
|
const sourceIdx = $columnLookupMap[column].__idx
|
|
@ -16,7 +16,7 @@ export type UIColumn = FieldSchema & {
|
||||||
autocolumn: boolean
|
autocolumn: boolean
|
||||||
}
|
}
|
||||||
calculationType: CalculationType
|
calculationType: CalculationType
|
||||||
__idx?: number
|
__idx: number
|
||||||
__left: number
|
__left: number
|
||||||
width: number
|
width: number
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,4 +8,5 @@ export interface UIFieldMutation {
|
||||||
visible?: boolean
|
visible?: boolean
|
||||||
readonly?: boolean
|
readonly?: boolean
|
||||||
width?: number
|
width?: number
|
||||||
|
order?: number
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue