Improve grid handling of non datasource plus and fix focused cell issue with row click actions

This commit is contained in:
Andrew Kingston 2023-10-09 09:36:01 +01:00
parent c48eca4a0f
commit a781860b1c
5 changed files with 43 additions and 19 deletions

View File

@ -18,6 +18,7 @@
contentLines,
isDragging,
dispatch,
rows,
} = getContext("grid")
$: rowSelected = !!$selectedRows[row._id]
@ -31,7 +32,7 @@
on:focus
on:mouseenter={$isDragging ? null : () => ($hoveredRowId = row._id)}
on:mouseleave={$isDragging ? null : () => ($hoveredRowId = null)}
on:click={() => dispatch("rowclick", row)}
on:click={() => dispatch("rowclick", rows.actions.cleanRow(row))}
>
{#each $renderedColumns as column, columnIdx (column.name)}
{@const cellId = `${row._id}-${column.name}`}

View File

@ -74,7 +74,7 @@
class="row"
on:mouseenter={$isDragging ? null : () => ($hoveredRowId = row._id)}
on:mouseleave={$isDragging ? null : () => ($hoveredRowId = null)}
on:click={() => dispatch("rowclick", row)}
on:click={() => dispatch("rowclick", rows.actions.cleanRow(row))}
>
<GutterCell {row} {rowFocused} {rowHovered} {rowSelected} />
{#if $stickyColumn}

View File

@ -17,6 +17,7 @@
focusedCellAPI,
focusedRowId,
notifications,
isDatasourcePlus,
} = getContext("grid")
$: style = makeStyle($menu)
@ -75,9 +76,7 @@
</MenuItem>
<MenuItem
icon="Copy"
disabled={isNewRow ||
!$focusedRow?._id ||
$focusedRow?._id?.startsWith("fake-")}
disabled={isNewRow || !$focusedRow?._id || !$isDatasourcePlus}
on:click={() => copyToClipboard($focusedRow?._id)}
on:click={menu.actions.close}
>

View File

@ -10,7 +10,7 @@ export const createStores = () => {
}
export const deriveStores = context => {
const { definition, schemaOverrides, columnWhitelist } = context
const { definition, schemaOverrides, columnWhitelist, datasource } = context
const schema = derived(definition, $definition => {
let schema = $definition?.schema
@ -60,9 +60,14 @@ export const deriveStores = context => {
}
)
const isDatasourcePlus = derived(datasource, $datasource => {
return ["table", "viewV2"].includes($datasource?.type)
})
return {
schema,
enrichedSchema,
isDatasourcePlus,
}
}

View File

@ -2,6 +2,7 @@ import { writable, derived, get } from "svelte/store"
import { fetchData } from "../../../fetch"
import { NewRowID, RowPageSize } from "../lib/constants"
import { tick } from "svelte"
import { Helpers } from "@budibase/bbui"
export const createStores = () => {
const rows = writable([])
@ -76,11 +77,11 @@ export const createActions = context => {
columns,
rowChangeCache,
inProgressChanges,
previousFocusedRowId,
hasNextPage,
error,
notifications,
fetch,
isDatasourcePlus,
} = context
const instanceLoaded = writable(false)
@ -361,7 +362,7 @@ export const createActions = context => {
// Update row
const saved = await datasource.actions.updateRow({
...row,
...cleanRow(row),
...get(rowChangeCache)[rowId],
})
@ -417,13 +418,15 @@ export const createActions = context => {
}
let rowsToAppend = []
let newRow
const $isDatasourcePlus = get(isDatasourcePlus)
for (let i = 0; i < newRows.length; i++) {
newRow = newRows[i]
// Ensure we have a unique _id.
// This means generating one for non DS+.
if (!newRow._id) {
newRow._id = `fake-${Math.random()}`
// This means generating one for non DS+, overriting any that may already
// exist as we cannot allow duplicates.
if (!$isDatasourcePlus) {
newRow._id = Helpers.uuid()
}
if (!rowCacheMap[newRow._id]) {
@ -462,15 +465,16 @@ export const createActions = context => {
return get(rowLookupMap)[id] != null
}
// Wipe the row change cache when changing row
previousFocusedRowId.subscribe(id => {
if (id && !get(inProgressChanges)[id]) {
rowChangeCache.update(state => {
delete state[id]
return state
})
// Cleans a row by removing any internal grid metadata from it.
// Call this before passing a row to any sort of external flow.
const cleanRow = row => {
let clone = { ...row }
delete clone.__idx
if (!get(isDatasourcePlus)) {
delete clone._id
}
})
return clone
}
return {
rows: {
@ -487,7 +491,22 @@ export const createActions = context => {
refreshRow,
replaceRow,
refreshData,
cleanRow,
},
},
}
}
export const initialise = context => {
const { rowChangeCache, inProgressChanges, previousFocusedRowId } = context
// Wipe the row change cache when changing row
previousFocusedRowId.subscribe(id => {
if (id && !get(inProgressChanges)[id]) {
rowChangeCache.update(state => {
delete state[id]
return state
})
}
})
}