Skip to content
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

context menu refactoring #5

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions df_designer_front/src/CustomNodes/GenericNode/ContextMenuItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import * as ContextMenu from "@radix-ui/react-context-menu";
import {
BoxSelect,
ClipboardPasteIcon,
Combine,
Copy,
FileText,
LayoutGrid,
LucideIcon,
Settings2,
Trash2,
} from "lucide-react";

interface ItemProps {
Icon: LucideIcon;
text: string;
hotKey?: string;
}

interface IMenuProps {
type:
| "copy"
| "paste"
| "toggleGrid"
| "createPreset"
| "selectAll"
| "settings"
| "doc"
| "delete";
onClick: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
hide?: boolean;
disabled?: boolean;
}

const items: { [item: string]: ItemProps } = {
copy: {
Icon: Copy,
text: "Copy",
hotKey: "Ctrl + C",
},
paste: {
Icon: ClipboardPasteIcon,
text: "Paste",
hotKey: "Ctrl + V",
},
toggleGrid: {
Icon: LayoutGrid,
text: "Show/hide grid",
hotKey: "Shift+G",
},
createPreset: {
Icon: Combine,
text: "Create preset",
hotKey: "Ctrl+G",
},
selectAll: {
Icon: BoxSelect,
text: "Select all",
hotKey: "Ctrl+A",
},
settings: {
Icon: Settings2,
text: "Settings",
hotKey: "",
},
doc: {
Icon: FileText,
text: "Doc",
hotKey: "",
},
delete: {
Icon: Trash2,
text: "Delete",
hotKey: "Del",
},
};

export const ContextMenuItem = ({
type,
onClick,
disabled = false,
hide = false,
}: IMenuProps) => {
if (hide) return null;

const { Icon, text, hotKey } = items[type];

return (
<ContextMenu.Item
disabled={disabled}
onClick={onClick}
className={`context-item ${disabled && "context-item-disabled"}`}
>
<div className="flex flex-row items-center gap-1">
<Icon className="h-4 w-4" />
<p>{text}</p>
</div>
{hotKey && <span className="text-neutral-400"> {hotKey} </span>}
</ContextMenu.Item>
);
};
120 changes: 52 additions & 68 deletions df_designer_front/src/CustomNodes/GenericNode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import EditNodeModal from "../../modals/EditNodeModal";
import _ from "lodash";
import { ManageIcon } from "../../icons/ManageIcon";
import EditLinkModal from "../../modals/editLinkModal";
import { ContextMenuItem } from "./ContextMenuItem";

