Skip to content

An embeddable code editor for the browser 🍯

License

Notifications You must be signed in to change notification settings

michaelbromley/codejar

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeJar – an embeddable code editor for the browser

Features

  • Lightweight (2 kB only)
  • 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

CodeJar 🍯 can be used via modules:

<script type="module">
  import {CodeJar} from 'https://medv.io/codejar/codejar.js'
</script>

Create element and init:

<div class="editor"></div>
<script>
  let jar = new CodeJar(document.querySelector('.editor'), Prism.highlightElement)
</script>

Second argument to CodeJar is highligting function (in this example PrismJS), but any function may be used:

const highlight = (editor: HTMLElement) => {
  const code = editor.textContent
  // Do something with code and set html.
  editor.innerHTML = code
}

let jar = new CodeJar(editor, highlight)

Third argument to CodeJar is options:

let options = {
  tab: ' '.repeat(4), // default is \t
}

let jar = new CodeJar(editor, highlight, options)

Some styles may be applied to our editor to make it better looking:

.editor {
    border-radius: 6px;
    box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2);
    font-family: 'Source Code Pro', monospace;
    font-size: 14px;
    font-weight: 400;
    height: 340px;
    letter-spacing: normal;
    line-height: 20px;
    padding: 10px;
    tab-size: 4;
}

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()

destroy()

Removes event listeners from editor.

Related

License

MIT

About

An embeddable code editor for the browser 🍯

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 89.2%
  • HTML 10.8%