feat(json-tools): 引入可折叠面板组件并优化布局交互
This commit is contained in:
@@ -0,0 +1,72 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { ChevronRight } from 'lucide-react';
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
|
export interface CollapsiblePanelProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'title'> {
|
||||||
|
/** 面板标题 */
|
||||||
|
title: string;
|
||||||
|
/** 是否处于折叠态 */
|
||||||
|
collapsed: boolean;
|
||||||
|
onToggleCollapse: () => void;
|
||||||
|
/** 折叠态展示的单行缩略预览文本 */
|
||||||
|
preview?: string;
|
||||||
|
/** 展开态内容 */
|
||||||
|
children?: React.ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function CollapsiblePanel({
|
||||||
|
title,
|
||||||
|
collapsed,
|
||||||
|
onToggleCollapse,
|
||||||
|
preview,
|
||||||
|
children,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}: CollapsiblePanelProps) {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
'flex flex-col overflow-hidden rounded-md border bg-card transition-all duration-300 ease-in-out',
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
style={{
|
||||||
|
flex: collapsed ? '0 0 36px' : '1 1 120px',
|
||||||
|
}}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onToggleCollapse}
|
||||||
|
className={cn(
|
||||||
|
'flex h-[36px] w-full cursor-pointer select-none items-center justify-between bg-muted/20 hover:bg-muted/30 transition-colors px-3 shrink-0 text-left focus:outline-none focus:bg-muted/40',
|
||||||
|
!collapsed && 'border-b border-border/60',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<span className="text-[11px] font-semibold text-foreground/90 shrink-0">{title}</span>
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
'flex-1 truncate font-mono text-xs text-muted-foreground/70 ml-3 transition-all duration-300',
|
||||||
|
collapsed ? 'opacity-100 max-w-full' : 'opacity-0 max-w-0 pointer-events-none',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{preview}
|
||||||
|
</span>
|
||||||
|
<ChevronRight
|
||||||
|
className={cn(
|
||||||
|
'h-4 w-4 shrink-0 text-muted-foreground hover:text-foreground transition-transform duration-300',
|
||||||
|
!collapsed && 'rotate-90',
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
'flex-1 min-h-0 flex flex-col transition-opacity duration-300',
|
||||||
|
collapsed ? 'opacity-0 pointer-events-none' : 'opacity-100',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,29 +1,24 @@
|
|||||||
import React, { useEffect, useMemo, useState } from 'react';
|
import React, { useEffect, useMemo, useState } from 'react';
|
||||||
import EmptyPlaceholder from '@/components/EmptyPlaceholder';
|
|
||||||
import TextInputArea from '@/components/TextInputArea';
|
import TextInputArea from '@/components/TextInputArea';
|
||||||
import JsonResultPanel from './JsonResultPanel';
|
import JsonResultPanel from './JsonResultPanel';
|
||||||
|
import CollapsiblePanel from './CollapsiblePanel';
|
||||||
import { validateJson } from '@/utils/jsonFormatter';
|
import { validateJson } from '@/utils/jsonFormatter';
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
|
import { buildPreview } from '../useJsonTools';
|
||||||
import type { ConvertFunction, ConvertResult } from '../types';
|
import type { ConvertFunction, ConvertResult } from '../types';
|
||||||
|
|
||||||
const CONVERT_LABELS: Record<
|
const CONVERT_LABELS: Record<string, { inputPlaceholder: string; outputLabel: string }> = {
|
||||||
string,
|
|
||||||
{ inputPlaceholder: string; outputLabel: string; emptyHint: string }
|
|
||||||
> = {
|
|
||||||
yaml: {
|
yaml: {
|
||||||
inputPlaceholder: '输入需要转换的 JSON...',
|
inputPlaceholder: '输入需要转换的 JSON...',
|
||||||
outputLabel: 'YAML 结果',
|
outputLabel: 'YAML 结果',
|
||||||
emptyHint: '输入 JSON 后点击转换',
|
|
||||||
},
|
},
|
||||||
toml: {
|
toml: {
|
||||||
inputPlaceholder: '输入需要转换的 JSON...',
|
inputPlaceholder: '输入需要转换的 JSON...',
|
||||||
outputLabel: 'TOML 结果',
|
outputLabel: 'TOML 结果',
|
||||||
emptyHint: '输入 JSON 后点击转换',
|
|
||||||
},
|
},
|
||||||
minify: {
|
minify: {
|
||||||
inputPlaceholder: '输入需要压缩的 JSON...',
|
inputPlaceholder: '输入需要压缩的 JSON...',
|
||||||
outputLabel: '压缩结果',
|
outputLabel: '压缩结果',
|
||||||
emptyHint: '输入 JSON 后点击压缩',
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -40,6 +35,7 @@ export default function JsonConvertSection({
|
|||||||
}: JsonConvertSectionProps) {
|
}: JsonConvertSectionProps) {
|
||||||
const [input, setInput] = useState('');
|
const [input, setInput] = useState('');
|
||||||
const [debouncedInput, setDebouncedInput] = useState('');
|
const [debouncedInput, setDebouncedInput] = useState('');
|
||||||
|
const [inputCollapsed, setInputCollapsed] = useState(false);
|
||||||
|
|
||||||
const labels = CONVERT_LABELS[mode] ?? CONVERT_LABELS.yaml;
|
const labels = CONVERT_LABELS[mode] ?? CONVERT_LABELS.yaml;
|
||||||
|
|
||||||
@@ -71,32 +67,40 @@ export default function JsonConvertSection({
|
|||||||
}
|
}
|
||||||
}, [debouncedInput, error, convertFunction]);
|
}, [debouncedInput, error, convertFunction]);
|
||||||
|
|
||||||
|
const preview = useMemo(() => buildPreview(input), [input]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('w-full flex flex-col gap-4', className)} {...props}>
|
<div className={cn('w-full flex flex-1 min-h-0 flex-col gap-3', className)} {...props}>
|
||||||
|
<CollapsiblePanel
|
||||||
|
title="JSON 输入"
|
||||||
|
collapsed={inputCollapsed}
|
||||||
|
onToggleCollapse={() => setInputCollapsed((v) => !v)}
|
||||||
|
preview={preview}
|
||||||
|
>
|
||||||
<TextInputArea
|
<TextInputArea
|
||||||
|
fill
|
||||||
|
borderless
|
||||||
placeholder={labels.inputPlaceholder}
|
placeholder={labels.inputPlaceholder}
|
||||||
value={input}
|
value={input}
|
||||||
onChange={setInput}
|
onChange={setInput}
|
||||||
externalError={error || runtimeError || undefined}
|
externalError={error || runtimeError || undefined}
|
||||||
showClear={true}
|
showClear={true}
|
||||||
allowCopy={true}
|
allowCopy={true}
|
||||||
minRows={7}
|
className="min-h-0 flex-1"
|
||||||
maxRows={14}
|
|
||||||
onClear={() => setInput('')}
|
onClear={() => setInput('')}
|
||||||
/>
|
/>
|
||||||
|
</CollapsiblePanel>
|
||||||
|
|
||||||
{result?.output ? (
|
{result?.output && (
|
||||||
|
<div className="flex min-h-0 flex-1">
|
||||||
<JsonResultPanel
|
<JsonResultPanel
|
||||||
|
fill
|
||||||
title={labels.outputLabel}
|
title={labels.outputLabel}
|
||||||
content={result.output}
|
content={result.output}
|
||||||
originalBytes={result.originalBytes}
|
originalBytes={result.originalBytes}
|
||||||
outputBytes={result.outputBytes}
|
outputBytes={result.outputBytes}
|
||||||
maxHeight="380px"
|
|
||||||
/>
|
/>
|
||||||
) : (
|
</div>
|
||||||
<EmptyPlaceholder>
|
|
||||||
{error ? '请修正上方 JSON 的语法错误以开启实时流式格式化' : labels.emptyHint}
|
|
||||||
</EmptyPlaceholder>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { ChevronRight } from 'lucide-react';
|
|
||||||
import TextInputArea from '@/components/TextInputArea';
|
import TextInputArea from '@/components/TextInputArea';
|
||||||
|
import CollapsiblePanel from './CollapsiblePanel';
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
export interface JsonDiffPanelProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
|
export interface JsonDiffPanelProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
|
||||||
@@ -30,49 +30,18 @@ export default function JsonDiffPanel({
|
|||||||
...props
|
...props
|
||||||
}: JsonDiffPanelProps) {
|
}: JsonDiffPanelProps) {
|
||||||
return (
|
return (
|
||||||
<div
|
<CollapsiblePanel
|
||||||
|
title={title}
|
||||||
|
collapsed={collapsed}
|
||||||
|
onToggleCollapse={onToggleCollapse}
|
||||||
|
preview={preview}
|
||||||
className={cn(
|
className={cn(
|
||||||
'flex flex-col overflow-hidden rounded-md border bg-card transition-all duration-300 ease-in-out',
|
|
||||||
error
|
error
|
||||||
? 'border-destructive focus-within:ring-1 focus-within:ring-destructive focus-within:border-destructive'
|
? 'border-destructive focus-within:ring-1 focus-within:ring-destructive focus-within:border-destructive'
|
||||||
: 'border-border focus-within:ring-1 focus-within:ring-ring focus-within:border-border/80',
|
: 'border-border focus-within:ring-1 focus-within:ring-ring focus-within:border-border/80',
|
||||||
className,
|
className,
|
||||||
)}
|
)}
|
||||||
style={{
|
|
||||||
flex: collapsed ? '0 0 36px' : '1 1 120px',
|
|
||||||
}}
|
|
||||||
{...props}
|
{...props}
|
||||||
>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={onToggleCollapse}
|
|
||||||
className={cn(
|
|
||||||
'flex h-[36px] w-full cursor-pointer select-none items-center justify-between bg-muted/20 hover:bg-muted/30 transition-colors px-3 shrink-0 text-left focus:outline-none focus:bg-muted/40',
|
|
||||||
!collapsed && 'border-b border-border/60',
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<span className="text-[11px] font-semibold text-foreground/90 shrink-0">{title}</span>
|
|
||||||
<span
|
|
||||||
className={cn(
|
|
||||||
'flex-1 truncate font-mono text-xs text-muted-foreground/70 ml-3 transition-all duration-300',
|
|
||||||
collapsed ? 'opacity-100 max-w-full' : 'opacity-0 max-w-0 pointer-events-none',
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
{preview}
|
|
||||||
</span>
|
|
||||||
<ChevronRight
|
|
||||||
className={cn(
|
|
||||||
'h-4 w-4 shrink-0 text-muted-foreground hover:text-foreground transition-transform duration-300',
|
|
||||||
!collapsed && 'rotate-90',
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
<div
|
|
||||||
className={cn(
|
|
||||||
'flex-1 min-h-0 flex flex-col transition-opacity duration-300',
|
|
||||||
collapsed ? 'opacity-0 pointer-events-none' : 'opacity-100',
|
|
||||||
)}
|
|
||||||
>
|
>
|
||||||
<TextInputArea
|
<TextInputArea
|
||||||
fill
|
fill
|
||||||
@@ -85,7 +54,6 @@ export default function JsonDiffPanel({
|
|||||||
borderless
|
borderless
|
||||||
className="min-h-0 flex-1"
|
className="min-h-0 flex-1"
|
||||||
/>
|
/>
|
||||||
</div>
|
</CollapsiblePanel>
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,18 +5,20 @@ import {
|
|||||||
type JsonFormatResult,
|
type JsonFormatResult,
|
||||||
validateJson,
|
validateJson,
|
||||||
} from '@/utils/jsonFormatter';
|
} from '@/utils/jsonFormatter';
|
||||||
import EmptyPlaceholder from '@/components/EmptyPlaceholder';
|
|
||||||
import SwitchButtonGroup from '@/components/SwitchButtonGroup';
|
import SwitchButtonGroup from '@/components/SwitchButtonGroup';
|
||||||
import TextInputArea from '@/components/TextInputArea';
|
import TextInputArea from '@/components/TextInputArea';
|
||||||
|
import CollapsiblePanel from './CollapsiblePanel';
|
||||||
import JsonResultPanel from './JsonResultPanel';
|
import JsonResultPanel from './JsonResultPanel';
|
||||||
import { Checkbox } from '@/components/ui/checkbox';
|
import { Checkbox } from '@/components/ui/checkbox';
|
||||||
import { Label } from '@/components/ui/label';
|
import { Label } from '@/components/ui/label';
|
||||||
|
import { buildPreview } from '../useJsonTools';
|
||||||
|
|
||||||
export default function JsonFormatSection() {
|
export default function JsonFormatSection() {
|
||||||
const [input, setInput] = useState('');
|
const [input, setInput] = useState('');
|
||||||
const [debouncedInput, setDebouncedInput] = useState('');
|
const [debouncedInput, setDebouncedInput] = useState('');
|
||||||
const [indentSize, setIndentSize] = useState<number>(2);
|
const [indentSize, setIndentSize] = useState<number>(2);
|
||||||
const [sortKeys, setSortKeys] = useState(false);
|
const [sortKeys, setSortKeys] = useState(false);
|
||||||
|
const [inputCollapsed, setInputCollapsed] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handle = setTimeout(() => {
|
const handle = setTimeout(() => {
|
||||||
@@ -47,9 +49,11 @@ export default function JsonFormatSection() {
|
|||||||
}
|
}
|
||||||
}, [debouncedInput, error, indentSize, sortKeys]);
|
}, [debouncedInput, error, indentSize, sortKeys]);
|
||||||
|
|
||||||
|
const preview = useMemo(() => buildPreview(input), [input]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="w-full flex flex-col gap-4">
|
<div className="w-full flex flex-1 min-h-0 flex-col gap-3">
|
||||||
<div className="flex h-10 items-center justify-between px-1.5 bg-secondary/40 rounded-xl border border-border/60">
|
<div className="flex h-10 items-center justify-between px-1.5 bg-secondary/40 rounded-xl border border-border/60 shrink-0">
|
||||||
<div className="flex gap-4 items-center w-full">
|
<div className="flex gap-4 items-center w-full">
|
||||||
<div className="flex gap-2 items-center shrink-0 select-none">
|
<div className="flex gap-2 items-center shrink-0 select-none">
|
||||||
<span className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider">
|
<span className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider">
|
||||||
@@ -57,7 +61,7 @@ export default function JsonFormatSection() {
|
|||||||
</span>
|
</span>
|
||||||
<SwitchButtonGroup
|
<SwitchButtonGroup
|
||||||
value={indentSize}
|
value={indentSize}
|
||||||
onChange={(v) => setIndentSize(Number(v))}
|
onChange={(v: number) => setIndentSize(Number(v))}
|
||||||
options={[2, 4, 6, 8].map((size) => ({ value: size, label: String(size) }))}
|
options={[2, 4, 6, 8].map((size) => ({ value: size, label: String(size) }))}
|
||||||
size="small"
|
size="small"
|
||||||
/>
|
/>
|
||||||
@@ -86,29 +90,36 @@ export default function JsonFormatSection() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<CollapsiblePanel
|
||||||
|
title="JSON 输入"
|
||||||
|
collapsed={inputCollapsed}
|
||||||
|
onToggleCollapse={() => setInputCollapsed((v) => !v)}
|
||||||
|
preview={preview}
|
||||||
|
>
|
||||||
<TextInputArea
|
<TextInputArea
|
||||||
|
fill
|
||||||
|
borderless
|
||||||
placeholder="输入需要格式化的 JSON..."
|
placeholder="输入需要格式化的 JSON..."
|
||||||
value={input}
|
value={input}
|
||||||
onChange={setInput}
|
onChange={setInput}
|
||||||
externalError={error || runtimeError || undefined}
|
externalError={error || runtimeError || undefined}
|
||||||
showClear={true}
|
showClear={true}
|
||||||
allowCopy={true}
|
allowCopy={true}
|
||||||
minRows={8}
|
className="min-h-0 flex-1"
|
||||||
maxRows={15}
|
|
||||||
onClear={() => setInput('')}
|
onClear={() => setInput('')}
|
||||||
/>
|
/>
|
||||||
|
</CollapsiblePanel>
|
||||||
|
|
||||||
{result?.formatted ? (
|
{result?.formatted && (
|
||||||
|
<div className="flex min-h-0 flex-1">
|
||||||
<JsonResultPanel
|
<JsonResultPanel
|
||||||
|
fill
|
||||||
title="格式化结果"
|
title="格式化结果"
|
||||||
content={result.formatted}
|
content={result.formatted}
|
||||||
originalBytes={result.originalBytes}
|
originalBytes={result.originalBytes}
|
||||||
outputBytes={result.formattedBytes}
|
outputBytes={result.formattedBytes}
|
||||||
/>
|
/>
|
||||||
) : (
|
</div>
|
||||||
<EmptyPlaceholder>
|
|
||||||
{error ? '请修正上方 JSON 的语法错误以开启实时流式格式化' : '输入 JSON 后点击格式化'}
|
|
||||||
</EmptyPlaceholder>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { formatBytes } from '@/utils/format';
|
import { formatBytes } from '@/utils/format';
|
||||||
import { CopyButton } from '@/components/CopyButton';
|
import { CopyButton } from '@/components/CopyButton';
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
export interface JsonResultPanelProps {
|
export interface JsonResultPanelProps {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -8,6 +9,11 @@ export interface JsonResultPanelProps {
|
|||||||
outputBytes: number;
|
outputBytes: number;
|
||||||
outputSizeLabel?: string;
|
outputSizeLabel?: string;
|
||||||
maxHeight?: string;
|
maxHeight?: string;
|
||||||
|
/**
|
||||||
|
* 撑满父容器剩余高度并启用内部滚动(而非按 maxHeight 截断)。
|
||||||
|
* 用于 popup / sidepanel 等固定高度场景下与折叠输入面板共享垂直空间。
|
||||||
|
*/
|
||||||
|
fill?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function JsonResultPanel({
|
export default function JsonResultPanel({
|
||||||
@@ -17,9 +23,15 @@ export default function JsonResultPanel({
|
|||||||
outputBytes,
|
outputBytes,
|
||||||
outputSizeLabel = '格式化后大小',
|
outputSizeLabel = '格式化后大小',
|
||||||
maxHeight = '420px',
|
maxHeight = '420px',
|
||||||
|
fill = false,
|
||||||
}: JsonResultPanelProps) {
|
}: JsonResultPanelProps) {
|
||||||
return (
|
return (
|
||||||
<div className="relative rounded-xl border border-border bg-card text-card-foreground shadow-sm overflow-hidden">
|
<div
|
||||||
|
className={cn(
|
||||||
|
'relative rounded-xl border border-border bg-card text-card-foreground shadow-sm overflow-hidden',
|
||||||
|
fill ? 'flex flex-col flex-1 min-h-0' : 'flex flex-col',
|
||||||
|
)}
|
||||||
|
>
|
||||||
<div className="flex h-9 items-center justify-between px-4 border-b border-border bg-muted/50 select-none">
|
<div className="flex h-9 items-center justify-between px-4 border-b border-border bg-muted/50 select-none">
|
||||||
<div className="flex gap-4 items-center">
|
<div className="flex gap-4 items-center">
|
||||||
<span className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/90">
|
<span className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/90">
|
||||||
@@ -43,8 +55,11 @@ export default function JsonResultPanel({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
className="p-4 font-mono text-xs text-foreground/90 whitespace-pre-wrap break-all overflow-y-auto leading-relaxed select-text"
|
className={cn(
|
||||||
style={{ maxHeight }}
|
'p-4 font-mono text-xs text-foreground/90 whitespace-pre-wrap break-all overflow-y-auto leading-relaxed select-text',
|
||||||
|
fill && 'flex-1 min-h-0',
|
||||||
|
)}
|
||||||
|
style={fill ? undefined : { maxHeight }}
|
||||||
>
|
>
|
||||||
{content}
|
{content}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||||
|
import userEvent from '@testing-library/user-event';
|
||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import JsonFormatSection from '../JsonFormatSection';
|
||||||
|
|
||||||
|
describe('JsonFormatSection', () => {
|
||||||
|
it('渲染输入面板标题与缩进/键名排序工具栏', () => {
|
||||||
|
render(<JsonFormatSection />);
|
||||||
|
expect(screen.getByText('JSON 输入')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('缩进')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('键名排序')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('输入无效 JSON 时不渲染结果面板与占位区', async () => {
|
||||||
|
render(<JsonFormatSection />);
|
||||||
|
const input = screen.getByPlaceholderText('输入需要格式化的 JSON...');
|
||||||
|
fireEvent.change(input, { target: { value: '{ invalid json ' } });
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.queryByText('格式化结果')).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
screen.queryByText('请修正上方 JSON 的语法错误以开启实时流式格式化'),
|
||||||
|
).not.toBeInTheDocument();
|
||||||
|
expect(screen.queryByText('输入 JSON 后点击格式化')).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('输入有效 JSON 后渲染格式化结果面板', async () => {
|
||||||
|
render(<JsonFormatSection />);
|
||||||
|
const input = screen.getByPlaceholderText('输入需要格式化的 JSON...');
|
||||||
|
fireEvent.change(input, { target: { value: '{"a":1,"b":2}' } });
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getByText('格式化结果')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
expect(screen.getByText(/"a": 1/)).toBeInTheDocument();
|
||||||
|
expect(screen.getByText(/"b": 2/)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('点击标题可折叠输入面板', async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
render(<JsonFormatSection />);
|
||||||
|
const input = screen.getByPlaceholderText('输入需要格式化的 JSON...');
|
||||||
|
fireEvent.change(input, { target: { value: '{"a":1}' } });
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getByText('格式化结果')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
await user.click(screen.getByText('JSON 输入'));
|
||||||
|
|
||||||
|
// 折叠后输入框内容区隐藏
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(input.closest('.opacity-0')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
// 折叠态展示单行预览
|
||||||
|
expect(screen.getByText('{"a":1}(点击展开)')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -48,7 +48,7 @@ export interface UseJsonToolsReturn {
|
|||||||
minifyConvert: ConvertFunction;
|
minifyConvert: ConvertFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
const buildPreview = (raw: string): string => {
|
export const buildPreview = (raw: string): string => {
|
||||||
const single = raw.replace(/\s+/g, ' ').trim();
|
const single = raw.replace(/\s+/g, ' ').trim();
|
||||||
const truncated = single.length > 80 ? `${single.slice(0, 80)}…` : single;
|
const truncated = single.length > 80 ? `${single.slice(0, 80)}…` : single;
|
||||||
return truncated ? `${truncated}(点击展开)` : '(点击展开)';
|
return truncated ? `${truncated}(点击展开)` : '(点击展开)';
|
||||||
|
|||||||
Reference in New Issue
Block a user