Adding code replacement techniques

This commit is contained in:
Eston Bond
2010-10-27 17:29:11 -07:00
parent c9cc0d3d37
commit 135156cea5
2 changed files with 155 additions and 3 deletions
+72 -3
View File
@@ -83,6 +83,7 @@
**/
var LanguageDefinition = {
_ACTIVE_LANG: '',
_LOADED_LANGS: [],
_LANG: {},
@@ -91,8 +92,32 @@
**/
define: function( name, definitionObject ) {
LanguageDefinition._LANG[name] = definitionObject;
LanguageDefinition._ACTIVE_LANG = name;
},
/**
* gets a definition object for a specified attribute
*
* @param string attr The specified attribute.
* @param string specified_lang The language to pull a definition for.
* @return object if exists, null otherwise
**/
getDefinitionFor: function( attr, specified_lang ) {
if ( !specified_lang ) {
specified_lang = LanguageDefinition._ACTIVE_LANG;
}
if ( LanguageDefinition.isLoadedFor(specified_lang) &&
LanguageDefinition._LANG[specified_lang][attr] &&
typeof LanguageDefinition_LANG[specified_lang][attr] == 'object' ) {
return LanguageDefinition._LANG[specified_lang][attr];
}
return null;
},
/**
* loadFor
* Asynchronously loads a definition file for the current markup.
@@ -174,14 +199,58 @@
.click(FunctionBar.evtFunctionButtonClick);
// show bar as active
$('#gollum-editor-function-bar').addClass('active');
this.isActive = true;
FunctionBar.isActive = true;
},
evtFunctionButtonClick: function(e) {
e.preventDefault();
alert($(this).attr('id'));
}
var def = LanguageDefinition.getDefinitionFor( $(this).attr() );
if ( def ) {
FunctionBar.executeAction( def );
}
},
executeAction: function( definitionObject ) {
// get the selected text from the textarea
var txt = $('#gollum-editor-body').val();
// hmm, I'm not sure this will work in a textarea
var selText = $('#gollum-editor-body').getSelection();
var repText = null;
// exec a function if it exists first
if ( definitionObject.exec &&
typeof definitionObject.exec == 'function' ) {
definitionObject(txt, selText, $('#gollum-editor-body'));
}
// exec search/replace if both exist
if ( definitionObject.replace &&
typeof definitionObject.replace == 'string' ) {
var searchRegex = /(.*)/gi;
if ( definitionObject.search &&
( typeof definitionObject.search == 'string' ||
typeof definitionObject.search == 'regexp' )) {
searchRegex = definitionObject.search;
} else {
debug('Invalid search regex, using default');
}
repText = selText.replace(searchRegex, definitionObject.replace);
} else {
debug('Invalid replacement string');
}
// now, add append if it exists
if ( definitionObject.append &&
typeof definitionObject.append == 'string' ) {
repText += definitionObject.append;
}
if (repText)
$('#gollum-editor-body').replaceSelection(repText);
}
};
/* })(jQuery); */
+83
View File
@@ -0,0 +1,83 @@
/*
* jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16
* (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd
*/
(function() {
var fieldSelection = {
getSelection: function() {
var e = this.jquery ? this[0] : this;
return (
/* mozilla / dom 3.0 */
('selectionStart' in e && function() {
var l = e.selectionEnd - e.selectionStart;
return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
}) ||
/* exploder */
(document.selection && function() {
e.focus();
var r = document.selection.createRange();
if (r == null) {
return { start: 0, end: e.value.length, length: 0 }
}
var re = e.createTextRange();
var rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);
return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
}) ||
/* browser not supported */
function() {
return { start: 0, end: e.value.length, length: 0 };
}
)();
},
replaceSelection: function() {
var e = this.jquery ? this[0] : this;
var text = arguments[0] || '';
return (
/* mozilla / dom 3.0 */
('selectionStart' in e && function() {
e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
return this;
}) ||
/* exploder */
(document.selection && function() {
e.focus();
document.selection.createRange().text = text;
return this;
}) ||
/* browser not supported */
function() {
e.value += text;
return this;
}
)();
}
};
jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; });
})();