notes
All repositories: gitoria
12.1 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` (it has its own `Style`) and `../icons/icons.hl` (the shared `Icon` every toolbar button holds; one copy per app, other components import it too). 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 minimum height of the editing surface and of the source view, in lines (default 6; 1 = one line); not applied to `readonly = "true"`, which hugs its text || `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).A row typed the way the Markdown is written (`| milk | 2 |`) into a cell and finished with Enter or Tab goes into the cells (this oneand the ones after it). The **Table** button in the toolbar puts an empty table (two header cells, one row) under the block the caret isin, the caret in its first header cell; an empty table is written as a real pipe table with empty cells (header row, delimiter row, body row), so it stays a table after the source switch, a save and a reload.- **Look**: the editor draws no table style of its own beyond a plain padding fallback: the page's theme styles `table`, `th`, `td`(the layouts theme: muted head, no grid, striped rows). The table scrolls sideways inside its own box when it is wider.- **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)The toolbar buttons are icons (`components/icons`: bold, italic, code, link, heading, lists, code block, file, undo, redo); each keeps `title` and `aria-label` with its shortcut. On a phone (max-width 40rem) the toolbar is two even rows (6 + 5 buttons, 5 + 5 with `files = "off"`) of 44 px targets, every icon inside the editor box, nothing to swipe; the "Markdown source" label is 15 px (still muted) and a 44 px target, and a table head is 16 px. On a desktop width the toolbar stays one row. The editor's focus frame is the only focus ring: the surface and the source view never draw their own, whatever the app theme says.| Action | Keys | Toolbar ||---|---|---|| bold / italic | Ctrl+B / Ctrl+I (Cmd on Mac) | B, I || inline code | Ctrl+E (Cmd on Mac): with a selection it makes the selection code; **without a selection it opens an empty code span** to type into; Ctrl+E inside code leaves the span (the caret goes behind it) | `<>` || 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. Enter twice in a list leaves the list. Ctrl/Cmd+click opens a link.**Ctrl+K edits a link** (mission 011): with a selection it makes the selection a link; with the caret **inside a link** the form openswith that link's address and Apply changes **that** link (no second link inside it), an empty address or Remove takes the link off(the text stays); with the caret only, in plain text, Apply inserts the address as a link. `javascript:` and other addresses that are not`http(s)://`, `mailto:`, `/path`, `#anchor` are refused. The caret returns to the text.**Leaving a code block**: a fenced code block is left by pressing **Enter twice at its end** (an empty last line goes and a paragraphfollows), by Ctrl+Alt+C / the Block button (it turns back into a paragraph), or by Backspace at its start. This also works when thecode block is the last thing in the editor.**Not supported**: nested lists (Tab in a list changes nothing — lists are one level, the focus moves on) and quotes (`> text` staysplain text and is written back as `\> text`, so no Markdown reader makes a quote of it). No HTML.## Files and images`files = "off"` hides the File button and leaves pasted / dropped files to the page (the events bubble on, e.g. to a FilesUpload);the editor itself then uploads nothing (used by the comments block's attach form).Drop 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. Chrome runs noanimations in a tab opened in the background, so the first focus or pointer-down on the surface builds it too (whichever comesfirst; built only once) — a tab shown later and typed in at once works.- 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.- Chrome's list / block commands can nest a `<ul>` or `<p>` inside the caret's `<p>`; after every edit the surface is flattenedagain (`flatten()`, one at a time), so tables, headings and code blocks work after a list or a heading.- Undo / redo is the browser's own; typing rules and the toolbar use the browser's editing commands so they undo.
Branches
Latest commits
- 71a9538bnotes: B27W report, log linemre
- 78ba0073notes: an ident access key (Authorization: Bearer idt_…) reads and writes its identity's notes through /api/me and /api/notes; gate tests/ident-access-notes.mjs (ident#27)mre
- 3de46dacnotes: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 73+5 failing → 78 passedmre
- 5909d458notes: MdEditor from components 59704ce (text on the clipboard always pastes, a file manager copy still attaches; pipe rows without a delimiter row; tickets#49, notes#5)mre
- 98bd6a98N5b: pipe rows without a delimiter row are a table (MdEditor ad4e929, plain copy); gate tests/dashboard-table.mjs; reportmre
- dbf93319N5 report: components gate rerun 613 passed, 0 failedmre
- 3a8bc0d2N5: empty Table-button table is written as a real pipe table (MdEditor 4bd1112, plain copy); gate tests/table-button.mjs; reportmre
- c16bd443N5: step 1 report (tables pass locally; his DASHBOARD note has no delimiter row)mre
- 6091320anotes: MdEditor 7437e32 (surface drawn at once when the page is claimed); gate measures claim-to-text on 5 note loads at 1280 and 390mre
- 0aa9277enotes: move each component's styles into its own Style block (shell and header selector in main.hl, list, editor frame and delete question in notes.hl); styles.hl keeps the theme inherit, the accent and the message colour; .gitignore ignores a bin symlink too; tests/shots.mjs for the pixel comparemre
- 9ba01de3notes: write the standby service out in full so the boot gate can read the main servicemre
- 98cab40anotes: standby service on its port plus 10 and a deploy that hands over through it without a gapmre
- 1fadb473notes gate: the editor table now has 1 px cell lines from the notes thememre
- 8762115cnotes: take the components e54f83a (paste as Markdown, text typed before the editor is built is kept, Table button); table lines come from the notes thememre
- ac23e194ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
- 2804f0d3Merge branch 'iw-ident-widget'mre
- 52d8d242notes: e2e script checks Log out and the selector in the header instead of the user menumre
- e5abf7b7notes header: the selector alone top right plus Log out as before the theme missions; the user icon is gonemre
- a2e343aeident selector back in the header (creator 2026-10-24): selector top right signed in and out on phone and desktop, no second Log in button, new gate checks (WIP: gates not re-run after the last edits)mre
- 7a889b82notes: MdEditor with the Table button, typed pipe rows and the theme's table look (components mt-tables); a table note is named by its header words; gate checks that fail on mainmre