functional editor

This commit is contained in:
ari committed 2021-12-15 15:31:26 -05:00
1 parent 8dfbb9faa9
commit 1ea5f83590
5 files changed
+110 -65

No files matched your search

+4 -1
View File
@@ -17,7 +17,10 @@ export const DAGCircle = ({
onEdit,
onAdd,
}: AddProps) => {
const { classes } = useCircleStyles({ size: size * 0.9 });
const { classes } = useCircleStyles({
size: size * 0.9,
hasOnFunction: !!onEdit || !!onAdd
});
console.log({ onEdit })
return <>
<foreignObject x={x} y={y} width={size} height={size}>
+9 -4
View File
@@ -149,15 +149,20 @@ export const useNodeStyles = createAppUseStyles<{
}
})
export const useCircleStyles = createAppUseStyles<{ size: number }>(theme => ({
root: ({size}) => ({
export const useCircleStyles = createAppUseStyles<{
size: number,
hasOnFunction: boolean,
}>(theme => ({
root: ({size, hasOnFunction}) => ({
backgroundColor: Color(theme.primary).set('rgb.g', 100).hex(),
borderRadius: 500,
height: size,
width: size,
cursor: "pointer",
cursor: hasOnFunction ? "pointer" : 'default',
"&:hover": {
backgroundColor: Color(theme.primary).set('rgb.g', 150).hex(),
backgroundColor: hasOnFunction ?
Color(theme.primary).set('rgb.g', 150).hex() :
Color(theme.primary).set('rgb.g', 100).hex(),
},
fontSize: `${size * 0.4}px`,
display: 'flex',
+36 -18
View File
@@ -1,23 +1,48 @@
import { DAGBoard } from "components/DAGSvgs/DAGBoard";
import { FC, useState } from "react";
import { AutomationSequenceNode } from 'types/automations';
import { useTheme } from "styles/theme";
import { SequenceNodeProps } from './types';
import { SequenceNodeProps, ModalState } 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;
}
import { MultiNodeEditor } from "components/MultiNodeEditors";
export const SequenceNodes: FC<SequenceNodeProps & { zoomLevel: number }> = (props) => {
const theme = useTheme();
const [modalState, setModalState] = useState<ModalState | null>(null);
console.log({ modalState });
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.onlyConditions ? ['condition'] : ['action', 'condition']}
/>
} else {
modalBody = <MultiNodeEditor
sequence={modalState.node}
onClose={() => setModalState(null)}
onSave={(i, n) => {
modalState.update([
...modalState.node.slice(0, i),
n as any,
...modalState.node.slice(i + 1),
]); setModalState(null)
}}
onAdd={n => modalState.update([...modalState.node, n as any])}
onRemove={(i) => {
modalState.update([
...modalState.node.slice(0, i),
...modalState.node.slice(i + 1),
]);
}}
allowedTypes={modalState.onlyConditions ? ['condition'] : ['action', 'condition']}
/>
}
}
return <>
<DAGBoard
zoomLevel={props.zoomLevel}
@@ -25,9 +50,7 @@ export const SequenceNodes: FC<SequenceNodeProps & { zoomLevel: number }> = (pro
props.startPoint,
props.sequence,
props.onChange,
(node, update, onlyConditions) => setModalState({
node, update, onlyConditions,
})
s => setModalState(s)
)}
settings={{
...props.dims,
@@ -36,12 +59,7 @@ export const SequenceNodes: FC<SequenceNodeProps & { zoomLevel: number }> = (pro
}}
/>
<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']}
/>}
{modalBody}
</Modal>
</>
}
@@ -4,11 +4,11 @@ import { Point } from "types/graphs";
import { makeUpdater, Updater } from './updater';
import { ChooseAction } from 'types/automations/actions';
import { AutomationCondition } from "types/automations/conditions";
import { ModalState } from "./types";
import { AutomationNode } from '../../types/automations/index';
export type UpdateNode = (
node: AutomationSequenceNode,
updateNode: (n: AutomationSequenceNode) => void,
onlyConditions: boolean,
export type UpdateModalState = (
s: ModalState
) => void;
const offsetPoint = (
@@ -44,8 +44,8 @@ function* dealWithNested(
innerStartLoc: Point,
updater: Updater,
sequence: AutomationSequenceNode[],
openModal: UpdateNode,
onEdit: () => void,
openModal: UpdateModalState,
onEdit?: () => void,
): Generator<DAGElement> {
const np = offsetPoint(innerStartLoc, [0.5, 0]);
yield {
@@ -57,7 +57,7 @@ function* dealWithNested(
yield {
type: 'circle',
loc: innerStartLoc,
onClick: ['edit', onEdit],
onClick: onEdit ? ['edit', onEdit]: undefined,
}
yield {
type: 'edge',
@@ -82,7 +82,7 @@ function* dealWithChoose(
nodeLoc: Point,
makeUpdater: (j: number | null) => Updater,
updateData: (data: ChooseAction['action_data']) => void,
openModal: UpdateNode,
openModal: UpdateModalState,
offset: ReturnType<typeof makeOffsetMaintainer>,
): Generator<DAGElement> {
offset.resetY()
@@ -96,23 +96,10 @@ function* dealWithChoose(
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,
onModalOpenForConditions(
openModal, j,
node.action_data,
updateData,
),
)) {
yield elm
@@ -134,8 +121,7 @@ function* dealWithChoose(
innerStartLoc,
makeUpdater(null),
node.action_data.default,
openModal,
() => {}
openModal
)) {
yield elm
if (elm.type === 'node') {
@@ -168,7 +154,7 @@ export function* computeNodesEdgesPos(
startPoint: Point,
sequence: AutomationSequenceNode[],
onChange: (s: AutomationSequenceNode[]) => void,
openModal: UpdateNode,
openModal: UpdateModalState,
): Generator<DAGElement> {
const rootUpdater = makeUpdater(sequence, onChange);
const offset = makeOffsetMaintainer(startPoint)
@@ -181,7 +167,7 @@ export function* computeNodesEdgesPos(
type: 'node',
loc: nodeLoc,
node,
onOpen: () => openModal(node, n => rootUpdater.updateNode(i, n), false),
onOpen: onModalOpenForNode(openModal, i, node, rootUpdater),
onRemove: () => rootUpdater.removeNode(i),
onAdd: i === sequence.length - 1 ? rootUpdater.addNode : undefined,
}
@@ -208,3 +194,38 @@ export function* computeNodesEdgesPos(
}
}
}
export const onModalOpenForNode = (
openModal: UpdateModalState,
i: number,
node: AutomationSequenceNode,
rootUpdater: Updater,
) => () => openModal({
single: true,
node,
onlyConditions: false,
update: n => rootUpdater.updateNode(i, n),
})
export const onModalOpenForConditions = (
openModal: UpdateModalState,
j: number,
data: ChooseAction['action_data'],
updateData: (data: ChooseAction['action_data']) => void,
) => () => openModal({
single: false,
node: data.choose[j].conditions,
onlyConditions: true,
update: conditions => updateData({
...data,
choose: [
...data.choose.slice(0, j),
{
sequence: data.choose[j].sequence,
conditions: conditions as AutomationCondition[],
},
...data.choose.slice(j+1),
]
}),
})
+11 -13
View File
@@ -9,16 +9,14 @@ export interface SequenceNodeProps {
onChange: (s: AutomationSequenceNode[]) => void;
}
// export interface SequenceNodeSettings extends BaseSequenceNodeSettings{
// sequence: AutomationSequenceNode[],
// onChange?: (s: AutomationSequenceNode[]) => void;
// }
// export interface BaseSequenceNodeSettings {
// startPoint: Point;
// nodeHeight: number;
// nodeWidth: number;
// distanceFactor: number;
// edgeColor: string;
// keyPrefix?: string;
// }
export type ModalState = {
single: true,
node: AutomationSequenceNode
update: (n: AutomationSequenceNode) => void;
onlyConditions: boolean;
} | {
single: false,
node: AutomationSequenceNode[]
update: (n: AutomationSequenceNode[]) => void;
onlyConditions: boolean;
}