Getting started
Install
bun add @adecore/editornpm install @adecore/editorpnpm add @adecore/editorThe package brings @adecore/editor-core and Shiki along. It has no React and no peer dependencies.
Styles
Import the @adecore/ui theme first and the editor's styles after it. The editor's --editor-*, --find-* and --agent-* tokens build on the theme's.
@import '@adecore/ui/theme.css';
@import '@adecore/editor/editor.css';An app without that theme defines the base tokens itself; see Theme and syntax.
Mount an editor
Create an engine once and mount it into an element. The editor fills the element.
import { createSmartEditorEngine } from '@adecore/editor';
const engine = createSmartEditorEngine({ tokenizer: async () => null });
const editor = engine.mount(element, {
text: 'const greeting = "hello";\n',
language: 'typescript',
theme: 'github-light'
});
const stopSave = editor.onSave(() => save(editor.getText()));text and theme are required. Options and keymaps lists the rest. The tokenizer above colors nothing; Theme and syntax wires Shiki.
Give the element a position and size, such as position: relative; height: 400px. In a flex or grid layout, its parent may also need min-height: 0. The editor follows the element's size.
Mod+S (Cmd+S on a Mac, Ctrl+S elsewhere) only calls the onSave listeners. The app does the saving.
Engine options
One engine mounts any number of editors. Each has its own document, history and decorations.
SmartEditorEngineOptions | Default | Meaning |
|---|---|---|
tokenizer | required | Returns the LineTokenizer for a language and theme, or null for plain text |
scopeColors | The colors of a theme by TextMate scope; without it semantic tokens are ignored | |
keymap | KEYMAP | The key table, or a function that returns it; see keymaps |
handBack | [] | KeyChords the editor leaves alone, so the app's own shortcuts work from inside it, or a function that returns them |
apple | false | Read Ctrl and Meta the way macOS does |
The editor is editable while the tokenizer loads, and ignores a tokenizer that fails or answers after a newer theme was set.
Tear down
dispose() removes the editor's DOM and listeners. Call the functions on* methods return when a listener goes away earlier. Dispose an editor before mounting another into the same element.
stopSave();
editor.dispose();Checklist
- The page imports
theme.cssand theneditor.css. - The element has a position and a height, and its flex or grid parent has
min-height: 0. - One engine serves every editor, with the same resolved key table the app's menus print.
labelnames each editor in the app's language.- The app saves in
onSave; the editor writes nothing. - Every editor is disposed before its element goes or another editor mounts into it.