-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
261 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
export async function GET({ params }) { | ||
const { id } = params; | ||
|
||
const script = `(function() { | ||
function resizeIframeToContentSize(iframe) { | ||
if (iframe.contentWindow) { | ||
const maxHeight = window.innerHeight * 0.8; // 80% of window height | ||
const chatContainerEl = iframe.contentWindow.document.getElementById('chat-container'); | ||
if(chatContainerEl){ | ||
const contentHeight = chatContainerEl.scrollHeight; | ||
console.log('Resizing iframe. Content height:', contentHeight); | ||
iframe.style.height = Math.max(400, Math.min(contentHeight, maxHeight)) + "px"; | ||
} | ||
} | ||
} | ||
document.addEventListener('DOMContentLoaded', function() { | ||
const button = document.createElement('button'); | ||
button.className = 'fixed bottom-5 right-5 z-50 px-1.5 py-1 bg-blue-500 text-white rounded cursor-pointer hover:bg-blue-600 transition-colors flex items-center focus:outline-none'; | ||
const img = document.createElement('img'); | ||
img.src = 'https://huggingface.co/chat/huggingchat/logo.svg'; | ||
img.alt = 'HuggingChat Logo'; | ||
img.className = 'size-4 mr-0.5'; | ||
const text = document.createTextNode('Chat'); | ||
button.appendChild(img); | ||
button.appendChild(text); | ||
const modal = document.createElement('div'); | ||
modal.className = 'hidden fixed inset-0 z-[1001] overflow-auto bg-black bg-opacity-50'; | ||
const modalContent = document.createElement('div'); | ||
modalContent.className = 'bg-transparent mx-auto my-[5%] max-w-2xl rounded'; | ||
const closeButton = document.createElement('span'); | ||
closeButton.innerHTML = '×'; | ||
closeButton.className = 'text-gray-500 float-right text-2xl font-bold cursor-pointer hover:text-gray-700'; | ||
const iframe = document.createElement('iframe'); | ||
iframe.className = 'w-full rounded-xl'; | ||
iframe.style.height = '400px'; // Set an initial height | ||
iframe.src = \`http://localhost:5173/chat/?embeddedAssistantId=${id}\`; | ||
iframe.onload = function() { | ||
console.log('Iframe loaded'); | ||
const iframeWindow = this.contentWindow; | ||
const iframeDocument = iframeWindow.document; | ||
let lastHeight = 0; | ||
function checkSize() { | ||
const chatContainer = iframeDocument.getElementById('chat-container'); | ||
if (chatContainer) { | ||
const newHeight = chatContainer.scrollHeight; | ||
if (newHeight !== lastHeight) { | ||
console.log('Height changed from', lastHeight, 'to', newHeight); | ||
resizeIframeToContentSize(iframe); | ||
lastHeight = newHeight; | ||
} | ||
} | ||
requestAnimationFrame(checkSize); | ||
} | ||
// Start continuous size checking | ||
checkSize(); | ||
// Set up MutationObserver as a backup | ||
const observer = new MutationObserver(() => { | ||
console.log('Mutation detected'); | ||
resizeIframeToContentSize(iframe); | ||
}); | ||
function initMutationObserver() { | ||
const chatContainer = iframeDocument.getElementById('chat-container'); | ||
if (chatContainer) { | ||
console.log('Chat container found, setting up MutationObserver'); | ||
observer.observe(chatContainer, { childList: true, subtree: true, attributes: true, characterData: true }); | ||
} else { | ||
console.log('Chat container not found, retrying...'); | ||
setTimeout(initMutationObserver, 500); // Retry after 500ms | ||
} | ||
} | ||
// Start trying to initialize the MutationObserver | ||
initMutationObserver(); | ||
// Resize on load | ||
resizeIframeToContentSize(iframe); | ||
}; | ||
modalContent.appendChild(closeButton); | ||
modalContent.appendChild(iframe); | ||
modal.appendChild(modalContent); | ||
function closeModal() { | ||
modal.classList.add('hidden'); | ||
} | ||
button.onclick = function() { | ||
modal.classList.remove('hidden'); | ||
resizeIframeToContentSize(iframe); // Resize on opening to ensure correct initial size | ||
}; | ||
closeButton.onclick = closeModal; | ||
window.onclick = function(event) { | ||
if (event.target == modal) { | ||
closeModal(); | ||
} | ||
}; | ||
document.addEventListener('keydown', function(event) { | ||
if (event.key === 'Escape') { | ||
closeModal(); | ||
} | ||
}); | ||
// Add resize event listener to adjust iframe height when window is resized | ||
window.addEventListener('resize', function() { | ||
if (!modal.classList.contains('hidden')) { | ||
resizeIframeToContentSize(iframe); | ||
} | ||
}); | ||
document.body.appendChild(button); | ||
document.body.appendChild(modal); | ||
}); | ||
})(); | ||
`; | ||
|
||
return new Response(script, { | ||
headers: { | ||
"Content-Type": "application/javascript", | ||
"Access-Control-Allow-Origin": "*", | ||
}, | ||
}); | ||
} |
Oops, something went wrong.