refactor: 重构 HtmlToMarkdown 和 MarkdownToHtml 页面,统一 shadcn 样式并优化打印与暗色模式

This commit is contained in:
雨霖铃
2026-05-22 22:06:34 +08:00
parent ae0b923e11
commit 9601afdd49
3 changed files with 200 additions and 135 deletions
+70 -36
View File
@@ -4,9 +4,11 @@ import { useLazyTranslation } from '@/utils/useLazyTranslation';
import PageHeader from '@/components/PageHeader'; import PageHeader from '@/components/PageHeader';
import CopyButton from '@/components/CopyButton'; import CopyButton from '@/components/CopyButton';
import SwitchButtonGroup from '@/components/SwitchButtonGroup'; import SwitchButtonGroup from '@/components/SwitchButtonGroup';
import { Button } from '@/components/ui/button'; // 💡 1. 全面回归规范:引入原生的 shadcn 原子 Button
import { useStorageState } from '@/utils/useStorageState'; import { useStorageState } from '@/utils/useStorageState';
import type { HtmlToMarkdownPreviewMode } from '@/types/storage'; import type { HtmlToMarkdownPreviewMode } from '@/types/storage';
import { htmlToMarkdown, downloadMarkdownFile, SAMPLE_HTML } from '@/utils/htmlToMarkdown'; import { downloadMarkdownFile, htmlToMarkdown, SAMPLE_HTML } from '@/utils/htmlToMarkdown';
import { cn } from '@/lib/utils';
const isValidPreviewMode = (val: unknown): val is HtmlToMarkdownPreviewMode => const isValidPreviewMode = (val: unknown): val is HtmlToMarkdownPreviewMode =>
typeof val === 'string' && ['split', 'preview', 'markdown'].includes(val); typeof val === 'string' && ['split', 'preview', 'markdown'].includes(val);
@@ -44,12 +46,20 @@ export default function HtmlToMarkdownPage() {
const showOutput = previewMode !== 'markdown'; const showOutput = previewMode !== 'markdown';
return ( return (
<div className="container mx-auto px-4 py-6 max-w-7xl"> /* 💡 统一间距尺寸:
<PageHeader title={t('pageTitle')} subtitle={t('pageSubtitle')} icon={<Code size={20} />} /> - 彻底清除多余的 container max-w-7xl 这种网页大边距,
- 统一收拢为我们先前在 Dashboard 页、JSON 工具箱制定的 p-4 space-y-4 标准极客桌面规格。
*/
<div className="p-4 w-full flex flex-col space-y-4 select-none animate-in fade-in duration-300">
<PageHeader
title={t('pageTitle')}
subtitle={t('pageSubtitle')}
icon={<Code className="h-4 w-4" />}
/>
<div className="flex flex-col gap-4"> <div className="flex flex-col gap-4">
{/* 工具栏 */} {/* 工具栏集成区 */}
<div className="flex flex-wrap items-center justify-between gap-3"> <div className="flex flex-col sm:flex-row gap-3 justify-between items-stretch sm:items-center bg-secondary/40 rounded-xl border border-border/60 px-1.5 py-1.5 sm:h-12">
<SwitchButtonGroup <SwitchButtonGroup
value={previewMode} value={previewMode}
options={[ options={[
@@ -59,46 +69,65 @@ export default function HtmlToMarkdownPage() {
]} ]}
onChange={handleModeChange} onChange={handleModeChange}
size="small" size="small"
className="w-full sm:w-auto"
/> />
<div className="flex gap-2"> <div className="flex gap-2 shrink-0">
<button {/* 2. 重塑下载按钮:接入受控 Button,追加 active 物理微缩放动效 */}
<Button
variant="outline"
size="sm"
onClick={handleDownload} onClick={handleDownload}
disabled={!result.markdown} disabled={!result.markdown}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-foreground bg-background border border-input rounded-lg hover:bg-muted disabled:opacity-50 disabled:cursor-not-allowed transition-colors" className="h-8 rounded-md font-medium text-xs gap-1.5 shadow-sm active:scale-95 transition-all"
> >
<Download size={14} /> <Download className="h-3.5 w-3.5" />
{t('download')} {t('download')}
</button> </Button>
<button
{/* 重塑清空按钮 */}
<Button
variant="outline"
size="sm"
onClick={handleClear} onClick={handleClear}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-foreground bg-background border border-input rounded-lg hover:bg-muted transition-colors" className="h-8 rounded-md font-medium text-xs gap-1.5 shadow-sm active:scale-95 text-destructive hover:text-destructive hover:bg-destructive/5 dark:hover:bg-destructive/10 border-input/60 transition-all"
> >
<Trash2 size={14} /> <Trash2 className="h-3.5 w-3.5" />
{t('clear')} {t('clear')}
</button> </Button>
</div> </div>
</div> </div>
{/* 错误提示 */} {/* 错误提示
- 💡 核心修复点:将硬编码的 bg-red-50 实色,完美超进化为系统的全自适应透明色变体
*/}
{error && ( {error && (
<div className="p-3 bg-red-50 border border-red-200 rounded-lg text-red-700 text-sm"> <div className="p-3.5 bg-destructive/10 border border-destructive/20 rounded-xl text-destructive text-xs font-semibold tracking-wide animate-in shake duration-300">
{error} {error}
</div> </div>
)} )}
{/* 主内容区 */} {/* 双翼/单栏联动面板展示区 */}
<div <div
className={`grid gap-4 min-h-[500px] ${ className={cn(
showInput && showOutput ? 'md:grid-cols-2' : 'grid-cols-1' 'grid gap-4 min-h-[460px] w-full',
}`} showInput && showOutput ? 'grid-cols-1 md:grid-cols-2' : 'grid-cols-1',
)}
> >
{/* HTML 输入 */} {/* HTML 输入端卡片面板 */}
{showInput && ( {showInput && (
<div className="border border-border rounded-xl overflow-hidden flex flex-col"> /* 3. 智能聚焦框联动(Focus Ring Clamping):
<div className="flex items-center justify-between px-4 py-2 bg-primary/5 border-b border-border"> - 外层容器追加 focus-within 变量追踪大闸。
<span className="text-xs font-bold text-muted-foreground">{t('inputLabel')}</span> - 只要用户用鼠标点击了内部的 textarea,外层整块精巧的圆角大边框会一帧内亮起 primary 系统的深色呼吸发光环,
<span className="text-xs text-muted-foreground"> - 这种“全外包裹层框聚焦”的体验极大模仿了本地原生 IDE 的硬核专业体验!
*/
<div className="border border-border rounded-xl bg-card text-card-foreground shadow-sm overflow-hidden flex flex-col transition-all duration-200 focus-within:ring-1 focus-within:ring-ring focus-within:border-ring">
{/* 卡片头部:改用标准的灰色 bg-muted/50 */}
<div className="flex h-9 items-center justify-between px-4 bg-muted/50 border-b border-border select-none">
<span className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider">
{t('inputLabel')}
</span>
<span className="text-[10px] font-mono text-muted-foreground/60 tabular-nums">
{t('charCount', { count: html.length })} {t('charCount', { count: html.length })}
</span> </span>
</div> </div>
@@ -106,25 +135,28 @@ export default function HtmlToMarkdownPage() {
value={html} value={html}
onChange={(e) => setHtml(e.target.value)} onChange={(e) => setHtml(e.target.value)}
placeholder={t('inputPlaceholder')} placeholder={t('inputPlaceholder')}
className="flex-1 min-h-[400px] p-4 font-mono text-[0.85rem] leading-relaxed resize-none focus:outline-none" className="flex-1 min-h-[380px] p-4 bg-transparent font-mono text-xs leading-relaxed resize-none focus:outline-none text-foreground/90 select-text"
/> />
</div> </div>
)} )}
{/* Markdown 输出 */} {/* Markdown 输出端卡片面板 */}
{showOutput && ( {showOutput && (
<div className="border border-border rounded-xl overflow-hidden flex flex-col"> <div className="border border-border rounded-xl bg-card text-card-foreground shadow-sm overflow-hidden flex flex-col">
<div className="flex items-center justify-between px-4 py-2 bg-primary/5 border-b border-border"> <div className="flex h-9 items-center justify-between px-4 bg-muted/50 border-b border-border select-none">
<span className="text-xs font-bold text-muted-foreground"> <span className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider">
{(previewMode as string) === 'markdown' {(previewMode as string) === 'markdown'
? t('markdownOutputLabel') ? t('markdownOutputLabel')
: t('previewLabel')} : t('previewLabel')}
</span> </span>
<div className="flex items-center gap-1"> <div className="flex items-center gap-2">
<span className="text-xs text-muted-foreground"> <span className="text-[10px] font-mono text-muted-foreground/60 tabular-nums">
{t('charCount', { count: result.markdownLength })} {t('charCount', { count: result.markdownLength })}
</span> </span>
<CopyButton text={result.markdown} size="small" /> <CopyButton
text={result.markdown}
className="h-6 w-6 rounded-md border text-muted-foreground"
/>
</div> </div>
</div> </div>
@@ -132,12 +164,14 @@ export default function HtmlToMarkdownPage() {
<textarea <textarea
value={result.markdown} value={result.markdown}
readOnly readOnly
className="flex-1 min-h-[400px] p-4 font-mono text-[0.85rem] leading-relaxed resize-none focus:outline-none bg-muted" className="flex-1 min-h-[380px] p-4 font-mono text-xs leading-relaxed resize-none focus:outline-none bg-muted/30 dark:bg-muted/10 text-foreground/80 select-text"
/> />
) : ( ) : (
<div className="flex-1 p-4 min-h-[400px] overflow-auto bg-background font-mono text-[0.85rem] leading-relaxed whitespace-pre-wrap break-words"> <div className="flex-1 p-4 min-h-[380px] overflow-auto bg-transparent font-mono text-xs leading-relaxed whitespace-pre-wrap break-all text-foreground/90 select-text">
{result.markdown || ( {result.markdown || (
<span className="text-muted-foreground">{t('emptyHint')}</span> <span className="text-muted-foreground/70 italic text-[11px] font-sans">
{t('emptyHint')}
</span>
)} )}
</div> </div>
)} )}
+119 -94
View File
@@ -1,97 +1,83 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Code, Trash2, Printer, Download } from 'lucide-react'; import { Code, Download, Printer, Trash2 } from 'lucide-react';
import { useLazyTranslation } from '@/utils/useLazyTranslation'; import { useLazyTranslation } from '@/utils/useLazyTranslation';
import PageHeader from '@/components/PageHeader'; import PageHeader from '@/components/PageHeader';
import CopyButton from '@/components/CopyButton'; import CopyButton from '@/components/CopyButton';
import SwitchButtonGroup from '@/components/SwitchButtonGroup'; import SwitchButtonGroup from '@/components/SwitchButtonGroup';
import { Button } from '@/components/ui/button'; // 💡 1. 全面回归原子组件规范
import { useStorageState } from '@/utils/useStorageState'; import { useStorageState } from '@/utils/useStorageState';
import type { MarkdownToHtmlPreviewMode } from '@/types/storage'; import type { MarkdownToHtmlPreviewMode } from '@/types/storage';
import { import {
markdownToHtml,
wrapHtmlDocument,
downloadHtmlFile, downloadHtmlFile,
markdownToHtml,
printHtml, printHtml,
SAMPLE_MARKDOWN, SAMPLE_MARKDOWN,
wrapHtmlDocument,
} from '@/utils/markdownToHtml'; } from '@/utils/markdownToHtml';
import { cn } from '@/lib/utils';
const isValidPreviewMode = (val: unknown): val is MarkdownToHtmlPreviewMode => const isValidPreviewMode = (val: unknown): val is MarkdownToHtmlPreviewMode =>
typeof val === 'string' && ['split', 'preview', 'html'].includes(val); typeof val === 'string' && ['split', 'preview', 'html'].includes(val);
// 💡 2. 样式超进化:将实色系全部改为标准 rgba 通道,确保在 iframe 内部能跟随宿主主题自适应混色
const PREVIEW_STYLES = ` const PREVIEW_STYLES = `
.markdown-body { .markdown-body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.6; line-height: 1.6;
color: inherit; color: var(--md-foreground, #1f2328);
background-color: transparent;
} }
.markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h1, .markdown-body h2, .markdown-body h3 {
.markdown-body h4, .markdown-body h5, .markdown-body h6 { margin-top: 24px;
margin-top: 20px; margin-bottom: 16px;
margin-bottom: 12px;
font-weight: 600; font-weight: 600;
line-height: 1.25; line-height: 1.25;
} }
.markdown-body h1 { font-size: 1.8em; border-bottom: 1px solid rgba(128,128,128,0.2); padding-bottom: 0.3em; } .markdown-body h1 { font-size: 1.6em; border-bottom: 1px solid var(--md-border, rgba(128,128,128,0.2)); padding-bottom: 0.3em; }
.markdown-body h2 { font-size: 1.5em; border-bottom: 1px solid rgba(128,128,128,0.2); padding-bottom: 0.3em; } .markdown-body h2 { font-size: 1.35em; border-bottom: 1px solid var(--md-border, rgba(128,128,128,0.2)); padding-bottom: 0.3em; }
.markdown-body h3 { font-size: 1.25em; } .markdown-body p { margin-top: 0; margin-bottom: 16px; font-size: 14px; }
.markdown-body p { margin-top: 0; margin-bottom: 12px; } .markdown-body a { color: #2563eb; text-decoration: none; }
.markdown-body a { color: #1976d2; text-decoration: none; }
.markdown-body a:hover { text-decoration: underline; } .markdown-body a:hover { text-decoration: underline; }
.markdown-body code { .markdown-body code {
background-color: rgba(128,128,128,0.1); background-color: var(--md-code-bg, rgba(128,128,128,0.08));
border-radius: 3px; border-radius: 4px;
font-size: 85%; font-size: 85%;
padding: 0.2em 0.4em; padding: 0.2em 0.4em;
font-family: 'SFMono-Regular', Consolas, monospace; font-family: Menlo, Consolas, monospace;
} }
.markdown-body pre { .markdown-body pre {
background-color: rgba(128,128,128,0.08); background-color: var(--md-pre-bg, rgba(128,128,128,0.05));
border-radius: 6px; border-radius: 8px;
font-size: 85%; font-size: 85%;
line-height: 1.45; line-height: 1.45;
overflow: auto; overflow: auto;
padding: 14px; padding: 16px;
margin: 0 0 12px; margin: 0 0 16px;
border: 1px solid var(--md-border, rgba(128,128,128,0.15));
} }
.markdown-body pre code { .markdown-body pre code {
background-color: transparent; background-color: transparent;
border: 0; border: 0;
display: inline;
line-height: inherit;
margin: 0;
padding: 0; padding: 0;
word-wrap: normal;
} }
.markdown-body blockquote { .markdown-body blockquote {
border-left: 0.25em solid rgba(128,128,128,0.3); border-left: 0.25em solid var(--md-quote-line, rgba(128,128,128,0.3));
color: rgba(128,128,128,0.7); color: var(--md-muted, rgba(128,128,128,0.6));
margin: 0 0 12px; margin: 0 0 16px;
padding: 0 1em; padding: 0 1em;
} }
.markdown-body ul, .markdown-body ol { margin-top: 0; margin-bottom: 12px; padding-left: 2em; }
.markdown-body li + li { margin-top: 0.25em; }
.markdown-body img { max-width: 100%; box-sizing: content-box; }
.markdown-body table { .markdown-body table {
border-collapse: collapse; border-collapse: collapse;
border-spacing: 0;
display: block;
overflow: auto;
width: 100%; width: 100%;
margin-bottom: 12px; margin-bottom: 16px;
font-size: 13px;
} }
.markdown-body table th, .markdown-body table td { .markdown-body table th, .markdown-body table td {
border: 1px solid rgba(128,128,128,0.25); border: 1px solid var(--md-border, rgba(128,128,128,0.2));
padding: 6px 13px; padding: 6px 13px;
} }
.markdown-body table tr:nth-child(2n) { background-color: rgba(128,128,128,0.05); } .markdown-body table tr:nth-child(2n) { background-color: var(--md-code-bg, rgba(128,128,128,0.03)); }
.markdown-body table th { font-weight: 600; background-color: rgba(128,128,128,0.05); } .markdown-body table th { font-weight: 600; background-color: var(--md-code-bg, rgba(128,128,128,0.05)); }
.markdown-body hr {
background-color: rgba(128,128,128,0.2);
border: 0;
height: 0.25em;
margin: 20px 0;
padding: 0;
}
.markdown-body input[type="checkbox"] { margin-right: 0.5em; }
`; `;
export default function MarkdownToHtmlPage() { export default function MarkdownToHtmlPage() {
@@ -107,22 +93,42 @@ export default function MarkdownToHtmlPage() {
const result = useMemo(() => markdownToHtml(markdown), [markdown]); const result = useMemo(() => markdownToHtml(markdown), [markdown]);
const error = result.hasError ? (result.error ?? null) : null; const error = result.hasError ? (result.error ?? null) : null;
// 💡 3. 沙箱暗黑模式自适应守卫(Sandbox Dark Mode Synchronizer):
// 根据宿主最外层的 HTML 是否包含 .dark 类名,动态向 iframe 注入对应的明暗模式 CSS 变量色轴
useEffect(() => { useEffect(() => {
const iframe = iframeRef.current; const iframe = iframeRef.current;
if (!iframe || !iframe.contentDocument) return; if (!iframe) return;
const doc = iframe.contentDocument; const isDarkMode = document.documentElement.classList.contains('dark');
doc.open(); const themeVariables = isDarkMode
doc.write(`<!DOCTYPE html> ? `:root {
<html> --md-foreground: #e6edf3;
--md-border: rgba(255,255,255,0.15);
--md-code-bg: rgba(255,255,255,0.1);
--md-pre-bg: rgba(255,255,255,0.05);
--md-muted: #8b949e;
}`
: `:root {
--md-foreground: #1f2328;
--md-border: rgba(128,128,128,0.2);
--md-code-bg: rgba(128,128,128,0.08);
--md-pre-bg: rgba(128,128,128,0.04);
--md-muted: #636c76;
}`;
iframe.srcdoc = `<!DOCTYPE html>
<html lang="zh">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<style>${PREVIEW_STYLES}</style> <style>
${themeVariables},
${PREVIEW_STYLES},
body { margin: 0; background-color: transparent; }
</style>
</head> </head>
<body class="markdown-body">${result.html}</body> <body class="markdown-body">${result.html}</body>
</html>`); </html>`;
doc.close(); }, [result.html, previewMode]); // 当切换预览模式或 HTML 改变时,自发刷新沙箱
}, [result.html]);
const handleModeChange = useCallback( const handleModeChange = useCallback(
(newMode: MarkdownToHtmlPreviewMode) => { (newMode: MarkdownToHtmlPreviewMode) => {
@@ -148,12 +154,16 @@ export default function MarkdownToHtmlPage() {
const showPreview = previewMode !== 'html'; const showPreview = previewMode !== 'html';
return ( return (
<div className="container mx-auto px-4 py-6 max-w-7xl"> <div className="p-4 w-full flex flex-col space-y-4 select-none animate-in fade-in duration-300">
<PageHeader title={t('pageTitle')} subtitle={t('pageSubtitle')} icon={<Code size={20} />} /> <PageHeader
title={t('pageTitle')}
subtitle={t('pageSubtitle')}
icon={<Code className="h-4 w-4" />}
/>
<div className="flex flex-col gap-4"> <div className="flex flex-col gap-4">
{/* 工具栏 */} {/* 工具集成控制护栏 */}
<div className="flex flex-wrap items-center justify-between gap-3"> <div className="flex flex-col sm:flex-row gap-3 justify-between items-stretch sm:items-center bg-secondary/40 rounded-xl border border-border/60 px-1.5 py-1.5 sm:h-12">
<SwitchButtonGroup <SwitchButtonGroup
value={previewMode} value={previewMode}
options={[ options={[
@@ -163,52 +173,64 @@ export default function MarkdownToHtmlPage() {
]} ]}
onChange={handleModeChange} onChange={handleModeChange}
size="small" size="small"
className="w-full sm:w-auto"
/> />
<div className="flex gap-2"> <div className="flex gap-2 shrink-0">
<button <Button
variant="outline"
size="sm"
onClick={handleClear} onClick={handleClear}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-foreground bg-background border border-input rounded-lg hover:bg-muted transition-colors" className="h-8 rounded-md font-medium text-xs gap-1.5 shadow-sm active:scale-95 text-destructive hover:text-destructive hover:bg-destructive/5 dark:hover:bg-destructive/10 border-input/60 transition-all"
> >
<Trash2 size={14} /> <Trash2 className="h-3.5 w-3.5" />
{t('clear')} {t('clear')}
</button> </Button>
<button <Button
variant="outline"
size="sm"
onClick={handlePrint} onClick={handlePrint}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-foreground bg-background border border-input rounded-lg hover:bg-muted transition-colors" disabled={!result.html}
className="h-8 rounded-md font-medium text-xs gap-1.5 shadow-sm active:scale-95 transition-all"
> >
<Printer size={14} /> <Printer className="h-3.5 w-3.5" />
{t('print')} {t('print')}
</button> </Button>
<button <Button
variant="outline"
size="sm"
onClick={handleDownload} onClick={handleDownload}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-foreground bg-background border border-input rounded-lg hover:bg-muted transition-colors" disabled={!result.html}
className="h-8 rounded-md font-medium text-xs gap-1.5 shadow-sm active:scale-95 transition-all"
> >
<Download size={14} /> <Download className="h-3.5 w-3.5" />
{t('download')} {t('download')}
</button> </Button>
</div> </div>
</div> </div>
{/* 错误提示 */} {/* 错误拦截提示 */}
{error && ( {error && (
<div className="p-3 bg-red-50 border border-red-200 rounded-lg text-red-700 text-sm"> <div className="p-3.5 bg-destructive/10 border border-destructive/20 rounded-xl text-destructive text-xs font-semibold tracking-wide animate-in shake duration-300">
{error} {error}
</div> </div>
)} )}
{/* 主内容区 */} {/* 主框架多栏联动排版轴 */}
<div <div
className={`grid gap-4 min-h-[500px] ${ className={cn(
showInput && showPreview ? 'md:grid-cols-2' : 'grid-cols-1' 'grid gap-4 min-h-[460px] w-full',
}`} showInput && showPreview ? 'grid-cols-1 md:grid-cols-2' : 'grid-cols-1',
)}
> >
{/* Markdown 输入 */} {/* Markdown 输入翼终端 */}
{showInput && ( {showInput && (
<div className="border border-border rounded-xl overflow-hidden flex flex-col"> <div className="border border-border rounded-xl bg-card text-card-foreground shadow-sm overflow-hidden flex flex-col transition-all duration-200 focus-within:ring-1 focus-within:ring-ring focus-within:border-ring">
<div className="flex items-center justify-between px-4 py-2 bg-primary/5 border-b border-border"> <div className="flex h-9 items-center justify-between px-4 bg-muted/50 border-b border-border select-none">
<span className="text-xs font-bold text-muted-foreground">{t('inputLabel')}</span> <span className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider">
<span className="text-xs text-muted-foreground"> {t('inputLabel')}
</span>
<span className="text-[10px] font-mono text-muted-foreground/60 tabular-nums">
{t('charCount', { count: markdown.length })} {t('charCount', { count: markdown.length })}
</span> </span>
</div> </div>
@@ -216,23 +238,26 @@ export default function MarkdownToHtmlPage() {
value={markdown} value={markdown}
onChange={(e) => setMarkdown(e.target.value)} onChange={(e) => setMarkdown(e.target.value)}
placeholder={t('inputPlaceholder')} placeholder={t('inputPlaceholder')}
className="flex-1 min-h-[400px] p-4 font-mono text-[0.85rem] leading-relaxed resize-none focus:outline-none" className="flex-1 min-h-[380px] p-4 bg-transparent font-mono text-xs leading-relaxed resize-none focus:outline-none text-foreground/90 select-text"
/> />
</div> </div>
)} )}
{/* 预览/输出区 */} {/* 实时 HTML/Iframe 预览翼终端 */}
{showPreview && ( {showPreview && (
<div className="border border-border rounded-xl overflow-hidden flex flex-col"> <div className="border border-border rounded-xl bg-card text-card-foreground shadow-sm overflow-hidden flex flex-col">
<div className="flex items-center justify-between px-4 py-2 bg-primary/5 border-b border-border"> <div className="flex h-9 items-center justify-between px-4 bg-muted/50 border-b border-border select-none">
<span className="text-xs font-bold text-muted-foreground"> <span className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider">
{(previewMode as string) === 'html' ? t('htmlOutputLabel') : t('previewLabel')} {(previewMode as string) === 'html' ? t('htmlOutputLabel') : t('previewLabel')}
</span> </span>
<div className="flex items-center gap-1"> <div className="flex items-center gap-2">
<span className="text-xs text-muted-foreground"> <span className="text-[10px] font-mono text-muted-foreground/60 tabular-nums">
{t('charCount', { count: result.htmlLength })} {t('charCount', { count: result.htmlLength })}
</span> </span>
<CopyButton text={result.html} size="small" /> <CopyButton
text={result.html}
className="h-6 w-6 rounded-md border text-muted-foreground"
/>
</div> </div>
</div> </div>
@@ -240,14 +265,14 @@ export default function MarkdownToHtmlPage() {
<textarea <textarea
value={result.html} value={result.html}
readOnly readOnly
className="flex-1 min-h-[400px] p-4 font-mono text-[0.8rem] leading-relaxed resize-none focus:outline-none bg-muted" className="flex-1 min-h-[380px] p-4 font-mono text-xs leading-relaxed resize-none focus:outline-none bg-muted/30 dark:bg-muted/10 text-foreground/80 select-text"
/> />
) : ( ) : (
<div className="flex-1 p-4 min-h-[400px] overflow-auto bg-background"> <div className="flex-1 p-4 min-h-[380px] overflow-auto bg-transparent">
<iframe <iframe
ref={iframeRef} ref={iframeRef}
title="markdown-preview" title="markdown-preview"
className="w-full h-full min-h-[380px] border-none bg-transparent" className="w-full h-full min-h-[350px] border-none bg-transparent"
/> />
</div> </div>
)} )}
+11 -5
View File
@@ -195,16 +195,17 @@ export function downloadHtmlFile(content: string, filename: string = 'export.htm
* @param title - 打印窗口标题 * @param title - 打印窗口标题
*/ */
export function printHtml(html: string, title: string = 'Markdown Preview'): void { export function printHtml(html: string, title: string = 'Markdown Preview'): void {
const printWindow = window.open('', '_blank', 'width=800,height=600'); const doc = wrapHtmlDocument(html, title);
const blob = new Blob([doc], { type: 'text/html;charset=utf-8' });
const url = URL.createObjectURL(blob);
const printWindow = window.open(url, '_blank', 'width=800,height=600');
if (!printWindow) { if (!printWindow) {
URL.revokeObjectURL(url);
console.error('无法打开打印窗口,请检查浏览器弹窗拦截设置'); console.error('无法打开打印窗口,请检查浏览器弹窗拦截设置');
return; return;
} }
const doc = wrapHtmlDocument(html, title);
printWindow.document.write(doc);
printWindow.document.close();
// 等待样式加载完成后打印 // 等待样式加载完成后打印
let printed = false; let printed = false;
printWindow.onload = () => { printWindow.onload = () => {
@@ -220,6 +221,11 @@ export function printHtml(html: string, title: string = 'Markdown Preview'): voi
printWindow.print(); printWindow.print();
} }
}, 500); }, 500);
// 打印完成后释放 Blob URL(浏览器标签页关闭后也会自动回收)
setTimeout(() => {
URL.revokeObjectURL(url);
}, 60000);
} }
/** /**