Skip to content
A configurable rich text editor πŸ“„βœ
JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode
cypress
example
src
.eslintrc.js
.gitignore
.huskyrc.json
LICENSE
README.md
azure-ci-build.yml
azure-pr-build.yml
babel.config.js
cypress.json
jest.config.js
package-lock.json
package.json
prettier.config.js

README.md

Build Status Cypress.io tests jest npm version

Story πŸŽ‰πŸ’»

Well, I know there are lot's of powerful richtext editors out in the wild! But I couldn't skip the fun parts, so I created one beacuase I needed something for my blog. This editor works based on contenteditable but I have avoided to use execCommand. You can check out the demo here. Demo

Getting Started

First of all you need an empty element on the page to handle the editor. It could be a <div> or an <article>:

<article id="richtext"></article>

To use the richtext you can both download it or install it from the npm.

JS File

Download the last version from here. And:

<script src="richtext.js"></script>
<script>
  var rt = Richtext.create(document.getElementById('richtext'), options); // See below for configuring options.
  // apply the styles on UI events. See below.
</script>

NPM

You can also install Richtext from npm:

npm i @alidoustkani/richtext
import {create} from '@alidoustkani/richtext'

const rt = create(document.getElementById('richtext'), options) // see below for options

API & Options

There are two kinds of methods in richtext: style() and apply(). style() applies the style to the selected text, and if there is no selection there won't be any style. apply() applies the style to the selected text and when there is no selection available it will add the style to the whole paragraph.

{
  defaulLink: 'https://',
  staySelected: true,
  decors: {
    important: 'strong', 
    highlight: {
      tag: 'span', 
      className: 'highlight-class-name'
    },
    notebox: {
      parent: true,
      tag: 'div',
      className: 'notebox-class-name'
    }
  }
}

Funcionalities

  • {Ctrl}ENTER: creates the next <p> editor and goes to it.
  • {Ctrl}SPACE: appends a RLM to the current position.

Options

  • defaultLink: default text in the link popup.
  • staySelected: for true value, the selection will remain the same after applying styles.
  • decors: styling configuration. Use this option to define your styling elements and css classes.

API

  • apply(type)
  • applyUnorderedList()
  • applyOrderedList()
  • applyCodebox()
  • selectImage()
  • setOptions(options)
  • style(type)
  • styleLink()

Example

import {create} from '@alidoustkani/richtext'

const rt = create(document.getElementById('richtext'), {
  defaulLink: 'https://',
  staySelected: true,
  decors: {
    important: 'strong', 
    highlight: {
      tag: 'span', 
      className: 'highlight-class-name'
    },
    notebox: {
      parent: true,
      tag: 'div',
      className: 'notebox-class-name'
    }
  }
})

importantBtn.addEventListener('click', () => {
  rt.style('important') // <strong>TEXT</strong>
})

highlightBtn.addEventListener('click', () => {
  rt.style('highlight') // <span class="highlight-class-name">TEXT</span>
})

noteboxBtn.addEventListener('click', () => {
  rt.apply('notebox') // <div class="notebox-class-name">TEXT</div>
})

linkBtn.addEventListener('click', () => {
  rt.styleLink() // <a href="LINK">TEXT</a>
})

ulBtn.addEventListener('click', () => {
  rt.applyUnorderedList() // <ul><li>TEXT</li></ul>
})

olBtn.addEventListener('click', () => {
  rt.applyOrderedList() // <ol><li>TEXT</li></ol>
})

imageBtn.addEventListener('click', () => {
  rt.selectImage() // <figure><img src="SRC"><figcaption>CAPTION</figcaption></figure>
})

License

MIT License

You can’t perform that action at this time.