diff options
Diffstat (limited to 'imports/codemirror/lib/codemirror.js')
-rw-r--r-- | imports/codemirror/lib/codemirror.js | 247 |
1 files changed, 164 insertions, 83 deletions
diff --git a/imports/codemirror/lib/codemirror.js b/imports/codemirror/lib/codemirror.js index 9c6e65e4..5434a8dd 100644 --- a/imports/codemirror/lib/codemirror.js +++ b/imports/codemirror/lib/codemirror.js | |||
@@ -1,4 +1,4 @@ | |||
1 | // CodeMirror version 2.22 | 1 | // CodeMirror version 2.23 |
2 | // | 2 | // |
3 | // All functions that need access to the editor's state live inside | 3 | // All functions that need access to the editor's state live inside |
4 | // the CodeMirror function. Below that, at the bottom of the file, | 4 | // the CodeMirror function. Below that, at the bottom of the file, |
@@ -15,9 +15,8 @@ var CodeMirror = (function() { | |||
15 | if (defaults.hasOwnProperty(opt)) | 15 | if (defaults.hasOwnProperty(opt)) |
16 | options[opt] = (givenOptions && givenOptions.hasOwnProperty(opt) ? givenOptions : defaults)[opt]; | 16 | options[opt] = (givenOptions && givenOptions.hasOwnProperty(opt) ? givenOptions : defaults)[opt]; |
17 | 17 | ||
18 | var targetDocument = options["document"]; | ||
19 | // The element in which the editor lives. | 18 | // The element in which the editor lives. |
20 | var wrapper = targetDocument.createElement("div"); | 19 | var wrapper = document.createElement("div"); |
21 | wrapper.className = "CodeMirror" + (options.lineWrapping ? " CodeMirror-wrap" : ""); | 20 | wrapper.className = "CodeMirror" + (options.lineWrapping ? " CodeMirror-wrap" : ""); |
22 | // This mess creates the base DOM structure for the editor. | 21 | // This mess creates the base DOM structure for the editor. |
23 | wrapper.innerHTML = | 22 | wrapper.innerHTML = |
@@ -48,7 +47,10 @@ var CodeMirror = (function() { | |||
48 | if (!webkit) lineSpace.draggable = true; | 47 | if (!webkit) lineSpace.draggable = true; |
49 | lineSpace.style.outline = "none"; | 48 | lineSpace.style.outline = "none"; |
50 | if (options.tabindex != null) input.tabIndex = options.tabindex; | 49 | if (options.tabindex != null) input.tabIndex = options.tabindex; |
50 | if (options.autofocus) focusInput(); | ||
51 | if (!options.gutter && !options.lineNumbers) gutter.style.display = "none"; | 51 | if (!options.gutter && !options.lineNumbers) gutter.style.display = "none"; |
52 | // Needed to handle Tab key in KHTML | ||
53 | if (khtml) inputDiv.style.height = "1px", inputDiv.style.position = "absolute"; | ||
52 | 54 | ||
53 | // Check for problem with IE innerHTML not working when we have a | 55 | // Check for problem with IE innerHTML not working when we have a |
54 | // P (or similar) parent node. | 56 | // P (or similar) parent node. |
@@ -81,12 +83,13 @@ var CodeMirror = (function() { | |||
81 | gutterDirty, callbacks; | 83 | gutterDirty, callbacks; |
82 | // Current visible range (may be bigger than the view window). | 84 | // Current visible range (may be bigger than the view window). |
83 | var displayOffset = 0, showingFrom = 0, showingTo = 0, lastSizeC = 0; | 85 | var displayOffset = 0, showingFrom = 0, showingTo = 0, lastSizeC = 0; |
84 | // bracketHighlighted is used to remember that a backet has been | 86 | // bracketHighlighted is used to remember that a bracket has been |
85 | // marked. | 87 | // marked. |
86 | var bracketHighlighted; | 88 | var bracketHighlighted; |
87 | // Tracks the maximum line length so that the horizontal scrollbar | 89 | // Tracks the maximum line length so that the horizontal scrollbar |
88 | // can be kept static when scrolling. | 90 | // can be kept static when scrolling. |
89 | var maxLine = "", maxWidth; | 91 | var maxLine = "", maxWidth; |
92 | var tabCache = {}; | ||
90 | 93 | ||
91 | // Initialize the content. | 94 | // Initialize the content. |
92 | operation(function(){setValue(options.value || ""); updateInput = false;})(); | 95 | operation(function(){setValue(options.value || ""); updateInput = false;})(); |
@@ -124,10 +127,16 @@ var CodeMirror = (function() { | |||
124 | if (!options.readOnly) replaceSelection(""); | 127 | if (!options.readOnly) replaceSelection(""); |
125 | })); | 128 | })); |
126 | 129 | ||
130 | // Needed to handle Tab key in KHTML | ||
131 | if (khtml) connect(code, "mouseup", function() { | ||
132 | if (document.activeElement == input) input.blur(); | ||
133 | focusInput(); | ||
134 | }); | ||
135 | |||
127 | // IE throws unspecified error in certain cases, when | 136 | // IE throws unspecified error in certain cases, when |
128 | // trying to access activeElement before onload | 137 | // trying to access activeElement before onload |
129 | var hasFocus; try { hasFocus = (targetDocument.activeElement == input); } catch(e) { } | 138 | var hasFocus; try { hasFocus = (document.activeElement == input); } catch(e) { } |
130 | if (hasFocus) setTimeout(onFocus, 20); | 139 | if (hasFocus || options.autofocus) setTimeout(onFocus, 20); |
131 | else onBlur(); | 140 | else onBlur(); |
132 | 141 | ||
133 | function isLine(l) {return l >= 0 && l < doc.size;} | 142 | function isLine(l) {return l >= 0 && l < doc.size;} |
@@ -178,17 +187,23 @@ var CodeMirror = (function() { | |||
178 | line = clipLine(line == null ? doc.size - 1: line); | 187 | line = clipLine(line == null ? doc.size - 1: line); |
179 | return getStateBefore(line + 1); | 188 | return getStateBefore(line + 1); |
180 | }, | 189 | }, |
181 | cursorCoords: function(start){ | 190 | cursorCoords: function(start, mode) { |
182 | if (start == null) start = sel.inverted; | 191 | if (start == null) start = sel.inverted; |
183 | return pageCoords(start ? sel.from : sel.to); | 192 | return this.charCoords(start ? sel.from : sel.to, mode); |
193 | }, | ||
194 | charCoords: function(pos, mode) { | ||
195 | pos = clipPos(pos); | ||
196 | if (mode == "local") return localCoords(pos, false); | ||
197 | if (mode == "div") return localCoords(pos, true); | ||
198 | return pageCoords(pos); | ||
184 | }, | 199 | }, |
185 | charCoords: function(pos){return pageCoords(clipPos(pos));}, | ||
186 | coordsChar: function(coords) { | 200 | coordsChar: function(coords) { |
187 | var off = eltOffset(lineSpace); | 201 | var off = eltOffset(lineSpace); |
188 | return coordsChar(coords.x - off.left, coords.y - off.top); | 202 | return coordsChar(coords.x - off.left, coords.y - off.top); |
189 | }, | 203 | }, |
190 | markText: operation(markText), | 204 | markText: operation(markText), |
191 | setBookmark: setBookmark, | 205 | setBookmark: setBookmark, |
206 | findMarksAt: findMarksAt, | ||
192 | setMarker: operation(addGutterMarker), | 207 | setMarker: operation(addGutterMarker), |
193 | clearMarker: operation(removeGutterMarker), | 208 | clearMarker: operation(removeGutterMarker), |
194 | setLineClass: operation(setLineClass), | 209 | setLineClass: operation(setLineClass), |
@@ -256,6 +271,7 @@ var CodeMirror = (function() { | |||
256 | replaceRange: operation(replaceRange), | 271 | replaceRange: operation(replaceRange), |
257 | getRange: function(from, to) {return getRange(clipPos(from), clipPos(to));}, | 272 | getRange: function(from, to) {return getRange(clipPos(from), clipPos(to));}, |
258 | 273 | ||
274 | triggerOnKeyDown: operation(onKeyDown), | ||
259 | execCommand: function(cmd) {return commands[cmd](instance);}, | 275 | execCommand: function(cmd) {return commands[cmd](instance);}, |
260 | // Stuff used by commands, probably not much use to outside code. | 276 | // Stuff used by commands, probably not much use to outside code. |
261 | moveH: operation(moveH), | 277 | moveH: operation(moveH), |
@@ -373,7 +389,7 @@ var CodeMirror = (function() { | |||
373 | !posLess(start, sel.from) && !posLess(sel.to, start)) { | 389 | !posLess(start, sel.from) && !posLess(sel.to, start)) { |
374 | // Let the drag handler handle this. | 390 | // Let the drag handler handle this. |
375 | if (webkit) lineSpace.draggable = true; | 391 | if (webkit) lineSpace.draggable = true; |
376 | var up = connect(targetDocument, "mouseup", operation(function(e2) { | 392 | var up = connect(document, "mouseup", operation(function(e2) { |
377 | if (webkit) lineSpace.draggable = false; | 393 | if (webkit) lineSpace.draggable = false; |
378 | draggingText = false; | 394 | draggingText = false; |
379 | up(); | 395 | up(); |
@@ -384,6 +400,8 @@ var CodeMirror = (function() { | |||
384 | } | 400 | } |
385 | }), true); | 401 | }), true); |
386 | draggingText = true; | 402 | draggingText = true; |
403 | // IE's approach to draggable | ||
404 | if (lineSpace.dragDrop) lineSpace.dragDrop(); | ||
387 | return; | 405 | return; |
388 | } | 406 | } |
389 | e_preventDefault(e); | 407 | e_preventDefault(e); |
@@ -402,12 +420,7 @@ var CodeMirror = (function() { | |||
402 | } | 420 | } |
403 | } | 421 | } |
404 | 422 | ||
405 | var move = connect(targetDocument, "mousemove", operation(function(e) { | 423 | function done(e) { |
406 | clearTimeout(going); | ||
407 | e_preventDefault(e); | ||
408 | extend(e); | ||
409 | }), true); | ||
410 | var up = connect(targetDocument, "mouseup", operation(function(e) { | ||
411 | clearTimeout(going); | 424 | clearTimeout(going); |
412 | var cur = posFromMouse(e); | 425 | var cur = posFromMouse(e); |
413 | if (cur) setSelectionUser(start, cur); | 426 | if (cur) setSelectionUser(start, cur); |
@@ -415,7 +428,14 @@ var CodeMirror = (function() { | |||
415 | focusInput(); | 428 | focusInput(); |
416 | updateInput = true; | 429 | updateInput = true; |
417 | move(); up(); | 430 | move(); up(); |
431 | } | ||
432 | var move = connect(document, "mousemove", operation(function(e) { | ||
433 | clearTimeout(going); | ||
434 | e_preventDefault(e); | ||
435 | if (!ie && !e_button(e)) done(e); | ||
436 | else extend(e); | ||
418 | }), true); | 437 | }), true); |
438 | var up = connect(document, "mouseup", operation(done), true); | ||
419 | } | 439 | } |
420 | function onDoubleClick(e) { | 440 | function onDoubleClick(e) { |
421 | for (var n = e_target(e); n != wrapper; n = n.parentNode) | 441 | for (var n = e_target(e); n != wrapper; n = n.parentNode) |
@@ -464,11 +484,14 @@ var CodeMirror = (function() { | |||
464 | } | 484 | } |
465 | function onDragStart(e) { | 485 | function onDragStart(e) { |
466 | var txt = getSelection(); | 486 | var txt = getSelection(); |
467 | // Disabled until further notice. Doesn't work on most browsers, | ||
468 | // and crashes Safari (issue #332). | ||
469 | //htmlEscape(txt); | ||
470 | //e.dataTransfer.setDragImage(escapeElement, 0, 0); | ||
471 | e.dataTransfer.setData("Text", txt); | 487 | e.dataTransfer.setData("Text", txt); |
488 | |||
489 | // Use dummy image instead of default browsers image. | ||
490 | if (gecko || chrome) { | ||
491 | var img = document.createElement('img'); | ||
492 | img.scr = 'data:image/gif;base64,R0lGODdhAgACAIAAAAAAAP///ywAAAAAAgACAAACAoRRADs='; //1x1 image | ||
493 | e.dataTransfer.setDragImage(img, 0, 0); | ||
494 | } | ||
472 | } | 495 | } |
473 | 496 | ||
474 | function doHandleBinding(bound, dropShift) { | 497 | function doHandleBinding(bound, dropShift) { |
@@ -506,13 +529,19 @@ var CodeMirror = (function() { | |||
506 | if (e_prop(e, "ctrlKey")) name = "Ctrl-" + name; | 529 | if (e_prop(e, "ctrlKey")) name = "Ctrl-" + name; |
507 | if (e_prop(e, "metaKey")) name = "Cmd-" + name; | 530 | if (e_prop(e, "metaKey")) name = "Cmd-" + name; |
508 | 531 | ||
509 | if (e_prop(e, "shiftKey")) | 532 | if (e_prop(e, "shiftKey")) { |
510 | handled = lookupKey("Shift-" + name, options.extraKeys, options.keyMap, | 533 | handled = lookupKey("Shift-" + name, options.extraKeys, options.keyMap, |
511 | function(b) {return doHandleBinding(b, true);}); | 534 | function(b) {return doHandleBinding(b, true);}) |
512 | if (!handled) | 535 | || lookupKey(name, options.extraKeys, options.keyMap, function(b) { |
536 | if (typeof b == "string" && /^go[A-Z]/.test(b)) return doHandleBinding(b); | ||
537 | }); | ||
538 | } else { | ||
513 | handled = lookupKey(name, options.extraKeys, options.keyMap, doHandleBinding); | 539 | handled = lookupKey(name, options.extraKeys, options.keyMap, doHandleBinding); |
514 | 540 | } | |
515 | if (handled) e_preventDefault(e); | 541 | if (handled) { |
542 | e_preventDefault(e); | ||
543 | if (ie) { e.oldKeyCode = e.keyCode; e.keyCode = 0; } | ||
544 | } | ||
516 | return handled; | 545 | return handled; |
517 | } | 546 | } |