70160
Ce
cel-tui / files
cel-tui / examples / markdown-editor.ts
11.1 KB
1/** 2 * cel-tui: Live Markdown Editor 3 * 4 * A split-pane markdown editor with live preview, showcasing: 5 * - Custom color theme (Catppuccin Mocha via true color) 6 * - Large multi-line TextInput editor 7 * - Live Markdown rendering (source ↔ preview) 8 * - Split-pane layout with VDivider 9 * - Scrollable preview pane 10 * - Focus management between editor and UI 11 * 12 * Run: bun run examples/markdown-editor.ts 13 */ 14 15import { 16 Markdown, 17 type MarkdownTheme, 18 Spacer, 19 VDivider, 20} from "@cel-tui/components"; 21import { 22 cel, 23 HStack, 24 ProcessTerminal, 25 Text, 26 TextInput, 27 type Theme, 28 VStack, 29} from "@cel-tui/core"; 30 31// ─── Theme: Catppuccin Mocha ──────────────────────────────────── 32 33const mocha: Theme = { 34 color00: "#1e1e2e", // base (dark bg) 35 color01: "#f38ba8", // red 36 color02: "#a6e3a1", // green 37 color03: "#f9e2af", // yellow 38 color04: "#89b4fa", // blue 39 color05: "#cba6f7", // mauve 40 color06: "#94e2d5", // teal 41 color07: "#cdd6f4", // text 42 color08: "#45475a", // surface1 (muted) 43 color09: "#f38ba8", // bright red 44 color10: "#a6e3a1", // bright green 45 color11: "#f9e2af", // bright yellow 46 color12: "#89b4fa", // bright blue 47 color13: "#cba6f7", // bright mauve 48 color14: "#94e2d5", // bright teal 49 color15: "#bac2de", // subtext1 50}; 51 52const mdTheme: MarkdownTheme = { 53 heading1: { bold: true, fgColor: "color05" }, 54 heading2: { bold: true, fgColor: "color04" }, 55 heading3: { bold: true, fgColor: "color06" }, 56 codeBlock: { bgColor: "color08", fgColor: "color07" }, 57 codeContent: { fgColor: "color07" }, 58 listMarker: { fgColor: "color05", bold: true }, 59 blockquoteBar: { fgColor: "color06" }, 60 blockquoteText: { italic: true, fgColor: "color15" }, 61 hr: { fgColor: "color08" }, 62 bold: { bold: true, fgColor: "color03" }, 63 italic: { italic: true }, 64 inlineCode: { fgColor: "color02" }, 65 link: { fgColor: "color04", underline: true }, 66}; 67 68// ─── Sample Documents ─────────────────────────────────────────── 69 70const DOCUMENTS: { name: string; content: string }[] = [ 71 { 72 name: "README.md", 73 content: `# Project Aurora 74 75A next-generation toolkit for building **terminal user interfaces** with style. 76 77## Features 78 79- **Declarative API** — describe your UI as a tree of components 80- **Flexbox layout** — stack, align, and distribute with familiar primitives 81- **True color themes** — go beyond the ANSI 16 palette 82- **Reactive rendering** — only re-render what changed 83 84## Getting Started 85 86Install the package: 87 88\`\`\`bash 89bun add @cel-tui/core @cel-tui/components 90\`\`\` 91 92Create your first app: 93 94\`\`\`ts 95import { cel, VStack, Text, ProcessTerminal } from "@cel-tui/core"; 96 97cel.init(new ProcessTerminal()); 98cel.viewport(() => 99 VStack({ height: "100%" }, [ 100 Text("Hello, world!", { bold: true }), 101 ]) 102); 103\`\`\` 104 105## Architecture 106 107The framework is built around **four primitives**: 108 1091. **VStack** — vertical layout container 1102. **HStack** — horizontal layout container 1113. **Text** — styled text leaf node 1124. **TextInput** — editable text container 113 114> All state is external. The framework just renders whatever tree you return. 115 116--- 117 118*Built with love for the terminal.*`, 119 }, 120 { 121 name: "CHANGELOG.md", 122 content: `# Changelog 123 124All notable changes to this project will be documented in this file. 125 126## [0.3.0] — 2026-04-05 127 128### Added 129 130- **True color theme support** — custom themes can now map color slots to 24-bit hex values 131- **Autogrowing TextInput** — \`maxHeight\` constraint works on the cross axis 132- **Native blinking cursor** — terminal cursor positioned at the focused TextInput 133 134### Fixed 135 136- Cursor position preserved correctly when value is cleared externally 137- Grapheme-aware editing — emoji and ZWJ sequences treated as single units 138 139### Changed 140 141- \`text-edit.ts\` now uses \`Intl.Segmenter\` for all cursor movement 142 143--- 144 145## [0.2.0] — 2026-03-15 146 147### Added 148 149- **Markdown component** with streaming support 150- **flexWrap** for HStack — children wrap to next row when they exceed width 151- **Select component** with fuzzy filtering 152 153### Fixed 154 155- Scroll offset clamping for \`Infinity\` (sticky-bottom pattern) 156 157> See the full history on [GitHub](https://github.com/example/aurora). 158 159--- 160 161## [0.1.0] — 2026-02-01 162 163### Added 164 165- Initial release 166- VStack, HStack, Text, TextInput primitives 167- Flexbox layout engine 168- Cell buffer rendering with differential updates 169- Kitty keyboard protocol support 170 171*First public release — here we go!*`, 172 }, 173 { 174 name: "DESIGN.md", 175 content: `# Design Notes 176 177## Color Philosophy 178 179The framework uses **16 numbered color slots** (\`color00\`–\`color15\`) rather than named colors like "red" or "white". This is intentional: 180 181> Named colors are deceptive. "white" on a dark theme is bright, but on a light theme it's the background color. Numbered slots make the abstraction explicit. 182 183### Slot Conventions 184 185| Range | Purpose | 186|-------|---------| 187| \`color00\`–\`color07\` | Base palette (dark to light) | 188| \`color08\`–\`color15\` | Bright variants | 189 190### Theme Example 191 192A **Catppuccin Mocha** theme maps slots to true color: 193 194\`\`\`ts 195const mocha: Theme = { 196 color00: "#1e1e2e", // base 197 color01: "#f38ba8", // red 198 color02: "#a6e3a1", // green 199 // ... 200}; 201\`\`\` 202 203## Layout Model 204 205The layout engine is inspired by **CSS Flexbox** but simplified for terminal constraints: 206 207- All coordinates are integer cells (no subpixel) 208- The **Largest Remainder Method** distributes fractional space fairly 209- **Intrinsic sizing** measures content before flex distribution 210 211### Key Differences from CSS 212 2131. No \`flex-shrink\` — children never shrink below intrinsic size 2142. No \`flex-basis\` — use \`width\`/\`height\` or \`minWidth\`/\`minHeight\` 2153. **Gap** is cell-based, not relative 2164. **Percentage** sizes resolve against the parent, not the viewport 217 218--- 219 220*These notes are living documentation — they evolve with the codebase.*`, 221 }, 222]; 223 224// ─── State ────────────────────────────────────────────────────── 225 226let activeDocIndex = 0; 227let editorFocused = true; 228let source = DOCUMENTS[0]!.content; 229let previewScroll = 0; 230let previewStickToBottom = false; 231 232// ─── Helpers ──────────────────────────────────────────────────── 233 234function activeDoc() { 235 return DOCUMENTS[activeDocIndex]!; 236} 237 238function switchDoc(index: number) { 239 // Save current edits 240 DOCUMENTS[activeDocIndex]!.content = source; 241 activeDocIndex = index; 242 source = DOCUMENTS[index]!.content; 243 previewScroll = 0; 244 previewStickToBottom = false; 245 cel.render(); 246} 247 248function handleChange(value: string) { 249 source = value; 250 cel.render(); 251} 252 253function quit() { 254 cel.stop(); 255 process.exit(0); 256} 257 258// ─── UI ───────────────────────────────────────────────────────── 259 260cel.init(new ProcessTerminal(), { theme: mocha }); 261 262cel.viewport(() => 263 VStack( 264 { 265 height: "100%", 266 bgColor: "color00", 267 fgColor: "color07", 268 onKeyPress: (key) => { 269 if (key === "ctrl+q" || key === "ctrl+c") quit(); 270 if (key === "ctrl+n") { 271 switchDoc((activeDocIndex + 1) % DOCUMENTS.length); 272 return; 273 } 274 if (key === "ctrl+p") { 275 switchDoc((activeDocIndex - 1 + DOCUMENTS.length) % DOCUMENTS.length); 276 return; 277 } 278 return false; 279 }, 280 }, 281 [ 282 // ── Title bar ── 283 HStack( 284 { 285 height: 1, 286 padding: { x: 1 }, 287 bgColor: "color05", 288 fgColor: "color00", 289 }, 290 [ 291 Text(" ✎ Markdown Editor ", { bold: true }), 292 Spacer(), 293 Text(activeDoc().name, { italic: true }), 294 ], 295 ), 296 297 // ── Tab bar ── 298 HStack({ height: 1, bgColor: "color08", fgColor: "color07" }, [ 299 ...DOCUMENTS.map((doc, i) => { 300 const isActive = i === activeDocIndex; 301 return HStack( 302 { 303 onClick: () => switchDoc(i), 304 focusable: false, 305 bgColor: isActive ? "color00" : "color08", 306 fgColor: isActive ? "color07" : "color15", 307 padding: { x: 1 }, 308 }, 309 [Text(doc.name, isActive ? { bold: true } : {})], 310 ); 311 }), 312 Spacer(), 313 Text(" ctrl+n/p: switch ", { fgColor: "color15" }), 314 ]), 315 316 // ── Main content: split pane ── 317 HStack({ flex: 1 }, [ 318 // Left: source editor 319 VStack({ flex: 1 }, [ 320 HStack( 321 { 322 height: 1, 323 padding: { x: 1 }, 324 bgColor: "color08", 325 fgColor: "color06", 326 }, 327 [ 328 Text("SOURCE", { bold: true }), 329 Spacer(), 330 Text(`${source.split("\n").length} lines`, { 331 fgColor: "color15", 332 }), 333 ], 334 ), 335 TextInput({ 336 flex: 1, 337 value: source, 338 onChange: handleChange, 339 padding: { x: 1 }, 340 focused: editorFocused, 341 onFocus: () => { 342 editorFocused = true; 343 cel.render(); 344 }, 345 onBlur: () => { 346 editorFocused = false; 347 cel.render(); 348 }, 349 bgColor: "color00", 350 fgColor: "color07", 351 focusStyle: { bgColor: "color00" }, 352 }), 353 ]), 354 355 // Divider 356 VDivider({ fgColor: "color08" }), 357 358 // Right: live preview 359 VStack({ flex: 1 }, [ 360 HStack( 361 { 362 height: 1, 363 padding: { x: 1 }, 364 bgColor: "color08", 365 fgColor: "color05", 366 }, 367 [Text("PREVIEW", { bold: true }), Spacer(), Text("live")], 368 ), 369 VStack( 370 { 371 flex: 1, 372 overflow: "scroll", 373 scrollbar: true, 374 padding: { x: 1, y: 1 }, 375 scrollOffset: previewStickToBottom ? Infinity : previewScroll, 376 onScroll: (offset, maxOffset) => { 377 previewScroll = offset; 378 previewStickToBottom = offset >= maxOffset; 379 cel.render(); 380 }, 381 }, 382 Markdown(source, { theme: mdTheme }), 383 ), 384 ]), 385 ]), 386 387 // ── Status bar ── 388 HStack( 389 { 390 height: 1, 391 padding: { x: 1 }, 392 bgColor: "color08", 393 fgColor: "color15", 394 }, 395 [ 396 Text(editorFocused ? " EDIT " : " VIEW ", { 397 bgColor: editorFocused ? "color02" : "color04", 398 fgColor: "color00", 399 bold: true, 400 }), 401 Text(` ${activeDoc().name} `, { fgColor: "color07" }), 402 Text(`${source.length} chars`, { fgColor: "color15" }), 403 Spacer(), 404 Text("esc: blur ", { fgColor: "color15" }), 405 Text("ctrl+q: quit", { fgColor: "color15" }), 406 ], 407 ), 408 ], 409 ), 410);