Need help with cl-editor?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

nenadpnc
202 Stars 15 Forks MIT License 196 Commits 8 Opened issues

Description

Lightweight text editor built with svelte, typescript

Services available

!
?

Need anything else?

Contributors list

Lightweight text editor

Built with svelte (no external dependencies)

File size (bundle includes css, html and js)

  • min: 30kb
  • gzip: 10kb

Installation

npm:

npm install --save cl-editor

HTML:

  ...


...

...

Usage

import Editor from 'cl-editor';
// or
const Editor = require('cl-editor');
// Initialize editor
const editor = new Editor({
    //  required
    target: document.getElementById('editor'),
    // optional
    props: {
        //  string if overwriting, object if customizing/creating
        // available actions:
        // 'viewHtml', 'undo', 'redo', 'b', 'i', 'u', 'strike', 'sup', 'sub', 'h1', 'h2', 'p', 'blockquote', 
        // 'ol', 'ul', 'hr', 'left', 'right', 'center', 'justify', 'a', 'image', 'forecolor', 'backcolor', 'removeFormat'
        actions: [
            'b', 'i', 'u', 'strike', 'ul', 'ol',
            {
                name: 'copy', // required
                icon: 'C', // string or html string (ex. ...)
                title: 'Copy',
                result: () => {
                    // copy current selection or whole editor content
                    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'
        ],
        // default 300px
        height: '300px',
        // initial html
        html: '',
        // remove format action clears formatting, but also removes some html tags.
        // you can specify which tags you want to be removed.
        removeFormatTags: ['h1', 'h2', 'blackquote'] // default
    }
})

API

// Methods
editor.exec(cmd: string, value?: string) // execute document command (document.executeCommand(cmd, false, value))
editor.getHtml(sanitize?: boolean) // returns html string from editor. if passed true as argument, html will be sanitized before return
editor.getText() // returns text string from editor
editor.setHtml(html: string, sanitize?: boolean) // sets html for editor. if second argument is true, html will be sanitized
editor.saveRange() // saves current editor cursor position or user selection
editor.restoreRange() // restores cursor position or user selection
// saveRange and restoreRange are useful when making custom actions
// that demands that focus is shifted from editor to, for example, modal window.
  • For list of available exec command visit https://codepen.io/netsi1964/pen/QbLLG
    js
    // Events
    editor.$on('change', (event) => console.log(event)) // on every keyup event
    editor.$on('blur', (event) => console.log(event)) // on editor blur event
    
    js
    // Props
    editor.refs. // references to editor, raw (textarea), modal and colorPicker HTMLElements
    

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.:
js
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
.

{@html html} 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:

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: - Alex-D/Trumbowyg - jaredreich/pell

Licence

MIT License

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.