I have this code inside my vue 3 app. I am using deep chat npm library https://www.npmjs.com/package/deep-chat-react for integrating AI chat functionality. I am not able to figure out how can I add a copy button to the AI generated chat. I want on click of that button, user should be able to copy the text for that particular chat on the clipboard.
Demo project link – https://stackblitz.com/edit/deep-chat-vue3?file=src%2FApp.vue
<deep-chat
intro-message='{"text": "Hi I am your assistant, ask me anything!"}'
:history="processedChatHistory"
:on-message="message => handleNewMessage(message)"
microphone="true"
auxiliary-style="
::-webkit-scrollbar {
width: 10px;
height: 10px;
}
::-webkit-scrollbar-thumb {
background-color: #54a7ff;
border-radius: 5px;
}"
class="deep-chat"
:direct-connection="{
openAI: {
key: openAiKey,
validateKeyProperty: true,
chat: { system_prompt: 'Assist me with anything you can' }
}
}"
:input-area-style="{ backgroundColor: '#ebf5ff' }"
>
<div
style="
width: 350px;
background-color: #fafafa;
border-radius: 10px;
padding: 12px;
padding-bottom: 15px;
display: none;
"
>
<div>
<div
style="
text-align: center;
margin-bottom: 15 px;
color: #444;
font-size: 12 px;
"
>
<b>AI-Powered Package Suggestions</b>
</div>
<div
style="
font-size: 12 px;
font-weight: 300;
text-align: center;
color: #bbb;
line-height: 20 px;
"
>
Our AI Assistance tool helps you design and customize dynamic packages
effortlessly
</div>
</div>
</div>
</deep-chat>
const deepChatHistory = ref([]);
const handleNewMessage=(event)=> {
if(event?.detail?.message && !event?.detail?.isInitial){
deepChatHistory.value.push(event?.detail?.message);
}
}
I’d be glad to help you add dynamic copy text functionality to your Deep Chat integration in Vue.js! Here’s a breakdown combining the best aspects of previous responses and addressing potential issues:
Understanding the Deep Chat Library:
Adding Copy Functionality:
Create a Custom Component (Recommended):
CopyButton.vue:<!– end list –>
Integrate the Component:
App.vuetemplate, modify thedeep-chatcomponent to include theCopyButton:Handle Copy Events (Optional):
App.vuescript, add methods to handle success and error events:<!– end list –>
Explanation:
CopyButton.vuecomponent creates a button with aclickevent handler.copyToClipboardmethod gets the text content of the previous element (the chat message) usingthis.$el.previousElementSibling.textContent.textareaelement, sets its value to the text, appends it to the body, and selects it.document.execCommand('copy').App.vuetemplate, you iterate through the chat history and render the chat messages along with theCopyButtoncomponent for each message.App.vueto handle success and error events from theCopyButton.