Giter Site home page Giter Site logo

taufik-nurrohman / rich-text-editor Goto Github PK

View Code? Open in Web Editor NEW
30.0 4.0 10.0 200 KB

A rich text editor that is designed to accept limited set of inline HTML tags only.

Home Page: http://taufik-nurrohman.js.org/rich-text-editor

License: MIT License

JavaScript 100.00%
rich text editor js javascript wysiwyg simple inline custom

rich-text-editor's Introduction

Rich Text Editor

A rich text editor that is designed to accept limited set of inline HTML tags only.

This rich text editor works best on any form that are accessible in the public such as comment form in a blog, chat form in a social media and question or answer form in a forum.

Rich Text Editor

Demo

Usage

<!DOCTYPE html>
<html dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Demo</title>
    <link href="rich-text-editor.min.css" rel="stylesheet">
  </head>
  <body>
    <textarea></textarea>
    <script src="rich-text-editor.min.js"></script>
    <script>
    var editor = new RTE(document.querySelector('textarea'));
    </script>
  </body>
</html>

Options

var editor = new RTE(source, config);
Variable Description
source The text area element.
config The configuration data. See below!
config = {
    classes: ['rich-text-editor'],
    tools: ['b', 'i', 'u', 'a', 'x'], // visible tool(s)
    tags: [ // allowed HTML tag(s)
        'a',
        'abbr',
        'b',
        'br',
        'code',
        'dfn',
        'del',
        'em',
        'i',
        'ins',
        'kbd',
        'mark',
        'p',
        'span',
        'strong',
        'u',
        'var'
    ],
    attributes: [ // allowed HTML attribute(s)
        'class',
        'data-[\\w-]+?',
        'href',
        'id',
        'rel',
        'style',
        'target',
        'title'
    ],
    text: {
        b: ['Bold', 'B', '⌘+B'],
        i: ['Italic', 'I', '⌘+I'],
        u: ['Underline', 'U', '⌘+U'],
        a: ['Link', 'A', '⌘+L'],
        x: ['Source', '&#x22ef;', '⌘+⇧+X']
    },
    tidy: true, // tidy HTML output?
    enter: true, // set to `false` to automatically submit the closest form on enter key press
    x: function(e, node) {}, // on mode change (view/source); set to `false` to disable the source view
    update: function(e, node) {} // on view/source update
};

Note: All block tags are not allowed except <p> tags without attributes.

Methods

Interactions

editor.focus();
editor.focus(0); // focus start
editor.focus(1); // focus end
editor.focus(true); // select all
editor.blur();
editor.enable();
editor.disable();
editor.create();
editor.destroy();

Properties

editor.lot; // selection storage [source, view]
editor.config; // editor configuration
editor.container; // editor container
editor.view; // editor view
editor.source; // editor source
editor.tools; // editor tools
editor.dialog; // editor dialog

Set Value

editor.set('Lorem ipsum dolor sit amet.');

Get Value

console.log(editor.get());

Save Selection

var s = editor.s();

Restore Selection

editor.r(s);

Get Selection

editor.v(); // as plain text
editor.v(true); // as HTML
editor.v(true, false); // as original selected HTML value in `editor.view`
editor.v(true, true, false); // as HTML and remove the wrapping `<p>` tag

Wrap Selection with HTML Element

editor.w('strong'); // toggle wrap/unwrap `<strong>` tag
editor.w('strong', 1); // force wrap `<strong>` tag
editor.w('strong', 0); // force unwrap `<strong>` tag

Wrap selection with HTML element and add attributes on that element:

var e = editor.w('a');
e.href = 'http://example.com';
e.rel = 'nofollow';
e.target = '_blank';

Collapse Selection

editor.c(0); // collapse to the start of the selection
editor.c(1); // collapse to the end of the selection

Insert HTML at Caret/Selection

