CodeJar-Async is a fork of CodeJar with async highlighting support.
- Lightweight (2.5 kB only)
- No dependencies
- Preserves indentation on a new line
- Adds closing brackets, quotes
- Indents line with the Tab key
- Supports undo/redo
-
Async highlighting (
${\color{red}NEW}$ )
Install CodeJar-Async 🍯 via npm:
npm i codejar-async
Create an element and init the CodeJar 🍯:
<div class="editor"></div>
<script>
let jar = CodeJar(document.querySelector('.editor'), highlight)
</script>
Second argument to CodeJar
is an async highlighting function (like Prism.js, highlight.js):
const highlight = async (editor: HTMLElement): string => {
const code = editor.textContent
return code.replace('foo', '<span style="color: red">foo</span>')
}
const jar = CodeJar(editor, highlight)
Third argument to CodeJar
is options:
tab: string
replaces "tabs" with given string. Default:\t
.- Note: use css rule
tab-size
to customize size.
- Note: use css rule
spellcheck: boolean
enables spellchecking on the editor. Defaultfalse
.catchTab: boolean
catches Tab keypress events and replaces it withtab
string. Default:true
.preserveIdent: boolean
keeps indent levels on new line. Defaulttrue
.addClosing: boolean
automatically adds closing brackets, quotes. Defaulttrue
.history
records history. Defaulttrue
.window
window object. Default:window
.
const options = {
tab: ' '.repeat(4), // default is '\t'
}
const jar = CodeJar(editor, highlight, options)
Updates the code.
jar.updateCode(`let foo = bar`)
Updates the options.
jar.updateOptions({tab: '\t'})
Calls callback on code updates.
jar.onUpdate(code => {
console.log(code)
})
Calls callback after the async highlighting is done.
jar.onHighlight(editor => {
console.log(editor.innerHTML)
})
Return current code.
let code = jar.toString()
Saves current cursor position.
let pos = jar.save()
Restore cursor position.
jar.restore(pos)
Saves current editor state to history.
Removes event listeners from editor.
- react-codejar - a React wrapper for CodeJar.
- ngx-codejar - an Angular wrapper for CodeJar.
- codejar-linenumbers - an JS library for line numbers.