stable editor
This commit is contained in:
1 parent
b29596fe78
commit
a56c7ff964
18 files changed
+832
-433
No files matched your search
@@ -1,12 +1,17 @@
|
||||
import { FC } from "react";
|
||||
import { FC, useState } from "react";
|
||||
|
||||
import ReactFlow, { MiniMap, Controls } from 'react-flow-renderer';
|
||||
import { AutomationData } from "types/automations";
|
||||
import { AutomationSequenceNode } from "types/automations";
|
||||
import { AutomationTrigger } from 'types/automations/triggers';
|
||||
import { triggerToFlow } from "./triggerConvertor";
|
||||
import { DAGNode } from "./DAGNode";
|
||||
import { DAGFlowDims, TriggerMakerOptions, FlowData } from './types';
|
||||
import { DAGAutomationFlowDims, TriggerMakerOptions, FlowData, ModalState } from './types';
|
||||
import { DAGCircle } from './DAGCircle';
|
||||
import { sequenceToFlow } from "./sequenceConvertor";
|
||||
import { makeSequenceUpdater } from "./updater";
|
||||
import { NodeEditor } from "components/NodeEditor";
|
||||
import { MultiNodeEditor } from "components/MultiNodeEditors";
|
||||
import { Modal } from "components/Modal";
|
||||
|
||||
const nodeTypes = {
|
||||
'dagnode': DAGNode,
|
||||
@@ -14,26 +19,89 @@ const nodeTypes = {
|
||||
}
|
||||
|
||||
interface Props {
|
||||
automation: AutomationData;
|
||||
dims: DAGFlowDims;
|
||||
opts: {
|
||||
triggers: TriggerMakerOptions;
|
||||
}
|
||||
sequence: AutomationSequenceNode[];
|
||||
trigger: AutomationTrigger[];
|
||||
dims: DAGAutomationFlowDims;
|
||||
onTriggerUpdate: (t: AutomationTrigger[]) => void;
|
||||
onSequenceUpdate: (s: AutomationSequenceNode[]) => void;
|
||||
}
|
||||
|
||||
export const DAGFlow: FC<Props> = ({
|
||||
automation,
|
||||
opts,
|
||||
export const DAGAutomationFlow: FC<Props> = ({
|
||||
sequence,
|
||||
trigger,
|
||||
onTriggerUpdate,
|
||||
onSequenceUpdate,
|
||||
dims,
|
||||
}) => {
|
||||
// state
|
||||
const [modalState, setModalState] = useState<ModalState | null>(null);
|
||||
|
||||
// modal
|
||||
let modalBody = <></>
|
||||
if (modalState) {
|
||||
if (modalState.single) {
|
||||
modalBody = <NodeEditor
|
||||
node={modalState.node}
|
||||
onClose={() => setModalState(null)}
|
||||
onSave={n => { modalState.update(n as any); setModalState(null); }}
|
||||
allowedTypes={modalState.allowedTypes}
|
||||
saveBtnCreateText={modalState.saveBtnCreateText}
|
||||
/>
|
||||
} else {
|
||||
modalBody = <MultiNodeEditor
|
||||
sequence={modalState.node}
|
||||
onClose={() => setModalState(null)}
|
||||
onSave={(n) => {
|
||||
modalState.update(n as any);
|
||||
setModalState(null);
|
||||
}}
|
||||
allowedTypes={['condition']}
|
||||
/>
|
||||
}
|
||||
}
|
||||
|
||||
// convert automation to flow data
|
||||
const condPoint = makeConditionPoint(dims);
|
||||
const flowData: FlowData = {
|
||||
nodes: [condPoint],
|
||||
edges: [],
|
||||
}
|
||||
triggerToFlow(flowData, automation.trigger, condPoint.id, dims, opts.triggers);
|
||||
sequenceToFlow(flowData, automation.sequence, condPoint.id, dims);
|
||||
return (
|
||||
triggerToFlow(flowData, trigger, condPoint.id, dims, {
|
||||
onAdd: () => onTriggerUpdate([
|
||||
...trigger, {
|
||||
platform: 'event',
|
||||
$smType: 'trigger',
|
||||
event_type: 'test',
|
||||
}
|
||||
]),
|
||||
onDelete: i => onTriggerUpdate([
|
||||
...trigger.slice(0, i),
|
||||
...trigger.slice(i + 1)
|
||||
]),
|
||||
onEdit: i => setModalState({
|
||||
single: true,
|
||||
node: trigger[i],
|
||||
update: t => onTriggerUpdate([
|
||||
...trigger.slice(0, i),
|
||||
t,
|
||||
...trigger.slice(i + 1)
|
||||
]),
|
||||
allowedTypes: ['trigger'],
|
||||
})
|
||||
});
|
||||
sequenceToFlow(
|
||||
flowData,
|
||||
sequence,
|
||||
condPoint.id,
|
||||
dims,
|
||||
makeSequenceUpdater(sequence, onSequenceUpdate, setModalState),
|
||||
);
|
||||
|
||||
// render
|
||||
return <>
|
||||
<Modal open={!!modalState}>
|
||||
{modalBody}
|
||||
</Modal>
|
||||
<ReactFlow
|
||||
nodeTypes={nodeTypes}
|
||||
snapToGrid
|
||||
@@ -45,7 +113,7 @@ export const DAGFlow: FC<Props> = ({
|
||||
<MiniMap />
|
||||
<Controls />
|
||||
</ReactFlow>
|
||||
);
|
||||
</>;
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +122,7 @@ const makeConditionPoint = ({
|
||||
nodeWidth,
|
||||
nodeHeight,
|
||||
circleSize,
|
||||
}: DAGFlowDims) => ({
|
||||
}: DAGAutomationFlowDims) => ({
|
||||
id: `c`,
|
||||
type: 'dagcircle',
|
||||
position: { x: padding.x + nodeWidth * 2, y: padding.y + nodeHeight * 0.25 },
|
||||
|
||||
Reference in new issue
Block a user