Skip to content

Instantly share code, notes, and snippets.

@yoko
Created February 26, 2010 11:07
Show Gist options
  • Save yoko/315638 to your computer and use it in GitHub Desktop.
Save yoko/315638 to your computer and use it in GitHub Desktop.
var emoji = {
binary_store: {},
convert: function(str, raw) {
var ret = [], img;
for (var i = 0, c; c = str.charCodeAt(i); i++) {
if (
0xE63E <= c && c <= 0xE6A5 ||
0xE6AC <= c && c <= 0xE6AE ||
0xE6B1 <= c && c <= 0xE6B3 ||
0xE6B7 <= c && c <= 0xE6BA ||
0xE6CE <= c && c <= 0xE70B ||
0xE70C <= c && c <= 0xE757
) {
emoji.binary_store[c] = str.charAt(i);
img = '<img src="/images/emoji/'+c+'.gif" alt="" width="12" height="12" class="emoji-'+c+'"/>';
if ($.browser.msie && !raw)
img = ['<wbr/>', img, '<wbr/>'].join(''); // [IE] wbr
ret.push(img);
}
else
ret.push(str.charAt(i));
}
return ret.join('');
}
};
var EmojiTextarea = function() {};
EmojiTextarea.prototype = {
container: null,
appendTo: function(selector, str, attrs) {
this.container = $('<div class="emoji-textarea textbox"/>')
.attr($.extend(attrs, {
contentEditable: true
}, attrs))
.appendTo(selector)
.html(emoji.convert(str, true));
return this.container;
},
val: function() {
if (!this.container) return '';
var str = this.container.clone();
$('img', str).each(function() {
var img = $(this);
if (/emoji-(\d+)/.test(img.attr('class')))
img.replaceWith(emoji.binary_store[RegExp.$1]);
});
return str.text();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment