Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contenteditable support #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# jQuery Emoji Picker #

## Note ##
This is fork of wedgies/jquery-emoji-picker with support of content-editable.

Use:

```javascript
$('.question').emojiPicker({
height: '300px',
width: '450px',
contenteditable: true
});
```

Also you always can get pure text with unicode emoji and save it to your DB for example:
```javascript
alert($('.question').data(text));
```

When content-editable is enabled, all unicode emoji in editor wil be automaticaly changed to images on plugin init

## Description ##

The top 3 things your users love:
Expand Down Expand Up @@ -31,7 +51,6 @@ Add the following to your `<head></head>` tag:
```html
<link rel="stylesheet" type="text/css" href="css/jquery.emojipicker.css">
<script type="text/javascript" src="js/jquery.emojipicker.js"></script>

<!-- Emoji Data -->
<link rel="stylesheet" type="text/css" href="css/jquery.emojipicker.a.css">
<script type="text/javascript" src="js/jquery.emojipicker.a.js"></script>
Expand Down
10 changes: 10 additions & 0 deletions css/jquery.emojipicker.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 73 additions & 2 deletions js/jquery.emojipicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
iconColor: 'black',
iconBackgroundColor: '#eee',
container: 'body',
button: true
button: true,
contenteditable: false
};

var MIN_WIDTH = 200,
Expand All @@ -30,6 +31,7 @@
// (type) Safety first
this.settings.width = parseInt(this.settings.width);
this.settings.height = parseInt(this.settings.height);
this.settings.contenteditable = this.settings.contenteditable.constructor === Boolean ? this.settings.contenteditable : false;

// Check for valid width/height
if(this.settings.width >= MAX_WIDTH) {
Expand Down Expand Up @@ -65,6 +67,49 @@
this.addPickerIcon();
this.createPicker();
this.listen();
this.ce();
},

ce: function () {
if (this.settings.contenteditable) {
var editor = this.$el;
var that = this;
var t = editor.text();
if(t.length > 0){
for (var i = 0; i < $.fn.emojiPicker.emojis.length; ++i) {
var emojiHtml = "<img src='data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAQAIBRAA7' class='emojiCe emoji-" + $.fn.emojiPicker.emojis[i].shortcode + "' data-unicode='" + toUnicode($.fn.emojiPicker.emojis[i].unicode) + "'/>";
t = t.replace(new RegExp(toUnicode($.fn.emojiPicker.emojis[i].unicode), 'g'), emojiHtml);
}
}
editor.html(t);
editor.on('input click', function (e) {
var element = this;
var doc = element.ownerDocument || element.document;
var win = doc.defaultView || doc.parentWindow;
var sel;
sel = win.getSelection();
if (sel.rangeCount > 0) {
var range = sel.getRangeAt(0);
sel.removeAllRanges();
sel.addRange(range);
that.editorRange = range;
}
that.getCeText();
});
editor.on('paste', function (e) {
e.preventDefault();
var text = (e.originalEvent || e).clipboardData.getData('text/plain') || prompt('Paste something..');
window.document.execCommand('insertText', false, text);
});
}
},

getCeText: function() {
var editor = this.$el.clone();
$('img', editor).each(function(){
$(this).replaceWith($(this).data('unicode'));
});
this.$el.data('text', editor.text());
},

addPickerIcon: function() {
Expand Down Expand Up @@ -206,7 +251,18 @@
var emojiShortcode = $(e.target).attr('class').split('emoji-')[1];
var emojiUnicode = toUnicode(findEmoji(emojiShortcode).unicode);

insertAtCaret(this.element, emojiUnicode);
if (this.settings.contenteditable) {
if(typeof this.editorRange === 'undefined'){
this.$el.focus();
this.$el.trigger('input');
}
var emojiHtml = "<img src='data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAQAIBRAA7' class='emojiCe " + $(e.target).attr('class') + "' data-unicode='" + emojiUnicode + "'/>";
this.editorRange = insertAtCeCaret(emojiHtml, this.editorRange);
this.getCeText();
}
else {
insertAtCaret(this.element, emojiUnicode);
}
},

emojiCategoryClicked: function(e) {
Expand Down Expand Up @@ -349,6 +405,21 @@
}
}

function insertAtCeCaret(myValue, range) {
var el = document.createElement("div");
el.innerHTML = myValue;
var frag = document.createDocumentFragment(), node, lastNode;
while ((node = el.firstChild)) {
lastNode = frag.appendChild(node);
}
range.insertNode(frag);
if (lastNode) {
range.setStartAfter(lastNode);
range.collapse(false);
}
return range;
}

function toUnicode(code) {
var codes = code.split('-').map(function(value, index) {
return parseInt(value, 16);
Expand Down