Implemented Phases 2-4 of the implementation plan: **Phase 2: Font Management System** - Created font loading service with caching - Added API route to list all 373 figlet fonts - Implemented font metadata types **Phase 3: Core Figlet Engine** - Built figlet.js wrapper service for ASCII art generation - Added async/sync rendering methods - Implemented debounced text updates (300ms) - Created utility functions (cn, debounce) **Phase 4: Main UI Components** - Built reusable UI primitives (Button, Input, Card) - Created TextInput component with character counter (100 char limit) - Implemented FontPreview with loading states - Added FontSelector with real-time search - Built main FigletConverter orchestrating all components **Features Implemented:** - Live preview with 300ms debounce - 373 fonts from xero/figlet-fonts collection - Fuzzy font search - Copy to clipboard - Download as .txt file - Responsive 3-column layout (mobile-friendly) - Character counter - Loading states - Empty states **Tech Stack:** - Next.js 16 App Router with Turbopack - React 19 with client components - TypeScript with strict types - Tailwind CSS 4 for styling - figlet.js for rendering - Font caching for performance The application is fully functional and ready for testing! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
110 lines
3.1 KiB
TypeScript
110 lines
3.1 KiB
TypeScript
'use client';
|
|
|
|
import * as React from 'react';
|
|
import { TextInput } from './TextInput';
|
|
import { FontPreview } from './FontPreview';
|
|
import { FontSelector } from './FontSelector';
|
|
import { textToAscii } from '@/lib/figlet/figletService';
|
|
import { getFontList } from '@/lib/figlet/fontLoader';
|
|
import { debounce } from '@/lib/utils/debounce';
|
|
import type { FigletFont } from '@/types/figlet';
|
|
|
|
export function FigletConverter() {
|
|
const [text, setText] = React.useState('Figlet UI');
|
|
const [selectedFont, setSelectedFont] = React.useState('Standard');
|
|
const [asciiArt, setAsciiArt] = React.useState('');
|
|
const [fonts, setFonts] = React.useState<FigletFont[]>([]);
|
|
const [isLoading, setIsLoading] = React.useState(false);
|
|
const [isCopied, setIsCopied] = React.useState(false);
|
|
|
|
// Load fonts on mount
|
|
React.useEffect(() => {
|
|
getFontList().then(setFonts);
|
|
}, []);
|
|
|
|
// Generate ASCII art
|
|
const generateAsciiArt = React.useCallback(
|
|
debounce(async (inputText: string, fontName: string) => {
|
|
if (!inputText) {
|
|
setAsciiArt('');
|
|
setIsLoading(false);
|
|
return;
|
|
}
|
|
|
|
setIsLoading(true);
|
|
try {
|
|
const result = await textToAscii(inputText, fontName);
|
|
setAsciiArt(result);
|
|
} catch (error) {
|
|
console.error('Error generating ASCII art:', error);
|
|
setAsciiArt('Error generating ASCII art. Please try a different font.');
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
}, 300),
|
|
[]
|
|
);
|
|
|
|
// Trigger generation when text or font changes
|
|
React.useEffect(() => {
|
|
generateAsciiArt(text, selectedFont);
|
|
}, [text, selectedFont, generateAsciiArt]);
|
|
|
|
// Copy to clipboard
|
|
const handleCopy = async () => {
|
|
if (!asciiArt) return;
|
|
|
|
try {
|
|
await navigator.clipboard.writeText(asciiArt);
|
|
setIsCopied(true);
|
|
setTimeout(() => setIsCopied(false), 2000);
|
|
} catch (error) {
|
|
console.error('Failed to copy:', error);
|
|
}
|
|
};
|
|
|
|
// Download as text file
|
|
const handleDownload = () => {
|
|
if (!asciiArt) return;
|
|
|
|
const blob = new Blob([asciiArt], { type: 'text/plain' });
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = `figlet-${selectedFont}-${Date.now()}.txt`;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
URL.revokeObjectURL(url);
|
|
};
|
|
|
|
return (
|
|
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
|
|
{/* Left Column - Input and Preview */}
|
|
<div className="lg:col-span-2 space-y-6">
|
|
<TextInput
|
|
value={text}
|
|
onChange={setText}
|
|
placeholder="Type your text here..."
|
|
/>
|
|
|
|
<FontPreview
|
|
text={isCopied ? 'Copied to clipboard! ✓' : asciiArt}
|
|
isLoading={isLoading}
|
|
onCopy={handleCopy}
|
|
onDownload={handleDownload}
|
|
/>
|
|
</div>
|
|
|
|
{/* Right Column - Font Selector */}
|
|
<div className="lg:col-span-1">
|
|
<FontSelector
|
|
fonts={fonts}
|
|
selectedFont={selectedFont}
|
|
onSelectFont={setSelectedFont}
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|