fix: fix copy issue. #44

This commit is contained in:
jaywcjlove
2024-08-04 23:34:33 +08:00
parent 41eb86cd2b
commit 77616468a4

View File

@@ -13,16 +13,22 @@ const Button = styled.button`
const CopyView: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & ToolBarProps }> = (props) => {
const { editorProps } = props;
const handleClick = () => {
const dom = editorProps.preview.current;
dom?.focus();
window.getSelection()?.removeAllRanges();
let range = document.createRange();
range.setStartBefore(dom?.firstChild!);
range.setEndAfter(dom?.lastChild!);
window.getSelection()?.addRange(range);
document.execCommand(`copy`);
window.getSelection()?.removeAllRanges();
toast.success(<div></div>);
const dom: HTMLDivElement | null = editorProps.preview.current;
if (!dom) {
toast.error(<div>dom is null</div>);
return;
}
dom.focus();
const htmlContent = dom.innerHTML;
navigator.clipboard
.writeText(htmlContent)
.then(() => {
toast.success(<div></div>);
})
.catch((err) => {
toast.error(<div>{JSON.stringify(err)}</div>);
console.error('Failed to copy: ', err);
});
};
return (
<Button type="button" onClick={handleClick}>