Skip to content

Commit

Permalink
feat: alert user that chat has been copied via aria-live (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellvix authored Dec 4, 2024
1 parent 932c050 commit 326d4bf
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/js/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,7 @@ class ChatLLM {
*/
CopyChatHistory(e) {
let text = '';
let notificationText = '';
if (typeof e == 'undefined') {
// check for passthrough
// get html of the full chat history
Expand All @@ -1971,9 +1972,11 @@ class ChatLLM {
if (e.target.id == 'chatLLM_copy_all') {
// get html of the full chat history
text = document.getElementById('chatLLM_chat_history').innerHTML;
notificationText = 'Copied All.';
} else if (e.target.classList.contains('chatLLM_message_copy_button')) {
// get the text of the element before the button
text = e.target.closest('p').previousElementSibling.innerHTML;
notificationText = 'Copied.';
}
} else if (e.type == 'keyup') {
// check for alt shift c or ctrl shift c
Expand All @@ -1985,6 +1988,7 @@ class ChatLLM {
);
if (elem) {
text = elem.innerHTML;
notificationText = 'Copied.';
}
} else if (
e.key == 'A' &&
Expand All @@ -1994,6 +1998,7 @@ class ChatLLM {
e.preventDefault();
// get html of the full chat history
text = document.getElementById('chatLLM_chat_history').innerHTML;
notificationText = 'Copied All.';
}
}

Expand All @@ -2011,6 +2016,12 @@ class ChatLLM {
// this messes up a bit with spacing, so kill more than 2 newlines in a row
markdown = markdown.replace(/\n{3,}/g, '\n\n');

if (notificationText != '') {
if (display) {
display.announceText(notificationText);
}
}

try {
navigator.clipboard.writeText(markdown); // note: this fails if you're on the inspector. That's fine as it'll never happen to real users
} catch (err) {
Expand Down

0 comments on commit 326d4bf

Please sign in to comment.