mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 06:06:01 +00:00
macro preview
This commit is contained in:
22
packages/datalib/src/MacroDefinition.ts
Normal file
22
packages/datalib/src/MacroDefinition.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
export interface MacroArgument {
|
||||
type: 'text' | 'select';
|
||||
label: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface MacroDefinition {
|
||||
title: string;
|
||||
name: string;
|
||||
group: string;
|
||||
description?: string;
|
||||
type: 'transformValue';
|
||||
code: string;
|
||||
args?: MacroArgument[];
|
||||
}
|
||||
|
||||
export interface MacroSelectedCell {
|
||||
column: string;
|
||||
row: number;
|
||||
}
|
||||
@@ -7,3 +7,5 @@ export * from "./ChangeSet";
|
||||
export * from "./filterName";
|
||||
export * from "./FreeTableGridDisplay";
|
||||
export * from "./FreeTableModel";
|
||||
export * from "./MacroDefinition";
|
||||
export * from "./runMacro";
|
||||
|
||||
59
packages/datalib/src/runMacro.ts
Normal file
59
packages/datalib/src/runMacro.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { FreeTableModel } from './FreeTableModel';
|
||||
import _ from 'lodash';
|
||||
import { MacroDefinition, MacroSelectedCell } from './MacroDefinition';
|
||||
|
||||
const getMacroFunction = {
|
||||
transformValue: (code) => `
|
||||
(value, args, modules, rowIndex, row, columnName) => {
|
||||
${code}
|
||||
}
|
||||
`,
|
||||
};
|
||||
|
||||
const modules = {
|
||||
lodash: _,
|
||||
};
|
||||
|
||||
export function runMacro(
|
||||
macro: MacroDefinition,
|
||||
macroArgs: {},
|
||||
data: FreeTableModel,
|
||||
preview: boolean,
|
||||
selectedCells: MacroSelectedCell[]
|
||||
): FreeTableModel {
|
||||
const func = eval(getMacroFunction[macro.type](macro.code));
|
||||
if (macro.type == 'transformValue') {
|
||||
const selectedRows = _.groupBy(selectedCells, 'row');
|
||||
const rows = data.rows.map((row, rowIndex) => {
|
||||
const selectedRow = selectedRows[rowIndex];
|
||||
if (selectedRow) {
|
||||
const columnSet = new Set(selectedRow.map((item) => item.column));
|
||||
const changedValues = [];
|
||||
const res = _.mapValues(row, (value, key) => {
|
||||
if (columnSet.has(key)) {
|
||||
const newValue = func(value, macroArgs, modules, rowIndex, row, key);
|
||||
if (preview && newValue != value) changedValues.push(key);
|
||||
return newValue;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
});
|
||||
if (changedValues.length > 0) {
|
||||
return {
|
||||
...res,
|
||||
__changedValues: new Set(changedValues),
|
||||
};
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
return row;
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
structure: data.structure,
|
||||
rows,
|
||||
};
|
||||
}
|
||||
return data;
|
||||
}
|
||||
Reference in New Issue
Block a user