Keldos commited on
Commit
b39ce2c
1 Parent(s): 8267ffb

feat: 切换亮暗色时的同时强制更改gradio框架亮暗色

Browse files

fix #552
另外,我重构了整个js框架,应该是没bug的,应该(

Files changed (1) hide show
  1. assets/custom.js +104 -66
assets/custom.js CHANGED
@@ -1,3 +1,4 @@
 
1
  // custom javascript here
2
 
3
  const MAX_HISTORY_LENGTH = 32;
@@ -6,67 +7,88 @@ var key_down_history = [];
6
  var currentIndex = -1;
7
  var user_input_ta;
8
 
 
 
 
 
 
 
 
9
  var ga = document.getElementsByTagName("gradio-app");
10
  var targetNode = ga[0];
11
 
12
- function selectHistory(mutations) {
 
13
  for (var i = 0; i < mutations.length; i++) {
14
- if (mutations[i].addedNodes.length) {
15
- var user_input_tb = document.getElementById('user_input_tb');
16
- if (user_input_tb) {
17
- // 监听到user_input_tb被添加到DOM树中
18
- // 这里可以编写元素加载完成后需要执行的代码
19
- user_input_ta = user_input_tb.querySelector("textarea");
20
- if (user_input_ta) {
21
- observer.disconnect(); // 停止监听
22
- // 在 textarea 上监听 keydown 事件
23
- user_input_ta.addEventListener("keydown", function (event) {
24
- var value = user_input_ta.value.trim();
25
- // 判断按下的是否为方向键
26
- if (event.code === 'ArrowUp' || event.code === 'ArrowDown') {
27
- // 如果按下的是方向键,且输入框中有内容,且历史记录中没有该内容,则不执行操作
28
- if (value && key_down_history.indexOf(value) === -1)
29
- return;
30
- // 对于需要响应的动作,阻止默认行为。
31
- event.preventDefault();
32
- var length = key_down_history.length;
33
- if (length === 0) {
34
- currentIndex = -1; // 如果历史记录为空,直接将当前选中的记录重置
35
- return;
36
- }
37
- if (currentIndex === -1) {
38
- currentIndex = length;
39
- }
40
- if (event.code === 'ArrowUp' && currentIndex > 0) {
41
- currentIndex--;
42
- user_input_ta.value = key_down_history[currentIndex];
43
- } else if (event.code === 'ArrowDown' && currentIndex < length - 1) {
44
- currentIndex++;
45
- user_input_ta.value = key_down_history[currentIndex];
46
- }
47
- user_input_ta.selectionStart = user_input_ta.value.length;
48
- user_input_ta.selectionEnd = user_input_ta.value.length;
49
- const input_event = new InputEvent("input", { bubbles: true, cancelable: true });
50
- user_input_ta.dispatchEvent(input_event);
51
- } else if (event.code === "Enter") {
52
- if (value) {
53
- currentIndex = -1;
54
- if (key_down_history.indexOf(value) === -1) {
55
- key_down_history.push(value);
56
- if (key_down_history.length > MAX_HISTORY_LENGTH) {
57
- key_down_history.shift();
58
- }
59
- }
60
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  }
62
- });
63
- break;
64
  }
65
  }
66
- }
67
  }
68
  }
69
- var userInfoDiv = null;
70
  function toggleUserInfoVisibility(shouldHide) {
71
  if (userInfoDiv) {
72
  if (shouldHide) {
@@ -78,7 +100,7 @@ function toggleUserInfoVisibility(shouldHide) {
78
  }
79
  function showOrHideUserInfo() {
80
  userInfoDiv = document.getElementById("user_info");
81
- var appTitleDiv = document.getElementById("app_title");
82
  var sendBtn = document.getElementById("submit_btn");
83
 
84
  // Bind mouse/touch events to show/hide user info
@@ -134,11 +156,30 @@ function showOrHideUserInfo() {
134
  }, 2000);
135
  }
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  function setChatbotHeight() {
138
  const screenWidth = window.innerWidth;
139
  const statusDisplay = document.querySelector('#status_display');
140
  const statusDisplayHeight = statusDisplay ? statusDisplay.offsetHeight : 0;
141
- const chatbot = document.querySelector('#chuanhu_chatbot');
142
  const vh = window.innerHeight * 0.01;
143
  document.documentElement.style.setProperty('--vh', `${vh}px`);
144
  if (screenWidth <= 320) {
@@ -168,24 +209,21 @@ function setChatbotHeight() {
168
  }
169
  }
170
 
171
- setChatbotHeight();
172
 
173
  var observer = new MutationObserver(function (mutations) {
174
- selectHistory(mutations);
175
- showOrHideUserInfo();
176
- }
177
- );
178
  observer.observe(targetNode, { childList: true, subtree: true });
179
 
180
 
181
  window.addEventListener("DOMContentLoaded", function () {
182
- setChatbotHeight();
183
- setTimeout(function () {
184
- showOrHideUserInfo();
185
- setChatbotHeight();
186
- }, 2000);
 
187
  });
188
-
189
  window.addEventListener('resize', setChatbotHeight);
190
  window.addEventListener('scroll', setChatbotHeight);
191
-
 
1
+
2
  // custom javascript here
3
 
4
  const MAX_HISTORY_LENGTH = 32;
 
7
  var currentIndex = -1;
8
  var user_input_ta;
9
 
10
+ var gradioContainer = null;
11
+ var user_input_ta = null;
12
+ var user_input_tb = null;
13
+ var userInfoDiv = null;
14
+ var appTitleDiv = null;
15
+ var chatbot = null;
16
+
17
  var ga = document.getElementsByTagName("gradio-app");
18
  var targetNode = ga[0];
19
 
20
+ // gradio 页面加载好了么???
21
+ function gradioLoaded(mutations) {
22
  for (var i = 0; i < mutations.length; i++) {
23
+ if (mutations[i].addedNodes.length) {
24
+ gradioContainer = document.querySelector(".gradio-container");
25
+ user_input_tb = document.getElementById('user_input_tb');
26
+ userInfoDiv = document.getElementById("user_info");
27
+ chatbot = document.querySelector('#chuanhu_chatbot');
28
+
29
+ if (gradioContainer) { // user_input_tb 加载出来了没?
30
+ adjustDarkMode();
31
+ }
32
+ if (user_input_tb) { // user_input_tb 加载出来了没?
33
+ selectHistory();
34
+ }
35
+ if (userInfoDiv) { // user_input_tb 加载出来了没?
36
+ setTimeout(showOrHideUserInfo(), 2000);
37
+ }
38
+ if (chatbot) { // user_input_tb 加载出来了没?
39
+ setChatbotHeight()
40
+ }
41
+ }
42
+ }
43
+ }
44
+
45
+ function selectHistory() {
46
+ user_input_ta = user_input_tb.querySelector("textarea");
47
+ if (user_input_ta) {
48
+ observer.disconnect(); // 停止监听
49
+ // textarea 上监听 keydown 事件
50
+ user_input_ta.addEventListener("keydown", function (event) {
51
+ var value = user_input_ta.value.trim();
52
+ // 判断按下的是否为方向键
53
+ if (event.code === 'ArrowUp' || event.code === 'ArrowDown') {
54
+ // 如果按下的是方向键,且输入框中有内容,且历史记录中没有该内容,则不执行操作
55
+ if (value && key_down_history.indexOf(value) === -1)
56
+ return;
57
+ // 对于需要响应的动作,阻止默认行为。
58
+ event.preventDefault();
59
+ var length = key_down_history.length;
60
+ if (length === 0) {
61
+ currentIndex = -1; // 如果历史记录为空,直接将当前选中的记录重置
62
+ return;
63
+ }
64
+ if (currentIndex === -1) {
65
+ currentIndex = length;
66
+ }
67
+ if (event.code === 'ArrowUp' && currentIndex > 0) {
68
+ currentIndex--;
69
+ user_input_ta.value = key_down_history[currentIndex];
70
+ } else if (event.code === 'ArrowDown' && currentIndex < length - 1) {
71
+ currentIndex++;
72
+ user_input_ta.value = key_down_history[currentIndex];
73
+ }
74
+ user_input_ta.selectionStart = user_input_ta.value.length;
75
+ user_input_ta.selectionEnd = user_input_ta.value.length;
76
+ const input_event = new InputEvent("input", { bubbles: true, cancelable: true });
77
+ user_input_ta.dispatchEvent(input_event);
78
+ } else if (event.code === "Enter") {
79
+ if (value) {
80
+ currentIndex = -1;
81
+ if (key_down_history.indexOf(value) === -1) {
82
+ key_down_history.push(value);
83
+ if (key_down_history.length > MAX_HISTORY_LENGTH) {
84
+ key_down_history.shift();
85
  }
86
+ }
 
87
  }
88
  }
89
+ });
90
  }
91
  }
 
92
  function toggleUserInfoVisibility(shouldHide) {
93
  if (userInfoDiv) {
94
  if (shouldHide) {
 
100
  }
101
  function showOrHideUserInfo() {
102
  userInfoDiv = document.getElementById("user_info");
103
+ appTitleDiv = document.getElementById("app_title");
104
  var sendBtn = document.getElementById("submit_btn");
105
 
106
  // Bind mouse/touch events to show/hide user info
 
156
  }, 2000);
157
  }
158
 
159
+ function toggleDarkMode(isEnabled) {
160
+ gradioContainer = document.querySelector(".gradio-container");
161
+ if (isEnabled) {
162
+ gradioContainer.classList.add("dark");
163
+ document.body.style.backgroundColor = "";
164
+ } else {
165
+ gradioContainer.classList.remove("dark");
166
+ }
167
+ }
168
+ function adjustDarkMode() {
169
+ const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
170
+ // 根据当前颜色模式设置初始状态
171
+ toggleDarkMode(darkModeQuery.matches);
172
+ // 监听颜色模式变化
173
+ darkModeQuery.addEventListener("change", (e) => {
174
+ toggleDarkMode(e.matches);
175
+ });
176
+ }
177
+
178
  function setChatbotHeight() {
179
  const screenWidth = window.innerWidth;
180
  const statusDisplay = document.querySelector('#status_display');
181
  const statusDisplayHeight = statusDisplay ? statusDisplay.offsetHeight : 0;
182
+ // const chatbot = document.querySelector('#chuanhu_chatbot');
183
  const vh = window.innerHeight * 0.01;
184
  document.documentElement.style.setProperty('--vh', `${vh}px`);
185
  if (screenWidth <= 320) {
 
209
  }
210
  }
211
 
 
212
 
213
  var observer = new MutationObserver(function (mutations) {
214
+ gradioLoaded(mutations);
215
+ });
 
 
216
  observer.observe(targetNode, { childList: true, subtree: true });
217
 
218
 
219
  window.addEventListener("DOMContentLoaded", function () {
220
+ // setChatbotHeight();
221
+ // setTimeout(function () {
222
+ // showOrHideUserInfo();
223
+ // setChatbotHeight();
224
+ // }, 2000);
225
+ // 本来是为了页面刷出来等两秒重试的,现在这么写不需要了~ 但框架先留着万一需要呢QAQ
226
  });
 
227
  window.addEventListener('resize', setChatbotHeight);
228
  window.addEventListener('scroll', setChatbotHeight);
229
+ window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", adjustDarkMode);