Line data Source code
1 : import { useToast } from '@/components/ui/toast/toastStore'
2 : import { t } from '@/i18n'
3 : import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph'
4 : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
5 : import { useNodeZIndex } from '@/renderer/extensions/vueNodes/composables/useNodeZIndex'
6 : import { isSelectOnly } from '@/utils/litegraphUtil'
7 :
8 : /**
9 : * Extract & Promisify Litegraph.createNode to allow for positioning
10 : * @param canvas
11 : * @param name
12 : */
13 6 : export async function createNode(
14 : canvas: LGraphCanvas,
15 : name: string
16 : ): Promise<LGraphNode | null> {
17 6 : if (!name) {
18 0 : return null
19 : }
20 6 : if (isSelectOnly(canvas)) {
21 0 : return null
22 : }
23 :
24 6 : const {
25 : graph,
26 : graph_mouse: [posX, posY]
27 : } = canvas
28 6 : const newNode = LiteGraph.createNode(name)
29 6 : await new Promise((r) => setTimeout(r, 0))
30 :
31 6 : if (newNode && graph) {
32 6 : newNode.pos = [posX, posY]
33 6 : const addedNode = graph.add(newNode) ?? null
34 :
35 6 : if (addedNode) {
36 6 : useNodeZIndex().bringNodeToFront(addedNode.id)
37 6 : graph.change()
38 : }
39 6 : return addedNode
40 : } else {
41 0 : useToast().warning(t('assetBrowser.failedToCreateNode'))
42 0 : return null
43 : }
44 : }
|