From 5faf357b4933e9c4ddff96fb1d925f0874f9cdad Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 24 Oct 2026 13:33:17 +0200 Subject: [PATCH] Chat: Copy response/message/code say Copied or Couldn't copy and fall back to the older copy route (#3731); a pasted screenshot is also read from clipboard items (#3748); the user bubble keeps its line breaks (#3720) --- apps/host/src/components/DropZone.tsx | 9 ++++++- apps/host/src/components/Markdown.tsx | 3 ++- apps/host/src/lib/copy-text.ts | 37 +++++++++++++++++++++++++++ apps/host/src/screens/Chat.tsx | 7 ++--- apps/host/src/screens/Serving.tsx | 11 +++----- apps/host/src/styles/app.css | 2 ++ 6 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 apps/host/src/lib/copy-text.ts diff --git a/apps/host/src/components/DropZone.tsx b/apps/host/src/components/DropZone.tsx index c0b1f976..cecf20f9 100644 --- a/apps/host/src/components/DropZone.tsx +++ b/apps/host/src/components/DropZone.tsx @@ -9,7 +9,14 @@ export const DROP_EVENT = 'strata:drop-files'; /** The pictures on the clipboard, named so each one is told apart in the composer (a pasted screenshot arrives as "image.png"). */ export function pastedFiles(data: DataTransfer | null | undefined): File[] { const out: File[] = []; - for (const file of Array.from(data?.files ?? [])) { + // `files` is empty for some screenshot tools (Win+Shift+S in WebView2): the same picture is then only in `items`. + const found = Array.from(data?.files ?? []); + if (!found.some(f => f.type.startsWith('image/'))) + for (const item of Array.from(data?.items ?? [])) { + const file = item.kind === 'file' && item.type.startsWith('image/') ? item.getAsFile() : null; + if (file) found.push(file); + } + for (const file of found) { if (!file.type.startsWith('image/')) continue; const extension = file.type.split('/')[1]?.replace('jpeg', 'jpg').replace(/[+].*/, '') || 'png'; out.push(new File([file], `Pasted picture ${new Date().toLocaleTimeString().replace(/[^0-9]+/g, '-').replace(/-$/, '')}${out.length ? `-${out.length + 1}` : ''}.${extension}`, { type: file.type })); diff --git a/apps/host/src/components/Markdown.tsx b/apps/host/src/components/Markdown.tsx index 996b191f..e771dccd 100644 --- a/apps/host/src/components/Markdown.tsx +++ b/apps/host/src/components/Markdown.tsx @@ -1,4 +1,5 @@ import { tr } from '../lib/i18n'; +import { copyAndTell } from '../lib/copy-text'; /** Safe, dependency-free rendering of the Markdown structures used in answers. */ import { Fragment, type ReactNode } from 'react'; import { Btn, IconBtn } from './ui'; @@ -69,7 +70,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 navigator.clipboard.writeText(body.join('\n'))}/>
); + 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')}/>
); continue; } const block = displayBlock(lines, i); diff --git a/apps/host/src/lib/copy-text.ts b/apps/host/src/lib/copy-text.ts new file mode 100644 index 00000000..243f3139 --- /dev/null +++ b/apps/host/src/lib/copy-text.ts @@ -0,0 +1,37 @@ +import { pushToast } from './store'; + +/** + * 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 { + 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/Chat.tsx b/apps/host/src/screens/Chat.tsx index 286fb723..44026e0c 100644 --- a/apps/host/src/screens/Chat.tsx +++ b/apps/host/src/screens/Chat.tsx @@ -41,6 +41,7 @@ import { relTime, cx, duration, tokens, tps } from '../lib/format'; import { createSubagentExample, pushToast, setPreviewTabClosed, branchFrom, continueLast, markRead, resolveTool, retryLast, selectSession, rewindTo, sendMessage, stopStreaming, useStore, } from '../lib/store'; import { getSlashCommands } from '../lib/commands'; import { useLocation } from '../lib/router'; +import { copyAndTell } from '../lib/copy-text'; import { composerSends } from '../lib/shortcut-bindings'; import type { ChatMessage, Session, ToolCall } from '../lib/types'; export function ChatScreen() { @@ -620,7 +621,7 @@ function Message({ message: m, isLast, onEditPrompt, onPreview, onOpenLink, onOp
- void navigator.clipboard.writeText(m.content)}/> + void copyAndTell(m.content, 'Message')}/> { const prompt = rewindTo(m.id); if (prompt !== null) @@ -659,11 +660,11 @@ function Message({ message: m, isLast, onEditPrompt, onPreview, onOpenLink, onOp {tr(!m.streaming && !m.timeSkipId && (m.content || m.error) ? (
{tr(isLast ? <> - void navigator.clipboard.writeText(m.content)}/> + void copyAndTell(m.content, 'Message')}/> void retryLast(m.id)}/> : null)} - {tr(!isLast ? <> : null)} + {tr(!isLast ? <> : null)} {tr(isLast ? : null)} {tr(m.tokens || m.tps || m.durationMs ?

{tr(m.tokens ? `${tokens(m.tokens)} tokens` : '')}{tr(m.tps ? ` · ${tps(m.tps)} tok/s` : '')}{tr(m.durationMs ? ` · ${duration(m.durationMs)}` : '')}{tr(m.demo ? ' · Example' : '')}

: null)} diff --git a/apps/host/src/screens/Serving.tsx b/apps/host/src/screens/Serving.tsx index ebe6609a..e2b0ce9c 100644 --- a/apps/host/src/screens/Serving.tsx +++ b/apps/host/src/screens/Serving.tsx @@ -1,3 +1,4 @@ +import { copyAndTell } from '../lib/copy-text'; import { tr } from '../lib/i18n'; import { appBaseUrl } from '../lib/connect-urls'; import { deviceLabel, instanceLabel } from '../lib/runtime-models'; @@ -8,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, pushToast } from '../lib/store'; +import { cancelRequest, setLocalSharing, setServingAccess, useStore } from '../lib/store'; export function ServingScreen() { const serving = useStore(s => s.serving); const storedRuntime = useStore(s => s.runtime), hardware = useStore(s => s.hardware); @@ -73,13 +74,7 @@ export function ServingScreen() { } } async function copyConnectionValue(value: string, label: string) { - try { - await navigator.clipboard.writeText(value); - pushToast('success', 'Copied', label); - } - catch { - pushToast('warn', 'Couldn’t copy', 'Select the value and copy it manually.'); - } + await copyAndTell(value, label); } 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)}/>}>
diff --git a/apps/host/src/styles/app.css b/apps/host/src/styles/app.css index bb27e246..6531816b 100644 --- a/apps/host/src/styles/app.css +++ b/apps/host/src/styles/app.css @@ -4083,6 +4083,8 @@ textarea.st-input { .st-msg--user .st-bubble { background: var(--st-accent-surface); color: var(--st-accent-surface-ink); + white-space: pre-wrap; /* a typed or pasted message keeps its line breaks (#3720) */ + overflow-wrap: anywhere; } /* October 2026 clarity pass. App-specific extensions survive design-kit sync. */