WYSIWYG editor developed as jQuery plugin

Overview

RichText

WYSIWYG editor developed as jQuery plugin.

Requirements

  • jQuery (v.3+, v.3.2+ recommended)
  • FontAwesome (v.4.7.0 / v.5+)
  • src/jquery.richtext.min.js
  • src/richtext.min.css

Initialize editor

Simply call .richText() on your jQuery('textarea') or jQuery('input') field (other HTML tags are allowed as well, but not recommended).

Options

.richText() allows several options to be set, the default option object is:

$(element).richText({

  // text formatting
  bold: true,
  italic: true,
  underline: true,

  // text alignment
  leftAlign: true,
  centerAlign: true,
  rightAlign: true,
  justify: true,

  // lists
  ol: true,
  ul: true,

  // title
  heading: true,

  // fonts
  fonts: true,
  fontList: [
      "Arial", 
      "Arial Black", 
      "Comic Sans MS", 
      "Courier New", 
      "Geneva", 
      "Georgia", 
      "Helvetica", 
      "Impact", 
      "Lucida Console", 
      "Tahoma", 
      "Times New Roman",
      "Verdana"
  ],
  fontColor: true,
  fontSize: true,

  // uploads
  imageUpload: true,
  fileUpload: true,

  // media
  videoEmbed: true,

  // link
  urls: true,

  // tables
  table: true,

  // code
  removeStyles: true,
  code: true,

  // colors
  colors: [],

  // dropdowns
  fileHTML: '',
  imageHTML: '',

  // translations
  translations: {
      'title': 'Title',
      'white': 'White',
      'black': 'Black',
      'brown': 'Brown',
      'beige': 'Beige',
      'darkBlue': 'Dark Blue',
      'blue': 'Blue',
      'lightBlue': 'Light Blue',
      'darkRed': 'Dark Red',
      'red': 'Red',
      'darkGreen': 'Dark Green',
      'green': 'Green',
      'purple': 'Purple',
      'darkTurquois': 'Dark Turquois',
      'turquois': 'Turquois',
      'darkOrange': 'Dark Orange',
      'orange': 'Orange',
      'yellow': 'Yellow',
      'imageURL': 'Image URL',
      'fileURL': 'File URL',
      'linkText': 'Link text',
      'url': 'URL',
      'size': 'Size',
      'responsive': 'Responsive',
      'text': 'Text',
      'openIn': 'Open in',
      'sameTab': 'Same tab',
      'newTab': 'New tab',
      'align': 'Align',
      'left': 'Left',
      'center': 'Center',
      'right': 'Right',
      'rows': 'Rows',
      'columns': 'Columns',
      'add': 'Add',
      'pleaseEnterURL': 'Please enter an URL',
      'videoURLnotSupported': 'Video URL not supported',
      'pleaseSelectImage': 'Please select an image',
      'pleaseSelectFile': 'Please select a file',
      'bold': 'Bold',
      'italic': 'Italic',
      'underline': 'Underline',
      'alignLeft': 'Align left',
      'alignCenter': 'Align centered',
      'alignRight': 'Align right',
      'addOrderedList': 'Add ordered list',
      'addUnorderedList': 'Add unordered list',
      'addHeading': 'Add Heading/title',
      'addFont': 'Add font',
      'addFontColor': 'Add font color',
      'addFontSize' : 'Add font size',
      'addImage': 'Add image',
      'addVideo': 'Add video',
      'addFile': 'Add file',
      'addURL': 'Add URL',
      'addTable': 'Add table',
      'removeStyles': 'Remove styles',
      'code': 'Show HTML code',
      'undo': 'Undo',
      'redo': 'Redo',
      'close': 'Close'
  },
            
  // privacy
  youtubeCookies: false,
    
  // preview
  preview: false,

  // placeholder
  placeholder: '',
  
  // developer settings
  useSingleQuotes: false,
  height: 0,
  heightPercentage: 0,
  adaptiveHeight: false,
  id: "",
  class: "",
  useParagraph: false,
  maxlength: 0,
  callback: undefined,
  useTabForNext: false
});

Text formatting

  • bold (default: (boolean) true) :: Defines if the bold button should be displayed in the editor toolbar
  • italic (default: (boolean) true) :: Defines if the italic button should be displayed
  • underline (default: (boolean) true) :: Displays the underline button

Fonts

  • fonts (default: (boolean) true) :: Enables font formatting
  • fontList :: Array of allowed fonts. The fonts set by default are fonts which should work on Windows, Mac and Linux by default. Setting fonts manually will overwrite the array.
  • fontSize (default: (boolean) true) :: Allows to use different font sizes

Text alignment

  • leftAlign (default: (boolean) true)
  • centerAlign (default: (boolean) true)
  • rightAlign (default: (boolean) true)
  • justify (default: (boolean) true)

Lists

  • ol (default: (boolean) true) :: Ordered list
  • ul (default: (boolean) true) :: Unordered list

Titles

  • heading (default: (boolean) true)

Colors

  • fontColor (default: (boolean) true)
  • colors :: Set own colors for the editor. They will replace the default colors. Example:
