Keldos commited on
Commit
38856b5
·
1 Parent(s): 9fb61e6

feat: 更新窗口加入:自动检测&手动检测&窗口高度跟随

Browse files
Files changed (3) hide show
  1. assets/custom.css +1 -1
  2. assets/custom.js +25 -4
  3. modules/utils.py +2 -0
assets/custom.css CHANGED
@@ -50,7 +50,7 @@ footer {
50
  width: 100%;
51
  justify-content: center;
52
  z-index: var(--layer-top);
53
- transition: top 0.5s ease-in-out;
54
  }
55
  #check-chuanhu-update {
56
  position: absolute;
 
50
  width: 100%;
51
  justify-content: center;
52
  z-index: var(--layer-top);
53
+ transition: top 0.3s ease-out;
54
  }
55
  #check-chuanhu-update {
56
  position: absolute;
assets/custom.js CHANGED
@@ -29,6 +29,7 @@ var ga = document.getElementsByTagName("gradio-app");
29
  var targetNode = ga[0];
30
  var isInIframe = (window.self !== window.top);
31
  var language = navigator.language.slice(0,2);
 
32
 
33
  var forView_i18n = {
34
  'zh': "仅供查看",
@@ -92,7 +93,8 @@ function gradioLoaded(mutations) {
92
  setChatbotScroll();
93
  }
94
  if (updateToast) {
95
- if (!updateInfoGotten) {
 
96
  updateLatestVersion();
97
  }
98
  }
@@ -504,6 +506,7 @@ function clearHistoryHtml() {
504
  }
505
  }
506
 
 
507
  async function getLatestRelease() {
508
  try {
509
  const response = await fetch('https://api.github.com/repos/gaizhenbiao/chuanhuchatgpt/releases/latest');
@@ -542,6 +545,8 @@ async function updateLatestVersion() {
542
  } else {
543
  noUpdate();
544
  }
 
 
545
  }
546
  } catch (error) {
547
  console.error(error);
@@ -555,10 +560,19 @@ function cancelUpdate() {
555
  closeUpdateToast();
556
  }
557
  function openUpdateToast() {
558
- updateToast.style.setProperty('top', '-20px');
 
 
 
 
559
  }
560
  function closeUpdateToast() {
561
  updateToast.style.setProperty('top', '-500px');
 
 
 
 
 
562
  }
563
  function noUpdate() {
564
  const versionInfoElement = document.getElementById('version-info-title');
@@ -570,7 +584,14 @@ function noUpdate() {
570
  gotoUpdateBtn.classList.add('hideK');
571
  closeUpdateBtn.classList.remove('hideK');
572
  }
573
-
 
 
 
 
 
 
 
574
  // 监视页面内部 DOM 变动
575
  var observer = new MutationObserver(function (mutations) {
576
  gradioLoaded(mutations);
@@ -583,7 +604,7 @@ window.addEventListener("DOMContentLoaded", function () {
583
  historyLoaded = false;
584
  });
585
  window.addEventListener('resize', setChatbotHeight);
586
- window.addEventListener('scroll', setChatbotHeight);
587
  window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", adjustDarkMode);
588
 
589
  // button svg code
 
29
  var targetNode = ga[0];
30
  var isInIframe = (window.self !== window.top);
31
  var language = navigator.language.slice(0,2);
32
+ var currentTime = new Date().getTime();
33
 
34
  var forView_i18n = {
35
  'zh': "仅供查看",
 
93
  setChatbotScroll();
94
  }
95
  if (updateToast) {
96
+ const lastCheckTime = localStorage.getItem('lastCheckTime') || 0;
97
+ if (currentTime - lastCheckTime > 3 * 24 * 60 * 60 * 1000 && !updateInfoGotten) {
98
  updateLatestVersion();
99
  }
100
  }
 
506
  }
507
  }
508
 
509
+ var showingUpdateInfo = false;
510
  async function getLatestRelease() {
511
  try {
512
  const response = await fetch('https://api.github.com/repos/gaizhenbiao/chuanhuchatgpt/releases/latest');
 
545
  } else {
546
  noUpdate();
547
  }
548
+ currentTime = new Date().getTime();
549
+ localStorage.setItem('lastCheckTime', currentTime);
550
  }
551
  } catch (error) {
552
  console.error(error);
 
560
  closeUpdateToast();
561
  }
562
  function openUpdateToast() {
563
+ setUpdateWindowHeight();
564
+ showingUpdateInfo = true;
565
+ // 是的,这个逻辑是弹出窗口也算检测过更新一次。
566
+ currentTime = new Date().getTime();
567
+ localStorage.setItem('lastCheckTime', currentTime);
568
  }
569
  function closeUpdateToast() {
570
  updateToast.style.setProperty('top', '-500px');
571
+ showingUpdateInfo = false;
572
+ }
573
+ function manualCheckUpdate() {
574
+ openUpdateToast();
575
+ updateLatestVersion();
576
  }
577
  function noUpdate() {
578
  const versionInfoElement = document.getElementById('version-info-title');
 
584
  gotoUpdateBtn.classList.add('hideK');
585
  closeUpdateBtn.classList.remove('hideK');
586
  }
587
+ function setUpdateWindowHeight() {
588
+ if (!showingUpdateInfo) {return;}
589
+ const scrollPosition = window.scrollY;
590
+ // const originalTop = updateToast.style.getPropertyValue('top');
591
+ const resultTop = scrollPosition - 20 + 'px';
592
+ updateToast.style.setProperty('top', resultTop);
593
+ }
594
+
595
  // 监视页面内部 DOM 变动
596
  var observer = new MutationObserver(function (mutations) {
597
  gradioLoaded(mutations);
 
604
  historyLoaded = false;
605
  });
606
  window.addEventListener('resize', setChatbotHeight);
607
+ window.addEventListener('scroll', function(){setChatbotHeight();setUpdateWindowHeight();});
608
  window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", adjustDarkMode);
609
 
610
  // button svg code
modules/utils.py CHANGED
@@ -577,6 +577,8 @@ def versions_html():
577
  Gradio: {gr.__version__}
578
   • 
579
  <a style="text-decoration:none;color:inherit" href="https://github.com/GaiZhenbiao/ChuanhuChatGPT">ChuanhuChat</a>: {repo_version}
 
 
580
  """
581
 
582
  def get_html(filename):
 
577
  Gradio: {gr.__version__}
578
   • 
579
  <a style="text-decoration:none;color:inherit" href="https://github.com/GaiZhenbiao/ChuanhuChatGPT">ChuanhuChat</a>: {repo_version}
580
+  • 
581
+ <span onclick="manualCheckUpdate()">Check for update...</span>
582
  """
583
 
584
  def get_html(filename):