mirror of
https://github.com/jaywcjlove/wxmp.git
synced 2026-01-10 15:28:47 +08:00
fix: fix copy issue. #44
This commit is contained in:
@@ -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}>
|
||||||
|
|||||||
Reference in New Issue
Block a user