Getting Started (original) (raw)
1. Include scripts
- The first script contains the html5 parser rules that are needed for wysihtml5 in order to create valid and desired markup. Either use one of the "existing parser rules sets":https://github.com/xing/wysihtml5/tree/master/parser_rules or create your own. Check the "advanced.js parser rules":https://github.com/xing/wysihtml5/blob/master/parser_rules/advanced.js for details.
- The second script is the minified wysihtml5 library. It's located in the "dist":https://github.com/xing/wysihtml5/tree/master/dist folder of this repository.
2. Create a textarea
wysihtml5 takes a textarea 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 wysihtml5 integrates smoothly with forms.
3. Create a toolbar
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":https://github.com/xing/wysihtml5/blob/master/examples/advanced.html or find a full list of "all supported commands here":https://github.com/xing/wysihtml5/wiki/Supported-Commands.
4. Initialize wysihtml5
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.
wysihtml5 supports many more "configuration options":https://github.com/xing/wysihtml5/wiki/Configuration.
5. Use a set of CSS classes to style the editor's content
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":https://github.com/xing/wysihtml5/blob/master/examples/css/stylesheet.css (see the "whitelist of allowed classes":https://github.com/xing/wysihtml5/blob/master/parser_rules/advanced.js). You can add these classes with the "stylesheets - configuration option":https://github.com/xing/wysihtml5/wiki/Configuration (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.
6. There you go
Congrats you just integrated wysihtml5 into your website. Now enjoy a cold beer.