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