GithubHelp home page GithubHelp logo

daniel-lewis-ab / codejar Goto Github PK

View Code? Open in Web Editor NEW

This project forked from antonmedv/codejar

0.0 0.0 0.0 76 KB

An embeddable code editor for the browser ๐Ÿฏ

Home Page: https://medv.io/codejar/

License: MIT License

TypeScript 90.67% HTML 9.33%

codejar's Introduction

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 ๐Ÿฏ:

<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: 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.
const options = {
  tab: ' '.repeat(4), // default is '\t'
  indentOn: /[(\[]$/, // 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

Become a sponsor

Every line of code in my repositories ๐Ÿ“– signifies my unwavering commitment to open source ๐Ÿ’ก. Your support ๐Ÿค ensures these projects keep thriving, innovating, and benefiting all ๐Ÿ’ผ. If my work has ever resonated ๐ŸŽต or helped you, kindly consider showing love โค๏ธ by sponsoring. ๐Ÿš€ Sponsor Me Today! ๐Ÿš€

License

MIT

codejar's People

Contributors

antonmedv avatar junghoocho avatar zamfofex avatar julianpoemp avatar hgs3 avatar bytesnz avatar zazaulola avatar tchan5217 avatar moderatelyconfused avatar mmulet avatar mike76-dev avatar lizheming avatar githubaccount8 avatar aziis98 avatar szepeviktor avatar timwillebrands avatar subhendudash02 avatar xyzshantaram avatar ryanhaoo avatar robertlyall avatar prestonhager avatar creekmind avatar blunt1337 avatar gnietschow avatar strd6 avatar 0xflotus avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.