add fallback for copying to clipboard over http (#1)
This commit is contained in:
parent
b45347173e
commit
8916066541
|
@ -102,16 +102,31 @@ export const ChatMessage: FC<Props> = memo(({ message, messageIndex, onEdit }) =
|
||||||
};
|
};
|
||||||
|
|
||||||
const copyOnClick = () => {
|
const copyOnClick = () => {
|
||||||
if (!navigator.clipboard) return;
|
// fallback to allow copying to clipboard over http
|
||||||
|
const copyToClipboardFallback = (text: string) => {
|
||||||
|
let textArea = document.createElement("textarea");
|
||||||
|
textArea.value = text;
|
||||||
|
textArea.style.position = "absolute";
|
||||||
|
textArea.style.opacity = "0";
|
||||||
|
document.body.appendChild(textArea);
|
||||||
|
textArea.select();
|
||||||
|
document.execCommand("copy");
|
||||||
|
textArea.remove();
|
||||||
|
};
|
||||||
|
|
||||||
|
if (navigator.clipboard && window.isSecureContext) {
|
||||||
|
navigator.clipboard.writeText(message.content);
|
||||||
|
} else {
|
||||||
|
copyToClipboardFallback(message.content);
|
||||||
|
}
|
||||||
|
|
||||||
navigator.clipboard.writeText(message.content).then(() => {
|
|
||||||
setMessageCopied(true);
|
setMessageCopied(true);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
setMessageCopied(false);
|
setMessageCopied(false);
|
||||||
}, 2000);
|
}, 2000);
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setMessageContent(message.content);
|
setMessageContent(message.content);
|
||||||
}, [message.content]);
|
}, [message.content]);
|
||||||
|
|
|
@ -20,17 +20,28 @@ export const CodeBlock: FC<Props> = memo(({ language, value }) => {
|
||||||
const [isCopied, setIsCopied] = useState<Boolean>(false);
|
const [isCopied, setIsCopied] = useState<Boolean>(false);
|
||||||
|
|
||||||
const copyToClipboard = () => {
|
const copyToClipboard = () => {
|
||||||
if (!navigator.clipboard || !navigator.clipboard.writeText) {
|
// fallback to allow copying to clipboard over http
|
||||||
return;
|
const copyToClipboardFallback = (text: string) => {
|
||||||
|
let textArea = document.createElement("textarea");
|
||||||
|
textArea.value = text;
|
||||||
|
textArea.style.position = "absolute";
|
||||||
|
textArea.style.opacity = "0";
|
||||||
|
document.body.appendChild(textArea);
|
||||||
|
textArea.select();
|
||||||
|
document.execCommand("copy");
|
||||||
|
textArea.remove();
|
||||||
|
};
|
||||||
|
|
||||||
|
if (navigator.clipboard && window.isSecureContext) {
|
||||||
|
navigator.clipboard.writeText(value);
|
||||||
|
} else {
|
||||||
|
copyToClipboardFallback(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
navigator.clipboard.writeText(value).then(() => {
|
|
||||||
setIsCopied(true);
|
setIsCopied(true);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
setIsCopied(false);
|
setIsCopied(false);
|
||||||
}, 2000);
|
}, 2000);
|
||||||
});
|
|
||||||
};
|
};
|
||||||
const downloadAsFile = () => {
|
const downloadAsFile = () => {
|
||||||
const fileExtension = programmingLanguages[language] || '.file';
|
const fileExtension = programmingLanguages[language] || '.file';
|
||||||
|
|
Loading…
Reference in New Issue