mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-09 14:22:08 -05:00
4 lines
106 KiB
JavaScript
4 lines
106 KiB
JavaScript
|
window.CodeMirror=function(){"use strict";function CodeMirror(place,givenOptions){var options={},defaults=CodeMirror.defaults;for(var opt in defaults)if(defaults.hasOwnProperty(opt))options[opt]=(givenOptions&&givenOptions.hasOwnProperty(opt)?givenOptions:defaults)[opt];var input=elt("textarea",null,null,"position: absolute; padding: 0; width: 1px; height: 1em");input.setAttribute("wrap","off");input.setAttribute("autocorrect","off");input.setAttribute("autocapitalize","off");var inputDiv=elt("div",[input],null,"overflow: hidden; position: relative; width: 3px; height: 0px;");var scrollbarInner=elt("div",null,"CodeMirror-scrollbar-inner");var scrollbar=elt("div",[scrollbarInner],"CodeMirror-scrollbar");var lineDiv=elt("div"),selectionDiv=elt("div",null,null,"position: relative; z-index: -1");var cursor=elt("pre"," ","CodeMirror-cursor"),widthForcer=elt("pre"," ","CodeMirror-cursor","visibility: hidden");var measure=elt("div",null,null,"position: absolute; width: 100%; height: 0px; overflow: hidden; visibility: hidden;");var lineSpace=elt("div",[measure,cursor,widthForcer,selectionDiv,lineDiv],null,"position: relative; z-index: 0");var gutterText=elt("div",null,"CodeMirror-gutter-text"),gutter=elt("div",[gutterText],"CodeMirror-gutter");var mover=elt("div",[gutter,elt("div",[lineSpace],"CodeMirror-lines")],null,"position: relative");var sizer=elt("div",[mover],null,"position: relative");var scroller=elt("div",[sizer],"CodeMirror-scroll");scroller.setAttribute("tabIndex","-1");var wrapper=elt("div",[inputDiv,scrollbar,scroller],"CodeMirror"+(options.lineWrapping?" CodeMirror-wrap":""));if(place.appendChild)place.appendChild(wrapper);else place(wrapper);themeChanged();keyMapChanged();if(ios)input.style.width="0px";if(!webkit)scroller.draggable=true;lineSpace.style.outline="none";if(options.tabindex!=null)input.tabIndex=options.tabindex;if(options.autofocus)focusInput();if(!options.gutter&&!options.lineNumbers)gutter.style.display="none";if(khtml)inputDiv.style.height="1px",inputDiv.style.position="absolute";if(mac_geLion){scrollbar.style.zIndex=-2;scrollbar.style.visibility="hidden"}else if(ie_lt8)scrollbar.style.minWidth="18px";var poll=new Delayed,highlight=new Delayed,blinker;var mode,doc=new BranchChunk([new LeafChunk([new Line("")])]),frontier=0,focused;loadMode();var sel={from:{line:0,ch:0},to:{line:0,ch:0},inverted:false};var shiftSelecting,lastClick,lastDoubleClick,lastScrollTop=0,draggingText,overwrite=false,suppressEdits=false,pasteIncoming=false;var updateInput,userSelChange,changes,textChanged,selectionChanged,gutterDirty,callbacks;var displayOffset=0,showingFrom=0,showingTo=0,lastSizeC=0;var bracketHighlighted;var maxLine=getLine(0),updateMaxLine=false,maxLineChanged=true;var pollingFast=false;var goalColumn=null;operation(function(){setValue(options.value||"");updateInput=false})();var history=new History;connect(scroller,"mousedown",operation(onMouseDown));connect(scroller,"dblclick",operation(onDoubleClick));connect(lineSpace,"selectstart",e_preventDefault);if(!gecko)connect(scroller,"contextmenu",onContextMenu);connect(scroller,"scroll",onScrollMain);connect(scrollbar,"scroll",onScrollBar);connect(scrollbar,"mousedown",function(){if(focused)setTimeout(focusInput,0)});var resizeHandler=connect(window,"resize",function(){if(wrapper.parentNode)updateDisplay(true);else resizeHandler()},true);connect(input,"keyup",operation(onKeyUp));connect(input,"input",fastPoll);connect(input,"keydown",operation(onKeyDown));connect(input,"keypress",operation(onKeyPress));connect(input,"focus",onFocus);connect(input,"blur",onBlur);function drag_(e){if(options.onDragEvent&&options.onDragEvent(instance,addStop(e)))return;e_stop(e)}if(options.dragDrop){connect(scroller,"dragstart",onDragStart);connect(scroller,"dragenter",drag_);connect(scroller,"dragover",drag_);connect(scroller,"drop",operation(onDrop))}connect(scroller,"paste",function(){focusInput();fastPoll()});connect(input,"paste",function(){pasteIncoming=true;fastPoll()});connect(input,"cut",operation(function(){if(!options.readOnly)replaceSelection("")}));if(khtml)con
|
|||
|
for(var j=0,l2=intact.length;j<l2;++j){var range=intact[j];if(change.to<=range.from&&change.diff)intact2.push({from:range.from+diff,to:range.to+diff,domStart:range.domStart});else if(change.to<=range.from||change.from>=range.to)intact2.push(range);else{if(change.from>range.from)intact2.push({from:range.from,to:change.from,domStart:range.domStart});if(change.to<range.to)intact2.push({from:change.to+diff,to:range.to+diff,domStart:range.domStart+(change.to-range.from)})}}intact=intact2}return intact}function patchDisplay(from,to,intact){function killNode(node){var tmp=node.nextSibling;node.parentNode.removeChild(node);return tmp}if(!intact.length)removeChildren(lineDiv);else{var domPos=0,curNode=lineDiv.firstChild,n;for(var i=0;i<intact.length;++i){var cur=intact[i];while(cur.domStart>domPos){curNode=killNode(curNode);domPos++}for(var j=0,e=cur.to-cur.from;j<e;++j){curNode=curNode.nextSibling;domPos++}}while(curNode)curNode=killNode(curNode)}var nextIntact=intact.shift(),curNode=lineDiv.firstChild,j=from;doc.iter(from,to,function(line){if(nextIntact&&nextIntact.to==j)nextIntact=intact.shift();if(!nextIntact||nextIntact.from>j){if(line.hidden)var lineElement=elt("pre");else{var lineElement=lineContent(line);if(line.className)lineElement.className=line.className;if(line.bgClassName){var pre=elt("pre"," ",line.bgClassName,"position: absolute; left: 0; right: 0; top: 0; bottom: 0; z-index: -2");lineElement=elt("div",[pre,lineElement],null,"position: relative")}}lineDiv.insertBefore(lineElement,curNode)}else{curNode=curNode.nextSibling}++j})}function updateGutter(){if(!options.gutter&&!options.lineNumbers)return;var hText=mover.offsetHeight,hEditor=scroller.clientHeight;gutter.style.height=(hText-hEditor<2?hEditor:hText)+"px";var fragment=document.createDocumentFragment(),i=showingFrom,normalNode;doc.iter(showingFrom,Math.max(showingTo,showingFrom+1),function(line){if(line.hidden){fragment.appendChild(elt("pre"))}else{var marker=line.gutterMarker;var text=options.lineNumbers?options.lineNumberFormatter(i+options.firstLineNumber):null;if(marker&&marker.text)text=marker.text.replace("%N%",text!=null?text:"");else if(text==null)text=" ";var markerElement=fragment.appendChild(elt("pre",null,marker&&marker.style));markerElement.innerHTML=text;for(var j=1;j<line.height;++j){markerElement.appendChild(elt("br"));markerElement.appendChild(document.createTextNode(" "))}if(!marker)normalNode=i}++i});gutter.style.display="none";removeChildrenAndAdd(gutterText,fragment);if(normalNode!=null&&options.lineNumbers){var node=gutterText.childNodes[normalNode-showingFrom];var minwidth=String(doc.size).length,val=eltText(node.firstChild),pad="";while(val.length+pad.length<minwidth)pad+=" ";if(pad)node.insertBefore(document.createTextNode(pad),node.firstChild)}gutter.style.display="";var resized=Math.abs((parseInt(lineSpace.style.marginLeft)||0)-gutter.offsetWidth)>2;lineSpace.style.marginLeft=gutter.offsetWidth+"px";gutterDirty=false;return resized}function updateSelection(){var collapsed=posEq(sel.from,sel.to);var fromPos=localCoords(sel.from,true);var toPos=collapsed?fromPos:localCoords(sel.to,true);var headPos=sel.inverted?fromPos:toPos,th=textHeight();var wrapOff=eltOffset(wrapper),lineOff=eltOffset(lineDiv);inputDiv.style.top=Math.max(0,Math.min(scroller.offsetHeight,headPos.y+lineOff.top-wrapOff.top))+"px";inputDiv.style.left=Math.max(0,Math.min(scroller.offsetWidth,headPos.x+lineOff.left-wrapOff.left))+"px";if(collapsed||options.showCursorWhenSelecting){cursor.style.top=headPos.y+"px";cursor.style.left=(options.lineWrapping?Math.min(headPos.x,lineSpace.offsetWidth):headPos.x)+"px";cursor.style.display=""}else{cursor.style.display="none"}if(!collapsed){var sameLine=fromPos.y==toPos.y,fragment=document.createDocumentFragment();var clientWidth=lineSpace.clientWidth||lineSpace.offsetWidth;var clientHeight=lineSpace.clientHeight||lineSpace.offsetHeight;var add=function(left,top,right,height){var rstyle=quirksMode?"width: "+(!right?clientWidth:clientWidth-right-left)+"px":"right: "+(right-1)+"px";fragment.appendChild(elt("div",null,"CodeMirror
|
|||
|
if(found===false){if(stop)stop();return true}if(found!=null&&handle(found))return true;if(map.nofallthrough){if(stop)stop();return true}var fallthrough=map.fallthrough;if(fallthrough==null)return false;if(Object.prototype.toString.call(fallthrough)!="[object Array]")return lookup(fallthrough);for(var i=0,e=fallthrough.length;i<e;++i){if(lookup(fallthrough[i]))return true}return false}if(extraMap&&lookup(extraMap))return true;return lookup(map)}function isModifierKey(event){var name=keyNames[e_prop(event,"keyCode")];return name=="Ctrl"||name=="Alt"||name=="Shift"||name=="Mod"}CodeMirror.isModifierKey=isModifierKey;CodeMirror.fromTextArea=function(textarea,options){if(!options)options={};options.value=textarea.value;if(!options.tabindex&&textarea.tabindex)options.tabindex=textarea.tabindex;if(options.autofocus==null){var hasFocus=document.body;try{hasFocus=document.activeElement}catch(e){}options.autofocus=hasFocus==textarea||textarea.getAttribute("autofocus")!=null&&hasFocus==document.body}function save(){textarea.value=instance.getValue()}if(textarea.form){var rmSubmit=connect(textarea.form,"submit",save,true);var form=textarea.form,realSubmit=form.submit;textarea.form.submit=function wrappedSubmit(){save();form.submit=realSubmit;form.submit();form.submit=wrappedSubmit}}textarea.style.display="none";var instance=CodeMirror(function(node){textarea.parentNode.insertBefore(node,textarea.nextSibling)},options);instance.save=save;instance.getTextArea=function(){return textarea};instance.toTextArea=function(){save();textarea.parentNode.removeChild(instance.getWrapperElement());textarea.style.display="";if(textarea.form){rmSubmit();if(typeof textarea.form.submit=="function")textarea.form.submit=realSubmit}};return instance};var gecko=/gecko\/\d/i.test(navigator.userAgent);var ie=/MSIE \d/.test(navigator.userAgent);var ie_lt8=/MSIE [1-7]\b/.test(navigator.userAgent);var ie_lt9=/MSIE [1-8]\b/.test(navigator.userAgent);var quirksMode=ie&&document.documentMode==5;var webkit=/WebKit\//.test(navigator.userAgent);var qtwebkit=webkit&&/Qt\/\d+\.\d+/.test(navigator.userAgent);var chrome=/Chrome\//.test(navigator.userAgent);var opera=/Opera\//.test(navigator.userAgent);var safari=/Apple Computer/.test(navigator.vendor);var khtml=/KHTML\//.test(navigator.userAgent);var mac_geLion=/Mac OS X 10\D([7-9]|\d\d)\D/.test(navigator.userAgent);var opera_version=opera&&navigator.userAgent.match(/Version\/(\d*\.\d*)/);if(opera_version)opera_version=Number(opera_version[1]);var flipCtrlCmd=mac&&(qtwebkit||opera&&(opera_version==null||opera_version<12.11));function copyState(mode,state){if(state===true)return state;if(mode.copyState)return mode.copyState(state);var nstate={};for(var n in state){var val=state[n];if(val instanceof Array)val=val.concat([]);nstate[n]=val}return nstate}CodeMirror.copyState=copyState;function startState(mode,a1,a2){return mode.startState?mode.startState(a1,a2):true}CodeMirror.startState=startState;CodeMirror.innerMode=function(mode,state){while(mode.innerMode){var info=mode.innerMode(state);state=info.state;mode=info.mode}return info||{mode:mode,state:state}};function StringStream(string,tabSize){this.pos=this.start=0;this.string=string;this.tabSize=tabSize||8}StringStream.prototype={eol:function(){return this.pos>=this.string.length},sol:function(){return this.pos==0},peek:function(){return this.string.charAt(this.pos)||undefined},next:function(){if(this.pos<this.string.length)return this.string.charAt(this.pos++)},eat:function(match){var ch=this.string.charAt(this.pos);if(typeof match=="string")var ok=ch==match;else var ok=ch&&(match.test?match.test(ch):match(ch));if(ok){++this.pos;return ch}},eatWhile:function(match){var start=this.pos;while(this.eat(match)){}return this.pos>start},eatSpace:function(){var start=this.pos;while(/[\s\u00a0]/.test(this.string.charAt(this.pos)))++this.pos;return this.pos>start},skipToEnd:function(){this.pos=this.string.length},skipTo:function(ch){var found=this.string.indexOf(ch,this.pos);if(found>-1){this.pos=found;return true}},backUp:function(n){this.pos-=n},column:function(){return count
|
|||
|
if(type=="catch")return cont(pushlex("form"),pushcontext,expect("("),funarg,expect(")"),statement,poplex,popcontext);return pass(pushlex("stat"),expression,expect(";"),poplex)}function expression(type){if(atomicTypes.hasOwnProperty(type))return cont(maybeoperator);if(type=="function")return cont(functiondef);if(type=="keyword c")return cont(maybeexpression);if(type=="(")return cont(pushlex(")"),maybeexpression,expect(")"),poplex,maybeoperator);if(type=="operator")return cont(expression);if(type=="[")return cont(pushlex("]"),commasep(expression,"]"),poplex,maybeoperator);if(type=="{")return cont(pushlex("}"),commasep(objprop,"}"),poplex,maybeoperator);return cont()}function maybeexpression(type){if(type.match(/[;\}\)\],]/))return pass();return pass(expression)}function maybeoperator(type,value){if(type=="operator"&&/\+\+|--/.test(value))return cont(maybeoperator);if(type=="operator"&&value=="?")return cont(expression,expect(":"),expression);if(type==";")return;if(type=="(")return cont(pushlex(")"),commasep(expression,")"),poplex,maybeoperator);if(type==".")return cont(property,maybeoperator);if(type=="[")return cont(pushlex("]"),expression,expect("]"),poplex,maybeoperator)}function maybelabel(type){if(type==":")return cont(poplex,statement);return pass(maybeoperator,expect(";"),poplex)}function property(type){if(type=="variable"){cx.marked="property";return cont()}}function objprop(type){if(type=="variable")cx.marked="property";if(atomicTypes.hasOwnProperty(type))return cont(expect(":"),expression)}function commasep(what,end){function proceed(type){if(type==",")return cont(what,proceed);if(type==end)return cont();return cont(expect(end))}return function commaSeparated(type){if(type==end)return cont();else return pass(what,proceed)}}function block(type){if(type=="}")return cont();return pass(statement,block)}function maybetype(type){if(type==":")return cont(typedef);return pass()}function typedef(type){if(type=="variable"){cx.marked="variable-3";return cont()}return pass()}function vardef1(type,value){if(type=="variable"){register(value);return isTS?cont(maybetype,vardef2):cont(vardef2)}return pass()}function vardef2(type,value){if(value=="=")return cont(expression,vardef2);if(type==",")return cont(vardef1)}function forspec1(type){if(type=="var")return cont(vardef1,expect(";"),forspec2);if(type==";")return cont(forspec2);if(type=="variable")return cont(formaybein);return cont(forspec2)}function formaybein(type,value){if(value=="in")return cont(expression);return cont(maybeoperator,forspec2)}function forspec2(type,value){if(type==";")return cont(forspec3);if(value=="in")return cont(expression);return cont(expression,expect(";"),forspec3)}function forspec3(type){if(type!=")")cont(expression)}function functiondef(type,value){if(type=="variable"){register(value);return cont(functiondef)}if(type=="(")return cont(pushlex(")"),pushcontext,commasep(funarg,")"),poplex,statement,popcontext)}function funarg(type,value){if(type=="variable"){register(value);return isTS?cont(maybetype):cont()}}return{startState:function(basecolumn){return{tokenize:jsTokenBase,lastType:null,cc:[],lexical:new JSLexical((basecolumn||0)-indentUnit,0,"block",false),localVars:parserConfig.localVars,globalVars:parserConfig.globalVars,context:parserConfig.localVars&&{vars:parserConfig.localVars},indented:0}},token:function(stream,state){if(stream.sol()){if(!state.lexical.hasOwnProperty("align"))state.lexical.align=false;state.indented=stream.indentation()}if(stream.eatSpace())return null;var style=state.tokenize(stream,state);if(type=="comment")return style;state.lastType=type;return parseJS(state,style,type,content,stream)},indent:function(state,textAfter){if(state.tokenize==jsTokenComment)return CodeMirror.Pass;if(state.tokenize!=jsTokenBase)return 0;var firstChar=textAfter&&textAfter.charAt(0),lexical=state.lexical;if(lexical.type=="stat"&&firstChar=="}")lexical=lexical.prev;var type=lexical.type,closing=firstChar==type;if(type=="vardef")return lexical.indented+(state.lastType=="operator"||state.lastType==","?4:0);else if(type=="form"&&firstChar=="{")return
|