From 42538e114ae48b90ea039f9a8264dcc170567887 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 24 Jun 2024 13:03:33 +0100 Subject: [PATCH] Fix reorder --- .../grid/overlays/ReorderOverlay.svelte | 22 ++++------ .../grid/overlays/ResizeOverlay.svelte | 29 ++++--------- .../src/components/grid/stores/columns.js | 10 ++--- .../src/components/grid/stores/reorder.js | 41 +++++++++++++------ .../src/components/grid/stores/scroll.js | 31 +++++++------- 5 files changed, 64 insertions(+), 69 deletions(-) diff --git a/packages/frontend-core/src/components/grid/overlays/ReorderOverlay.svelte b/packages/frontend-core/src/components/grid/overlays/ReorderOverlay.svelte index 656065ba74..b14db312bf 100644 --- a/packages/frontend-core/src/components/grid/overlays/ReorderOverlay.svelte +++ b/packages/frontend-core/src/components/grid/overlays/ReorderOverlay.svelte @@ -1,37 +1,33 @@ diff --git a/packages/frontend-core/src/components/grid/overlays/ResizeOverlay.svelte b/packages/frontend-core/src/components/grid/overlays/ResizeOverlay.svelte index 64dd258c4f..956ac13e5c 100644 --- a/packages/frontend-core/src/components/grid/overlays/ResizeOverlay.svelte +++ b/packages/frontend-core/src/components/grid/overlays/ResizeOverlay.svelte @@ -1,41 +1,28 @@ {#if !$isReordering} - {#if $displayColumn} -
resize.actions.startResizing($displayColumn, e)} - on:touchstart={e => resize.actions.startResizing($displayColumn, e)} - on:dblclick={() => resize.actions.resetSize($displayColumn)} - style="left:{GutterWidth + $displayColumn.width}px;" - > -
-
- {/if} {#each $visibleColumns as column}
resize.actions.startResizing(column, e)} on:touchstart={e => resize.actions.startResizing(column, e)} on:dblclick={() => resize.actions.resetSize(column)} - style={getStyle(column, offset, $scrollLeft)} + style={getStyle(column, $scrollLeft)} >
diff --git a/packages/frontend-core/src/components/grid/stores/columns.js b/packages/frontend-core/src/components/grid/stores/columns.js index 49353dbb25..d575ca7910 100644 --- a/packages/frontend-core/src/components/grid/stores/columns.js +++ b/packages/frontend-core/src/components/grid/stores/columns.js @@ -1,5 +1,5 @@ import { derived, get, writable } from "svelte/store" -import { DefaultColumnWidth } from "../lib/constants" +import { DefaultColumnWidth, GutterWidth } from "../lib/constants" export const createStores = () => { const columns = writable([]) @@ -39,16 +39,16 @@ export const deriveStores = context => { // Derived list of columns which have not been explicitly hidden, and enrich // with an index so we can easily select nearby columns const visibleColumns = derived(columns, $columns => { - let offset = 0 + let offset = GutterWidth return $columns .filter(col => col.visible) - .map((column, idx) => { + .map((col, idx) => { const enriched = { - ...column, + ...col, __left: offset, __idx: idx, } - offset += column.width + offset += col.width return enriched }) }) diff --git a/packages/frontend-core/src/components/grid/stores/reorder.js b/packages/frontend-core/src/components/grid/stores/reorder.js index 4cab9142fc..3d41b2227e 100644 --- a/packages/frontend-core/src/components/grid/stores/reorder.js +++ b/packages/frontend-core/src/components/grid/stores/reorder.js @@ -4,10 +4,10 @@ import { parseEventLocation } from "../lib/utils" const reorderInitialState = { sourceColumn: null, targetColumn: null, + insertAfter: false, breakpoints: [], gridLeft: 0, width: 0, - latestX: 0, increment: 0, } @@ -35,10 +35,11 @@ export const createActions = context => { bounds, visibleColumns, maxScrollLeft, - width, datasource, + bodyLeft, + width, } = context - + let latestX = 0 let autoScrollInterval let isAutoScrolling @@ -46,13 +47,25 @@ export const createActions = context => { const startReordering = (column, e) => { const $scrollableColumns = get(scrollableColumns) const $bounds = get(bounds) + const $bodyLeft = get(bodyLeft) // Generate new breakpoints for the current columns const breakpoints = $scrollableColumns.map(col => ({ - x: col.__left + col.width, + x: col.__left - $bodyLeft, column: col.name, + insertAfter: false, })) + // Add a very left breakpoint as well + const lastCol = $scrollableColumns[$scrollableColumns.length - 1] + if (lastCol) { + breakpoints.push({ + x: lastCol.__left + lastCol.width - $bodyLeft, + column: lastCol.name, + insertAfter: true, + }) + } + // Update state reorder.set({ sourceColumn: column, @@ -77,10 +90,7 @@ export const createActions = context => { const onReorderMouseMove = e => { // Immediately handle the current position const { x } = parseEventLocation(e) - reorder.update(state => ({ - ...state, - latestX: x, - })) + latestX = x considerReorderPosition() // Check if we need to start auto-scrolling @@ -110,20 +120,25 @@ export const createActions = context => { const $scroll = get(scroll) // Compute the closest breakpoint to the current position - let targetColumn + let breakpoint let minDistance = Number.MAX_SAFE_INTEGER - const mouseX = $reorder.latestX - $reorder.gridLeft + $scroll.left + const mouseX = latestX - $reorder.gridLeft + $scroll.left $reorder.breakpoints.forEach(point => { const distance = Math.abs(point.x - mouseX) if (distance < minDistance) { minDistance = distance - targetColumn = point.column + breakpoint = point } }) - if (targetColumn !== $reorder.targetColumn) { + if ( + breakpoint && + (breakpoint.column !== $reorder.targetColumn || + breakpoint.insertAfter !== $reorder.insertAfter) + ) { reorder.update(state => ({ ...state, - targetColumn, + targetColumn: breakpoint.column, + insertAfter: breakpoint.insertAfter, })) } } diff --git a/packages/frontend-core/src/components/grid/stores/scroll.js b/packages/frontend-core/src/components/grid/stores/scroll.js index 806cad67da..4a46e5c539 100644 --- a/packages/frontend-core/src/components/grid/stores/scroll.js +++ b/packages/frontend-core/src/components/grid/stores/scroll.js @@ -32,44 +32,40 @@ export const deriveStores = context => { } = context // Memoize store primitives - const stickyColumnWidth = derived(displayColumn, $col => $col?.width || 0, 0) + const bodyLeft = derived(displayColumn, $displayColumn => { + return ($displayColumn?.width || 0) + GutterWidth + }) // Derive vertical limits const contentHeight = derived( [rows, rowHeight], - ([$rows, $rowHeight]) => ($rows.length + 1) * $rowHeight + Padding, - 0 + ([$rows, $rowHeight]) => ($rows.length + 1) * $rowHeight + Padding ) const maxScrollTop = derived( [height, contentHeight], - ([$height, $contentHeight]) => Math.max($contentHeight - $height, 0), - 0 + ([$height, $contentHeight]) => Math.max($contentHeight - $height, 0) ) // Derive horizontal limits const contentWidth = derived( - [visibleColumns, stickyColumnWidth, buttonColumnWidth], - ([$visibleColumns, $stickyColumnWidth, $buttonColumnWidth]) => { + [visibleColumns, buttonColumnWidth], + ([$visibleColumns, $buttonColumnWidth]) => { const space = Math.max(Padding, $buttonColumnWidth - 1) - let width = GutterWidth + space + $stickyColumnWidth + let width = GutterWidth + space $visibleColumns.forEach(col => { width += col.width }) return width - }, - 0 - ) - const screenWidth = derived( - [width, stickyColumnWidth], - ([$width, $stickyColumnWidth]) => $width + GutterWidth + $stickyColumnWidth, - 0 + } ) + const screenWidth = derived([width, bodyLeft], ([$width, $bodyLeft]) => { + return $width + $bodyLeft + }) const maxScrollLeft = derived( [contentWidth, screenWidth], ([$contentWidth, $screenWidth]) => { return Math.max($contentWidth - $screenWidth, 0) - }, - 0 + } ) // Derive whether to show scrollbars or not @@ -87,6 +83,7 @@ export const deriveStores = context => { ) return { + bodyLeft, contentHeight, contentWidth, screenWidth,