Spaces:
Sleeping
Sleeping
Keldos
commited on
Commit
•
b39ce2c
1
Parent(s):
8267ffb
feat: 切换亮暗色时的同时强制更改gradio框架亮暗色
Browse filesfix #552
另外,我重构了整个js框架,应该是没bug的,应该(
- 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 |
-
|
|
|
13 |
for (var i = 0; i < mutations.length; i++) {
|
14 |
-
if (mutations[i].addedNodes.length) {
|
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 |
-
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 |
-
|
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 |
-
|
175 |
-
|
176 |
-
}
|
177 |
-
);
|
178 |
observer.observe(targetNode, { childList: true, subtree: true });
|
179 |
|
180 |
|
181 |
window.addEventListener("DOMContentLoaded", function () {
|
182 |
-
setChatbotHeight();
|
183 |
-
setTimeout(function () {
|
184 |
-
|
185 |
-
|
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);
|