From 20086193ebcecb8806858ff789905056bcfe4691 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Wed, 12 Oct 2016 15:07:36 +0200 Subject: [PATCH] Support HTML input to rcube_text_editor.replace() (#5456) --- program/js/editor.js | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/program/js/editor.js b/program/js/editor.js index f2fe604cf..6eb473b62 100644 --- a/program/js/editor.js +++ b/program/js/editor.js @@ -407,27 +407,41 @@ function rcube_text_editor(config, id) }; // replace selection with text snippet - this.replace = function(text) + // input can be a string or object with 'text' and 'html' properties + this.replace = function(input) { - var ed = this.editor; + var format, ed = this.editor; // insert into tinymce editor if (ed) { ed.getWin().focus(); // correct focus in IE & Chrome - ed.selection.setContent(rcmail.quote_html(text).replace(/\r?\n/g, '
'), { format:'text' }); + + if ($.type(input) == 'object') { + input = input.html; + format = 'html'; + } + else { + input = rcmail.quote_html(input).replace(/\r?\n/g, '
'); + format = 'text'; + } + + ed.selection.setContent(input, {format: format}); } // replace selection in compose textarea else if (ed = rcube_find_object(this.id)) { - var selection = $(ed).is(':focus') ? rcmail.get_input_selection(ed) : { start:0, end:0 }, - inp_value = ed.value; - pre = inp_value.substring(0, selection.start), - end = inp_value.substring(selection.end, inp_value.length); + var selection = $(ed).is(':focus') ? rcmail.get_input_selection(ed) : {start: 0, end: 0}, + value = ed.value; + pre = value.substring(0, selection.start), + end = value.substring(selection.end, value.length); + + if ($.type(input) == 'object') + input = input.text; // insert response text - ed.value = pre + text + end; + ed.value = pre + input + end; // set caret after inserted text - rcmail.set_caret_pos(ed, selection.start + text.length); + rcmail.set_caret_pos(ed, selection.start + input.length); ed.focus(); } };