Spaces:
Runtime error
Runtime error
File size: 43,691 Bytes
53db153 286ad58 f919021 286ad58 53db153 286ad58 53db153 286ad58 53db153 286ad58 5c9d988 493e751 5c9d988 53db153 5c9d988 0e6bf57 5c9d988 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 78ed9eb 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 78ed9eb 53db153 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 5c9d988 493e751 78ed9eb 5c9d988 493e751 78ed9eb 493e751 78ed9eb 493e751 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 493e751 5c9d988 493e751 5c9d988 78ed9eb 5c9d988 78ed9eb 5c9d988 78ed9eb 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 f919021 53db153 5c9d988 53db153 5c9d988 53db153 78ed9eb 53db153 | 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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 | <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Banana Pro AI</title>
<link rel="stylesheet" href="style.css">
<style>
/* 顶部固定布局样式 */
.input-section {
position: fixed !important;
top: 0 !important;
bottom: auto !important;
left: 0 !important;
right: 0 !important;
transform: none !important;
width: 100% !important;
max-width: 100% !important;
border-radius: 0 !important;
z-index: 1000 !important;
box-shadow: 0 2px 20px rgba(0,0,0,0.3) !important;
}
header {
margin-top: 80px; /* 为输入区留空间 */
}
.history-container {
padding-bottom: 20px !important;
}
/* 调整输入区内部布局 */
.control-bar {
max-width: 1200px;
margin: 0 auto;
padding: 12px 20px !important;
}
.preview-bar {
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
}
.preview-bar.visible {
padding: 12px 20px;
}
/* 动态调整header间距 */
.has-preview header {
margin-top: 180px;
}
@media (max-width: 768px) {
header {
margin-top: 70px;
}
.has-preview header {
margin-top: 150px;
}
}
</style>
</head>
<body>
<!-- 在预览栏显示时添加 has-preview 类 -->
<script>
// 监听预览栏变化
const observePreviewBar = () => {
const previewBar = document.getElementById('preview-bar');
const observer = new MutationObserver(() => {
if (previewBar.classList.contains('visible')) {
document.body.classList.add('has-preview');
} else {
document.body.classList.remove('has-preview');
}
});
observer.observe(previewBar, { attributes: true, attributeFilter: ['class'] });
};
document.addEventListener('DOMContentLoaded', observePreviewBar);
</script>
<!-- 登录界面 -->
<div id="login-overlay">
<div class="login-card glass-panel">
<h1 style="font-size: 2rem;">🍌</h1>
<h3 style="margin: 10px 0; color: #cbd5e1;">Banana Pro</h3>
<input type="password" id="pwd" placeholder="Password" onkeypress="if(event.key==='Enter')doLogin()">
<button class="btn-3d" onclick="doLogin()" style="width: 100%; padding: 10px;">Unlock</button>
</div>
</div>
<!-- 主应用 -->
<div class="app-container" id="app" style="filter: blur(10px); pointer-events: none;">
<!-- 顶部固定输入区 -->
<div class="input-section glass-panel" id="drop-zone">
<!-- 上部分:预览条 -->
<div class="preview-bar" id="preview-bar"></div>
<!-- 下部分:操作栏 -->
<div class="control-bar">
<button class="upload-trigger" id="upload-btn" title="上传参考图">📷</button>
<textarea id="prompt" class="main-input" rows="1" placeholder="描述画面... (支持拖拽图片)"></textarea>
<button class="btn-3d send-btn" id="send-btn">
<span>生成</span>
<div class="loader"></div>
</button>
</div>
</div>
<header>
<div>
<h2>创意画板</h2>
<div style="font-size: 12px; color: var(--text-sub);" id="status-bar">Ready</div>
</div>
</header>
<!-- 历史画廊 -->
<div class="history-container">
<div class="grid-layout" id="gallery"></div>
<section class="public-gallery-section glass-panel">
<div class="section-title">
<div>
<h3>✨ 社区创意画廊</h3>
<p class="section-subtitle" id="public-gallery-hint">分享你的作品,欣赏社区灵感</p>
</div>
<div class="section-actions">
<button class="icon-btn" id="refresh-public-gallery" title="刷新公共画廊" aria-label="刷新公共画廊">⟳</button>
</div>
</div>
<div class="grid-layout public-grid" id="public-gallery"></div>
</section>
</div>
<input type="file" id="file-input" multiple accept="image/*" hidden>
</div>
<!-- 详情弹窗 -->
<div class="modal" id="modal">
<div class="modal-content glass-panel">
<button class="close-modal" onclick="closeModal()">×</button>
<div class="modal-img-area">
<img id="m-img" src="">
</div>
<div class="modal-footer">
<div class="input-refs" id="m-refs"></div>
<div class="prompt-display" id="m-prompt"></div>
<button class="btn-3d" id="m-reuse" style="width: 100%; padding: 12px;">
🎨 复用参数与图片
</button>
</div>
</div>
</div>
<script>
// ============================================
// 全局状态管理
// ============================================
const AppState = {
db: null,
currentImages: [], // 当前上传的图片 Base64 数组
galleryData: [], // 个人画廊数据缓存
publicGalleryData: [], // 公共画廊数据缓存
currentModalItem: null // 当前弹窗显示的项目
};
const STORAGE_KEYS = {
publicGalleryTokens: 'BananaPro_PublicGallery_Tokens_v1'
};
const DEFAULT_PUBLIC_HINT = '分享你的作品,欣赏社区灵感';
const DB_NAME = 'BananaProDB_v3';
const DB_VERSION = 1;
const STORE_NAME = 'artworks';
// ============================================
// IndexedDB 模块(使用 Blob 存储避免 Base64 问题)
// ============================================
const Database = {
async init() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onerror = () => reject(request.error);
request.onsuccess = () => {
AppState.db = request.result;
resolve();
};
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(STORE_NAME)) {
const store = db.createObjectStore(STORE_NAME, {
keyPath: 'id',
autoIncrement: true
});
store.createIndex('timestamp', 'timestamp', { unique: false });
}
};
});
},
async save(item) {
return new Promise((resolve, reject) => {
const tx = AppState.db.transaction([STORE_NAME], 'readwrite');
const store = tx.objectStore(STORE_NAME);
// 直接存储完整对象,不做任何转换
const record = {
prompt: item.prompt,
image: item.image, // 完整的 data:image/... 字符串
inputImages: item.inputImages || [],
timestamp: Date.now()
};
const request = store.add(record);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
},
async getAll() {
return new Promise((resolve, reject) => {
const tx = AppState.db.transaction([STORE_NAME], 'readonly');
const store = tx.objectStore(STORE_NAME);
const request = store.getAll();
request.onsuccess = () => {
// 按时间戳倒序
const results = request.result.sort((a, b) => b.timestamp - a.timestamp);
resolve(results);
};
request.onerror = () => reject(request.error);
});
},
async delete(id) {
return new Promise((resolve, reject) => {
const tx = AppState.db.transaction([STORE_NAME], 'readwrite');
const store = tx.objectStore(STORE_NAME);
const request = store.delete(id);
request.onsuccess = () => resolve();
request.onerror = () => reject(request.error);
});
},
async getById(id) {
return new Promise((resolve, reject) => {
const tx = AppState.db.transaction([STORE_NAME], 'readonly');
const store = tx.objectStore(STORE_NAME);
const request = store.get(id);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
};
// ============================================
// 图片处理模块
// ============================================
const ImageHandler = {
// 文件转 Base64
fileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = () => reject(reader.error);
reader.readAsDataURL(file);
});
},
// 压缩图片(手机端优化)
async compressImage(base64Data, maxWidth = 1280) {
return new Promise((resolve) => {
const img = new Image();
img.onload = () => {
if (img.width <= maxWidth && base64Data.length < 500000) {
resolve(base64Data);
return;
}
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const scale = Math.min(1, maxWidth / img.width);
canvas.width = img.width * scale;
canvas.height = img.height * scale;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
resolve(canvas.toDataURL('image/jpeg', 0.8));
};
img.onerror = () => resolve(base64Data);
img.src = base64Data;
});
},
// 处理上传的文件
async processFiles(files) {
const maxImages = 16;
const imageFiles = Array.from(files).filter(f => f.type.startsWith('image/'));
for (const file of imageFiles) {
if (AppState.currentImages.length >= maxImages) {
alert(`最多只能上传 ${maxImages} 张图片`);
break;
}
try {
let base64 = await this.fileToBase64(file);
base64 = await this.compressImage(base64);
AppState.currentImages.push(base64);
} catch (err) {
console.error('图片读取失败:', err);
}
}
PreviewManager.render();
},
// 移除指定索引的图片
removeAt(index) {
AppState.currentImages.splice(index, 1);
PreviewManager.render();
},
// 清空所有上传的图片
clear() {
AppState.currentImages = [];
PreviewManager.render();
},
// 设置图片(用于复用功能)
setImages(images) {
AppState.currentImages = images ? [...images] : [];
PreviewManager.render();
}
};
// ============================================
// 预览条管理模块
// ============================================
const PreviewManager = {
container: null,
uploadBtn: null,
statusBar: null,
init() {
this.container = document.getElementById('preview-bar');
this.uploadBtn = document.getElementById('upload-btn');
this.statusBar = document.getElementById('status-bar');
},
render() {
// 清空容器
this.container.innerHTML = '';
const images = AppState.currentImages;
if (images.length === 0) {
this.container.classList.remove('visible');
this.uploadBtn.classList.remove('active');
this.statusBar.textContent = 'Ready';
return;
}
this.container.classList.add('visible');
this.uploadBtn.classList.add('active');
this.statusBar.textContent = `已选择 ${images.length}/16 张图片`;
// 使用 DOM API 创建元素,避免 innerHTML 导致的编码问题
images.forEach((imgData, index) => {
const wrapper = document.createElement('div');
wrapper.className = 'thumb-wrapper';
const img = document.createElement('img');
img.src = imgData; // 直接设置 src,不经过字符串拼接
const removeBtn = document.createElement('div');
removeBtn.className = 'thumb-remove';
removeBtn.textContent = '×';
removeBtn.onclick = (e) => {
e.stopPropagation();
ImageHandler.removeAt(index);
};
wrapper.appendChild(img);
wrapper.appendChild(removeBtn);
this.container.appendChild(wrapper);
});
}
};
// ============================================
// 画廊管理模块
// ============================================
const GalleryManager = {
container: null,
init() {
this.container = document.getElementById('gallery');
},
async load() {
try {
AppState.galleryData = await Database.getAll();
this.render();
} catch (err) {
console.error('加载画廊失败:', err);
}
},
render() {
this.container.innerHTML = '';
if (AppState.galleryData.length === 0) {
this.container.innerHTML = `
<div style="grid-column: 1/-1; text-align: center; color: var(--text-sub); padding: 60px 20px;">
<div style="font-size: 48px; margin-bottom: 10px;">🎨</div>
<div>暂无作品,开始创作吧!</div>
</div>
`;
return;
}
const fragment = document.createDocumentFragment();
AppState.galleryData.forEach(item => {
const card = this.createCard(item);
fragment.appendChild(card);
});
this.container.appendChild(fragment);
},
createCard(item) {
const el = document.createElement('div');
el.className = 'history-item';
el.dataset.id = item.id;
// 参考图标记
if (item.inputImages && item.inputImages.length > 0) {
const badge = document.createElement('div');
badge.className = 'item-badge';
badge.textContent = `📎 ${item.inputImages.length}`;
el.appendChild(badge);
}
// 主图片 - 使用 DOM API 设置 src
const img = document.createElement('img');
img.loading = 'lazy';
img.decoding = 'async';
img.src = item.image;
img.alt = `作品 ${item.id}`;
img.onerror = () => {
console.error('图片加载失败, ID:', item.id);
img.src = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect fill="%23333" width="100" height="100"/><text fill="%23666" x="50%" y="50%" text-anchor="middle" dy=".3em">Error</text></svg>';
};
el.appendChild(img);
// 操作按钮
const actions = document.createElement('div');
actions.className = 'item-actions';
const shareBtn = document.createElement('button');
shareBtn.className = 'icon-btn share-btn';
shareBtn.textContent = '🌐';
shareBtn.title = '发布到公共画廊';
shareBtn.setAttribute('aria-label', '发布到公共画廊');
shareBtn.onclick = (e) => {
e.stopPropagation();
PublicGalleryManager.share(item, shareBtn);
};
const downloadBtn = document.createElement('button');
downloadBtn.className = 'icon-btn';
downloadBtn.textContent = '⬇';
downloadBtn.title = '下载图片';
downloadBtn.onclick = (e) => {
e.stopPropagation();
this.downloadImage(item);
};
const deleteBtn = document.createElement('button');
deleteBtn.className = 'icon-btn';
deleteBtn.style.background = 'rgba(239,68,68,0.8)';
deleteBtn.textContent = '🗑';
deleteBtn.title = '删除图片';
deleteBtn.onclick = (e) => {
e.stopPropagation();
this.deleteItem(item.id);
};
actions.appendChild(shareBtn);
actions.appendChild(downloadBtn);
actions.appendChild(deleteBtn);
el.appendChild(actions);
// 点击打开弹窗
el.onclick = () => ModalManager.open(item);
return el;
},
downloadImage(item) {
const link = document.createElement('a');
link.href = item.image;
link.download = `banana-pro-${item.id}-${Date.now()}.png`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
},
async deleteItem(id) {
if (!confirm('确定要删除这张图片吗?')) return;
try {
await Database.delete(id);
await this.load();
} catch (err) {
console.error('删除失败:', err);
alert('删除失败');
}
}
};
// ============================================
// 弹窗管理模块
// ============================================
const ModalManager = {
modal: null,
imgEl: null,
promptEl: null,
refsEl: null,
reuseBtn: null,
init() {
this.modal = document.getElementById('modal');
this.imgEl = document.getElementById('m-img');
this.promptEl = document.getElementById('m-prompt');
this.refsEl = document.getElementById('m-refs');
this.reuseBtn = document.getElementById('m-reuse');
// 点击背景关闭
this.modal.onclick = (e) => {
if (e.target === this.modal) this.close();
};
// ESC 关闭
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') this.close();
});
},
open(item) {
AppState.currentModalItem = item;
// 设置主图 - 直接赋值
this.imgEl.src = item.image;
// 设置提示词
this.promptEl.textContent = item.prompt;
// 渲染参考图
this.refsEl.innerHTML = '';
if (item.inputImages && item.inputImages.length > 0) {
item.inputImages.forEach(imgData => {
const thumb = document.createElement('img');
thumb.className = 'ref-thumb';
thumb.src = imgData; // 直接赋值
thumb.onclick = () => window.open(imgData, '_blank');
this.refsEl.appendChild(thumb);
});
}
// 绑定复用按钮
this.reuseBtn.onclick = () => this.reuse();
this.modal.style.display = 'flex';
},
close() {
this.modal.style.display = 'none';
AppState.currentModalItem = null;
},
reuse() {
const item = AppState.currentModalItem;
if (!item) return;
// 复用提示词
const textarea = document.getElementById('prompt');
textarea.value = item.prompt;
textarea.style.height = 'auto';
textarea.style.height = textarea.scrollHeight + 'px';
// 复用参考图
if (item.inputImages && item.inputImages.length > 0) {
ImageHandler.setImages(item.inputImages);
} else {
ImageHandler.clear();
}
this.close();
textarea.focus();
}
};
// ============================================
// 公共画廊管理模块
// ============================================
const PublicGalleryManager = {
container: null,
refreshBtn: null,
hintEl: null,
tokens: {},
isLoading: false,
defaultHint: DEFAULT_PUBLIC_HINT,
hintTimer: null,
syncTimer: null,
syncInterval: 0,
loadTimeout: null,
lastFetchTime: 0,
minFetchInterval: 5000,
init() {
this.container = document.getElementById('public-gallery');
this.refreshBtn = document.getElementById('refresh-public-gallery');
this.hintEl = document.getElementById('public-gallery-hint');
this.tokens = this.loadTokens();
if (this.hintEl && this.hintEl.textContent) {
this.defaultHint = this.hintEl.textContent;
}
this.setHint(this.defaultHint);
if (this.refreshBtn) {
this.refreshBtn.onclick = () => this.fetch();
}
this.initialFetch();
},
async initialFetch() {
await this.fetch();
},
startRealtimeSync() {
// 实时同步已禁用 - 改为手动刷新提高性能
},
stopRealtimeSync() {
if (this.syncTimer) {
clearInterval(this.syncTimer);
this.syncTimer = null;
}
},
loadTokens() {
try {
const raw = localStorage.getItem(STORAGE_KEYS.publicGalleryTokens);
return raw ? JSON.parse(raw) : {};
} catch (error) {
console.warn('无法读取公共画廊令牌:', error);
return {};
}
},
saveTokens() {
try {
localStorage.setItem(STORAGE_KEYS.publicGalleryTokens, JSON.stringify(this.tokens));
} catch (error) {
console.warn('无法保存公共画廊令牌:', error);
}
},
setHint(message, mode = 'default') {
if (!this.hintEl) return;
this.hintEl.textContent = message;
this.hintEl.classList.remove('is-error', 'is-success');
if (mode === 'error') {
this.hintEl.classList.add('is-error');
} else if (mode === 'success') {
this.hintEl.classList.add('is-success');
}
clearTimeout(this.hintTimer);
if (mode !== 'default') {
this.hintTimer = setTimeout(() => {
this.setHint(this.defaultHint);
}, 4000);
}
},
setLoading(loading) {
this.isLoading = loading;
if (this.refreshBtn) {
this.refreshBtn.classList.toggle('spinning', loading);
this.refreshBtn.disabled = loading;
}
// 防止加载超时 - 30秒后强制停止
if (loading) {
clearTimeout(this.loadTimeout);
this.loadTimeout = setTimeout(() => {
if (this.isLoading) {
this.setLoading(false);
this.setHint('加载超时,请稍后重试', 'error');
}
}, 30000);
} else {
clearTimeout(this.loadTimeout);
}
},
async fetch() {
if (!this.container) return;
// 防止过于频繁的请求
const now = Date.now();
if (now - this.lastFetchTime < this.minFetchInterval) {
return;
}
this.lastFetchTime = now;
this.setLoading(true);
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 15000);
const res = await fetch('/api/public-gallery', {
signal: controller.signal
});
clearTimeout(timeoutId);
const data = await res.json();
if (!res.ok || !data.success) {
throw new Error(data.message || '无法加载公共画廊');
}
AppState.publicGalleryData = data.items || [];
this.render();
this.setHint(this.defaultHint);
} catch (error) {
console.error('公共画廊加载失败:', error);
if (error.name === 'AbortError') {
this.setHint('加载超时,请检查网络', 'error');
} else {
this.setHint('加载失败,请稍后重试', 'error');
}
if (AppState.publicGalleryData.length === 0) {
this.showEmpty('无法加载公共画廊,请稍后重试');
}
} finally {
this.setLoading(false);
}
},
showEmpty(message) {
if (!this.container) return;
this.container.innerHTML = `
<div class="public-gallery-empty">
<div>🌌</div>
<p>${message}</p>
</div>
`;
},
render() {
if (!this.container) return;
if (!AppState.publicGalleryData || AppState.publicGalleryData.length === 0) {
this.showEmpty('还没有人分享作品,成为第一个吧!');
return;
}
this.container.innerHTML = '';
const fragment = document.createDocumentFragment();
AppState.publicGalleryData.forEach(item => {
const card = this.createCard(item);
fragment.appendChild(card);
});
this.container.appendChild(fragment);
},
createCard(item) {
const card = document.createElement('div');
card.className = 'public-card';
card.dataset.id = item.id;
const img = document.createElement('img');
img.loading = 'lazy';
img.decoding = 'async';
img.src = item.image;
img.alt = item.prompt || '创意作品';
card.appendChild(img);
const info = document.createElement('div');
info.className = 'public-card-info';
const promptText = document.createElement('p');
promptText.className = 'public-card-prompt';
promptText.textContent = item.prompt || '未提供提示词';
promptText.title = item.prompt;
info.appendChild(promptText);
const footer = document.createElement('div');
footer.className = 'public-card-footer';
const timeText = document.createElement('span');
timeText.textContent = this.formatTimestamp(item.timestamp);
footer.appendChild(timeText);
if (this.canDelete(item.id)) {
const deleteBtn = document.createElement('button');
deleteBtn.className = 'icon-btn small public-delete-btn';
deleteBtn.textContent = '🗑';
deleteBtn.title = '删除这张作品';
deleteBtn.onclick = (e) => {
e.stopPropagation();
this.delete(item.id);
};
footer.appendChild(deleteBtn);
}
info.appendChild(footer);
card.appendChild(info);
card.onclick = () => ModalManager.open(item);
return card;
},
formatTimestamp(timestamp) {
if (!timestamp) return '';
const date = new Date(timestamp);
if (Number.isNaN(date.getTime())) {
return '';
}
return date.toLocaleString();
},
canDelete(id) {
return true;
},
markOwned(id, token) {
if (!id || !token) return;
this.tokens[id] = token;
this.saveTokens();
},
removeOwnership(id) {
if (this.tokens[id]) {
delete this.tokens[id];
this.saveTokens();
}
},
async share(item, triggerBtn) {
if (!item) return;
const confirmShare = confirm('确认将这张作品发布到公共画廊?提示词将对所有人可见。');
if (!confirmShare) {
return;
}
if (triggerBtn) {
triggerBtn.disabled = true;
}
try {
const res = await fetch('/api/public-gallery', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: item.prompt,
image: item.image,
inputImages: item.inputImages || []
})
});
const data = await res.json();
if (!res.ok || !data.success) {
throw new Error(data.message || '发布失败');
}
AppState.publicGalleryData = [data.item, ...(AppState.publicGalleryData || [])];
this.markOwned(data.item.id, data.deleteToken);
this.render();
this.setHint('作品已发布至公共画廊', 'success');
} catch (error) {
console.error('发布到公共画廊失败:', error);
alert('发布失败: ' + error.message);
this.setHint('发布失败,请稍后重试', 'error');
} finally {
if (triggerBtn) {
triggerBtn.disabled = false;
}
}
},
async delete(id) {
const confirmDelete = confirm('确定要删除这张公共画廊作品吗?');
if (!confirmDelete) {
return;
}
try {
const res = await fetch(`/api/public-gallery/${id}`, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' }
});
const data = await res.json();
if (!res.ok || !data.success) {
throw new Error(data.message || '删除失败');
}
AppState.publicGalleryData = AppState.publicGalleryData.filter(item => item.id !== id);
this.removeOwnership(id);
this.render();
this.setHint('作品已删除', 'success');
} catch (error) {
console.error('删除公共画廊作品失败:', error);
alert('删除失败: ' + error.message);
this.setHint('删除失败,请稍后再试', 'error');
}
}
};
// ============================================
// 生成请求模块
// ============================================
const Generator = {
sendBtn: null,
textarea: null,
statusBar: null,
isGenerating: false,
generateTimeout: null,
init() {
this.sendBtn = document.getElementById('send-btn');
this.textarea = document.getElementById('prompt');
this.statusBar = document.getElementById('status-bar');
this.sendBtn.onclick = () => this.generate();
// 输入框自动高度
this.textarea.addEventListener('input', () => {
this.textarea.style.height = 'auto';
this.textarea.style.height = this.textarea.scrollHeight + 'px';
});
// 回车发送(Shift+Enter 换行)
this.textarea.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
this.generate();
}
});
},
setLoading(loading) {
this.isGenerating = loading;
if (loading) {
this.sendBtn.classList.add('loading');
this.sendBtn.disabled = true;
this.statusBar.textContent = '⏳ 生成中...';
// 防止生成超时 - 3分钟后强制停止
clearTimeout(this.generateTimeout);
this.generateTimeout = setTimeout(() => {
if (this.isGenerating) {
this.setLoading(false);
this.statusBar.textContent = '生成超时,请重试';
alert('生成超时,请检查网络后重试');
}
}, 180000);
} else {
this.sendBtn.classList.remove('loading');
this.sendBtn.disabled = false;
this.statusBar.textContent = 'Ready';
clearTimeout(this.generateTimeout);
}
},
async generate() {
if (this.isGenerating) return;
const prompt = this.textarea.value.trim();
if (!prompt) {
alert('请输入提示词');
return;
}
this.setLoading(true);
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 120000);
const response = await fetch('/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: prompt,
images: AppState.currentImages
}),
signal: controller.signal
});
clearTimeout(timeoutId);
const data = await response.json();
if (!response.ok || !data.success) {
throw new Error(data.message || '生成失败');
}
// 验证返回的图片数据
if (!data.image || !data.image.startsWith('data:image')) {
throw new Error('返回的图片数据无效');
}
// 保存到数据库
await Database.save({
prompt: prompt,
image: data.image,
inputImages: [...AppState.currentImages]
});
// 刷新画廊
await GalleryManager.load();
// 清空输入
this.textarea.value = '';
this.textarea.style.height = 'auto';
ImageHandler.clear();
this.statusBar.textContent = '✅ 生成成功';
} catch (err) {
console.error('生成失败:', err);
if (err.name === 'AbortError') {
this.statusBar.textContent = '⚠️ 生成超时';
alert('生成超时,请检查网络后重试');
} else {
this.statusBar.textContent = '❌ 生成失败';
alert('生成失败: ' + err.message);
}
} finally {
this.setLoading(false);
setTimeout(() => {
if (this.statusBar.textContent !== 'Ready') {
this.statusBar.textContent = 'Ready';
}
}, 3000);
}
}
};
// ============================================
// 认证模块
// ============================================
const Auth = {
async check() {
try {
const res = await fetch('/api/check-auth');
const data = await res.json();
return data.authenticated;
} catch {
return false;
}
},
async login(password) {
const res = await fetch('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ password })
});
const data = await res.json();
return data.success;
},
unlock() {
document.getElementById('login-overlay').style.display = 'none';
const app = document.getElementById('app');
app.style.filter = 'none';
app.style.pointerEvents = 'all';
}
};
// ============================================
// 拖拽上传模块
// ============================================
const DragDrop = {
dropZone: null,
init() {
this.dropZone = document.getElementById('drop-zone');
// 阻止默认行为
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(event => {
this.dropZone.addEventListener(event, (e) => {
e.preventDefault();
e.stopPropagation();
});
});
// 拖入高亮
['dragenter', 'dragover'].forEach(event => {
this.dropZone.addEventListener(event, () => {
this.dropZone.style.borderColor = 'var(--accent-color)';
this.dropZone.style.background = 'rgba(59, 130, 246, 0.1)';
});
});
// 拖出恢复
['dragleave', 'drop'].forEach(event => {
this.dropZone.addEventListener(event, () => {
this.dropZone.style.borderColor = '';
this.dropZone.style.background = '';
});
});
// 放下处理
this.dropZone.addEventListener('drop', async (e) => {
const files = e.dataTransfer.files;
if (files.length > 0) {
await ImageHandler.processFiles(files);
}
});
}
};
// ============================================
// 文件选择模块
// ============================================
const FileSelector = {
input: null,
btn: null,
init() {
this.input = document.getElementById('file-input');
this.btn = document.getElementById('upload-btn');
this.btn.onclick = () => this.input.click();
this.input.onchange = async () => {
if (this.input.files.length > 0) {
await ImageHandler.processFiles(this.input.files);
}
this.input.value = ''; // 重置以便重复选择
};
}
};
// ============================================
// 全局函数(供 HTML onclick 调用)
// ============================================
async function loadWorkspaceData() {
try {
await Promise.all([
GalleryManager.load(),
PublicGalleryManager.fetch()
]);
} catch (error) {
console.error('加载画廊数据失败:', error);
}
}
async function doLogin() {
const pwd = document.getElementById('pwd').value;
if (!pwd) return;
const success = await Auth.login(pwd);
if (success) {
Auth.unlock();
await loadWorkspaceData();
} else {
alert('密码错误');
}
}
function closeModal() {
ModalManager.close();
}
// ============================================
// 应用初始化
// ============================================
async function initApp() {
try {
// 初始化数据库
await Database.init();
// 初始化各模块
PreviewManager.init();
GalleryManager.init();
ModalManager.init();
PublicGalleryManager.init();
Generator.init();
DragDrop.init();
FileSelector.init();
// 检查认证状态
const isAuth = await Auth.check();
if (isAuth) {
Auth.unlock();
await loadWorkspaceData();
}
console.log('App initialized successfully');
} catch (err) {
console.error('App initialization failed:', err);
}
}
// 启动应用
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initApp);
} else {
initApp();
}
</script>
</body>
</html> |