mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-23 03:26:00 +00:00
insert multiple JSON documents
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
setChangeSetRowData,
|
||||
compileMacroFunction,
|
||||
runMacroOnValue,
|
||||
changeSetInsertDocuments,
|
||||
} from 'dbgate-datalib';
|
||||
import Grider, { GriderRowStatus } from './Grider';
|
||||
|
||||
@@ -181,6 +182,12 @@ export default class ChangeSetGrider extends Grider {
|
||||
return res;
|
||||
}
|
||||
|
||||
insertDocuments(documents: any[]): number {
|
||||
const res = this.rowCountInUpdate;
|
||||
this.applyModification(chs => changeSetInsertDocuments(chs, documents, this.display.baseTableOrCollection));
|
||||
return res;
|
||||
}
|
||||
|
||||
beginUpdate() {
|
||||
this.batchChangeSet = this.changeSet;
|
||||
}
|
||||
|
||||
@@ -1094,7 +1094,7 @@
|
||||
return [row, col];
|
||||
}
|
||||
|
||||
function handlePaste(event) {
|
||||
async function handlePaste(event) {
|
||||
var pastedText = undefined;
|
||||
// @ts-ignore
|
||||
if (window.clipboardData && window.clipboardData.getData) {
|
||||
@@ -1105,41 +1105,62 @@
|
||||
pastedText = event.clipboardData.getData('text/plain');
|
||||
}
|
||||
event.preventDefault();
|
||||
grider.beginUpdate();
|
||||
const pasteRows = pastedText
|
||||
.replace(/\r/g, '')
|
||||
.split('\n')
|
||||
.map(row => row.split('\t'));
|
||||
const selectedRegular = cellsToRegularCells(selectedCells);
|
||||
if (selectedRegular.length <= 1) {
|
||||
const startRow = isRegularCell(currentCell) ? currentCell[0] : grider.rowCount;
|
||||
const startCol = isRegularCell(currentCell) ? currentCell[1] : 0;
|
||||
let rowIndex = startRow;
|
||||
for (const rowData of pasteRows) {
|
||||
if (rowIndex >= grider.rowCountInUpdate) {
|
||||
grider.insertRow();
|
||||
}
|
||||
let colIndex = startCol;
|
||||
for (const cell of rowData) {
|
||||
setCellValue([rowIndex, colIndex], cell == '(NULL)' ? null : cell);
|
||||
colIndex += 1;
|
||||
}
|
||||
rowIndex += 1;
|
||||
|
||||
let json = null;
|
||||
if (grider.canInsert) {
|
||||
try {
|
||||
json = JSON.parse(pastedText);
|
||||
} catch (e) {
|
||||
json = null;
|
||||
}
|
||||
}
|
||||
if (selectedRegular.length > 1) {
|
||||
const startRow: number = _.min(selectedRegular.map(x => x[0]));
|
||||
const startCol: number = _.min(selectedRegular.map(x => x[1]));
|
||||
for (const cell of selectedRegular) {
|
||||
const [rowIndex, colIndex] = cell;
|
||||
const selectionRow = rowIndex - startRow;
|
||||
const selectionCol = colIndex - startCol;
|
||||
const pasteRow = pasteRows[selectionRow % pasteRows.length];
|
||||
const pasteCell = pasteRow[selectionCol % pasteRow.length];
|
||||
setCellValue(cell, pasteCell);
|
||||
|
||||
if (json && (_.isArray(json) || _.isPlainObject(json))) {
|
||||
const rowIndex = grider.insertDocuments(_.isArray(json) ? json : [json]);
|
||||
const cell = [rowIndex, (currentCell && currentCell[1]) || 0];
|
||||
// @ts-ignore
|
||||
currentCell = cell;
|
||||
// @ts-ignore
|
||||
selectedCells = [cell];
|
||||
await tick();
|
||||
scrollIntoView(cell);
|
||||
} else {
|
||||
grider.beginUpdate();
|
||||
const pasteRows = pastedText
|
||||
.replace(/\r/g, '')
|
||||
.split('\n')
|
||||
.map(row => row.split('\t'));
|
||||
const selectedRegular = cellsToRegularCells(selectedCells);
|
||||
if (selectedRegular.length <= 1) {
|
||||
const startRow = isRegularCell(currentCell) ? currentCell[0] : grider.rowCount;
|
||||
const startCol = isRegularCell(currentCell) ? currentCell[1] : 0;
|
||||
let rowIndex = startRow;
|
||||
for (const rowData of pasteRows) {
|
||||
if (rowIndex >= grider.rowCountInUpdate) {
|
||||
grider.insertRow();
|
||||
}
|
||||
let colIndex = startCol;
|
||||
for (const cell of rowData) {
|
||||
setCellValue([rowIndex, colIndex], cell == '(NULL)' ? null : cell);
|
||||
colIndex += 1;
|
||||
}
|
||||
rowIndex += 1;
|
||||
}
|
||||
}
|
||||
if (selectedRegular.length > 1) {
|
||||
const startRow: number = _.min(selectedRegular.map(x => x[0]));
|
||||
const startCol: number = _.min(selectedRegular.map(x => x[1]));
|
||||
for (const cell of selectedRegular) {
|
||||
const [rowIndex, colIndex] = cell;
|
||||
const selectionRow = rowIndex - startRow;
|
||||
const selectionCol = colIndex - startCol;
|
||||
const pasteRow = pasteRows[selectionRow % pasteRows.length];
|
||||
const pasteCell = pasteRow[selectionCol % pasteRow.length];
|
||||
setCellValue(cell, pasteCell);
|
||||
}
|
||||
}
|
||||
grider.endUpdate();
|
||||
}
|
||||
grider.endUpdate();
|
||||
}
|
||||
|
||||
function cellsToRegularCells(cells) {
|
||||
@@ -1296,7 +1317,7 @@
|
||||
<ErrorInfo message={errorMessage} alignTop />
|
||||
{:else if isDynamicStructure && isLoadedAll && grider?.rowCount == 0}
|
||||
<div>
|
||||
<ErrorInfo alignTop message="No rows loaded, check filter or add new documents" />
|
||||
<ErrorInfo alignTop message="No rows loaded, check filter or add new documents. You could copy documents from ohter collections/tables with Copy advanved/Copy as JSON command." />
|
||||
{#if display.filterCount > 0}
|
||||
<FormStyledButton value="Reset filter" on:click={() => display.clearFilters()} />
|
||||
{/if}
|
||||
|
||||
@@ -22,6 +22,9 @@ export default abstract class Grider {
|
||||
insertRow(): number {
|
||||
return null;
|
||||
}
|
||||
insertDocuments(documents: any[]): number {
|
||||
return null;
|
||||
}
|
||||
revertRowChanges(index: number) {}
|
||||
revertAllChanges() {}
|
||||
undo() {}
|
||||
|
||||
@@ -47,6 +47,15 @@ export default class FreeTableGrider extends Grider {
|
||||
};
|
||||
return this.currentModel.rows.length - 1;
|
||||
}
|
||||
insertDocuments(documents: any[]): number {
|
||||
const model = this.currentModel;
|
||||
this.currentModel = {
|
||||
...model,
|
||||
rows: [...model.rows, ...documents],
|
||||
};
|
||||
return this.currentModel.rows.length - documents.length;
|
||||
}
|
||||
|
||||
deleteRow(index: number) {
|
||||
const model = this.currentModel;
|
||||
this.currentModel = {
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
import EditJsonModal from '../modals/EditJsonModal.svelte';
|
||||
import ChangeSetGrider from '../datagrid/ChangeSetGrider';
|
||||
import { setContext } from 'svelte';
|
||||
import _ from 'lodash';
|
||||
|
||||
export let tabid;
|
||||
export let conid;
|
||||
@@ -139,8 +140,7 @@
|
||||
json: {},
|
||||
onSave: value => {
|
||||
const grider = new ChangeSetGrider(loadedRows, $changeSetStore, dispatchChangeSet, display);
|
||||
const newRowIndex = grider.insertRow();
|
||||
grider.setRowData(newRowIndex, value);
|
||||
grider.insertDocuments(_.isArray(value) ? value : [value]);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user