Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An embeddable code editor for the browser 🍯

License

NotificationsYou must be signed in to change notification settings

antonmedv/codejar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeJar – an embeddable code editor for the browser

npmnpm bundle size

Features

  • Lightweight (2.45 kB only)
  • No dependencies
  • Preserves indentation on a new line
  • Adds closing brackets, quotes
  • Indents line with theTab key
  • Supportsundo/redo

Getting Started

Install CodeJar 🍯   via npm:

npm i codejar

Create an element and init the CodeJar 🍯:

<divclass="editor"></div><script>letjar=CodeJar(document.querySelector('.editor'),highlight)</script>

Second argument toCodeJar is a highlighting function (like Prism.js, highlight.js):

consthighlight=(editor:HTMLElement)=>{constcode=editor.textContentcode=code.replace('foo','<span>foo</span>')editor.innerHTML=code}constjar=CodeJar(editor,highlight)

Third argument toCodeJar is options:

  • tab: string replaces "tabs" with given string. Default:\t.
    • Note: use css ruletab-size to customize size.
  • indentOn: RegExp allows auto indent rule to be customized. Default/[({\[]$/.
  • moveToNewLine: RegExp checks in extra newline character need to be added. Default/^[)}\]]/.
  • 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.
  • autoclose object
    • open string characters that triggers the autoclose function
    • close string characters that correspond to the opening ones and close the object.
constoptions={tab:' '.repeat(4),// default is '\t'indentOn:/[(\[]$/,// default is /{$/autoclose:{open:`([{*`,// default is `([{'"`close:`)]}*`// default is `)]}'"`}}constjar=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.

letcode=jar.toString()

save(): string

Saves current cursor position.

letpos=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

License

MIT

About

An embeddable code editor for the browser 🍯

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp