-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
5 changed files
with
165 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
export async function GET() { | ||
const script = `(function() { | ||
function resizeIframeToContentSize(iframe) { | ||
if (iframe.contentWindow) { | ||
const maxHeight = window.innerHeight * 0.8; // 80% of window height | ||
const contentHeight = iframe.contentWindow.document.body.scrollHeight; | ||
console.log("contentHeight", contentHeight); | ||
iframe.style.height = Math.min(contentHeight, maxHeight) + "px"; | ||
} | ||
} | ||
document.addEventListener('DOMContentLoaded', function() { | ||
const button = document.createElement('button'); | ||
button.innerHTML = 'AI Chat'; | ||
button.className = 'fixed bottom-5 left-5 z-50 px-5 py-2.5 bg-blue-500 text-white rounded cursor-pointer hover:bg-blue-600 transition-colors'; | ||
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 = '500px'; // Set an initial height | ||
iframe.src = \`http://localhost:5173/chat/\`; | ||
iframe.onload = function() { | ||
const iframeWindow = this.contentWindow; | ||
iframeWindow.parent = { | ||
resizeIframeToContentSize: resizeIframeToContentSize.bind(null, iframe) | ||
}; | ||
const script = iframeWindow.document.createElement('script'); | ||
script.textContent = \` | ||
this.container = this.frameElement.contentWindow.document.body; | ||
this.lastScrollHeight = 0; | ||
this.watch = () => { | ||
cancelAnimationFrame(this.watcher); | ||
if (this.lastScrollHeight !== container.scrollHeight) { | ||
parent.resizeIframeToContentSize(); | ||
} | ||
this.lastScrollHeight = container.scrollHeight; | ||
this.watcher = requestAnimationFrame(this.watch); | ||
}; | ||
this.watcher = window.requestAnimationFrame(this.watch); | ||
\`; | ||
iframeWindow.document.body.appendChild(script); | ||
}; | ||
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": "*", | ||
}, | ||
}); | ||
} |