-
Notifications
You must be signed in to change notification settings - Fork 17
refactor Canvas #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
refactor Canvas #189
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8f1c6cc
refactor Canvas
lihebi abc81df
check for null content before parse
lihebi d6873b2
update store & db inside reactflow provider
lihebi 0e424d6
abstract triggerUpdate a little
lihebi 24e2f72
remove toDelete in deletePod
lihebi 794c5bc
remove minHeight of ResultBlock
lihebi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
import { | ||
useCallback, | ||
useState, | ||
useRef, | ||
useContext, | ||
useEffect, | ||
memo, | ||
} from "react"; | ||
import * as React from "react"; | ||
import ReactFlow, { | ||
addEdge, | ||
applyEdgeChanges, | ||
applyNodeChanges, | ||
Background, | ||
MiniMap, | ||
Controls, | ||
Handle, | ||
useReactFlow, | ||
Position, | ||
ConnectionMode, | ||
MarkerType, | ||
Node, | ||
} from "reactflow"; | ||
import "reactflow/dist/style.css"; | ||
|
||
import Box from "@mui/material/Box"; | ||
import InputBase from "@mui/material/InputBase"; | ||
import CircularProgress from "@mui/material/CircularProgress"; | ||
import Tooltip from "@mui/material/Tooltip"; | ||
import IconButton from "@mui/material/IconButton"; | ||
import Grid from "@mui/material/Grid"; | ||
import DeleteIcon from "@mui/icons-material/Delete"; | ||
import Moveable from "react-moveable"; | ||
|
||
import { useStore } from "zustand"; | ||
|
||
import { RepoContext, RoleType } from "../../lib/store"; | ||
|
||
interface Props { | ||
data: any; | ||
id: string; | ||
isConnectable: boolean; | ||
selected: boolean; | ||
// note that xPos and yPos are the absolute position of the node | ||
xPos: number; | ||
yPos: number; | ||
} | ||
|
||
export const ScopeNode = memo<Props>(function ScopeNode({ | ||
data, | ||
id, | ||
isConnectable, | ||
selected, | ||
}) { | ||
// add resize to the node | ||
const ref = useRef(null); | ||
const store = useContext(RepoContext); | ||
if (!store) throw new Error("Missing BearContext.Provider in the tree"); | ||
const reactFlowInstance = useReactFlow(); | ||
const setPodName = useStore(store, (state) => state.setPodName); | ||
const setPodGeo = useStore(store, (state) => state.setPodGeo); | ||
const [target, setTarget] = React.useState<any>(); | ||
const nodesMap = useStore(store, (state) => state.ydoc.getMap<Node>("pods")); | ||
const [frame] = React.useState({ | ||
translate: [0, 0], | ||
}); | ||
// const selected = useStore(store, (state) => state.pods[id]?.selected); | ||
const role = useStore(store, (state) => state.role); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
const onResize = useCallback(({ width, height, offx, offy }) => { | ||
const node = nodesMap.get(id); | ||
if (node) { | ||
node.style = { ...node.style, width, height }; | ||
node.position.x += offx; | ||
node.position.y += offy; | ||
nodesMap.set(id, node); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
setTarget(ref.current); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!data.name) return; | ||
setPodName({ id, name: data.name || "" }); | ||
if (inputRef?.current) { | ||
inputRef.current.value = data.name; | ||
} | ||
}, [data.name, id, setPodName]); | ||
|
||
return ( | ||
<Box | ||
ref={ref} | ||
sx={{ | ||
width: "100%", | ||
height: "100%", | ||
border: "solid 1px #d6dee6", | ||
borderRadius: "4px", | ||
}} | ||
className="custom-drag-handle" | ||
> | ||
<Box | ||
sx={{ | ||
display: "flex", | ||
marginLeft: "10px", | ||
borderRadius: "4px", | ||
position: "absolute", | ||
border: "solid 1px #d6dee6", | ||
right: "25px", | ||
top: "-15px", | ||
background: "white", | ||
zIndex: 250, | ||
justifyContent: "center", | ||
}} | ||
> | ||
{role !== RoleType.GUEST && ( | ||
<Tooltip title="Delete" className="nodrag"> | ||
<IconButton | ||
size="small" | ||
onClick={(e: any) => { | ||
// This does not work, will throw "Parent node | ||
// jqgdsz2ns6k57vich0bf not found" when deleting a scope. | ||
// | ||
// nodesMap.delete(id); | ||
// | ||
// But this works: | ||
reactFlowInstance.deleteElements({ nodes: [{ id }] }); | ||
}} | ||
> | ||
<DeleteIcon fontSize="inherit" /> | ||
</IconButton> | ||
</Tooltip> | ||
)} | ||
</Box> | ||
<Handle | ||
type="source" | ||
position={Position.Top} | ||
id="top" | ||
isConnectable={isConnectable} | ||
/> | ||
{/* The header of scope nodes. */} | ||
<Box | ||
className="custom-drag-handle" | ||
// bgcolor={"rgb(225,225,225)"} | ||
sx={{ display: "flex" }} | ||
> | ||
<Grid container spacing={2} sx={{ alignItems: "center" }}> | ||
<Grid item xs={4}> | ||
{/* <IconButton size="small"> | ||
<CircleIcon sx={{ color: "red" }} fontSize="inherit" /> | ||
</IconButton> */} | ||
</Grid> | ||
<Grid item xs={4}> | ||
<Box | ||
sx={{ | ||
display: "flex", | ||
flexGrow: 1, | ||
justifyContent: "center", | ||
}} | ||
> | ||
<InputBase | ||
className="nodrag" | ||
defaultValue={data.name || "Scope"} | ||
onBlur={(e) => { | ||
const name = e.target.value; | ||
if (name === data.name) return; | ||
const node = nodesMap.get(id); | ||
if (node) { | ||
nodesMap.set(id, { | ||
...node, | ||
data: { ...node.data, name }, | ||
}); | ||
} | ||
// setPodName({ id, name }); | ||
}} | ||
inputRef={inputRef} | ||
disabled={role === RoleType.GUEST} | ||
inputProps={{ | ||
style: { | ||
padding: "0px", | ||
textAlign: "center", | ||
textOverflow: "ellipsis", | ||
}, | ||
}} | ||
></InputBase> | ||
</Box> | ||
</Grid> | ||
<Grid item xs={4}></Grid> | ||
</Grid> | ||
</Box> | ||
<Handle | ||
type="source" | ||
position={Position.Bottom} | ||
id="bottom" | ||
isConnectable={isConnectable} | ||
/> | ||
<Handle | ||
type="source" | ||
position={Position.Left} | ||
id="left" | ||
isConnectable={isConnectable} | ||
/> | ||
<Handle | ||
type="source" | ||
position={Position.Right} | ||
id="right" | ||
isConnectable={isConnectable} | ||
/> | ||
{selected && role !== RoleType.GUEST && ( | ||
<Moveable | ||
target={target} | ||
resizable={true} | ||
keepRatio={false} | ||
throttleResize={1} | ||
renderDirections={["e", "s", "se"]} | ||
edge={false} | ||
zoom={1} | ||
origin={false} | ||
padding={{ left: 0, top: 0, right: 0, bottom: 0 }} | ||
onResizeStart={(e) => { | ||
e.setOrigin(["%", "%"]); | ||
e.dragStart && e.dragStart.set(frame.translate); | ||
}} | ||
onResize={(e) => { | ||
const beforeTranslate = e.drag.beforeTranslate; | ||
frame.translate = beforeTranslate; | ||
e.target.style.width = `${e.width}px`; | ||
e.target.style.height = `${e.height}px`; | ||
e.target.style.transform = `translate(${beforeTranslate[0]}px, ${beforeTranslate[1]}px)`; | ||
onResize({ | ||
width: e.width, | ||
height: e.height, | ||
offx: beforeTranslate[0], | ||
offy: beforeTranslate[1], | ||
}); | ||
setPodGeo(id, { | ||
width: e.width, | ||
height: e.height, | ||
}); | ||
}} | ||
/> | ||
)} | ||
</Box> | ||
); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@li-xin-yi
nodesMap.delete
andreactFlowInstance.deleteElements
are indeed different, te latter seems to support recursive deletion of a scope.