Skip to content

Commit

Permalink
Improve grid handling of non datasource plus and fix focused cell iss…
Browse files Browse the repository at this point in the history
…ue with row click actions
  • Loading branch information
aptkingston committed Oct 9, 2023
1 parent 7ca304b commit 4667d08
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
contentLines,
isDragging,
dispatch,
rows,
} = getContext("grid")
$: rowSelected = !!$selectedRows[row._id]
Expand All @@ -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}`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
focusedCellAPI,
focusedRowId,
notifications,
isDatasourcePlus,
} = getContext("grid")
$: style = makeStyle($menu)
Expand Down Expand Up @@ -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}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,9 +60,14 @@ export const deriveStores = context => {
}
)

const isDatasourcePlus = derived(datasource, $datasource => {
return ["table", "viewV2"].includes($datasource?.type)
})

return {
schema,
enrichedSchema,
isDatasourcePlus,
}
}

Expand Down
45 changes: 32 additions & 13 deletions packages/frontend-core/src/components/grid/stores/rows.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand Down Expand Up @@ -76,11 +77,11 @@ export const createActions = context => {
columns,
rowChangeCache,
inProgressChanges,
previousFocusedRowId,
hasNextPage,
error,
notifications,
fetch,
isDatasourcePlus,
} = context
const instanceLoaded = writable(false)

Expand Down Expand Up @@ -361,7 +362,7 @@ export const createActions = context => {

// Update row
const saved = await datasource.actions.updateRow({
...row,
...cleanRow(row),
...get(rowChangeCache)[rowId],
})

Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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: {
Expand All @@ -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
})
}
})
}

0 comments on commit 4667d08

Please sign in to comment.