Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
automatic1111
GitHub Repository: automatic1111/stable-diffusion-webui
Path: blob/master/javascript/edit-attention.js
3055 views
1
function keyupEditAttention(event) {
2
let target = event.originalTarget || event.composedPath()[0];
3
if (!target.matches("*:is([id*='_toprow'] [id*='_prompt'], .prompt) textarea")) return;
4
if (!(event.metaKey || event.ctrlKey)) return;
5
6
let isPlus = event.key == "ArrowUp";
7
let isMinus = event.key == "ArrowDown";
8
if (!isPlus && !isMinus) return;
9
10
let selectionStart = target.selectionStart;
11
let selectionEnd = target.selectionEnd;
12
let text = target.value;
13
14
function selectCurrentParenthesisBlock(OPEN, CLOSE) {
15
if (selectionStart !== selectionEnd) return false;
16
17
// Find opening parenthesis around current cursor
18
const before = text.substring(0, selectionStart);
19
let beforeParen = before.lastIndexOf(OPEN);
20
if (beforeParen == -1) return false;
21
22
let beforeClosingParen = before.lastIndexOf(CLOSE);
23
if (beforeClosingParen != -1 && beforeClosingParen > beforeParen) return false;
24
25
// Find closing parenthesis around current cursor
26
const after = text.substring(selectionStart);
27
let afterParen = after.indexOf(CLOSE);
28
if (afterParen == -1) return false;
29
30
let afterOpeningParen = after.indexOf(OPEN);
31
if (afterOpeningParen != -1 && afterOpeningParen < afterParen) return false;
32
33
// Set the selection to the text between the parenthesis
34
const parenContent = text.substring(beforeParen + 1, selectionStart + afterParen);
35
if (/.*:-?[\d.]+/s.test(parenContent)) {
36
const lastColon = parenContent.lastIndexOf(":");
37
selectionStart = beforeParen + 1;
38
selectionEnd = selectionStart + lastColon;
39
} else {
40
selectionStart = beforeParen + 1;
41
selectionEnd = selectionStart + parenContent.length;
42
}
43
44
target.setSelectionRange(selectionStart, selectionEnd);
45
return true;
46
}
47
48
function selectCurrentWord() {
49
if (selectionStart !== selectionEnd) return false;
50
const whitespace_delimiters = {"Tab": "\t", "Carriage Return": "\r", "Line Feed": "\n"};
51
let delimiters = opts.keyedit_delimiters;
52
53
for (let i of opts.keyedit_delimiters_whitespace) {
54
delimiters += whitespace_delimiters[i];
55
}
56
57
// seek backward to find beginning
58
while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) {
59
selectionStart--;
60
}
61
62
// seek forward to find end
63
while (!delimiters.includes(text[selectionEnd]) && selectionEnd < text.length) {
64
selectionEnd++;
65
}
66
67
// deselect surrounding whitespace
68
while (text[selectionStart] == " " && selectionStart < selectionEnd) {
69
selectionStart++;
70
}
71
while (text[selectionEnd - 1] == " " && selectionEnd > selectionStart) {
72
selectionEnd--;
73
}
74
75
target.setSelectionRange(selectionStart, selectionEnd);
76
return true;
77
}
78
79
// If the user hasn't selected anything, let's select their current parenthesis block or word
80
if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')') && !selectCurrentParenthesisBlock('[', ']')) {
81
selectCurrentWord();
82
}
83
84
event.preventDefault();
85
86
var closeCharacter = ')';
87
var delta = opts.keyedit_precision_attention;
88
var start = selectionStart > 0 ? text[selectionStart - 1] : "";
89
var end = text[selectionEnd];
90
91
if (start == '<') {
92
closeCharacter = '>';
93
delta = opts.keyedit_precision_extra;
94
} else if (start == '(' && end == ')' || start == '[' && end == ']') { // convert old-style (((emphasis)))
95
let numParen = 0;
96
97
while (text[selectionStart - numParen - 1] == start && text[selectionEnd + numParen] == end) {
98
numParen++;
99
}
100
101
if (start == "[") {
102
weight = (1 / 1.1) ** numParen;
103
} else {
104
weight = 1.1 ** numParen;
105
}
106
107
weight = Math.round(weight / opts.keyedit_precision_attention) * opts.keyedit_precision_attention;
108
109
text = text.slice(0, selectionStart - numParen) + "(" + text.slice(selectionStart, selectionEnd) + ":" + weight + ")" + text.slice(selectionEnd + numParen);
110
selectionStart -= numParen - 1;
111
selectionEnd -= numParen - 1;
112
} else if (start != '(') {
113
// do not include spaces at the end
114
while (selectionEnd > selectionStart && text[selectionEnd - 1] == ' ') {
115
selectionEnd--;
116
}
117
118
if (selectionStart == selectionEnd) {
119
return;
120
}
121
122
text = text.slice(0, selectionStart) + "(" + text.slice(selectionStart, selectionEnd) + ":1.0)" + text.slice(selectionEnd);
123
124
selectionStart++;
125
selectionEnd++;
126
}
127
128
if (text[selectionEnd] != ':') return;
129
var weightLength = text.slice(selectionEnd + 1).indexOf(closeCharacter) + 1;
130
var weight = parseFloat(text.slice(selectionEnd + 1, selectionEnd + weightLength));
131
if (isNaN(weight)) return;
132
133
weight += isPlus ? delta : -delta;
134
weight = parseFloat(weight.toPrecision(12));
135
if (Number.isInteger(weight)) weight += ".0";
136
137
if (closeCharacter == ')' && weight == 1) {
138
var endParenPos = text.substring(selectionEnd).indexOf(')');
139
text = text.slice(0, selectionStart - 1) + text.slice(selectionStart, selectionEnd) + text.slice(selectionEnd + endParenPos + 1);
140
selectionStart--;
141
selectionEnd--;
142
} else {
143
text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + weightLength);
144
}
145
146
target.focus();
147
target.value = text;
148
target.selectionStart = selectionStart;
149
target.selectionEnd = selectionEnd;
150
151
updateInput(target);
152
}
153
154
addEventListener('keydown', (event) => {
155
keyupEditAttention(event);
156
});
157
158