feat(PowerInput): show humanized variable label

This commit is contained in:
Ali BARIN
2022-09-20 23:11:29 +02:00
parent 53264e0637
commit 90f4c873bb
4 changed files with 36 additions and 17 deletions

View File

@@ -38,10 +38,10 @@ const Suggestions = (props: SuggestionsProps) => {
onSuggestionClick = () => null, onSuggestionClick = () => null,
} = props; } = props;
const [current, setCurrent] = React.useState<number | null>(0); const [current, setCurrent] = React.useState<number | null>(0);
const [listLength, setListLength] = React.useState<number | undefined>(SHORT_LIST_LENGTH); const [listLength, setListLength] = React.useState<number>(SHORT_LIST_LENGTH);
const expandList = () => { const expandList = () => {
setListLength(undefined); setListLength(Infinity);
}; };
const collapseList = () => { const collapseList = () => {
@@ -72,7 +72,7 @@ const Suggestions = (props: SuggestionsProps) => {
primary={option.name} primary={option.name}
/> />
{option.output?.length && ( {!!option.output?.length && (
current === index ? <ExpandLess /> : <ExpandMore /> current === index ? <ExpandLess /> : <ExpandMore />
)} )}
</ListItemButton> </ListItemButton>
@@ -105,7 +105,7 @@ const Suggestions = (props: SuggestionsProps) => {
} }
</List> </List>
{listLength && ( {option.output?.length > listLength && (
<Button <Button
fullWidth fullWidth
onClick={expandList} onClick={expandList}

View File

@@ -38,7 +38,7 @@ export const processStepWithExecutions = (steps: IStep[]): any[] => {
return steps.map((step: IStep, index: number) => ({ return steps.map((step: IStep, index: number) => ({
id: step.id, id: step.id,
// TODO: replace with step.name once introduced // TODO: replace with step.name once introduced
name: `${index + 1}. ${step.appKey}`, name: `${index + 1}. ${step.appKey?.charAt(0)?.toUpperCase() + step.appKey?.slice(1)}`,
output: process(step.executionSteps?.[0]?.dataOut || {}, `step.${step.id}`), output: process(step.executionSteps?.[0]?.dataOut || {}, `step.${step.id}`),
})); }));
}; };

View File

@@ -72,11 +72,11 @@ const PowerInput = (props: PowerInputProps) => {
(variable: Pick<VariableElement, "name" | "value">) => { (variable: Pick<VariableElement, "name" | "value">) => {
if (target) { if (target) {
Transforms.select(editor, target); Transforms.select(editor, target);
insertVariable(editor, variable); insertVariable(editor, variable, stepsWithVariables);
setTarget(null); setTarget(null);
} }
}, },
[index, target] [index, target, stepsWithVariables]
); );
const onKeyDown = React.useCallback( const onKeyDown = React.useCallback(
@@ -97,7 +97,7 @@ const PowerInput = (props: PowerInputProps) => {
case 'Enter': { case 'Enter': {
event.preventDefault(); event.preventDefault();
Transforms.select(editor, target); Transforms.select(editor, target);
insertVariable(editor, stepsWithVariables[0].output[index]); insertVariable(editor, stepsWithVariables[0].output[index], stepsWithVariables);
setTarget(null); setTarget(null);
break break
} }
@@ -122,7 +122,7 @@ const PowerInput = (props: PowerInputProps) => {
render={({ field: { value, onChange: controllerOnChange, onBlur: controllerOnBlur, } }) => ( render={({ field: { value, onChange: controllerOnChange, onBlur: controllerOnBlur, } }) => (
<Slate <Slate
editor={editor} editor={editor}
value={deserialize(value)} value={deserialize(value, stepsWithVariables)}
onChange={value => { onChange={value => {
controllerOnChange(serialize(value)); controllerOnChange(serialize(value));
const { selection } = editor const { selection } = editor
@@ -195,7 +195,7 @@ const PowerInput = (props: PowerInputProps) => {
<Popper <Popper
open={target !== null && search !== null} open={target !== null && search !== null}
anchorEl={editorRef.current} anchorEl={editorRef.current}
style={{ width: editorRef.current?.clientWidth }} style={{ width: editorRef.current?.clientWidth, zIndex: 1, }}
> >
<Suggestions <Suggestions
query={search} query={search}

View File

@@ -8,9 +8,26 @@ import type {
VariableElement, VariableElement,
} from './types'; } from './types';
export const deserialize = (value: string): Descendant[] => { function getStepPosition(id: string, stepsWithVariables: Record<string, unknown>[]) {
const variableRegExp = /({{.*?}})/g; const stepIndex = stepsWithVariables.findIndex((stepWithVariables) => {
return stepWithVariables.id === id;
});
return stepIndex + 1;
}
function humanizeVariableName(variableName: string, stepsWithVariables: Record<string, unknown>[]) {
const nameWithoutCurlies = variableName.replace(/{{|}}/g, '');
const stepId = nameWithoutCurlies.match(stepIdRegExp)?.[1] || '';
const stepPosition = getStepPosition(stepId, stepsWithVariables);
const humanizedVariableName = nameWithoutCurlies.replace(`step.${stepId}.`, `step${stepPosition}.`);
return humanizedVariableName;
}
const variableRegExp = /({{.*?}})/;
const stepIdRegExp = /^step.([\da-zA-Z-]*)/;
export const deserialize = (value: string, stepsWithVariables: any[]): Descendant[] => {
if (!value) return [{ if (!value) return [{
type: 'paragraph', type: 'paragraph',
children: [{ text: '', }], children: [{ text: '', }],
@@ -26,7 +43,8 @@ export const deserialize = (value: string): Descendant[] => {
if (node.match(variableRegExp)) { if (node.match(variableRegExp)) {
return { return {
type: 'variable', type: 'variable',
name: node.replace(/{{|}}/g, ''), name: humanizeVariableName(node, stepsWithVariables),
value: node,
children: [{ text: '' }], children: [{ text: '' }],
}; };
} }
@@ -55,7 +73,7 @@ const serializeNode = (node: CustomElement | Descendant): string => {
} }
if (node.type === 'variable') { if (node.type === 'variable') {
return `{{${node.name}}}`; return node.value as string;
} }
return node.children.map(n => serializeNode(n)).join(''); return node.children.map(n => serializeNode(n)).join('');
@@ -75,13 +93,14 @@ export const withVariables = (editor: CustomEditor) => {
return editor; return editor;
} }
export const insertVariable = (editor: CustomEditor, variableData: Pick<VariableElement, "name" | "value">) => { export const insertVariable = (editor: CustomEditor, variableData: Pick<VariableElement, "name" | "value">, stepsWithVariables: Record<string, unknown>[]) => {
const variable: VariableElement = { const variable: VariableElement = {
type: 'variable', type: 'variable',
name: variableData.name, name: humanizeVariableName(variableData.name as string, stepsWithVariables),
value: variableData.value, value: `{{${variableData.name}}}`,
children: [{ text: '' }], children: [{ text: '' }],
}; };
Transforms.insertNodes(editor, variable); Transforms.insertNodes(editor, variable);
Transforms.move(editor); Transforms.move(editor);
} }