From 0bda4a19529f7aa460aa1a340f8b7e5f98fd706d Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 6 Sep 2023 16:56:27 +0100 Subject: [PATCH] Add option to duplicate columns --- .../src/components/grid/cells/DataCell.svelte | 5 ++- .../components/grid/cells/HeaderCell.svelte | 36 +++++++++++++++++++ .../grid/overlays/KeyboardManager.svelte | 2 ++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/frontend-core/src/components/grid/cells/DataCell.svelte b/packages/frontend-core/src/components/grid/cells/DataCell.svelte index 19bdf1bd74..daf0a03276 100644 --- a/packages/frontend-core/src/components/grid/cells/DataCell.svelte +++ b/packages/frontend-core/src/components/grid/cells/DataCell.svelte @@ -56,7 +56,10 @@ isActive: () => api?.isActive?.() ?? false, onKeyDown: (...params) => api?.onKeyDown(...params), isReadonly: () => readonly, - getType: () => column.schema.type, + getType: () => { + console.log("getType", column) + return column.schema.type + }, getValue: () => row[column.name], setValue: (value, options = { save: true }) => { validation.actions.setError(cellId, null) diff --git a/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte b/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte index c5f1f7896a..a58b4c8fe4 100644 --- a/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte +++ b/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte @@ -20,6 +20,8 @@ config, ui, columns, + definition, + datasource, } = getContext("grid") const bannedDisplayColumnTypes = [ @@ -118,6 +120,33 @@ open = false } + const duplicateColumn = async () => { + open = false + + // Generate new name + let newName = `${column.name} copy` + let attempts = 2 + while ($definition.schema[newName]) { + newName = `${column.name} copy ${attempts++}` + } + + // Save schema with new column + const existingColumnDefinition = $definition.schema[column.name] + await datasource.actions.saveDefinition({ + ...$definition, + schema: { + ...$definition.schema, + [newName]: { + ...existingColumnDefinition, + name: newName, + schema: { + ...existingColumnDefinition.schema, + }, + }, + }, + }) + } + onMount(() => subscribe("close-edit-column", cancelEdit)) @@ -192,6 +221,13 @@ > Edit column + + Duplicate column + { + console.log("start", $focusedCellAPI, $focusedCellAPI.isReadonly()) if ($focusedCellAPI && !$focusedCellAPI.isReadonly()) { const type = $focusedCellAPI.getType() + console.log(type) if (type === "number" && keyCodeIsNumber(keyCode)) { // Update the value locally but don't save it yet $focusedCellAPI.setValue(parseInt(key), { save: false })