var colors;
colors["#FFFFFF"] = 'White';
colors["#000000"] = 'Black';

Uploads/Files

  • imageUpload (default: (boolean) true)
  • fileUpload (default: (boolean) true)

Media/Videos

  • videoEmbed (default: (boolean) true) :: Simplify embedding videos from YouTube, Facebook, Vimeo and Dailymotion

Links

  • urls (default: (boolean) true)

Tables

  • table (default: (boolean) true)

Code

  • removeStyles (default: (boolean) true) :: Allows to remove the CSS styles from the selection
  • code (default: (boolean) true) :: Allows to display the raw HTML code

Custom dropdowns

Custom dropdowns allow to customize in a restricted way the dropdowns in the editor.

  • fileHTML :: HTML string of the file dropdown. MUST include an input field (select, input or textarea) with the id equal to fileURL.
  • imageHTML :: HTML string of the image dropdown. MUST include an input field (select, input or textarea) with the id equal to imageURL.

Translations

  • translations :: An object of key-value entries allowing to set other texts/translations for the editor. The keys must stay the same as in the default translation object.

Privacy settings

  • youtubeCookies (default: (boolean) false) :: If set to true, YouTube might set tracking cookies. By default (if the value is set to false), youtube-nocookie.com will be used to display YouTube videos.

Preview

  • preview (default: (boolean) false) :: If set to true, the contenteditable property is set to false and the toolbar buttons are not loaded.

Placeholder

  • placeholder (default: (string) '') :: If a non-empty string is set, a placeholder will be shown if no text has been written in the contenteditable part of the editor. HTML code without text is considered empty and displays the placeholder.

Tabbing

  • useTabForNext (default: (boolean) false) :: If set to true, you can tab to the next input element or RichText editor within the contenteditable part of the editor.

Developer settings

  • useSingleQuotes (default: (boolean) false) :: Replaces all double quotes from HTML attributes to single quotes, if set to (boolean) true.
  • height (default: (int) 0) :: Sets a custom height for the editor frame and code view. The default value 0 uses the initial height set with CSS. To overwrite the height without using this setting (and without using inline CSS), use the CSS selectors .richText .richText-editor and .richText .richText-initial to change the height.
  • heightPercentage (default: (int) 0) :: Sets a custom percentage height based on the editor's parent element. This won't work if the height option is used as well.
  • adaptiveHeight (default: (boolean) false) :: If true, the height of the editor will be adapted based on the height of the content.
  • id (default: (string) "") :: Sets a custom ID for the editor
  • class (default: (string) "") :: Sets additional custom classes for the editor
  • useParagraph (default: (boolean) false) :: Uses paragraph tags instead of div containers (browser default) when pressing ENTER, if set to true.
  • maxlength (default: (int) 0) :: Defines a max length for the text (HTML length not considered!). The default value 0 doesn't define any limit
  • callback (default: undefined) :: Sets a callback if the editor has been loaded. The first and only parameter of the callback contains the jQuery element of the editor

Undo RichText

There's now the possibility to undo the RichText editor to the state before .richText() has been called.

For this to work, simply call .unRichText() on the initial textarea, on which .richText() has previously been called.

It is possible to delay unRichText() by a given amount of milliseconds with the parameter: {delay: 2000}. Additionally the callback option is available as well.

FAQ

How do I set the RichText value through jQuery? Using .val() doesn't work?

The change event needs to be triggered, in order to update the value within RichText. Using .val('Some text').trigger('change') on your jQuery node will solve your issue.

Contributing

If you have any ideas, suggestions, issues or bugfixes, feel free to contribute.

Check out the contributing guidelines for ways to offer feedback and contribute.

Planned changes

  • Add/remove columns/cells in table after it was created
