-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
379 lines (321 loc) · 11.6 KB
/
script.js
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
let jsonData;
// 异步加载 JSON
async function loadJSON(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
}
// 创建表单组
function createFormGroup(groupId, groupData) {
const contentWrapper = document.createElement("div");
contentWrapper.className = "p-3";
let hasInputs = false;
for (const [key, value] of Object.entries(groupData.inputs)) {
if (!Array.isArray(value)) {
const label = document.createElement("label");
label.className = "block mb-1 text-sm";
label.textContent = key;
const inputField = createInputField(groupId, key, value);
contentWrapper.appendChild(label);
contentWrapper.appendChild(inputField);
hasInputs = true;
}
}
// 如果没有可生成的表单项,则返回 null
if (!hasInputs) {
return null;
}
const group = document.createElement("div");
group.className = "mb-4 border rounded-lg overflow-hidden";
const groupHeader = document.createElement("div");
groupHeader.className = "bg-gray-100 p-2 cursor-pointer flex items-center";
const toggleIcon = document.createElement("span");
toggleIcon.className = "mr-2 transition-transform duration-300";
toggleIcon.textContent = "▼";
const groupTitle = document.createElement("h3");
groupTitle.className = "text-sm font-semibold flex-grow";
groupTitle.textContent = groupData.class_type;
groupHeader.appendChild(toggleIcon);
groupHeader.appendChild(groupTitle);
group.appendChild(groupHeader);
group.appendChild(contentWrapper);
// 添加展开/折叠功能
let isExpanded = true;
groupHeader.addEventListener("click", () => {
isExpanded = !isExpanded;
contentWrapper.style.display = isExpanded ? "block" : "none";
toggleIcon.style.transform = isExpanded ? "rotate(0deg)" : "rotate(-90deg)";
});
return group;
}
// 创建表单项
function createInputField(groupId, name, value) {
const input = document.createElement("input");
input.name = name;
input.value = value;
input.className = "w-full p-2 mb-2 border rounded";
if (typeof value === "string") {
input.type = "text";
} else if (typeof value === "number") {
input.type = "number";
}
input.addEventListener("input", (event) => {
let newValue = event.target.value;
if (input.type === "number") {
newValue = Number(newValue);
}
updateJsonValue(groupId, name, newValue);
});
return input;
}
// 更新表单值
function updateJsonValue(groupId, key, newValue) {
jsonData[groupId].inputs[key] = newValue;
}
// 添加新函数:更新提交按钮状态
function updateSubmitButtonState() {
const submitButton = $("#submit-button");
const formContainer = $("#form-container");
if (formContainer.children().length === 0) {
submitButton.prop('disabled', true);
submitButton.addClass("disabled:opacity-75 cursor-not-allowed");
} else {
submitButton.prop('disabled', false);
submitButton.removeClass("disabled:opacity-75 cursor-not-allowed");
}
}
// 根据 JSON 结构生成表单
function generateForm(data) {
const formContainer = document.getElementById("form-container");
formContainer.innerHTML = ''; // 清空之前的表单内容
for (const [groupId, groupData] of Object.entries(data)) {
const formGroup = createFormGroup(groupId, groupData);
if (formGroup) {
formContainer.appendChild(formGroup);
}
}
// 生成表单后更新按钮状态
updateSubmitButtonState();
}
// 添加新函数:从 localStorage 加载图片
function loadImagesFromLocalStorage() {
console.log("从 localStorage 加载图片");
const storedData = JSON.parse(localStorage.getItem('comfyui-web-lite')) || { images: [] };
console.log("存储的图片:", storedData.images);
const imageContainer = document.getElementById('image-container');
imageContainer.innerHTML = '';
// 反转数组顺序,以便最新的图片显示在前面
storedData.images.reverse().forEach(imageUrl => addImageCard(imageUrl));
}
// 添加新函数:创建图片卡片
function addImageCard(imageUrl) {
console.log("添加图片卡片,URL:", imageUrl);
const imageContainer = document.getElementById('image-container');
const card = document.createElement('div');
card.className = 'bg-white rounded-lg shadow-md overflow-hidden mb-4 relative grow-0';
card.style.width = '200px';
card.style.height = '200px';
card.innerHTML = `
<img src="${imageUrl}" alt="Generated Image" class="w-full h-full object-cover cursor-pointer">
<button class="delete-image absolute top-2 right-2 bg-red-500 text-white px-2 py-1 rounded hover:bg-red-600 transition duration-300 text-xs">×</button>
`;
card.querySelector('.delete-image').addEventListener('click', (e) => {
e.stopPropagation(); // 防止触发图片点击事件
deleteImage(imageUrl);
card.remove();
});
card.querySelector('img').addEventListener('click', () => {
showFullscreenImage(imageUrl);
});
// 将新卡片插入到容器的最前面
imageContainer.insertBefore(card, imageContainer.firstChild);
}
function showFullscreenImage(imageUrl) {
const fullscreenContainer = document.getElementById('fullscreen-container');
const fullscreenImage = document.getElementById('fullscreen-image');
fullscreenImage.src = imageUrl;
fullscreenContainer.classList.remove('hidden');
}
// 添加新函数:删除图片
function deleteImage(imageUrl) {
const storedData = JSON.parse(localStorage.getItem('comfyui-web-lite')) || { images: [] };
storedData.images = storedData.images.filter(url => url !== imageUrl);
localStorage.setItem('comfyui-web-lite', JSON.stringify(storedData));
}
// 添加图片到 localStorage
function addImageToLocalStorage(imageUrl) {
const storedData = JSON.parse(localStorage.getItem('comfyui-web-lite')) || { images: [] };
// 将新图片 URL 添加到数组的开头
storedData.images.unshift(imageUrl);
localStorage.setItem('comfyui-web-lite', JSON.stringify(storedData));
}
// 修改 generate 函数
function generate(prompt, client_id) {
return new Promise((resolve, reject) => {
const socket = new WebSocket(
"ws://" + server_address + "/ws?clientId=" + client_id
);
const timeout = setTimeout(() => {
reject(new Error("生成超时"));
socket.close();
}, 30000); // 30秒超时
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Received WebSocket message:", data);
if (data.type === "executed" && "images" in data.data.output) {
clearTimeout(timeout);
const image = data.data.output.images[0];
const filename = image.filename;
const subfolder = image.subfolder;
const imageAddress =
"http://" +
server_address +
"/view?filename=" +
filename +
"&type=output&subfolder=" +
subfolder;
// 保存图片地址到 localStorage
addImageToLocalStorage(imageAddress);
console.log("Generated image address:", imageAddress);
addImageCard(imageAddress);
resolve(imageAddress);
socket.close();
}
};
socket.onerror = (error) => {
clearTimeout(timeout);
console.error("WebSocket error:", error);
reject(error);
socket.close();
};
fetch("http://" + server_address + "/prompt", {
method: "POST",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ prompt: prompt, client_id: client_id }),
}).catch((error) => {
clearTimeout(timeout);
console.error("Fetch error:", error);
reject(error);
socket.close();
});
});
}
$(document).ready(function () {
console.log("Document ready, loading images");
const server_address = "127.0.0.1:8188"; // 更新为你的服务器地址和端口
// 生成唯一的客户端ID
function uuidv4() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
(c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
);
}
const client_id = uuidv4();
// 连接到 WebSocket,发送 prompt,并接收图像地址
function generate(prompt, client_id) {
return new Promise((resolve, reject) => {
const socket = new WebSocket(
"ws://" + server_address + "/ws?clientId=" + client_id
);
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Received WebSocket message:", data);
if (data.type === "executed" && "images" in data.data.output) {
const image = data.data.output.images[0];
const filename = image.filename;
const subfolder = image.subfolder;
const imageAddress =
"http://" +
server_address +
"/view?filename=" +
filename +
"&type=output&subfolder=" +
subfolder;
console.log("Generated image address:", imageAddress);
addImageToLocalStorage(imageAddress);
addImageCard(imageAddress);
resolve(imageAddress);
socket.close();
}
};
socket.onerror = (error) => {
console.error("WebSocket error:", error);
reject(error);
socket.close();
};
fetch("http://" + server_address + "/prompt", {
method: "POST",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ prompt: prompt, client_id: client_id }),
}).catch((error) => {
console.error("Fetch error:", error);
reject(error);
socket.close();
});
});
}
// 处理文件上传
$("#upload-flow-json").on("change", function (event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function (event) {
jsonData = JSON.parse(event.target.result);
console.log("Uploaded JSON:", jsonData);
generateForm(jsonData);
};
reader.onerror = function (error) {
console.error("Error reading JSON file:", error);
};
if (file) {
reader.readAsText(file);
} else {
// 如果没有选择文件,清空表单并更新按钮状态
$("#form-container").empty();
updateSubmitButtonState();
}
});
// 修改表单提交处理
$("#submit-button").on("click", async function () {
const submitButton = $(this);
// 检查按钮是否被禁用
if (submitButton.prop('disabled')) {
return;
}
submitButton.addClass("disabled:opacity-75 cursor-not-allowed");
submitButton.prop('disabled', true);
submitButton.text('Generate...');
const promptProcess = JSON.stringify(jsonData);
const prompt = JSON.parse(promptProcess);
console.log("prompt:", prompt);
try {
const imageAddress = await generate(prompt, client_id);
console.log("Generated image address:", imageAddress);
} catch (error) {
console.error("Error generating image:", error);
} finally {
submitButton.removeClass("disabled:opacity-75 cursor-not-allowed");
submitButton.prop('disabled', false);
submitButton.text('Generate Image');
updateSubmitButtonState();
}
});
// 页面加载时读取 localStorage 中的图片
loadImagesFromLocalStorage();
// 关闭全屏图片
$('#close-fullscreen, #fullscreen-container').on('click', function(e) {
if (e.target === this) {
$('#fullscreen-container').addClass('hidden');
}
});
// 防止点击图片时关闭全屏
$('#fullscreen-image').on('click', function(e) {
e.stopPropagation();
});
});