Show More
@@ -25,9 +25,9 b'' | |||||
25 |
|
25 | |||
26 | /** |
|
26 | /** | |
27 | * Add the desired characters to the start and end of selection. |
|
27 | * Add the desired characters to the start and end of selection. | |
|
28 | * | |||
28 | * @param start Start (left) text |
|
29 | * @param start Start (left) text | |
29 | * @param end End (right) text |
|
30 | * @param end End (right) text | |
30 | * @returns {boolean} |
|
|||
31 | */ |
|
31 | */ | |
32 | function addMarkToMsg(start, end) { |
|
32 | function addMarkToMsg(start, end) { | |
33 | if (end.length == 0) { |
|
33 | if (end.length == 0) { | |
@@ -65,63 +65,38 b' function addMarkToMsg(start, end) {' | |||||
65 | /** |
|
65 | /** | |
66 | * Add text to the beginning of each selected line. Partially selected lines |
|
66 | * Add text to the beginning of each selected line. Partially selected lines | |
67 | * are included |
|
67 | * are included | |
68 | * @param textToAdd |
|
68 | * | |
69 | * @returns {*} |
|
69 | * @param textToAdd Text to add to the each line | |
70 | */ |
|
70 | */ | |
71 | function addTextToEachLineOfSelection(textToAdd) { |
|
71 | function addTextToEachLineOfSelection(textToAdd) { | |
72 | var editor, end, newValue, start, value, _ref, _ref1; |
|
72 | var textareas = $('textarea'); | |
73 | editor = document.getElementsByTagName('textarea')[0]; |
|
73 | ||
74 | _ref = [editor.selectionStart, editor.selectionEnd], start = _ref[0], end = _ref[1]; |
|
74 | for (var i = 0; i < textareas.length; i++) { | |
75 | if (start == null) { |
|
75 | var textarea = textareas[i]; | |
76 | return; |
|
76 | ||
77 | } |
|
77 | if (document.selection) { | |
78 | if (start === end) { |
|
78 | textarea.focus(); | |
79 | return; |
|
79 | ||
80 | } |
|
80 | var sel = document.selection.createRange(); | |
81 | console.log("Selection range: start=" + start + " end=" + end); |
|
81 | sel.text = start + sel.text + end; | |
82 | value = editor.value; |
|
82 | } else if (textarea.selectionStart || textarea.selectionStart == '0') { | |
83 | _ref1 = getLinesRange(start, end, value), start = _ref1[0], end = _ref1[1]; |
|
83 | textarea.focus(); | |
84 | newValue = replaceLines(start, end, value, textToAdd); |
|
|||
85 | return editor.value = newValue; |
|
|||
86 | } |
|
|||
87 |
|
84 | |||
88 | function replaceLines(start, end, value, textToAdd) { |
|
85 | var startPos = textarea.selectionStart; | |
89 | var line, replacedText, text; |
|
86 | var endPos = textarea.selectionEnd; | |
90 | text = value.slice(start, end); |
|
87 | ||
91 | replacedText = ((function() { |
|
88 | var oldValue = textarea.value; | |
92 | var _i, _len, _ref, _results; |
|
89 | ||
93 | _ref = text.split("\n"); |
|
90 | var textBeforeSelection = oldValue.substring(0, startPos); | |
94 | _results = []; |
|
91 | var selectionText = oldValue.substring(startPos, endPos) | |
95 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
|
92 | .replace(/\n/g, '\n' + textToAdd); | |
96 | line = _ref[_i]; |
|
93 | textarea.value = textBeforeSelection + textToAdd + | |
97 | _results.push(textToAdd + line); |
|
94 | selectionText + | |
|
95 | oldValue.substring(endPos, oldValue.length); | |||
|
96 | } else { | |||
|
97 | textarea.value += textToAdd; | |||
98 | } |
|
98 | } | |
99 | return _results; |
|
99 | } | |
100 | })()).join("\n"); |
|
|||
101 | return replaceSubstring(start, end, value, replacedText); |
|
|||
102 | } |
|
|||
103 |
|
||||
104 | function replaceSubstring(start, end, string, replacingString) { |
|
|||
105 | return string.slice(0, start) + replacingString + string.slice(end); |
|
|||
106 | } |
|
|||
107 |
|
100 | |||
108 | function getLinesRange(start, end, value) { |
|
101 | return false; | |
109 | var i, rangeEnd, rangeStart, _i, _j, _ref, _ref1; |
|
|||
110 | if (value[start] === "\n") { |
|
|||
111 | start = start - 1; |
|
|||
112 | } |
|
|||
113 | _ref = [start, end], rangeStart = _ref[0], rangeEnd = _ref[1]; |
|
|||
114 | for (i = _i = start; start <= 0 ? _i <= 0 : _i >= 0; i = start <= 0 ? ++_i : --_i) { |
|
|||
115 | if (value[i] === "\n") { |
|
|||
116 | break; |
|
|||
117 | } |
|
|||
118 | rangeStart = i; |
|
|||
119 | } |
|
|||
120 | for (i = _j = end, _ref1 = value.length; end <= _ref1 ? _j < _ref1 : _j > _ref1; i = end <= _ref1 ? ++_j : --_j) { |
|
|||
121 | if (value[i] === "\n") { |
|
|||
122 | break; |
|
|||
123 | } |
|
|||
124 | rangeEnd = i; |
|
|||
125 | } |
|
|||
126 | return [rangeStart, rangeEnd]; |
|
|||
127 | } |
|
102 | } |
General Comments 0
You need to be logged in to leave comments.
Login now