Bootstrap3-wysihtml5 is a javascript plugin that makes it easy to create simple, beautiful wysiwyg editors with the help of wysihtml5x and Twitter Bootstrap 3. It based on the Project https://github.com/schnawel007/bootstrap3-wysihtml5/
http://arvigeus.github.io/bootstrap3-wysihtml5
Download the latest version of bootstrap3-wysihtml5.
<link rel="stylesheet" type="text/css" href="lib/css/bootstrap.min.css" /> <!-- Please use the newest Version of Bootstrap 3.0.X -->
<link rel="stylesheet" type="text/css" href="src/bootstrap3-wysihtml5.css" />
<link rel="stylesheet" type="text/css" href="src/bootstrap3-wysihtml5-editor.css" />
<script src="lib/js/jquery-1.11.1.min.js"></script>
<script src="lib/js/bootstrap.min.js"></script>
<script src="lib/js/wysihtml5x-toolbar.min.js"></script>
<script src="src/bootstrap3-wysihtml5.min.js"></script>
<textarea id="some-textarea" placeholder="Enter text ..."></textarea>
<script type="text/javascript">
$('.textarea').wysihtml5({stylesheets: ["path/to/bootstrap3-wysihtml5-editor.css"]});
//or
$('.div').wysihtml5();
</script>
Stylesheets are needed to be included both as editor's parameter and page stylesheet
You can get the html generated by getting the value of the text area, e.g.
$('.textarea').val();
An options object can be passed in to .wysihtml5() to configure the editor:
$('.textarea').wysihtml5({someOption: 23, ...})
To override which buttons to show, set the appropriate flags:
$('.textarea').wysihtml5({
"font-styles": true, //Font styling, e.g. h1, h2, etc
"color": true, //Button to change color of font
"emphasis": true, //Italics, bold, etc
"textAlign": true, //Text align (left, right, center, justify)
"lists": true, //(Un)ordered lists, e.g. Bullets, Numbers
"blockquote": true, //Button to insert quote
"link": true, //Button to insert a link
"table": true, //Button to insert a table
"image": true, //Button to insert an image
"video": true, //Button to insert YouTube video
"html": false //Button which allows you to edit the generated HTML
});
To define custom templates for buttons, you can submit a customTemplates hash with the new definitions. Each entry should be a function which expects 'locale' and optional 'options' to manage the translations.
For example, the default template used for the editHtml mode button looks like this (with size fetched from the optional 'options')
<li>
<div class='btn-group'>
<button class='btn btn-" + size + "' data-wysihtml5-action='change_view' title='" + locale.html.edit + "'><i class='icon-pencil'></i></a>"
</div>
</li>
You can change it to not use the pencil icon (for example) by defining the custom template like this:
var myCustomTemplates = {
html : function(locale) {
return "<li>" +
"<div class='btn-group'>" +
"<a class='btn btn-default' data-wysihtml5-action='change_view' title='" + locale.html.edit + "'>HTML</a>" +
"</div>" +
"</li>";
}
}
// pass in your custom templates on init
$('.textarea').wysihtml5({
customTemplates: myCustomTemplates
});
This will override only the toolbar template for html, and all others will use the default.
It is possible to supply a number of stylesheets for inclusion in the editor <iframe>
:
$('.textarea').wysihtml5({
"stylesheets": ["path/to/bootstrap3-wysihtml5-editor.css", "/path/to/editor.css"]
});
Wysihtml5 exposes a "number of events":https://github.com/xing/wysihtml5/wiki/Events. You can hook into these events when initialising the editor:
$('.textarea').wysihtml5({
"events": {
"load": function() {
console.log("Loaded!");
},
"blur": function() {
console.log("Blured");
}
}
});
Options you pass in will be added to the defaults via a shallow copy. (see "jQuery.extend()":http://api.jquery.com/jQuery.extend/ for details). You can use a deep copy instead (which is probably what you want if you're adding tags or classes to parserRules) via 'deepExtend', as in the parserRules example below.
If you find the editor is stripping out tags or attributes you need, then you'll want to extend (or replace) parserRules. This example extends the defaults to allow the <strong>
and <em>
tags, and the class "middle":
$('.textarea').wysihtml5('deepExtend', {
parserRules: {
classes: {
"middle": 1
},
tags: {
strong: {},
em: {}
}
}
});
There's quite a bit that can be done with parserRules; see "wysihtml5's advanced parser ruleset":https://github.com/xing/wysihtml5/blob/master/parser_rules/advanced.js for details. bootstrap-wysihtml5's default parserRules can be found "in the source":https://github.com/jhollingworth/bootstrap-wysihtml5/blob/master/src/bootstrap-wysihtml5.js (just search for 'parserRules' in the file).
To disable any rules altogether, use:
$('.textarea').wysihtml5('deepExtend', {
useParserRules: false
});
You can change bootstrap-wysihtml5's defaults by altering:
$.fn.wysihtml5.defaultOptions
This object has the same structure as the options object you pass in to .wysihtml5(). You can revert to the original defaults by calling:
$(this).wysihtml5('resetDefaults')
Operations on the defaults are not thread-safe; if you're going to change the defaults, you probably want to do it only once, after you load the bootstrap-wysihtml plugin and before you start instantiating your editors.
You can access the "wysihtml5 editor object":https://github.com/xing/wysihtml5 like this:
var wysihtml5Editor = $('.textarea').data("wysihtml5").editor;
wysihtml5Editor.composer.commands.exec("bold");
You can use Bootstrap-wysihtml5 in other languages. There are some translations available in the src/locales directory. You can include your desired one after the plugin and pass its key to the editor. Example:
<script src="src/locales/bootstrap-wysihtml5.pt-BR.js"></script>
<script type="text/javascript">
$('.textarea').wysihtml5({locale: "bg-BG"});
</script>
It is possible to use custom translations as well. Just add a new key to $.fn.wysihtml5.locale before calling the editor constructor.