Skip to content

Instantly share code, notes, and snippets.

@tanqhnguyen
Created July 17, 2012 11:28
Show Gist options
  • Save tanqhnguyen/3128892 to your computer and use it in GitHub Desktop.
Save tanqhnguyen/3128892 to your computer and use it in GitHub Desktop.
e.preventDefault();
var doc = e.delegateTarget;
var selection = window.getSelection();
if (!selection.rangeCount) return;
var range = selection.getRangeAt(0);
var content = $(range.cloneContents()).text();
if (!content.length) return;
var precursor = document.createRange();
precursor.setStart(doc, 0);
precursor.setEnd(range.startContainer, range.startOffset);
var preContent = $(precursor.cloneContents()).text();
selection.removeAllRanges();
var start = preContent.length;
var end = start + content.length;
var highlight = {
'id' : new Date().getTime(),
'start': start,
'end': end,
'content': content,
dateCreated: moment(new Date()).format(Highlight.dateFormat),
color: this.getHighlightColor(),
user_id: this.getCurrentUser().id
};
doc.innerHTML = Bitmarq.Highlighter.AddElemHighlight(doc, highlight, {
className: this.selectedClass,
style : {
"background-color": "#" + this.getHighlightColor()
},
data: {
cid: 'c12'
}
});
var selectedEl = this.$('.' + this.selectedClass);
var highlight = new Highlight(highlight);
// create the context action menu view
selectedEl.append(this.contextActionMenuTemplate());
this.createContextActionMenu(selectedEl, highlight);
// decorates the selected text
this.currentHighlight.trigger("change:color");
this.model.get('highlights').create(this.currentHighlight);
// add the current document to the history
Dispatch.trigger('addCurrentDocumentToHistory');
return false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment