|
| 1 | +export async function GET({ params }) { |
| 2 | + const { id } = params; |
| 3 | + |
| 4 | + const script = `(function() { |
| 5 | + function resizeIframeToContentSize(iframe) { |
| 6 | + if (iframe.contentWindow) { |
| 7 | + const maxHeight = window.innerHeight * 0.8; // 80% of window height |
| 8 | + const chatContainerEl = iframe.contentWindow.document.getElementById('chat-container'); |
| 9 | + if(chatContainerEl){ |
| 10 | + const contentHeight = chatContainerEl.scrollHeight; |
| 11 | + console.log('Resizing iframe. Content height:', contentHeight); |
| 12 | + iframe.style.height = Math.max(400, Math.min(contentHeight, maxHeight)) + "px"; |
| 13 | + } |
| 14 | + } |
| 15 | + } |
| 16 | +
|
| 17 | + document.addEventListener('DOMContentLoaded', function() { |
| 18 | + const button = document.createElement('button'); |
| 19 | + 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'; |
| 20 | +
|
| 21 | + const img = document.createElement('img'); |
| 22 | + img.src = 'https://huggingface.co/chat/huggingchat/logo.svg'; |
| 23 | + img.alt = 'HuggingChat Logo'; |
| 24 | + img.className = 'size-4 mr-0.5'; |
| 25 | +
|
| 26 | + const text = document.createTextNode('Chat'); |
| 27 | +
|
| 28 | + button.appendChild(img); |
| 29 | + button.appendChild(text); |
| 30 | +
|
| 31 | + const modal = document.createElement('div'); |
| 32 | + modal.className = 'hidden fixed inset-0 z-[1001] overflow-auto bg-black bg-opacity-50'; |
| 33 | +
|
| 34 | + const modalContent = document.createElement('div'); |
| 35 | + modalContent.className = 'bg-transparent mx-auto my-[5%] max-w-2xl rounded'; |
| 36 | +
|
| 37 | + const closeButton = document.createElement('span'); |
| 38 | + closeButton.innerHTML = '×'; |
| 39 | + closeButton.className = 'text-gray-500 float-right text-2xl font-bold cursor-pointer hover:text-gray-700'; |
| 40 | +
|
| 41 | + const iframe = document.createElement('iframe'); |
| 42 | + iframe.className = 'w-full rounded-xl'; |
| 43 | + iframe.style.height = '400px'; // Set an initial height |
| 44 | + iframe.src = \`http://localhost:5173/chat/?embeddedAssistantId=${id}\`; |
| 45 | + |
| 46 | + iframe.onload = function() { |
| 47 | + console.log('Iframe loaded'); |
| 48 | + const iframeWindow = this.contentWindow; |
| 49 | + const iframeDocument = iframeWindow.document; |
| 50 | + |
| 51 | + let lastHeight = 0; |
| 52 | + |
| 53 | + function checkSize() { |
| 54 | + const chatContainer = iframeDocument.getElementById('chat-container'); |
| 55 | + if (chatContainer) { |
| 56 | + const newHeight = chatContainer.scrollHeight; |
| 57 | + if (newHeight !== lastHeight) { |
| 58 | + console.log('Height changed from', lastHeight, 'to', newHeight); |
| 59 | + resizeIframeToContentSize(iframe); |
| 60 | + lastHeight = newHeight; |
| 61 | + } |
| 62 | + } |
| 63 | + requestAnimationFrame(checkSize); |
| 64 | + } |
| 65 | +
|
| 66 | + // Start continuous size checking |
| 67 | + checkSize(); |
| 68 | +
|
| 69 | + // Set up MutationObserver as a backup |
| 70 | + const observer = new MutationObserver(() => { |
| 71 | + console.log('Mutation detected'); |
| 72 | + resizeIframeToContentSize(iframe); |
| 73 | + }); |
| 74 | +
|
| 75 | + function initMutationObserver() { |
| 76 | + const chatContainer = iframeDocument.getElementById('chat-container'); |
| 77 | + if (chatContainer) { |
| 78 | + console.log('Chat container found, setting up MutationObserver'); |
| 79 | + observer.observe(chatContainer, { childList: true, subtree: true, attributes: true, characterData: true }); |
| 80 | + } else { |
| 81 | + console.log('Chat container not found, retrying...'); |
| 82 | + setTimeout(initMutationObserver, 500); // Retry after 500ms |
| 83 | + } |
| 84 | + } |
| 85 | +
|
| 86 | + // Start trying to initialize the MutationObserver |
| 87 | + initMutationObserver(); |
| 88 | +
|
| 89 | + // Resize on load |
| 90 | + resizeIframeToContentSize(iframe); |
| 91 | + }; |
| 92 | +
|
| 93 | + modalContent.appendChild(closeButton); |
| 94 | + modalContent.appendChild(iframe); |
| 95 | + modal.appendChild(modalContent); |
| 96 | +
|
| 97 | + function closeModal() { |
| 98 | + modal.classList.add('hidden'); |
| 99 | + } |
| 100 | +
|
| 101 | + button.onclick = function() { |
| 102 | + modal.classList.remove('hidden'); |
| 103 | + resizeIframeToContentSize(iframe); // Resize on opening to ensure correct initial size |
| 104 | + }; |
| 105 | +
|
| 106 | + closeButton.onclick = closeModal; |
| 107 | +
|
| 108 | + window.onclick = function(event) { |
| 109 | + if (event.target == modal) { |
| 110 | + closeModal(); |
| 111 | + } |
| 112 | + }; |
| 113 | +
|
| 114 | + document.addEventListener('keydown', function(event) { |
| 115 | + if (event.key === 'Escape') { |
| 116 | + closeModal(); |
| 117 | + } |
| 118 | + }); |
| 119 | +
|
| 120 | + // Add resize event listener to adjust iframe height when window is resized |
| 121 | + window.addEventListener('resize', function() { |
| 122 | + if (!modal.classList.contains('hidden')) { |
| 123 | + resizeIframeToContentSize(iframe); |
| 124 | + } |
| 125 | + }); |
| 126 | +
|
| 127 | + document.body.appendChild(button); |
| 128 | + document.body.appendChild(modal); |
| 129 | + }); |
| 130 | +})(); |
| 131 | +`; |
| 132 | + |
| 133 | + return new Response(script, { |
| 134 | + headers: { |
| 135 | + "Content-Type": "application/javascript", |
| 136 | + "Access-Control-Allow-Origin": "*", |
| 137 | + }, |
| 138 | + }); |
| 139 | +} |
0 commit comments