MCPcopy Create free account
hub / github.com/Comfy-Org/litegraph.js / inner_onMenuAdded

Method inner_onMenuAdded

src/LGraphCanvas.ts:976–1049  ·  view source on GitHub ↗
(base_category: string, prev_menu?: ContextMenu<string>)

Source from the content-addressed store, hash-verified

974 }
975
976 function inner_onMenuAdded(base_category: string, prev_menu?: ContextMenu<string>): void {
977 if (!graph) return
978
979 const categories = LiteGraph
980 .getNodeTypesCategories(canvas.filter || graph.filter)
981 .filter(category => category.startsWith(base_category))
982 const entries: AddNodeMenu[] = []
983
984 for (const category of categories) {
985 if (!category) continue
986
987 const base_category_regex = new RegExp(`^(${base_category})`)
988 const category_name = category
989 .replace(base_category_regex, "")
990 .split("/", 1)[0]
991 const category_path =
992 base_category === ""
993 ? `${category_name}/`
994 : `${base_category}${category_name}/`
995
996 let name = category_name
997 // in case it has a namespace like "shader::math/rand" it hides the namespace
998 if (name.includes("::")) name = name.split("::", 2)[1]
999
1000 const index = entries.findIndex(entry => entry.value === category_path)
1001 if (index === -1) {
1002 entries.push({
1003 value: category_path,
1004 content: name,
1005 has_submenu: true,
1006 callback: function (value, event, mouseEvent, contextMenu) {
1007 inner_onMenuAdded(value.value, contextMenu)
1008 },
1009 })
1010 }
1011 }
1012
1013 const nodes = LiteGraph.getNodeTypesInCategory(
1014 base_category.slice(0, -1),
1015 canvas.filter || graph.filter,
1016 )
1017
1018 for (const node of nodes) {
1019 if (node.skip_list) continue
1020
1021 const entry: AddNodeMenu = {
1022 value: node.type,
1023 content: node.title,
1024 has_submenu: false,
1025 callback: function (value, event, mouseEvent, contextMenu) {
1026 if (!canvas.graph) throw new NullGraphError()
1027
1028 const first_event = contextMenu.getFirstEvent()
1029 canvas.graph.beforeChange()
1030 const node = LiteGraph.createNode(value.value)
1031 if (node) {
1032 if (!first_event) throw new TypeError("Context menu event was null. This should not occur in normal usage.")
1033 node.pos = canvas.convertEventToCanvasOffset(first_event)

Callers

nothing calls this directly

Calls 8

getFirstEventMethod · 0.80
beforeChangeMethod · 0.80
createNodeMethod · 0.80
addMethod · 0.80
afterChangeMethod · 0.80

Tested by

no test coverage detected