components
All repositories: gitoria
2.8 KB
# Code editorA code field: the code is **coloured while you type**, line numbers follow, **Tab** indents and **Shift+Tab** outdents,**Ctrl+Z / Ctrl+Shift+Z** undo and redo. A port of custom-ide's CodeJar editor as a pure Hybriel component: no JavaScriptof its own. Demo: `/code-editor`.Use it in a page:```import CodeEditor from './code-editor.hl'form { method = "post" action = "/save"CodeEditor { name = "source" value = "let x = 1" language = "js" }}```## FilesCopy `code-editor.hl` (one file; it has its own `Style`). Nothing else. Every colour is `var(--color-…, <palette value>)`(the syntax colours use the palette tokens `--orange`, `--green-light`, `--yellow`, `--green-dark`), so an app thatdeclares the WorldAPI tokens (and its `--color-accent`) wins and an app without them still looks right.## Attributes| Attribute | Meaning ||---|---|| `name` | the field name the text posts under (default `code`) || `value` | the start text || `language` | `hybriel` (default), `js` (or `javascript`), `plain` (no colours) || `spaces` | 0 (default): Tab writes a tab character; N: Tab writes N spaces |## What it does- **Highlighting** while typing: comments (`//`, `/* */`), strings (`'` `"` `` ` ``), numbers, keywords, function names (a namebefore `(`), Capitalised names (types). The caret stays where it was.- **Line numbers** in a gutter that follows the number of lines (it scrolls sideways with the code).- **Tab / Shift+Tab**: at the caret a tab (or `spaces` spaces) is inserted / removed; with several lines selected every line isindented / outdented.- **Enter** keeps the indent of the line; after `(` `[` `{` it indents once more, and a closing bracket right behind the caretmoves to its own line.- **Brackets and quotes** close themselves (a selection is wrapped); typing the closing character right before the same onesteps over it; an apostrophe inside a word (`don't`) stays alone.- **Undo / redo**: Ctrl+Z, Ctrl+Shift+Z (or Ctrl+Y). The editor keeps its own history (snapshots of text and caret, 300 atmost); typing within 300 ms is one step, Enter / Tab / a bracket / a paste are their own steps.- **Paste** is taken as plain text; the text is never treated as HTML.## Reading the valueThe text is in a hidden `<input name>`, so it posts in a form. A change is announced with a bubbling DOM `input` event on thatfield (`e.target.name`, `e.target.value`); the demo shows it below each editor:```form { on input(e) { emit gotCode(e) } … }on gotCode(e) { if (e.target.name == 'source') { source = e.target.value } }```## Not in it (custom-ide has more)Search, minimap, breadcrumbs, change bars, per-language grammars beyond the three above, read-only mode. Not tested: Firefox,Safari, touch, IME input (composition is left alone until it ends), very large files. Editing needs the page to be live(Hybriel has no mount event; the code appears when the page has loaded).
Branches
- mainmain branch