- Overview
- Documents
MediumEditor
This is a clone of medium.com inline editor toolbar.
Since I always had problems with bloated editors and I loved the simplicity of medium.com solution I've tried to implement their WYSIWYG approach with this script.
MediumEditor has been written using vanilla JavaScript, no additional frameworks required.
Tested on Google Chrome, Firefox and IE9+.
First, you need to attach medium editor's stylesheet to your page:
<link rel="stylesheet" href="css/medium-editor.css"> <!-- Core --> <link rel="stylesheet" href="css/themes/default.css"> <!-- or any other theme -->
The next step is to reference the editor's script and instantiate a new MediumEditor object:
<script src="js/medium-editor.js"></script> <script>var editor = new MediumEditor('.editable');</script>
The above code will transform all the elements with the .editable class into HTML5 editable contents and add the medium editor toolbar to them.
You can also pass a list of HTML elements:
var elements = document.querySelectorAll('.editable'), editor = new MediumEditor(elements);
Initialization options
- allowMultiParagraphSelection: enables the toolbar when selecting multiple paragraphs/block elements. Default: true
- anchorInputPlaceholder: text to be shown as placeholder of the anchor input. Default: Paste or type a link
- anchorPreviewHideDelay: time in milliseconds to show the anchor tag preview after the mouse has left the anchor tag. Default: 500
- buttons: the set of buttons to display on the toolbar. Default: ['bold', 'italic', 'underline', 'anchor', 'header1', 'header2', 'quote']
- buttonLabels: type of labels on the buttons. Values: 'fontawesome', {'bold': '<b>b</b>', 'italic': '<i>i</i>'}. Default: false
- delay: time in milliseconds to show the toolbar or anchor tag preview. Default: 0
- diffLeft: value in pixels to be added to the X axis positioning of the toolbar. Default: 0
- diffTop: value in pixels to be added to the Y axis positioning of the toolbar. Default: -10
- disableReturn: enables/disables the use of the return-key. You can also set specific element behavior by using setting a data-disable-return attribute. Default: false
- disableToolbar: enables/disables the toolbar, adding only the contenteditable behavior. You can also set specific element behavior by using setting a data-disable-toolbar attribute. Default: false
- firstHeader: HTML tag to be used as first header. Default: h3
- forcePlainText: Forces pasting as plain text. Default: true
- placeholder: Defines the default placeholder for empty contenteditables. You can overwrite it by setting a data-placeholder attribute on your elements. Default: 'Type your text'
- secondHeader: HTML tag to be used as second header. Default: h4
- targetBlank: enables/disables target="_blank" for anchor tags. Default: false
Example:
var editor = new MediumEditor('.editable', { anchorInputPlaceholder: 'Type a link', buttons: ['bold', 'italic', 'quote'], diffLeft: 25, diffTop: 10, firstHeader: 'h1', secondHeader: 'h2', delay: 1000, targetBlank: true });
Extra buttons
Medium Editor, by default, will show only the buttons listed above to avoid a huge toolbar. There are a couple of extra buttons you can use:
- superscript
- subscript
- strikethrough
- unorderedlist
- orderedlist
- pre
- image (this simply converts selected text to an image tag)
- indent (moves the selected text up one level)
- outdent (moves the selected text down one level)