Skip to content

Commit 10031f9

Browse files
CopilotSoonIter
andauthored
fix(theme/llmsUI): add LLMS clipboard fallback for HTTP pages (#3237)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: SoonIter <79413249+SoonIter@users.noreply.github.com>
1 parent 57912a0 commit 10031f9

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

packages/core/src/theme/components/Llms/LlmsCopyButton.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { useCallback, useRef, useState } from 'react';
99
import { SvgWrapper } from '../SvgWrapper';
1010
import './index.scss';
1111
import './LlmsCopyButton.scss';
12+
import { copyToClipboard } from './copy';
1213
import { useMdUrl } from './useMdUrl';
1314

1415
export interface LlmsCopyButtonProps
@@ -51,7 +52,7 @@ export function LlmsCopyButton(props: LlmsCopyButtonProps) {
5152
cache.get(url) ?? (await fetch(url).then(res => res.text()));
5253

5354
cache.set(url, content);
54-
await navigator.clipboard.writeText(content);
55+
await copyToClipboard(content);
5556
} finally {
5657
setLoading(false);
5758
setFinished(true);

packages/core/src/theme/components/Llms/LlmsCopyRow.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useI18n } from '@rspress/core/runtime';
22
import { IconCopy, IconSuccess, SvgWrapper } from '@theme';
33
import { useCallback, useRef, useState } from 'react';
4+
import { copyToClipboard } from './copy';
45
import { useMdUrl } from './useMdUrl';
56

67
const cache = new Map<string, string>();
@@ -19,7 +20,7 @@ export function LlmsCopyRow() {
1920
const content: string =
2021
cache.get(pathname) ?? (await fetch(pathname).then(res => res.text()));
2122
cache.set(pathname, content);
22-
await navigator.clipboard.writeText(content);
23+
await copyToClipboard(content);
2324
} finally {
2425
setLoading(false);
2526
setFinished(true);

packages/core/src/theme/components/Llms/LlmsViewOptions.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useEffect, useMemo, useRef, useState } from 'react';
1010
import { SvgWrapper } from '../SvgWrapper';
1111
import './index.scss';
1212
import './LlmsViewOptions.scss';
13+
import { copyToClipboard } from './copy';
1314
import { useMdUrl } from './useMdUrl';
1415

1516
type Option =
@@ -98,7 +99,7 @@ export function LlmsViewOptions({
9899
title: t('copyMarkdownLinkText'),
99100
icon: <SvgWrapper icon={IconLink} />,
100101
onClick: () => {
101-
navigator.clipboard.writeText(fullMarkdownUrl);
102+
void copyToClipboard(fullMarkdownUrl);
102103
},
103104
},
104105
chatgpt: {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import copy from 'copy-to-clipboard';
2+
3+
export async function copyToClipboard(text: string) {
4+
if (navigator.clipboard?.writeText) {
5+
try {
6+
await navigator.clipboard.writeText(text);
7+
return;
8+
} catch {}
9+
}
10+
11+
copy(text);
12+
}

0 commit comments

Comments
 (0)