fix:writeText不可用问题
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import Icon from "../common/Icon";
|
||||
import type { StableVersion } from "../../services/api";
|
||||
import { copyToClipboard } from "../../lib/clipboard";
|
||||
|
||||
type VersionReceiptModalProps = {
|
||||
publishedVersion: StableVersion | null;
|
||||
@@ -31,10 +32,8 @@ export function VersionReceiptModal({
|
||||
<code>{publishedVersion.versions_id}</code>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
void navigator.clipboard.writeText(
|
||||
publishedVersion.versions_id,
|
||||
);
|
||||
onClick={async () => {
|
||||
await copyToClipboard(publishedVersion.versions_id);
|
||||
onCopy("versions_id 已复制");
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -13,6 +13,7 @@ import { VersionReceiptModal } from "../../components/platform/VersionReceiptMod
|
||||
import { getSessionCache, useScriptWorkspaceStore } from "./state/scriptWorkspaceStore";
|
||||
import { useUiStore } from "./state/uiStore";
|
||||
import { ScriptWorkspace } from "./ScriptWorkspace";
|
||||
import { copyToClipboard } from "../../lib/clipboard";
|
||||
|
||||
export default function ScriptsPage() {
|
||||
const { currentWorkspace, user } = useAuth();
|
||||
@@ -263,7 +264,7 @@ export default function ScriptsPage() {
|
||||
stripped = stripped.slice(fullPrefix.length);
|
||||
}
|
||||
try {
|
||||
await navigator.clipboard.writeText(stripped);
|
||||
await copyToClipboard(stripped);
|
||||
pushToast({ tone: "success", message: `已复制:${stripped}` });
|
||||
} catch {
|
||||
pushToast({ tone: "error", message: "复制失败" });
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* 复制文本到剪贴板 - 兼容 HTTP 和 HTTPS 环境
|
||||
*
|
||||
* 在 HTTPS 环境下使用现代 Clipboard API
|
||||
* 在 HTTP 环境下降级使用 execCommand 方案
|
||||
*
|
||||
* @param text - 要复制的文本
|
||||
* @returns Promise<void>
|
||||
*/
|
||||
export async function copyToClipboard(text: string): Promise<void> {
|
||||
// 优先使用现代 Clipboard API (HTTPS 环境)
|
||||
if (navigator.clipboard?.writeText) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
return;
|
||||
}
|
||||
|
||||
// 降级方案:execCommand (HTTP 环境 / 旧浏览器)
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.value = text;
|
||||
textarea.style.position = "fixed";
|
||||
textarea.style.opacity = "0";
|
||||
textarea.style.left = "-9999px";
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
|
||||
try {
|
||||
document.execCommand("copy");
|
||||
} finally {
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user