1
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
2
+ // 第 1 部分: 工具函数
3
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
4
+
1
5
function gradioApp ( ) {
2
6
// https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript
3
7
const elems = document . getElementsByTagName ( 'gradio-app' ) ;
@@ -37,6 +41,51 @@ function getCookie(name) {
37
41
return null ;
38
42
}
39
43
44
+ let toastCount = 0 ;
45
+ function toast_push ( msg , duration ) {
46
+ duration = isNaN ( duration ) ? 3000 : duration ;
47
+ const existingToasts = document . querySelectorAll ( '.toast' ) ;
48
+ existingToasts . forEach ( toast => {
49
+ toast . style . top = `${ parseInt ( toast . style . top , 10 ) - 70 } px` ;
50
+ } ) ;
51
+ const m = document . createElement ( 'div' ) ;
52
+ m . innerHTML = msg ;
53
+ m . classList . add ( 'toast' ) ;
54
+ m . style . cssText = `font-size: var(--text-md) !important; color: rgb(255, 255, 255); background-color: rgba(0, 0, 0, 0.6); padding: 10px 15px; border-radius: 4px; position: fixed; top: ${ 50 + toastCount * 70 } %; left: 50%; transform: translateX(-50%); width: auto; text-align: center; transition: top 0.3s;` ;
55
+ document . body . appendChild ( m ) ;
56
+ setTimeout ( function ( ) {
57
+ m . style . opacity = '0' ;
58
+ setTimeout ( function ( ) {
59
+ document . body . removeChild ( m ) ;
60
+ toastCount -- ;
61
+ } , 500 ) ;
62
+ } , duration ) ;
63
+ toastCount ++ ;
64
+ }
65
+
66
+ function toast_up ( msg ) {
67
+ var m = document . getElementById ( 'toast_up' ) ;
68
+ if ( m ) {
69
+ document . body . removeChild ( m ) ; // remove the loader from the body
70
+ }
71
+ m = document . createElement ( 'div' ) ;
72
+ m . id = 'toast_up' ;
73
+ m . innerHTML = msg ;
74
+ m . style . cssText = "font-size: var(--text-md) !important; color: rgb(255, 255, 255); background-color: rgba(0, 0, 100, 0.6); padding: 10px 15px; margin: 0 0 0 -60px; border-radius: 4px; position: fixed; top: 50%; left: 50%; width: auto; text-align: center;" ;
75
+ document . body . appendChild ( m ) ;
76
+ }
77
+ function toast_down ( ) {
78
+ var m = document . getElementById ( 'toast_up' ) ;
79
+ if ( m ) {
80
+ document . body . removeChild ( m ) ; // remove the loader from the body
81
+ }
82
+ }
83
+
84
+
85
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
86
+ // 第 2 部分: 复制按钮
87
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
88
+
40
89
function addCopyButton ( botElement ) {
41
90
// https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript
42
91
// Copy bot button
@@ -98,6 +147,12 @@ function chatbotContentChanged(attempt = 1, force = false) {
98
147
}
99
148
}
100
149
150
+
151
+
152
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
153
+ // 第 3 部分: chatbot动态高度调整
154
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
155
+
101
156
function chatbotAutoHeight ( ) {
102
157
// 自动调整高度
103
158
function update_height ( ) {
@@ -127,8 +182,6 @@ function chatbotAutoHeight() {
127
182
} , 50 ) ; // 每100毫秒执行一次
128
183
}
129
184
130
-
131
-
132
185
function get_elements ( consider_state_panel = false ) {
133
186
var chatbot = document . querySelector ( '#gpt-chatbot > div.wrap.svelte-18telvq' ) ;
134
187
if ( ! chatbot ) {
@@ -154,6 +207,18 @@ function get_elements(consider_state_panel = false) {
154
207
}
155
208
156
209
210
+
211
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
212
+ // 第 4 部分: 粘贴、拖拽文件上传
213
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
214
+
215
+ var elem_upload = null ;
216
+ var elem_upload_float = null ;
217
+ var elem_input_main = null ;
218
+ var elem_input_float = null ;
219
+ var elem_chatbot = null ;
220
+ var exist_file_msg = '⚠️请先删除上传区(左上方)中的历史文件,再尝试上传。'
221
+
157
222
function add_func_paste ( input ) {
158
223
let paste_files = [ ] ;
159
224
if ( input ) {
@@ -182,20 +247,21 @@ function add_func_paste(input) {
182
247
183
248
function add_func_drag ( elem ) {
184
249
if ( elem ) {
185
- const dragEvents = [ "dragover" , "dragenter" ] ;
250
+ const dragEvents = [ "dragover" ] ;
186
251
const leaveEvents = [ "dragleave" , "dragend" , "drop" ] ;
187
252
188
253
const onDrag = function ( e ) {
189
254
e . preventDefault ( ) ;
190
255
e . stopPropagation ( ) ;
191
256
if ( elem_upload_float . querySelector ( "input[type=file]" ) ) {
192
- toast_push ( '释放以上传文件' , 50 )
257
+ toast_up ( '⚠️释放以上传文件' )
193
258
} else {
194
- toast_push ( '⚠️请先删除上传区中的历史文件,再尝试上传。' , 50 )
259
+ toast_up ( exist_file_msg )
195
260
}
196
261
} ;
197
262
198
263
const onLeave = function ( e ) {
264
+ toast_down ( ) ;
199
265
e . preventDefault ( ) ;
200
266
e . stopPropagation ( ) ;
201
267
} ;
@@ -237,35 +303,11 @@ async function upload_files(files) {
237
303
Object . defineProperty ( event , "currentTarget" , { value : uploadInputElement , enumerable : true } ) ;
238
304
Object . defineProperty ( uploadInputElement , "files" , { value : files , enumerable : true } ) ;
239
305
uploadInputElement . dispatchEvent ( event ) ;
240
-
241
- // toast_push('🎉上传文件成功', 2000)
242
306
} else {
243
- toast_push ( '⚠️请先删除上传区中的历史文件,再尝试上传。' , 3000 )
307
+ toast_push ( exist_file_msg , 3000 )
244
308
}
245
309
}
246
310
}
247
- //提示信息 封装
248
- function toast_push ( msg , duration ) {
249
- duration = isNaN ( duration ) ? 3000 : duration ;
250
- const m = document . createElement ( 'div' ) ;
251
- m . innerHTML = msg ;
252
- m . style . cssText = "font-size: var(--text-md) !important; color: rgb(255, 255, 255);background-color: rgba(0, 0, 0, 0.6);padding: 10px 15px;margin: 0 0 0 -60px;border-radius: 4px;position: fixed; top: 50%;left: 50%;width: auto; text-align: center;" ;
253
- document . body . appendChild ( m ) ;
254
- setTimeout ( function ( ) {
255
- var d = 0.5 ;
256
- m . style . opacity = '0' ;
257
- setTimeout ( function ( ) {
258
- document . body . removeChild ( m )
259
- } , d * 1000 ) ;
260
- } , duration ) ;
261
- }
262
-
263
- var elem_upload = null ;
264
- var elem_upload_float = null ;
265
- var elem_input_main = null ;
266
- var elem_input_float = null ;
267
- var gptChatbot = null ;
268
-
269
311
270
312
function begin_loading_status ( ) {
271
313
// Create the loader div and add styling
@@ -293,6 +335,7 @@ function begin_loading_status() {
293
335
}` ;
294
336
document . head . appendChild ( styleSheet ) ;
295
337
}
338
+
296
339
function cancel_loading_status ( ) {
297
340
var loadingElement = document . getElementById ( 'Js_File_Loading' ) ;
298
341
if ( loadingElement ) {
@@ -311,6 +354,7 @@ function cancel_loading_status() {
311
354
} ) ;
312
355
}
313
356
}
357
+
314
358
function register_upload_event ( ) {
315
359
elem_upload_float = document . getElementById ( 'elem_upload_float' )
316
360
const upload_component = elem_upload_float . querySelector ( "input[type=file]" ) ;
@@ -321,13 +365,15 @@ function register_upload_event() {
321
365
} ) ;
322
366
}
323
367
}
368
+
324
369
function monitoring_input_box ( ) {
325
370
register_upload_event ( ) ;
326
371
327
372
elem_upload = document . getElementById ( 'elem_upload' )
328
373
elem_upload_float = document . getElementById ( 'elem_upload_float' )
329
374
elem_input_main = document . getElementById ( 'user_input_main' )
330
375
elem_input_float = document . getElementById ( 'user_input_float' )
376
+ elem_chatbot = document . getElementById ( 'gpt-chatbot' )
331
377
332
378
if ( elem_input_main ) {
333
379
if ( elem_input_main . querySelector ( "textarea" ) ) {
@@ -339,9 +385,8 @@ function monitoring_input_box() {
339
385
add_func_paste ( elem_input_float . querySelector ( "textarea" ) )
340
386
}
341
387
}
342
- gptChatbot = document . getElementById ( 'gpt-chatbot' )
343
- if ( gptChatbot ) {
344
- add_func_drag ( gptChatbot )
388
+ if ( elem_chatbot ) {
389
+ add_func_drag ( elem_chatbot )
345
390
}
346
391
}
347
392
@@ -352,6 +397,14 @@ window.addEventListener("DOMContentLoaded", function () {
352
397
gradioApp ( ) . addEventListener ( "render" , monitoring_input_box ) ;
353
398
} ) ;
354
399
400
+
401
+
402
+
403
+
404
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
405
+ // 第 5 部分: 音频按钮样式变化
406
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
407
+
355
408
function audio_fn_init ( ) {
356
409
let audio_component = document . getElementById ( 'elem_audio' ) ;
357
410
if ( audio_component ) {
@@ -388,6 +441,13 @@ function audio_fn_init() {
388
441
}
389
442
}
390
443
444
+
445
+
446
+
447
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
448
+ // 第 6 部分: JS初始化函数
449
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
450
+
391
451
function GptAcademicJavaScriptInit ( LAYOUT = "LEFT-RIGHT" ) {
392
452
audio_fn_init ( ) ;
393
453
chatbotIndicator = gradioApp ( ) . querySelector ( '#gpt-chatbot > div.wrap' ) ;
0 commit comments