simple modal node editor
This commit is contained in:
1 parent
90d4fbf8f6
commit
fde0f49688
6 files changed
+101
-35
No files matched your search
@@ -29,11 +29,13 @@ export const ActionChooseState: OptionManager<ChooseAction> = {
|
||||
label="Choose Sequence"
|
||||
value={state.action_data.choose}
|
||||
onChange={choose => update({ choose })}
|
||||
resizable
|
||||
/>
|
||||
<InputYaml
|
||||
label="Default Sequence"
|
||||
value={state.action_data.default}
|
||||
onChange={d => update({ default: d })}
|
||||
resizable
|
||||
/>
|
||||
</>
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import InputYaml from 'components/Inputs/InputYaml';
|
||||
import { ServiceAction } from "types/automations/actions";
|
||||
import { OptionManager, updateActionData } from './OptionManager';
|
||||
|
||||
@@ -35,7 +36,7 @@ export const ActionCallServiceState: OptionManager<ServiceAction> = {
|
||||
value={service}
|
||||
onChange={service => update({ service })}
|
||||
/>
|
||||
<InputText
|
||||
<InputYaml
|
||||
label="Target"
|
||||
value={target}
|
||||
onChange={target => update({ target })}
|
||||
|
||||
@@ -10,7 +10,7 @@ export default {
|
||||
component: SequenceNodes,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {
|
||||
zoomLevel: 1,
|
||||
zoomLevel: 1.5,
|
||||
startPoint: [0.5, 0.5],
|
||||
dims: {
|
||||
nodeHeight: NODE_HEIGHT,
|
||||
|
||||
@@ -1,23 +1,47 @@
|
||||
import { DAGBoard } from "components/DAGSvgs/DAGBoard";
|
||||
import { FC } from "react";
|
||||
import { FC, useState } from "react";
|
||||
import { AutomationSequenceNode } from 'types/automations';
|
||||
import { useTheme } from "styles/theme";
|
||||
import { SequenceNodeProps } from './types';
|
||||
import Color from 'chroma-js';
|
||||
import { computeNodesEdgesPos } from './positions';
|
||||
import { Modal } from 'components/Modal';
|
||||
import { NodeEditor } from 'components/NodeEditor';
|
||||
|
||||
interface ModalState {
|
||||
node: AutomationSequenceNode;
|
||||
update: (n: AutomationSequenceNode) => void;
|
||||
onlyConditions: boolean;
|
||||
}
|
||||
|
||||
export const SequenceNodes: FC<SequenceNodeProps & { zoomLevel: number }> = (props) => {
|
||||
const theme = useTheme();
|
||||
return <DAGBoard
|
||||
zoomLevel={props.zoomLevel}
|
||||
elements={computeNodesEdgesPos(
|
||||
props.startPoint,
|
||||
props.sequence,
|
||||
props.onChange
|
||||
)}
|
||||
settings={{
|
||||
...props.dims,
|
||||
edgeNextColor: theme.secondaryAccent,
|
||||
edgeChildColor: Color(theme.secondaryAccent).set('rgb.b', 200).hex(),
|
||||
}}
|
||||
/>
|
||||
const [modalState, setModalState] = useState<ModalState | null>(null);
|
||||
console.log({ modalState });
|
||||
return <>
|
||||
<DAGBoard
|
||||
zoomLevel={props.zoomLevel}
|
||||
elements={computeNodesEdgesPos(
|
||||
props.startPoint,
|
||||
props.sequence,
|
||||
props.onChange,
|
||||
(node, update, onlyConditions) => setModalState({
|
||||
node, update, onlyConditions,
|
||||
})
|
||||
)}
|
||||
settings={{
|
||||
...props.dims,
|
||||
edgeNextColor: theme.secondaryAccent,
|
||||
edgeChildColor: Color(theme.secondaryAccent).set('rgb.b', 200).hex(),
|
||||
}}
|
||||
/>
|
||||
<Modal open={!!modalState}>
|
||||
{modalState && <NodeEditor
|
||||
node={modalState.node}
|
||||
onClose={() => setModalState(null)}
|
||||
onSave={n => { modalState.update(n as any); setModalState(null) }}
|
||||
allowedTypes={modalState.onlyConditions ? ['condition'] : ['action', 'condition']}
|
||||
/>}
|
||||
</Modal>
|
||||
</>
|
||||
}
|
||||
@@ -2,7 +2,14 @@ import { DAGElement } from "components/DAGSvgs/DAGBoard";
|
||||
import { AutomationSequenceNode } from "types/automations";
|
||||
import { Point } from "types/graphs";
|
||||
import { makeUpdater, Updater } from './updater';
|
||||
import { ChooseAction } from '../../types/automations/actions';
|
||||
import { ChooseAction } from 'types/automations/actions';
|
||||
import { AutomationCondition } from "types/automations/conditions";
|
||||
|
||||
export type UpdateNode = (
|
||||
node: AutomationSequenceNode,
|
||||
updateNode: (n: AutomationSequenceNode) => void,
|
||||
onlyConditions: boolean,
|
||||
) => void;
|
||||
|
||||
const offsetPoint = (
|
||||
[x, y]: Point,
|
||||
@@ -37,7 +44,8 @@ function* dealWithNested(
|
||||
innerStartLoc: Point,
|
||||
updater: Updater,
|
||||
sequence: AutomationSequenceNode[],
|
||||
onEdit?: () => void,
|
||||
openModal: UpdateNode,
|
||||
onEdit: () => void,
|
||||
): Generator<DAGElement> {
|
||||
const np = offsetPoint(innerStartLoc, [0.5, 0]);
|
||||
yield {
|
||||
@@ -63,6 +71,7 @@ function* dealWithNested(
|
||||
np,
|
||||
sequence,
|
||||
updater.onChange,
|
||||
openModal,
|
||||
)) {
|
||||
yield childNode
|
||||
}
|
||||
@@ -72,19 +81,39 @@ function* dealWithChoose(
|
||||
node: ChooseAction,
|
||||
nodeLoc: Point,
|
||||
makeUpdater: (j: number | null) => Updater,
|
||||
onAdd: () => void,
|
||||
updateData: (data: ChooseAction['action_data']) => void,
|
||||
openModal: UpdateNode,
|
||||
offset: ReturnType<typeof makeOffsetMaintainer>,
|
||||
): Generator<DAGElement> {
|
||||
offset.resetY()
|
||||
// conditions
|
||||
for (let j = 0; j < node.action_data.choose.length; j++) {
|
||||
const innerStartLoc = offsetPoint(nodeLoc, [1, j + 1 + offset.y]);
|
||||
const updater = makeUpdater(j);
|
||||
for (let elm of dealWithNested(
|
||||
nodeLoc,
|
||||
innerStartLoc ,
|
||||
makeUpdater(j),
|
||||
updater,
|
||||
node.action_data.choose[j].sequence,
|
||||
() => {},
|
||||
openModal,
|
||||
() => openModal(
|
||||
node.action_data.choose[j].conditions[0] ?? {
|
||||
'$smType': 'condition', 'condition': 'and', 'condition_data': {
|
||||
'conditions': []
|
||||
}},
|
||||
(n) => updateData({
|
||||
...node.action_data,
|
||||
choose: [
|
||||
...node.action_data.choose.slice(0, j),
|
||||
{
|
||||
sequence: node.action_data.choose[j].sequence,
|
||||
conditions: [n] as AutomationCondition[],
|
||||
},
|
||||
...node.action_data.choose.slice(j+1),
|
||||
]
|
||||
}),
|
||||
true,
|
||||
),
|
||||
)) {
|
||||
yield elm
|
||||
if (elm.type === 'node') {
|
||||
@@ -105,6 +134,8 @@ function* dealWithChoose(
|
||||
innerStartLoc,
|
||||
makeUpdater(null),
|
||||
node.action_data.default,
|
||||
openModal,
|
||||
() => {}
|
||||
)) {
|
||||
yield elm
|
||||
if (elm.type === 'node') {
|
||||
@@ -119,7 +150,16 @@ function* dealWithChoose(
|
||||
yield {
|
||||
type: 'circle',
|
||||
loc: newCondLoc,
|
||||
onClick: ['add', onAdd],
|
||||
onClick: ['add', () => updateData({
|
||||
...node.action_data,
|
||||
choose: [
|
||||
...node.action_data.choose,
|
||||
{
|
||||
conditions: [],
|
||||
sequence: [],
|
||||
}
|
||||
]
|
||||
})],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +168,7 @@ export function* computeNodesEdgesPos(
|
||||
startPoint: Point,
|
||||
sequence: AutomationSequenceNode[],
|
||||
onChange: (s: AutomationSequenceNode[]) => void,
|
||||
openModal: UpdateNode,
|
||||
): Generator<DAGElement> {
|
||||
const rootUpdater = makeUpdater(sequence, onChange);
|
||||
const offset = makeOffsetMaintainer(startPoint)
|
||||
@@ -140,6 +181,7 @@ export function* computeNodesEdgesPos(
|
||||
type: 'node',
|
||||
loc: nodeLoc,
|
||||
node,
|
||||
onOpen: () => openModal(node, n => rootUpdater.updateNode(i, n), false),
|
||||
onRemove: () => rootUpdater.removeNode(i),
|
||||
onAdd: i === sequence.length - 1 ? rootUpdater.addNode : undefined,
|
||||
}
|
||||
@@ -159,19 +201,8 @@ export function* computeNodesEdgesPos(
|
||||
node,
|
||||
nodeLoc,
|
||||
j => rootUpdater.makeChildUpdaterForChooseAction(i, j),
|
||||
() => rootUpdater.updateNode(i, {
|
||||
...node,
|
||||
action_data: {
|
||||
...node.action_data,
|
||||
choose: [
|
||||
...node.action_data.choose,
|
||||
{
|
||||
conditions: [],
|
||||
sequence: [],
|
||||
}
|
||||
]
|
||||
}
|
||||
}),
|
||||
actionData => rootUpdater.updateChooseActionData(i, actionData),
|
||||
openModal,
|
||||
offset,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { AutomationSequenceNode } from "types/automations";
|
||||
import { ChooseAction } from "types/automations/actions";
|
||||
|
||||
export type Updater = ReturnType<typeof makeUpdater>;
|
||||
|
||||
@@ -34,6 +35,13 @@ export const makeUpdater = (
|
||||
...sequence.slice(i+1),
|
||||
])
|
||||
},
|
||||
updateChooseActionData(i: number, data: ChooseAction['action_data']) {
|
||||
const node = sequence[i] as ChooseAction;
|
||||
return this.updateNode(i, {
|
||||
...node,
|
||||
action_data: data,
|
||||
})
|
||||
},
|
||||
makeChildUpdaterForChooseAction(i: number, j:number | null) {
|
||||
const node = sequence[i];
|
||||
if ((node.$smType === 'action') && (node.action === 'choose')) {
|
||||
|
||||
Reference in new issue
Block a user