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

View File

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

View File

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