fix(copy): copyToClipboard 始终发送 postMessage

- 移除 copyToClipboard 内独立的 isInIframe 判断
- 改为始终调用 sendToParent,由 sendToParent 内部统一判断 iframe 环境
- 与 openSkillDialog 等其他 iframe 通信保持一致
This commit is contained in:
mt 2026-06-11 09:50:15 +08:00
parent 03ff3ece7f
commit 1637a0e71c

View File

@ -18,29 +18,27 @@ export const externalLinkClassNoUnderline = "text-primary hover:underline";
* In iframe context, sends message to parent window to handle clipboard operation.
*/
export async function copyToClipboard(text: string): Promise<void> {
const isInIframe = window.self !== window.top;
const message = {
type: POST_MESSAGE_TYPES.COPY_TO_CLIPBOARD,
text,
} as const;
if (isInIframe) {
console.log("[copyToClipboard] called, text length:", text.length);
// 始终发送 postMessage由 sendToParent 内部判断是否为 iframe 环境
// 与 openSkillDialog 等其他 iframe 通信保持一致
try {
// Request parent window to copy
sendToParent(message);
console.log(
"[copyToClipboard] iframe mode → postMessage to parent",
message,
);
return;
} catch (error) {
console.warn("[copyToClipboard] iframe postMessage failed", error);
}
} catch {
// no-op
}
// Direct clipboard access when not in iframe
console.log("[copyToClipboard] direct mode", message);
// 同时也尝试直接写剪贴板(非 iframe 场景兜底)
try {
await navigator.clipboard.writeText(text);
} catch {
// no-op: 在 iframe 环境下由父窗口处理
}
}
/**