Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement an editor pool for keeping state when switching between files.

- Editors are now stored on a pool, so that their state (e.g: cursor position, selection, scroll position) is preserved when switching between files. (fixes #35) When you switch back to a previously opened file, its editor is restored from the pool.
- To keep memory consumption low, the pool will store up to 8 editors. The least recently used policy is used to determine which editors should be disposed.
- Code editors are now focused by default when you open or switch to a file.
  • Loading branch information...
commit 56a18d331d2446bd8bfb85f5f270080b15978f5b 1 parent 513bbd4
Marco Aurélio authored
3  client/index.html
View
@@ -27,8 +27,9 @@
<script src="js/external/mode/clike/clike.js"></script>
<script src="js/external/mode/xml/xml.js"></script>
<script src="js/external/mode/htmlembedded/htmlembedded.js"></script>
-
+
<script src="js/ServerConnection.js"></script>
+ <script src="js/EditorPool.js"></script>
<script src="js/UserInterfaceController.js"></script>
<script src="js/nide.js"></script>
<script src="js/CodeEditor.js"></script>
9 client/js/CodeEditor.js
View
@@ -18,7 +18,7 @@
case !!path.match(/\.ejs$/): return 'application/x-ejs'
case !!path.match(/\.jsp$/): return 'application/x-jsp'
case !!path.match(/\.aspx$/): return 'application/x-aspx'
- default: return undefined;
+ default: return 'text/plain';
}
}
@@ -313,6 +313,7 @@
content = editor.getValue()
changed = true
}})
+ codeMirror.focus()
var content = file
var changed = false;
@@ -346,6 +347,12 @@
galaxyBackground.appendChild(editor)
galaxyBackground.className = 'galaxy-background'
+ galaxyBackground.focus = function() {
+ if (codeMirror) {
+ codeMirror.focus()
+ }
+ }
+
return galaxyBackground
}
})()
51 client/js/EditorPool.js
View
@@ -0,0 +1,51 @@
+(function() {
+ var MAX_EDITORS = 8
+
+ var EditorPool = window.EditorPool = function() {
+ this.editors = []
+ }
+
+ EditorPool.prototype.editorForEntry = function(entry, withDiscarted) {
+ var editor;
+ for (var i=0; i < this.editors.length; i++) {
+ if (this.editors[i].path == entry.path &&
+ this.editors[i].type == entry.type) {
+ // Move editor to the first position in array
+ editor = this.editors.splice(i, 1)[0]
+ this.editors.splice(0, 0, editor);
+ return editor.element
+ }
+ }
+
+ editor = {
+ type: entry.type,
+ path: entry.path
+ }
+
+ switch(entry.type) {
+ case "file":
+ editor.element = new CodeEditor(entry)
+ break;
+ case "directory":
+ editor.element = new DirectoryEditor(entry)
+ break;
+ case "documentation":
+ editor.element = new DocumentationViewer(entry)
+ break;
+ case "npm":
+ editor.element = new NPMEditor(entry)
+ break;
+ }
+
+ this.editors.splice(0, 0, editor)
+
+ if (this.editors.length > MAX_EDITORS) {
+ var discarted = this.editors.pop()
+ if (withDiscarted) {
+ withDiscarted(discarted)
+ }
+ }
+
+ return editor.element
+ }
+})()
30 client/js/UserInterfaceController.js
View
@@ -232,10 +232,18 @@ var UserInterfaceController = function() {
document.getElementById('search-results').innerHTML = '';
document.getElementById('search-results').appendChild(ul);
}
+
+ var editorPool = new EditorPool();
var setCurrentEditor = function(editor) {
- $('#content')[0].innerHTML = ''
- $('#content').append(editor)
+ var children = $('#content').children()
+ children.css({ visibility: 'hidden', zIndex: -1 });
+ if ($.inArray(editor, children) >= 0) {
+ $(editor).css({ visibility: 'visible', zIndex: 1 })
+ } else {
+ $('#content').append(editor)
+ }
+ editor.focus()
}
this.displayWelcomeScreen = function() {
@@ -279,21 +287,9 @@ var UserInterfaceController = function() {
currentFile = entry
$(htmlElement || htmlElementByPathTable[currentFile.path]).addClass('selected')
- var editor;
- switch(entry.type) {
- case "file":
- editor = new CodeEditor(entry)
- break;
- case "directory":
- editor = new DirectoryEditor(entry)
- break;
- case "documentation":
- editor = new DocumentationViewer(entry)
- break;
- case "npm":
- editor = new NPMEditor(entry)
- break;
- }
+ var editor = editorPool.editorForEntry(entry, function(discarted){
+ $(discarted).remove()
+ })
setCurrentEditor(editor)
}
Please sign in to comment.
Something went wrong with that request. Please try again.