Lightweight code editor Web Component with syntax highlighting
npm i code-edit
|
pnpm add code-edit
|
yarn add code-edit
|
webview source
import 'code-syntax/themes/default.css'
import 'plenty-themes/laser.css'
import { CodeEditElement, languages } from 'code-edit'
languages.js = import('code-syntax/languages/js')
customElements.define('code-edit', CodeEditElement)
const style = document.createElement('style')
style.textContent = /*css*/ `
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
body {
background: #333;
}
pre {
color: var(--color);
background: var(--background);
}
code-edit {
margin: 15px;
padding: 15px;
resize: both;
}
`
document.head.appendChild(style)
document.body.innerHTML = /*html*/ `
<code-edit id="demo" autoresize autofocus language="js" theme="laser">export interface HTMLCodeEditElement {
value?: string
language?: string
syntax?: SyntaxDefinition | Promise<{ default: SyntaxDefinition }>
theme?: string
tabsize?: number
tabstyle?: 'tabs' | 'spaces'
comments?: string
}</code-edit>
`
CodeEditElement – CodeEdit custom element
import { CodeEditElement } from 'code-edit'
customElements.define('code-edit', CodeEditElement)
<code-edit autofocus autoresize language="js" theme="deep"> ... </code-edit>
constructor()
new CodeEditElement() autoFocus = false – Autofocus to the element when page loadsboolean
autoResize = false – Whether to autoresize to the height of the contentsboolean
language = '' – Language for syntax highlightingstring
syntax = {} – Passing a syntax definition regexp manuallySyntaxOrImport
tabSize = 2 – Tab sizenumber
tabStyle = 'spaces' – Tab style"spaces" | "tabs"
theme = '' – Theme to usestring
value = '' – The editor's valuestring
scrollLeft scrollTop All contributions are welcome!
#
src/code-edit.tsx#L95comments='// /* */'– Commentstring