Line data Source code
1 : import { resolveNodeDefText, st, te } from '@/i18n'
2 : import { legacyMenuCompat } from '@/lib/litegraph/src/contextMenuCompat'
3 : import type {
4 : IContextMenuOptions,
5 : IContextMenuValue,
6 : INodeInputSlot,
7 : IWidget
8 : } from '@/lib/litegraph/src/litegraph'
9 : import { LGraphCanvas, LiteGraph } from '@/lib/litegraph/src/litegraph'
10 : import { app } from '@/scripts/app'
11 :
12 1224 : export function translateContextMenuItems(
13 : values: readonly (IContextMenuValue | string | null)[] | undefined,
14 : options: IContextMenuOptions
15 : ) {
16 1224 : if (!values) return
17 150 : const reInput = /Convert (.*) to input/
18 150 : const reWidget = /Convert (.*) to widget/
19 150 : const cvt = st('contextMenu.Convert ', 'Convert ')
20 150 : const tinp = st('contextMenu. to input', ' to input')
21 150 : const twgt = st('contextMenu. to widget', ' to widget')
22 1224 : for (const value of values) {
23 1358 : if (typeof value === 'string') continue
24 :
25 1358 : translateContextMenuItems(value?.submenu?.options, options)
26 1358 : if (!value?.content) continue
27 944 : if (te(`contextMenu.${value.content}`)) {
28 602 : value.content = st(`contextMenu.${value.content}`, value.content)
29 : }
30 :
31 1358 : const extraInfo = (options.extra || options.parentMenu?.options.extra) as
32 : | { inputs?: INodeInputSlot[]; widgets?: IWidget[] }
33 : | undefined
34 1358 : const inputs = extraInfo?.inputs ?? []
35 1358 : const widgets = extraInfo?.widgets ?? []
36 1358 : const labelFor = (name: string) =>
37 : inputs.find((input) => input.name === name)?.label ??
38 : widgets.find((widget) => widget.name === name)?.label ??
39 : name
40 1358 : const matchInput = value.content.match(reInput)
41 1358 : if (matchInput) {
42 0 : value.content = cvt + labelFor(matchInput[1]) + tinp
43 0 : continue
44 : }
45 845 : const matchWidget = value.content.match(reWidget)
46 1080 : if (matchWidget) {
47 0 : value.content = cvt + labelFor(matchWidget[1]) + twgt
48 : }
49 : }
50 : }
51 :
52 : /**
53 : * Add translation for litegraph context menu.
54 : */
55 2355 : export const useContextMenuTranslation = () => {
56 : // Install compatibility layer BEFORE any extensions load
57 2355 : legacyMenuCompat.install(LGraphCanvas.prototype, 'getCanvasMenuOptions')
58 :
59 2355 : const { getCanvasMenuOptions } = LGraphCanvas.prototype
60 2355 : const getCanvasCenterMenuOptions = function (
61 : this: LGraphCanvas,
62 : ...args: Parameters<typeof getCanvasMenuOptions>
63 : ) {
64 32 : const res: (IContextMenuValue | null)[] = getCanvasMenuOptions.apply(
65 : this,
66 : args
67 : )
68 :
69 : // Add items from new extension API
70 32 : const newApiItems = app.collectCanvasMenuItems(this)
71 32 : for (const item of newApiItems) {
72 111 : res.push(item)
73 : }
74 :
75 : // Add legacy monkey-patched items
76 32 : const legacyItems = legacyMenuCompat.extractLegacyItems(
77 : 'getCanvasMenuOptions',
78 : this,
79 : ...args
80 : )
81 32 : for (const item of legacyItems) {
82 0 : res.push(item)
83 : }
84 :
85 : // Translate all items
86 32 : for (const item of res) {
87 207 : if (item?.content) {
88 175 : item.content = st(`contextMenu.${item.content}`, item.content)
89 : }
90 : }
91 32 : return res
92 : }
93 :
94 2355 : LGraphCanvas.prototype.getCanvasMenuOptions = getCanvasCenterMenuOptions
95 :
96 2355 : legacyMenuCompat.registerWrapper(
97 : 'getCanvasMenuOptions',
98 : getCanvasCenterMenuOptions,
99 : getCanvasMenuOptions,
100 : LGraphCanvas.prototype
101 : )
102 :
103 : // Install compatibility layer for getNodeMenuOptions
104 2355 : legacyMenuCompat.install(LGraphCanvas.prototype, 'getNodeMenuOptions')
105 :
106 : // Wrap getNodeMenuOptions to add new API items
107 2355 : const nodeMenuFn = LGraphCanvas.prototype.getNodeMenuOptions
108 2355 : const getNodeMenuOptionsWithExtensions = function (
109 : this: LGraphCanvas,
110 : ...args: Parameters<typeof nodeMenuFn>
111 : ) {
112 2340 : const res = nodeMenuFn.apply(this, args) as (IContextMenuValue | null)[]
113 :
114 : // Add items from new extension API
115 2340 : const node = args[0]
116 2340 : const newApiItems = app.collectNodeMenuItems(node)
117 2340 : for (const item of newApiItems) {
118 0 : res.push(item)
119 : }
120 :
121 : // Add legacy monkey-patched items
122 2340 : const legacyItems = legacyMenuCompat.extractLegacyItems(
123 : 'getNodeMenuOptions',
124 : this,
125 : ...args
126 : )
127 2340 : for (const item of legacyItems) {
128 0 : res.push(item)
129 : }
130 :
131 2340 : return res
132 : }
133 :
134 2355 : LGraphCanvas.prototype.getNodeMenuOptions = getNodeMenuOptionsWithExtensions
135 :
136 2355 : legacyMenuCompat.registerWrapper(
137 : 'getNodeMenuOptions',
138 : getNodeMenuOptionsWithExtensions,
139 : nodeMenuFn,
140 : LGraphCanvas.prototype
141 : )
142 :
143 2355 : const OriginalContextMenu = LiteGraph.ContextMenu
144 128 : function ContextMenu(
145 : values: (IContextMenuValue | string)[],
146 : options: IContextMenuOptions
147 : ) {
148 128 : if (options.title) {
149 78 : options.title = resolveNodeDefText('display_name', options.title)
150 : }
151 128 : translateContextMenuItems(values, options)
152 : const ctx = new OriginalContextMenu(values, options)
153 128 : return ctx
154 : }
155 :
156 2355 : LiteGraph.ContextMenu = ContextMenu as unknown as typeof LiteGraph.ContextMenu
157 2355 : LiteGraph.ContextMenu.prototype = OriginalContextMenu.prototype
158 : }
|