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