Skip to content

Commit

Permalink
Merge pull request #46 from ls1intum/17-create-canvas
Browse files Browse the repository at this point in the history
feat: 17 create canvas
  • Loading branch information
egenerse authored Dec 19, 2024
2 parents 5d0f113 + 2d240f8 commit 24218ff
Show file tree
Hide file tree
Showing 11 changed files with 1,744 additions and 888 deletions.
57 changes: 49 additions & 8 deletions library/lib/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,55 @@
import { useState } from "react"
import {
ReactFlow,
ReactFlowInstance,
ReactFlowProvider,
type Node,
type Edge,
Background,
BackgroundVariant,
Controls,
MiniMap,
} from "@xyflow/react"

export function App() {
const [count, setCount] = useState(0)
import "@xyflow/react/dist/style.css"
import { MAX_SCALE_TO_ZOOM_IN, MIN_SCALE_TO_ZOOM_OUT } from "./contants"

const initialNodes: Node[] = [
{ id: "1", position: { x: 0, y: 0 }, data: { label: "1" } },
{ id: "2", position: { x: 0, y: 100 }, data: { label: "2" } },
]
const initialEdges: Edge[] = [{ id: "e1-2", source: "1", target: "2" }]

interface AppProps {
onReactFlowInit: (instance: ReactFlowInstance) => void
}

function App({ onReactFlowInit }: AppProps) {
return (
<div>
Library Count:{count}
<button onClick={() => setCount(count + 1)}>
Increment From Library
</button>
<div style={{ width: "100vw", height: "100vh" }}>
<ReactFlow
nodes={initialNodes}
edges={initialEdges}
onInit={onReactFlowInit}
fitView
minZoom={MIN_SCALE_TO_ZOOM_OUT}
maxZoom={MAX_SCALE_TO_ZOOM_IN}
>
<Background variant={BackgroundVariant.Lines} />
<MiniMap zoomable pannable />
<Controls />
</ReactFlow>
</div>
)
}

export function AppWithProvider({
onReactFlowInit,
}: {
onReactFlowInit: (instance: ReactFlowInstance) => void
}) {
return (
<ReactFlowProvider>
<App onReactFlowInit={onReactFlowInit} />
</ReactFlowProvider>
)
}
2 changes: 2 additions & 0 deletions library/lib/contants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const MIN_SCALE_TO_ZOOM_OUT = 0.4
export const MAX_SCALE_TO_ZOOM_IN = 2.5
20 changes: 16 additions & 4 deletions library/lib/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import ReactDOM from "react-dom/client"
import { App } from "./App"
import { AppWithProvider } from "./App"
import { ReactFlowInstance, type Node } from "@xyflow/react"
export { type Node } from "@xyflow/react"

export class Apollon2 {
private root: ReactDOM.Root | null = null
private reactFlowInstance: ReactFlowInstance | null = null

constructor(element: HTMLElement) {
this.root = ReactDOM.createRoot(element)
this.root.render(<App />)
this.root.render(
<AppWithProvider onReactFlowInit={this.setReactFlowInstance.bind(this)} />
)
}

public getRandomNumber(): number {
return Math.random() * 100
private setReactFlowInstance(instance: ReactFlowInstance) {
this.reactFlowInstance = instance
}

public getNodes(): Node[] {
if (this.reactFlowInstance) {
return this.reactFlowInstance?.getNodes()
}
return []
}

public dispose() {
Expand Down
8 changes: 6 additions & 2 deletions library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"lint": "eslint .",
"lint:fix": "eslint --fix ."
},
"dependencies": {
"@xyflow/react": "12.3.6"
},
"peerDependencies": {
"react": "18.3.1",
"react-dom": "18.3.1"
Expand All @@ -29,7 +32,8 @@
"globals": "15.13.0",
"typescript": "5.7.2",
"typescript-eslint": "8.18.1",
"vite": "6.0.1",
"vite-plugin-dts": "4.3.0"
"vite": "6.0.3",
"vite-plugin-dts": "4.3.0",
"vite-plugin-lib-inject-css": "^2.1.1"
}
}
3 changes: 2 additions & 1 deletion library/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { defineConfig } from "vite"
import react from "@vitejs/plugin-react"
import dts from "vite-plugin-dts"
import { resolve } from "path"
import { libInjectCss } from "vite-plugin-lib-inject-css"

export default defineConfig({
plugins: [react(), dts({ include: ["lib"] })],
plugins: [react(), dts({ include: ["lib"] }), libInjectCss()],
build: {
copyPublicDir: false,
lib: {
Expand Down
Loading

0 comments on commit 24218ff

Please sign in to comment.