修改复制的问题

This commit is contained in:
2026-07-15 10:11:53 +08:00
parent b4166ecadb
commit a9190ba4e1
5 changed files with 55 additions and 19 deletions
+4 -12
View File
@@ -1,4 +1,5 @@
import React, { useEffect, useRef, useState } from "react";
import { copyToClipboard } from "../utils/clipboard";
import { createPortal } from "react-dom";
import {
Button,
@@ -2741,10 +2742,7 @@ const GeneratePage: React.FC = () => {
type="text"
size="small"
icon={<CopyOutlined />}
onClick={() => {
navigator.clipboard.writeText(editedPrompt);
message.success("已复制");
}}
onClick={async () => { const ok = await copyToClipboard(editedPrompt); message.success(ok ? '已复制' : '复制失败'); }}
style={{ color: "#94a3b8" }}
/>
</Tooltip>
@@ -2886,10 +2884,7 @@ const GeneratePage: React.FC = () => {
type="text"
size="small"
icon={<CopyOutlined />}
onClick={() => {
navigator.clipboard.writeText(editedPrompt);
message.success("已复制");
}}
onClick={async () => { const ok = await copyToClipboard(editedPrompt); message.success(ok ? '已复制' : '复制失败'); }}
style={{ color: "#94a3b8" }}
/>
</Tooltip>
@@ -3683,10 +3678,7 @@ const GeneratePage: React.FC = () => {
type="text"
size="small"
icon={<CopyOutlined />}
onClick={() => {
navigator.clipboard.writeText(prompt);
message.success("已复制");
}}
onClick={async () => { const ok = await copyToClipboard(prompt); message.success(ok ? '已复制' : '复制失败'); }}
style={{ color: "#94a3b8" }}
/>
</Tooltip>
+2 -1
View File
@@ -33,6 +33,7 @@ import { useNavigate } from 'react-router-dom';
import { useAppStore } from '../store/useAppStore';
import type { GenerationStatus, AspectRatio, Resolution } from '../types';
import { formatDate } from '../utils/formatDate';
import { copyToClipboard } from '../utils/clipboard';
const statusConfig: Record<GenerationStatus, { color: string; text: string; icon: React.ReactNode }> = {
optimizing: { color: 'processing', text: '优化中', icon: <LoadingOutlined spin /> },
@@ -300,7 +301,7 @@ const RecordsPage: React.FC = () => {
<Space size={4}>
<Tooltip title="复制">
<Button type="text" size="small" icon={<CopyOutlined />}
onClick={() => { navigator.clipboard.writeText(prompt); message.success('已复制'); }}
onClick={async () => { const ok = await copyToClipboard(prompt); message.success(ok ? '已复制' : '复制失败'); }}
style={{ color: '#94a3b8' }} />
</Tooltip>
{record.status === 'prompt_optimized' && (
+23
View File
@@ -0,0 +1,23 @@
/** 安全复制文本到剪贴板,兼容非 HTTPS 环境 */
export async function copyToClipboard(text: string): Promise<boolean> {
try {
if (navigator.clipboard && typeof navigator.clipboard.writeText === 'function') {
await navigator.clipboard.writeText(text);
return true;
}
// 降级方案:使用 textarea + execCommand
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.left = '-9999px';
textarea.style.top = '0';
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
const succeeded = document.execCommand('copy');
document.body.removeChild(textarea);
return succeeded;
} catch {
return false;
}
}