浏览代码

embed.js add esc exit and fix avoid infinite nesting (#6360)

Co-authored-by: luowei <glpat-EjySCyNjWiLqAED-YmwM>
Charlie.Wei 9 月之前
父节点
当前提交
279f1c986f
共有 2 个文件被更改,包括 28 次插入3 次删除
  1. 28 3
      web/public/embed.js
  2. 0 0
      web/public/embed.min.js

+ 28 - 3
web/public/embed.js

@@ -11,6 +11,7 @@
   const configKey = "difyChatbotConfig";
   const buttonId = "dify-chatbot-bubble-button";
   const iframeId = "dify-chatbot-bubble-window";
+  const config = window[configKey];
 
   // SVG icons for open and close states
   const svgIcons = {
@@ -24,7 +25,6 @@
 
   // Main function to embed the chatbot
   function embedChatbot() {
-    const config = window[configKey];
     if (!config || !config.token) {
       console.error(`${configKey} is empty or token is not provided`);
       return;
@@ -141,11 +141,19 @@
         if (!targetIframe) {
           createIframe();
           resetIframePosition();
+          this.title = "Exit (ESC)";
           displayDiv.innerHTML = svgIcons.close;
+          document.addEventListener('keydown', handleEscKey);
           return;
         }
         targetIframe.style.display = targetIframe.style.display === "none" ? "block" : "none";
         displayDiv.innerHTML = targetIframe.style.display === "none" ? svgIcons.open : svgIcons.close;
+
+        if (targetIframe.style.display === "none") {
+          document.removeEventListener('keydown', handleEscKey);
+        } else {
+          document.addEventListener('keydown', handleEscKey);
+        }
         
         resetIframePosition();
       });
@@ -220,6 +228,23 @@
     }
   }
 
-  // Set the embedChatbot function to run when the body is loaded
-  document.body.onload = embedChatbot;
+  // Add esc Exit keyboard event triggered
+  function handleEscKey(event) {
+    if (event.key === 'Escape') {
+      const targetIframe = document.getElementById(iframeId);
+      const button = document.getElementById(buttonId);
+      if (targetIframe && targetIframe.style.display !== 'none') {
+        targetIframe.style.display = 'none';
+        button.querySelector('div').innerHTML = svgIcons.open;
+      }
+    }
+  }
+  document.addEventListener('keydown', handleEscKey);
+
+  // Set the embedChatbot function to run when the body is loaded,Avoid infinite nesting
+  if (config?.dynamicScript) {
+    embedChatbot();
+  } else {
+    document.body.onload = embedChatbot;
+  }
 })();

文件差异内容过多而无法显示
+ 0 - 0
web/public/embed.min.js


部分文件因为文件数量过多而无法显示