User:WhoSaidThat/CodeMirrorInterface.js

From Wikispecies
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* 
	
	Wikimedia CodeMirror based script interface

	Based on the examples from codemirror.net

*/
function CodeMirrorInterface(element,mode){

  this.theme='default';
  
  this.availableModes={'js':'javascript','css':'css','xml':'xml'};
  mode=this.availableModes[mode]?this.availableModes[mode]:'wikitext';

  if(element.tagName){
    this.editor = CodeMirror.fromTextArea( element,{'theme':this.theme,'mode': mode,'lineNumbers': true ,
       lineWrapping: true, onGutterClick: CodeMirror.newFoldFunction(CodeMirror.braceRangeFinder) } );
  }else{
    var element=$(element).get(0);
    if(!element){  return; }
    this.editor = CodeMirror.fromTextArea( element,{'theme':this.theme,'mode': mode,'lineNumbers': true,
    lineWrapping: true, onGutterClick: CodeMirror.newFoldFunction(CodeMirror.braceRangeFinder) } );
  }
  
  this.editor.setOption( 'onKeyEvent' , captureKeyPress );
  
  
  //this.editor.setOption( 'onGutterClick',CodeMirror.newFoldFunction(CodeMirror.braceRangeFinder) );
  
  this.searchObject = { 'lastPos' :null, 'lastQuery': null, 'marked' : [] };

  this.unmark = function(){
    for (var i = 0; i < this.searchObject.marked.length; ++i) this.searchObject.marked[i].clear();
    this.searchObject.marked.length = 0;
  }

  this.mark=function(text){
    
    for (var cursor = this.editor.getSearchCursor(text); cursor.findNext();)
      this.searchObject.marked.push(this.editor.markText(cursor.from(), cursor.to(), "cm-"+this.theme+"-searched"));
      
  }
    
  this.search = function (text) {
    this.unmark();
    if (!text) return;
    this.mark(text);
    if (this.searchObject.lastQuery != text) this.searchObject.lastPos = null;
    var cursor = this.editor.getSearchCursor(text, this.searchObject.lastPos || this.editor.getCursor());
    if (!cursor.findNext()) {
      cursor = this.editor.getSearchCursor(text);
      if (!cursor.findNext()) return;
    }
    this.editor.setSelection(cursor.from(), cursor.to());
    this.searchObject.lastQuery = text; this.searchObject.lastPos = cursor.to();
    
  }

  
  this.replaceSingle = function(find,replace){
    this.unmark();
    var cursor = this.editor.getSearchCursor(find);
    cursor.findNext();
    cursor.replace(replace);
    this.mark(find);
  }
  this.replaceAll = function(find,replace){
    this.unmark();
    if (!find) return;
    for (var cursor = this.editor.getSearchCursor(find); cursor.findNext();) cursor.replace(replace);
  }
  
  function captureKeyPress(cm, e){
    // Hook into ctrl-space
    if (e.keyCode == 32 && (e.ctrlKey || e.metaKey) && !e.altKey) {
      e.stop();
      return CodeMirror.simpleHint(cm, CodeMirror.javascriptHint);
    }
  }
  
}

CodeMirror.defineMode("wikitext", function(config, parserConfig) {
  var wikitextOverlay = {
    token: function(stream, state) {
      if (stream.match("{{")) {
        while ((ch = stream.next()) != null)
          if (ch == "}" && stream.next() == "}") break;
        return "template";
      }
      if (stream.match("[[")) {
        while ((ch = stream.next()) != null)
          if (ch == "]" && stream.next() == "]") break;
        return "link";
      }
      while (stream.next() != null && !stream.match("{{", false) && !stream.match("[[", false)) {}
      return null;
    }
  };
  return CodeMirror.overlayParser(CodeMirror.getMode(config, parserConfig.backdrop || "text/html"), wikitextOverlay);
});