Vanilla JS · No Dependencies
onvshark-texteditor
Lightweight, zero-dependency rich text editor for any web project. Attach it to a textarea by id and pick the buttons you need.
Installation
<link rel="stylesheet" href="texteditor.css">
<script src="texteditor.js"></script>
Live Demo
All tools
Blok teks lalu pakai toolbar. Klik gambar atau video untuk ubah ukuran.
Size + Color only
tools: ['size', 'color']
No toolbar
tools: [] — Enter tetap jalan.
API Reference
const editor = OnvTextEditor.init(id, options)
| # | Parameter | Type | Description |
| 1 |
id |
string | HTMLTextAreaElement |
Textarea id ('desc' or '#desc') or the element |
| 2 |
options |
object |
Optional, see the options table below |
Options
| Key | Type | Default | Description |
tools | string[] | all | Toolbar buttons to show. Order always follows the toolbar. |
color | string | '#000000' | Starting value of the color picker |
size | number | 16 | Starting value of the font size input (px) |
Tools
| Value | Description |
'bold' | Bold |
'italic' | Italic |
'underline' | Underline |
'ul' | Bullet list |
'ol' | Numbered list |
'align' | Alignment dropdown: left, center, right, justify |
'color' | Text color |
'size' | Font size (px) |
'link' | Link: text + URL |
'image' | Image: URL + alt text |
'video' | YouTube / Vimeo embed |
'fullscreen' | Fullscreen |
'code' | HTML code view |
Return
| Property / Method | Description |
getHTML() | Current HTML (same as textarea.value) |
setHTML(html) | Load content, e.g. after fetching data for an edit form |
element | Wrapper element (.onvs-texteditor) |
OnvTextEditor.sanitize(html) // clean HTML before showing it on a public page
Code Examples
All tools
const desc = OnvTextEditor.init('desc')
Size + color only
const title = OnvTextEditor.init('title', { tools: ['size', 'color'] })
Many textareas on one page
const title = OnvTextEditor.init('title', { tools: ['size', 'color'] })
const content = OnvTextEditor.init('content')
Load data for an edit form
desc.setHTML(data.description)
Show on a public page
el.innerHTML = OnvTextEditor.sanitize(html)
Sanitize
Everything not listed is removed, including scripts, onclick / onerror and javascript: links.
| Kind | Allowed |
| Tags | b strong i em u br p div span ul ol li a img iframe |
| Styles | color · font-size (px) · text-align · width (% / px, img & iframe only) |
a href | https:// · http:// · mailto: · tel:, always opens in a new tab |
img src | https:// · http:// · site path /... |
iframe src | YouTube and Vimeo embeds only |
Theme Variables
| Variable | Default |
--onvs-texteditor-border | #d0d5dd |
--onvs-texteditor-focus | #1a4099 |
--onvs-texteditor-bg | #fff |
--onvs-texteditor-bar-bg | #f6f7f9 |
--onvs-texteditor-text | inherit |
--onvs-texteditor-btn-hover | #e9ecf2 |
--onvs-texteditor-btn-active | #dbe4ff |
--onvs-texteditor-placeholder | #98a2b3 |
--onvs-texteditor-radius | 6px |
--onvs-texteditor-min-height | 120px |
Behavior
- The textarea is hidden and always holds the sanitized HTML, so a normal form submit sends it.
- One editor per textarea. Calling
init again returns the existing editor.
- Font size: select text, then use the +/- arrows or type a number.
- Link, image and video open the editor's own modal. Enter saves, Esc cancels.
- Cursor inside a link + Link button edits that link. An empty URL removes it.
- Click an image or video to set width 25% / 50% / 75% / 100%, original size, or delete.
- Paste is always plain text.
- Image upload is not supported yet. Use an image URL.