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

@@ -2,16 +2,36 @@ import { createGridCache, FreeTableGridDisplay } from '@dbgate/datalib';
import React from 'react';
import DataGridCore from '../datagrid/DataGridCore';
import FreeTableGrider from './FreeTableGrider';
import MacroPreviewGrider from './MacroPreviewGrider';
export default function FreeTableGridCore(props) {
const { modelState, dispatchModel, config, setConfig, macroPreview, macroValues } = props;
const grider = React.useMemo(() => FreeTableGrider.factory(props), FreeTableGrider.factoryDeps(props));
const [cache, setCache] = React.useState(createGridCache());
const [selectedCells, setSelectedCells] = React.useState([]);
const grider = React.useMemo(
() =>
macroPreview
? new MacroPreviewGrider(modelState.value, macroPreview, macroValues, selectedCells)
: FreeTableGrider.factory(props),
[
...FreeTableGrider.factoryDeps(props),
macroPreview,
macroPreview ? macroValues : null,
macroPreview ? selectedCells : null,
]
);
const display = React.useMemo(() => new FreeTableGridDisplay(modelState.value, config, setConfig, cache, setCache), [
modelState.value,
config,
cache,
]);
return <DataGridCore {...props} grider={grider} display={display} />;
return (
<DataGridCore
{...props}
grider={grider}
display={display}
onSelectionChanged={macroPreview ? setSelectedCells : null}
/>
);
}

View File

@@ -1,11 +1,28 @@
import React from 'react';
import { FormTextField, FormSubmit, FormArchiveFolderSelect, FormRow, FormLabel } from '../utility/forms';
import _ from 'lodash';
import {
FormTextField,
FormSubmit,
FormArchiveFolderSelect,
FormRow,
FormLabel,
FormSelectField,
} from '../utility/forms';
import { Formik, Form, useFormikContext } from 'formik';
function MacroArgument({ arg }) {
if (arg.type == 'text') {
return <FormTextField label={arg.label} name={arg.name} />;
}
if (arg.type == 'select') {
return (
<FormSelectField label={arg.label} name={arg.name}>
{arg.options.map((opt) =>
_.isString(opt) ? <option value={opt}>{opt}</option> : <option value={opt.value}>{opt.name}</option>
)}
</FormSelectField>
);
}
return null;
}

View File

@@ -0,0 +1,17 @@
import { FreeTableModel, MacroDefinition, MacroSelectedCell, runMacro } from '@dbgate/datalib';
import Grider from '../datagrid/Grider';
export default class MacroPreviewGrider extends Grider {
model: FreeTableModel;
constructor(model: FreeTableModel, macro: MacroDefinition, macroArgs: {}, selectedCells: MacroSelectedCell[]) {
super();
this.model = runMacro(macro, macroArgs, model, true, selectedCells);
}
getRowData(index: any) {
return this.model.rows[index];
}
get rowCount() {
return this.model.rows.length;
}
}

View File

@@ -2,15 +2,15 @@ const macros = [
{
title: 'Remove diacritics',
name: 'removeDiacritics',
group: 'text',
group: 'Text',
description: 'Removes diacritics from selected cells',
type: 'transformValue',
code: `value => modules.diacritics.remove(value)`,
code: `return modules.lodash.deburr(value)`,
},
{
title: 'Search & replace text',
name: 'stringReplace',
group: 'text',
group: 'Text',
description: 'Search & replace text or regular expression',
type: 'transformValue',
args: [
@@ -25,7 +25,31 @@ const macros = [
name: 'replace',
},
],
code: `value => value ? value.toString().replace(args.find, args.replace) : value`,
code: `return value ? value.toString().replace(args.find, args.replace) : value`,
},
{
title: 'Change text case',
name: 'changeTextCase',
group: 'Text',
description: 'Uppercase, lowercase and other case functions',
type: 'transformValue',
args: [
{
type: 'select',
options: ['toUpper', 'toLower', 'lowerCase', 'upperCase', 'kebabCase', 'snakeCase', 'camelCase', 'startCase'],
label: 'Type',
name: 'caseTransform',
},
],
code: `return modules.lodash[args.caseTransform || 'toUpper'](value)`,
},
{
title: 'Row index',
name: 'rowIndex',
group: 'Tools',
description: 'index of row from 1 (autoincrement)',
type: 'transformValue',
code: `return rowIndex + 1`,
},
];