Browse files

Add prefixCursor function for EditControls

In some cases, `prefixSelectionLines` was used where it didn't make
sense or didn't work as expected.
  • Loading branch information...
1 parent ddf08e7 commit cb1f2e1f4185f59f01b9dbf23a77335c1ee42731 @gamefreak committed Apr 17, 2012
Showing with 15 additions and 3 deletions.
  1. +15 −3 lib/reddit_enhancement_suite.user.js
View
18 lib/reddit_enhancement_suite.user.js
@@ -9069,7 +9069,7 @@ modules['commentPreview'] = {
var disapproval = new EditControl(
'ಠ\_ಠ',
function() {
- prefixSelectionLines( targetTextArea, 'ಠ\\\_ಠ' );
+ prefixCursor( targetTextArea, 'ಠ\\\_ಠ' );
refreshPreview( preview, targetTextArea );
targetTextArea.focus();
}
@@ -9095,7 +9095,7 @@ modules['commentPreview'] = {
var reddiquette = new EditControl(
'reddiquette',
function() {
- prefixSelectionLines( targetTextArea, '[reddiquette](http://www.reddit.com/help/reddiquette)' );
+ prefixCursor( targetTextArea, '[reddiquette](http://www.reddit.com/help/reddiquette)' );
refreshPreview( preview, targetTextArea );
targetTextArea.focus();
}
@@ -9115,7 +9115,7 @@ modules['commentPreview'] = {
controlBox.appendChild( promoteRES.create() );
Array.prototype.slice.call(modules['commentPreview'].options['macros'].value).forEach(function(elem, index, array) {
controlBox.appendChild(new EditControl(elem[0], function(){
- prefixSelectionLines( targetTextArea, elem[1] );
+ prefixCursor( targetTextArea, elem[1] );
refreshPreview( preview, targetTextArea );
targetTextArea.focus();
}).create());
@@ -9198,6 +9198,18 @@ modules['commentPreview'] = {
}
}
+ function prefixCursor ( targetTextArea, prefix )
+ {
+ //Is scrollTop necessary?
+ var scrollTop = targetTextArea.scrollTop;
+ var text = targetTextArea.value;
+ var selectionStart = targetTextArea.selectionStart;
+ text = text.slice(0, selectionStart) + prefix + text.slice(selectionStart);
+ targetTextArea.value = text;
+ targetTextArea.selectionStart += prefix.length;
+ targetTextArea.scrollTop = scrollTop;
+ }
+
function prefixSelectionLines( targetTextArea, prefix )
{
var scrollTop = targetTextArea.scrollTop;

0 comments on commit cb1f2e1

Please sign in to comment.