Skip to content

stagas/textarea-code

Repository files navigation

textarea-code

Web Component that extends a textarea element with code editor behavior.

npm i textarea-code pnpm add textarea-code yarn add textarea-code

Examples

# web

    Try it live

    # view source example/web.ts

    import { TextAreaCodeElement } from 'textarea-code'
    
    customElements.define('textarea-code', TextAreaCodeElement, {
      extends: 'textarea',
    })
    
    document.body.innerHTML = /*html*/ `
    <style>
      html,
      body {
        width: 100%;
        height: 100%;
        padding: 0;
        margin: 0;
      }
    
      body,
      textarea {
        background: #292827;
        color: #fff;
        caret-color: #fff;
      }
    
      textarea {
        font-family: monospace;
        font-size: 10pt;
        line-height: 16px;
        padding: 0;
        margin: 0;
      }
    </style>
    <textarea id="output" is="textarea-code" rows="15" cols="60" tab-size="2" tab-style="spaces"></textarea>
    `
    
    const output = document.getElementById('output') as TextAreaCodeElement
    
    output.textContent = `\
    if (e.altKey || (cmdKey && e.shiftKey)) {
      if (['ArrowUp', 'ArrowDown', 'PageUp', 'PageDown'].includes(e.key)) {
        e.preventDefault()
        this.buffer.moveLines(
          {
            ArrowUp: -1,
            ArrowDown: 1,
            PageUp: -this.pageSize,
            PageDown: this.pageSize,
          }[e.key as 'ArrowUp']
        )
        return
      }
    }
    `

API

# Buffer src/buffer.ts#L19
# TextAreaCodeElement src/textarea-code.ts#L10
# Options src/buffer.ts#L11
# Point src/types.ts#L3
# SelectionDirection src/types.ts#L1

    "forward" | "backward" | "none"

Credits

Contributing

Fork or edit and submit a PR.

All contributions are welcome!

License

MIT © 2022 stagas