ui: try to save text with formatting in secure contexts

This commit is contained in:
nick-delirium 2025-05-07 15:08:00 +02:00
parent 20cab5b104
commit 4aa75ce14b
No known key found for this signature in database
GPG key ID: 93ABD695DF5FDBA0
2 changed files with 25 additions and 4 deletions

View file

@ -102,7 +102,7 @@ export function ChatMsg({
<IconButton tooltip="Dislike this answer" onClick={() => onFeedback('dislike', messageId)}>
<ThumbsDown size={16} />
</IconButton>
<CopyButton content={text} isIcon />
<CopyButton content={() => bodyRef.current?.innerHTML} isIcon format={'text/html'} />
<IconButton processing={isProcessing} tooltip="Export as PDF" onClick={onExport}>
<FileDown size={16} />
</IconButton>

View file

@ -10,15 +10,36 @@ function CopyButton({
btnText = 'copy',
size = 'small',
isIcon = false,
format = 'text/plain',
}) {
const [copied, setCopied] = useState(false);
const copyHandler = () => {
setCopied(true);
copy(content);
const reset = () => {
setTimeout(() => {
setCopied(false);
}, 1000);
}
const copyHandler = () => {
setCopied(true);
const contentIsGetter = typeof content === 'function'
const textContent = contentIsGetter ? content() : content;
const isHttps = window.location.protocol === 'https:';
if (!isHttps) {
copy(textContent);
reset();
return;
}
const blob = new Blob([textContent], { type: format });
const cbItem = new ClipboardItem({
[format]: blob
})
navigator.clipboard.write([cbItem])
.catch(e => {
copy(textContent);
})
.finally(() => {
reset()
})
};
if (isIcon) {