stable editor
This commit is contained in:
1 parent
b29596fe78
commit
a56c7ff964
18 files changed
+832
-433
No files matched your search
@@ -1,22 +1,32 @@
|
||||
import { useState } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { AutomationNode, AutomationNodeTypes } from 'types/automations';
|
||||
import { AutomationNodeSubtype } from 'types/automations';
|
||||
import { getOptionManager } from './getOptionManager';
|
||||
|
||||
|
||||
export const useEditorNodeState = (node: AutomationNode) => {
|
||||
const [state, setState] = useState(node);
|
||||
const [{ node: state, isModified }, setAllState] = useState({
|
||||
node,
|
||||
isModified: false,
|
||||
});
|
||||
useEffect(() => {
|
||||
setAllState({ node, isModified: false })
|
||||
}, [node])
|
||||
const setState = (node: AutomationNode) => setAllState(({ node, isModified: true }))
|
||||
const nodeType: AutomationNodeTypes = state.$smType ?? 'trigger';
|
||||
const subType: AutomationNodeSubtype =
|
||||
state.$smType === 'action' ? state.action :
|
||||
state.$smType === 'condition' ? state.condition :
|
||||
state.platform;
|
||||
const subTypes = getSubTypeList(nodeType)
|
||||
const subTypes = getSubTypeList(nodeType)
|
||||
const optionManager = getOptionManager(nodeType, subType as any);
|
||||
return {
|
||||
nodeType,
|
||||
subType,
|
||||
subTypes,
|
||||
get isModified() {
|
||||
return isModified
|
||||
},
|
||||
get data() {
|
||||
return state
|
||||
},
|
||||
@@ -101,7 +111,7 @@ const getSubTypeList = <T extends AutomationNodeTypes>(nodeType: T): AutomationN
|
||||
'time',
|
||||
'trigger',
|
||||
'zone',
|
||||
]as any
|
||||
] as any
|
||||
case 'trigger':
|
||||
return [
|
||||
'event',
|
||||
@@ -116,7 +126,7 @@ const getSubTypeList = <T extends AutomationNodeTypes>(nodeType: T): AutomationN
|
||||
'webhook',
|
||||
'zone',
|
||||
'device',
|
||||
]as any
|
||||
] as any
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -1,16 +1,27 @@
|
||||
.node-editor--root {
|
||||
display : flex;
|
||||
flex-direction : column;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items : center;
|
||||
height : 100%;
|
||||
align-items: center;
|
||||
height: 100%;
|
||||
padding: 1em;
|
||||
margin-top: 0.5em;
|
||||
}
|
||||
|
||||
.node-editor--root.modded {
|
||||
border: 1px solid var(--change);
|
||||
border-radius: 5px;
|
||||
}
|
||||
|
||||
.node-editor--root.modded * {
|
||||
border-color: var(--change);
|
||||
}
|
||||
|
||||
.node-editor--body-title {
|
||||
display : flex;
|
||||
flex-direction : row;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: space-between;
|
||||
margin-bottom : 6px;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
|
||||
.node-editor--body-title>* {
|
||||
@@ -18,21 +29,22 @@
|
||||
}
|
||||
|
||||
.node-editor--body {
|
||||
overflow : overlay;
|
||||
padding-left : 1%;
|
||||
overflow: overlay;
|
||||
padding-left: 1%;
|
||||
padding-right: 1%;
|
||||
|
||||
min-width : 100%;
|
||||
min-width: 100%;
|
||||
min-height: fit-content;
|
||||
}
|
||||
|
||||
.node-editor--footer {
|
||||
bottom : 0;
|
||||
left : 0;
|
||||
display : flex;
|
||||
flex-direction : row;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: space-between;
|
||||
align-items : center;
|
||||
width : 100%;
|
||||
min-height : 50px;
|
||||
}
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
min-height: 50px;
|
||||
min-width: 25vw;
|
||||
}
|
||||
@@ -11,6 +11,7 @@ export interface Props {
|
||||
allowedTypes?: Array<keyof AutomationNodeMapping>;
|
||||
onClose?: () => void;
|
||||
onSave?: (n: AutomationNode) => void;
|
||||
onFlags?: (isReady: boolean, isModified: boolean) => void;
|
||||
saveBtnCreateText?: boolean;
|
||||
}
|
||||
|
||||
@@ -19,11 +20,22 @@ export const NodeEditor: FC<Props> = ({
|
||||
allowedTypes = ["action", "condition", "trigger"],
|
||||
onClose = () => { },
|
||||
onSave = () => { },
|
||||
onFlags = () => { },
|
||||
saveBtnCreateText = false,
|
||||
children
|
||||
}) => {
|
||||
// state
|
||||
const state = useEditorNodeState(node);
|
||||
|
||||
return <div className="node-editor--root">
|
||||
// state
|
||||
const isModified = state.isModified;
|
||||
const isReady = state.isReady();
|
||||
|
||||
// events
|
||||
onFlags(isReady, isModified);
|
||||
|
||||
// render
|
||||
return <div className={["node-editor--root", isModified ? "modded" : ""].join(" ")}>
|
||||
<div className="node-editor--body">
|
||||
<div className="node-editor--body-title">
|
||||
{allowedTypes.length > 1 ? <InputList
|
||||
@@ -44,8 +56,9 @@ export const NodeEditor: FC<Props> = ({
|
||||
<div className="node-editor--footer">
|
||||
<Button onClick={onClose}>Close</Button>
|
||||
<Button onClick={() => {
|
||||
state.isReady() && onSave(state.data)
|
||||
}} disabled={!state.isReady()}>{saveBtnCreateText ? "Create" : "Save"}</Button>
|
||||
isReady && onSave(state.data)
|
||||
}} disabled={!isReady}>{saveBtnCreateText ? "Create" : "Save"}</Button>
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
Reference in new issue
Block a user