diff --git a/src/_util/copyToClipboard.ts b/src/_util/copyToClipboard.ts new file mode 100644 index 00000000..e0bde7a3 --- /dev/null +++ b/src/_util/copyToClipboard.ts @@ -0,0 +1,18 @@ +export function copy(text: string) { + let success: boolean; + const textarea = document.createElement('textarea'); + textarea.value = text; + textarea.style.position = 'fixed'; + textarea.style.opacity = '0'; + document.body.appendChild(textarea); + textarea.select(); + try { + document.execCommand('copy'); + success = true; + } catch (e) { + console.error('fallback 复制失败', e); + success = false; + } + document.body.removeChild(textarea); + return success; +} diff --git a/src/chat-action/action.tsx b/src/chat-action/action.tsx index 219b6230..9648bb45 100644 --- a/src/chat-action/action.tsx +++ b/src/chat-action/action.tsx @@ -10,6 +10,7 @@ import '../../src/tooltip'; import { Component, signal, tag } from 'omi'; import { getClassPrefix } from '../_util/classname'; +import { copy as fallbackCopy } from '../_util/copyToClipboard'; import { setExportparts } from '../_util/dom'; import { type ChatComment } from '../chat-engine'; import { MessagePlugin } from '../message'; @@ -30,14 +31,20 @@ export const renderActions = ( const clickCopyHandler = () => { const text = copyText.toString(); if (!text) return; - navigator.clipboard - .writeText(copyText.toString()) - .then(() => { - MessagePlugin.success('复制成功'); - }) - .catch(() => { - MessagePlugin.success('复制失败,请手动复制'); - }); + if (navigator.clipboard) { + navigator.clipboard + .writeText(copyText.toString()) + .then(() => { + MessagePlugin.success('复制成功'); + }) + .catch(() => { + MessagePlugin.success('复制失败,请手动复制'); + }); + } else { + const success = fallbackCopy(text); + if (success) MessagePlugin.success('复制成功'); + else MessagePlugin.error('复制失败,请手动复制'); + } }; const handleClickAction = (action: TdChatActionsName, data: any) => { diff --git a/src/collapse/collapse-panel.tsx b/src/collapse/collapse-panel.tsx index 36273bec..3aba10a5 100644 --- a/src/collapse/collapse-panel.tsx +++ b/src/collapse/collapse-panel.tsx @@ -156,7 +156,7 @@ export default class CollapsePanel extends Component { } renderBody() { - const isActive = this.injection.getCollapseValue().includes(this.innerValue.value); + const isActive = this.injection.getCollapseValue?.().includes(this.innerValue.value); const { destroyOnCollapse } = this.props; if (this.afterLeaved.value === null && !isActive) { return null;