MCPcopy
hub / github.com/antonmedv/codejar

github.com/antonmedv/codejar @4.3.0 sqlite

repository ↗ · DeepWiki ↗ · release 4.3.0 ↗
37 symbols 110 edges 2 files 0 documented · 0%
README

CodeJar – an embeddable code editor for the browser

npm npm bundle size

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 🍯:






<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: string replaces "tabs" with given string. Default: \t. - Note: use css rule tab-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. Default false. - catchTab: boolean catches Tab keypress events and replaces it with tab string. Default: true. - preserveIdent: boolean keeps indent levels on new line. Default true. - addClosing: boolean automatically adds closing brackets, quotes. Default true. - history records history. Default true. - 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.

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

License

MIT

Core symbols most depended-on inside this repo

restore
called by 11
codejar.ts
save
called by 9
codejar.ts
preventDefault
called by 8
codejar.ts
insert
called by 7
codejar.ts
on
called by 6
codejar.ts
recordHistory
called by 6
codejar.ts
getSelection
called by 6
codejar.ts
toString
called by 5
codejar.ts

Shape

Function 37

Languages

TypeScript100%

Modules by API surface

codejar.ts33 symbols
cursor.ts4 symbols

Dependencies from manifests, versioned

babel-minify0.5.2 · 1×
gzip-size-cli5.1.0 · 1×
release-it16.1.3 · 1×
typescript5.1.6 · 1×

For agents

$ claude mcp add codejar \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact