forked from finos/architecture-as-code
-
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.
Open node-detail-preview on node selection
- Loading branch information
1 parent
eafc9d4
commit fa3ec52
Showing
14 changed files
with
285 additions
and
187 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 |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
position: relative; | ||
display: flex; | ||
justify-content: center; | ||
} | ||
} |
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,89 +1,12 @@ | ||
import { useEffect, useState } from "react"; | ||
import './App.css' | ||
import { BrowserJsPlumbInstance, newInstance } from "@jsplumb/browser-ui" | ||
import FileUploader from "./components/fileuploader/FileUploader"; | ||
import { NodeLayout, RelationshipLayout } from "./layout"; | ||
import Graph from "./components/graph/Graph"; | ||
import { saveAs } from 'file-saver'; | ||
import { CALMNode } from "./types"; | ||
|
||
interface LayoutObject { | ||
'unique-id': string, | ||
x: number, | ||
y: number | ||
} | ||
import Drawer from './components/drawer/Drawer' | ||
|
||
function App() { | ||
const [nodes, setNodes] = useState<NodeLayout[]>([]); | ||
const [relationships, setRelationships] = useState<RelationshipLayout[]>([]); | ||
const [instance, setInstance] = useState<BrowserJsPlumbInstance>(); | ||
const [title, setTitle] = useState('Architecture as Code'); | ||
|
||
function enhanceNodesWithLayout(nodes: CALMNode[], coords: LayoutObject[]): NodeLayout[] { | ||
return nodes.map(node => { | ||
const coordObject = coords.find((coord) => coord["unique-id"] == node["unique-id"]); | ||
return { | ||
...node, | ||
x: coordObject?.x || 400, | ||
y: coordObject?.y || 400 | ||
} | ||
}); | ||
} | ||
|
||
async function handleFile(instanceFile: File, layoutFile?: File) { | ||
const instanceString = await instanceFile.text(); | ||
const calmInstance = JSON.parse(instanceString); | ||
setTitle(instanceFile.name); | ||
|
||
const nodePositions: LayoutObject[] = []; | ||
if (layoutFile) { | ||
const layoutString = await layoutFile.text(); | ||
const layout = JSON.parse(layoutString); | ||
layout.nodes.forEach((node: LayoutObject) => nodePositions.push(node)); | ||
} | ||
|
||
setNodes(enhanceNodesWithLayout(calmInstance.nodes, nodePositions)); | ||
setRelationships(calmInstance.relationships); | ||
} | ||
|
||
function onSave() { | ||
const outputNodes = nodes.map(node => { | ||
const bounds = document.getElementById(node["unique-id"])?.getBoundingClientRect(); | ||
|
||
return { | ||
"unique-id": node["unique-id"], | ||
"x": bounds?.x, | ||
"y": bounds?.y | ||
} | ||
}); | ||
|
||
const output = JSON.stringify({ | ||
nodes: outputNodes, | ||
relationships | ||
}); | ||
|
||
const blob = new Blob([output], {type: "text/plain;charset=utf-8"}); | ||
saveAs(blob, "layout.json"); | ||
} | ||
|
||
useEffect(() => { | ||
setInstance(newInstance({ | ||
container: document.getElementById('app')! | ||
})); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<h1>{title}</h1> | ||
|
||
<FileUploader callback={handleFile}/> | ||
|
||
<button onClick={onSave}>SaveAs</button> | ||
<div id="app"> | ||
{instance && <Graph instance={instance} nodes={nodes} relationships={relationships} />} | ||
</div> | ||
<Drawer /> | ||
</> | ||
); | ||
) | ||
} | ||
|
||
export default App |
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,130 @@ | ||
import Sidebar from '../sidebar/Sidebar' | ||
import { BrowserJsPlumbInstance, newInstance } from '@jsplumb/browser-ui' | ||
import saveAs from 'file-saver' | ||
import { useEffect, useState } from 'react' | ||
import { NodeLayout, RelationshipLayout } from '../../layout' | ||
import { CALMNode } from '../../types' | ||
import FileUploader from '../fileuploader/FileUploader' | ||
import Graph from '../graph/Graph' | ||
|
||
interface LayoutObject { | ||
'unique-id': string | ||
x: number | ||
y: number | ||
} | ||
|
||
function Drawer() { | ||
const [nodes, setNodes] = useState<NodeLayout[]>([]) | ||
const [relationships, setRelationships] = useState<RelationshipLayout[]>([]) | ||
const [instance, setInstance] = useState<BrowserJsPlumbInstance>() | ||
const [title, setTitle] = useState('Architecture as Code') | ||
const [selectedNode, setSelectedNode] = useState(null) | ||
|
||
const closeSidebar = () => { | ||
setSelectedNode(null) | ||
} | ||
|
||
function enhanceNodesWithLayout( | ||
nodes: CALMNode[], | ||
coords: LayoutObject[] | ||
): NodeLayout[] { | ||
return nodes.map((node) => { | ||
const coordObject = coords.find( | ||
(coord) => coord['unique-id'] == node['unique-id'] | ||
) | ||
return { | ||
...node, | ||
x: coordObject?.x || 400, | ||
y: coordObject?.y || 400, | ||
} | ||
}) | ||
} | ||
|
||
async function handleFile(instanceFile: File, layoutFile?: File) { | ||
const instanceString = await instanceFile.text() | ||
const calmInstance = JSON.parse(instanceString) | ||
setTitle(instanceFile.name) | ||
|
||
const nodePositions: LayoutObject[] = [] | ||
if (layoutFile) { | ||
const layoutString = await layoutFile.text() | ||
const layout = JSON.parse(layoutString) | ||
layout.nodes.forEach((node: LayoutObject) => | ||
nodePositions.push(node) | ||
) | ||
} | ||
|
||
setNodes(enhanceNodesWithLayout(calmInstance.nodes, nodePositions)) | ||
setRelationships(calmInstance.relationships) | ||
} | ||
|
||
function onSave() { | ||
const outputNodes = nodes.map((node) => { | ||
const bounds = document | ||
.getElementById(node['unique-id']) | ||
?.getBoundingClientRect() | ||
|
||
return { | ||
'unique-id': node['unique-id'], | ||
x: bounds?.x, | ||
y: bounds?.y, | ||
} | ||
}) | ||
|
||
const output = JSON.stringify({ | ||
nodes: outputNodes, | ||
relationships, | ||
}) | ||
|
||
const blob = new Blob([output], { type: 'text/plain;charset=utf-8' }) | ||
saveAs(blob, 'layout.json') | ||
} | ||
|
||
useEffect(() => { | ||
setInstance( | ||
newInstance({ | ||
container: document.getElementById('app')!, | ||
}) | ||
) | ||
}, []) | ||
|
||
return ( | ||
<> | ||
<div | ||
className={`drawer drawer-end ${selectedNode ? 'drawer-open' : ''}`} | ||
> | ||
<input | ||
type="checkbox" | ||
className="drawer-toggle" | ||
checked={!!selectedNode} | ||
onChange={closeSidebar} | ||
/> | ||
<div className="drawer-content"> | ||
<div className="text-xl font-bold">{title}</div> | ||
<FileUploader callback={handleFile} /> | ||
<button className="btn" onClick={onSave}> | ||
SaveAs | ||
</button> | ||
<div id="app"> | ||
{instance && ( | ||
<Graph | ||
instance={instance} | ||
nodes={nodes} | ||
relationships={relationships} | ||
setSelectedNode={setSelectedNode} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
{selectedNode && ( | ||
<Sidebar | ||
selectedNode={selectedNode} | ||
closeSidebar={closeSidebar} | ||
/> | ||
)} | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
export default Drawer |
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
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,34 @@ | ||
import { IoCloseOutline } from 'react-icons/io5' | ||
import { CALMNode } from '../../types' | ||
|
||
interface SidebarProps { | ||
selectedNode: CALMNode | ||
closeSidebar: () => void | ||
} | ||
|
||
function Sidebar({ selectedNode, closeSidebar }: SidebarProps) { | ||
return ( | ||
<div className="drawer-side"> | ||
<label | ||
htmlFor="node-details" | ||
className="drawer-overlay" | ||
onClick={closeSidebar} | ||
></label> | ||
<div className="menu bg-base-200 text-base-content min-h-full w-80 p-4"> | ||
<button | ||
onClick={closeSidebar} | ||
className="btn btn-square ml-auto" | ||
> | ||
<IoCloseOutline /> | ||
</button> | ||
<div className="text-xl font-bold">Node</div> | ||
<span>unique-id: {selectedNode['unique-id']}</span> | ||
<span>name: {selectedNode.name}</span> | ||
<span>node-type: {selectedNode['node-type']}</span> | ||
<span>description: {selectedNode.description}</span> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default Sidebar |
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,8 +1,8 @@ | ||
import { CALMNode, CALMRelationship } from "./types" | ||
import { CALMNode, CALMRelationship } from './types' | ||
|
||
export interface NodeLayout extends CALMNode { | ||
"x": number, | ||
"y": number | ||
x: number | ||
y: number | ||
} | ||
|
||
export type RelationshipLayout = CALMRelationship; | ||
export type RelationshipLayout = CALMRelationship |
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
Oops, something went wrong.