Merge f-0243-copyfix into f-0243-rel

This commit is contained in:
Niko1221 committed 2026-10-24 22:13:51 +02:00
commit da1eb6bc69
3 files changed
+10 -42

No files matched your search

+7 -2
View File
@@ -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(<div className="st-code" key={key}><div className="st-code__head"><span>{language || tr('code')}</span>{tr(/^html$/i.test(language) && onPreviewHtml ? <Btn size="sm" onClick={() => onPreviewHtml(body.join('\n'))}>{tr("Open preview")}</Btn> : null)}<IconBtn name="copy" title={tr("Copy code")} onClick={() => void copyAndTell(body.join('\n'), 'Code')}/></div><SyntaxCode text={body.join('\n')} language={language}/></div>);
nodes.push(<div className="st-code" key={key}><div className="st-code__head"><span>{language || tr('code')}</span>{tr(/^html$/i.test(language) && onPreviewHtml ? <Btn size="sm" onClick={() => onPreviewHtml(body.join('\n'))}>{tr("Open preview")}</Btn> : null)}<IconBtn name="copy" title={tr("Copy code")} onClick={() => void copyCode(body.join('\n'))}/></div><SyntaxCode text={body.join('\n')} language={language}/></div>);
continue;
}
const block = displayBlock(lines, i);
-37
View File
@@ -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<boolean> {
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<void> {
// 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.');
}
+3 -3
View File
@@ -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 <Screen title={tr("Serving")} actions={<><Btn icon="link" onClick={() => setCloudflareOpen(true)}>{tr("Cloudflare tunnel")}</Btn><IconBtn name="info" title={tr("Connection guide")} onClick={() => setGuide(true)}/></>}><div className="stack stack--lg">