"tests/distributed/DDP/run_race_test.sh" did not exist on "53e1b61a1e2498e66e4af9ff19e0bc55955b24b0"
editAttention.js 5.76 KB
Newer Older
1
2
3
4
5
import { app } from "/scripts/app.js";

// Allows you to edit the attention weight by holding ctrl (or cmd) and using the up/down arrow keys

app.registerExtension({
missionfloyd's avatar
missionfloyd committed
6
    name: "Comfy.EditAttention",
7
    init() {
missionfloyd's avatar
missionfloyd committed
8
9
10
11
12
13
        const editAttentionDelta = app.ui.settings.addSetting({
            id: "Comfy.EditAttention.Delta",
            name: "Ctrl+up/down precision",
            type: "slider",
            attrs: {
                min: 0.01,
comfyanonymous's avatar
comfyanonymous committed
14
                max: 0.5,
missionfloyd's avatar
missionfloyd committed
15
16
                step: 0.01,
            },
comfyanonymous's avatar
comfyanonymous committed
17
            defaultValue: 0.05,
missionfloyd's avatar
missionfloyd committed
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
        function incrementWeight(weight, delta) {
            const floatWeight = parseFloat(weight);
            if (isNaN(floatWeight)) return weight;
            const newWeight = floatWeight + delta;
            if (newWeight < 0) return "0";
            return String(Number(newWeight.toFixed(10)));
        }

        function findNearestEnclosure(text, cursorPos) {
            let start = cursorPos, end = cursorPos;
            let openCount = 0, closeCount = 0;

            // Find opening parenthesis before cursor
            while (start >= 0) {
                start--;
                if (text[start] === "(" && openCount === closeCount) break;
                if (text[start] === "(") openCount++;
                if (text[start] === ")") closeCount++;
            }
            if (start < 0) return false;

            openCount = 0;
            closeCount = 0;

            // Find closing parenthesis after cursor
            while (end < text.length) {
                if (text[end] === ")" && openCount === closeCount) break;
                if (text[end] === "(") openCount++;
                if (text[end] === ")") closeCount++;
                end++;
            }
            if (end === text.length) return false;

            return { start: start + 1, end: end };
        }

        function addWeightToParentheses(text) {
            const parenRegex = /^\((.*)\)$/;
            const parenMatch = text.match(parenRegex);

            const floatRegex = /:([+-]?(\d*\.)?\d+([eE][+-]?\d+)?)/;
            const floatMatch = text.match(floatRegex);

            if (parenMatch && !floatMatch) {
                return `(${parenMatch[1]}:1.0)`;
            } else {
                return text;
            }
        };

        function editAttention(event) {
            const inputField = event.composedPath()[0];
missionfloyd's avatar
missionfloyd committed
72
            const delta = parseFloat(editAttentionDelta.value);
73
74
75
76
77
78
79
80
81
82
83

            if (inputField.tagName !== "TEXTAREA") return;
            if (!(event.key === "ArrowUp" || event.key === "ArrowDown")) return;
            if (!event.ctrlKey && !event.metaKey) return;

            event.preventDefault();

            let start = inputField.selectionStart;
            let end = inputField.selectionEnd;
            let selectedText = inputField.value.substring(start, end);

EllangoK's avatar
EllangoK committed
84
            // If there is no selection, attempt to find the nearest enclosure, or select the current word
85
86
87
88
89
90
91
            if (!selectedText) {
                const nearestEnclosure = findNearestEnclosure(inputField.value, start);
                if (nearestEnclosure) {
                    start = nearestEnclosure.start;
                    end = nearestEnclosure.end;
                    selectedText = inputField.value.substring(start, end);
                } else {
EllangoK's avatar
EllangoK committed
92
                    // Select the current word, find the start and end of the word (first space before and after)
EllangoK's avatar
EllangoK committed
93
94
95
96
97
98
99
100
101
102
                    const wordStart = inputField.value.substring(0, start).lastIndexOf(" ") + 1;
                    const wordEnd = inputField.value.substring(end).indexOf(" ");
                    // If there is no space after the word, select to the end of the string
                    if (wordEnd === -1) {
                        end = inputField.value.length;
                    } else {
                        end += wordEnd;
                    }
                    start = wordStart;

EllangoK's avatar
EllangoK committed
103
104
105
106
107
108
109
110
111
                    // Remove all punctuation at the end and beginning of the word
                    while (inputField.value[start].match(/[.,\/#!$%\^&\*;:{}=\-_`~()]/)) {
                        start++;
                    }
                    while (inputField.value[end - 1].match(/[.,\/#!$%\^&\*;:{}=\-_`~()]/)) {
                        end--;
                    }
                    selectedText = inputField.value.substring(start, end);
                    if (!selectedText) return;
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
                }
            }

            // If the selection ends with a space, remove it
            if (selectedText[selectedText.length - 1] === " ") {
                selectedText = selectedText.substring(0, selectedText.length - 1);
                end -= 1;
            }

            // If there are parentheses left and right of the selection, select them
            if (inputField.value[start - 1] === "(" && inputField.value[end] === ")") {
                start -= 1;
                end += 1;
                selectedText = inputField.value.substring(start, end);
            }

            // If the selection is not enclosed in parentheses, add them
            if (selectedText[0] !== "(" || selectedText[selectedText.length - 1] !== ")") {
                selectedText = `(${selectedText})`;
            }

            // If the selection does not have a weight, add a weight of 1.0
            selectedText = addWeightToParentheses(selectedText);

            // Increment the weight
            const weightDelta = event.key === "ArrowUp" ? delta : -delta;
            const updatedText = selectedText.replace(/(.*:)(\d+(\.\d+)?)(.*)/, (match, prefix, weight, _, suffix) => {
missionfloyd's avatar
missionfloyd committed
139
                return prefix + incrementWeight(weight, weightDelta) + suffix;
140
141
142
143
144
145
146
            });

            inputField.setRangeText(updatedText, start, end, "select");
        }
        window.addEventListener("keydown", editAttention);
    },
});