gitoria

layouts

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7979397c7979397creport ML-mobilemre7979397c/components/md-editor/README.md

11.1 KB

  1. # Markdown editor
  2. A Markdown field you edit **visually**: headings look like headings, **bold** is bold, lists are lists — while you type.
  3. The value stays **plain Markdown**. A port of `worldapi-components/md-editor.js` as a pure Hybriel component: no JavaScript
  4. of its own. Demo: `/md-editor`.
  5. Use it in a page:
  6. ```
  7. import MdEditor from './md-editor.hl'
  8. form { method = "post" action = "/save"
  9. MdEditor { name = "summary" value = "# Title" }
  10. }
  11. ```
  12. ## Files
  13. Copy `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:
  14. every colour is `var(--color-…, <base value>)`, so an app that declares the tokens (and its `--color-accent`) wins and
  15. an app without them shows the base look (black, white, greys, no frames — root README "Look").
  16. ## Attributes
  17. | Attribute | Meaning |
  18. |---|---|
  19. | `name` | the field name the Markdown posts under (default `markdown`) |
  20. | `value` | the start Markdown |
  21. | `placeholder` | the grey text of an empty editor (default `Write…`) |
  22. | `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 |
  23. | `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) |
  24. ## Reading the value
  25. The Markdown lives in a hidden `<input name="…">` inside `<md-editor>`. It posts with the form, and every change fires a
  26. bubbling DOM `input` event on that input: a wrapper element catches it with `on input(e) { text = e.target.value }`
  27. (`e.target.name` says which editor, when there are several). A host cannot read a child member (hybriel#87), so the DOM
  28. event is the way out.
  29. ## What it makes (the Markdown tickets renders)
  30. Headings `#`…`######` (toolbar: H cycles paragraph → H1 → H2 → H3), paragraphs (a single line break is kept), `-` / `1.` lists
  31. (one level), fenced code blocks, `` `code` ``, links `[text](url)` / `<https://…>` / bare `https://…` (only `http(s)://`,
  32. `mailto:`, `/path`, `#anchor` become links), *em*, **strong**. Images `![alt](address)` (http(s) or a `/path`; no `data:` / `javascript:`).
  33. **Tables**: GitHub pipe tables (below). Nothing else (quotes, nesting, HTML) can be made; pasted text is read as Markdown and
  34. pasted HTML is kept as plain text — except an HTML table, which becomes a table.
  35. ## Tables (mission 004, creator notes#5)
  36. The behaviour of notes' `md-editor.js` (9acc27f), built in Hybriel:
  37. ```
  38. | Item | Qty | Note \| x |
  39. | :------- | :-: | --------: |
  40. | **milk** | `2` | fresh |
  41. ```
  42. - **Syntax**: a header row, a delimiter row with the same number of cells (`---`; `:--` left, `:-:` center, `--:` right), then body rows
  43. up to a blank line, a heading, a list or a fence. Outer pipes are optional; `\|` is a pipe inside a cell (also in `code`); inline
  44. Markdown (bold, italic, code, links, images) works in cells. Missing cells are padded, extra cells dropped. A table can follow a
  45. paragraph line directly.
  46. - **Typing**: type the header row (`| Item | Qty |`), Enter, the delimiter row (`|---|--:|`), Enter: the two lines become a table with
  47. an 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;
  48. **Enter** adds a row below; Enter in an empty last row removes it and leaves the table. A cell is one line (Shift+Enter does
  49. nothing there) and takes no block (heading / list / code block buttons and keys do nothing in a cell).
  50. - **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;
  51. Backspace / Delete next to a table never joins it with the text (an empty paragraph next to it goes). A selection that reaches
  52. into a table empties the selected cells (the table stays); a table selected as a whole goes.
  53. - **Writing**: an unchanged table is written back byte for byte (its own spelling), also when the text around it changes. A changed
  54. table is written padded to its column widths, `| :--- | :-: | --: |`, with `\|` for a pipe in a cell. A table whose cells are all
  55. empty is dropped.
  56. - **Pasting**: Markdown text with a table becomes a table. Copied HTML with a `<table>` (a spreadsheet, a web page) becomes a table of
  57. its cells' text (`align` / `text-align` of the first row kept, short rows padded); the rest of that HTML is taken as plain text.
  58. Into a cell everything is pasted on one line. The HTML is read in an inert document (no script runs, nothing loads).
  59. - **Look**: thin grey cell lines (`var(--color-border, #999)`), a bold header, no colour. A wide table scrolls inside its own box
  60. (`display: block; overflow-x: auto`), the page does not scroll sideways on a phone.
  61. - **Limits**: adding a row, leaving a table and turning typed lines into a table change the DOM directly, not through the browser's
  62. editing commands, so Ctrl+Z does not take those steps back one by one (typing inside cells undoes as usual). No column
  63. add / remove buttons; rows and columns come from typing or the Markdown source view.
  64. ## Keyboard and toolbar (the toolbar also works on a phone)
  65. 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. 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.
  66. | Action | Keys | Toolbar |
  67. |---|---|---|
  68. | bold / italic | Ctrl+B / Ctrl+I (Cmd on Mac) | B, I |
  69. | 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) | `<>` |
  70. | link (add / change / remove) | Ctrl+K | Link → small form (Enter applies, Esc closes) |
  71. | heading 1/2/3, paragraph | Ctrl+Alt+1/2/3, Ctrl+Alt+0 | H |
  72. | bulleted / numbered list | Ctrl+Shift+8 / Ctrl+Shift+7 | • List, 1. List |
  73. | code block | Ctrl+Alt+C | Block |
  74. | undo / redo | Ctrl+Z / Ctrl+Shift+Z | ↶ ↷ |
  75. | send the form | Ctrl+Enter | |
  76. | line break inside a paragraph | Shift+Enter | |
  77. **Typing Markdown formats it**: `# `, `## `, `### `, `- `, `* `, `1. ` at the start of a line make a heading / list; ```` ``` ```` + Enter a code
  78. block; a closed `**x**`, `*x*`, `_x_`, `` `x` `` becomes formatted (Ctrl+Z turns it back). Backspace at the start of a heading /
  79. list item / code block makes it a paragraph. Enter twice in a list leaves the list. Ctrl/Cmd+click opens a link.
  80. **Ctrl+K edits a link** (mission 011): with a selection it makes the selection a link; with the caret **inside a link** the form opens
  81. with that link's address and Apply changes **that** link (no second link inside it), an empty address or Remove takes the link off
  82. (the text stays); with the caret only, in plain text, Apply inserts the address as a link. `javascript:` and other addresses that are not
  83. `http(s)://`, `mailto:`, `/path`, `#anchor` are refused. The caret returns to the text.
  84. **Leaving a code block**: a fenced code block is left by pressing **Enter twice at its end** (an empty last line goes and a paragraph
  85. follows), by Ctrl+Alt+C / the Block button (it turns back into a paragraph), or by Backspace at its start. This also works when the
  86. code block is the last thing in the editor.
  87. **Not supported**: nested lists (Tab in a list changes nothing — lists are one level, the focus moves on) and quotes (`> text` stays
  88. plain text and is written back as `\> text`, so no Markdown reader makes a quote of it). No HTML.
  89. ## Files and images
  90. `files = "off"` hides the File button and leaves pasted / dropped files to the page (the events bubble on, e.g. to a FilesUpload);
  91. the editor itself then uploads nothing (used by the comments block's attach form).
  92. Drop files on the editor, paste them, or press **File** in the toolbar (several at once). Each file is sent to the app with
  93. `emit server mdUpload(files)` — the framework uploads it over HTTP in parts and a progress bar per file shows under the toolbar
  94. (`on client uploadProgress`). The **app decides where files are stored**: it must answer that face, or the upload fails
  95. ("The upload failed." under the toolbar). The face gets a list of `{ bytes, name, type, size }` and returns a list of addresses,
  96. one per file, in order. An image (`image/*`) lands in the text as `![name](address)` and is drawn; any other file as `[name](address)`.
  97. Dropped files land where they were dropped, pasted / chosen ones at the caret (or at the end).
  98. ```
  99. on server mdUpload(files) {
  100. let urls = []
  101. for (f of files) { writeFile('storage/files/' + f.name, f.bytes) urls.push('/files/' + f.name) }
  102. return urls
  103. }
  104. ```
  105. 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`) keeps
  106. files 24 h in `storage/md-files` under a random name, with a known extension only, served with `nosniff` + `sandbox`.
  107. ## Markdown source
  108. A "Markdown source" button in the footer switches the field to a plain textarea holding the raw Markdown (for copying it
  109. out or pasting a larger text in) and back to "Visual editor"; switching back reads the typed text again.
  110. ## How it is built (and its limits)
  111. - The formatted surface is a `contenteditable` element; its content is built with `createElement` / text nodes only (never
  112. `innerHTML` of user text) and is never re-rendered by the framework. Hybriel has no mount event, so a 100 ms CSS animation
  113. on the surface fires `animationiteration` once the page is live and that hands the editor its first content. Chrome runs no
  114. animations in a tab opened in the background, so the first focus or pointer-down on the surface builds it too (whichever comes
  115. first; built only once) — a tab shown later and typed in at once works.
  116. - An untouched value is returned byte for byte; a changed value is written in one canonical form (`*em*`, `**strong**`, `- item`,
  117. `1. item`, ```` ``` ```` fences, backslash escapes where needed). What Markdown cannot hold is dropped (empty paragraphs).
  118. - Line ends: a real carriage return (Windows `CR LF`, in a value or a pasted text) is dropped when the Markdown is read; a backslash
  119. followed by `r` is text and stays (`C:\root`, ticket #17 — before, both characters were removed).
  120. - Tables: every cell is a `th` / `td` of a real `<table>` in the surface; the browser edits the text inside the cells, the editor
  121. owns the structure (rows, leaving, the never-merge rules) in `beforeinput`.
  122. - Not ported from the JS version: the `<textarea>` "enhance" mode, `disabled` / `toolbar="none"` (`readonly` came in mission 006), the `[text](url)`
  123. typing rule, per-block byte-for-byte spelling, and the `MdEditor.markdown` test API.
  124. - Chrome's list / block commands can nest a `<ul>` or `<p>` inside the caret's `<p>`; after every edit the surface is flattened
  125. again (`flatten()`, one at a time), so tables, headings and code blocks work after a list or a heading.
  126. - Undo / redo is the browser's own; typing rules and the toolbar use the browser's editing commands so they undo.

Branches

Latest commits

  • 7979397creport ML-mobilemre
  • 85d2ed25gate: wordingmre
  • 98a311c0gate: 390 px checks for list rows, table word, edited tap target, code in titlesmre
  • 769b99d7theme: table cells keep a word whole, code in a title stays text-sized, 'edited' is a 44 px tap target on the phonemre
  • c36b7595list rows (notes, mail) stack their parts full width: the date no longer a one-letter column beside the titlemre
  • 02aa74aasync components.hybriel 134b4e3: MdEditor builds an editor put into the page at once, redraws a surface put in anew (F5, hybriel#211)mre
  • 268b1c87sync components.hybriel 6b0936a: the 'edited' foldable drops the theme's border and marginmre
  • c51d039fsync components.hybriel 8d79ff7: bubble hugs its content, quiet 'edited' label; gate checksmre
  • f4ad7eecsync components 9a: shared Icon (icons/icons.hl), md-editor toolbar icons + single focus ring (components f4-icons ba96735)mre
  • db258c11sync-components: copy icons/icons.hl + README (F4); testsmre
  • badc1879md-editor.hl: verbatim copy of components f3-mdvalue (an older text handed back is kept)mre
  • 867a8375md-editor.hl: verbatim copy of components f3-mdvalue (an outside value change is shown by the editor)mre
  • 5d8d2c67tests: wait for hydration before real clicks after a navigation (H2)mre
  • 99db79f0re-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • 30353f60tests: e2e-md-showcase Chrome range from LAYOUTS_GATE_CHROME_MD, default unchangedmre
  • 16a7878cre-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 6af559e8docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 5599cf56deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • 0bcc6294Mission 011: e2e-197, brief, report, LOGmre
  • 137303c5MainMenu marks the current page from the route (hybriel#197, server-rendered); current prop/item.current stay as fallback; tracker demo drops current; gate: SSR marker; docsmre