Change regex replacement methods to allow for replacement at cursor
This commit is contained in:
@@ -453,6 +453,7 @@
|
|||||||
var selText = FunctionBar.getFieldSelection( $('#gollum-editor-body') );
|
var selText = FunctionBar.getFieldSelection( $('#gollum-editor-body') );
|
||||||
var repText = selText;
|
var repText = selText;
|
||||||
var reselect = true;
|
var reselect = true;
|
||||||
|
var cursor = null;
|
||||||
|
|
||||||
// execute a replacement function if one exists
|
// execute a replacement function if one exists
|
||||||
if ( definitionObject.exec &&
|
if ( definitionObject.exec &&
|
||||||
@@ -469,12 +470,36 @@
|
|||||||
searchExp = definitionObject.search;
|
searchExp = definitionObject.search;
|
||||||
debug( searchExp );
|
debug( searchExp );
|
||||||
}
|
}
|
||||||
|
debug(repText);
|
||||||
// replace text
|
// replace text
|
||||||
if ( definitionObject.replace &&
|
if ( definitionObject.replace &&
|
||||||
typeof definitionObject.replace == 'string' ) {
|
typeof definitionObject.replace == 'string' ) {
|
||||||
debug('Running replacement - using ' + definitionObject.replace);
|
debug('Running replacement - using ' + definitionObject.replace);
|
||||||
repText = repText.replace( searchExp, definitionObject.replace );
|
var rt = definitionObject.replace;
|
||||||
|
var matches = searchExp.exec( repText );
|
||||||
|
if ( matches && matches.length > 1 ) {
|
||||||
|
debug(matches);
|
||||||
|
for ( var i = 1; i < matches.length; i++ ) {
|
||||||
|
var searchStr = '$' + i;
|
||||||
|
debug('searching for ' + searchStr + ' to replace with ' + matches[i]);
|
||||||
|
rt = rt.replace( searchStr, matches[i] );
|
||||||
|
}
|
||||||
|
repText = rt;
|
||||||
|
} else if ( repText == '' ) {
|
||||||
|
debug('Search string is empty');
|
||||||
|
|
||||||
|
// find position of $1 - this is where we will place the cursor
|
||||||
|
var cursor = rt.indexOf('$1');
|
||||||
|
|
||||||
|
// we have an empty string, so just remove backreferences
|
||||||
|
repText = rt.replace( /\$[\d]/g, '' );
|
||||||
|
|
||||||
|
// if the position of $1 doesn't exist, stick the cursor in
|
||||||
|
// the middle
|
||||||
|
if ( cursor == -1 ) {
|
||||||
|
cursor = Math.floor( rt.length / 2 );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// append if necessary
|
// append if necessary
|
||||||
@@ -485,10 +510,10 @@
|
|||||||
}
|
}
|
||||||
repText += definitionObject.append;
|
repText += definitionObject.append;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (repText)
|
if ( repText )
|
||||||
FunctionBar.replaceFieldSelection( $('#gollum-editor-body'),
|
FunctionBar.replaceFieldSelection( $('#gollum-editor-body'),
|
||||||
repText, reselect );
|
repText, reselect, cursor );
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -607,7 +632,7 @@
|
|||||||
* @param string The string to replace the current selection with.
|
* @param string The string to replace the current selection with.
|
||||||
* @param boolean Reselect the new text range.
|
* @param boolean Reselect the new text range.
|
||||||
*/
|
*/
|
||||||
replaceFieldSelection: function( $field, replaceText, reselect ) {
|
replaceFieldSelection: function( $field, replaceText, reselect, cursorOffset ) {
|
||||||
var selPos = FunctionBar.getFieldSelectionPosition( $field );
|
var selPos = FunctionBar.getFieldSelectionPosition( $field );
|
||||||
var fullStr = $field.val();
|
var fullStr = $field.val();
|
||||||
var selectNew = true;
|
var selectNew = true;
|
||||||
@@ -626,13 +651,25 @@
|
|||||||
|
|
||||||
if ( selectNew ) {
|
if ( selectNew ) {
|
||||||
if ( $field[0].setSelectionRange ) {
|
if ( $field[0].setSelectionRange ) {
|
||||||
$field[0].setSelectionRange( selPos.start,
|
if ( cursorOffset ) {
|
||||||
selPos.start + replaceText.length );
|
$field[0].setSelectionRange(
|
||||||
|
selPos.start + cursorOffset,
|
||||||
|
selPos.start + cursorOffset
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$field[0].setSelectionRange( selPos.start,
|
||||||
|
selPos.start + replaceText.length );
|
||||||
|
}
|
||||||
} else if ( $field[0].createTextRange ) {
|
} else if ( $field[0].createTextRange ) {
|
||||||
var range = $field[0].createTextRange();
|
var range = $field[0].createTextRange();
|
||||||
range.collapse( true );
|
range.collapse( true );
|
||||||
range.moveEnd( 'character', selPos.start + replaceText.length );
|
if ( cursorOffset ) {
|
||||||
range.moveStart( 'character', selPos.start );
|
range.moveEnd( selPos.start + cursorOffset );
|
||||||
|
range.moveStart( selPos.start + cursorOffset );
|
||||||
|
} else {
|
||||||
|
range.moveEnd( 'character', selPos.start + replaceText.length );
|
||||||
|
range.moveStart( 'character', selPos.start );
|
||||||
|
}
|
||||||
range.select();
|
range.select();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user