Lightweight text editor
Built with svelte (no external dependencies)
File size (bundle includes css, html and js)
Installation
npm:
npm install --save cl-editor
HTML:
<head>
...
</head>
<body>
...
<div id="editor"></div>
...
</body>
Usage
import Editor from 'cl-editor';
const Editor = require('cl-editor');
const editor = new Editor({
target: document.getElementById('editor'),
props: {
actions: [
'b', 'i', 'u', 'strike', 'ul', 'ol',
{
name: 'copy',
icon: '<b>C</b>',
title: 'Copy',
result: () => {
const selection = window.getSelection();
if (!selection.toString().length) {
const range = document.createRange();
range.selectNodeContents(editor.refs.editor);
selection.removeAllRanges();
selection.addRange(range);
}
editor.exec('copy');
}
},
'h1', 'h2', 'p'
],
height: '300px',
html: '',
removeFormatTags: ['h1', 'h2', 'blackquote']
}
})
API
editor.exec(cmd: string, value?: string)
editor.getHtml(sanitize?: boolean)
editor.getText()
editor.setHtml(html: string, sanitize?: boolean)
editor.saveRange()
editor.restoreRange()
editor.$on('change', (event) => console.log(event))
editor.$on('blur', (event) => console.log(event))
editor.refs.<editor | raw | modal | colorPicker>
Actions
The actions
prop lists predefined actions (and/or adds new actions) to be shown in the toolbar.
If the prop is not set, all actions
defined and exported in actions.js are made available, in the order in which they are defined.
To limit or change the order of predefined actions shown, set it by passing an array of names of actions defined, eg.:
actions={["b", "i", "u", "h2", "ul", "left", "center", "justify", "forecolor"]}
The editor looks up to see if name is already defined, and adds it to the toolbar if it is.
You can add a custom action by inserting it in the array, like how "copy" is defined in example above. Take a look at actions.js
for more examples.
Usage in Svelte
It is easier to import and work directly from the source if you are using Svelte. You can handle change
events via on:change
.
<script>
import Editor from "cl-editor/src/Editor.svelte"
let html = '<h3>hello</h3>'
let editor
</script>
{@html html}
<Editor {html} on:change={(evt)=>html = evt.detail}/>
Example of customising the color picker palette
<script>
import Editor from "cl-editor/src/Editor.svelte"
let html = '<h3>hello</h3>'
let colors = ['#000000', '#e60000', '#ff9900', '#ffff00', '#008a00', '#0066cc', '#9933ff',
'#ffffff', '#facccc', '#ffebcc', '#ffffcc', '#cce8cc', '#cce0f5', '#ebd6ff',
'#bbbbbb', '#f06666', '#ffc266', '#ffff66', '#66b966', '#66a3e0', '#c285ff',
'#888888', '#a10000', '#b26b00', '#b2b200', '#006100', '#0047b2', '#6b24b2',
'#444444', '#5c0000', '#663d00', '#666600', '#003700', '#002966', '#3d1466']
let editor
</script>
{@html html}
<Editor {html} {colors} on:change={(evt)=>html = evt.detail}/>
To limit or define the tools shown in the toolbar, pass in an actions
prop.
To easily get the editor content DOM element, pass an contentId
prop, eg. contentId='notes-content'
.
This is useful if you want to listen to resize of the editor and respond accordingly.
To do so, first enable resize on the editor:
.cl-content {
resize: both;
}
Now observe the resize:
<script>
const ro = new ResizeObserver(entries => {
const contentWd = entries[0].contentRect.width
})
let editor
$: editor && ro.observe(document.getElementById('notes-content'))
</script>
<Editor {...otherEditorCfgs} contentId='notes-content' bind:this={editor} />
Run demo
git clone https://github.com/nenadpnc/cl-text-editor.git cl-editor
cd cl-editor
npm i
npm run dev
References
This library is inspired by these open source repos:
Licence
MIT License