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