diff --git a/apps/host/src/components/Markdown.tsx b/apps/host/src/components/Markdown.tsx index fe65886f..f10cca54 100644 --- a/apps/host/src/components/Markdown.tsx +++ b/apps/host/src/components/Markdown.tsx @@ -1,5 +1,5 @@ import { tr } from '../lib/i18n'; -import { copyAndTell } from '../lib/copy-text'; +import { copyText } from '../lib/clipboard'; /** Safe, dependency-free rendering of the Markdown structures used in answers. */ import { Fragment, type ReactNode } from 'react'; import { Btn, IconBtn } from './ui'; @@ -57,6 +57,11 @@ export function continuesParagraph(lines: string[], at: number): boolean { return false; return !displayBlock(lines, at); } +/** Copy a code block. The toast store needs a browser, so it is loaded here, when someone copies: the renderer itself must render without one. */ +async function copyCode(text: string) { + const { pushToast } = await import('../lib/store'); + await copyText(text, 'Code', { toast: pushToast }); +} export function Markdown({ text, onOpenLink, onPreviewHtml, imageSession }: { text: string; onOpenLink?: (url: string) => void; @@ -79,7 +84,7 @@ export function Markdown({ text, onOpenLink, onPreviewHtml, imageSession }: { while (i < lines.length && !/^\s*```/.test(lines[i])) body.push(lines[i++]); i++; - nodes.push(
{language || tr('code')}{tr(/^html$/i.test(language) && onPreviewHtml ? onPreviewHtml(body.join('\n'))}>{tr("Open preview")} : null)} void copyAndTell(body.join('\n'), 'Code')}/>
); + nodes.push(
{language || tr('code')}{tr(/^html$/i.test(language) && onPreviewHtml ? onPreviewHtml(body.join('\n'))}>{tr("Open preview")} : null)} void copyCode(body.join('\n'))}/>
); continue; } const block = displayBlock(lines, i); diff --git a/apps/host/src/lib/copy-text.ts b/apps/host/src/lib/copy-text.ts deleted file mode 100644 index c4881936..00000000 --- a/apps/host/src/lib/copy-text.ts +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Put text on the clipboard. The browser clipboard call can be refused inside the app window (WebView2 asks no one and says no), - * so a hidden text field and the older copy command are the second try. Resolves true when one of them worked. - */ -export async function copyText(value: string): Promise { - try { - await navigator.clipboard.writeText(value); - return true; - } - catch { - // fall through to the older route - } - try { - const field = document.createElement('textarea'); - field.value = value; - field.setAttribute('readonly', ''); - field.style.cssText = 'position:fixed;left:-9999px;top:0;opacity:0'; - document.body.appendChild(field); - field.select(); - const ok = document.execCommand('copy'); - field.remove(); - return ok; - } - catch { - return false; - } -} - -/** Copy and say so: "Copied", or "Couldn't copy" with what to do instead. */ -export async function copyAndTell(value: string, label: string): Promise { - // loaded here, not at the top: the store needs a browser, and the Markdown renderer (which copies code) is also rendered without one - const { pushToast } = await import('./store'); - if (await copyText(value)) - pushToast('success', 'Copied', label); - else - pushToast('warn', 'Couldn’t copy', 'Select the text and copy it manually.'); -} diff --git a/apps/host/src/screens/Serving.tsx b/apps/host/src/screens/Serving.tsx index e2b0ce9c..0eb86676 100644 --- a/apps/host/src/screens/Serving.tsx +++ b/apps/host/src/screens/Serving.tsx @@ -1,4 +1,4 @@ -import { copyAndTell } from '../lib/copy-text'; +import { copyText } from '../lib/clipboard'; import { tr } from '../lib/i18n'; import { appBaseUrl } from '../lib/connect-urls'; import { deviceLabel, instanceLabel } from '../lib/runtime-models'; @@ -9,7 +9,7 @@ import { useEffect, useRef, useState } from 'react'; import { Btn, Dialog, Empty, Icon, IconBtn, Seg, Select, ErrorNotice } from '../components/ui'; import { Screen, SectionHead } from '../components/layout'; import { clockTime, duration, tokens } from '../lib/format'; -import { cancelRequest, setLocalSharing, setServingAccess, useStore } from '../lib/store'; +import { cancelRequest, setLocalSharing, setServingAccess, useStore, pushToast } from '../lib/store'; export function ServingScreen() { const serving = useStore(s => s.serving); const storedRuntime = useStore(s => s.runtime), hardware = useStore(s => s.hardware); @@ -74,7 +74,7 @@ export function ServingScreen() { } } async function copyConnectionValue(value: string, label: string) { - await copyAndTell(value, label); + await copyText(value, label, { toast: pushToast }); } const code = `curl ${serving.baseUrl}${protocol === 'responses' ? '/responses' : protocol === 'openai' ? serving.endpoints.openai : serving.endpoints.anthropic} \\\n -H 'Content-Type: application/json'${serving.access.apiKeySet ? " \\\n -H 'Authorization: Bearer YOUR_API_KEY'" : ''} \\\n -d '${JSON.stringify({ model: loaded?.apiModelId ?? 'YOUR_MODEL_ID', ...(protocol === 'responses' ? { input: 'Hello', store: false } : { ...(protocol === 'anthropic' ? { max_tokens: 256 } : {}), messages: [{ role: 'user', content: 'Hello' }] }) }, null, 2)}'`; return setCloudflareOpen(true)}>{tr("Cloudflare tunnel")} setGuide(true)}/>}>