From d43304792acb253735bee96926e0eb26f9e99246 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Mon, 5 Apr 2021 20:08:23 +0200 Subject: [PATCH] edit json document --- packages/datalib/src/ChangeSet.ts | 38 ++++++++++++++++++- packages/web/src/datagrid/ChangeSetGrider.ts | 20 +++++++++- packages/web/src/datagrid/DataGridCore.svelte | 21 +++++++++- .../forms/FormElectronFileSelectorRaw.svelte | 2 +- .../web/src/jsonview/CollectionJsonRow.svelte | 35 ++++++++++++++++- .../src/jsonview/CollectionJsonView.svelte | 2 +- .../web/src/modals/ConfirmNoSqlModal.svelte | 2 +- .../web/src/modals/ConfirmSqlModal.svelte | 2 +- packages/web/src/modals/EditJsonModal.svelte | 25 +++++++++--- .../web/src/modals/ErrorMessageModal.svelte | 2 +- .../web/src/modals/InsertJoinModal.svelte | 4 +- 11 files changed, 137 insertions(+), 16 deletions(-) diff --git a/packages/datalib/src/ChangeSet.ts b/packages/datalib/src/ChangeSet.ts index 8fd21d61..97c7aef1 100644 --- a/packages/datalib/src/ChangeSet.ts +++ b/packages/datalib/src/ChangeSet.ts @@ -78,7 +78,6 @@ export function setChangeSetValue( definition: ChangeSetFieldDefinition, value: string ): ChangeSet { - console.log('SET', changeSet, definition, value); if (!changeSet || !definition) return changeSet; let [fieldName, existingItem] = findExistingChangeSetItem(changeSet, definition); if (fieldName == 'deletes') { @@ -119,6 +118,43 @@ export function setChangeSetValue( }; } +export function setChangeSetRowData(changeSet: ChangeSet, definition: ChangeSetRowDefinition, document: any): ChangeSet { + if (!changeSet || !definition) return changeSet; + let [fieldName, existingItem] = findExistingChangeSetItem(changeSet, definition); + if (fieldName == 'deletes') { + changeSet = revertChangeSetRowChanges(changeSet, definition); + [fieldName, existingItem] = findExistingChangeSetItem(changeSet, definition); + } + if (existingItem) { + return { + ...changeSet, + [fieldName]: changeSet[fieldName].map(item => + item == existingItem + ? { + ...item, + fields: {}, + document, + } + : item + ), + }; + } + + return { + ...changeSet, + [fieldName]: [ + ...changeSet[fieldName], + { + pureName: definition.pureName, + schemaName: definition.schemaName, + condition: definition.condition, + insertedRowIndex: definition.insertedRowIndex, + document, + }, + ], + }; +} + // export function batchUpdateChangeSet( // changeSet: ChangeSet, // rowDefinitions: ChangeSetRowDefinition[], diff --git a/packages/web/src/datagrid/ChangeSetGrider.ts b/packages/web/src/datagrid/ChangeSetGrider.ts index f3675fbe..0e674b56 100644 --- a/packages/web/src/datagrid/ChangeSetGrider.ts +++ b/packages/web/src/datagrid/ChangeSetGrider.ts @@ -11,11 +11,23 @@ import { MacroSelectedCell, revertChangeSetRowChanges, setChangeSetValue, + setChangeSetRowData, compileMacroFunction, runMacroOnValue, } from 'dbgate-datalib'; import Grider, { GriderRowStatus } from './Grider'; +function getRowFromItem(row, matchedChangeSetItem) { + return matchedChangeSetItem.document + ? { + ...matchedChangeSetItem.document, + ...matchedChangeSetItem.fields, + } + : { + ...row, + ...matchedChangeSetItem.fields, + }; +} export default class ChangeSetGrider extends Grider { public insertedRows: any[]; public changeSet: ChangeSet; @@ -71,7 +83,7 @@ export default class ChangeSetGrider extends Grider { const rowDefinition = this.display?.getChangeSetRow(row, insertedRowIndex); const [matchedField, matchedChangeSetItem] = findExistingChangeSetItem(this.changeSet, rowDefinition); const rowUpdated = matchedChangeSetItem - ? { ...row, ...matchedChangeSetItem.fields } + ? getRowFromItem(row, matchedChangeSetItem) : this.compiledMacroFunc ? { ...row } : row; @@ -143,6 +155,12 @@ export default class ChangeSetGrider extends Grider { this.applyModification(chs => setChangeSetValue(chs, definition, value)); } + setRowData(index: number, document: any) { + const row = this.getRowSource(index); + const definition = this.display.getChangeSetRow(row, this.getInsertedRowIndex(index)); + this.applyModification(chs => setChangeSetRowData(chs, definition, document)); + } + deleteRow(index: number) { this.requireRowCache(index); this.applyModification(chs => deleteChangeSetRows(chs, this.rowDefinitionsCache[index])); diff --git a/packages/web/src/datagrid/DataGridCore.svelte b/packages/web/src/datagrid/DataGridCore.svelte index 4d53cc2f..dbdddd22 100644 --- a/packages/web/src/datagrid/DataGridCore.svelte +++ b/packages/web/src/datagrid/DataGridCore.svelte @@ -140,6 +140,14 @@ onClick: () => getCurrentDataGrid().switchToJson(), }); + registerCommand({ + id: 'dataGrid.editJsonDocument', + category: 'Data grid', + name: 'Edit row as JSON document', + testEnabled: () => getCurrentDataGrid()?.editJsonEnabled(), + onClick: () => getCurrentDataGrid().editJsonDocument(), + }); + registerCommand({ id: 'dataGrid.filterSelected', category: 'Data grid', @@ -256,6 +264,7 @@ import ErrorInfo from '../elements/ErrorInfo.svelte'; import { dataGridRowHeight } from './DataGridRowHeightMeter.svelte'; import FormStyledButton from '../elements/FormStyledButton.svelte'; + import { editJsonRowDocument } from '../jsonview/CollectionJsonRow.svelte'; export let onLoadNextData = undefined; export let grider = undefined; @@ -274,7 +283,7 @@ export let onOpenQuery = null; export let onOpenActiveChart = null; export let formViewAvailable = false; - export let jsonViewAvailable=false; + export let jsonViewAvailable = false; export let errorMessage = undefined; export let isLoadedAll; @@ -502,6 +511,15 @@ ); } + export function editJsonEnabled() { + return grider.editable && isDynamicStructure && _.uniq(selectedCells.map(x => x[0])).length == 1; + } + + export function editJsonDocument() { + const rowIndex = selectedCells[0][0]; + editJsonRowDocument(grider, rowIndex); + } + // export function getGeneralAllowSave() { // return generalAllowSave; // } @@ -1029,6 +1047,7 @@ { command: 'dataGrid.export' }, { command: 'dataGrid.switchToForm' }, { command: 'dataGrid.switchToJson' }, + { command: 'dataGrid.editJsonDocument' }, { divider: true }, { command: 'dataGrid.save' }, { command: 'dataGrid.revertRowChanges' }, diff --git a/packages/web/src/forms/FormElectronFileSelectorRaw.svelte b/packages/web/src/forms/FormElectronFileSelectorRaw.svelte index e3993ca5..c2555475 100644 --- a/packages/web/src/forms/FormElectronFileSelectorRaw.svelte +++ b/packages/web/src/forms/FormElectronFileSelectorRaw.svelte @@ -24,6 +24,6 @@
- + Browse
diff --git a/packages/web/src/jsonview/CollectionJsonRow.svelte b/packages/web/src/jsonview/CollectionJsonRow.svelte index e028ff46..72f6d0b7 100644 --- a/packages/web/src/jsonview/CollectionJsonRow.svelte +++ b/packages/web/src/jsonview/CollectionJsonRow.svelte @@ -1,5 +1,25 @@ + +
{#each _.range(0, grider.rowCount) as rowIndex} - + {/each}
diff --git a/packages/web/src/modals/ConfirmNoSqlModal.svelte b/packages/web/src/modals/ConfirmNoSqlModal.svelte index 7a9fa612..67513ea6 100644 --- a/packages/web/src/modals/ConfirmNoSqlModal.svelte +++ b/packages/web/src/modals/ConfirmNoSqlModal.svelte @@ -27,7 +27,7 @@ onConfirm(); }} /> - + diff --git a/packages/web/src/modals/ConfirmSqlModal.svelte b/packages/web/src/modals/ConfirmSqlModal.svelte index 441fb20a..168c1f46 100644 --- a/packages/web/src/modals/ConfirmSqlModal.svelte +++ b/packages/web/src/modals/ConfirmSqlModal.svelte @@ -28,7 +28,7 @@ onConfirm(); }} /> - + diff --git a/packages/web/src/modals/EditJsonModal.svelte b/packages/web/src/modals/EditJsonModal.svelte index 573502bb..ea17456e 100644 --- a/packages/web/src/modals/EditJsonModal.svelte +++ b/packages/web/src/modals/EditJsonModal.svelte @@ -1,14 +1,23 @@ @@ -23,12 +32,18 @@ { - if (onSave(value)) { - closeCurrentModal(); + try { + const parsed = JSON.parse(value); + if (onSave(parsed)) { + closeCurrentModal(); + } + } catch (err) { + showModal(ErrorMessageModal, { message: err.message }); + return; } }} /> - + diff --git a/packages/web/src/modals/ErrorMessageModal.svelte b/packages/web/src/modals/ErrorMessageModal.svelte index 64240cfa..4b48d240 100644 --- a/packages/web/src/modals/ErrorMessageModal.svelte +++ b/packages/web/src/modals/ErrorMessageModal.svelte @@ -23,7 +23,7 @@
- +
diff --git a/packages/web/src/modals/InsertJoinModal.svelte b/packages/web/src/modals/InsertJoinModal.svelte index 465b67fb..bc0687bf 100644 --- a/packages/web/src/modals/InsertJoinModal.svelte +++ b/packages/web/src/modals/InsertJoinModal.svelte @@ -169,12 +169,12 @@ { + on:click={() => { closeCurrentModal(); onInsert(sqlPreview); }} /> - +