Keldos commited on
Commit
0d358c4
1 Parent(s): b39ce2c

chore: chatbot高度调整增加`iframe`判断,避免窗口疯狂变长

Browse files
Files changed (1) hide show
  1. assets/custom.js +21 -35
assets/custom.js CHANGED
@@ -16,26 +16,28 @@ 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
  }
@@ -89,6 +91,7 @@ function selectHistory() {
89
  });
90
  }
91
  }
 
92
  function toggleUserInfoVisibility(shouldHide) {
93
  if (userInfoDiv) {
94
  if (shouldHide) {
@@ -99,8 +102,6 @@ function toggleUserInfoVisibility(shouldHide) {
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
@@ -179,50 +180,35 @@ 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);
 
16
 
17
  var ga = document.getElementsByTagName("gradio-app");
18
  var targetNode = ga[0];
19
+ var isInIframe = (window.self !== window.top);
20
 
21
+ // gradio 页面加载好了么??? 我能动你的元素了么??
22
  function gradioLoaded(mutations) {
23
  for (var i = 0; i < mutations.length; i++) {
24
  if (mutations[i].addedNodes.length) {
25
  gradioContainer = document.querySelector(".gradio-container");
26
  user_input_tb = document.getElementById('user_input_tb');
27
  userInfoDiv = document.getElementById("user_info");
28
+ appTitleDiv = document.getElementById("app_title");
29
  chatbot = document.querySelector('#chuanhu_chatbot');
30
 
31
+ if (gradioContainer) { // gradioCainter 加载出来了没?
32
  adjustDarkMode();
33
  }
34
  if (user_input_tb) { // user_input_tb 加载出来了没?
35
  selectHistory();
36
  }
37
+ if (userInfoDiv && appTitleDiv) { // userInfoDiv 和 appTitleDiv 加载出来了没?
38
  setTimeout(showOrHideUserInfo(), 2000);
39
  }
40
+ if (chatbot) { // chatbot 加载出来了没?
41
  setChatbotHeight()
42
  }
43
  }
 
91
  });
92
  }
93
  }
94
+
95
  function toggleUserInfoVisibility(shouldHide) {
96
  if (userInfoDiv) {
97
  if (shouldHide) {
 
102
  }
103
  }
104
  function showOrHideUserInfo() {
 
 
105
  var sendBtn = document.getElementById("submit_btn");
106
 
107
  // Bind mouse/touch events to show/hide user info
 
180
  const screenWidth = window.innerWidth;
181
  const statusDisplay = document.querySelector('#status_display');
182
  const statusDisplayHeight = statusDisplay ? statusDisplay.offsetHeight : 0;
183
+ const wrap = chatbot.querySelector('.wrap');
184
  const vh = window.innerHeight * 0.01;
185
  document.documentElement.style.setProperty('--vh', `${vh}px`);
186
+ if (isInIframe) {
187
+ chatbot.style.height = `700px`;
188
+ wrap.style.maxHeight = `calc(700px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`
189
+ } else {
190
+ if (screenWidth <= 320) {
191
  chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px)`;
192
+ wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
193
+ } else if (screenWidth <= 499) {
 
 
 
 
 
194
  chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px)`;
195
+ wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
196
+ } else {
 
 
 
 
 
197
  chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px)`;
198
+ wrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
 
 
 
199
  }
200
  }
201
  }
202
 
203
+ // 监视页面内部 DOM 变动
204
  var observer = new MutationObserver(function (mutations) {
205
  gradioLoaded(mutations);
206
  });
207
  observer.observe(targetNode, { childList: true, subtree: true });
208
 
209
+ // 监视页面变化
210
  window.addEventListener("DOMContentLoaded", function () {
211
+ isInIframe = (window.self !== window.top);
 
 
 
 
 
212
  });
213
  window.addEventListener('resize', setChatbotHeight);
214
  window.addEventListener('scroll', setChatbotHeight);