import { createMemo, createSignal, For, onCleanup, onMount, Show } from 'solid-js'; import { EXAMPLES, type ExampleInfo } from '../../src/model/examples'; import { thumbnail, type Thumbnail } from '../live/thumbnail'; import { REOPEN_HINT } from '../reopen'; export const BLANK = `diagram "Untitled" version=1 node start "Start" node next "Next step" edge go start -> next `; /** The parts of `text` that match `query`, for highlighting. */ function marked(text: string, query: string) { if (!query) return [{ text, hit: false }]; const at = text.toLowerCase().indexOf(query.toLowerCase()); if (at < 0) return [{ text, hit: false }]; return [ { text: text.slice(0, at), hit: false }, { text: text.slice(at, at + query.length), hit: true }, { text: text.slice(at + query.length), hit: false }, ]; } function Marked(props: { text: string; query: string }) { return ( {part => (part.hit ? {part.text} : part.text)} ); } /** * Picks a starting diagram: New starts a blank one, and a button naming the current one opens a * searchable gallery of every example, drawn as thumbnails. */ export function ExamplePicker(props: { current: string; title: string; onPick: (id: string) => void; onOpenFile: (file: File) => void; }) { const [open, setOpen] = createSignal(false); const [query, setQuery] = createSignal(''); let wrap!: HTMLDivElement; let input!: HTMLInputElement; let button!: HTMLButtonElement; const shown = createMemo(() => { const q = query().trim().toLowerCase(); return q ? EXAMPLES.filter(e => `${e.title} ${e.summary} ${e.id}`.toLowerCase().includes(q)) : EXAMPLES; }); const pick = (id: string) => { setOpen(false); setQuery(''); props.onPick(id); }; const toggle = () => { setOpen(o => !o); if (open()) queueMicrotask(() => input.focus()); }; const outside = (e: PointerEvent) => { if (!wrap.contains(e.target as Node)) setOpen(false); }; onMount(() => document.addEventListener('pointerdown', outside)); onCleanup(() => document.removeEventListener('pointerdown', outside)); return ( <>
); } function Card(props: { example: ExampleInfo; query: string; current: boolean; onPick: (id: string) => void; }) { const [thumb, setThumb] = createSignal(null); onMount(() => void thumbnail(props.example.id, props.example.source).then(setThumb)); return ( ); }