Skip to content

Commit 16ad339

Browse files
committed
feat: add copy button plugin to chat interface
- Add a copy button plugin to the chat.js file - Add CSS style for the copy button - Add a new JS file for the copy button plugin
1 parent 2b3b66c commit 16ad339

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

client/css/style.css

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,4 +619,63 @@ a:-webkit-any-link {
619619
resize: vertical;
620620
max-height: 150px;
621621
min-height: 80px;
622+
}
623+
624+
/* style for hljs copy */
625+
.hljs-copy-wrapper {
626+
position: relative;
627+
overflow: hidden
628+
}
629+
630+
.hljs-copy-wrapper:hover .hljs-copy-button,
631+
.hljs-copy-button:focus {
632+
transform: translateX(0)
633+
}
634+
635+
.hljs-copy-button {
636+
position: absolute;
637+
transform: translateX(calc(100% + 1.125em));
638+
top: 1em;
639+
right: 1em;
640+
width: 2rem;
641+
height: 2rem;
642+
text-indent: -9999px;
643+
color: #fff;
644+
border-radius: .25rem;
645+
border: 1px solid #ffffff22;
646+
background-color: #2d2b57;
647+
background-image: url('data:image/svg+xml;utf-8,<svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M6 5C5.73478 5 5.48043 5.10536 5.29289 5.29289C5.10536 5.48043 5 5.73478 5 6V20C5 20.2652 5.10536 20.5196 5.29289 20.7071C5.48043 20.8946 5.73478 21 6 21H18C18.2652 21 18.5196 20.8946 18.7071 20.7071C18.8946 20.5196 19 20.2652 19 20V6C19 5.73478 18.8946 5.48043 18.7071 5.29289C18.5196 5.10536 18.2652 5 18 5H16C15.4477 5 15 4.55228 15 4C15 3.44772 15.4477 3 16 3H18C18.7956 3 19.5587 3.31607 20.1213 3.87868C20.6839 4.44129 21 5.20435 21 6V20C21 20.7957 20.6839 21.5587 20.1213 22.1213C19.5587 22.6839 18.7957 23 18 23H6C5.20435 23 4.44129 22.6839 3.87868 22.1213C3.31607 21.5587 3 20.7957 3 20V6C3 5.20435 3.31607 4.44129 3.87868 3.87868C4.44129 3.31607 5.20435 3 6 3H8C8.55228 3 9 3.44772 9 4C9 4.55228 8.55228 5 8 5H6Z" fill="white"/><path fill-rule="evenodd" clip-rule="evenodd" d="M7 3C7 1.89543 7.89543 1 9 1H15C16.1046 1 17 1.89543 17 3V5C17 6.10457 16.1046 7 15 7H9C7.89543 7 7 6.10457 7 5V3ZM15 3H9V5H15V3Z" fill="white"/></svg>');
648+
background-repeat: no-repeat;
649+
background-position: center;
650+
transition: background-color 200ms ease, transform 200ms ease-out
651+
}
652+
653+
.hljs-copy-button:hover {
654+
border-color: #ffffff44
655+
}
656+
657+
.hljs-copy-button:active {
658+
border-color: #ffffff66
659+
}
660+
661+
.hljs-copy-button[data-copied="true"] {
662+
text-indent: 0;
663+
width: auto;
664+
background-image: none
665+
}
666+
667+
@media(prefers-reduced-motion) {
668+
.hljs-copy-button {
669+
transition: none
670+
}
671+
}
672+
673+
.hljs-copy-alert {
674+
clip: rect(0 0 0 0);
675+
clip-path: inset(50%);
676+
height: 1px;
677+
overflow: hidden;
678+
position: absolute;
679+
white-space: nowrap;
680+
width: 1px
622681
}

client/html/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
}
5757
</style>
5858
<script src="/assets/js/highlight.min.js"></script>
59+
<script src="/assets/js/highlightjs-copy.min.js"></script>
5960
<script>window.conversation_id = `{{chat_id}}`</script>
6061
<title>ChatGPT</title>
6162
</head>

client/js/chat.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const stop_generating = document.querySelector(`.stop_generating`);
88
const send_button = document.querySelector(`#send-button`);
99
let prompt_lock = false
1010

11+
hljs.addPlugin(new CopyButtonPlugin());
12+
1113
const format = (text) => {
1214
return text.replace(/(?:\r\n|\r|\n)/g, '<br>')
1315
}

client/js/highlightjs-copy.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)