mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat: add conditional caching and allow-same-origin to file preview, add PreviewHtmlButton
This commit is contained in:
1 parent
7f9403ce5e
commit
ba08f5c24a
10 files changed
+155
-63
No files matched your search
@@ -47,7 +47,7 @@ describe('FilePreview - HTML preview', () => {
|
||||
|
||||
const iframe = screen.getByTitle('HTML preview: dashboard.html')
|
||||
expect(iframe).toBeInTheDocument()
|
||||
expect(iframe).toHaveAttribute('sandbox', 'allow-scripts')
|
||||
expect(iframe).toHaveAttribute('sandbox', 'allow-scripts allow-same-origin')
|
||||
expect(iframe.getAttribute('src')).toContain('/api/files/preview/test-repo/dashboard.html')
|
||||
})
|
||||
|
||||
|
||||
@@ -219,6 +219,12 @@ export const FilePreview = memo(function FilePreview({ file, hideHeader = false,
|
||||
['application/json', 'application/xml', 'text/javascript', 'text/typescript'].includes(file.mimeType || '')
|
||||
|
||||
const renderContent = () => {
|
||||
const htmlPreviewFrame = (
|
||||
<div className="h-[calc(100vh-12rem)] min-h-[480px] rounded border border-border overflow-hidden bg-white">
|
||||
<HtmlPreviewFrame title={`HTML preview: ${file.name}`} src={getFilePreviewUrl(file.path)} />
|
||||
</div>
|
||||
)
|
||||
|
||||
if (file.mimeType?.startsWith('image/')) {
|
||||
return (
|
||||
<div className="flex justify-center p-4">
|
||||
@@ -236,11 +242,7 @@ export const FilePreview = memo(function FilePreview({ file, hideHeader = false,
|
||||
const showMarkdownPreview = isMarkdownFile && markdownPreview && viewMode !== 'edit'
|
||||
|
||||
if (showHtmlPreview) {
|
||||
return (
|
||||
<div className="h-[calc(100vh-12rem)] min-h-[480px] rounded border border-border overflow-hidden bg-white">
|
||||
<HtmlPreviewFrame title={`HTML preview: ${file.name}`} src={getFilePreviewUrl(file.path)} />
|
||||
</div>
|
||||
)
|
||||
return htmlPreviewFrame
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -301,11 +303,7 @@ export const FilePreview = memo(function FilePreview({ file, hideHeader = false,
|
||||
}
|
||||
|
||||
if (isHtmlFile && htmlPreview) {
|
||||
return (
|
||||
<div className="h-[calc(100vh-12rem)] min-h-[480px] rounded border border-border overflow-hidden bg-white">
|
||||
<HtmlPreviewFrame title={`HTML preview: ${file.name}`} src={getFilePreviewUrl(file.path)} />
|
||||
</div>
|
||||
)
|
||||
return htmlPreviewFrame
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
@@ -62,7 +62,7 @@ describe('HtmlArtifactPanel', () => {
|
||||
expect(iframe).toBeInTheDocument()
|
||||
expect(iframe).toHaveAttribute('src')
|
||||
expect(iframe.getAttribute('src')).toContain('/api/files/preview/')
|
||||
expect(iframe).toHaveAttribute('sandbox', 'allow-scripts')
|
||||
expect(iframe).toHaveAttribute('sandbox', 'allow-scripts allow-same-origin')
|
||||
})
|
||||
|
||||
it('does not include allow-same-origin in sandbox', () => {
|
||||
|
||||
@@ -8,12 +8,13 @@ interface HtmlPreviewFrameProps {
|
||||
}
|
||||
|
||||
export function HtmlPreviewFrame({ title, src, srcDoc, className }: HtmlPreviewFrameProps) {
|
||||
const sandbox = src ? 'allow-scripts allow-same-origin' : 'allow-scripts'
|
||||
return (
|
||||
<iframe
|
||||
title={title}
|
||||
src={src}
|
||||
srcDoc={srcDoc}
|
||||
sandbox="allow-scripts"
|
||||
sandbox={sandbox}
|
||||
referrerPolicy="no-referrer"
|
||||
className={cn('h-full w-full border-0 bg-white', className)}
|
||||
/>
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
interface PreviewHtmlButtonProps {
|
||||
onClick: () => void
|
||||
className?: string
|
||||
}
|
||||
|
||||
export function PreviewHtmlButton({ onClick, className }: PreviewHtmlButtonProps) {
|
||||
return (
|
||||
<button
|
||||
onClick={onClick}
|
||||
className={cn(
|
||||
'rounded bg-card hover:bg-card-hover text-muted-foreground hover:text-foreground text-xs',
|
||||
className,
|
||||
)}
|
||||
title="Preview HTML artifact"
|
||||
>
|
||||
Preview HTML
|
||||
</button>
|
||||
)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { useMobile } from '@/hooks/useMobile'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { isHtmlPath } from '@/lib/htmlArtifacts'
|
||||
import type { OpenHtmlArtifactInput } from '@/lib/htmlArtifacts'
|
||||
import { PreviewHtmlButton } from '@/components/html-preview/PreviewHtmlButton'
|
||||
|
||||
interface CodePreviewProps {
|
||||
fileName: string
|
||||
@@ -92,13 +93,7 @@ export function CodePreview({ fileName, content, onHtmlArtifactOpen }: CodePrevi
|
||||
<span className="font-medium truncate flex-1">{extractFileFromPath(fileName)}</span>
|
||||
<div className="flex items-center gap-1 flex-shrink-0">
|
||||
{isHtmlFile && onHtmlArtifactOpen && (
|
||||
<button
|
||||
onClick={handleHtmlArtifact}
|
||||
className="px-2 py-1 rounded bg-card hover:bg-card-hover text-muted-foreground hover:text-foreground text-xs"
|
||||
title="Preview HTML artifact"
|
||||
>
|
||||
Preview HTML
|
||||
</button>
|
||||
<PreviewHtmlButton onClick={handleHtmlArtifact} className="px-2 py-1" />
|
||||
)}
|
||||
<CopyButton content={content} title="Copy" iconSize="sm" variant="ghost" />
|
||||
</div>
|
||||
|
||||
@@ -6,6 +6,7 @@ import rehypeRaw from 'rehype-raw'
|
||||
import mermaid from 'mermaid'
|
||||
import { Maximize2, X, AlertCircle } from 'lucide-react'
|
||||
import { CopyButton } from '@/components/ui/copy-button'
|
||||
import { PreviewHtmlButton } from '@/components/html-preview/PreviewHtmlButton'
|
||||
import type { components } from '@/api/opencode-types'
|
||||
import type { OpenHtmlArtifactInput } from '@/lib/htmlArtifacts'
|
||||
import { useTheme } from '@/hooks/useTheme'
|
||||
@@ -181,13 +182,10 @@ function CodeBlock({ children, className, onHtmlArtifactOpen, ...props }: CodeBl
|
||||
</pre>
|
||||
<div className="absolute top-2 right-2 flex gap-1">
|
||||
{isHtmlCode && onHtmlArtifactOpen && (
|
||||
<button
|
||||
<PreviewHtmlButton
|
||||
onClick={() => onHtmlArtifactOpen({ source: 'inline', html: codeContent, title: 'HTML artifact' })}
|
||||
className="p-1.5 rounded bg-card hover:bg-card-hover text-muted-foreground hover:text-foreground text-xs"
|
||||
title="Preview HTML artifact"
|
||||
>
|
||||
Preview HTML
|
||||
</button>
|
||||
className="p-1.5"
|
||||
/>
|
||||
)}
|
||||
<CopyButton content={rawCodeContent} title="Copy code" />
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user