mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-14 12:56:01 +08:00
embed.js add esc exit and fix avoid infinite nesting (#6360)
Co-authored-by: luowei <glpat-EjySCyNjWiLqAED-YmwM>
This commit is contained in:
parent
443e96777b
commit
279f1c986f
@ -11,6 +11,7 @@
|
|||||||
const configKey = "difyChatbotConfig";
|
const configKey = "difyChatbotConfig";
|
||||||
const buttonId = "dify-chatbot-bubble-button";
|
const buttonId = "dify-chatbot-bubble-button";
|
||||||
const iframeId = "dify-chatbot-bubble-window";
|
const iframeId = "dify-chatbot-bubble-window";
|
||||||
|
const config = window[configKey];
|
||||||
|
|
||||||
// SVG icons for open and close states
|
// SVG icons for open and close states
|
||||||
const svgIcons = {
|
const svgIcons = {
|
||||||
@ -24,7 +25,6 @@
|
|||||||
|
|
||||||
// Main function to embed the chatbot
|
// Main function to embed the chatbot
|
||||||
function embedChatbot() {
|
function embedChatbot() {
|
||||||
const config = window[configKey];
|
|
||||||
if (!config || !config.token) {
|
if (!config || !config.token) {
|
||||||
console.error(`${configKey} is empty or token is not provided`);
|
console.error(`${configKey} is empty or token is not provided`);
|
||||||
return;
|
return;
|
||||||
@ -141,11 +141,19 @@
|
|||||||
if (!targetIframe) {
|
if (!targetIframe) {
|
||||||
createIframe();
|
createIframe();
|
||||||
resetIframePosition();
|
resetIframePosition();
|
||||||
|
this.title = "Exit (ESC)";
|
||||||
displayDiv.innerHTML = svgIcons.close;
|
displayDiv.innerHTML = svgIcons.close;
|
||||||
|
document.addEventListener('keydown', handleEscKey);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
targetIframe.style.display = targetIframe.style.display === "none" ? "block" : "none";
|
targetIframe.style.display = targetIframe.style.display === "none" ? "block" : "none";
|
||||||
displayDiv.innerHTML = targetIframe.style.display === "none" ? svgIcons.open : svgIcons.close;
|
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();
|
resetIframePosition();
|
||||||
});
|
});
|
||||||
@ -220,6 +228,23 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the embedChatbot function to run when the body is loaded
|
// Add esc Exit keyboard event triggered
|
||||||
document.body.onload = embedChatbot;
|
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;
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
32
web/public/embed.min.js
vendored
32
web/public/embed.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user