From d2cb97ab0a21e6d4812a537e3607e700f0cc2c9d Mon Sep 17 00:00:00 2001 From: Matt Pass Date: Mon, 22 Jul 2013 09:05:06 +0100 Subject: [PATCH] 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 --- lib/ice-coder.js | 45 +++++++++++++++++++++++++++++++++++++++----- lib/ice-coder.min.js | 6 +++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/ice-coder.js b/lib/ice-coder.js index 238a29c..3f2e945 100644 --- a/lib/ice-coder.js +++ b/lib/ice-coder.js @@ -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;i0) { 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-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(); } } diff --git a/lib/ice-coder.min.js b/lib/ice-coder.min.js index d9c80fa..7842a9a 100644 --- a/lib/ice-coder.min.js +++ b/lib/ice-coder.min.js @@ -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