macro preview

This commit is contained in:
Jan Prochazka
2020-10-31 09:04:59 +01:00
parent d243e8cee5
commit cc385c12ec
8 changed files with 201 additions and 7 deletions

View 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;
}

View File

@@ -7,3 +7,5 @@ export * from "./ChangeSet";
export * from "./filterName";
export * from "./FreeTableGridDisplay";
export * from "./FreeTableModel";
export * from "./MacroDefinition";
export * from "./runMacro";

View 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;
}