CodeEditor

App

Engine-agnostic code editor primitive on a textarea engine (line-number gutter, active line, theme, readonly, placeholder). M2: Tab / Shift+Tab indent (Esc then Tab leaves), Ctrl/Cmd+/ comment toggle, auto-indent, bracket and quote pairs, a toolbar (find / replace, format, copy, full screen), format-on-save. M3: diagnostics (markers in the gutter, tinted lines, a list that jumps to the spot), completion (getSuggestions, a listbox) and hover (getHover). format, getSuggestions and getHover name global functions (or are passed to KuiCodeEditor.attach). Later: Monaco lazy engine, multi-cursor, minimap, folding, vim/emacs keymap. Pixel-identical React sibling at modules/ui/CodeEditor/index.tsx.

JavaScript readonly

Preview

Read-only JavaScript snippet with line numbers.

Code
<%- include('modules/ui/CodeEditor', {
  id: 'example',
  label: 'example.js',
  language: 'js',
  theme: 'light',
  value: source,
  readonly: true,
  showLineNumbers: true
}) %>

Markdown editable

Preview

Editable markdown. The hidden form input is synced to the value.

Code
<%- include('modules/ui/CodeEditor', {
  id: 'readme',
  name: 'readme',
  label: 'README.md',
  language: 'markdown',
  theme: 'dark',
  value: src,
  placeholder: 'Start typing markdown…'
}) %>

Script with aids (M2 / M3)

Preview

Type msg. or press Ctrl+Space for suggestions; rest the pointer on msg.payload; Ctrl+F finds.

Code
<%- include('modules/ui/CodeEditor', {
  id: 'script',
  label: 'script.js',
  language: 'js',
  value: code,
  showToolbar: true,
  format: 'tidyScript',          // global function (value) => string | Promise<string>
  formatOnSave: true,
  getSuggestions: 'scriptNames', // global function (ctx) => Suggestion[]
  getHover: 'scriptHelp',        // global function (ctx) => { contents } | null
  markers: [{ line: 3, column: 4, severity: 'error', message: 'This ( is not closed.' }]
}) %>

<script>
  // Or hand the functions over in the browser, and update the markers later:
  KuiCodeEditor.attach(document.querySelector('[data-kui-codeeditor="script"]'), { getSuggestions, getHover, format });
  KuiCodeEditor.get('script').setMarkers(lint(KuiCodeEditor.get('script').getValue()));
</script>
Source modules/ui/CodeEditor/CodeEditor.ejs