-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from ls1intum/24-see-components-from-sidebar
feat: 24 see components from sidebar
- Loading branch information
Showing
25 changed files
with
803 additions
and
139 deletions.
There are no files selected for viewing
This file contains 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 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,18 @@ | ||
import React from "react" | ||
|
||
interface DividerProps { | ||
style?: React.CSSProperties | ||
} | ||
|
||
export const Divider: React.FC<DividerProps> = ({ style }) => { | ||
return ( | ||
<div | ||
style={{ | ||
height: "2px", | ||
width: "100%", | ||
backgroundColor: "#000", | ||
...style, | ||
}} | ||
/> | ||
) | ||
} |
This file contains 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,65 @@ | ||
import React, { DragEvent } from "react" | ||
import { dropElementConfig, transformScale } from "@/constant" | ||
import { Divider } from "./Divider" | ||
import { DropNodeData } from "@/types" | ||
|
||
const onDragStart = (event: DragEvent, { type, data }: DropNodeData) => { | ||
const rect = (event.target as HTMLElement).getBoundingClientRect() | ||
const offsetX = (event.clientX - rect.left) / transformScale // Cursor offset from the element's left | ||
const offsetY = (event.clientY - rect.top) / transformScale // Cursor offset from the element's top | ||
|
||
// Pass the offset along with the type and data | ||
event.dataTransfer.setData( | ||
"text/plain", | ||
JSON.stringify({ type, data, offsetX, offsetY }) | ||
) | ||
event.dataTransfer.effectAllowed = "move" | ||
} | ||
|
||
export const Sidebar = () => { | ||
return ( | ||
<aside style={{ height: "100%", backgroundColor: "#f0f0f0" }}> | ||
<div | ||
style={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
gap: "8px", | ||
margin: "10px", | ||
}} | ||
> | ||
{dropElementConfig.map((config) => ( | ||
<React.Fragment key={`${config.type}_${config.name}`}> | ||
{/* Add separator before the Color Description */} | ||
{config.type === "colorDescription" && ( | ||
<Divider style={{ margin: "3px 0" }} /> | ||
)} | ||
<div | ||
style={{ | ||
width: config.width * transformScale, | ||
height: config.height * transformScale, | ||
overflow: "hidden", | ||
zIndex: 2, | ||
}} | ||
draggable | ||
onDragStart={(event: DragEvent) => | ||
onDragStart(event, { | ||
type: config.type, | ||
data: config.defaultData, | ||
offsetX: 0, | ||
offsetY: 0, | ||
}) | ||
} | ||
> | ||
{React.createElement(config.svg, { | ||
width: config.width, | ||
height: config.height, | ||
...config.defaultData, | ||
transformScale, | ||
})} | ||
</div> | ||
</React.Fragment> | ||
))} | ||
</div> | ||
</aside> | ||
) | ||
} |
This file contains 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from "./Text" | ||
export * from "./ThemedElements" | ||
export * from "./Sidebar" | ||
export * from "./Divider" |
This file contains 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,87 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { ClassSVG, PackageSVG, ColorDescriptionSVG } from "@/svgs" | ||
import { generateUUID } from "@/utils" | ||
import { ClassType } from "@/types" | ||
import { DiagramNodeTypeKeys } from "@/nodes" | ||
|
||
export const transformScale = 0.8 | ||
const droppedElementWidth = 160 | ||
const droppedElementHeight = 110 | ||
|
||
export const dropElementConfig: { | ||
type: DiagramNodeTypeKeys | ||
name: string | ||
width: number | ||
height: number | ||
defaultData: Record<string, unknown> | ||
svg: React.FC<any> | ||
}[] = [ | ||
{ | ||
type: "package", | ||
name: "Package", | ||
width: droppedElementWidth, | ||
height: droppedElementHeight, | ||
defaultData: { name: "Package" }, | ||
svg: (props) => <PackageSVG {...props} />, | ||
}, | ||
{ | ||
type: "class", | ||
name: "Class", | ||
width: droppedElementWidth, | ||
height: droppedElementHeight, | ||
defaultData: { | ||
name: "Class", | ||
methods: [{ id: generateUUID(), name: "+ method()" }], | ||
attributes: [{ id: generateUUID(), name: "+ attribute: Type" }], | ||
// strokeWidth: 1, | ||
}, | ||
svg: (props) => <ClassSVG {...props} />, | ||
}, | ||
{ | ||
type: "class", | ||
name: "Abstract", | ||
width: droppedElementWidth, | ||
height: droppedElementHeight, | ||
defaultData: { | ||
name: "Abstract", | ||
stereotype: ClassType.Abstract, | ||
methods: [{ id: generateUUID(), name: "+ method()" }], | ||
attributes: [{ id: generateUUID(), name: "+ attribute: Type" }], | ||
}, | ||
svg: (props) => <ClassSVG {...props} />, | ||
}, | ||
{ | ||
type: "class", | ||
name: "Enumeration", | ||
width: droppedElementWidth, | ||
height: droppedElementHeight, | ||
defaultData: { | ||
name: "Enumeration", | ||
stereotype: ClassType.Enumeration, | ||
methods: [{ id: generateUUID(), name: "+ method()" }], | ||
attributes: [{ id: generateUUID(), name: "+ attribute: Type" }], | ||
}, | ||
svg: (props) => <ClassSVG {...props} />, | ||
}, | ||
{ | ||
type: "class", | ||
name: "Interface", | ||
width: droppedElementWidth, | ||
height: droppedElementHeight, | ||
defaultData: { | ||
name: "Interface", | ||
stereotype: ClassType.Interface, | ||
methods: [{ id: generateUUID(), name: "+ method()" }], | ||
attributes: [{ id: generateUUID(), name: "+ attribute: Type" }], | ||
}, | ||
svg: (props) => <ClassSVG {...props} />, | ||
}, | ||
{ | ||
type: "colorDescription", | ||
name: "Color Description", | ||
width: droppedElementWidth, | ||
height: 48, | ||
defaultData: { description: "Color Description" }, | ||
svg: (props) => <ColorDescriptionSVG {...props} />, | ||
}, | ||
] |
This file contains 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 @@ | ||
export * from "./useDragDrop" |
This file contains 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,57 @@ | ||
import { dropElementConfig } from "@/constant" | ||
import { DropNodeData } from "@/types" | ||
import { generateUUID } from "@/utils" | ||
import { useReactFlow, type Node } from "@xyflow/react" | ||
import { useCallback, DragEvent } from "react" | ||
|
||
export const useDragDrop = () => { | ||
const { screenToFlowPosition, setNodes } = useReactFlow() | ||
|
||
const onDrop = useCallback( | ||
(event: DragEvent) => { | ||
event.preventDefault() | ||
const dropData = JSON.parse( | ||
event.dataTransfer.getData("text/plain") | ||
) as DropNodeData | ||
|
||
const config = dropElementConfig.find( | ||
(config) => config.type === dropData.type | ||
) | ||
// Validate the dropped element type | ||
if (!config) { | ||
console.warn(`Unknown drop element type: ${dropData.type}`) | ||
return | ||
} | ||
// Convert the drop position to the flow position | ||
const dropPosition = screenToFlowPosition({ | ||
x: event.clientX, | ||
y: event.clientY, | ||
}) | ||
|
||
// Adjust position by subtracting the offset | ||
const position = { | ||
x: dropPosition.x - dropData.offsetX, | ||
y: dropPosition.y - dropData.offsetY, | ||
} | ||
|
||
const newNode: Node = { | ||
width: config.width, | ||
height: config.height, | ||
id: generateUUID(), | ||
type: dropData.type, | ||
position, | ||
data: { ...config.defaultData, ...dropData.data }, | ||
} | ||
|
||
setNodes((nds) => nds.concat(newNode)) | ||
}, | ||
[screenToFlowPosition, setNodes] | ||
) | ||
|
||
const onDragOver = useCallback((event: DragEvent) => { | ||
event.preventDefault() | ||
event.dataTransfer.dropEffect = "move" | ||
}, []) | ||
|
||
return { onDrop, onDragOver } | ||
} |
This file contains 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 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,31 @@ | ||
import { NodeProps, NodeResizer, type Node } from "@xyflow/react" | ||
import { DefaultNodeWrapper } from "@/nodes/wrappers" | ||
import { TitleAndDescriptionSVG } from "@/svgs" | ||
|
||
type Props = Node<{ | ||
description?: string | ||
title: string | ||
}> | ||
|
||
export function TitleAndDesctiption({ | ||
selected, | ||
width, | ||
height, | ||
data: { description, title }, | ||
}: NodeProps<Props>) { | ||
if (!width || !height) { | ||
return null | ||
} | ||
|
||
return ( | ||
<DefaultNodeWrapper> | ||
<TitleAndDescriptionSVG | ||
width={width} | ||
height={height} | ||
title={title} | ||
description={description || ""} | ||
/> | ||
<NodeResizer isVisible={selected} minHeight={200} /> | ||
</DefaultNodeWrapper> | ||
) | ||
} |
Oops, something went wrong.