File size: 3,228 Bytes
8866644
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { app } from "../../../scripts/app.js";


app.registerExtension({
    name: "easy bookmark",
    registerCustomNodes() {
        class Bookmark {
            type = 'easy bookmark'
            title = "πŸ”–";

            slot_start_y = -20;

            ___collapsed_width = 0;

            get _collapsed_width() {
                return this.___collapsed_width;
            }

            set _collapsed_width(width){
                const canvas = app.canvas ;
                const ctx = canvas.canvas.getContext('2d');
                if(ctx){
                   const oldFont = ctx.font;
                    ctx.font = canvas.title_text_font;
                    this.___collapsed_width = 40 +  ctx.measureText(this.title).width;
                    ctx.font = oldFont;
                }
            }

            isVirtualNode = true;
            serialize_widgets = true;
            keypressBound = null;

            constructor() {

                this.addWidget('text', 'shortcut_key', '1', (value) => {
                  value = value.trim()[0] || '1';
                  if(value !== ''){
                      this.title = "πŸ”– " + value;
                  }
                },{
                  y: 8,
                });
                this.addWidget('number', 'zoom', 1, (value) => {}, {
                  y: 8 + LiteGraph.NODE_WIDGET_HEIGHT + 4,
                  max: 2,
                  min: 0.5,
                  precision: 2,
                });
                this.keypressBound = this.onKeypress.bind(this);
            }

            onAdded(){
                setTimeout(_=>{
                    const value = this.widgets[0].value
                    if(value){
                        this.title = "πŸ”– " + value;
                    }
                },1)
                window.addEventListener("keydown", this.keypressBound);
            }

            onRemoved() {
                window.removeEventListener("keydown", this.keypressBound);
            }

            onKeypress(event){
                const target = event.target;
                if (['input','textarea'].includes(target.localName)) {
                  return;
                }
                if (this.widgets[0] && event.key.toLocaleLowerCase() === this.widgets[0].value.toLocaleLowerCase()) {
                  this.canvasToBookmark();
                }
            }

            canvasToBookmark() {
                const canvas = app.canvas;
                // ComfyUI seemed to break us again, but couldn't repro. No reason to not check, I guess.
                // https://github.com/rgthree/rgthree-comfy/issues/71
                if (canvas?.ds?.offset) {
                  canvas.ds.offset[0] = -this.pos[0]  + 16;
                  canvas.ds.offset[1] = -this.pos[1]  + 40;
                }
                if (canvas?.ds?.scale != null) {
                  canvas.ds.scale = Number(this.widgets[1].value || 1);
                }
                canvas.setDirty(true, true);
              }
        }

        LiteGraph.registerNodeType(
			"easy bookmark",
			Object.assign(Bookmark,{
                title: "Bookmark πŸ”–",
            })
		);

        Bookmark.category = "EasyUse/Util"
    }
})