Index: trunk/phase3/resources/jquery/jquery.textSelection.js |
— | — | @@ -53,6 +53,8 @@ |
54 | 54 | */ |
55 | 55 | encapsulateSelection: function( options ) { |
56 | 56 | return this.each( function() { |
| 57 | + var pre = options.pre, post = options.post; |
| 58 | + |
57 | 59 | /** |
58 | 60 | * Check if the selected text is the same as the insert text |
59 | 61 | */ |
— | — | @@ -65,16 +67,34 @@ |
66 | 68 | } else { |
67 | 69 | while ( selText.charAt( selText.length - 1 ) == ' ' ) { |
68 | 70 | // Exclude ending space char |
69 | | - selText = selText.substring(0, selText.length - 1); |
70 | | - options.post += ' '; |
| 71 | + selText = selText.substring( 0, selText.length - 1 ); |
| 72 | + post += ' '; |
71 | 73 | } |
72 | 74 | while ( selText.charAt( 0 ) == ' ' ) { |
73 | 75 | // Exclude prepending space char |
74 | | - selText = selText.substring(1, selText.length); |
75 | | - options.pre = ' ' + options.pre; |
| 76 | + selText = selText.substring( 1, selText.length ); |
| 77 | + pre = ' ' + pre; |
76 | 78 | } |
77 | 79 | } |
78 | 80 | } |
| 81 | + |
| 82 | + /** |
| 83 | + * Do the splitlines stuff. |
| 84 | + * |
| 85 | + * Wrap each line of the selected text with pre and post |
| 86 | + */ |
| 87 | + function doSplitLines( selText, pre, post ) { |
| 88 | + var insertText = ''; |
| 89 | + var selTextArr = selText.split( '\n' ); |
| 90 | + for ( var i = 0; i < selTextArr.length; i++ ) { |
| 91 | + insertText += pre + selTextArr[i] + post; |
| 92 | + if ( i != selTextArr.length - 1 ) { |
| 93 | + insertText += '\n'; |
| 94 | + } |
| 95 | + } |
| 96 | + return insertText; |
| 97 | + } |
| 98 | + |
79 | 99 | var isSample = false; |
80 | 100 | if ( this.style.display == 'none' ) { |
81 | 101 | // Do nothing |
— | — | @@ -86,29 +106,34 @@ |
87 | 107 | var endPos = this.selectionEnd; |
88 | 108 | var scrollTop = this.scrollTop; |
89 | 109 | checkSelectedText(); |
| 110 | + |
| 111 | + var insertText = pre + selText + post; |
| 112 | + if ( options.splitlines ) { |
| 113 | + insertText = doSplitLines( selText, pre, post ); |
| 114 | + } |
90 | 115 | if ( options.ownline ) { |
91 | 116 | if ( startPos != 0 && this.value.charAt( startPos - 1 ) != "\n" ) { |
92 | | - options.pre = "\n" + options.pre; |
| 117 | + insertText = "\n" + insertText; |
93 | 118 | } |
94 | 119 | if ( this.value.charAt( endPos ) != "\n" ) { |
95 | | - options.post += "\n"; |
| 120 | + insertText += "\n"; |
96 | 121 | } |
97 | 122 | } |
98 | | - this.value = this.value.substring( 0, startPos ) + options.pre + selText + options.post + |
| 123 | + this.value = this.value.substring( 0, startPos ) + insertText + |
99 | 124 | this.value.substring( endPos, this.value.length ); |
100 | 125 | // Setting this.value scrolls the textarea to the top, restore the scroll position |
101 | 126 | this.scrollTop = scrollTop; |
102 | 127 | if ( window.opera ) { |
103 | | - options.pre = options.pre.replace( /\r?\n/g, "\r\n" ); |
| 128 | + pre = pre.replace( /\r?\n/g, "\r\n" ); |
104 | 129 | selText = selText.replace( /\r?\n/g, "\r\n" ); |
105 | | - options.post = options.post.replace( /\r?\n/g, "\r\n" ); |
| 130 | + post = post.replace( /\r?\n/g, "\r\n" ); |
106 | 131 | } |
107 | 132 | if ( isSample && options.selectPeri ) { |
108 | | - this.selectionStart = startPos + options.pre.length; |
109 | | - this.selectionEnd = startPos + options.pre.length + selText.length; |
| 133 | + this.selectionStart = startPos + pre.length; |
| 134 | + this.selectionEnd = startPos + pre.length + selText.length; |
110 | 135 | } else { |
111 | | - this.selectionStart = startPos + options.pre.length + selText.length + |
112 | | - options.post.length; |
| 136 | + this.selectionStart = startPos + pre.length + selText.length + |
| 137 | + post.length; |
113 | 138 | this.selectionEnd = this.selectionStart; |
114 | 139 | } |
115 | 140 | } else if ( document.selection && document.selection.createRange ) { |
— | — | @@ -120,33 +145,39 @@ |
121 | 146 | var selText = $(this).textSelection( 'getSelection' ); |
122 | 147 | var scrollTop = this.scrollTop; |
123 | 148 | var range = document.selection.createRange(); |
| 149 | + |
| 150 | + checkSelectedText(); |
| 151 | + var insertText = pre + selText + post; |
| 152 | + if ( options.splitlines ) { |
| 153 | + insertText = doSplitLines( selText, pre, post ); |
| 154 | + } |
124 | 155 | if ( options.ownline && range.moveStart ) { |
125 | 156 | var range2 = document.selection.createRange(); |
126 | 157 | range2.collapse(); |
127 | 158 | range2.moveStart( 'character', -1 ); |
128 | 159 | // FIXME: Which check is correct? |
129 | 160 | if ( range2.text != "\r" && range2.text != "\n" && range2.text != "" ) { |
130 | | - options.pre = "\n" + options.pre; |
| 161 | + insertText = "\n" + insertText; |
131 | 162 | } |
132 | 163 | var range3 = document.selection.createRange(); |
133 | 164 | range3.collapse( false ); |
134 | 165 | range3.moveEnd( 'character', 1 ); |
135 | 166 | if ( range3.text != "\r" && range3.text != "\n" && range3.text != "" ) { |
136 | | - options.post += "\n"; |
| 167 | + insertText += "\n"; |
137 | 168 | } |
138 | 169 | } |
139 | | - checkSelectedText(); |
140 | | - range.text = options.pre + selText + options.post; |
| 170 | + |
| 171 | + range.text = insertText; |
141 | 172 | if ( isSample && options.selectPeri && range.moveStart ) { |
142 | | - range.moveStart( 'character', - options.post.length - selText.length ); |
143 | | - range.moveEnd( 'character', - options.post.length ); |
| 173 | + range.moveStart( 'character', - post.length - selText.length ); |
| 174 | + range.moveEnd( 'character', - post.length ); |
144 | 175 | } |
145 | 176 | range.select(); |
146 | 177 | // Restore the scroll position |
147 | 178 | this.scrollTop = scrollTop; |
148 | 179 | } |
149 | 180 | $(this).trigger( 'encapsulateSelection', [ options.pre, options.peri, options.post, options.ownline, |
150 | | - options.replace ] ); |
| 181 | + options.replace, options.spitlines ] ); |
151 | 182 | }); |
152 | 183 | }, |
153 | 184 | /** |
— | — | @@ -382,7 +413,8 @@ |
383 | 414 | 'post': '', // Text to insert after the cursor/selection |
384 | 415 | 'ownline': false, // Put the inserted text on a line of its own |
385 | 416 | 'replace': false, // If there is a selection, replace it with peri instead of leaving it alone |
386 | | - 'selectPeri': true // Select the peri text if it was inserted (but not if there was a selection and replace==false) |
| 417 | + 'selectPeri': true, // Select the peri text if it was inserted (but not if there was a selection and replace==false) |
| 418 | + 'splitlines': true // If multiple lines are selected, encapsulate each line individually |
387 | 419 | }, options ); |
388 | 420 | break; |
389 | 421 | case 'getCaretPosition': |
Index: trunk/extensions/WikiEditor/modules/jquery.wikiEditor.toolbar.config.js |
— | — | @@ -270,7 +270,8 @@ |
271 | 271 | 'pre': "* ", |
272 | 272 | 'periMsg': 'wikieditor-toolbar-tool-ulist-example', |
273 | 273 | 'post': "", |
274 | | - 'ownline': true |
| 274 | + 'ownline': true, |
| 275 | + 'splitlines': true |
275 | 276 | } |
276 | 277 | } |
277 | 278 | }, |
— | — | @@ -285,7 +286,8 @@ |
286 | 287 | 'pre': "# ", |
287 | 288 | 'periMsg': 'wikieditor-toolbar-tool-olist-example', |
288 | 289 | 'post': "", |
289 | | - 'ownline': true |
| 290 | + 'ownline': true, |
| 291 | + 'splitlines': true |
290 | 292 | } |
291 | 293 | } |
292 | 294 | }, |