Changeset 4948

Show
Ignore:
Timestamp:
10/30/09 10:04:57 (4 weeks ago)
Author:
auno
Message:

Updated codemirror code. BugzID#102871 102873

Location:
branches/greyhound/mt-static/codemirror/js
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • branches/greyhound/mt-static/codemirror/js/codemirror.js

    r4932 r4948  
    2727  // their meaning. 
    2828  setDefaults(CodeMirrorConfig, { 
    29     lang: "en", 
    3029    stylesheet: "", 
    3130    path: "", 
     
    3534    passDelay: 200, 
    3635    passTime: 50, 
     36    lineNumberDelay: 200, 
     37    lineNumberTime: 50, 
    3738    continuousScanning: false, 
    3839    saveFunction: null, 
     
    4344    textWrapping: true, 
    4445    readOnly: false, 
    45     width: "100%", 
     46    width: "", 
    4647    height: "300px", 
    4748    autoMatchParens: false, 
     
    7273  } 
    7374 
    74   function activateLineNumbers(frame, nums) { 
    75     var win = frame.contentWindow, doc = win.document, 
    76         scroller = nums.firstChild; 
    77  
    78     var nextNum = 1, barWidth = null; 
    79     function sizeBar() { 
    80       for (var root = frame; root.parentNode; root = root.parentNode); 
    81       if (!nums.parentNode || root != document || !win.Editor) { 
    82         // Clear event handlers (their nodes might already be collected, so try/catch) 
    83         try{onScroll();}catch(e){} 
    84         try{onResize();}catch(e){} 
    85         clearInterval(sizeInterval); 
    86         return; 
    87       } 
    88  
    89       if (nums.offsetWidth != barWidth) { 
    90         barWidth = nums.offsetWidth; 
    91         nums.style.left = "-" + (frame.parentNode.style.marginLeft = barWidth + "px"); 
    92       } 
    93     } 
    94     function update() { 
    95       var diff = 20 + Math.max(doc.body.offsetHeight, frame.offsetHeight) - scroller.offsetHeight; 
    96       for (var n = Math.ceil(diff / 10); n > 0; n--) { 
    97         var div = document.createElement("DIV"); 
    98         div.appendChild(document.createTextNode(nextNum++)); 
    99         scroller.appendChild(div); 
    100       } 
    101       nums.scrollTop = doc.body.scrollTop || doc.documentElement.scrollTop || 0; 
    102     } 
    103     var onScroll = win.addEventHandler(win, "scroll", update, true), 
    104         onResize = win.addEventHandler(win, "resize", update, true), 
    105         sizeInterval = setInterval(sizeBar, 500); 
    106     sizeBar(); 
    107     update(); 
    108   } 
    109  
    11075  function CodeMirror(place, options) { 
    11176    // Backward compatibility for deprecated options. 
     
    12287    frame.src = "javascript:false;"; 
    12388    frame.style.border = "0"; 
    124     frame.style.width = options.width; 
    125     frame.style.height = options.height; 
     89    frame.style.width = '100%'; 
     90    frame.style.height = '100%'; 
    12691    // display: block occasionally suppresses some Firefox bugs, so we 
    12792    // always add it, redundant as it sounds. 
     
    13196    div.style.position = "relative"; 
    13297    div.className = "CodeMirror-wrapping"; 
     98    div.style.width = options.width; 
     99    div.style.height = options.height; 
     100 
    133101    if (place.appendChild) place.appendChild(div); 
    134102    else place(div); 
     
    146114      options.stylesheet = [options.stylesheet]; 
    147115 
    148     var html = ["<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html lang="+options.lang+"><head>"]; 
     116    var html = ["<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html><head>"]; 
    149117    // Hack to work around a bunch of IE8-specific problems. 
    150118    html.push("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\"/>"); 
     
    167135    init: function() { 
    168136      if (this.options.initCallback) this.options.initCallback(this); 
    169       if (this.lineNumbers) activateLineNumbers(this.frame, this.lineNumbers); 
     137      if (this.options.lineNumbers) this.activateLineNumbers(); 
    170138      if (this.options.reindentOnLoad) this.reindent(); 
    171139    }, 
     
    208176    setParser: function(name) {this.editor.setParser(name);}, 
    209177    setSpellcheck: function(on) {this.win.document.body.spellcheck = on;}, 
    210     setTextWrapping: function(on) {this.win.document.body.style.whiteSpace = on ? "" : "nowrap";}, 
     178    setTextWrapping: function(on) { 
     179      if (on == this.options.textWrapping) return; 
     180      this.win.document.body.style.whiteSpace = on ? "" : "nowrap"; 
     181      this.options.textWrapping = on; 
     182      if (this.lineNumbers) { 
     183        this.setLineNumbers(false); 
     184        this.setLineNumbers(true); 
     185      } 
     186    }, 
    211187    setIndentUnit: function(unit) {this.win.indentUnit = unit;}, 
    212188    setUndoDepth: function(depth) {this.editor.history.maxDepth = depth;}, 
     
    215191      if (on && !this.lineNumbers) { 
    216192        this.lineNumbers = addLineNumberDiv(this.wrapping); 
    217         activateLineNumbers(this.frame, this.lineNumbers); 
     193        this.activateLineNumbers(); 
    218194      } 
    219195      else if (!on && this.lineNumbers) { 
     
    258234    currentLine: function() { 
    259235      return this.lineNumber(this.cursorPosition().line); 
     236    }, 
     237 
     238    activateLineNumbers: function() { 
     239      var frame = this.frame, win = frame.contentWindow, doc = win.document, body = doc.body, 
     240          nums = this.lineNumbers, scroller = nums.firstChild, self = this; 
     241      var barWidth = null; 
     242 
     243      function sizeBar() { 
     244        for (var root = frame; root.parentNode; root = root.parentNode); 
     245        if (!nums.parentNode || root != document || !win.Editor) { 
     246          // Clear event handlers (their nodes might already be collected, so try/catch) 
     247          try{clear();}catch(e){} 
     248          clearInterval(sizeInterval); 
     249          return; 
     250        } 
     251 
     252        if (nums.offsetWidth != barWidth) { 
     253          barWidth = nums.offsetWidth; 
     254          nums.style.left = "-" + (frame.parentNode.style.marginLeft = barWidth + "px"); 
     255        } 
     256      } 
     257      function doScroll() { 
     258        nums.scrollTop = body.scrollTop || doc.documentElement.scrollTop || 0; 
     259      } 
     260      // Cleanup function, registered by nonWrapping and wrapping. 
     261      var clear = function(){}; 
     262      sizeBar(); 
     263      sizeInterval = setInterval(sizeBar, 500); 
     264 
     265      function nonWrapping() { 
     266        var nextNum = 1; 
     267        function update() { 
     268          var target = 50 + Math.max(body.offsetHeight, frame.offsetHeight); 
     269          while (scroller.offsetHeight < target) { 
     270            scroller.appendChild(document.createElement("DIV")); 
     271            scroller.lastChild.innerHTML = nextNum++; 
     272          } 
     273          doScroll(); 
     274        } 
     275        var onScroll = win.addEventHandler(win, "scroll", update, true), 
     276            onResize = win.addEventHandler(win, "resize", update, true); 
     277        clear = function(){onScroll(); onResize();}; 
     278      } 
     279      function wrapping() { 
     280        var node, lineNum, next, pos; 
     281 
     282        function addNum(n) { 
     283          if (!lineNum) lineNum = scroller.appendChild(document.createElement("DIV")); 
     284          lineNum.innerHTML = n; 
     285          pos = lineNum.offsetHeight + lineNum.offsetTop; 
     286          lineNum = lineNum.nextSibling; 
     287        } 
     288        function work() { 
     289          if (!scroller.parentNode || scroller.parentNode != self.lineNumbers) return; 
     290 
     291          var endTime = new Date().getTime() + self.options.lineNumberTime; 
     292          while (node) { 
     293            addNum(next++); 
     294            for (; node && !win.isBR(node); node = node.nextSibling) { 
     295              var bott = node.offsetTop + node.offsetHeight; 
     296              while (bott - 3 > pos) addNum("&nbsp;"); 
     297            } 
     298            if (node) node = node.nextSibling; 
     299            if (new Date().getTime() > endTime) { 
     300              pending = setTimeout(work, self.options.lineNumberDelay); 
     301              return; 
     302            } 
     303          } 
     304          // While there are un-processed number DIVs, or the scroller is smaller than the frame... 
     305          var target = 50 + Math.max(body.offsetHeight, frame.offsetHeight); 
     306          while (lineNum || scroller.offsetHeight < target) addNum(next++); 
     307          doScroll(); 
     308        } 
     309        function start() { 
     310          doScroll(); 
     311          node = body.firstChild; 
     312          lineNum = scroller.firstChild; 
     313          pos = 0; 
     314          next = 1; 
     315          work(); 
     316        } 
     317 
     318        start(); 
     319        var pending = null; 
     320        function update() { 
     321          if (pending) clearTimeout(pending); 
     322          pending = setTimeout(start, self.options.lineNumberDelay); 
     323        } 
     324        self.updateNumbers = update; 
     325        var onScroll = win.addEventHandler(win, "scroll", doScroll, true), 
     326            onResize = win.addEventHandler(win, "resize", update, true); 
     327        clear = function(){ 
     328          if (pending) clearTimeout(pending); 
     329          if (self.updateNumbers == update) self.updateNumbers == null; 
     330          onScroll(); 
     331          onResize(); 
     332        }; 
     333      } 
     334      (this.options.textWrapping ? wrapping : nonWrapping)(); 
    260335    } 
    261336  }; 
  • branches/greyhound/mt-static/codemirror/js/editor.js

    r4932 r4948  
    125125    var point = null; 
    126126 
     127    // This an Opera-specific hack -- always insert an empty span 
     128    // between two BRs, because Opera's cursor code gets terribly 
     129    // confused when the cursor is between two BRs. 
     130    var afterBR = true; 
     131 
    127132    // Insert a normalized node at the current point. If it is a text 
    128133    // node, wrap it in a <span>, and give that span a currentText 
     
    137142        part = makePartSpan(part, owner); 
    138143        text = part.currentText; 
     144        afterBR = false; 
     145      } 
     146      else { 
     147        if (afterBR && window.opera) 
     148          point(makePartSpan(""), owner); 
     149        afterBR = true; 
    139150      } 
    140151      part.dirty = true; 
     
    174185      if (partNode(node)){ 
    175186        nodeQueue.push(node); 
     187        afterBR = false; 
    176188        return yield(node.currentText, c); 
    177189      } 
    178190      else if (isBR(node)) { 
     191        if (afterBR && window.opera) 
     192          node.parentNode.insertBefore(makePartSpan("", owner), node); 
    179193        nodeQueue.push(node); 
     194        afterBR = true; 
    180195        return yield("\n", c); 
    181196      } 
     
    354369    var container = this.container = this.doc.body; 
    355370    this.win = window; 
    356     this.history = new History(container, options.undoDepth, options.undoDelay, 
    357                                this, options.onChange); 
     371    this.history = new History(container, options.undoDepth, options.undoDelay, this); 
    358372    var self = this; 
    359373 
     
    369383    if (options.content) 
    370384      this.importCode(options.content); 
     385    this.history.onChange = options.onChange; 
    371386 
    372387    if (!options.readOnly) { 
     
    419434        catch(e) {} 
    420435        if (text !== null) { 
     436          event.stop(); 
    421437          self.replaceSelection(text); 
    422438          select.scrollToCursor(this.container); 
    423           event.stop(); 
    424439        } 
    425440      }); 
     
    431446      container.style.whiteSpace = "nowrap"; 
    432447    } 
    433   } 
     448   } 
    434449 
    435450  function isSafeKey(code) { 
     
    764779    keyUp: function(event) { 
    765780      this.cursorActivity(isSafeKey(event.keyCode)); 
     781      if (event.keyCode == 13) 
     782        this.scheduleHighlight(); 
    766783    }, 
    767784 
     
    835852      if (this.highlight(pos, endOfLine(to, this.container), true, 20) === false) 
    836853        return false; 
    837       select.selectMarked(); 
    838854      return true; 
    839855    }, 
     
    10101026        this.selectionSnapshot = select.getBookmark(this.container); 
    10111027      } 
     1028      if (window.frameElement && window.frameElement.CodeMirror.updateNumbers) { 
     1029        window.frameElement.CodeMirror.updateNumbers(); 
     1030      } 
    10121031 
    10131032      var activity = this.options.cursorActivity; 
     
    10181037        if (activity) activity(cursor); 
    10191038        if (!safe) { 
    1020 //          this.scheduleHighlight(); 
    10211039          this.addDirtyNode(cursor); 
    10221040        } 
     
    10661084          // longer in the document, it should not be returned. 
    10671085          while (found && found.parentNode != this.container) 
    1068             found = found.parentNode 
     1086            found = found.parentNode; 
    10691087          if (found && (found.dirty || found.nodeType == 3)) 
    10701088            return found; 
     
    12001218          stream = stringStream(traversal), 
    12011219          parsed = from ? from.parserFromHere(stream) : Editor.Parser.make(stream); 
     1220 
     1221      function surroundedByBRs(node) { 
     1222        return (node.previousSibling == null || isBR(node.previousSibling)) && 
     1223               (node.nextSibling == null || isBR(node.nextSibling)); 
     1224      } 
    12021225 
    12031226      // parts is an interface to make it possible to 'delay' fetching 
     
    12321255          // insertNewlineAtCursor). 
    12331256          while (part && isSpan(part) && part.currentText == "") { 
    1234             var old = part; 
    1235             this.remove(); 
    1236             part = this.get(); 
    1237             // Adjust selection information, if any. See select.js for details. 
    1238             select.snapshotMove(old.firstChild, part && (part.firstChild || part), 0); 
     1257            // Leave empty nodes that are alone on a line alone in 
     1258            // Opera, since that browsers doesn't deal well with 
     1259            // having 2 BRs in a row. 
     1260            if (window.opera && surroundedByBRs(part)) { 
     1261              this.next(); 
     1262              part = this.get(); 
     1263            } 
     1264            else { 
     1265              var old = part; 
     1266              this.remove(); 
     1267              part = this.get(); 
     1268              // Adjust selection information, if any. See select.js for details. 
     1269              select.snapshotMove(old.firstChild, part && (part.firstChild || part), 0); 
     1270            } 
    12391271          } 
     1272           
    12401273          return part; 
    12411274        } 
  • branches/greyhound/mt-static/codemirror/js/select.js

    r4932 r4948  
    6767    } 
    6868    if (screen_y < 0 || atEnd || screen_y > (win.innerHeight || html.clientHeight || 0) - 50) { 
    69       scroll_y = atEnd ? 1e10 : y; 
     69      scroll_y = atEnd ? 1e6 : y; 
    7070      scroll = true; 
    7171    } 
  • branches/greyhound/mt-static/codemirror/js/undo.js

    r4932 r4948  
    2727// unfortunately, the 'parent' window -- a window that is not in 
    2828// designMode, and on which setTimeout works in every browser. 
    29 function History(container, maxDepth, commitDelay, editor, onChange) { 
     29function History(container, maxDepth, commitDelay, editor) { 
    3030  this.container = container; 
    3131  this.maxDepth = maxDepth; this.commitDelay = commitDelay; 
    3232  this.editor = editor; this.parent = editor.parent; 
    33   this.onChange = onChange; 
    3433  // This line object represents the initial, empty editor. 
    3534  var initial = {text: "", from: null, to: null};