Comments added and a couple of fixes

Condensed code on checking if find or replace are the active elements
Fix to remainder string used by function snippet, needed 1 char more
This commit is contained in:
Matt Pass
2013-07-22 09:05:06 +01:00
parent 98514ac46f
commit d2cb97ab0a
2 changed files with 43 additions and 8 deletions

View File

@@ -2015,7 +2015,9 @@ var ICEcoder = {
top.ICEcoder.draggingTab = tab;
top.ICEcoder.diffStartX = top.ICEcoder.mouseX;
top.ICEcoder.tabDragMouseXStart = (top.ICEcoder.mouseX - (parseInt(top.ICEcoder.files.style.width,10)+53+18)) % 150;
// Put tab we're dragging over others
top.get('tab'+tab).style.zIndex = 2;
// Set classes for other tabs (tabSlide) and the one we're dragging (tabDrag)
for (var i=1; i<=top.ICEcoder.openFiles.length; i++) {
top.get('tab'+i).className = i!==tab
? "tab tabSlide"
@@ -2029,6 +2031,7 @@ var ICEcoder = {
lastTabWidth = parseInt(top.get('tab'+top.ICEcoder.openFiles.length).style.width,10)+18;
// Set the left position but stay within left side (53) and new tab
top.ICEcoder.thisLeft = thisLeft = top.ICEcoder.tabDragMouseX >= 53
? top.ICEcoder.tabDragMouseX <= parseInt(top.get('newTab').style.left,10) - lastTabWidth
? top.ICEcoder.tabDragMouseX : (parseInt(top.get('newTab').style.left,10) - lastTabWidth) : 53;
@@ -2036,6 +2039,8 @@ var ICEcoder = {
top.get('tab'+top.ICEcoder.draggingTab).style.left = thisLeft + "px";
top.ICEcoder.dragTabNo = dragTabNo = top.ICEcoder.draggingTab;
// Set the opacities of tabs then positions of tabs we're not dragging
for (var i=1; i<=top.ICEcoder.openFiles.length; i++) {
top.get('tab'+i).style.opacity = i == top.ICEcoder.draggingTab ? 1 : 0.5;
tabWidth = top.ICEcoder.tabLeftPos[i] ? top.ICEcoder.tabLeftPos[i] - top.ICEcoder.tabLeftPos[i-1] : tabWidth;
@@ -2057,7 +2062,9 @@ var ICEcoder = {
tabDragEnd: function() {
var swapWith, tempArray;
// Set the tab widths
top.ICEcoder.setTabWidths();
// Determin what tabs we've swapped and reset classname, opacity & z-index for all
for (var i=1; i<=top.ICEcoder.openFiles.length; i++) {
if (top.ICEcoder.thisLeft >= top.ICEcoder.tabLeftPos[i-1]) {
swapWith = top.ICEcoder.thisLeft == top.ICEcoder.tabLeftPos[0] ? 1 : top.ICEcoder.dragTabNo > i ? i+1 : i;
@@ -2073,12 +2080,15 @@ var ICEcoder = {
}
}
if (top.ICEcoder.thisLeft && top.ICEcoder.thisLeft!==false) {
// Make a number ascending array
tempArray = [];
for (var i=1;i<=top.ICEcoder.openFiles.length;i++) {
tempArray.push(i);
}
// Then swap our tab numbers
tempArray.splice(top.ICEcoder.dragTabNo-1,1);
tempArray.splice(swapWith-1,0,top.ICEcoder.dragTabNo);
// Now we have an order to sort against
ICEcoder.sortTabs(tempArray);
}
top.ICEcoder.setTabWidths();
@@ -2090,26 +2100,32 @@ var ICEcoder = {
sortTabs: function(newOrder) {
var a, b, changedContent = [], openFiles = [], openFileMDTs = [], cMInstances = [], selectedTabWillBe;
// Setup an array of our actual arrays and the blank ones
a = [ICEcoder.changedContent, ICEcoder.openFiles, ICEcoder.openFileMDTs, ICEcoder.cMInstances];
b = [changedContent, openFiles, openFileMDTs, cMInstances];
// Push the new order values into array b then set into array a
for (var i=0;i<a.length;i++) {
for (var j=0;j<a[i].length;j++) {
b[i].push(a[i][newOrder[j]-1]);
}
a[i] = b[i];
}
// Begin swapping tab id's around to an ascending order and work out new selectedTab
for (var i=0;i<newOrder.length;i++) {
top.get('tab'+newOrder[i]).id = "tab" + (i+1) + ".temp";
if (top.ICEcoder.selectedTab == newOrder[i]) {
selectedTabWillBe = (i+1);
}
}
// Now remove the .temp part from all tabs to get new ascending order
for (var i=0;i<newOrder.length;i++) {
top.get('tab'+(i+1)+'.temp').id = "tab"+(i+1);
}
// Set the classname for sliding
if (top.get('tab'+selectedTabWillBe)) {
top.get('tab'+selectedTabWillBe).className = "tab tabSlide";
}
// Finally, set the array values, tab widths and switch tab
ICEcoder.changedContent = a[0];
ICEcoder.openFiles = a[1];
ICEcoder.openFileMDTs = a[2];
@@ -2126,11 +2142,13 @@ var ICEcoder = {
currentArray = [];
currentArrayFull = [];
alphaArray = [];
// Get filenames, full paths and set classname for sliding
for (var i=0;i<top.ICEcoder.openFiles.length;i++) {
currentArray.push(top.ICEcoder.openFiles[i].slice(top.ICEcoder.openFiles[i].lastIndexOf('/')+1));
currentArrayFull.push(top.ICEcoder.openFiles[i]);
top.get('tab'+(i+1)).className = "tab tabSlide";
}
// Get our next value, which is the next filename alpha lowest value and full path
while (currentArray.length>0) {
nextValue = currentArray[0];
nextValueFull = currentArrayFull[0];
@@ -2142,10 +2160,12 @@ var ICEcoder = {
nextPos = i;
}
}
// When we've got it, push into alphaArray and splice out of arrays
alphaArray.push((top.ICEcoder.openFiles.indexOf(nextValueFull)+1));
currentArray.splice(nextPos,1);
currentArrayFull.splice(nextPos,1);
}
// Once done, sort our tabs into new order
top.ICEcoder.sortTabs(alphaArray);
}
},
@@ -2267,19 +2287,19 @@ var ICEcoder = {
top.ICEcoder.openPrompt();
return false;
// CTRL+Space (Show snippet)
// CTRL+Space (Add snippet)
} else if(key==32 && evt.ctrlKey && area=="content") {
top.ICEcoder.addSnippet();
return false;
// Space outisde of editor (Focus on editor)
} else if(key==32 && area!="content" && document.activeElement.id!="find" && document.activeElement.id!="replace") {
} else if(key==32 && area!="content" && ["find","replace"].indexOf(document.activeElement.id)==-1) {
if (top.ICEcoder.getcMInstance()) {
top.ICEcoder.getcMInstance().focus();
}
return false;
// CTRL+J (Jump to definition)
// CTRL+J (Jump to definition/back again)
} else if(key==74 && evt.ctrlKey && area=="content") {
top.ICEcoder.jumpToDefinition();
return false;
@@ -2308,18 +2328,22 @@ var ICEcoder = {
key = evt.keyCode ? evt.keyCode : evt.which ? evt.which : evt.charCode;
},
// Add snippet panel below line
// Add snippet code completion
addSnippet: function() {
var cM, lineNo, whiteSpace, content;
// Get line content after trimming whitespace
cM = ICEcoder.getcMInstance();
lineNo = cM.getCursor().line;
whiteSpace = cM.getLine(lineNo).length - cM.getLine(lineNo).replace(/^\s\s*/, '').length;
content = cM.getLine(lineNo).slice(whiteSpace);
// function snippet
if (content.slice(0,8)=="function") {
top.ICEcoder.doSnippet('function','function VAR() {\nINDENT\tCURSOR\nINDENT}');
// if snippet
} else if (content.slice(0,2)=="if") {
top.ICEcoder.doSnippet('if','if (CURSOR) {\nINDENT\t\nINDENT}');
// for snippet
} else if (content.slice(0,3)=="for") {
top.ICEcoder.doSnippet('for','for (var i=0; i<CURSOR; i++) {\nINDENT\t\nINDENT}');
}
@@ -2329,19 +2353,28 @@ var ICEcoder = {
doSnippet: function(tgtString,replaceString) {
var cM, lineNo, lineContents, remainder, strPos, replacedLine, whiteSpace, curPos, sPos, lineNoCount;
// Get line contents
cM = top.ICEcoder.getcMInstance();
lineNo = cM.getCursor().line;
lineContents = cM.getLine(lineNo);
// Find our target string
if (lineContents.indexOf(tgtString)>-1) {
// Get text on the line from our target to the end
remainder = cM.getLine(lineNo);
strPos = remainder.indexOf(tgtString);
remainder = remainder.slice(remainder.indexOf(tgtString)+tgtString.length);
remainder = remainder.slice(remainder.indexOf(tgtString)+tgtString.length+1);
// Replace the function name if any
replaceString = replaceString.replace(/VAR/g,remainder);
// Get replaced string from start to our strPos
replacedLine = cM.getLine(lineNo).slice(0,strPos);
// Trim whitespace from start
whiteSpace = cM.getLine(lineNo).length - cM.getLine(lineNo).replace(/^\s\s*/, '').length;
whiteSpace = cM.getLine(lineNo).slice(0,whiteSpace);
// Replace indent with whatever whitespace we have
replaceString = replaceString.replace(/INDENT/g,whiteSpace);
replacedLine += replaceString;
// Get cursor position
curPos = replacedLine.indexOf("CURSOR");
sPos = 0;
lineNoCount = lineNo;
@@ -2351,8 +2384,10 @@ var ICEcoder = {
lineNoCount = lineNoCount+1;
}
}
// Clear the cursor string and set the cursor there
cM.setLine(lineNo, replacedLine.replace("CURSOR",""));
cM.setCursor(lineNoCount,curPos);
// Finally, focus on the editor
cM.focus();
}
}

View File

@@ -102,6 +102,6 @@ if(b.altKey){if(b.ctrlKey&&"content"==a){if(68==c)return top.ICEcoder.tagWrapper
!1;if(51==c)return top.ICEcoder.tagWrapper("h3"),!1;if(52==c)return top.ICEcoder.tagWrapper("h4"),!1;if(53==c)return top.ICEcoder.tagWrapper("h5"),!1;if(54==c)return top.ICEcoder.tagWrapper("h6"),!1;if(13==c)return top.ICEcoder.addLineBreakAtEnd(),!1}return c}return 70==c&&b.ctrlKey?(top.get("find").focus(),!1):71==c&&b.ctrlKey?(top.get("goToLineNo").focus(),!1):73==c&&b.ctrlKey&&"content"==a?(c=top.ICEcoder.caretLocType.toLowerCase()+" ","CSS"==top.ICEcoder.caretLocType||"PHP"==top.ICEcoder.caretLocType?
window.open("http://voke.fm/"+c+top.ICEcoder.getcMInstance().getSelection()):("Content"==top.ICEcoder.caretLocType&&(c=""),window.open("http://www.google.com/#output=search&q="+c+top.ICEcoder.getcMInstance().getSelection())),!1):39==c&&b.ctrlKey&&"content"!=a?(top.ICEcoder.nextTab(),!1):37==c&&b.ctrlKey&&"content"!=a?(top.ICEcoder.previousTab(),!1):38==c&&b.ctrlKey&&"content"==a?(top.ICEcoder.moveLine("up"),!1):40==c&&b.ctrlKey&&"content"==a?(top.ICEcoder.moveLine("down"),!1):107==c&&b.ctrlKey?("content"==
a?top.ICEcoder.duplicateLines():top.ICEcoder.newTab(),!1):109==c&&b.ctrlKey?("content"==a?top.ICEcoder.removeLines():top.ICEcoder.closeTab(top.ICEcoder.selectedTab),!1):83==c&&b.ctrlKey?(b.shiftKey?top.ICEcoder.saveFile("saveAs"):top.ICEcoder.saveFile(),!1):13==c&&b.ctrlKey&&"/[NEW]"!=top.ICEcoder.openFiles[top.ICEcoder.selectedTab-1]?(window.open(top.ICEcoder.openFiles[top.ICEcoder.selectedTab-1]),!1):79==c&&b.ctrlKey?(top.ICEcoder.openPrompt(),!1):32==c&&b.ctrlKey&&"content"==a?(top.ICEcoder.addSnippet(),
!1):32==c&&"content"!=a&&"find"!=document.activeElement.id&&"replace"!=document.activeElement.id?(top.ICEcoder.getcMInstance()&&top.ICEcoder.getcMInstance().focus(),!1):74==c&&b.ctrlKey&&"content"==a?(top.ICEcoder.jumpToDefinition(),!1):27==c&&"content"==a?(top.ICEcoder.lineCommentToggle(),!1):27==c&&"content"!=a?(top.ICEcoder.cancelAllActions(),!1):c},resetKeys:function(a){},addSnippet:function(){var a,b,c;a=ICEcoder.getcMInstance();b=a.getCursor().line;c=a.getLine(b).length-a.getLine(b).replace(/^\s\s*/,
"").length;a=a.getLine(b).slice(c);"function"==a.slice(0,8)?top.ICEcoder.doSnippet("function","function VAR() {\nINDENT\tCURSOR\nINDENT}"):"if"==a.slice(0,2)?top.ICEcoder.doSnippet("if","if (CURSOR) {\nINDENT\t\nINDENT}"):"for"==a.slice(0,3)&&top.ICEcoder.doSnippet("for","for (var i=0; i<CURSOR; i++) {\nINDENT\t\nINDENT}")},doSnippet:function(a,b){var c,d,e,f,g,l;c=top.ICEcoder.getcMInstance();d=c.getCursor().line;if(-1<c.getLine(d).indexOf(a)){e=c.getLine(d);f=e.indexOf(a);e=e.slice(e.indexOf(a)+
a.length);b=b.replace(/VAR/g,e);e=c.getLine(d).slice(0,f);f=c.getLine(d).length-c.getLine(d).replace(/^\s\s*/,"").length;f=c.getLine(d).slice(0,f);b=b.replace(/INDENT/g,f);e+=b;f=e.indexOf("CURSOR");g=0;l=d;for(i=0;i<e.length;i++)e.indexOf("\n",g)<e.indexOf("CURSOR")&&(g=e.indexOf("\n",g)+1,l+=1);c.setLine(d,e.replace("CURSOR",""));c.setCursor(l,f);c.focus()}}};
!1):32==c&&"content"!=a&&-1==["find","replace"].indexOf(document.activeElement.id)?(top.ICEcoder.getcMInstance()&&top.ICEcoder.getcMInstance().focus(),!1):74==c&&b.ctrlKey&&"content"==a?(top.ICEcoder.jumpToDefinition(),!1):27==c&&"content"==a?(top.ICEcoder.lineCommentToggle(),!1):27==c&&"content"!=a?(top.ICEcoder.cancelAllActions(),!1):c},resetKeys:function(a){},addSnippet:function(){var a,b,c;a=ICEcoder.getcMInstance();b=a.getCursor().line;c=a.getLine(b).length-a.getLine(b).replace(/^\s\s*/,"").length;
a=a.getLine(b).slice(c);"function"==a.slice(0,8)?top.ICEcoder.doSnippet("function","function VAR() {\nINDENT\tCURSOR\nINDENT}"):"if"==a.slice(0,2)?top.ICEcoder.doSnippet("if","if (CURSOR) {\nINDENT\t\nINDENT}"):"for"==a.slice(0,3)&&top.ICEcoder.doSnippet("for","for (var i=0; i<CURSOR; i++) {\nINDENT\t\nINDENT}")},doSnippet:function(a,b){var c,d,e,f,g,l;c=top.ICEcoder.getcMInstance();d=c.getCursor().line;if(-1<c.getLine(d).indexOf(a)){e=c.getLine(d);f=e.indexOf(a);e=e.slice(e.indexOf(a)+a.length+1);
b=b.replace(/VAR/g,e);e=c.getLine(d).slice(0,f);f=c.getLine(d).length-c.getLine(d).replace(/^\s\s*/,"").length;f=c.getLine(d).slice(0,f);b=b.replace(/INDENT/g,f);e+=b;f=e.indexOf("CURSOR");g=0;l=d;for(i=0;i<e.length;i++)e.indexOf("\n",g)<e.indexOf("CURSOR")&&(g=e.indexOf("\n",g)+1,l+=1);c.setLine(d,e.replace("CURSOR",""));c.setCursor(l,f);c.focus()}}};