export default function GenericNode({
data,
Expand Down Expand Up @@ -628,10 +629,15 @@ export default function GenericNode({
// e.preventDefault();
// const node = flows.find((flow) => flow.id === tabId).data.nodes.find((node: NodeType) => node.id === data.id)
// console.log(node)
setLastCopiedSelection(_.cloneDeep(lastSelection))
// console.log(lastCopiedSelection)
setLastCopiedSelection(_.cloneDeep(lastSelection))
setSuccessData({ title: "Node was succesfully copied!" })
}
const openSettings = (e) => openPopUp(
data.node.base_classes[0] === 'links'
? <EditLinkModal data={data} />
: <EditNodeModal data={data} />
)


return (
Expand Down Expand Up @@ -848,79 +854,57 @@ export default function GenericNode({
</ContextMenu.Trigger>
<ContextMenu.Portal container={document.getElementById('modal_root')}>
<ContextMenu.Content className="context-wrapper">
{data.id !== "LOCAL_NODE" && data.id !== "GLOBAL_NODE" && (
<>
<ContextMenu.Item onClick={e => copy(e)}
className=" context-item">
<div className="flex flex-row items-center gap-1">
<CopyIcon />
<p>Copy</p>
</div>
<span className="text-neutral-400"> Ctrl+C </span>
</ContextMenu.Item>
{/* <ContextMenu.Item disabled onClick={e => {
let bounds = document.getElementById('reactFlowWrapper').getBoundingClientRect()
// console.log(bounds)
const node = reactFlowInstance.getNode(data.id)
// console.log(node)
// console.log(getNodePositionWithOrigin(node))
const pos = getNodePositionWithOrigin(node)
// console.log(node)
deleteNode(data.id)
setTimeout(() => {
paste(
lastCopiedSelection,
{
x: node.position.x,
y: node.position.y
})
}, 20);
}}
className=" context-item context-item-disabled">
<div className="flex flex-row items-center gap-1">
<ReplaceIcon className="w-4 h-4" />
<p>Paste to replace</p>
</div>
<span className="text-neutral-400"> Ctrl+Shift+V </span>
</ContextMenu.Item> */}
{/* <ContextMenu.Item disabled onClick={e => { }}
className=" context-item context-item-disabled">
<div className="flex flex-row items-center gap-1">
<Combine className="w-4 h-4" />
<p>Create preset</p>
</div>
<span className="text-neutral-400"> Shift+A </span>
</ContextMenu.Item> */}
</>
)}
<ContextMenu.Item onSelect={e => e.preventDefault()} onClick={e => openPopUp(data.node.base_classes[0] === 'links' ? <EditLinkModal data={data} /> : <EditNodeModal data={data} />)}
className=" context-item "
>
{/* <ContextMenu.Item disabled onClick={e => {
let bounds = document.getElementById('reactFlowWrapper').getBoundingClientRect()
// console.log(bounds)
const node = reactFlowInstance.getNode(data.id)
// console.log(node)
// console.log(getNodePositionWithOrigin(node))
const pos = getNodePositionWithOrigin(node)
// console.log(node)
deleteNode(data.id)
setTimeout(() => {
paste(
lastCopiedSelection,
{
x: node.position.x,
y: node.position.y
})
}, 20);
}}
className=" context-item context-item-disabled">
<div className="flex flex-row items-center gap-1">
<Settings2 className="w-4 h-4" />
<p>Settings</p>
<ReplaceIcon className="w-4 h-4" />
<p>Paste to replace</p>
</div>
{/* <span className="text-neutral-400"> </span> */}
</ContextMenu.Item>
<ContextMenu.Item className=" context-item ">
<span className="text-neutral-400"> Ctrl+Shift+V </span>
</ContextMenu.Item> */}
{/* <ContextMenu.Item disabled onClick={e => { }}
className=" context-item context-item-disabled">
<div className="flex flex-row items-center gap-1">
<FileText className="w-4 h-4" />
<p>Doc</p>
<Combine className="w-4 h-4" />
<p>Create preset</p>
</div>
{/* <span className="text-neutral-400"> Ctrl+C </span> */}
</ContextMenu.Item>
<span className="text-neutral-400"> Shift+A </span>
</ContextMenu.Item> */}
<ContextMenuItem
type='copy'
onClick={copy}
hide={data.id === "LOCAL_NODE" || data.id === "GLOBAL_NODE"}
/>
<ContextMenuItem
type='settings'
onClick={openSettings}
/>
<ContextMenuItem type="doc" onClick={_.noop} />
{data.id !== "LOCAL_NODE" && data.id !== "GLOBAL_NODE" && (
<>
<ContextMenu.Separator className="w-[90%] mx-auto h-[1px] bg-[#666] " />
<ContextMenu.Item onClick={e => { deleteNode(data.id) }} className=" context-item ">
<div className="flex flex-row items-center gap-1">
<Trash2 className="w-4 h-4" />
<p>Delete</p>
</div>
<span className="text-neutral-400"> Del </span>
</ContextMenu.Item>
</>
<ContextMenu.Separator className="w-[90%] mx-auto h-[1px] bg-[#666] " />
)}
<ContextMenuItem
type="delete"
onClick={e => deleteNode(data.id)}
hide={data.id === "LOCAL_NODE" || data.id === "GLOBAL_NODE"}
/>
</ContextMenu.Content>
</ContextMenu.Portal>
</ContextMenu.Root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { darkContext } from "../../../../contexts/darkContext";
import dagre from 'dagre';
import LayoutFlow from "../LayoutComponent";
import { useNavigate } from "react-router-dom";
import { ContextMenuItem } from "../../../../CustomNodes/GenericNode/ContextMenuItem";

const nodeDefTypes = {
genericNode: GenericNode,
Expand Down Expand Up @@ -731,7 +732,37 @@ export default function Page({ flow }: { flow: FlowType }) {
// console.log(fe)
// }, [])


const copy = (e) => {
setLastCopiedSelection(_.cloneDeep(lastSelection));
}
const pasteNode = (e) => {
// let bounds = reactFlowWrapper.current.getBoundingClientRect();
// const nodesPositions = flow.data.nodes.map((node: NodeType) => node.position)
// if (!(lastSelection.nodes.map((node) => node.id).includes("GLOBAL_NODE") || lastSelection.nodes.map((node) => node.id).includes("LOCAL_NODE"))) {
// if (!isMouseOnNode) {
// if ((nodesPositions[0].x) < (position.x - bounds.left) && (position.x - bounds.left) < (nodesPositions[0].x + 384)) {
// // console.log('1')
// }
// paste(lastCopiedSelection, {
// x: position.x - bounds.left,
// y: position.y - bounds.top,
// });
// // console.log('first')
// } else {
// setErrorData({ title: "You can't paste node over node! Nodes can't intersect!" })
// }
// } else setErrorData({ title: "You can't paste Global/Local Node copy!" })
if (lastCopiedSelection) {
setIsCloneVisible(true);
getNodesSelectionZone(lastCopiedSelection.nodes);
getRightestNode(lastCopiedSelection.nodes);
}
}
const createPreset = (e) => {
if (lastSelection?.nodes?.length > 0)
openPopUp(<AddPresetModal lastSelection={lastSelection} />);
}

return (
<>
{isCloneVisible && (
Expand Down Expand Up @@ -836,76 +867,26 @@ export default function Page({ flow }: { flow: FlowType }) {
</ContextMenu.Trigger>
<ContextMenu.Portal container={document.getElementById('modal_root')}>
<ContextMenu.Content className="context-wrapper">
{(lastSelection?.nodes?.length > 0 || lastSelection?.edges?.length > 0) && (
<ContextMenu.Item onClick={e => {
setLastCopiedSelection(_.cloneDeep(lastSelection))
}} className=" context-item " >
<div className="flex flex-row items-center gap-1">
<Copy className="w-4 h-4" />
<p>Copy</p>
</div>
<span className="text-neutral-400"> Ctrl+C </span>
</ContextMenu.Item>
)}
{lastCopiedSelection?.nodes?.length > 0 && (
<ContextMenu.Item onClick={e => {
// let bounds = reactFlowWrapper.current.getBoundingClientRect();
// const nodesPositions = flow.data.nodes.map((node: NodeType) => node.position)
// if (!(lastSelection.nodes.map((node) => node.id).includes("GLOBAL_NODE") || lastSelection.nodes.map((node) => node.id).includes("LOCAL_NODE"))) {
// if (!isMouseOnNode) {
// if ((nodesPositions[0].x) < (position.x - bounds.left) && (position.x - bounds.left) < (nodesPositions[0].x + 384)) {
// // console.log('1')
// }
// paste(lastCopiedSelection, {
// x: position.x - bounds.left,
// y: position.y - bounds.top,
// });
// // console.log('first')
// } else {
// setErrorData({ title: "You can't paste node over node! Nodes can't intersect!" })
// }
// } else setErrorData({ title: "You can't paste Global/Local Node copy!" })
if (
lastCopiedSelection
) {
setIsCloneVisible(true)
getNodesSelectionZone(lastCopiedSelection.nodes)
getRightestNode(lastCopiedSelection.nodes)
}
}}
className=" context-item ">
<div className="flex flex-row items-center gap-1">
<ClipboardPasteIcon className="w-4 h-4" />
<p>Paste</p>
</div>
<span className="text-neutral-400"> Ctrl+V </span>
</ContextMenu.Item>
)}
<ContextMenu.Item onClick={e => setGrid(!grid)} className=" context-item " >
<div className="flex flex-row items-center gap-1">
<LayoutGrid className="w-4 h-4" />
<p>Show/hide grid</p>
</div>
<span className="text-neutral-400"> Shift+G </span>
</ContextMenu.Item>
<ContextMenu.Item disabled={lastSelection?.nodes?.length <= 0} onClick={e => { if (lastSelection?.nodes?.length > 0) openPopUp(<AddPresetModal lastSelection={lastSelection} />) }}
className={` context-item ${lastSelection?.nodes?.length <= 0 && 'context-item-disabled'} `}>
<div className="flex flex-row items-center gap-1">
<Combine className="w-4 h-4" />
<p>Create preset</p>
</div>
<span className="text-neutral-400"> Ctrl+G </span>
</ContextMenu.Item>
<ContextMenu.Item onClick={e => {
// onSelectionChange({nodes: flow.data.nodes, edges: flow.data.edges})
selectAllHandler()
}} className=" context-item " >
<div className="flex flex-row items-center gap-1">
<BoxSelect className="w-4 h-4" />
<p>Select all</p>
</div>
<span className="text-neutral-400"> Ctrl+A </span>
</ContextMenu.Item>
<ContextMenuItem
type="copy"
onClick={copy}
hide={!lastSelection?.nodes?.length && !lastSelection?.edges?.length}
/>
<ContextMenuItem
type="paste"
onClick={pasteNode}
hide={!lastCopiedSelection?.nodes?.length}
/>
<ContextMenuItem type="toggleGrid" onClick={(e) => setGrid(!grid)} />
<ContextMenuItem
type="createPreset"
disabled={lastSelection?.nodes?.length <= 0}
onClick={createPreset}
/>
<ContextMenuItem
type="selectAll"
onClick={selectAllHandler}
/>
</ContextMenu.Content>
</ContextMenu.Portal>
</ContextMenu.Root>
Expand Down