Skip to content

Instantly share code, notes, and snippets.

@Koshub
Created February 21, 2015 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Koshub/a39655478f7e8ffc8df1 to your computer and use it in GitHub Desktop.
Save Koshub/a39655478f7e8ffc8df1 to your computer and use it in GitHub Desktop.
Quoting selection for pyha.ru
$('li.post > div > div > p > span > a.quote').сlick(function() {
function getSelection() {
var text = "", element = null;
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var node = sel.getRangeAt(0).commonAncestorContainer;
element = node.nodeType == 1 ? node : node.parentNode;
text = sel.toString();
}
} else if (typeof document.selection != "undefined" &&
document.selection.type != "Control") {
var textRange = document.selection.createRange();
element = textRange.parentElement();
text = textRange.text;
}
return {
isValid: (element != null && text != ""),
selectedText: text,
containerElement: element
};
}
var post_msgID = $(this).parent().parent().parent().parent().parent().find('a:first').attr('name');
var post_date = $(this).parent().siblings('a').html();
var user_login = $(this).parent().parent().parent().parent().find('div:first > p > a').html();
var formatter = $('#switcher option:selected').val() || 'bbcode';
var editorIsUsingBBCode = ($('#switcher').length && formatter == 'bbcode');
var selection = getSelection();
if (selection.isValid) {
if ($(this).parents('li.post').has(selection.containerElement).length) {
$('#id_message').focus();
var selectedText = selection.selectedText;
var text = $('#id_message').val().length > 0 ? "\n" : "";
if (editorIsUsingBBCode) {
text += '[quote]' + selectedText + '[/quote]\n[b]@' + user_login + '[/b], ';
} else {
text += "> " + selectedText.replace(/\n/g, '\n> ') + '\n\n**@' + user_login + '**, ';
}
$.markItUp( {
replaceWith: text
});
$.scrollTo('#id_message');
} else {
alert('Выделять там, нажимать тут - не порядок!');
}
} else {
var text = $.get('/forum/quote/' + post_msgID + '/' + formatter + '/', function(response) {
$('#id_message').focus();
var text = response['text'];
if (editorIsUsingBBCode) {
text += '\n[b]@' + user_login + '[/b], '
} else {
text += '\n\n**@' + user_login + '**, '
}
$.markItUp({
replaceWith: text
});
$.scrollTo('#id_message');
}, 'json');
}
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment