<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="http://www.examplet.buss.hk/js/jquery.caret.min.js"></script>
<style type="text/css">
body { font-size:13px; font-family:Lucida Sans, Lucida Sans Unicode, Arial, Sans-Serif; color:#666; margin:20px;}
#menu {padding:5px; background-color:#f5f5f5; display:none;
position:absolute; top:0px; left:0px; overflow:hidden;
border:solid 1px #929292;}
#menu:hover {background-color:rgba(245, 245, 245, 1);}
#menu a { padding:3px 5px; border:solid 1px #f5f5f5; color:#000; text-decoration:none; font-size:14px;
float:left; display:block; text-align:center; zoom:1;}
#menu a:hover { border:solid 1px #bea881; background-color:#fff2cb;}
#preview { width:400px; background-color:#f8f8f8; border:solid 1px #929292; padding:10px;}
#preview b, #preview u, #preview i {color:#c30;}
</style>
</head>
<body>
<div id="main">
<div>
<p style="font-size:16px;">
Select some text here:<br />
<textarea id="description" rows="8" cols="50">The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.</textarea>
<textarea id="description2" rows="5" cols="40">lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum.</textarea>
</p>
Preview:
<div id="preview">The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.</div>
<div id="menu">
<a href="#" id="bold">b</a>
<a href="#" id="italic">i</a>
<a href="#" id="underline">u</a>
<a href="#" id="link">Link</a>
</div>
</div>
</div>
</body>
</html>
(function($){
$.fn.minibar = function(options) {
var defaults = {
},
settings = $.extend({}, defaults, options);
this.each(function() {
var $this = $(this);
var mouseX = 0,
mouseY = 0,
selectionLength,
activeElement;
$this.mousemove(function(e) {
// track mouse position
mouseX = e.pageX;
mouseY = e.pageY;
});
$(document).mousedown(function() {
$("#menu").fadeOut();
});
if($.browser.msie) {
$this.mouseup(function() {
// get the mouse position an show the menu
if(document.selection && document.selection.createRange().text.length) { // ie6 hack to check if anything is selected
$("#menu").css("top", mouseY - 30).css("left", mouseX + 10).stop(true, true).fadeIn();
selectionLength = document.selection.createRange().text.length;
}
});
} else {
$this.select(function() {
console.log(document.activeElement);
$("#menu").css("top", mouseY - 30).css("left", mouseX + 10).fadeIn("1000");
$('textarea').removeClass('active');
$(document.activeElement).addClass('active');
});
}
$("#bold").click(function() {
wrapText("<b>", "</b>");
$("#menu").fadeOut();
emptySelection();
});
$("#italic").click(function() {
wrapText("<i>", "</i>");
$("#menu").fadeOut();
emptySelection();
});
$("#underline").click(function() {
wrapText("<u>", "</u>");
$("#menu").fadeOut();
emptySelection();
});
$("#link").click(function() {
var url = prompt("Enter URL", "http://");
if (url != null)
wrapText("<a href='" + url + "'>", "</a>");
$("#menu").fadeOut();
emptySelection();
});
var emptySelection = function() {
if (document.selection) {
document.selection.empty();
} else if (window.getSelection) {
window.getSelection().removeAllRanges();
}
}
var wrapText = function(startText, endText){
//alert($this.caret().text.length);
//if(!$this.caret().text.length) {
//return;
//}
// Get the text before the selection
$this = $('textarea.active');
var before = $this.val().substring(0, $this.caret().start);
// Get the text after the selection
var after = $this.val().substring($this.caret().end, $this.val().length);
// merge text before the selection, a selection wrapped with inserted text and a text after the selection
$this.val(before + startText + $this.caret().text + endText + after);
//alert($this[0].innerHTML);
// Update the preview
$("#preview").html($this.val());
}
});
// returns the jQuery object to allow for chainability.
return this;
}
})(jQuery);
$(document).ready(function() {
$('textarea').minibar();
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |