Permalink
Browse files

break codemirror into its own extension

  • Loading branch information...
1 parent 1c2e9ed commit ccf93b607ae21c4bd5d7f7428541d7ba5347f96d @jweir committed May 11, 2012
Showing with 49 additions and 24 deletions.
  1. +1 −0 demo/{local_revisions.html → codemirror.html}
  2. +10 −0 lib/local-revisions-codemirror.js
  3. +38 −24 lib/local-revisions.js
@@ -5,6 +5,7 @@
<link rel="stylesheet" href="lib/codemirror.css">
<script src="lib/codemirror.js"></script>
<script src="../lib/local-revisions.js"></script>
+ <script src="../lib/local-revisions-codemirror.js"></script>
<style type="text/css">
.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
@@ -0,0 +1,10 @@
+// LocalRevision extension for CodeMirror
+(function(){
+ CodeMirror.defineExtension("localRevision", function(key) {
+ var editor = this,
+ get = function(){ return editor.getValue()},
+ set = function(x){ return editor.setValue(x)};
+
+ return new LocalRevision(key,get,set);
+ });
+}());
View
@@ -1,32 +1,33 @@
+// stores timestamped revisions for anything which is text(or can be text encoded)
+// this was created to create a simple version history for CodeMirror, but can be
+// applied to other editors or objects.
(function() {
- // saves a timestamped version of the document to localStoreage
- // requires that localRevisionsKey be named
- // TODO
- // maxRevisions sets how many revisions will be saved
-
- var storage = window.localStorage;
-
- var addIndex = function(){
- var timestamp = this.key + "-" + new Date().getTime(),
- index = this.index();
-
- index.push(timestamp);
- storage.setItem(this.key, index.join(","));
- return timestamp;
- }
+ // create a new local revision object
+ //
+ // `key` a unique name to identify the revision history
+ //
+ // `getValue` function which returns the current value of the text to track
+ //
+ // `setValue` a function which sets the text value you are tracking. Used to revert
+ // to a previous revision.
function LocalRevision(key, getValue, setValue){
this.getValue = getValue;
this.setValue = setValue;
this.key = key;
}
+ // returns an index of revision keys
+ // each revision is stored as a `key`-`timestamp`
LocalRevision.prototype.index = function(){
- var i = (storage.getItem(this.key) || "").split(",");
- if(i[0].length == 0){i.shift();} // strip out the first empty item
+ var i = (storage.getItem(this.key) || "").split(","); // index is stored as CSV
+ if(i[0].length === 0){i.shift();} // strip out the first empty item
return i;
}
+ // Creates a new revision, if the value has changed from the previous value.
+ //
+ // Returns the indexKey on success, false if there is no change.
LocalRevision.prototype.write = function(){
var lastValue = this.lastRevision(),
newValue = this.getValue(),
@@ -41,17 +42,20 @@
}
}
+ // returns the text of the last revision
LocalRevision.prototype.lastRevision = function(){
var keys = this.index();
return storage.getItem(keys[keys.length -1]);
}
+ // returns the text of the revision
LocalRevision.prototype.read = function(index){
var text = storage.getItem(index);
this.setValue(text);
return index;
}
+ // removes all revisions for this object
LocalRevision.prototype.clear = function(){
var revisions = this.index();
for(var i = 0; i < revisions.length; i++){
@@ -60,12 +64,22 @@
storage.removeItem(this.key);
}
- // TODO break this out
- CodeMirror.defineExtension("localRevision", function(key) {
- var editor = this,
- get = function(){ return editor.getValue()},
- set = function(x){ return editor.setValue(x)};
+ var storage = window.localStorage;
+
+ // private function for creating a new index
+ function addIndex(){
+ var timestamp = this.key + "-" + new Date().getTime(),
+ index = this.index();
+
+ index.push(timestamp);
+ storage.setItem(this.key, index.join(","));
+ return timestamp;
+ }
+
+ window.LocalRevision = LocalRevision;
- return new LocalRevision(key,get,set);
- });
}());
+
+// ## TODO
+// * maxRevisions to limit how many revisions are stored
+// * use `storage` event to allow documents in other windows to gain sync

0 comments on commit ccf93b6

Please sign in to comment.