Comments
  • Issue when using title option

    Issue when using title option

    I am using the text editor for an article creation functionality. There are multiple languages, so there must be an instance of the text editor for each one. I initially thought that the multiple editors on a single page were the cause, but I tried with only a single editor and the problem persists. It is the following: When I use the title option more than 2 or 3 times the content editor element disappears. The toolbars and content remain, but this element <div class="richText-editor" id="richText-xjr0bk" contenteditable="true">{content here}</div> disappears from the page. Also I noted that in the demo page when I use the heading option it changes the current content, but when I use it on my site it duplicates the content. I attached a screenshot of the issue.

    This is the code I use to initialize the richtext editors. And my textearea element doesn't have additional events attached that might mess with the plugin. `$('.richtext').each(function () { var id = $(this).attr('id');

                $(this).richText({
                    // uploads
                    imageUpload: false,
                    fileUpload: false,
                    // link
                    urls: true,
                    //media
                    videoEmbed: false,
                    // translations
                    // dev settings
                    useSingleQuotes: false,
                    id: "richtext-" + id,
                    useParagraph: false
                });
            });`
    

    issue

    opened by pgyordanov 10
  • text editor format appending double quotes conflict with json structure

    text editor format appending double quotes conflict with json structure

    I am loving this Wysiwigg the down of it is that for some reason it's not escaping double quotes correctly and conflicting on the json structure causing it to duplicate the line previously saved.

    If you look at my example the text duplicated, if I go on code view and replace double quotes for single quotes it fixes all and saves again without duplicating the content.

    https://www.dropbox.com/s/qgsxjg8pf0999q0/Screenshot%202017-07-28%2010.27.17.png?dl=0

    https://www.dropbox.com/s/vm0nc6ft66tw217/Screenshot%202017-07-28%2010.27.04.png?dl=0

    THE JSON:

    {"above_price":"48Hrs</font></b> delivery service with SMS or email notification! because your order is cartPriceTotal**\">**You got UK free track <b><font color=\"#000000\">48Hrs</font></b> delivery service with SMS or email notification! because your order is cartPriceTotal"}

    bug enhancement 
    opened by EFXPROMedia 10
  • Formatter not working properly when using ctrl + a

    Formatter not working properly when using ctrl + a

    When I try to remove a formatting style from the whole text in RichText - it does not remove all of it, or at some parts - not removing anything at all. I noticed it, because the text I'm formatting has black color added to it, but the formatter tool doesn't remove it. When selecting the whole text with the mouse is working btw, just when using ctrl + a shortkey.

    bug 
    opened by Phantomrider 8
  • Format remover not working properly?

    Format remover not working properly?

    I'm not sure if it is a bug or not, but when the unformatter tool is always erasing the whole text in the area. Is this made on purpose and if not - is there any chance of implementing the unformatter to work only on selected text?

    bug wontfix 
    opened by Phantomrider 8
  • Editor is not working properly while attaching editor to a textarea returned from ajax

    Editor is not working properly while attaching editor to a textarea returned from ajax

    Editor is not working properly while attaching editor to a textarea from the data returned by ajax. First time ajax request it is working from 2nd time on wards it is not working, is there a way we can clear something like cache etc.

    I am seeing the following error in the console

    jquery.richtext.js:626 Uncaught TypeError: Failed to execute 'selectNodeContents' on 'Range': parameter 1 is not of type 'Node'.
        at saveSelection (jquery.richtext.js:626)
        at doSave (jquery.richtext.js:720)
        at HTMLDivElement.<anonymous> (jquery.richtext.js:341)
        at HTMLDocument.dispatch (jquery-1.12.4.min.js:3)
        at HTMLDocument.r.handle (jquery-1.12.4.min.js:3)
    

    Thanks Kartheek

    bug 
    opened by vneerukattu 8
  • Adding languages versions

    Adding languages versions

    Hi, I am using your amazing plugin, but I have a little problem - every time I update the core files my translations (the project is for non-English speakers) for the plugin are gone, which is a bummer, because I have to translate it again, then minify it and upload it to my project. It would be nice if you can add an array option (or maybe getting langs from stand-alone file) for specific language. This would be great, because it would allow us to use your plugin for multilingual websites.

    enhancement 
    opened by Phantomrider 7
  • Get html text to upload to a database

    Get html text to upload to a database

    I wanted to see how to access the text entered in the textarea and then upload it to a database... another thing... can writing scripts and PHP code be blocked in the textarea?

    opened by RubenRDC 6
  •  Please fix the bug.

    Please fix the bug.

    Describe the problem

    Nice to meet you, I'm Kenta.

    First of all, I'm not familiar with English, so I apologize that English may be wrong.

    I'm currently using "Minimal Rich Text Editor With jQuery And FontAwesome-RichText". There I found a bug.

    Please check the verification video by clicking the URL below. "[external link removed]"

    In the video above The area outside the dragged range is also listed.

    If you do not select the first character of the line, only the dragged line will be listed. Please correct so that even if you select the first character of the line, only the selected range will be a list.

    When you add a character link in a table tag for the same reason If you drag everything, it will be thrown out of the tag. Please click the URL below to check the verification video. [external link removed]

    I am very in trouble. Can you please correct the above two points?

    What version and browser are you using?

    Version 3 chrome Latest

    bug 
    opened by twenty20ty 5
  • How can RichText be incorporated into a GPLv3+ software ?

    How can RichText be incorporated into a GPLv3+ software ?

    Sorry, I am not a lawyer (but living and working in France; and very careful about licensing issues since a free software enthusiast).

    I am developing professionally Bismon. That code is GPLv3+ licensed and copyrighted by my employer CEA, and funded by the European Commission thru two H2020 projects.

    For technical details about Bismon read this draft report (draft of a future H2020 deliverable).

    Bismon is also a web server using libonion. I would like to copy the src/jquery.richtext.js file into Bismon's webroot/ directory. Since your src/jquery.richtext.js file has no copyright notice (as the AGPL license requires) I feel unauthorized to do so.

    I am doing this professionally as I was warned (internally) to be very careful about free software licenses.

    I would appreciate a reply by email to removed regarding this topic. I suggest adding a proper AGPLv3+ copyright notice in every source file.

    Respectful regards and Happy new 2020 year.

    opened by bstarynk 5
  • Runs scripts as you are typing

    Runs scripts as you are typing

    I was testing exploits. Making sure my code didn't allow dangerous stuff to be saved on the back-end. I found something weird with your editor.

    If you toggle into code mode, and type "