-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { MacroNode } from "@flyde/core"; | ||
import { getVariables } from "./getInlineVariables"; | ||
|
||
export interface InlineValueConfig { | ||
type: "string" | "boolean" | "number" | "json" | "expression"; | ||
value: string; | ||
} | ||
|
||
export const InlineValue: MacroNode<InlineValueConfig> = { | ||
id: "InlineValue", | ||
displayName: "Inline Value", | ||
description: "A static value or JS expression", | ||
runFnBuilder: (config) => { | ||
return (inputs, outputs, adv) => { | ||
if (config.type === "expression") { | ||
try { | ||
const resFn = eval(`(inputs) => ${config.value}`); | ||
outputs.value.next(resFn(inputs)); | ||
} catch (e) { | ||
adv.onError(e); | ||
} | ||
} else { | ||
outputs.value.next(config.value); | ||
} | ||
}; | ||
}, | ||
definitionBuilder: (config) => { | ||
const inputNames = | ||
config.type === "expression" ? getVariables(config.value) : []; | ||
return { | ||
defaultStyle: { | ||
size: "small", | ||
icon: "code", | ||
}, | ||
displayName: "Inline Value", | ||
description: "A static value or JS expression", | ||
inputs: Object.fromEntries(inputNames.map((input) => [input, {}]) ?? []), | ||
outputs: { | ||
value: { | ||
displayName: "Value", | ||
description: "Emits the value configured", | ||
}, | ||
}, | ||
}; | ||
}, | ||
defaultData: { | ||
type: "string", | ||
value: "", | ||
}, | ||
editorComponentBundlePath: "../../../dist/ui/InlineValue.js", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { | ||
FormGroup, | ||
HTMLSelect, | ||
InputGroup, | ||
NumericInput, | ||
TextArea, | ||
} from "@blueprintjs/core"; | ||
import type { InlineValueConfig } from "./InlineValue.flyde"; | ||
import React, { useCallback, useMemo } from "react"; | ||
import { getVariables } from "./getInlineVariables"; | ||
|
||
const types: InlineValueConfig["type"][] = [ | ||
"string", | ||
"number", | ||
"json", | ||
"boolean", | ||
"expression", | ||
]; | ||
|
||
const defaultValuePerType = { | ||
string: (currValue: any) => `${currValue}`, | ||
number: (currValue: any) => | ||
isNaN(Number(currValue)) ? 0 : Number(currValue), | ||
json: (currValue: any) => JSON.stringify(currValue), | ||
boolean: (currValue: any) => !!currValue, | ||
expression: (currValue: any) => currValue, | ||
}; | ||
|
||
function InlineValueEditor(props: { | ||
value: InlineValueConfig; | ||
onChange: (value: InlineValueConfig) => void; | ||
}) { | ||
const { value, onChange } = props; | ||
|
||
const changeType = useCallback( | ||
(type) => { | ||
onChange({ value: defaultValuePerType[type](value.value), type }); | ||
}, | ||
[value, onChange] | ||
); | ||
|
||
const changeValue = useCallback( | ||
(_val) => { | ||
onChange({ ...value, value: _val }); | ||
}, | ||
[value, onChange] | ||
); | ||
|
||
const editorPanel = useMemo(() => { | ||
switch (value.type) { | ||
case "string": | ||
return ( | ||
<FormGroup label="Value:"> | ||
<InputGroup | ||
type="text" | ||
value={value.value} | ||
onChange={(e) => changeValue(e.target.value)} | ||
/> | ||
</FormGroup> | ||
); | ||
case "number": | ||
return ( | ||
<FormGroup label="Value:"> | ||
<NumericInput | ||
value={value.value} | ||
onValueChange={(e) => changeValue(e)} | ||
/> | ||
</FormGroup> | ||
); | ||
case "json": | ||
return ( | ||
<FormGroup label="Value:"> | ||
<TextArea | ||
value={value.value} | ||
onChange={(e) => changeValue(e.target.value)} | ||
/> | ||
</FormGroup> | ||
); | ||
case "boolean": | ||
return ( | ||
<FormGroup label="Value:"> | ||
<HTMLSelect | ||
value={value.value} | ||
onChange={(e) => changeValue(e.target.value === "true")} | ||
> | ||
<option value="true">true</option> | ||
<option value="false">false</option> | ||
</HTMLSelect> | ||
</FormGroup> | ||
); | ||
case "expression": { | ||
const vars = getVariables(value.value); | ||
return ( | ||
<> | ||
<FormGroup | ||
label="Value:" | ||
helperText={`Accepts any valid JS extension. `} | ||
> | ||
<TextArea | ||
value={value.value} | ||
fill | ||
onChange={(e) => changeValue(e.target.value)} | ||
/> | ||
</FormGroup> | ||
<div> | ||
{vars.length > 0 ? ( | ||
<small> | ||
External inputs exposed from this expression:{" "} | ||
<em>{vars.join(", ")}</em> | ||
</small> | ||
) : ( | ||
<small> | ||
Expose external inputs by using the "inputs" object. For | ||
example, "inputs.a + inputs.b" will expose 2 inputs, a and b, | ||
and sum them. | ||
</small> | ||
)} | ||
</div> | ||
</> | ||
); | ||
} | ||
} | ||
}, [value, changeValue]); | ||
|
||
return ( | ||
<div> | ||
<FormGroup label="Value type:"> | ||
<HTMLSelect | ||
value={value.type} | ||
onChange={(e) => | ||
changeType(e.target.value as InlineValueConfig["type"]) | ||
} | ||
> | ||
{types.map((type) => ( | ||
<option key={type} value={type}> | ||
{type} | ||
</option> | ||
))} | ||
</HTMLSelect> | ||
</FormGroup> | ||
{editorPanel} | ||
</div> | ||
); | ||
} | ||
|
||
export default InlineValueEditor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const getVariables = (code: string) => { | ||
return (code.match(/inputs\.([a-zA-Z]\w*)/g) || []).map((v) => | ||
v.replace(/inputs\./, "") | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters