Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.