File size: 29,046 Bytes
1e3b872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
import { app } from "../../scripts/app.js";
import { tinyterraReloadNode, wait, rebootAPI, getConfig, convertToInput, hideWidget } from "./utils.js";
import { openFullscreenApp, _setDefaultFullscreenNode } from "./ttNfullscreen.js";

class TinyTerra extends EventTarget {
    constructor() {
        super();
        this.ctrlKey = false
        this.altKey = false
        this.shiftKey = false
        this.downKeys = {}
        this.processingMouseDown = false
        this.processingMouseUp = false
        this.processingMouseMove = false
        window.addEventListener("keydown", (e) => {
            this.handleKeydown(e)
        })
        window.addEventListener("keyup", (e) => {
            this.handleKeyup(e)
        })
        this.initialiseContextMenu()
        this.initialiseNodeMenu()
        this.injectTtnCss()
    }
    async initialiseContextMenu() {
        const that = this;
        setTimeout(async () => {
            const getCanvasMenuOptions = LGraphCanvas.prototype.getCanvasMenuOptions;
            LGraphCanvas.prototype.getCanvasMenuOptions = function (...args) {
                const options = getCanvasMenuOptions.apply(this, [...args]);
                options.push(null);
                options.push({
                    content: `🌏 tinyterraNodes`,
                    className: "ttN-contextmenu-item ttN-contextmenu-main-item",
                    submenu: {
                        options: that.getTinyTerraContextMenuItems(),
                    },
                });

                // Remove consecutive null entries
                let i = 0;
                while (i < options.length) {
                    if (options[i] === null && (i === 0 || options[i - 1] === null)) {
                    options.splice(i, 1);
                    } else {
                    i++;
                    }
                }
                return options;
            };
        }, 1000);
    }
    getTinyTerraContextMenuItems() {
        const that = this 
        return [
            {
                content: "🌏 Nodes",
                disabled: true,
                className: "tinyterra-contextmenu-item tinyterra-contextmenu-label",
            },
            {
                content: "base",
                className: "tinyterra-contextmenu-item",
                has_submenu: true,
                callback: (...args) => {
                    that.addTTNodeMenu('base/', args[3], args[2])
                }
            },
            {
                content: "pipe",
                className: "tinyterra-contextmenu-item",
                has_submenu: true,
                callback: (...args) => {
                    that.addTTNodeMenu('pipe/', args[3], args[2])
                }
            },
            {
                content: "xyPlot",
                className: "tinyterra-contextmenu-item",
                has_submenu: true,
                callback: (...args) => {
                    that.addTTNodeMenu('xyPlot/', args[3], args[2])
                }
            },
            {
                content: "text",
                className: "tinyterra-contextmenu-item",
                has_submenu: true,
                callback: (...args) => {
                    that.addTTNodeMenu('text/', args[3], args[2])
                }
            },
            {
                content: "image",
                className: "tinyterra-contextmenu-item",
                has_submenu: true,
                callback: (...args) => {
                    that.addTTNodeMenu('image/', args[3], args[2])
                }
            },
            {
                content: "util",
                className: "tinyterra-contextmenu-item",
                has_submenu: true,
                callback: (...args) => {
                    that.addTTNodeMenu('util/', args[3], args[2])
                }
            },
            {
                content: "🌏 Add Group",
                disabled: true,
                className: "tinyterra-contextmenu-item tinyterra-contextmenu-label",
            },
            {
                content: "Basic Sampling",
                className: "tinyterra-contextmenu-item",
                has_submenu: true,
                callback : function(value, event, mouseEvent, contextMenu){
                    that.addGroupMenu('basic', contextMenu, mouseEvent)
                }
            },
            {
                content: "Upscaling",
                className: "tinyterra-contextmenu-item",
                has_submenu: true,
                callback : function(value, event, mouseEvent, contextMenu){
                    that.addGroupMenu('upscale', contextMenu, mouseEvent)
                }
            },
            {
                content: "xyPlotting",
                className: "tinyterra-contextmenu-item",
                has_submenu: true,
                callback : function(value, event, mouseEvent, contextMenu){
                    that.addGroupMenu('xyPlot', contextMenu, mouseEvent)
                }
            },
            {
                content: "🌏 Extras",
                disabled: true,
                className: "tinyterra-contextmenu-item tinyterra-contextmenu-label",
            },
            {
                content: "βš™οΈ Settings (tinyterra)",
                disabled: true, //!!this.settingsDialog,
                className: "tinyterra-contextmenu-item",
                callback: (...args) => {
                    this.settingsDialog = new tinyterraConfigDialog().show();
                    this.settingsDialog.addEventListener("close", (e) => {
                        this.settingsDialog = null;
                    });
                },
            },
            {
                content: "πŸ›‘ Reboot Comfy",
                className: "tinyterra-contextmenu-item",
                callback: (...args) => {
                    rebootAPI();
                    wait(1000).then(() => {
                        window.location.reload();
                    });
                }
            },
            {
                content: "⭐ Star on Github",
                className: "tinyterra-contextmenu-item",
                callback: (...args) => {
                    window.open("https://github.com/TinyTerra/ComfyUI_tinyterraNodes", "_blank");
                },
            },
            {
                content: "β˜• Support TinyTerra",
                className: "tinyterra-contextmenu-item",
                callback: (...args) => {
                    window.open("https://buymeacoffee.com/tinyterra", "_blank");
                },
            },
            
        ];
    }
    addNode = async (node, pos) => {
        var canvas = LGraphCanvas.active_canvas;
        canvas.graph.beforeChange();
        var node = LiteGraph.createNode(node);
        if (node) {
            node.pos = pos;
            canvas.graph.add(node);
        }
        canvas.graph.afterChange();
        return node
    }
    addGroup = async (contextMenu, nodes) => {
        var first_event = contextMenu.getFirstEvent();
        var canvas = LGraphCanvas.active_canvas;
        var canvasOffset = canvas.convertEventToCanvasOffset(first_event);

        // Create Nodes
        for (const nodeData of Object.values(nodes)) {
            var node = await this.addNode(nodeData.nodeType, canvasOffset); 
            nodeData.graphNode = node;
            canvasOffset = [canvasOffset[0] + nodeData.width + 10, canvasOffset[1]];
        }

        // Handle Widget Changes
        for (const nodeData of Object.values(nodes)) {
            var node = nodeData.graphNode;
            if (nodeData.widgets) {
                for (const [widget, value] of Object.entries(nodeData.widgets)) {
                    if (value == 'toInput') {
                        const config = getConfig(widget, node)
                        convertToInput(node, node.widgets.find((w) => w.name === widget), config);
                    } else {
                        if (node) {
                            node.widgets.find((w) => w.name === widget).value = value
                        }
                    }
                }
            }
        }

        // Handle Connections
        for (const nodeData of Object.values(nodes)) {
            var node = nodeData.graphNode;
            if (nodeData.connections) {
                for (const c of nodeData.connections) {
                    node.connect(parseInt(c[0]), nodes[c[1]].graphNode.id, c[2]);
                }
            }
        }
    }
    addTTNodeMenu(category, prev_menu, e, callback=null) {
        var canvas = LGraphCanvas.active_canvas;
        var ref_window = canvas.getCanvasWindow();
        var graph = canvas.graph;
        const base_category = '🌏 tinyterra/' + category

        var entries = [];

        var nodes = LiteGraph.getNodeTypesInCategory(base_category.slice(0, -1), canvas.filter || graph.filter );
        nodes.map(function(node){
            if (node.skip_list)
                return;

            var entry = { 
                value: node.type, 
                content: node.title, 
                className: "tinyterra-contextmenu-item", 
                has_submenu: false, 
                callback : function(value, event, mouseEvent, contextMenu){
                    var first_event = contextMenu.getFirstEvent();
                    canvas.graph.beforeChange();
                    var node = LiteGraph.createNode(value.value);
                    if (node) {
                        node.pos = canvas.convertEventToCanvasOffset(first_event);
                        canvas.graph.add(node);
                    }
                    if(callback)
                        callback(node);
                    canvas.graph.afterChange();
                }
            }

            entries.push(entry);
        });

        new LiteGraph.ContextMenu( entries, { event: e, parentMenu: prev_menu }, ref_window );
    }
    addGroupMenu(group, prev_menu, e) {
        const that = this;
        var canvas = LGraphCanvas.active_canvas;
        var ref_window = canvas.getCanvasWindow();
        let entries;
        switch (group) {
            case "basic":
                entries = [
                    {   content: "Base ttN",
                        className: "tinyterra-contextmenu-item",
                        callback : async function(value, event, mouseEvent, contextMenu){
                            const nodes = {
                                'Loader': {
                                    nodeType: 'ttN tinyLoader',
                                    graphNode: null,
                                    width: 315,
                                    connections: [
                                        [0, 'Conditioning', 'model'],
                                        [1, 'KSampler', 'latent'],
                                        [2, 'KSampler', 'vae'],
                                        [3, 'Conditioning', 'clip'],
                                    ],
                                },
                                'Conditioning': {
                                    nodeType: 'ttN conditioning',
                                    graphNode: null,
                                    width: 400,
                                    connections: [
                                        [0, 'KSampler', 'model'],
                                        [1, 'KSampler', 'positive'],
                                        [2, 'KSampler', 'negative'],
                                        [3, 'KSampler', 'clip'],
                                    ],
                                },
                                'KSampler': {
                                    nodeType: 'ttN KSampler_v2',
                                    graphNode: null,
                                    width: 262,
                                    widgets: {
                                        image_output: 'Preview'
                                    }
                                }
                            }
                            that.addGroup(contextMenu, nodes)
                        }
                    },
                    {   content: "Pipe Basic",
                        className: "tinyterra-contextmenu-item",
                        callback : async function(value, event, mouseEvent, contextMenu){
                            const nodes = {
                                'Loader': {
                                    nodeType: 'ttN pipeLoader_v2',
                                    graphNode: null,
                                    width: 315,
                                    connections: [
                                        [0, 'KSampler', 'pipe']
                                    ],
                                },
                                'KSampler': {
                                    nodeType: 'ttN pipeKSampler_v2',
                                    graphNode: null,
                                    width: 262,
                                    widgets: {
                                        image_output: 'Preview'
                                    }
                                }
                            }
                            that.addGroup(contextMenu, nodes)
                        }
                    },
                    {   content: "Pipe SDXL",
                        className: "tinyterra-contextmenu-item",
                        callback : async function(value, event, mouseEvent, contextMenu){
                            const nodes = {
                                'Loader': {
                                    nodeType: 'ttN pipeLoaderSDXL_v2',
                                    graphNode: null,
                                    width: 365,
                                    connections: [
                                        [0, 'KSampler', 'sdxl_pipe']
                                    ],
                                },
                                'KSampler': {
                                    nodeType: 'ttN pipeKSamplerSDXL_v2',
                                    graphNode: null,
                                    width: 365,
                                    widgets: {
                                        image_output: 'Preview'
                                    }
                                }
                            }
                            that.addGroup(contextMenu, nodes)
                        }
                    },
                ];
                break;

            case "upscale":
                entries = [
                    {   content: "Base upscale",
                        className: "tinyterra-contextmenu-item",
                        callback : async function(value, event, mouseEvent, contextMenu){
                            const nodes = {
                                'Loader': {
                                    nodeType: 'ttN tinyLoader',
                                    graphNode: null,
                                    width: 315,
                                    connections: [
                                        [0, 'Conditioning', 'model'],
                                        [1, 'KSampler', 'latent'],
                                        [2, 'KSampler', 'vae'],
                                        [3, 'Conditioning', 'clip'],
                                    ],
                                },
                                'Conditioning': {
                                    nodeType: 'ttN conditioning',
                                    graphNode: null,
                                    width: 400,
                                    connections: [
                                        [0, 'KSampler', 'model'],
                                        [1, 'KSampler', 'positive'],
                                        [2, 'KSampler', 'negative'],
                                        [3, 'KSampler', 'clip'],
                                    ],
                                },
                                'KSampler': {
                                    nodeType: 'ttN KSampler_v2',
                                    graphNode: null,
                                    width: 262,
                                    connections: [
                                        [0, 'KSampler2', 'model'],
                                        [1, 'KSampler2', 'positive'],
                                        [2, 'KSampler2', 'negative'],
                                        [3, 'KSampler2', 'latent'],
                                        [4, 'KSampler2', 'vae'],
                                        [5, 'KSampler2', 'clip'],
                                        [6, 'KSampler2', 'input_image_override']
                                    ],
                                    widgets: {
                                        image_output: 'Preview',
                                    }
                                },
                                'KSampler2': {
                                    nodeType: 'ttN KSampler_v2',
                                    graphNode: null,
                                    width: 262,
                                    widgets: {
                                        upscale_method: '[hiresFix] nearest-exact',
                                        image_output: 'Preview',
                                        denoise: 0.5,
                                        steps: 15
                                    }
                                },
                            }
                            that.addGroup(contextMenu, nodes)
                        }
                    },
                    {   content: "Pipe Upscale",
                        className: "tinyterra-contextmenu-item",
                        callback : async function(value, event, mouseEvent, contextMenu){
                            const nodes = {
                                'loader1': {
                                    nodeType: 'ttN pipeLoader_v2',
                                    graphNode: null,
                                    width: 315,
                                    connections: [
                                        [0, 'ksampler', 'pipe']
                                    ],
                                },
                                'ksampler': {
                                    nodeType: 'ttN pipeKSampler_v2',
                                    graphNode: null,
                                    width: 262,
                                    connections: [
                                        [0, 'ksampler2', 'pipe']
                                    ],
                                    widgets: {
                                        image_output: 'Preview'
                                    },
                                },
                                'ksampler2': {
                                    nodeType: 'ttN pipeKSampler_v2',
                                    graphNode: null,
                                    width: 262,
                                    widgets: {
                                        upscale_method: '[hiresFix] nearest-exact',
                                        denoise: 0.5,
                                        seed: 'toInput',
                                        image_output: 'Preview'
                                    }
                                }
                            }
                            that.addGroup(contextMenu, nodes)
                        }
                    },
                ];
                break;

            case "xyPlot":
                entries = [
                    {   content: "Base xyPlot",
                        className: "tinyterra-contextmenu-item",
                        callback : async function(value, event, mouseEvent, contextMenu){
                            const nodes = {
                                'Loader': {
                                    nodeType: 'ttN tinyLoader',
                                    graphNode: null,
                                    width: 315,
                                    connections: [
                                        [0, 'Conditioning', 'model'],
                                        [1, 'KSampler', 'latent'],
                                        [2, 'KSampler', 'vae'],
                                        [3, 'Conditioning', 'clip'],
                                    ],
                                },
                                'Conditioning': {
                                    nodeType: 'ttN conditioning',
                                    graphNode: null,
                                    width: 400,
                                    connections: [
                                        [0, 'KSampler', 'model'],
                                        [1, 'KSampler', 'positive'],
                                        [2, 'KSampler', 'negative'],
                                        [3, 'KSampler', 'clip'],
                                    ],
                                },
                                'xyPlot': {
                                    nodeType: 'ttN advanced xyPlot',
                                    graphNode: null,
                                    width: 400,
                                    connections: [
                                        [0, 'KSampler', 'adv_xyPlot'],
                                    ],
                                },
                                'KSampler': {
                                    nodeType: 'ttN KSampler_v2',
                                    graphNode: null,
                                    width: 262,
                                    widgets: {
                                        image_output: 'Preview'
                                    }
                                }
                            }
                            that.addGroup(contextMenu, nodes)
                        }
                    },
                    {   content: "Pipe xyPlot",
                        className: "tinyterra-contextmenu-item",
                        callback : async function(value, event, mouseEvent, contextMenu){
                            const nodes = {
                                'Loader': {
                                    nodeType: 'ttN pipeLoader_v2',
                                    graphNode: null,
                                    width: 315,
                                    connections: [
                                        [0, 'KSampler', 'pipe'],
                                    ],
                                },
                                'xyPlot': {
                                    nodeType: 'ttN advanced xyPlot',
                                    graphNode: null,
                                    width: 400,
                                    connections: [
                                        [0, 'KSampler', 'adv_xyPlot'],
                                    ],
                                },
                                'KSampler': {
                                    nodeType: 'ttN pipeKSampler_v2',
                                    graphNode: null,
                                    width: 262,
                                    widgets: {
                                        image_output: 'Preview'
                                    }
                                }
                            }
                            that.addGroup(contextMenu, nodes)
                        }
                    },
                ]
        }
        new LiteGraph.ContextMenu( entries, { event: e, parentMenu: prev_menu }, ref_window );
    }
    async initialiseNodeMenu() {
        const that = this;
        setTimeout(async () => {
            const getNodeMenuOptions = LGraphCanvas.prototype.getNodeMenuOptions;
            LGraphCanvas.prototype.getNodeMenuOptions = function (node) {
                const options = getNodeMenuOptions.apply(this, arguments);
                node.setDirtyCanvas(true, true);
                const ttNoptions = that.getTinyTerraNodeMenuItems(node)
                options.splice(options.length - 1, 0, ...ttNoptions, null);
                
                return options;
            };
        },500)
    }
    getTinyTerraNodeMenuItems(node) {
        return [
            {
                content: "🌏 Fullscreen",
                callback: () => { openFullscreenApp(node) }
            },
            {
                content: "🌏 Set Default Fullscreen Node",
                callback: _setDefaultFullscreenNode
            },
            {
                content: "🌏 Clear Default Fullscreen Node",
                callback: function () {
                    sessionStorage.removeItem('Comfy.Settings.ttN.default_fullscreen_node');
                }
            },
            null,
            {
                content: "🌏 Default Node BG Color",
                has_submenu: true,
                callback: LGraphCanvas.ttNsetDefaultBGColor
            },
            {
                content: "🌏 Node Dimensions",
                callback: () => { LGraphCanvas.prototype.ttNsetNodeDimension(node); }
            },
            {
                content: "🌏 Reload Node",
                callback: () => {
                    const active_canvas = LGraphCanvas.active_canvas;
                    if (!active_canvas.selected_nodes || Object.keys(active_canvas.selected_nodes).length <= 1) {
                        tinyterraReloadNode(node);
                    } else {
                        for (var i in active_canvas.selected_nodes) {
                            tinyterraReloadNode(active_canvas.selected_nodes[i]);
                        }
                    }
                }
            },
        ]
    }
    handleKeydown(e) {
        this.ctrlKey = !!e.ctrlKey
        this.altKey = !!e.altKey
        this.shiftKey = !!e.shiftKey
        this.downKeys[e.key.toLocaleUpperCase()] = true
        this.downKeys["^" + e.key.toLocaleUpperCase()] = true
    }
    handleKeyup(e) {
        this.ctrlKey = !!e.ctrlKey
        this.altKey = !!e.altKey
        this.shiftKey = !!e.shiftKey
        this.downKeys[e.key.toLocaleUpperCase()] = false
        this.downKeys["^" + e.key.toLocaleUpperCase()] = false
    }
    injectTtnCss() {
        let link = document.createElement("link");
        link.rel = "stylesheet";
        link.type = "text/css";
        link.href = "extensions/ComfyUI_tinyterraNodes/ttN.css";
        document.head.appendChild(link);
    }
}

