Permalink
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...
1 parent 513bbd4 commit 56a18d331d2446bd8bfb85f5f270080b15978f5b @coreh coreh committed Jan 4, 2012
Showing with 74 additions and 19 deletions.
  1. +2 −1 client/index.html
  2. +8 −1 client/js/CodeEditor.js
  3. +51 −0 client/js/EditorPool.js
  4. +13 −17 client/js/UserInterfaceController.js
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>
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
}
})()
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
+ }
+})()
@@ -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)
}

0 comments on commit 56a18d3

Please sign in to comment.