Remove redundant sheet schema context and fix issues with mutating table schema

This commit is contained in:
Andrew Kingston 2023-03-29 15:35:36 +01:00
parent eeda58822c
commit b7bea0556c
3 changed files with 27 additions and 27 deletions

View File

@ -91,7 +91,7 @@
...tableDefinition, ...tableDefinition,
primaryDisplay: column.name, primaryDisplay: column.name,
}) })
await rows.actions.refreshSchema() await rows.actions.refreshTableDefinition()
open = false open = false
} }
</script> </script>
@ -164,9 +164,13 @@
<MenuItem disabled={!canMoveRight} icon="ArrowRight" on:click={moveRight}> <MenuItem disabled={!canMoveRight} icon="ArrowRight" on:click={moveRight}>
Move right Move right
</MenuItem> </MenuItem>
<MenuItem icon="Label" on:click={makeDisplayColumn} <MenuItem
>Use as display column</MenuItem icon="Label"
on:click={makeDisplayColumn}
disabled={column.idx === "sticky"}
> >
Use as display column
</MenuItem>
</Menu> </Menu>
</Popover> </Popover>

View File

@ -3,7 +3,7 @@ import { derived, get, writable } from "svelte/store"
export const DefaultColumnWidth = 200 export const DefaultColumnWidth = 200
export const createColumnsStores = context => { export const createColumnsStores = context => {
const { schema } = context const { table } = context
const columns = writable([]) const columns = writable([])
const stickyColumn = writable(null) const stickyColumn = writable(null)
@ -37,8 +37,9 @@ export const createColumnsStores = context => {
) )
// Merge new schema fields with existing schema in order to preserve widths // Merge new schema fields with existing schema in order to preserve widths
schema.subscribe($schema => { table.subscribe($table => {
if (!$schema) { const schema = $table?.schema
if (!schema) {
columns.set([]) columns.set([])
stickyColumn.set(null) stickyColumn.set(null)
return return
@ -46,10 +47,16 @@ export const createColumnsStores = context => {
const currentColumns = get(columns) const currentColumns = get(columns)
const currentStickyColumn = get(stickyColumn) const currentStickyColumn = get(stickyColumn)
// Find primary display
let primaryDisplay
if ($table.primaryDisplay && schema[$table.primaryDisplay]) {
primaryDisplay = $table.primaryDisplay
}
// Get field list // Get field list
let fields = [] let fields = []
Object.entries($schema || {}).forEach(([field, fieldSchema]) => { Object.keys(schema).forEach(field => {
if (!fieldSchema.primaryDisplay) { if (field !== primaryDisplay) {
fields.push(field) fields.push(field)
} }
}) })
@ -66,31 +73,28 @@ export const createColumnsStores = context => {
return { return {
name: field, name: field,
width: existing?.width || DefaultColumnWidth, width: existing?.width || DefaultColumnWidth,
schema: $schema[field], schema: schema[field],
visible: existing?.visible ?? true, visible: existing?.visible ?? true,
} }
}) })
) )
// Update sticky column // Update sticky column
const primaryDisplay = Object.entries($schema).find(entry => {
return entry[1].primaryDisplay
})
if (!primaryDisplay) { if (!primaryDisplay) {
return return
} }
// Check if there is an existing column with this name so we can keep // Check if there is an existing column with this name so we can keep
// the width setting // the width setting
let existing = currentColumns.find(x => x.name === primaryDisplay[0]) let existing = currentColumns.find(x => x.name === primaryDisplay)
if (!existing && currentStickyColumn?.name === primaryDisplay[0]) { if (!existing && currentStickyColumn?.name === primaryDisplay) {
existing = currentStickyColumn existing = currentStickyColumn
} }
stickyColumn.set({ stickyColumn.set({
name: primaryDisplay[0], name: primaryDisplay,
width: existing?.width || DefaultColumnWidth, width: existing?.width || DefaultColumnWidth,
left: 40, left: 40,
schema: primaryDisplay[1], schema: schema[primaryDisplay],
idx: "sticky", idx: "sticky",
}) })
}) })

View File

@ -6,7 +6,6 @@ export const createRowsStore = context => {
const { config, API, scroll } = context const { config, API, scroll } = context
const tableId = derived(config, $config => $config.tableId) const tableId = derived(config, $config => $config.tableId)
const rows = writable([]) const rows = writable([])
const schema = writable({})
const table = writable(null) const table = writable(null)
const filter = writable([]) const filter = writable([])
const loaded = writable(false) const loaded = writable(false)
@ -89,13 +88,7 @@ export const createRowsStore = context => {
scroll.update(state => ({ ...state, top: 0 })) scroll.update(state => ({ ...state, top: 0 }))
} }
// Update schema and enrich primary display into schema // Update table definition
let newSchema = $fetch.schema
const primaryDisplay = $fetch.definition?.primaryDisplay
if (primaryDisplay && newSchema[primaryDisplay]) {
newSchema[primaryDisplay].primaryDisplay = true
}
schema.set(newSchema)
table.set($fetch.definition) table.set($fetch.definition)
// Process new rows // Process new rows
@ -267,7 +260,7 @@ export const createRowsStore = context => {
} }
// Refreshes the schema of the data fetch subscription // Refreshes the schema of the data fetch subscription
const refreshSchema = async () => { const refreshTableDefinition = async () => {
return await get(fetch)?.refreshDefinition() return await get(fetch)?.refreshDefinition()
} }
@ -288,12 +281,11 @@ export const createRowsStore = context => {
loadNextPage, loadNextPage,
refreshRow, refreshRow,
refreshData, refreshData,
refreshSchema, refreshTableDefinition,
}, },
}, },
rowLookupMap, rowLookupMap,
table, table,
schema,
sort, sort,
filter, filter,
loaded, loaded,