export const tinyterra = new TinyTerra();
window.tinyterra = tinyterra;

app.registerExtension({
    name: "comfy.ttN",
    async beforeRegisterNodeDef(nodeType, nodeData, app) {
        if (nodeData.name.startsWith("ttN")) {
            const origOnConfigure = nodeType.prototype.onConfigure;
            nodeType.prototype.onConfigure = function () {
                const r = origOnConfigure ? origOnConfigure.apply(this, arguments) : undefined;
                let nodeVersion = nodeData.input.hidden?.ttNnodeVersion ? nodeData.input.hidden.ttNnodeVersion : null;
                nodeType.ttNnodeVersion = nodeVersion;
                this.properties['ttNnodeVersion'] = this.properties['ttNnodeVersion'] ? this.properties['ttNnodeVersion'] : nodeVersion;
                if ((this.properties['ttNnodeVersion'].split(".")[0] !== nodeVersion.split(".")[0]) || (this.properties['ttNnodeVersion'].split(".")[1] !== nodeVersion.split(".")[1])) {
                    if (!this.properties['origVals']) {
                        this.properties['origVals'] = { bgcolor: this.bgcolor, color: this.color, title: this.title }
                    }
                    this.bgcolor = "#e76066";
                    this.color = "#ff0b1e";
                    this.title = this.title.includes("Node Version Mismatch") ? this.title : this.title + " - Node Version Mismatch"
                } else if (this.properties['origVals']) {
                    this.bgcolor = this.properties.origVals.bgcolor;
                    this.color = this.properties.origVals.color;
                    this.title = this.properties.origVals.title;
                    delete this.properties['origVals']
                }
                return r;
            };
        }
    },
    nodeCreated(node) {
        if (["pipeLoader", "pipeLoaderSDXL"].includes(node.constructor.title)) {
            for (let widget of node.widgets) {
                if (widget.name === "control_after_generate") {
                    widget.value = "fixed"
                }
            }
        }
    }
});