# HG changeset patch # User neko259 # Date 2014-05-20 09:37:18 # Node ID 5f7e76d5b8554e50afb06f6a8b110ba4b77a00cc # Parent c595713d2c0b132b0aeb009ee41225119e72646c Fixed adding text to multiple lines with formatting panel diff --git a/boards/static/js/panel.js b/boards/static/js/panel.js --- a/boards/static/js/panel.js +++ b/boards/static/js/panel.js @@ -25,9 +25,9 @@ /** * Add the desired characters to the start and end of selection. + * * @param start Start (left) text * @param end End (right) text - * @returns {boolean} */ function addMarkToMsg(start, end) { if (end.length == 0) { @@ -65,63 +65,38 @@ function addMarkToMsg(start, end) { /** * Add text to the beginning of each selected line. Partially selected lines * are included - * @param textToAdd - * @returns {*} + * + * @param textToAdd Text to add to the each line */ function addTextToEachLineOfSelection(textToAdd) { - var editor, end, newValue, start, value, _ref, _ref1; - editor = document.getElementsByTagName('textarea')[0]; - _ref = [editor.selectionStart, editor.selectionEnd], start = _ref[0], end = _ref[1]; - if (start == null) { - return; - } - if (start === end) { - return; - } - console.log("Selection range: start=" + start + " end=" + end); - value = editor.value; - _ref1 = getLinesRange(start, end, value), start = _ref1[0], end = _ref1[1]; - newValue = replaceLines(start, end, value, textToAdd); - return editor.value = newValue; -} + var textareas = $('textarea'); + + for (var i = 0; i < textareas.length; i++) { + var textarea = textareas[i]; + + if (document.selection) { + textarea.focus(); + + var sel = document.selection.createRange(); + sel.text = start + sel.text + end; + } else if (textarea.selectionStart || textarea.selectionStart == '0') { + textarea.focus(); -function replaceLines(start, end, value, textToAdd) { - var line, replacedText, text; - text = value.slice(start, end); - replacedText = ((function() { - var _i, _len, _ref, _results; - _ref = text.split("\n"); - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - line = _ref[_i]; - _results.push(textToAdd + line); + var startPos = textarea.selectionStart; + var endPos = textarea.selectionEnd; + + var oldValue = textarea.value; + + var textBeforeSelection = oldValue.substring(0, startPos); + var selectionText = oldValue.substring(startPos, endPos) + .replace(/\n/g, '\n' + textToAdd); + textarea.value = textBeforeSelection + textToAdd + + selectionText + + oldValue.substring(endPos, oldValue.length); + } else { + textarea.value += textToAdd; } - return _results; - })()).join("\n"); - return replaceSubstring(start, end, value, replacedText); -} - -function replaceSubstring(start, end, string, replacingString) { - return string.slice(0, start) + replacingString + string.slice(end); -} + } -function getLinesRange(start, end, value) { - var i, rangeEnd, rangeStart, _i, _j, _ref, _ref1; - if (value[start] === "\n") { - start = start - 1; - } - _ref = [start, end], rangeStart = _ref[0], rangeEnd = _ref[1]; - for (i = _i = start; start <= 0 ? _i <= 0 : _i >= 0; i = start <= 0 ? ++_i : --_i) { - if (value[i] === "\n") { - break; - } - rangeStart = i; - } - for (i = _j = end, _ref1 = value.length; end <= _ref1 ? _j < _ref1 : _j > _ref1; i = end <= _ref1 ? ++_j : --_j) { - if (value[i] === "\n") { - break; - } - rangeEnd = i; - } - return [rangeStart, rangeEnd]; + return false; }