editor.i('<img arc="file.png">', true); // select the inserted HTML
editor.i('<img arc="file.png">', 0); // put caret after the inserted HTML (insert before caret)
editor.i('<img arc="file.png">', 1); // put caret before the inserted HTML (insert after caret)

Get Selected HTML Node

editor.e(); // return list of nodes in the current selection
editor.e('a'); // check if the selected text is an `<a>` tag

Sanitize HTML

editor.f('foo bar <b>baz</b> <span>qux</span>');

Get Selection as Node

console.log(editor.n());

Create Selection Range

Create selection range from A node to B node.

var p = editor.view.querySelectorAll('p');
editor.m([p[0], p.pop()]);

Tool State

if (editor.is.e('strong')) {
    // caret was selecting a `<strong>` tag or is placed in a `<strong>` tag
}

Tool

Create

// id: the tool ID
// text: array of [title, content, description]
// fn: the function that will be triggered on click
// i: tool index from the current tool list (if not defined, tool will be put at the end of the list)
//
// editor.t(id, text, fn, i);

Create a bold button:

editor.t('b', ['Bold', '<b>B</b>', 'Ctrl+B'], function(e, node) {  });

Dialog

Create

// fn(e, input);
editor.d(placeholder, value, fn);

Show

editor.d.v();
editor.d.v(true); // save previous selection

Hide

editor.d.x();
editor.d.x(true); // restore previous selection
editor.d.x(true, true); // ignore the error, exit anyway

State

Check whether these elements are visible at the time:

editor.is.view; // the rich text editor view
editor.is.source; // the HTML source view
editor.is.dialog; // the dialog view
editor.is.focus; // check if cursor is active in the view
editor.is.blur; // check if cursor is not active in the view
editor.is.error; // check if there was an error

rich-text-editor's People

Contributors

renovate-bot avatar taufik-nurrohman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rich-text-editor's Issues

TODO: Stupid Simple Rich Text Editor

I think it’s the time to make this rich text editor API to follow this text editor API.

There shouldn’t be any default controls available. This application should only serve as a helper to replace <textarea> element into a normalized <div contenteditable> element.

Editor controls, keyboard key handling and editor source feature should be created as a separate extensions using the exposed methods.


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Paste on Mobile removes pasted content immediately

When tested on Apple and Android mobile devices (both on chrome so far) the paste function is not working. As far I can tell the function is being triggered (I extended the paste delay) but as soon as the delay is up it seems the content is not found in "view" and is removed.

CTRL+B to start formatting bold „on the fly”

I'm absolutely new to this, please be kind in reading my request
As I observed, formatting is only possibile by selecting text and then aplying format

I the above is correct, I would suggest implementing „start formatting (bold, italic)” by pressing CTRL+(B, I) - then typing - followed ofcourse by „stop formating (bold, italic) = CTRL+(B,I)

In other words, CTRL+B would insert [b] or [/b] - according to the editor.state

Example CTRL+B bold CTRL+B normal text


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Suppor for list elements

Hi, I really like the way the editor is simple, light -weight.
I'm just wondering why doesn't this have a provision for list elements and what does it takes to add list elements to the same?

Thanks
Sujeet

Positioning

How can I set the (absolute) position of the editor? There are no top, left, height, width properties.
I currently use $('.rich-text-editor.view.expand').css('top','160px'); but this doesn't work if there is more than one editor since they all have the same classes and there are no id or name attributes to differentiate between them.

Select the Whole Node on Wrap State

The workaround is currently hacky, but finally I have managed to make the selection wraps to the outside of the inserted node. Like this:

select-node-outer

|<strong>text here</strong>|

Native document.execCommand method wraps the selection inside the inserted node:

native

<b>|text here|</b>

Bug of Insert After’s Cursor Visibility

Current

Works fine just after doing the insert before thing.

error-select-after

Expected

error-select-after-expect

When the cursor selects some text, it will automatically hide the blink effect. Look like the bug was caused by a zero-width selection of … something.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.