| | import { useStore } from '@nanostores/react'; |
| | import { $templates } from 'features/nodes/store/nodesSlice'; |
| | import { NODE_WIDTH } from 'features/nodes/types/constants'; |
| | import type { AnyNode, InvocationTemplate } from 'features/nodes/types/invocation'; |
| | import { buildCurrentImageNode } from 'features/nodes/util/node/buildCurrentImageNode'; |
| | import { buildInvocationNode } from 'features/nodes/util/node/buildInvocationNode'; |
| | import { buildNotesNode } from 'features/nodes/util/node/buildNotesNode'; |
| | import { useCallback } from 'react'; |
| | import { useReactFlow } from 'reactflow'; |
| |
|
| | export const useBuildNode = () => { |
| | const templates = useStore($templates); |
| | const flow = useReactFlow(); |
| |
|
| | return useCallback( |
| | |
| | (type: string | 'current_image' | 'notes'): AnyNode => { |
| | let _x = window.innerWidth / 2; |
| | let _y = window.innerHeight / 2; |
| |
|
| | |
| | const rect = document.querySelector('#workflow-editor')?.getBoundingClientRect(); |
| |
|
| | if (rect) { |
| | _x = rect.width / 2 - NODE_WIDTH / 2 + rect.left; |
| | _y = rect.height / 2 - NODE_WIDTH / 2 + rect.top; |
| | } |
| |
|
| | const position = flow.screenToFlowPosition({ |
| | x: _x, |
| | y: _y, |
| | }); |
| |
|
| | if (type === 'current_image') { |
| | return buildCurrentImageNode(position); |
| | } |
| |
|
| | if (type === 'notes') { |
| | return buildNotesNode(position); |
| | } |
| |
|
| | |
| | |
| | const template = templates[type] as InvocationTemplate; |
| |
|
| | return buildInvocationNode(position, template); |
| | }, |
| | [templates, flow] |
| | ); |
| | }; |
| |
|