Spaces:
Runtime error
Runtime error
Update assets/custom.js
Browse files- assets/custom.js +212 -53
assets/custom.js
CHANGED
@@ -1,70 +1,229 @@
|
|
|
|
1 |
// custom javascript here
|
|
|
2 |
const MAX_HISTORY_LENGTH = 32;
|
3 |
|
4 |
var key_down_history = [];
|
5 |
var currentIndex = -1;
|
6 |
var user_input_ta;
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
var ga = document.getElementsByTagName("gradio-app");
|
9 |
var targetNode = ga[0];
|
10 |
-
|
|
|
|
|
11 |
for (var i = 0; i < mutations.length; i++) {
|
12 |
-
if (mutations[i].addedNodes.length) {
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
if (
|
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 |
-
break;
|
62 |
}
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
}
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
});
|
|
|
67 |
|
68 |
-
// 监听目标节点的子节点列表是否发生变化
|
69 |
-
observer.observe(targetNode, { childList: true , subtree: true });
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
// custom javascript here
|
3 |
+
|
4 |
const MAX_HISTORY_LENGTH = 32;
|
5 |
|
6 |
var key_down_history = [];
|
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) {
|
95 |
+
userInfoDiv.classList.add("hideK");
|
96 |
+
} else {
|
97 |
+
userInfoDiv.classList.remove("hideK");
|
98 |
}
|
99 |
+
}
|
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
|
107 |
+
appTitleDiv.addEventListener("mouseenter", function () {
|
108 |
+
toggleUserInfoVisibility(false);
|
109 |
+
});
|
110 |
+
userInfoDiv.addEventListener("mouseenter", function () {
|
111 |
+
toggleUserInfoVisibility(false);
|
112 |
+
});
|
113 |
+
sendBtn.addEventListener("mouseenter", function () {
|
114 |
+
toggleUserInfoVisibility(false);
|
115 |
+
});
|
116 |
+
|
117 |
+
appTitleDiv.addEventListener("mouseleave", function () {
|
118 |
+
toggleUserInfoVisibility(true);
|
119 |
+
});
|
120 |
+
userInfoDiv.addEventListener("mouseleave", function () {
|
121 |
+
toggleUserInfoVisibility(true);
|
122 |
+
});
|
123 |
+
sendBtn.addEventListener("mouseleave", function () {
|
124 |
+
toggleUserInfoVisibility(true);
|
125 |
+
});
|
126 |
+
|
127 |
+
appTitleDiv.ontouchstart = function () {
|
128 |
+
toggleUserInfoVisibility(false);
|
129 |
+
};
|
130 |
+
userInfoDiv.ontouchstart = function () {
|
131 |
+
toggleUserInfoVisibility(false);
|
132 |
+
};
|
133 |
+
sendBtn.ontouchstart = function () {
|
134 |
+
toggleUserInfoVisibility(false);
|
135 |
+
};
|
136 |
+
|
137 |
+
appTitleDiv.ontouchend = function () {
|
138 |
+
setTimeout(function () {
|
139 |
+
toggleUserInfoVisibility(true);
|
140 |
+
}, 3000);
|
141 |
+
};
|
142 |
+
userInfoDiv.ontouchend = function () {
|
143 |
+
setTimeout(function () {
|
144 |
+
toggleUserInfoVisibility(true);
|
145 |
+
}, 3000);
|
146 |
+
};
|
147 |
+
sendBtn.ontouchend = function () {
|
148 |
+
setTimeout(function () {
|
149 |
+
toggleUserInfoVisibility(true);
|
150 |
+
}, 3000); // Delay 1 second to hide user info
|
151 |
+
};
|
152 |
+
|
153 |
+
// Hide user info after 2 second
|
154 |
+
setTimeout(function () {
|
155 |
+
toggleUserInfoVisibility(true);
|
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) {
|
186 |
+
if (chatbot) {
|
187 |
+
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px)`;
|
188 |
+
const wrap = chatbot.querySelector('.wrap');
|
189 |
+
if (wrap) {
|
190 |
+
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
|
191 |
+
}
|
192 |
+
}
|
193 |
+
} else if (screenWidth <= 499) {
|
194 |
+
if (chatbot) {
|
195 |
+
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px)`;
|
196 |
+
const wrap = chatbot.querySelector('.wrap');
|
197 |
+
if (wrap) {
|
198 |
+
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
|
199 |
+
}
|
200 |
+
}
|
201 |
+
} else {
|
202 |
+
if (chatbot) {
|
203 |
+
chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px)`;
|
204 |
+
const wrap = chatbot.querySelector('.wrap');
|
205 |
+
if (wrap) {
|
206 |
+
wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
|
207 |
+
}
|
208 |
+
}
|
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);
|