Get started
Installation
npm install trumbowyg
bower install trumbowyg
If you don't already do it, load jQuery at bottom of <body>
like so:
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-3.2.1.min.js"><\/script>')</script>
Trumbowyg requires jQuery >= 1.8
After these lines, you have to load Trumbowyg.
<script src="node_modules/trumbowyg/dist/trumbowyg.min.js"></script>
Don't forget to load Trumbowyg CSS in the <head>
, or load your own style for the editor.
<link rel="stylesheet" href="node_modules/trumbowyg/dist/ui/trumbowyg.min.css">
Basics
This the minimal code to transform a simple div into the amazing WYSIWYG editor which is Trumbowyg.
$('#trumbowyg-demo').trumbowyg();
If you want to set options to Trumbowyg, add an object which contains your options as parameters.
$('#trumbowyg-demo').trumbowyg({
btns: ['strong', 'em', '|', 'insertImage'],
autogrow: true
});
Common issues
SVG icons does not load
- check if you are in HTTP(S) protocol;
- check if you did not have strong problem;
If your problem is not solved by these tips, check the SVG icons section.
General
Prefix
You can change prefix of all class added on elements of Trumbowyg using this option:
$('textarea').trumbowyg({
prefix: 'custom-prefix'
});
For example, the bold button class is now custom-prefix-bold-button
Default value is trumbowyg
Localization
Your users don't speak english? No problem, Trumbowyg has a language parameter. You have to load the
appropriate lang file. Search in /dist/langs
folder to see if a language file already
exists, if not create it and share it :). See add a localization for
more details on language file.
Don't forget include the lang file in your pages:
<script type="text/javascript" src="js/dist/langs/fr.min.js"></script>
Warning, include lang file after Trumbowyg and before instantiating the editor!
Usage of language parameter:
$('textarea').trumbowyg({
lang: 'fr'
});
If the lang was not found, english values are used by default.
Custom skin
Trumbowyg is flexible and simple. You want a different look?
Replace existing CSS file
Copy /src/ui/
folder somewhere and customize style. Finally, link your new CSS file and
remove link to Trumbowyg default style.
It is not recommended to directly edit the CSS file in the trumbowyg folder. When you update the plugin, your modifications will be erased.
Multiple skins for multiple Trumbowyg
This is useful when you do not want same design for all instances of Trumbowyg.
Use the prefix option:
$('.editor-modern-ui').trumbowyg({
prefix: 'modern-ui'
});
In your CSS, you can now stylize .modern-ui-link-button
for example, whitout conflicts with
the default skin. It's necessary to redefine style for all elements. If you want to start from the
default skin, copy /src/ui/
folder, replace all trumbowyg-
by your prefix and
work from here.
SVG icons
Added in 2.1.0
A pack of SVG icons is available and enabled by default. This file is loaded via XHR request in JavaScript so it is possible the path is not matching with your assets file paths. You can change the path of the SVG or disable this feature.
Change SVG path globally
To change SVG path, you need to set the path in global Trumbowyg configuration object:
$.trumbowyg.svgPath = '/assets/my-custom-path/icons.svg';
This global way needs to be done before initialize any Trumbowyg instance.
Disable SVG icons globally
If you do not want SVG icons, you can set this option to false. Then, you can add your custom icons by CSS or what you want.
$.trumbowyg.svgPath = false:
This global way needs to be done before initialize any Trumbowyg instance.
Change SVG path or disable SVG icons locally
You can also apply svgPath
option for an isolated editor only.
$('.editor').trumbowyg({
svgPath: false // or a path like '/assets/my-custom-path/icons.svg'
});
Use SVG icons without XHR or via an another protocol than HTTP(S)
If you want to be IE9-complient you should use this option.
If you do not want use HTTP(S) protocol or use XHR to load icons, you can load icons in your HTML
by your own. You just need to include the
icons.svg file
inline in your <body>
:
<div id="trumbowyg-icons">
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="trumbowyg-blockquote" viewBox="0 0 72 72"><path d="..."></path></symbol>
<symbol id="trumbowyg-bold" viewBox="0 0 72 72"><path d="..."></path></symbol>
<symbol id="trumbowyg-close" viewBox="0 0 72 72"><path d="..."></path></symbol>
<!-- ... all other icons here -->
</svg>
</div>
You need to use this id: <prefix>-icons
.
Trumbowyg check if a #<prefix>-icons
exists. If not, it loads SVG icons file via
XHR and put it in a div with this id. Next editor on the page did not load icons again. By adding
this div in the page, you "hack" this mecanism to load it by your own.
Placeholder
Like the HTML5 attribute on input and textarea, you can add a placeholder with Trumbowyg.
<div class="my-editor" placeholder="Your text as placeholder"></div>
Placeholder is visible only if the element is empty (no HTML/text).
Placeholder is both managed in JS and CSS with the default style. If you create your own style, you
need to apply the following style (replace trumbowyg
by your prefix):
.trumbowyg-editor[contenteditable=true]:empty::before{
content: attr(placeholder);
color: #999;
}
Disabled
Added in 2.1.0
Trumbowyg supports disabled
attribute on a textarea and an option.
<textarea class="my-editor" disabled></textarea>
$('.a-disabled-editor').trumbowyg({
disabled: true
});
You can switch between disabled and enabled states by using API
Basic Options
Button pane
It's probably the most interesting option, it allows you to choose the buttons that appears in the
button pane. This option is an array containing string values representing the buttons or vertical
separators (using the pipe character). To create your own custom button pane, define an array and pass
it to the btns
option.
$('.simple-editor').trumbowyg({
btns: [['bold', 'italic'], ['link']]
});
By default, btns
option value is:
$('.simple-editor').trumbowyg({
btns: [
['viewHTML'],
['formatting'],
'btnGrp-semantic',
['superscript', 'subscript'],
['link'],
['insertImage'],
'btnGrp-justify',
'btnGrp-lists',
['horizontalRule'],
['removeformat'],
['fullscreen']
]
});
/** WTF are btnGrp-* buttons? There are just arrays of strings :)
* jQuery.trumbowyg.btnsGrps = {
* design: ['bold', 'italic', 'underline', 'strikethrough'],
* semantic: ['strong', 'em'],
* justify: ['justifyLeft', 'justifyCenter', 'justifyRight', 'justifyFull'],
* lists: ['unorderedList', 'orderedList']
* };
*/
Semantic
Generates a better, more semantic oriented HTML (i.e. <em>
instead of
<i>
, <strong>
intsead of <b>
, etc.). It's just a
boolean:
$('.trumbowyg').trumbowyg({
semantic: false
});
This option deactivates the underline button by default because they do not convey any real semantic. If you want to reactivate them, you have to do it explicitly, see Button pane
This option is set to true
by default
Reset CSS
If you don't want the page style to impact on the look of the text in the editor, you will need to apply
a reset-css on the editor. You can activate this with the resetCss
option:
$('.trumbowyg').trumbowyg({
resetCss: true
});
This option is set to false
by default. The reset only applies to the editor, not to the
generated HTML code.
Remove format pasted
If you don't want styles pasted from clipboard (from Word or other webpage for example), pass the removeformatPasted
option to true
$('.trumbowyg').trumbowyg({
removeformatPasted: true
});
Remove format pasted is not active by default (set to false
).
Auto grow
The text editing zone can extend itself when writing a long text. To activate this feature, use the autogrow option:
$('.trumbowyg').trumbowyg({
autogrow: true
});
Autogrow is not active by default (set to false
).
Auto grow on enter
Added in 2.7.0
The text editing zone can extend itself when editor get focus and reduce on blur. To activate this feature, use the following option:
$('.trumbowyg').trumbowyg({
autogrowOnEnter: true
});
Autogrow on enter is not active by default (set to false
).
Advanced Options
Add a localization
The structure of a language file is simple, it is a JavaScript Object which associate a translation to any key. For example:
jQuery.trumbowyg.langs.fr = {
_dir: "ltr", // This line is optionnal, but usefull to override the `dir` option
bold: "Gras",
close: "Fermer"
};
Some localizations are added each day, you can find them here
You can submit a new localization file by creating a new pull request on the Github repository.
English is the default localization, you don't need to include any file to get Trumbowyg in English.
API
Modal box
When you want create your custom extension for Trumbowyg, you can open and close a modal box with custom inner HTML code, listen events and more.
Open and close
For that use the .trumbowyg()
method and give parameters "openModal"
or "closeModal"
like that:
// Open a modal box
var $modal = $("#editor").trumbowyg("openModal", {
title: "A title for modal box",
content: "<p>Content in HTML which you want include in created modal box</p>"
});
// Close current modal box
$("#editor").trumbowyg("closeModal");
An openModal
call returns a jQuery object which contains the modal box. You need this
object if you want to use listen events (see below).
Only one modal box can open at any given moment. So, openModal
return false if a modal is
curently opened.
Events on modal box
Modal boxes in Trumbowy come with two buttons: "Confirm" and "Cancel". An event is associated to each button:
tbwsubmit
: Trigged when form is submittbwreset
: Trigged when user cancel operation
// Open a modal box
var $modal = $("#editor").trumbowyg("openModal", {
title: "A title for modal box",
content: "<p>Content in HTML which you want include in created modal box</p>"
});
// Listen clicks on modal box buttons
$modal.on('trumbowyg-confirm', function(e){
// Save datas
$("#editor").trumbowyg("closeModal");
});
$modal.on('trumbowyg-cancel', function(e){
$("#editor").trumbowyg("closeModal");
});
Only build inputs in modal
If you want only add inputs in the modal box, this function was more simple. Indeed, you do not manage confirm and close buttons, and get all input value on confirm.
var img = $('img#an-img');
$("#editor").trumbowyg("openModalInsert", {
title: "A title for modal box",
fields: {
url: {
value: img.attr('src')
},
alt: {
label: 'Alt',
name: 'alt',
value: img.attr('alt')
},
example: {
// Missing label is replaced by the key of this object (here 'example')
// Missing name is the same
// When value is missing, value = ''
}
},
callback: function(values){
img.attr('src', values['url']);
img.attr('alt', values['alt']);
return true; // Return true if you have finished with this modal box
// If you do not return anything, you need manage yourself the closure of the modal box
}
});
Range
Managing correctly text range, is not so trivial. Trumbowyg has a system to save and restore selection range which does not involves typical getter/setter.
Save and get current range
// Save current range
$('#editor').trumbowyg('saveRange');
// Restore last saved range
$('#editor').trumbowyg('restoreRange');
Get selection range
// range contains a JavaScript range
var range = $('#editor').trumbowyg('getRange');
Get last saved range text
var text = $('#editor').trumbowyg('getRangeText');
Manage content
You can set and get current HTML content of the editor with a getter/setter:
// Get HTML content
$('#editor').trumbowyg('html');
// Set HTML content
$('#editor').trumbowyg('html', "<p>Your content here</p>");
Empty
You can empty the content of the editor.
$('#editor').trumbowyg('empty');
Enable/disable edition
Added in 2.1.0
As you can disable editor by using disabled option, you can also switch between enabled and disabled states by using API.
$('#editor').trumbowyg('disable');
$('#editor').trumbowyg('enable');
Destroy editor
When you wish, you can restore the previous state of the element was used to create the editor.
$('#editor').trumbowyg('destroy');
Events
Some events are fired on the jQuery element which is used to build the editor.
- Focus on editor:
tbwfocus
- Blur on editor:
tbwblur
- Editor is initialized:
tbwinit
2.0.0 - Change in editor:
tbwchange
2.0.0 - Resize the editor on autogrow:
tbwresize
2.0.0 - Paste something in the editor:
tbwpaste
2.0.0 - Switch to fullscreen mode:
tbwopenfullscreen
2.0.0 - Leave editor's fullscreen mode:
tbwclosefullscreen
2.0.0 - Close the editor:
tbwclose
2.0.0
$('#editor')
.trumbowyg() // Build Trumbowyg on the #editor element
.on('tbwfocus', function(){ console.log('Focus!'); }); // Listen for `tbwfocus` event
.on('tbwblur', function(){ console.log('Blur!'); }); // Listen for `tbwblur` event