-
Notifications
You must be signed in to change notification settings - Fork 337
Getting Started
<!-- Library -->
<script src="/path-to-wysihtml5/dist/wysihtml-toolbar.min.js"></script>
<!-- wysihtml5 parser rules -->
<script src="/path-to-wysihtml5/parser_rules/advanced.js"></script>
- The first script is the minified wysihtml library. It’s located in the dist folder of this repository.
- The second script contains the html5 parser rules that are needed for wysihtml in order to create valid and desired markup.
Either use one of the existing parser rules sets or create your own.
Check the advanced.js parser rules for details.
<form>
<textarea id="wysihtml-textarea" placeholder="Enter your text ..." autofocus></textarea>
</form>
<div id="wysihtml-div" data-placeholder="Enter your text ..."></div>
wysihtml takes a textarea or div and transforms it into a rich text editor. The textarea acts as a fallback for unsupported browsers (eg. IE < 8). Make sure the textarea element has an id, so we can later access it easily from javascript.
The resulting rich text editor will much behave and look like the textarea since behavior (placeholder, autofocus, …) and css styles will be copied over.
Please note: The textarea will always hold the editor’s generated markup. Therefore wysihtml integrates smoothly with forms.
<div id="wysihtml-toolbar" style="display: none;">
<a data-wysihtml-command="bold">bold</a>
<a data-wysihtml-command="italic">italic</a>
<!-- Some wysihtml5 commands require extra parameters -->
<a data-wysihtml-command="foreColor" data-wysihtml-command-value="red">red</a>
<a data-wysihtml-command="foreColor" data-wysihtml-command-value="green">green</a>
<a data-wysihtml-command="foreColor" data-wysihtml-command-value="blue">blue</a>
<!-- Some wysihtml5 commands like 'createLink' require extra paramaters specified by the user (eg. href) -->
<a data-wysihtml-command="createLink">insert link</a>
<div data-wysihtml-dialog="createLink" style="display: none;">
<label>
Link:
<input data-wysihtml-dialog-field="href" value="http://" class="text">
</label>
<a data-wysihtml-dialog-action="save">OK</a> <a data-wysihtml-dialog-action="cancel">Cancel</a>
</div>
</div>
The toolbar contains the formatting options. Make sure the toolbar element has an id and has display: none
.
Please note: wysihtml5 supports many more formatting commands. Check the advanced demo or find a full list of all supported commands here.
<script>
var editor = new wysihtml.Editor("wysihtml-textarea", { // id of textarea element
toolbar: "wysihtml-toolbar", // id of toolbar element
parserRules: wysihtml5ParserRules // defined in parser rules set
});
</script>
Make sure you place the
<script>
at the end of the document, before the </body>
tag because the document must be loaded before running the script. Or, test if document is loaded (i.e. jQuery’s $(document).ready()
) and initialize the editor aferwards.
wysihtml supports many more configuration options.
Browsers use a default style sheet to style elements, so if you use b, i, ul and li, there is already some styling visible in the editor.
But for the colors, we use classes like .wysiwyg-color-fuchsia
, and for floats, we use .wysiwyg-float-right
or -left
.
See the CSS of the advanced demo (see the whitelist of allowed classes). You can add these classes with the stylesheets – configuration option (when you initialize wysihtml5, see above), i.e.
stylesheets: ["css/reset.css", "css/editor.css"]
The stylesheets are linked from within the head of the iframe’s content then.
Congrats you just integrated wysihtml into your website.
Now enjoy a cold beer.