codejar

antonmedvMIT长期未更新

一个可嵌入浏览器的轻量级代码编辑器,体积小、无依赖,适合在网页中快速集成代码输入功能。

An embeddable code editor for the browser 🍯

4.3.0发布于 2025-10-14 09:06 UTC1,988star126fork同步于 2026-11-07 07:32 UTC

怎么上手

从 README 代码块原样提取,可能是其中一种安装方式
npm i codejar
硬件要求

README 没有说明硬件要求。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

这个项目的说明还在生成中,先参考下面「最近发版」和 README。

最近发版

5 个版本4.3.0
  1. 4.3.0Release 4.3.0最新正式版2025-10-14 · 2025-10-14 09:06 UTC
    • 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)
  2. 4.2.0Release 4.2.02023-08-03 · 2023-08-03 13:56 UTC
    查看更新说明
    • Respect defaultPrevented in handlePaste (89940be)
  3. 4.1.2Release 4.1.22023-08-02 · 2023-08-02 00:24 UTC
    查看更新说明
    • Place cursor before uneditable element (cfbd05c)
  4. 4.1.1Release 4.1.12023-07-26 · 2023-07-26 13:50 UTC
    查看更新说明
    • Add typesVersions (aa824b1)
  5. 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 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    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.
    • 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