Skip to content

Commit

Permalink
Merge pull request #6 from Rudiney/master
Browse files Browse the repository at this point in the history
Add a change event callback
  • Loading branch information
steveathon committed Nov 19, 2014
2 parents 627b717 + 7a20588 commit 3668296
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Add change event callback

1.0.2 - Merged some extras

* Added support for placeholder attribute as requested in issue #143 and #136
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Features
* Drag and drop files to insert images
* Supports image upload
* Supports image capture on mobile devices
* Events

Basic Usage
-----------
Expand Down Expand Up @@ -72,6 +73,17 @@ $('#editor').wysiwyg({
});
```

Events
------

#### Change
Fired whenever anything changes. See this example [events.html](examples/events.html)
```javascript
$('#editor').wysiwyg().on('change', function(){
alert('something has been changed on the editor');
});
```

Styling for mobile devices
--------------------------

Expand Down Expand Up @@ -103,7 +115,7 @@ History
------------

The original version of this code (below) appeared to be no longer maintained. There
were a number of outstanding changes which needed to be merged in and a few which
were a number of outstanding changes which needed to be merged in and a few which
included performance and feature improvements. These have now been included in this
master branch.

Expand Down
124 changes: 124 additions & 0 deletions examples/events.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Tiny, opensource, Bootstrap WYSIWYG rich text editor</title>

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<link href="../external/google-code-prettify/prettify.css" rel="stylesheet" />
<link rel="stylesheet"
href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
<link
href="http://netdna.bootstrapcdn.com/font-awesome/3.0.2/css/font-awesome.css"
rel="stylesheet" />
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="../external/jquery.hotkeys.js"></script>
<script
src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script src="../external/google-code-prettify/prettify.js"></script>

<link href="../css/style.css" rel="stylesheet" />
<script src="../src/bootstrap-wysiwyg.js"></script>
</head>

<body>
<div class="container">
<h1>Type something to see events</h1>

<div class="btn-toolbar" data-role="editor-toolbar"
data-target="#editor">
<div class="btn-group">
<a class="btn btn-default dropdown-toggle" data-toggle="dropdown"
title="Font Size"><i class="icon-text-height"></i>&nbsp;<b
class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a data-edit="fontSize 5"><font size="5">Huge</font>
</a>
</li>
<li><a data-edit="fontSize 3"><font size="3">Normal</font>
</a>
</li>
<li><a data-edit="fontSize 1"><font size="1">Small</font>
</a>
</li>
</ul>
</div>
<div class="btn-group">
<a class="btn btn-default" data-edit="bold"
title="Bold (Ctrl/Cmd+B)"><i class="icon-bold"></i>
</a> <a class="btn btn-default" data-edit="italic"
title="Italic (Ctrl/Cmd+I)"><i class="icon-italic"></i>
</a> <a class="btn btn-default" data-edit="strikethrough"
title="Strikethrough"><i class="icon-strikethrough"></i>
</a> <a class="btn btn-default" data-edit="underline"
title="Underline (Ctrl/Cmd+U)"><i class="icon-underline"></i>
</a>
</div>
<div class="btn-group">
<a class="btn btn-default" data-edit="insertunorderedlist"
title="Bullet list"><i class="icon-list-ul"></i>
</a> <a class="btn btn-default" data-edit="insertorderedlist"
title="Number list"><i class="icon-list-ol"></i>
</a> <a class="btn btn-default" data-edit="outdent"
title="Reduce indent (Shift+Tab)"><i class="icon-indent-left"></i>
</a> <a class="btn btn-default" data-edit="indent" title="Indent (Tab)"><i
class="icon-indent-right"></i>
</a>
</div>
<div class="btn-group">
<a class="btn btn-default" data-edit="justifyleft"
title="Align Left (Ctrl/Cmd+L)"><i class="icon-align-left"></i>
</a> <a class="btn btn-default" data-edit="justifycenter"
title="Center (Ctrl/Cmd+E)"><i class="icon-align-center"></i>
</a> <a class="btn btn-default" data-edit="justifyright"
title="Align Right (Ctrl/Cmd+R)"><i class="icon-align-right"></i>
</a> <a class="btn btn-default" data-edit="justifyfull"
title="Justify (Ctrl/Cmd+J)"><i class="icon-align-justify"></i>
</a>
</div>
<div class="btn-group">
<a class="btn btn-default dropdown-toggle" data-toggle="dropdown"
title="Hyperlink"><i class="icon-link"></i>
</a>
<div class="dropdown-menu input-append">
<input class="span2" placeholder="URL" type="text"
data-edit="createLink" />
<button class="btn" type="button">Add</button>
</div>
</div>

<div class="btn-group">
<a class="btn btn-default" data-edit="unlink"
title="Remove Hyperlink"><i class="icon-cut"></i>
</a> <a class="btn btn-default"
title="Insert picture (or just drag & drop)" id="pictureBtn">
<i class="icon-picture"></i> <input type="file"
data-role="magic-overlay" data-target="#pictureBtn"
data-edit="insertImage" /> </a>
</div>
<div class="btn-group">
<a class="btn btn-default" data-edit="undo"
title="Undo (Ctrl/Cmd+Z)"><i class="icon-undo"></i>
</a> <a class="btn btn-default" data-edit="redo"
title="Redo (Ctrl/Cmd+Y)"><i class="icon-repeat"></i>
</a>
</div>
</div>
<div id="editor" class="lead" placeholder="This is a basic example with no toolbars."></div>
<h2>Live Preview</h2>
<div id="editorPreview"></div>
<h2>Events fired:</h2>
<p id="events_log">
</p>
</div>
<script type='text/javascript'>
$('#editor').wysiwyg().on('change', function(){
$('#editorPreview').html($(this).cleanHtml());
$('#events_log').append(' change');
});
</script>
</body>
</html>
37 changes: 20 additions & 17 deletions src/bootstrap-wysiwyg.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* @fileoverview
/* @fileoverview
* Provides full bootstrap based, multi-instance WYSIWYG editor.
*
*
* "Name" = 'bootstrap-wysiwyg'
* "Author" = 'Various, see LICENCE'
* "Version" = '1.0.1'
* "About" = 'Tiny Bootstrap and JQuery Based WISWYG rich text editor.'
* "About" = 'Tiny Bootstrap and JQuery Based WISWYG rich text editor.'
*/
(function ($) {
'use strict';
/** underscoreThrottle()
* From underscore http://underscorejs.org/docs/underscore.html
/** underscoreThrottle()
* From underscore http://underscorejs.org/docs/underscore.html
*/
var underscoreThrottle = function(func, wait) {
var context, args, timeout, result;
Expand Down Expand Up @@ -51,8 +51,8 @@
$(this).html($(this).text());
$(this).attr('contenteditable',true);
$(this).data('wysiwyg-html-mode',false);
}
}

// Strip the images with src="data:image/.." out;
if ( o === true && $(this).parent().is("form") ) {
var gGal = $(this).html;
Expand Down Expand Up @@ -100,16 +100,17 @@
var commandArr = commandWithArgs.split(' '),
command = commandArr.shift(),
args = commandArr.join(' ') + (valueArg || '');

var parts = commandWithArgs.split('-');

if ( parts.length == 1 ) {
document.execCommand(command, 0, args);
}
}
else if ( parts[0] == 'format' && parts.length == 2) {
document.execCommand('formatBlock', false, parts[1] );
}

editor.trigger('change');
updateToolbar();
},
bindHotkeys = function (hotKeys) {
Expand All @@ -127,6 +128,8 @@
}
});
});

editor.keyup(function(){ editor.trigger('change'); });
},
getCurrentRange = function () {
var sel, range;
Expand Down Expand Up @@ -155,13 +158,13 @@
}
selection.addRange(selectedRange);
}
}
}
else if (document.selection && selectedRange) {
selectedRange.select()
}
},
// Adding Toggle HTML based on the work by @jd0000, but cleaned up a little to work in this context.

// Adding Toggle HTML based on the work by @jd0000, but cleaned up a little to work in this context.
toggleHtmlEdit = function(a) {
if ( $(editor).data("wysiwyg-html-mode") !== true ) {
var oContent = $(editor).html();
Expand Down Expand Up @@ -209,13 +212,13 @@
toolbar.find(toolbarBtnSelector, wrapper).click(function () {
restoreSelection();
editor.focus();

if ($(this).data(options.commandRole) === 'html') {
toggleHtmlEdit();
}
else {
execCommand($(this).data(options.commandRole));
}
}
saveSelection();
});
toolbar.find('[data-toggle=dropdown]').click(restoreSelection);
Expand Down Expand Up @@ -264,7 +267,7 @@
options = $.extend(true, {}, $.fn.wysiwyg.defaults, userOptions);
toolbarBtnSelector = 'a[data-' + options.commandRole + '],button[data-' + options.commandRole + '],input[type=button][data-' + options.commandRole + ']';
bindHotkeys(options.hotKeys);

// Support placeholder attribute on the DIV
if ($(this).attr('placeholder') != '') {
$(this).addClass('placeholderText');
Expand All @@ -282,7 +285,7 @@
}
})
}

if (options.dragAndDropImages) {
initFileDrops();
}
Expand Down

0 comments on commit 3668296

Please sign in to comment.