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