Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat(gfs): copy button + plain-text preview for image and text previews

Miguelon Peluchon65b4836708ad
diff --git a/control-ui/app/constants/gfsMarkdownPreview.ts b/control-ui/app/constants/gfsMarkdownPreview.ts
index 6927c0292..0fe3b143b 100644
--- a/control-ui/app/constants/gfsMarkdownPreview.ts
+++ b/control-ui/app/constants/gfsMarkdownPreview.ts
@@ -1,2 +1,6 @@
-export const GFS_MARKDOWN_PREVIEW_EXTENSIONS: ReadonlySet<string> = new Set(['md', 'markdown'])
+export const GFS_MARKDOWN_PREVIEW_EXTENSIONS: ReadonlySet<string> = new Set([
+ 'md',
+ 'markdown',
+ 'txt',
+])
export const GFS_MARKDOWN_PREVIEW_MAX_BYTES = 2 * 1024 * 1024
diff --git a/control-ui/app/globals.css b/control-ui/app/globals.css
index e332875a5..a2e1bc252 100644
--- a/control-ui/app/globals.css
+++ b/control-ui/app/globals.css
@@ -11624,6 +11624,56 @@ button.cu-gfs-tree__button:focus-visible,
border-radius: 999px;
}
+.cu-gfs-image-preview-dialog__header-actions,
+.cu-gfs-markdown-preview-dialog__header-actions,
+.cu-gfs-video-preview-dialog__header-actions {
+ display: inline-flex;
+ align-items: center;
+ gap: var(--cu-space-1);
+}
+
+.cu-gfs-image-preview-dialog__copy.cu-btn,
+.cu-gfs-markdown-preview-dialog__copy.cu-btn,
+.cu-gfs-video-preview-dialog__copy.cu-btn {
+ height: 2.25rem;
+ padding-inline: var(--cu-space-3);
+ border-radius: 999px;
+ color: var(--cu-text-soft);
+}
+
+.cu-gfs-image-preview-dialog__copy.cu-btn:hover:not(:disabled),
+.cu-gfs-markdown-preview-dialog__copy.cu-btn:hover:not(:disabled),
+.cu-gfs-video-preview-dialog__copy.cu-btn:hover:not(:disabled) {
+ color: var(--cu-accent);
+}
+
+.cu-gfs-preview-button__label {
+ font-size: 0.8125rem;
+ font-weight: 600;
+}
+
+.cu-gfs-text-preview__content {
+ min-width: 0;
+ min-height: 0;
+ padding: var(--cu-space-5);
+}
+
+.cu-gfs-text-preview__body {
+ max-width: 60rem;
+ margin: 0;
+ padding: var(--cu-space-4);
+ border: 1px solid var(--cu-border-subtle);
+ border-radius: var(--cu-radius-sm);
+ background: var(--cu-bg);
+ color: var(--cu-text);
+ font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
+ font-size: 0.8125rem;
+ line-height: 1.5;
+ overflow: auto;
+ white-space: pre-wrap;
+ word-break: break-word;
+}
+
.cu-gfs-image-preview-dialog__body,
.cu-gfs-video-preview-dialog__body {
min-width: 0;
diff --git a/control-ui/components/GfsImagePreview/index.tsx b/control-ui/components/GfsImagePreview/index.tsx
index ef37a5392..5ebca802d 100644
--- a/control-ui/components/GfsImagePreview/index.tsx
+++ b/control-ui/components/GfsImagePreview/index.tsx
@@ -2,6 +2,8 @@
import { useEffect, useId, useRef, useState } from 'react'
import { createPortal } from 'react-dom'
+import { IconCopy } from '@components/Sidebar/icons'
+import { useToast } from '@components/Toast'
import { IconX } from '@components/icons'
import { Button } from '@components/ui'
import { gfsFetchFileBlob } from '@lib/api'
@@ -18,7 +20,11 @@ export function GfsImagePreview({
const titleId = useId()
const dialogRef = useRef<HTMLElement | null>(null)
const [previewUrl, setPreviewUrl] = useState<string | null>(null)
+ const [sourceBlob, setSourceBlob] = useState<Blob | null>(null)
const [previewError, setPreviewError] = useState<string | null>(null)
+ const [copyState, setCopyState] = useState<'idle' | 'copied' | 'error'>('idle')
+ const copyResetTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
+ const { showToast } = useToast()
useEffect(() => {
const previouslyFocused = document.activeElement
@@ -42,10 +48,12 @@ export function GfsImagePreview({
async function loadPreview(): Promise<void> {
try {
assertGfsImagePreviewSize(byteLength)
- const sourceBlob = await gfsFetchFileBlob(rid)
- assertGfsImagePreviewSize(sourceBlob.size)
+ const fetched = await gfsFetchFileBlob(rid)
+ assertGfsImagePreviewSize(fetched.size)
if (!active) return
- objectUrl = URL.createObjectURL(new Blob([sourceBlob], { type: mimeType }))
+ const blob = new Blob([fetched], { type: mimeType })
+ setSourceBlob(blob)
+ objectUrl = URL.createObjectURL(blob)
setPreviewUrl(objectUrl)
} catch (error) {
if (!active) return
@@ -60,6 +68,45 @@ export function GfsImagePreview({
}
}, [byteLength, mimeType, rid])
+ useEffect(() => {
+ return () => {
+ if (copyResetTimeoutRef.current) clearTimeout(copyResetTimeoutRef.current)
+ }
+ }, [])
+
+ async function copyImageToClipboard(): Promise<void> {
+ if (!sourceBlob) return
+ try {
+ if (typeof ClipboardItem !== 'undefined' && navigator.clipboard?.write) {
+ let clipboardBlob: Blob | null = sourceBlob
+ if (!sourceBlob.type.includes('png')) {
+ clipboardBlob = await convertBlobToPng(sourceBlob)
+ }
+ if (clipboardBlob) {
+ await navigator.clipboard.write([new ClipboardItem({ 'image/png': clipboardBlob })])
+ setCopyState('copied')
+ showToast(`Copied ${fileName} to the clipboard.`, { tone: 'success' })
+ if (copyResetTimeoutRef.current) clearTimeout(copyResetTimeoutRef.current)
+ copyResetTimeoutRef.current = setTimeout(() => setCopyState('idle'), 2000)
+ return
+ }
+ }
+ if (navigator.clipboard?.writeText && previewUrl) {
+ await navigator.clipboard.writeText(previewUrl)
+ setCopyState('copied')
+ showToast(`Copied a ${fileName} link to the clipboard.`, { tone: 'success' })
+ } else {
+ setCopyState('error')
+ showToast('Copy failed — check browser clipboard permissions.', { tone: 'error' })
+ }
+ } catch {
+ setCopyState('error')
+ showToast('Copy failed — check browser clipboard permissions.', { tone: 'error' })
+ }
+ if (copyResetTimeoutRef.current) clearTimeout(copyResetTimeoutRef.current)
+ copyResetTimeoutRef.current = setTimeout(() => setCopyState('idle'), 2000)
+ }
+
return createPortal(
<div
className="cu-modal-backdrop cu-gfs-image-preview-modal"
@@ -77,15 +124,31 @@ export function GfsImagePreview({
>
<header className="cu-gfs-image-preview-dialog__header">
<h3 id={titleId}>{fileName}</h3>
- <Button
- className="cu-gfs-image-preview-dialog__close"
- data-preview-close
- variant="ghost"
- aria-label="Close image preview"
- onClick={onClose}
- >
- <IconX width={18} height={18} />
- </Button>
+ <div className="cu-gfs-image-preview-dialog__header-actions">
+ <Button
+ className="cu-gfs-image-preview-dialog__copy"
+ variant="ghost"
+ aria-label={
+ copyState === 'copied' ? 'Copied image to clipboard' : 'Copy image to clipboard'
+ }
+ disabled={!sourceBlob}
+ onClick={() => void copyImageToClipboard()}
+ >
+ <IconCopy width={18} height={18} />
+ <span className="cu-gfs-preview-button__label">
+ {copyState === 'copied' ? 'Copied' : 'Copy'}
+ </span>
+ </Button>
+ <Button
+ className="cu-gfs-image-preview-dialog__close"
+ data-preview-close
+ variant="ghost"
+ aria-label="Close image preview"
+ onClick={onClose}
+ >
+ <IconX width={18} height={18} />
+ </Button>
+ </div>
</header>
<div className="cu-gfs-image-preview-dialog__body">
{previewError ? (
@@ -113,4 +176,20 @@ export function GfsImagePreview({
)
}
+async function convertBlobToPng(blob: Blob): Promise<Blob | null> {
+ if (typeof createImageBitmap === 'undefined') return null
+ try {
+ const bitmap = await createImageBitmap(blob)
+ const canvas = document.createElement('canvas')
+ canvas.width = bitmap.width
+ canvas.height = bitmap.height
+ const ctx = canvas.getContext('2d')
+ if (!ctx) return null
+ ctx.drawImage(bitmap, 0, 0)
+ return await new Promise<Blob | null>(resolve => canvas.toBlob(resolve, 'image/png'))
+ } catch {
+ return null
+ }
+}
+
export type { GfsImagePreviewProps } from './types'
diff --git a/control-ui/components/GfsMarkdownPreview/index.tsx b/control-ui/components/GfsMarkdownPreview/index.tsx
index 56c62bb47..6ae45b8c1 100644
--- a/control-ui/components/GfsMarkdownPreview/index.tsx
+++ b/control-ui/components/GfsMarkdownPreview/index.tsx
@@ -3,6 +3,8 @@
import { Fragment, useEffect, useId, useMemo, useRef, useState } from 'react'
import type { ReactNode } from 'react'
import { createPortal } from 'react-dom'
+import { IconCopy } from '@components/Sidebar/icons'
+import { useToast } from '@components/Toast'
import { IconX } from '@components/icons'
import { Button } from '@components/ui'
import { gfsFetchFileBlob } from '@lib/api'
@@ -59,6 +61,10 @@ function renderBlock(block: MarkdownBlock): ReactNode {
return block.ordered ? <ol key={block.id}>{items}</ol> : <ul key={block.id}>{items}</ul>
}
+function isPlainTextName(fileName: string): boolean {
+ return fileName.toLowerCase().endsWith('.txt')
+}
+
export function GfsMarkdownPreview({
byteLength,
fileName,
@@ -69,7 +75,17 @@ export function GfsMarkdownPreview({
const dialogRef = useRef<HTMLElement | null>(null)
const [source, setSource] = useState<string | null>(null)
const [previewError, setPreviewError] = useState<string | null>(null)
+ const [copyState, setCopyState] = useState<'idle' | 'copied' | 'error'>('idle')
+ const copyResetTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
+ const { showToast } = useToast()
const blocks = useMemo(() => (source === null ? [] : parseVanillaMarkdown(source)), [source])
+ const isPlainText = isPlainTextName(fileName)
+
+ useEffect(() => {
+ return () => {
+ if (copyResetTimeoutRef.current) clearTimeout(copyResetTimeoutRef.current)
+ }
+ }, [])
useEffect(() => {
const previouslyFocused = document.activeElement
@@ -110,6 +126,25 @@ export function GfsMarkdownPreview({
}
}, [byteLength, rid])
+ async function copySourceToClipboard(): Promise<void> {
+ if (source === null) return
+ if (!navigator.clipboard?.writeText) {
+ showToast('Clipboard is not available in this browser.', { tone: 'error' })
+ setCopyState('error')
+ return
+ }
+ try {
+ await navigator.clipboard.writeText(source)
+ setCopyState('copied')
+ showToast(`Copied ${fileName} to the clipboard.`, { tone: 'success' })
+ } catch {
+ setCopyState('error')
+ showToast('Copy failed — check browser clipboard permissions.', { tone: 'error' })
+ }
+ if (copyResetTimeoutRef.current) clearTimeout(copyResetTimeoutRef.current)
+ copyResetTimeoutRef.current = setTimeout(() => setCopyState('idle'), 2000)
+ }
+
return createPortal(
<div
className="cu-modal-backdrop cu-gfs-markdown-preview-modal"
@@ -127,15 +162,33 @@ export function GfsMarkdownPreview({
>
<header className="cu-gfs-markdown-preview-dialog__header">
<h3 id={titleId}>{fileName}</h3>
- <Button
- className="cu-gfs-markdown-preview-dialog__close"
- data-preview-close
- variant="ghost"
- aria-label="Close Markdown preview"
- onClick={onClose}
- >
- <IconX width={18} height={18} />
- </Button>
+ <div className="cu-gfs-markdown-preview-dialog__header-actions">
+ <Button
+ className="cu-gfs-markdown-preview-dialog__copy"
+ variant="ghost"
+ aria-label={
+ copyState === 'copied'
+ ? 'Copied preview contents to clipboard'
+ : 'Copy preview contents to clipboard'
+ }
+ disabled={source === null}
+ onClick={() => void copySourceToClipboard()}
+ >
+ <IconCopy width={18} height={18} />
+ <span className="cu-gfs-preview-button__label">
+ {copyState === 'copied' ? 'Copied' : 'Copy'}
+ </span>
+ </Button>
+ <Button
+ className="cu-gfs-markdown-preview-dialog__close"
+ data-preview-close
+ variant="ghost"
+ aria-label="Close preview"
+ onClick={onClose}
+ >
+ <IconX width={18} height={18} />
+ </Button>
+ </div>
</header>
<div className="cu-gfs-markdown-preview-dialog__body">
{previewError ? (
@@ -145,20 +198,33 @@ export function GfsMarkdownPreview({
) : null}
{!previewError && source === null ? (
<div className="cu-gfs-markdown-preview-dialog__loading" role="status">
- Loading Markdown preview…
+ Loading preview…
</div>
) : null}
{source !== null && !previewError ? (
- <article
- aria-label={`Markdown preview of ${fileName}`}
- className="cu-gfs-markdown-preview__content"
- >
- {blocks.length > 0 ? (
- blocks.map(renderBlock)
- ) : (
- <p className="cu-gfs-markdown-preview__empty">This Markdown file is empty.</p>
- )}
- </article>
+ isPlainText ? (
+ <article
+ aria-label={`Text preview of ${fileName}`}
+ className="cu-gfs-text-preview__content"
+ >
+ {source.length > 0 ? (
+ <pre className="cu-gfs-text-preview__body">{source}</pre>
+ ) : (
+ <p className="cu-gfs-markdown-preview__empty">This text file is empty.</p>
+ )}
+ </article>
+ ) : (
+ <article
+ aria-label={`Markdown preview of ${fileName}`}
+ className="cu-gfs-markdown-preview__content"
+ >
+ {blocks.length > 0 ? (
+ blocks.map(renderBlock)
+ ) : (
+ <p className="cu-gfs-markdown-preview__empty">This Markdown file is empty.</p>
+ )}
+ </article>
+ )
) : null}
</div>
</section>
diff --git a/control-ui/components/Sidebar/icons.tsx b/control-ui/components/Sidebar/icons.tsx
index e99e485b2..451de80a9 100644
--- a/control-ui/components/Sidebar/icons.tsx
+++ b/control-ui/components/Sidebar/icons.tsx
@@ -1,5 +1,20 @@
import React from 'react'
+export function IconCopy() {
+ return (
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ width="18"
+ height="18"
+ viewBox="0 0 384 384"
+ fill="currentColor"
+ aria-hidden="true"
+ >
+ <path d="M280 64H112c-8.8 0-16 7.2-16 16v32c0 8.8 7.2 16 16 16h168c8.8 0 16-7.2 16-16V80c0-8.8-7.2-16-16-16zM32 224c0-35.3 28.7-64 64-64h192c35.3 0 64 28.7 64 64v128c0 35.3-28.7 64-64 64H96c-35.3 0-64-28.7-64-64V224zm128 48c-8.8 0-16 7.2-16 16 0 8.8 7.2 16 16 16h128c8.8 0 16-7.2 16-16 0-8.8-7.2-16-16-16H160z" />
+ </svg>
+ )
+}
+
export function IconWorkflow() {
return (
<svg
diff --git a/control-ui/components/__tests__/GfsBrowser.test.tsx b/control-ui/components/__tests__/GfsBrowser.test.tsx
index eb06e138b..2530f77f9 100644
--- a/control-ui/components/__tests__/GfsBrowser.test.tsx
+++ b/control-ui/components/__tests__/GfsBrowser.test.tsx
@@ -492,14 +492,14 @@ describe('GfsBrowser', () => {
it('previews supported image files from the file name and resource menu', async () => {
mockApiGet.mockResolvedValueOnce({
- items: [child('avatar.PNG', 'file', 2), child('notes.txt', 'file', 3)],
+ items: [child('avatar.PNG', 'file', 2), child('notes.bin', 'file', 3)],
nextCursor: null,
})
mockGfsFetchFileBlob.mockResolvedValue(new Blob(['image bytes']))
renderBrowser()
const previewTrigger = await screen.findByRole('button', { name: 'avatar.PNG' })
- expect(screen.getByText('notes.txt').closest('button')).toBeNull()
+ expect(screen.getByText('notes.bin').closest('button')).toBeNull()
fireEvent.click(previewTrigger)
const dialog = await screen.findByRole('dialog', { name: 'avatar.PNG' })
@@ -509,7 +509,8 @@ describe('GfsBrowser', () => {
expect(mockGfsFetchFileBlob).toHaveBeenCalledWith('r2')
expect((mockCreateObjectUrl.mock.calls[0]?.[0] as Blob).type).toBe('image/png')
- fireEvent.click(within(dialog).getByRole('button', { name: 'Close image preview' }))
+ const closeButton = within(dialog).getByRole('button', { name: /close image preview/i })
+ fireEvent.click(closeButton)
await waitFor(() => expect(screen.queryByRole('dialog', { name: 'avatar.PNG' })).toBeNull())
expect(mockRevokeObjectUrl).toHaveBeenCalledWith('blob:gfs-image-preview')
@@ -519,7 +520,7 @@ describe('GfsBrowser', () => {
fireEvent.keyDown(window, { key: 'Escape' })
await waitFor(() => expect(screen.queryByRole('dialog', { name: 'avatar.PNG' })).toBeNull())
- await openResourceMenu('notes.txt')
+ await openResourceMenu('notes.bin')
expect(screen.queryByRole('menuitem', { name: 'Preview' })).toBeNull()
})
@@ -551,6 +552,51 @@ describe('GfsBrowser', () => {
expect(mockGfsFetchFileBlob).not.toHaveBeenCalled()
})
+ it('copies markdown source to the clipboard via the preview header button', async () => {
+ const writeText = vi.fn(async () => undefined)
+ Object.defineProperty(navigator, 'clipboard', {
+ configurable: true,
+ value: { ...navigator.clipboard, writeText },
+ })
+
+ mockApiGet.mockResolvedValueOnce({
+ items: [child('README.md', 'file', 9)],
+ nextCursor: null,
+ })
+ mockGfsFetchFileBlob.mockResolvedValueOnce(
+ new Blob(['# Hello\n\nGreetings.'], { type: 'text/markdown' })
+ )
+
+ renderBrowser()
+ fireEvent.click(await screen.findByRole('button', { name: 'README.md' }))
+ const dialog = await screen.findByRole('dialog', { name: 'README.md' })
+
+ const copyButton = within(dialog).getByRole('button', {
+ name: /Copy preview contents to clipboard/i,
+ })
+ fireEvent.click(copyButton)
+
+ await waitFor(() => expect(writeText).toHaveBeenCalledWith('# Hello\n\nGreetings.'))
+ })
+
+ it('renders a .txt file as plain text inside the preview dialog', async () => {
+ mockApiGet.mockResolvedValueOnce({
+ items: [child('notes.txt', 'file', 11)],
+ nextCursor: null,
+ })
+ mockGfsFetchFileBlob.mockResolvedValueOnce(
+ new Blob(['line one\nline two\twith tab'], { type: 'text/plain' })
+ )
+
+ renderBrowser()
+ fireEvent.click(await screen.findByRole('button', { name: 'notes.txt' }))
+
+ const dialog = await screen.findByRole('dialog', { name: 'notes.txt' })
+ const pre = await within(dialog).findByText(/line one/)
+ expect(pre.tagName).toBe('PRE')
+ expect(pre.textContent).toContain('line two\twith tab')
+ })
+
it('previews supported video files in a closable HTML5 video dialog', async () => {
mockApiGet.mockResolvedValueOnce({
items: [child('demo.mp4', 'file', 7)],
@@ -602,7 +648,7 @@ describe('GfsBrowser', () => {
expect(dialog.querySelector('script')).toBeNull()
expect(mockGfsFetchFileBlob).toHaveBeenCalledWith('r5')
- fireEvent.click(within(dialog).getByRole('button', { name: 'Close Markdown preview' }))
+ fireEvent.click(within(dialog).getByRole('button', { name: /^close preview$/i }))
await waitFor(() => expect(screen.queryByRole('dialog', { name: 'README.md' })).toBeNull())
await openResourceMenu('README.md')
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index cc84d59f4..7c0904573 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.439",
+ "version": "0.1.440",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.439",
+ "version": "0.1.440",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/llm-providers": "file:../packages/llm-providers",
diff --git a/control-ui/package.json b/control-ui/package.json
index 2a75f5b65..163a5a324 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.439",
+ "version": "0.1.440",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",
diff --git a/desktop-app/ui/src/components/GfsImagePreview/index.tsx b/desktop-app/ui/src/components/GfsImagePreview/index.tsx
index 57e0b9d1c..d673dbb51 100644
--- a/desktop-app/ui/src/components/GfsImagePreview/index.tsx
+++ b/desktop-app/ui/src/components/GfsImagePreview/index.tsx
@@ -1,7 +1,7 @@
import { useEffect, useId, useRef, useState } from 'react'
import { createPortal } from 'react-dom'
import { IconButton, StatusBanner } from '@components/Common'
-import { IconClose } from '@components/SidebarNav/icons'
+import { IconClose, IconCopy } from '@components/SidebarNav/icons'
import { assertGfsImagePreviewSize } from '@lib/gfsImagePreview'
import type { GfsImagePreviewProps } from './types'
@@ -16,7 +16,10 @@ export function GfsImagePreview({
const titleId = useId()
const closeButtonRef = useRef<HTMLButtonElement | null>(null)
const [previewUrl, setPreviewUrl] = useState<string | null>(null)
+ const [sourceBlob, setSourceBlob] = useState<Blob | null>(null)
const [previewError, setPreviewError] = useState<string | null>(null)
+ const [copyState, setCopyState] = useState<'idle' | 'copied' | 'error'>('idle')
+ const copyResetTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
useEffect(() => {
closeButtonRef.current?.focus()
@@ -37,7 +40,9 @@ export function GfsImagePreview({
const { bytes } = await window.clerum.gfs.download(gfsUri)
assertGfsImagePreviewSize(bytes.byteLength)
if (!active) return
- objectUrl = URL.createObjectURL(new Blob([bytes], { type: mimeType }))
+ const blob = new Blob([bytes], { type: mimeType })
+ setSourceBlob(blob)
+ objectUrl = URL.createObjectURL(blob)
setPreviewUrl(objectUrl)
} catch (error) {
if (!active) return
@@ -53,6 +58,42 @@ export function GfsImagePreview({
}
}, [byteLength, gfsUri, mimeType, onDownloadError])
+ useEffect(() => {
+ return () => {
+ if (copyResetTimeoutRef.current) clearTimeout(copyResetTimeoutRef.current)
+ }
+ }, [])
+
+ async function copyImageToClipboard(): Promise<void> {
+ if (!sourceBlob) return
+ try {
+ if (typeof ClipboardItem !== 'undefined' && navigator.clipboard?.write) {
+ let clipboardBlob: Blob | null = sourceBlob
+ if (!sourceBlob.type.includes('png')) {
+ clipboardBlob = await convertBlobToPng(sourceBlob)
+ }
+ if (clipboardBlob) {
+ await navigator.clipboard.write([new ClipboardItem({ 'image/png': clipboardBlob })])
+ setCopyState('copied')
+ } else if (navigator.clipboard?.writeText && previewUrl) {
+ await navigator.clipboard.writeText(previewUrl)
+ setCopyState('copied')
+ } else {
+ setCopyState('error')
+ }
+ } else if (navigator.clipboard?.writeText && previewUrl) {
+ await navigator.clipboard.writeText(previewUrl)
+ setCopyState('copied')
+ } else {
+ setCopyState('error')
+ }
+ } catch {
+ setCopyState('error')
+ }
+ if (copyResetTimeoutRef.current) clearTimeout(copyResetTimeoutRef.current)
+ copyResetTimeoutRef.current = setTimeout(() => setCopyState('idle'), 2000)
+ }
+
return createPortal(
<div
className="da-gfs-image-preview-modal"
@@ -69,15 +110,28 @@ export function GfsImagePreview({
>
<header className="da-gfs-image-preview-dialog__header">
<h3 id={titleId}>{fileName}</h3>
- <IconButton
- ref={closeButtonRef}
- label="Close image preview"
- onClick={onClose}
- size="sm"
- variant="ghost"
- >
- <IconClose />
- </IconButton>
+ <div className="da-gfs-image-preview-dialog__header-actions">
+ <IconButton
+ label={
+ copyState === 'copied' ? 'Copied image to clipboard' : 'Copy image to clipboard'
+ }
+ disabled={!sourceBlob}
+ onClick={() => void copyImageToClipboard()}
+ size="sm"
+ variant="ghost"
+ >
+ <IconCopy />
+ </IconButton>
+ <IconButton
+ ref={closeButtonRef}
+ label="Close image preview"
+ onClick={onClose}
+ size="sm"
+ variant="ghost"
+ >
+ <IconClose />
+ </IconButton>
+ </div>
</header>
<div className="da-gfs-image-preview-dialog__body">
{previewError ? <StatusBanner tone="error" text={previewError} /> : null}
@@ -101,4 +155,20 @@ export function GfsImagePreview({
)
}
+async function convertBlobToPng(blob: Blob): Promise<Blob | null> {
+ if (typeof createImageBitmap === 'undefined') return null
+ try {
+ const bitmap = await createImageBitmap(blob)
+ const canvas = document.createElement('canvas')
+ canvas.width = bitmap.width
+ canvas.height = bitmap.height
+ const ctx = canvas.getContext('2d')
+ if (!ctx) return null
+ ctx.drawImage(bitmap, 0, 0)
+ return await new Promise<Blob | null>(resolve => canvas.toBlob(resolve, 'image/png'))
+ } catch {
+ return null
+ }
+}
+
export type { GfsImagePreviewProps } from './types'
diff --git a/desktop-app/ui/src/components/GfsMarkdownPreview/index.tsx b/desktop-app/ui/src/components/GfsMarkdownPreview/index.tsx
index e27c37b60..8839ffc19 100644
--- a/desktop-app/ui/src/components/GfsMarkdownPreview/index.tsx
+++ b/desktop-app/ui/src/components/GfsMarkdownPreview/index.tsx
@@ -2,12 +2,16 @@ import { Fragment, useEffect, useId, useMemo, useRef, useState } from 'react'
import type { ReactNode } from 'react'
import { createPortal } from 'react-dom'
import { IconButton, StatusBanner } from '@components/Common'
-import { IconClose } from '@components/SidebarNav/icons'
+import { IconClose, IconCopy } from '@components/SidebarNav/icons'
import { assertGfsMarkdownPreviewSize } from '@lib/gfsMarkdownPreview'
import { parseVanillaMarkdown } from '@lib/vanillaMarkdown'
import type { MarkdownBlock, MarkdownInlineNode } from '@lib/vanillaMarkdown.types'
import type { GfsMarkdownPreviewProps } from './types'
+function isPlainTextName(fileName: string): boolean {
+ return fileName.toLowerCase().endsWith('.txt')
+}
+
function renderInlineNodes(nodes: MarkdownInlineNode[]): ReactNode[] {
return nodes.map(node => {
if (node.kind === 'text') return <Fragment key={node.id}>{node.value}</Fragment>
@@ -67,7 +71,16 @@ export function GfsMarkdownPreview({
const closeButtonRef = useRef<HTMLButtonElement | null>(null)
const [source, setSource] = useState<string | null>(null)
const [previewError, setPreviewError] = useState<string | null>(null)
+ const [copyState, setCopyState] = useState<'idle' | 'copied' | 'error'>('idle')
+ const copyResetTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
const blocks = useMemo(() => (source === null ? [] : parseVanillaMarkdown(source)), [source])
+ const isPlainText = isPlainTextName(fileName)
+
+ useEffect(() => {
+ return () => {
+ if (copyResetTimeoutRef.current) clearTimeout(copyResetTimeoutRef.current)
+ }
+ }, [])
useEffect(() => {
closeButtonRef.current?.focus()
@@ -103,6 +116,22 @@ export function GfsMarkdownPreview({
}
}, [byteLength, gfsUri, onDownloadError])
+ async function copySourceToClipboard(): Promise<void> {
+ if (source === null) return
+ try {
+ if (navigator.clipboard?.writeText) {
+ await navigator.clipboard.writeText(source)
+ setCopyState('copied')
+ } else {
+ setCopyState('error')
+ }
+ } catch {
+ setCopyState('error')
+ }
+ if (copyResetTimeoutRef.current) clearTimeout(copyResetTimeoutRef.current)
+ copyResetTimeoutRef.current = setTimeout(() => setCopyState('idle'), 2000)
+ }
+
return createPortal(
<div
className="da-gfs-markdown-preview-modal"
@@ -119,34 +148,62 @@ export function GfsMarkdownPreview({
>
<header className="da-gfs-markdown-preview-dialog__header">
<h3 id={titleId}>{fileName}</h3>
- <IconButton
- ref={closeButtonRef}
- label="Close Markdown preview"
- onClick={onClose}
- size="sm"
- variant="ghost"
- >
- <IconClose />
- </IconButton>
+ <div className="da-gfs-markdown-preview-dialog__header-actions">
+ <IconButton
+ label={
+ copyState === 'copied'
+ ? 'Copied preview contents to clipboard'
+ : 'Copy preview contents to clipboard'
+ }
+ disabled={source === null}
+ onClick={() => void copySourceToClipboard()}
+ size="sm"
+ variant="ghost"
+ >
+ <IconCopy />
+ </IconButton>
+ <IconButton
+ ref={closeButtonRef}
+ label="Close preview"
+ onClick={onClose}
+ size="sm"
+ variant="ghost"
+ >
+ <IconClose />
+ </IconButton>
+ </div>
</header>
<div className="da-gfs-markdown-preview-dialog__body">
{previewError ? <StatusBanner tone="error" text={previewError} /> : null}
{!previewError && source === null ? (
<div className="da-gfs-markdown-preview-dialog__loading" role="status">
- Loading Markdown preview…
+ Loading preview…
</div>
) : null}
{source !== null && !previewError ? (
- <article
- aria-label={`Markdown preview of ${fileName}`}
- className="da-gfs-markdown-preview__content markdown-content"
- >
- {blocks.length > 0 ? (
- blocks.map(renderBlock)
- ) : (
- <p className="da-gfs-markdown-preview__empty">This Markdown file is empty.</p>
- )}
- </article>
+ isPlainText ? (
+ <article
+ aria-label={`Text preview of ${fileName}`}
+ className="da-gfs-text-preview__content"
+ >
+ {source.length > 0 ? (
+ <pre className="da-gfs-text-preview__body">{source}</pre>
+ ) : (
+ <p className="da-gfs-markdown-preview__empty">This text file is empty.</p>
+ )}
+ </article>
+ ) : (
+ <article
+ aria-label={`Markdown preview of ${fileName}`}
+ className="da-gfs-markdown-preview__content markdown-content"
+ >
+ {blocks.length > 0 ? (
+ blocks.map(renderBlock)
+ ) : (
+ <p className="da-gfs-markdown-preview__empty">This Markdown file is empty.</p>
+ )}
+ </article>
+ )
) : null}
</div>
</section>
diff --git a/desktop-app/ui/src/components/SidebarNav/icons.tsx b/desktop-app/ui/src/components/SidebarNav/icons.tsx
index b4fc3ec4f..77f976aa6 100644
--- a/desktop-app/ui/src/components/SidebarNav/icons.tsx
+++ b/desktop-app/ui/src/components/SidebarNav/icons.tsx
@@ -80,11 +80,12 @@ export function IconAttachFile(props: IconProps) {
)
}
+// Copy to clipboard — two-stacked-documents glyph (Font Awesome fa-copy,
+// solid). Used by the GFS image and text/markdown preview modals.
export function IconCopy(props: IconProps) {
return (
- <svg viewBox="0 0 24 24" aria-hidden="true" {...BASE_STROKE_PROPS} {...props}>
- <rect width="14" height="14" x="8" y="8" rx="2" />
- <path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />
+ <svg viewBox="0 0 384 384" fill="currentColor" aria-hidden="true" {...props}>
+ <path d="M280 64H112c-8.8 0-16 7.2-16 16v32c0 8.8 7.2 16 16 16h168c8.8 0 16-7.2 16-16V80c0-8.8-7.2-16-16-16zM32 224c0-35.3 28.7-64 64-64h192c35.3 0 64 28.7 64 64v128c0 35.3-28.7 64-64 64H96c-35.3 0-64-28.7-64-64V224zm128 48c-8.8 0-16 7.2-16 16 0 8.8 7.2 16 16 16h128c8.8 0 16-7.2 16-16 0-8.8-7.2-16-16-16H160z" />
</svg>
)
}
diff --git a/desktop-app/ui/src/constants/gfsMarkdownPreview.ts b/desktop-app/ui/src/constants/gfsMarkdownPreview.ts
index 6927c0292..0fe3b143b 100644
--- a/desktop-app/ui/src/constants/gfsMarkdownPreview.ts
+++ b/desktop-app/ui/src/constants/gfsMarkdownPreview.ts
@@ -1,2 +1,6 @@
-export const GFS_MARKDOWN_PREVIEW_EXTENSIONS: ReadonlySet<string> = new Set(['md', 'markdown'])
+export const GFS_MARKDOWN_PREVIEW_EXTENSIONS: ReadonlySet<string> = new Set([
+ 'md',
+ 'markdown',
+ 'txt',
+])
export const GFS_MARKDOWN_PREVIEW_MAX_BYTES = 2 * 1024 * 1024
diff --git a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx
index 9016b6963..6ae4b1b3f 100644
--- a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx
+++ b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx
@@ -1621,6 +1621,91 @@ describe('FilesPage', () => {
expect(revokeObjectURL).toHaveBeenCalledWith('blob:gfs-image-preview')
})
+ it('copies markdown source to the clipboard via the preview header button', async () => {
+ const writeText = vi.fn(async () => undefined)
+ Object.defineProperty(navigator, 'clipboard', {
+ configurable: true,
+ value: { ...(navigator.clipboard ?? {}), writeText },
+ })
+
+ const markdown = '# Hello\n\nGreetings.'
+ const download = vi.fn(async () => ({
+ bytes: new TextEncoder().encode(markdown).buffer,
+ }))
+ Object.defineProperty(window, 'clerum', {
+ configurable: true,
+ value: { gfs: { download } },
+ })
+ hookMock.useGfsBrowserController.mockReturnValue({
+ ...baseController(),
+ accessibleResources: [
+ {
+ resourceId: 'markdown-2',
+ rid: 'markdown-2',
+ gfsUri: 'gfs://main/markdown-2',
+ drive: 'main',
+ parentResourceId: null,
+ name: 'README.md',
+ kind: 'file',
+ path: '/README.md',
+ version: 1,
+ bytes: markdown.length,
+ sources: ['grant'],
+ permissions: ['read'],
+ coversDescendants: false,
+ },
+ ],
+ })
+
+ renderFilesPage()
+ fireEvent.click(screen.getByRole('button', { name: 'README.md' }))
+ const dialog = await screen.findByRole('dialog', { name: 'README.md' })
+
+ const copyButton = within(dialog).getByRole('button', {
+ name: /Copy preview contents to clipboard/i,
+ })
+ fireEvent.click(copyButton)
+
+ await waitFor(() => expect(writeText).toHaveBeenCalledWith(markdown))
+ })
+
+ it('renders a .txt file as plain text inside the preview dialog', async () => {
+ const download = vi.fn(async () => ({
+ bytes: new TextEncoder().encode('line one\nline two\twith tab').buffer,
+ }))
+ Object.defineProperty(window, 'clerum', {
+ configurable: true,
+ value: { gfs: { download } },
+ })
+ hookMock.useGfsBrowserController.mockReturnValue({
+ ...baseController(),
+ accessibleResources: [
+ {
+ resourceId: 'text-1',
+ rid: 'text-1',
+ gfsUri: 'gfs://main/text-1',
+ drive: 'main',
+ parentResourceId: null,
+ name: 'notes.txt',
+ kind: 'file',
+ path: '/notes.txt',
+ version: 1,
+ bytes: 26,
+ sources: ['grant'],
+ permissions: ['read'],
+ coversDescendants: false,
+ },
+ ],
+ })
+
+ renderFilesPage()
+ fireEvent.click(screen.getByRole('button', { name: 'notes.txt' }))
+ const dialog = await screen.findByRole('dialog', { name: 'notes.txt' })
+ const pre = await within(dialog).findByText(/line one/)
+ expect(pre.tagName).toBe('PRE')
+ expect(pre.textContent).toContain('line two\twith tab')
+ })
+
it('previews a video file in a closable HTML5 video dialog', async () => {
const download = vi.fn(async () => ({ bytes: new Uint8Array([1, 2, 3]).buffer }))
const createObjectURL = vi.fn(() => 'blob:gfs-video-preview')
@@ -1715,7 +1800,7 @@ describe('FilesPage', () => {
expect(dialog.querySelector('script')).toBeNull()
expect(download).toHaveBeenCalledWith('gfs://main/markdown-1')
- fireEvent.click(within(dialog).getByRole('button', { name: 'Close Markdown preview' }))
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Close preview' }))
await waitFor(() => expect(screen.queryByRole('dialog', { name: 'README.md' })).toBeNull())
})
diff --git a/desktop-app/ui/src/styles.css b/desktop-app/ui/src/styles.css
index 89efbf134..aed6c1823 100644
--- a/desktop-app/ui/src/styles.css
+++ b/desktop-app/ui/src/styles.css
@@ -14212,6 +14212,36 @@ pre {
background: rgba(var(--panel-rgb), 0.48);
}
+.da-gfs-image-preview-dialog__header-actions,
+.da-gfs-markdown-preview-dialog__header-actions,
+.da-gfs-video-preview-dialog__header-actions {
+ display: inline-flex;
+ align-items: center;
+ gap: var(--space-2);
+}
+
+.da-gfs-text-preview__content {
+ min-width: 0;
+ min-height: 0;
+ padding: var(--space-5);
+}
+
+.da-gfs-text-preview__body {
+ max-width: 60rem;
+ margin: 0;
+ padding: var(--space-4);
+ border: 1px solid rgba(var(--edge-rgb), 0.24);
+ border-radius: var(--radius-sm);
+ background: var(--panel);
+ color: var(--text);
+ font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
+ font-size: var(--font-size-sm);
+ line-height: 1.5;
+ overflow: auto;
+ white-space: pre-wrap;
+ word-break: break-word;
+}
+
.da-gfs-image-preview-dialog__header h3,
.da-gfs-markdown-preview-dialog__header h3,
.da-gfs-video-preview-dialog__header h3 {