File size: 2,724 Bytes
4450790
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { app } from "scripts/app.js";
import { rgthreeApi } from "rgthree/common/rgthree_api.js";

import type {
  LGraphCanvas as TLGraphCanvas,
  IWidget,
  LGraphNode,
  ContextMenuEventListener,
  ContextMenu,
  IContextMenuItem,
} from "../typings/litegraph.js";

const PASS_THROUGH = function <T extends any, I extends any>(item: T) {
  return item as T;
};

/**

 * Shows a lora chooser context menu.

 */
export async function showLoraChooser(

  event: PointerEvent,

  callback: ContextMenuEventListener,

  parentMenu?: ContextMenu | null,

  loras?: string[],

) {
  const canvas = app.canvas as TLGraphCanvas;
  if (!loras) {
    loras = ["None", ...(await rgthreeApi.getLoras())];
  }
  new LiteGraph.ContextMenu(loras, {
    event: event,
    parentMenu,
    title: "Choose a lora",
    scale: Math.max(1, canvas.ds?.scale ?? 1),
    className: "dark",
    callback,
  });
}

/**

 * Shows a context menu chooser of nodes.

 *

 * @param mapFn The function used to map each node to the context menu item. If null is returned

 *     it will be filtered out (rather than use a separate filter method).

 */
export function showNodesChooser<T extends IContextMenuItem>(
  event: PointerEvent,
  mapFn: (n: LGraphNode) => T | null,
  callback: ContextMenuEventListener,
  parentMenu?: ContextMenu,
) {
  const canvas = app.canvas as TLGraphCanvas;
  const nodesOptions: T[] = (app.graph._nodes as LGraphNode[])
    .map(mapFn)
    .filter((e): e is NonNullable<any> => e != null);

  nodesOptions.sort((a: any, b: any) => {
    return a.value - b.value;
  });

  new LiteGraph.ContextMenu(nodesOptions, {
    event: event,
    parentMenu,
    title: "Choose a node id",
    scale: Math.max(1, canvas.ds?.scale ?? 1),
    className: "dark",
    callback,
  });
}

/**

 * Shows a conmtext menu chooser for a specific node.

 *

 * @param mapFn The function used to map each node to the context menu item. If null is returned

 *     it will be filtered out (rather than use a separate filter method).

 */
export function showWidgetsChooser<T extends IContextMenuItem>(
  event: PointerEvent | MouseEvent,
  node: LGraphNode,
  mapFn: (n: IWidget) => T | null,
  callback: ContextMenuEventListener,
  parentMenu?: ContextMenu,
) {
  const options: T[] = (node.widgets || [])
    .map(mapFn)
    .filter((e): e is NonNullable<any> => e != null);
  if (options.length) {
    const canvas = app.canvas as TLGraphCanvas;
    new LiteGraph.ContextMenu(options, {
      event,
      parentMenu,
      title: "Choose an input/widget",
      scale: Math.max(1, canvas.ds?.scale ?? 1),
      className: "dark",
      callback,
    });
  }
}