From 152d157aaa2fc43cf8c0af39914943f5bbaac92e Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 25 Nov 2022 08:34:44 +0000 Subject: [PATCH] Add ability to add new rows --- .../app/spreadsheet/OptionsCell.svelte | 19 ++++--- .../app/spreadsheet/Spreadsheet.svelte | 50 +++++++++++++++---- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/packages/client/src/components/app/spreadsheet/OptionsCell.svelte b/packages/client/src/components/app/spreadsheet/OptionsCell.svelte index eda93cc9c8..fcde18bbc1 100644 --- a/packages/client/src/components/app/spreadsheet/OptionsCell.svelte +++ b/packages/client/src/components/app/spreadsheet/OptionsCell.svelte @@ -52,9 +52,9 @@
{value}
- {:else if value} + {:else}
- {value} + {value || ""}
{/if} {#if selected} @@ -62,12 +62,17 @@ {/if} {#if open}
-
-
- {value} + {#if value} +
+
+ {value} +
+
- -
+ {/if} {#each options.filter(x => x !== value) as option}
onChange(option)}>
diff --git a/packages/client/src/components/app/spreadsheet/Spreadsheet.svelte b/packages/client/src/components/app/spreadsheet/Spreadsheet.svelte index 06db8e7f9c..267a8a7d85 100644 --- a/packages/client/src/components/app/spreadsheet/Spreadsheet.svelte +++ b/packages/client/src/components/app/spreadsheet/Spreadsheet.svelte @@ -23,6 +23,7 @@ let selectedRows = {} let horizontallyScrolled = false let changeCache = {} + let newRows = [] $: query = LuceneUtils.buildLuceneQuery(filter) $: fetch = createFetch(table) @@ -38,6 +39,7 @@ $: schema = $fetch.schema $: rowCount = $fetch.rows?.length || 0 $: selectedRowCount = Object.values(selectedRows).filter(x => !!x).length + $: rows = getSortedRows($fetch.rows, newRows) const createFetch = datasource => { return fetchData({ @@ -100,14 +102,14 @@ if (allSelected) { selectedRows = {} } else { - $fetch.rows.forEach(row => { + rows.forEach(row => { selectedRows[row._id] = true }) } } const handleChange = async (rowId, field, value) => { - let row = $fetch.rows.find(x => x._id === rowId) + let row = rows.find(x => x._id === rowId) if (!row) { return } @@ -125,10 +127,10 @@ const deleteRows = () => { // Fetch full row objects to be deleted - const rows = Object.entries(selectedRows) + const rowsToDelete = Object.entries(selectedRows) .map(entry => { if (entry[1] === true) { - return $fetch.rows.find(x => x._id === entry[0]) + return rows.find(x => x._id === entry[0]) } else { return null } @@ -139,7 +141,7 @@ const performDeletion = async () => { await API.deleteRows({ tableId: table.tableId, - rows, + rows: rowsToDelete, }) await fetch.refresh() notificationStore.actions.success( @@ -163,6 +165,23 @@ performDeletion ) } + + const addRow = async field => { + const res = await API.saveRow({ tableId: table.tableId }) + selectedCell = `${res._id}-${field}` + newRows.push(res._id) + await fetch.refresh() + } + + const getSortedRows = (rows, newRows) => { + let sortedRows = rows.slice() + sortedRows.sort((a, b) => { + const aIndex = newRows.indexOf(a._id) + const bIndex = newRows.indexOf(b._id) + return aIndex < bIndex ? -1 : 1 + }) + return sortedRows + }
@@ -185,7 +204,12 @@ {/if}
-
+
(selectedCell = null)} + >
- {#each $fetch.rows as row, rowIdx (row._id)} + {#each rows as row, rowIdx (row._id)} {@const rowSelected = !!selectedRows[row._id]} {@const rowHovered = hoveredRow === row._id} {@const data = { ...row, ...changeCache[row._id] }} @@ -254,14 +278,15 @@ {/each} -
+
{#each fields as field, fieldIdx}
addRow(field)} /> {/each}
@@ -284,9 +309,9 @@ justify-content: flex-start; align-items: stretch; overflow: auto; - max-height: 800px; + max-height: 1014px; position: relative; - padding-bottom: 80px; + padding-bottom: 180px; padding-right: 100px; } @@ -363,6 +388,9 @@ .cell.row-selected { background-color: rgb(224, 242, 255); } + .cell.new:hover { + cursor: pointer; + } .header { background: var(--spectrum-global-color-gray-200);