Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stagas/textarea-code
Web Component that extends a textarea element with code editor behavior.
https://github.com/stagas/textarea-code
code custom-elements editor textarea
Last synced: 6 days ago
JSON representation
Web Component that extends a textarea element with code editor behavior.
- Host: GitHub
- URL: https://github.com/stagas/textarea-code
- Owner: stagas
- License: mit
- Created: 2021-12-16T08:35:19.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-03T02:14:02.000Z (over 2 years ago)
- Last Synced: 2024-08-11T10:51:43.118Z (3 months ago)
- Topics: code, custom-elements, editor, textarea
- Language: TypeScript
- Homepage:
- Size: 90.8 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
textarea-codeWeb Component that extends a textarea element with code editor behavior.
npm i textarea-code
pnpm add textarea-code
yarn add textarea-code
## Examples
#
web
- #
view source
example/web.ts
```ts
import { TextAreaCodeElement } from 'textarea-code'
customElements.define('textarea-code', TextAreaCodeElement, {
extends: 'textarea',
})
document.body.innerHTML = /*html*/ `
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;
}
`
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
# constructor
(textarea, insert, options) src/buffer.ts#L24
# new Buffer
()
options
= {}
Partial<Options>
insert
src/buffer.ts#L21 # options
src/buffer.ts#L22 # numberOfLines
src/buffer.ts#L42 tab
src/buffer.ts#L38 value
src/buffer.ts#L34 deleteLine
() src/buffer.ts#L362
deleteLine() =>
- void
duplicate
() src/buffer.ts#L334
duplicate() =>
- void
getArea
({ end, start }) src/buffer.ts#L112 {
# end
src/buffer.ts#L112
start
src/buffer.ts#L112 } getArea({ end, start }) =>
- [ number, number ]
getLineCol
(n) src/buffer.ts#L83 # getPositionFromLineCol
(pos) src/buffer.ts#L98 # getRange
() src/buffer.ts#L66
getRange() =>
indent
(unindent) src/buffer.ts#L241 # unindent
boolean
indent(unindent) =>
- void
lineAt
(line) src/buffer.ts#L50 # line
number
lineAt(line) =>
- string
moveCaretEnd
(withSelection) src/buffer.ts#L264 # withSelection
boolean
moveCaretEnd(withSelection) =>
- void
moveCaretHome
(withSelection) src/buffer.ts#L257 # withSelection
boolean
moveCaretHome(withSelection) =>
- void
moveCaretLines
(lines, withSelection) src/buffer.ts#L272 # moveCaretTo
(Point, selection, direction) src/buffer.ts#L124 null
| Point
direction
= ...
"forward"
| "backward"
| "none"
moveCaretTo(Point, selection, direction) =>
- void
moveLines
(diff) src/buffer.ts#L281 # diff
number
moveLines(diff) =>
- void
replaceBlock
(replacer) src/buffer.ts#L144 # replacer
(text, startLine)
# text
string
startLine
number
replacer(text, startLine) =>
- {
# diff
src/buffer.ts#L145
number
left
src/buffer.ts#L145 # text
src/buffer.ts#L145 string
replaceBlock(replacer) =>
- void
scrollIntoView
(pos) src/buffer.ts#L54 # pos
number
scrollIntoView(pos) =>
- void
setSelectionRange
(start, end, direction) src/buffer.ts#L118 # toggleDoubleComment
() src/buffer.ts#L210
toggleDoubleComment() =>
- void
toggleSingleComment
() src/buffer.ts#L186
toggleSingleComment() =>
- void
getLineCol
(value, n) src/buffer.ts#L85 # getPositionFromLineCol
(value, point) src/buffer.ts#L102 # lineAt
(value, line) src/buffer.ts#L46 # TextAreaCodeElement
src/textarea-code.ts#L10 # constructor
(args)
$
src/sigl/dist/types/sigl.d.ts#L25 Context<TextAreaCodeElement & JsxContext<TextAreaCodeElement> & Omit<{
# ctor
Class<T>
<T>(ctor) =>
CleanClass<T>
ctx
<T>(ctx) =>
Wrapper<T>
# Boolean
src/sigl/dist/types/index.d.ts#L9
undefined | boolean
Number
src/sigl/dist/types/index.d.ts#L8 undefined | number
String
src/sigl/dist/types/index.d.ts#L7 undefined | string
"transition"
>> buffer
src/textarea-code.ts#L15 # comments
= '// /* */'
src/textarea-code.ts#L13 string
context
src/sigl/dist/types/sigl.d.ts#L26 ContextClass<TextAreaCodeElement & JsxContext<TextAreaCodeElement> & Omit<{
# ctor
Class<T>
<T>(ctor) =>
CleanClass<T>
ctx
<T>(ctx) =>
Wrapper<T>
# Boolean
src/sigl/dist/types/index.d.ts#L9
undefined | boolean
Number
src/sigl/dist/types/index.d.ts#L8 undefined | number
String
src/sigl/dist/types/index.d.ts#L7 undefined | string
"transition"
>> dispatch
src/sigl/dist/types/events.d.ts#L4 Dispatch<# (name, detail, init)
host
src/sigl/dist/types/sigl.d.ts#L24 # lineHeight
= 16
src/textarea-code.ts#L18 number
onKeyDown
src/textarea-code.ts#L19 EventHandler<TextAreaCodeElement, KeyboardEvent>
onmounted
EventHandler<TextAreaCodeElement, CustomEvent<any>>
onunmounted
EventHandler<TextAreaCodeElement, CustomEvent<any>>
pageSize
src/textarea-code.ts#L16 number
tabSize
= 2
src/textarea-code.ts#L11 number
tabStyle
= 'spaces'
src/textarea-code.ts#L12 "spaces"
| "tabs"
viewHeight
src/textarea-code.ts#L17 number
created
(ctx) # ctx
Context<TextAreaCodeElement & JsxContext<TextAreaCodeElement> & Omit<{
# ctor
Class<T>
<T>(ctor) =>
CleanClass<T>
ctx
<T>(ctx) =>
Wrapper<T>
# Boolean
src/sigl/dist/types/index.d.ts#L9
undefined | boolean
Number
src/sigl/dist/types/index.d.ts#L8 undefined | number
String
src/sigl/dist/types/index.d.ts#L7 undefined | string
"transition"
>> created(ctx) =>
- void
mounted
($) src/textarea-code.ts#L21 # $
Context<TextAreaCodeElement & JsxContext<TextAreaCodeElement> & Omit<{
# ctor
Class<T>
<T>(ctor) =>
CleanClass<T>
ctx
<T>(ctx) =>
Wrapper<T>
# Boolean
src/sigl/dist/types/index.d.ts#L9
undefined | boolean
Number
src/sigl/dist/types/index.d.ts#L8 undefined | number
String
src/sigl/dist/types/index.d.ts#L7 undefined | string
"transition"
>> mounted($) =>
- void
on
(name) # name
on<K>(name) =>
On<Fn<[ EventHandler<TextAreaCodeElement, LifecycleEvents & object [K]> ], Off>>
toJSON
()
toJSON() =>
Pick<TextAreaCodeElement, keyof TextAreaCodeElement>
Options
src/buffer.ts#L11 # comments
src/buffer.ts#L14
[ string, string, string ]
tabSize
src/buffer.ts#L13 number
tabStyle
src/buffer.ts#L12 "spaces"
| "tabs"
Point
src/types.ts#L3 # col
src/types.ts#L5
number
line
src/types.ts#L4 number
SelectionDirection
src/types.ts#L1 "forward"
| "backward"
| "none"
## Credits
- [sigl](https://npmjs.org/package/sigl) by [stagas](https://github.com/stagas) – Web framework
## Contributing
[Fork](https://github.com/stagas/textarea-code/fork) or [edit](https://github.dev/stagas/textarea-code) and submit a PR.
All contributions are welcome!
## License
MIT © 2022 [stagas](https://github.com/stagas)