- Fixed bug with contenteditable plaintext-only mode on Firefox versions >136 (#126) (4d19174)
- Added support to extendes autoclose simbols (#122) (ab52adb)
- Fixed cursor jumping to bottom when editor is nested inside a shadow root (#112) (245726c)
- Added parameter to updateCode to optionaly prevent callback being called (#110) (2587dc3)
- Added reference to codejar-linenumbers library (#108) (7568251)
项目说明
这个项目的说明还在生成中,先参考下面「最近发版」和 README。
- 4.3.0Release 4.3.0最新正式版2025-10-14 · 2025-10-14 09:06 UTC
- 4.2.0Release 4.2.02023-08-03 · 2023-08-03 13:56 UTC
查看更新说明
- Respect defaultPrevented in handlePaste (89940be)
- 4.1.2Release 4.1.22023-08-02 · 2023-08-02 00:24 UTC
查看更新说明
- Place cursor before uneditable element (cfbd05c)
- 4.1.1Release 4.1.12023-07-26 · 2023-07-26 13:50 UTC
查看更新说明
- Add typesVersions (aa824b1)
- 4.1.0Release 4.1.02023-07-26 · 2023-07-26 13:43 UTC
查看更新说明
- Add onPaste event (90b0b86)
- Fix pos restore for cut event (f730c38)
用户点评
登录用户的真实使用记录,可按版本和硬件筛选共 0 条
还没有人点评。第一个说出真实体验的人最有价值。
Features
- Lightweight (2.45 kB only)
- No dependencies
- Preserves indentation on a new line
- Adds closing brackets, quotes
- Indents line with the Tab key
- Supports undo/redo
Getting Started
Install CodeJar 🍯 via npm:
npm i codejar
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 a highlighting function (like Prism.js, highlight.js):
const highlight = (editor: HTMLElement) => {
const code = editor.textContent
code = code.replace('foo', '<span style="color: red">foo</span>')
editor.innerHTML = code
}
const jar = CodeJar(editor, highlight)
Third argument to CodeJar is options:
tab: stringreplaces "tabs" with given string. Default:\t.- Note: use css rule
tab-sizeto customize size.
- Note: use css rule
indentOn: RegExpallows auto indent rule to be customized. Default/[({\[]$/.moveToNewLine: RegExpchecks in extra newline character need to be added. Default/^[)}\]]/.spellcheck: booleanenables spellchecking on the editor. Defaultfalse.catchTab: booleancatches Tab keypress events and replaces it withtabstring. Default:true.preserveIdent: booleankeeps indent levels on new line. Defaulttrue.addClosing: booleanautomatically adds closing brackets, quotes. Defaulttrue.historyrecords history. Defaulttrue.windowwindow object. Default:window.autocloseobjectopen stringcharacters that triggers the autoclose functionclose stringcharacters that correspond to the opening ones and close the object.
const options = {
tab: ' '.repeat(4), // default is '\t'
indentOn: /[(\[]$/, // default is /{$/
autoclose: {
open: `([{*`, // default is `([{'"`
close: `)]}*` // default is `)]}'"`
}
}
const jar = CodeJar(editor, highlight, options)
API
updateCode(string)
Updates the code.
jar.updateCode(`let foo = bar`)
updateOptions(Partial<Options>)
Updates the options.
jar.updateOptions({tab: '\t'})
onUpdate((code: string) => void)
Calls callback on code updates.
jar.onUpdate(code => {
console.log(code)
})
toString(): string
Return current code.
let code = jar.toString()
save(): string
Saves current cursor position.
let pos = jar.save()
restore(pos: Position)
Restore cursor position.
jar.restore(pos)
recordHistory()
Saves current editor state to history.
destroy()
Removes event listeners from editor.
Related
- react-codejar - a React wrapper for CodeJar.
- ngx-codejar - an Angular wrapper for CodeJar.
- codejar-linenumbers - an JS library for line numbers.