##// END OF EJS Templates
Fixed adding text to multiple lines with formatting panel
neko259 -
r685:5f7e76d5 default
parent child Browse files
Show More
@@ -1,127 +1,102 b''
1 1 /*
2 2 @licstart The following is the entire license notice for the
3 3 JavaScript code in this page.
4 4
5 5
6 6 Copyright (C) 2013 neko259
7 7
8 8 The JavaScript code in this page is free software: you can
9 9 redistribute it and/or modify it under the terms of the GNU
10 10 General Public License (GNU GPL) as published by the Free Software
11 11 Foundation, either version 3 of the License, or (at your option)
12 12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15 15
16 16 As additional permission under GNU GPL version 3 section 7, you
17 17 may distribute non-source (e.g., minimized or compacted) forms of
18 18 that code without the copy of the GNU GPL normally required by
19 19 section 4, provided you include this license notice and a URL
20 20 through which recipients can access the Corresponding Source.
21 21
22 22 @licend The above is the entire license notice
23 23 for the JavaScript code in this page.
24 24 */
25 25
26 26 /**
27 27 * Add the desired characters to the start and end of selection.
28 *
28 29 * @param start Start (left) text
29 30 * @param end End (right) text
30 * @returns {boolean}
31 31 */
32 32 function addMarkToMsg(start, end) {
33 33 if (end.length == 0) {
34 34 return addTextToEachLineOfSelection(start);
35 35 }
36 36
37 37 var textareas = $('textarea');
38 38
39 39 for (var i = 0; i < textareas.length; i++) {
40 40 var textarea = textareas[i];
41 41
42 42 if (document.selection) {
43 43 textarea.focus();
44 44
45 45 var sel = document.selection.createRange();
46 46 sel.text = start + sel.text + end;
47 47 } else if (textarea.selectionStart || textarea.selectionStart == '0') {
48 48 textarea.focus();
49 49
50 50 var startPos = textarea.selectionStart;
51 51 var endPos = textarea.selectionEnd;
52 52
53 53 var oldValue = textarea.value;
54 54 textarea.value = oldValue.substring(0, startPos) + start +
55 55 oldValue.substring(startPos, endPos) + end +
56 56 oldValue.substring(endPos, oldValue.length);
57 57 } else {
58 58 textarea.value += start + end;
59 59 }
60 60 }
61 61
62 62 return false;
63 63 }
64 64
65 65 /**
66 66 * Add text to the beginning of each selected line. Partially selected lines
67 67 * are included
68 * @param textToAdd
69 * @returns {*}
68 *
69 * @param textToAdd Text to add to the each line
70 70 */
71 71 function addTextToEachLineOfSelection(textToAdd) {
72 var editor, end, newValue, start, value, _ref, _ref1;
73 editor = document.getElementsByTagName('textarea')[0];
74 _ref = [editor.selectionStart, editor.selectionEnd], start = _ref[0], end = _ref[1];
75 if (start == null) {
76 return;
72 var textareas = $('textarea');
73
74 for (var i = 0; i < textareas.length; i++) {
75 var textarea = textareas[i];
76
77 if (document.selection) {
78 textarea.focus();
79
80 var sel = document.selection.createRange();
81 sel.text = start + sel.text + end;
82 } else if (textarea.selectionStart || textarea.selectionStart == '0') {
83 textarea.focus();
84
85 var startPos = textarea.selectionStart;
86 var endPos = textarea.selectionEnd;
87
88 var oldValue = textarea.value;
89
90 var textBeforeSelection = oldValue.substring(0, startPos);
91 var selectionText = oldValue.substring(startPos, endPos)
92 .replace(/\n/g, '\n' + textToAdd);
93 textarea.value = textBeforeSelection + textToAdd +
94 selectionText +
95 oldValue.substring(endPos, oldValue.length);
96 } else {
97 textarea.value += textToAdd;
77 98 }
78 if (start === end) {
79 return;
80 }
81 console.log("Selection range: start=" + start + " end=" + end);
82 value = editor.value;
83 _ref1 = getLinesRange(start, end, value), start = _ref1[0], end = _ref1[1];
84 newValue = replaceLines(start, end, value, textToAdd);
85 return editor.value = newValue;
86 99 }
87 100
88 function replaceLines(start, end, value, textToAdd) {
89 var line, replacedText, text;
90 text = value.slice(start, end);
91 replacedText = ((function() {
92 var _i, _len, _ref, _results;
93 _ref = text.split("\n");
94 _results = [];
95 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
96 line = _ref[_i];
97 _results.push(textToAdd + line);
98 }
99 return _results;
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);
101 return false;
106 102 }
107
108 function getLinesRange(start, end, value) {
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 }
General Comments 0
You need to be logged in to leave comments. Login now