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

feat: 17 create canvas #46

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
56 changes: 48 additions & 8 deletions library/lib/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,54 @@
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"

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={0.6}
maxZoom={2}
>
<Background variant={BackgroundVariant.Lines} />
<MiniMap zoomable pannable />
<Controls />
</ReactFlow>
</div>
)
}

export function AppWithProvider({
onReactFlowInit,
}: {
onReactFlowInit: (instance: ReactFlowInstance) => void
}) {
return (
<ReactFlowProvider>
<App onReactFlowInit={onReactFlowInit} />
</ReactFlowProvider>
)
}
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
Loading