Support reordering on mobile

This commit is contained in:
Andrew Kingston 2024-04-17 11:27:27 +01:00
parent b529e98b05
commit ea3cd0cd9b
2 changed files with 23 additions and 8 deletions

View File

@ -18,7 +18,6 @@
export let column export let column
export let idx export let idx
export let orderable = true
const { const {
reorder, reorder,
@ -66,6 +65,7 @@
$: resetSearchValue(column.name) $: resetSearchValue(column.name)
$: searching = searchValue != null $: searching = searchValue != null
$: debouncedUpdateFilter(searchValue) $: debouncedUpdateFilter(searchValue)
$: orderable = !column.primaryDisplay
const getSortingLabels = type => { const getSortingLabels = type => {
switch (type) { switch (type) {
@ -112,7 +112,7 @@
} }
const onMouseDown = e => { const onMouseDown = e => {
if (e.button === 0 && orderable) { if ((e.touches?.length || e.button === 0) && orderable) {
timeout = setTimeout(() => { timeout = setTimeout(() => {
reorder.actions.startReordering(column.name, e) reorder.actions.startReordering(column.name, e)
}, 200) }, 200)
@ -120,7 +120,7 @@
} }
const onMouseUp = e => { const onMouseUp = e => {
if (e.button === 0 && orderable) { if ((e.touches?.length || e.button === 0) && orderable) {
clearTimeout(timeout) clearTimeout(timeout)
} }
} }
@ -258,6 +258,9 @@
<GridCell <GridCell
on:mousedown={onMouseDown} on:mousedown={onMouseDown}
on:mouseup={onMouseUp} on:mouseup={onMouseUp}
on:touchstart={onMouseDown}
on:touchend={onMouseUp}
on:touchcancel={onMouseUp}
on:contextmenu={onContextMenu} on:contextmenu={onContextMenu}
width={column.width} width={column.width}
left={column.left} left={column.left}
@ -347,7 +350,8 @@
<MenuItem <MenuItem
icon="Label" icon="Label"
on:click={makeDisplayColumn} on:click={makeDisplayColumn}
disabled={idx === "sticky" || !canBeDisplayColumn(column.schema.type)} disabled={column.primaryDisplay ||
!canBeDisplayColumn(column.schema.type)}
> >
Use as display column Use as display column
</MenuItem> </MenuItem>
@ -378,7 +382,7 @@
Move right Move right
</MenuItem> </MenuItem>
<MenuItem <MenuItem
disabled={idx === "sticky" || !$config.showControls} disabled={column.primaryDisplay || !$config.showControls}
icon="VisibilityOff" icon="VisibilityOff"
on:click={hideColumn} on:click={hideColumn}
> >

View File

@ -40,6 +40,7 @@ export const createActions = context => {
// 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, e) => {
console.log("start", column)
const $visibleColumns = get(visibleColumns) const $visibleColumns = get(visibleColumns)
const $bounds = get(bounds) const $bounds = get(bounds)
const $stickyColumn = get(stickyColumn) const $stickyColumn = get(stickyColumn)
@ -55,6 +56,11 @@ export const createActions = context => {
x: 0, x: 0,
column: $stickyColumn.name, column: $stickyColumn.name,
}) })
} else if (!$visibleColumns[0].primaryDisplay) {
breakpoints.unshift({
x: 0,
column: null,
})
} }
// Update state // Update state
@ -69,6 +75,9 @@ export const createActions = context => {
// Add listeners to handle mouse movement // Add listeners to handle mouse movement
document.addEventListener("mousemove", onReorderMouseMove) document.addEventListener("mousemove", onReorderMouseMove)
document.addEventListener("mouseup", stopReordering) document.addEventListener("mouseup", stopReordering)
document.addEventListener("touchmove", onReorderMouseMove)
document.addEventListener("touchend", stopReordering)
document.addEventListener("touchcancel", stopReordering)
// Trigger a move event immediately so ensure a candidate column is chosen // Trigger a move event immediately so ensure a candidate column is chosen
onReorderMouseMove(e) onReorderMouseMove(e)
@ -77,7 +86,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 => {
// Immediately handle the current position // Immediately handle the current position
const x = e.clientX const x = e.clientX ?? e.touches?.[0]?.clientX
reorder.update(state => ({ reorder.update(state => ({
...state, ...state,
latestX: x, latestX: x,
@ -168,6 +177,9 @@ export const createActions = context => {
// Remove event handlers // Remove event handlers
document.removeEventListener("mousemove", onReorderMouseMove) document.removeEventListener("mousemove", onReorderMouseMove)
document.removeEventListener("mouseup", stopReordering) document.removeEventListener("mouseup", stopReordering)
document.removeEventListener("touchmove", onReorderMouseMove)
document.removeEventListener("touchend", stopReordering)
document.removeEventListener("touchcancel", stopReordering)
// Save column changes // Save column changes
await columns.actions.saveChanges() await columns.actions.saveChanges()
@ -185,8 +197,7 @@ export const createActions = context => {
if (--targetIdx < sourceIdx) { if (--targetIdx < sourceIdx) {
targetIdx++ targetIdx++
} }
state.splice(targetIdx, 0, removed[0]) return state.toSpliced(targetIdx, 0, removed[0])
return state.slice()
}) })
} }