From 135156cea58dbbf20ec666f0128704e709b36eb7 Mon Sep 17 00:00:00 2001 From: Eston Bond Date: Wed, 27 Oct 2010 17:29:11 -0700 Subject: [PATCH] Adding code replacement techniques --- scratch/js/gollum.editor.js | 75 ++++++++++++++++++++++++-- scratch/js/jquery.fieldselection.js | 83 +++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 scratch/js/jquery.fieldselection.js diff --git a/scratch/js/gollum.editor.js b/scratch/js/gollum.editor.js index 1844199f..299b05e4 100644 --- a/scratch/js/gollum.editor.js +++ b/scratch/js/gollum.editor.js @@ -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); */ diff --git a/scratch/js/jquery.fieldselection.js b/scratch/js/jquery.fieldselection.js new file mode 100644 index 00000000..92fb6562 --- /dev/null +++ b/scratch/js/jquery.fieldselection.js @@ -0,0 +1,83 @@ +/* + * jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16 + * (c) 2006 Alex Brem - 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; }); + +})(); \ No newline at end of file