Line data Source code
1 : import { app } from '@/scripts/app'
2 : import type { ComfyExtension } from '@/types/comfy'
3 :
4 : type Enclosure = {
5 : start: number
6 : end: number
7 : }
8 :
9 1 : export function incrementWeight(weight: string, delta: number): string {
10 1 : const floatWeight = parseFloat(weight)
11 1 : if (isNaN(floatWeight)) return weight
12 1 : const newWeight = floatWeight + delta
13 1 : return String(Number(newWeight.toFixed(10)))
14 : }
15 :
16 0 : export function findNearestEnclosure(
17 : text: string,
18 : cursorPos: number
19 : ): Enclosure | null {
20 0 : let start = cursorPos
21 0 : let end = cursorPos
22 0 : let openCount = 0
23 0 : let closeCount = 0
24 :
25 0 : if (text[cursorPos] === '(') {
26 0 : end = cursorPos + 1
27 : } else {
28 0 : while (start >= 0) {
29 0 : start--
30 0 : if (text[start] === '(' && openCount === closeCount) break
31 0 : if (text[start] === '(') openCount++
32 0 : if (text[start] === ')') closeCount++
33 : }
34 0 : if (start < 0) return null
35 0 : openCount = 0
36 0 : closeCount = 0
37 : }
38 :
39 0 : while (end < text.length) {
40 0 : if (text[end] === ')' && openCount === closeCount) break
41 0 : if (text[end] === '(') openCount++
42 0 : if (text[end] === ')') closeCount++
43 0 : end++
44 : }
45 0 : if (end === text.length) return null
46 :
47 0 : return { start: start + 1, end: end }
48 : }
49 :
50 1 : export function addWeightToParentheses(text: string): string {
51 1 : const parenMatch = text.match(/^\((.*)\)$/)
52 1 : if (!parenMatch) return text
53 1 : const innerText = parenMatch[1]
54 : // A time-like pattern (e.g. "12:30") is preceded by whitespace or string-start;
55 : // everything else ending in ":number" is a weight, including digit-ending names like "v2:1.5".
56 : const looksLikeTime = /(?:^|\s)\d{1,2}:\d{2}$/.test(innerText)
57 : const hasTrailingWeight =
58 : !looksLikeTime && /:[+-]?(?:\d*\.)?\d+(?:[eE][+-]?\d+)?$/.test(innerText)
59 1 : return hasTrailingWeight ? text : `(${innerText}:1.0)`
60 : }
61 :
62 2355 : export const editAttentionExtension: ComfyExtension = {
63 : name: 'Comfy.EditAttention',
64 : init() {
65 2355 : const editAttentionDelta = app.ui.settings.addSetting({
66 : id: 'Comfy.EditAttention.Delta',
67 : category: ['Comfy', 'EditTokenWeight', 'Delta'],
68 : name: 'Ctrl+up/down precision',
69 : type: 'slider',
70 : attrs: {
71 : min: 0.01,
72 : max: 0.5,
73 : step: 0.01
74 : },
75 : defaultValue: 0.05
76 : })
77 :
78 1310 : function editAttention(event: KeyboardEvent) {
79 1310 : const inputField = event.composedPath()[0]
80 1310 : const delta = parseFloat(String(editAttentionDelta.value))
81 :
82 1310 : if (!(inputField instanceof HTMLTextAreaElement)) return
83 1310 : if (!(event.key === 'ArrowUp' || event.key === 'ArrowDown')) return
84 1310 : if (!event.ctrlKey && !event.metaKey) return
85 :
86 1 : event.preventDefault()
87 :
88 1 : let start = inputField.selectionStart
89 1 : let end = inputField.selectionEnd
90 1 : let selectedText = inputField.value.substring(start, end)
91 :
92 4 : if (!selectedText) {
93 0 : const nearestEnclosure = findNearestEnclosure(inputField.value, start)
94 0 : if (nearestEnclosure) {
95 0 : start = nearestEnclosure.start
96 0 : end = nearestEnclosure.end
97 0 : selectedText = inputField.value.substring(start, end)
98 : } else {
99 0 : const delimiters = ' .,\\/!?%^*;:{}=-_`~()\r\n\t'
100 :
101 0 : while (
102 : !delimiters.includes(inputField.value[start - 1]) &&
103 : start > 0
104 : ) {
105 0 : start--
106 : }
107 :
108 0 : while (
109 : !delimiters.includes(inputField.value[end]) &&
110 : end < inputField.value.length
111 : ) {
112 0 : end++
113 : }
114 :
115 0 : selectedText = inputField.value.substring(start, end)
116 0 : if (!selectedText) return
117 : }
118 : }
119 :
120 : const selectionEndsWithSpace =
121 : selectedText[selectedText.length - 1] === ' '
122 4 : if (selectionEndsWithSpace) {
123 0 : selectedText = selectedText.substring(0, selectedText.length - 1)
124 0 : end -= 1
125 : }
126 :
127 : const selectionIsSurroundedByParens =
128 : inputField.value[start - 1] === '(' && inputField.value[end] === ')'
129 1310 : if (selectionIsSurroundedByParens) {
130 0 : start -= 1
131 0 : end += 1
132 0 : selectedText = inputField.value.substring(start, end)
133 : }
134 :
135 : const selectionIsNotEnclosedInParens =
136 : selectedText[0] !== '(' || selectedText[selectedText.length - 1] !== ')'
137 1310 : if (selectionIsNotEnclosedInParens) selectedText = `(${selectedText})`
138 :
139 1 : selectedText = addWeightToParentheses(selectedText)
140 :
141 1310 : const weightDelta = event.key === 'ArrowUp' ? delta : -delta
142 1310 : const updatedText = selectedText.replace(
143 : /\((.*):([+-]?(?:\d*\.)?\d+(?:[eE][+-]?\d+)?)\)/,
144 : (_, text, weight) => {
145 1 : weight = incrementWeight(weight, weightDelta)
146 1 : if (weight == 1) {
147 0 : return text
148 : } else {
149 1 : return `(${text}:${weight})`
150 : }
151 : }
152 : )
153 :
154 1310 : inputField.setSelectionRange(start, end)
155 : // Intentional use of deprecated: https://developer.mozilla.org/docs/Web/API/Document/execCommand#using_inserttext
156 1310 : document.execCommand('insertText', false, updatedText)
157 1310 : inputField.setSelectionRange(start, start + updatedText.length)
158 : }
159 2355 : window.addEventListener('keydown', editAttention)
160 : }
161 : }
|