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.

Lihat di GitHub
version license vanilla js no dependencies size
v1.0.0  ·  by Refael Sinaga

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.

HTML (textarea value)
Public page (sanitize)

Size + Color only

tools: ['size', 'color']

No toolbar

tools: [] — Enter tetap jalan.


API Reference

const editor = OnvTextEditor.init(id, options)
#ParameterTypeDescription
1 id string | HTMLTextAreaElement Textarea id ('desc' or '#desc') or the element
2 options object Optional, see the options table below

Options

KeyTypeDefaultDescription
toolsstring[]allToolbar buttons to show. Order always follows the toolbar.
colorstring'#000000'Starting value of the color picker
sizenumber16Starting value of the font size input (px)

Tools

ValueDescription
'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 / MethodDescription
getHTML()Current HTML (same as textarea.value)
setHTML(html)Load content, e.g. after fetching data for an edit form
elementWrapper 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.

KindAllowed
Tagsb strong i em u br p div span ul ol li a img iframe
Stylescolor · font-size (px) · text-align · width (% / px, img & iframe only)
a hrefhttps:// · http:// · mailto: · tel:, always opens in a new tab
img srchttps:// · http:// · site path /...
iframe srcYouTube and Vimeo embeds only

Theme Variables

VariableDefault
--onvs-texteditor-border#d0d5dd
--onvs-texteditor-focus#1a4099
--onvs-texteditor-bg#fff
--onvs-texteditor-bar-bg#f6f7f9
--onvs-texteditor-textinherit
--onvs-texteditor-btn-hover#e9ecf2
--onvs-texteditor-btn-active#dbe4ff
--onvs-texteditor-placeholder#98a2b3
--onvs-texteditor-radius6px
--onvs-texteditor-min-height120px

Behavior