components
All repositories: gitoria
8.6 KB
# Markdown editorA Markdown field you edit **visually**: headings look like headings, **bold** is bold, lists are lists — while you type.The value stays **plain Markdown**. A port of `worldapi-components/md-editor.js` as a pure Hybriel component: no JavaScriptof its own. Demo: `/md-editor`.Use it in a page:```import MdEditor from './md-editor.hl'form { method = "post" action = "/save"MdEditor { name = "summary" value = "# Title" }}```## FilesCopy `md-editor.hl` (one file; it has its own `Style`). Nothing else. It needs the WorldAPI colour tokens only as an option:every colour is `var(--color-…, <base value>)`, so an app that declares the tokens (and its `--color-accent`) wins andan app without them shows the base look (black, white, greys, no frames — root README "Look").## Attributes| Attribute | Meaning ||---|---|| `name` | the field name the Markdown posts under (default `markdown`) || `value` | the start Markdown || `placeholder` | the grey text of an empty editor (default `Write…`) || `rows` | the height of the source view (default 6) || `readonly` | `"true"`: the Markdown shown formatted and nothing else — no toolbar, no source switch, not editable, no field background (mission 006: the comments block draws every comment with it) |## Reading the valueThe Markdown lives in a hidden `<input name="…">` inside `<md-editor>`. It posts with the form, and every change fires abubbling DOM `input` event on that input: a wrapper element catches it with `on input(e) { text = e.target.value }`(`e.target.name` says which editor, when there are several). A host cannot read a child member (hybriel#87), so the DOMevent is the way out.## What it makes (the Markdown tickets renders)Headings `#`…`######` (toolbar: H cycles paragraph → H1 → H2 → H3), paragraphs (a single line break is kept), `-` / `1.` lists(one level), fenced code blocks, `` `code` ``, links `[text](url)` / `<https://…>` / bare `https://…` (only `http(s)://`,`mailto:`, `/path`, `#anchor` become links), *em*, **strong**. Images `` (http(s) or a `/path`; no `data:` / `javascript:`).**Tables**: GitHub pipe tables (below). Nothing else (quotes, nesting, HTML) can be made; pasted text is read as Markdown andpasted HTML is kept as plain text — except an HTML table, which becomes a table.## Tables (mission 004, creator notes#5)The behaviour of notes' `md-editor.js` (9acc27f), built in Hybriel:```| Item | Qty | Note \| x || :------- | :-: | --------: || **milk** | `2` | fresh |```- **Syntax**: a header row, a delimiter row with the same number of cells (`---`; `:--` left, `:-:` center, `--:` right), then body rowsup to a blank line, a heading, a list or a fence. Outer pipes are optional; `\|` is a pipe inside a cell (also in `code`); inlineMarkdown (bold, italic, code, links, images) works in cells. Missing cells are padded, extra cells dropped. A table can follow aparagraph line directly.- **Typing**: type the header row (`| Item | Qty |`), Enter, the delimiter row (`|---|--:|`), Enter: the two lines become a table withan empty row, the caret in its first cell. **Tab** / **Shift+Tab** go to the next / previous cell, Tab in the last cell adds a row;**Enter** adds a row below; Enter in an empty last row removes it and leaves the table. A cell is one line (Shift+Enter doesnothing there) and takes no block (heading / list / code block buttons and keys do nothing in a cell).- **Never merged**: Backspace at the start of a cell goes to the end of the cell before; Delete at the end of a cell does nothing;Backspace / Delete next to a table never joins it with the text (an empty paragraph next to it goes). A selection that reachesinto a table empties the selected cells (the table stays); a table selected as a whole goes.- **Writing**: an unchanged table is written back byte for byte (its own spelling), also when the text around it changes. A changedtable is written padded to its column widths, `| :--- | :-: | --: |`, with `\|` for a pipe in a cell. A table whose cells are allempty is dropped.- **Pasting**: Markdown text with a table becomes a table. Copied HTML with a `<table>` (a spreadsheet, a web page) becomes a table ofits cells' text (`align` / `text-align` of the first row kept, short rows padded); the rest of that HTML is taken as plain text.Into a cell everything is pasted on one line. The HTML is read in an inert document (no script runs, nothing loads).- **Look**: thin grey cell lines (`var(--color-border, #999)`), a bold header, no colour. A wide table scrolls inside its own box(`display: block; overflow-x: auto`), the page does not scroll sideways on a phone.- **Limits**: adding a row, leaving a table and turning typed lines into a table change the DOM directly, not through the browser'sediting commands, so Ctrl+Z does not take those steps back one by one (typing inside cells undoes as usual). No columnadd / remove buttons; rows and columns come from typing or the Markdown source view.## Keyboard and toolbar (the toolbar also works on a phone)| Action | Keys | Toolbar ||---|---|---|| bold / italic / inline code | Ctrl+B / Ctrl+I / Ctrl+E (Cmd on Mac) | B, I, `<>` || link (add / change / remove) | Ctrl+K | Link → small form (Enter applies, Esc closes) || heading 1/2/3, paragraph | Ctrl+Alt+1/2/3, Ctrl+Alt+0 | H || bulleted / numbered list | Ctrl+Shift+8 / Ctrl+Shift+7 | • List, 1. List || code block | Ctrl+Alt+C | Block || undo / redo | Ctrl+Z / Ctrl+Shift+Z | ↶ ↷ || send the form | Ctrl+Enter | || line break inside a paragraph | Shift+Enter | |**Typing Markdown formats it**: `# `, `- `, `* `, `1. ` at the start of a line make a heading / list; ```` ``` ```` + Enter a codeblock; a closed `**x**`, `*x*`, `_x_`, `` `x` `` becomes formatted (Ctrl+Z turns it back). Backspace at the start of a heading /list item / code block makes it a paragraph. Ctrl/Cmd+click opens a link.## Files and imagesDrop files on the editor, paste them, or press **File** in the toolbar (several at once). Each file is sent to the app with`emit server mdUpload(files)` — the framework uploads it over HTTP in parts and a progress bar per file shows under the toolbar(`on client uploadProgress`). The **app decides where files are stored**: it must answer that face, or the upload fails("The upload failed." under the toolbar). The face gets a list of `{ bytes, name, type, size }` and returns a list of addresses,one per file, in order. An image (`image/*`) lands in the text as `` and is drawn; any other file as `[name](address)`.Dropped files land where they were dropped, pasted / chosen ones at the caret (or at the end).```on server mdUpload(files) {let urls = []for (f of files) { writeFile('storage/files/' + f.name, f.bytes) urls.push('/files/' + f.name) }return urls}```The app also sets `uploadMax` on `WebFramework` (default 1 GiB) and serves the address (`directory` route). Demo: `demo.hl` (the site's `lib/uploads.hl`) keepsfiles 24 h in `storage/md-files` under a random name, with a known extension only, served with `nosniff` + `sandbox`.## Markdown sourceA "Markdown source" button in the footer switches the field to a plain textarea holding the raw Markdown (for copying itout or pasting a larger text in) and back to "Visual editor"; switching back reads the typed text again.## How it is built (and its limits)- The formatted surface is a `contenteditable` element; its content is built with `createElement` / text nodes only (never`innerHTML` of user text) and is never re-rendered by the framework. Hybriel has no mount event, so a 100 ms CSS animationon the surface fires `animationiteration` once the page is live and that hands the editor its first content.- An untouched value is returned byte for byte; a changed value is written in one canonical form (`*em*`, `**strong**`, `- item`,`1. item`, ```` ``` ```` fences, backslash escapes where needed). What Markdown cannot hold is dropped (empty paragraphs).- Line ends: a real carriage return (Windows `CR LF`, in a value or a pasted text) is dropped when the Markdown is read; a backslashfollowed by `r` is text and stays (`C:\root`, ticket #17 — before, both characters were removed).- Tables: every cell is a `th` / `td` of a real `<table>` in the surface; the browser edits the text inside the cells, the editorowns the structure (rows, leaving, the never-merge rules) in `beforeinput`.- Not ported from the JS version: the `<textarea>` "enhance" mode, `disabled` / `toolbar="none"` (`readonly` came in mission 006), the `[text](url)`typing rule, per-block byte-for-byte spelling, and the `MdEditor.markdown` test API.- Undo / redo is the browser's own; typing rules and the toolbar use the browser's editing commands so they undo.
Branches
Latest commits
- 9db927dfpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre
- 00a95329components (mission 007): re-vendor Hybriel to the shared static build 0f396652 (plugins; bin/ git-ignored), gate 324/0 old and new, real-data copy unchanged, report 007, same-output.mjs ports overridablemre
- fe50766bmission brief 007mre
- 2dce51b8components (mission 006) 4/4: docs (README comments + gate 324, STATUS, LOG), report reports/006-comments.mdmre
- 9ec7273ecomponents layouts#2 (mission 006) 3/4: comments — each text is MdEditor readonly directly in its row (hl:web builds a composed child anew when a bound value changes, so the keyed one-row list was not needed); demo 'bob edits his comment elsewhere (a push)' + gate check that a text the host changes from outside is drawn anew; theme radii (var(--radius-small/large) with 5px/10px fallbacks: layouts' check-theme 0 problems with comments.hl copied in); README 'How it is built'; gate 324/0 (34 comments checks)mre
- fd57c18bcomponents layouts#2 (mission 006) 2/3: comments block (components/comments, demo /comments) — pure Hybriel: '1 comment' / 'n comments', avatar initial, bubble with name, time, 'edited' (<details> with the earlier versions), the text drawn by MdEditor readonly (one-row list keyed by id + text, so an edit redraws), like ♥ n (aria-pressed), Edit / Delete (asks first) only where author == user, the form below in MdEditor (Comment button or Ctrl+Enter; a fresh editor after a post). Host passes comments + user and hands onPost / onEdit / onDelete / onLike (hybriel#87); demo keeps a mock thread from lib/comments.hl in the page, switches alice / bob / nobody. Home entry, routes, map line, README; base-look gate covers comments; 33 gate checks with real clicks / keysmre
- 850a376bcomponents (mission 006) 1/3: MdEditor readonly = "true" — the Markdown shown formatted and nothing else (no toolbar, link form, file rows, placeholder, source view or switch; article contenteditable false, tabindex -1, role document; no field background or focus ring); the mount only loads the value. For the comments block, which draws every comment with the editor's own renderermre
- ee9f1b57mission brief w6mre
- fe691586components (mission 005) 4/4: docs (README components + gate 290, STATUS, LOG), report reports/005-fixes-16-17-5.mdmre
- f1ebab53components #5 (mission 005) 3/4: file upload for several files (components/files-upload, demo /files-upload) — pure Hybriel, each file through emit server uploadFile (hybriel#94), drop or choose many, a row per file with live progress, one after the other in order, a drop during an upload waits, a file over uploadMax fails in its row and the next goes, 'n of m uploaded'; home entry, routes, map line, README; pagination keyboard focus ring #555 (Chrome painted its own orange one); docker-compose init: true (hybriel#156); gate 290/0 (17 #5 checks with real file drops, 1 #16 focus check)mre
- 3b73ae5bcomponents #16 (mission 005) 2/4: Pagination tells the host the target page — the host hands onPage (hybriel#87: a function, not emit), called on every click that changes the page (pointer, Enter/Space, script click; Previous/Next/numbers/first/last; not … or the current page) with the target read from the clicked button's data-page before the rebuild; demo shows what the page was told; README Data out; 15 gate checks (14 fail on the old pagination)mre
- 6b27c0f0components #17 (mission 005) 1/4: md-editor keeps backslash + r — parseInto dropped the two characters '\r' (C:\root -> C:oot) instead of a real carriage return; CR now decoded from JSON (String.fromCharCode does not parse in a member), real CRs also go from pasted plain text; 5 gate checks (3 fail on the old md-editor.hl)mre
- 1a5dc327mission brief w5mre
- a567b579components (mission 004) 3/3: docs (README Hybriel a832f73b + tables, STATUS, LOG), report reports/004-md-editor-tables.mdmre
- 70fe7ab5components (mission 004) 2/3: GitHub pipe tables in the Hybriel MdEditor (creator notes#5; behaviour of notes md-editor.js 9acc27f, no JavaScript) — parse/render (header, :--/:-:/--: alignment, \| in cells and code, inline Markdown in cells, outer pipes optional), padded writer, unchanged table byte for byte, typing | a | b | Enter |---|---| Enter makes a table, Tab/Shift+Tab, Enter new row, Enter in empty last row leaves, Backspace/Delete never merge, selection across a table empties cells, pasted HTML table -> table, thin #999 lines, wide table scrolls in its box; demo third editor 'plan'; gate 252/0 (22 new table checks, 20 fail on the old md-editor.hl)mre
- f2adb3f3components (mission 004) 1/3: Hybriel master a832f73b (incremental build cache, HL_CACHE_DIR, #151) — bin/hybriel sha256 e1b53f7ce687f5afbe3db34bcda69958f56e730571e4841923c9c8eb3d272db9 built from git archive a832f73b (HL_NO_PLUGINS=1 native/build.sh), the same nine plugins; container: HL_CACHE_DIR=/cache on volume hybriel-cache, /tmp tmpfs; tests wait COMPONENTS_COLD_MS (120 s) for every start; deploy.sh DEPLOY_URL_WAIT 120 s; gate 230/0 coldmre
- 91bd8a0dmission brief (w4)mre
- 3bc905c5components #15 (mission 003) 2/2: docs (README Look, STATUS, LOG), reportmre
- 5a98d77ecomponents #15 (mission 003) 1/2: black/white base look — greys for nuances, no WorldAPI fallbacks, no decorative frames; site + icon black/white; gate 230/0mre
- 3f93d716components code order (mission 002) 4/4: docs, tests/letcount.py, tests/same-output*, reportmre