Spaces:
Running
Running
File size: 10,761 Bytes
24d6dda |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
"use client";
import { useState } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@hanzo/ui/primitives/card";
import { Button } from "@hanzo/ui/primitives/button";
import { Textarea } from "@hanzo/ui/primitives/textarea";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@hanzo/ui/primitives/tabs";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@hanzo/ui/primitives/select";
import { Separator } from "@hanzo/ui/primitives/separator";
import { ScrollArea } from "@hanzo/ui/primitives/scroll-area";
import {
Bold,
Italic,
Link,
List,
ListOrdered,
Quote,
Code,
Heading1,
Heading2,
Image,
Table,
Download,
Copy,
Eye,
FileText,
Save
} from "lucide-react";
export default function MarkdownEditor() {
const [markdown, setMarkdown] = useState(`# Welcome to Hanzo Markdown Editor
Built with **@hanzo/ui components** for a seamless writing experience.
## Features
- π **Live Preview** - See your changes in real-time
- π¨ **Syntax Highlighting** - Beautiful code blocks
- π± **Responsive Design** - Works on all devices
- π **Dark Mode Support** - Easy on the eyes
## Code Example
\`\`\`typescript
import { Card } from "@hanzo/ui/primitives/card";
import { Button } from "@hanzo/ui/primitives/button";
export function MyComponent() {
return (
<Card>
<CardContent>
<Button>Click me!</Button>
</CardContent>
</Card>
);
}
\`\`\`
## Lists
### Unordered List
- First item
- Second item
- Third item
### Ordered List
1. Step one
2. Step two
3. Step three
## Blockquote
> "The best way to predict the future is to invent it."
> - Alan Kay
## Table
| Feature | Status | Priority |
|---------|--------|----------|
| Live Preview | β
Complete | High |
| Export Options | β
Complete | Medium |
| Collaboration | π§ In Progress | Low |
---
Start writing your content above!
`);
const [viewMode, setViewMode] = useState("split");
const insertMarkdown = (before: string, after: string = "") => {
const textarea = document.getElementById("markdown-input") as HTMLTextAreaElement;
if (!textarea) return;
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const selectedText = markdown.substring(start, end);
const newText = before + selectedText + after;
const newMarkdown =
markdown.substring(0, start) +
newText +
markdown.substring(end);
setMarkdown(newMarkdown);
// Restore cursor position
setTimeout(() => {
textarea.focus();
textarea.setSelectionRange(
start + before.length,
start + before.length + selectedText.length
);
}, 0);
};
const renderMarkdown = (text: string) => {
// Simple markdown to HTML conversion (in production, use a proper markdown parser)
let html = text
.replace(/^### (.*$)/gim, '<h3 class="text-lg font-semibold mt-4 mb-2">$1</h3>')
.replace(/^## (.*$)/gim, '<h2 class="text-xl font-bold mt-6 mb-3">$1</h2>')
.replace(/^# (.*$)/gim, '<h1 class="text-2xl font-bold mt-6 mb-4">$1</h1>')
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
.replace(/\*(.+?)\*/g, '<em>$1</em>')
.replace(/```(\w+)?\n([\s\S]*?)```/g, '<pre class="bg-muted p-4 rounded-lg my-4 overflow-x-auto"><code>$2</code></pre>')
.replace(/`(.+?)`/g, '<code class="bg-muted px-1 py-0.5 rounded text-sm">$1</code>')
.replace(/^> (.*$)/gim, '<blockquote class="border-l-4 border-blue-500 pl-4 my-4 italic">$1</blockquote>')
.replace(/^- (.*$)/gim, '<li class="ml-6">β’ $1</li>')
.replace(/^\d+\. (.*$)/gim, '<li class="ml-6">$1</li>')
.replace(/\[(.+?)\]\((.+?)\)/g, '<a href="$2" class="text-blue-600 hover:underline">$1</a>')
.replace(/\n\n/g, '</p><p class="mb-4">')
.replace(/^---$/gim, '<hr class="my-6 border-t">');
return `<div class="prose prose-sm max-w-none"><p class="mb-4">${html}</p></div>`;
};
return (
<div className="min-h-screen bg-background">
{/* Header Toolbar */}
<div className="border-b">
<div className="container mx-auto px-6 py-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-4">
<h1 className="text-xl font-bold">Markdown Editor</h1>
<Separator orientation="vertical" className="h-6" />
<div className="flex items-center gap-1">
<Button
variant="ghost"
size="icon"
onClick={() => insertMarkdown("**", "**")}
title="Bold"
>
<Bold className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => insertMarkdown("*", "*")}
title="Italic"
>
<Italic className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => insertMarkdown("# ")}
title="Heading 1"
>
<Heading1 className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => insertMarkdown("## ")}
title="Heading 2"
>
<Heading2 className="w-4 h-4" />
</Button>
<Separator orientation="vertical" className="h-6 mx-1" />
<Button
variant="ghost"
size="icon"
onClick={() => insertMarkdown("[", "](url)")}
title="Link"
>
<Link className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => insertMarkdown("")}
title="Image"
>
<Image className="w-4 h-4" />
</Button>
<Separator orientation="vertical" className="h-6 mx-1" />
<Button
variant="ghost"
size="icon"
onClick={() => insertMarkdown("- ")}
title="Bullet List"
>
<List className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => insertMarkdown("1. ")}
title="Numbered List"
>
<ListOrdered className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => insertMarkdown("> ")}
title="Quote"
>
<Quote className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => insertMarkdown("```\n", "\n```")}
title="Code Block"
>
<Code className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => insertMarkdown("| Column 1 | Column 2 |\n|----------|----------|\n| ", " | |")}
title="Table"
>
<Table className="w-4 h-4" />
</Button>
</div>
</div>
<div className="flex items-center gap-2">
<Select value={viewMode} onValueChange={setViewMode}>
<SelectTrigger className="w-32">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="edit">Edit Only</SelectItem>
<SelectItem value="split">Split View</SelectItem>
<SelectItem value="preview">Preview Only</SelectItem>
</SelectContent>
</Select>
<Button variant="outline" size="sm">
<Copy className="w-4 h-4 mr-2" />
Copy
</Button>
<Button variant="outline" size="sm">
<Download className="w-4 h-4 mr-2" />
Export
</Button>
<Button size="sm" className="bg-blue-600 hover:bg-blue-700">
<Save className="w-4 h-4 mr-2" />
Save
</Button>
</div>
</div>
</div>
</div>
{/* Editor Area */}
<div className="flex h-[calc(100vh-4rem)]">
{/* Editor Panel */}
{(viewMode === "edit" || viewMode === "split") && (
<div className={viewMode === "split" ? "w-1/2 border-r" : "w-full"}>
<ScrollArea className="h-full">
<Textarea
id="markdown-input"
value={markdown}
onChange={(e) => setMarkdown(e.target.value)}
className="min-h-full p-6 resize-none border-0 focus-visible:ring-0 font-mono text-sm"
placeholder="Start writing in markdown..."
/>
</ScrollArea>
</div>
)}
{/* Preview Panel */}
{(viewMode === "preview" || viewMode === "split") && (
<div className={viewMode === "split" ? "w-1/2" : "w-full"}>
<ScrollArea className="h-full">
<Card className="border-0 rounded-none">
<CardContent className="p-6">
<div
className="markdown-preview"
dangerouslySetInnerHTML={{ __html: renderMarkdown(markdown) }}
/>
</CardContent>
</Card>
</ScrollArea>
</div>
)}
</div>
{/* Status Bar */}
<div className="border-t">
<div className="container mx-auto px-6 py-2">
<div className="flex items-center justify-between text-xs text-muted-foreground">
<div className="flex items-center gap-4">
<span>{markdown.length} characters</span>
<span>{markdown.split(/\s+/).filter(w => w).length} words</span>
<span>{markdown.split("\n").length} lines</span>
</div>
<div className="flex items-center gap-2">
<FileText className="w-3 h-3" />
<span>Markdown</span>
</div>
</div>
</div>
</div>
</div>
);
} |