Make canned response insertion and extraction work with both plaintext and HTML editors

pull/134/head
Thomas Bruederli 11 years ago
parent 0b1de8a487
commit 2d6242ffb2

@ -3307,19 +3307,27 @@ function rcube_webmail()
if (!insert) if (!insert)
return false; return false;
// get cursor pos // insert into tinyMCE editor
var textarea = rcube_find_object(this.env.composebody), if ($("input[name='_is_html']").val() == '1') {
selection = $(textarea).is(':focus') ? this.get_input_selection(textarea) : { start:0, end:0 }, var editor = tinyMCE.get(this.env.composebody);
inp_value = textarea.value; editor.getWin().focus(); // correct focus in IE & Chrome
pre = inp_value.substring(0, selection.start), editor.selection.setContent(insert, { format:'text' });
end = inp_value.substring(selection.end, inp_value.length); }
// replace selection in compose textarea
else {
var textarea = rcube_find_object(this.env.composebody),
selection = $(textarea).is(':focus') ? this.get_input_selection(textarea) : { start:0, end:0 },
inp_value = textarea.value;
pre = inp_value.substring(0, selection.start),
end = inp_value.substring(selection.end, inp_value.length);
// insert response text // insert response text
textarea.value = pre + insert + end; textarea.value = pre + insert + end;
// set caret after inserted text // set caret after inserted text
this.set_caret_pos(textarea, selection.start + insert.length); this.set_caret_pos(textarea, selection.start + insert.length);
textarea.focus(); textarea.focus();
}
}; };
/** /**
@ -3327,20 +3335,37 @@ function rcube_webmail()
*/ */
this.save_response = function() this.save_response = function()
{ {
var textarea = rcube_find_object(this.env.composebody), var sigstart, text = '', strip = false;
text = '', sigstart;
if (textarea && $(textarea).is(':focus')) { // get selected text from tinyMCE editor
text = this.get_input_selection(textarea).text; if ($("input[name='_is_html']").val() == '1') {
var editor = tinyMCE.get(this.env.composebody);
editor.getWin().focus(); // correct focus in IE & Chrome
text = editor.selection.getContent({ format:'text' });
if (!text) {
text = editor.getContent({ format:'text' });
strip = true;
}
} }
// get selected text from compose textarea
else {
var textarea = rcube_find_object(this.env.composebody), sigstart;
if (textarea && $(textarea).is(':focus')) {
text = this.get_input_selection(textarea).text;
}
if (!text && textarea) { if (!text && textarea) {
text = textarea.value; text = textarea.value;
strip = true;
}
}
// strip off signature // strip off signature
if (strip) {
sigstart = text.indexOf('-- \n'); sigstart = text.indexOf('-- \n');
if (sigstart > 0) { if (sigstart > 0) {
text = textarea.value.substring(0, sigstart); text = text.substring(0, sigstart);
} }
} }
@ -3390,7 +3415,7 @@ function rcube_webmail()
$('<a>').addClass('insertresponse active') $('<a>').addClass('insertresponse active')
.attr('href', '#') .attr('href', '#')
.attr('rel', key) .attr('rel', key)
.html(response.name) .html(this.quote_html(response.name))
.appendTo(li) .appendTo(li)
.mousedown(function(e){ .mousedown(function(e){
return rcube_event.cancel(e); return rcube_event.cancel(e);
@ -6878,6 +6903,14 @@ function rcube_webmail()
/********* helper methods *********/ /********* helper methods *********/
/********************************************************/ /********************************************************/
/**
* Quote html entities
*/
this.quote_html = function(str)
{
return String(str).replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
};
// get window.opener.rcmail if available // get window.opener.rcmail if available
this.opener = function() this.opener = function()
{ {
@ -6951,40 +6984,43 @@ function rcube_webmail()
textInputRange, len, endRange; textInputRange, len, endRange;
if (typeof obj.selectionStart == "number" && typeof obj.selectionEnd == "number") { if (typeof obj.selectionStart == "number" && typeof obj.selectionEnd == "number") {
normalizedValue = obj.value; normalizedValue = obj.value;
start = obj.selectionStart; start = obj.selectionStart;
end = obj.selectionEnd; end = obj.selectionEnd;
} else { }
range = document.selection.createRange(); else {
range = document.selection.createRange();
if (range && range.parentElement() == obj) {
len = obj.value.length; if (range && range.parentElement() == obj) {
normalizedValue = obj.value.replace(/\r\n/g, "\n"); len = obj.value.length;
normalizedValue = obj.value.replace(/\r\n/g, "\n");
// create a working TextRange that lives only in the input
textInputRange = obj.createTextRange(); // create a working TextRange that lives only in the input
textInputRange.moveToBookmark(range.getBookmark()); textInputRange = obj.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want // Check if the start and end of the selection are at the very end
// in those cases // of the input, since moveStart/moveEnd doesn't return what we want
endRange = obj.createTextRange(); // in those cases
endRange.collapse(false); endRange = obj.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len; if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
} else { start = end = len;
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
} }
else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
}
else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
}
} }
return { start:start, end:end, text:normalizedValue.substr(start, end-start) }; return { start:start, end:end, text:normalizedValue.substr(start, end-start) };

Loading…
Cancel
Save