components
All repositories: gitoria
2.6 KB
// demo.hl — `/md-editor`: two editors on one page (the second starts empty and shows its placeholder), and the Markdown each holds// shown below. The editor announces a change with a bubbling DOM `input` event on its hidden field: `e.target.value` is the Markdown.parent '../main.hl'import MdEditor from './md-editor.hl'import { writeFile, mkDir, listDir, remove } from 'hl:fs'import { randomBytes } from 'hl:crypto'import { now } from 'hl:time'__title = 'Markdown editor · Hybriel components'static NL = ""String note = '# Hello' + NL + NL + 'Some **bold** and *italic* text with `code` and a [link](https://example.com).' + NL + NL + '- first' + NL + '- second'String summary = ''View {mdEditorDemo {h1 { "Markdown editor" }p { class = "muted" "Edit it visually: headings look like headings, bold is bold, lists are lists. The value below each editor is plain Markdown; paste Markdown in and it shows formatted." }form { id = "md-form" method = "get" action = "/md-editor" on input(e) { emit demoValue(e) }MdEditor { name = "note" value = note }h2 { "Value of “note”" }pre { id = "md-value" note }MdEditor { name = "summary" value = summary placeholder = "A second editor, empty: type here…" }h2 { "Value of “summary”" }pre { id = "md-value-summary" summary }p { button { type = "submit" "Send (posts both as Markdown)" } }}p { a { id = "readme" href = "/md-editor/README.md" "README (how to use it)" } }}}on demoValue(e) {if (e.target.name == 'note') { note = e.target.value }if (e.target.name == 'summary') { summary = e.target.value }}// WHERE THE FILES GO IS THE APP'S DECISION: the editor sends dropped / pasted / chosen files here (`emit server mdUpload`), this demo// keeps them in storage/md-files (served at /md-editor/files/…, see project.hl) under a random name, only with a known kind of// extension (nothing a browser would run), for one day. It answers the address of each file, in order.static kinds = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'pdf', 'txt', 'md', 'zip']on server mdUpload(files) {mkDir('storage/md-files')let urls = []for (f of files) {let dot = f.name.lastIndexOf('.')let ext = dot >= 0 ? f.name.slice(dot + 1).toLowerCase() : ''let known = falsefor (k of kinds) { if (k == ext) { known = true } }let stored = now() + '-' + randomBytes(6) + '.' + (known ? ext : 'bin')writeFile('storage/md-files/' + stored, f.bytes)urls.push('/md-editor/files/' + stored)}for (e of listDir('storage/md-files')) {let stamp = toNumber(e.name.slice(0, e.name.indexOf('-')))if (stamp != null && stamp < now() - 86400000) { remove(e.path) }}return urls}
Branches
- mainmain branch