Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial import.

  • Loading branch information...
commit a88a5b4eb4eb5426e956b30f5eb168713472596a 0 parents
Michael Bleigh mbleigh authored
1  .gitignore
@@ -0,0 +1 @@
+.DS_Store
1  Guardfile
@@ -0,0 +1 @@
+guard 'coffeescript', :input => 'src', :output => 'build', :bare => true
59 README.md
@@ -0,0 +1,59 @@
+# Javascript Buffered Undo Manager
+
+This library provides a simple undo manager with update buffering
+to avoid a scenario where undos become data-intensive or cumbersome
+for the user in scenarios with fast-changing content.
+
+## Requirements
+
+As of now BufferedUndoManager requires both jQuery and Underscore.
+
+## Usage
+
+The usage of the BufferedUndoManager is straightforward:
+
+```javascript
+var manager = new BufferedUndoManager();
+
+manager.update("first");
+manager.update("second");
+manager.state; // "second"
+manager.undo();
+manager.state; // "first"
+manager.redo();
+manager.state; // "second"
+
+// What if we have frequent updates, such as a keyup?
+manager.update("t");
+manager.update("th");
+manager.update("thi");
+manager.update("thir");
+manager.update("third");
+manager.state; // "third"
+manager.undo();
+manager.state; // "second"
+
+// Pass the force option to trigger an update regardless of
+// speed.
+manager.update("t", {force: true});
+manager.update("th");
+manager.undo();
+manager.state; // "t"
+```
+
+By default the manager will buffer input for one second (the input must
+become idle for one second before another entry will be created).
+
+## Documentation
+
+You can see the [Docco Docs](https://divshot.github.com/buffered_undo_manager/docs/buffered_undo_manager.html) for more in-depth API documentation.
+
+## License
+
+Copyright (c) 2012 Divshot
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
111 build/buffered_undo_manager.js
@@ -0,0 +1,111 @@
+var BufferedUndoManager,
+ __slice = [].slice;
+
+BufferedUndoManager = (function() {
+
+ BufferedUndoManager.name = 'BufferedUndoManager';
+
+ function BufferedUndoManager(options) {
+ this.bindings = {};
+ this.options = _.extend({
+ buffer: 1000,
+ synchronizeOnUpdate: false,
+ comparator: function(a, b) {
+ return a === b;
+ }
+ }, options);
+ this.reset(this.options.state);
+ }
+
+ BufferedUndoManager.prototype.reset = function(state) {
+ delete this.undos;
+ delete this.redos;
+ this.undos = [];
+ this.redos = [];
+ this.bufferReady = true;
+ return this.state = state;
+ };
+
+ BufferedUndoManager.prototype.undo = function() {
+ console.log("Performing undo...");
+ if (!this.canUndo()) {
+ return false;
+ }
+ this.redos.push(this.state);
+ this.state = this.undos.pop();
+ this.trigger('undo', this.state);
+ this.synchronize();
+ return this.undos.length;
+ };
+
+ BufferedUndoManager.prototype.redo = function() {
+ if (!this.canRedo()) {
+ return false;
+ }
+ this.undos.push(this.state);
+ this.state = this.redos.pop();
+ this.trigger('redo', this.state);
+ this.synchronize();
+ return this.redos.length;
+ };
+
+ BufferedUndoManager.prototype.canUndo = function() {
+ return this.undos.length > 0;
+ };
+
+ BufferedUndoManager.prototype.canRedo = function() {
+ return this.redos.length > 0;
+ };
+
+ BufferedUndoManager.prototype.on = function() {
+ var args, _ref;
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+ return (_ref = $(this)).on.apply(_ref, args);
+ };
+
+ BufferedUndoManager.prototype.off = function() {
+ var args, _ref;
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+ return (_ref = $(this)).off.apply(_ref, args);
+ };
+
+ BufferedUndoManager.prototype.update = function(state, options) {
+ var _this = this;
+ if (options == null) {
+ options = {};
+ }
+ if (this.options.comparator(this.state, state) && !options.force) {
+ return false;
+ }
+ this.redos = [];
+ if (options.force || this.bufferReady) {
+ this.undos.push(this.state);
+ this.trigger('push', this.state);
+ this.bufferReady = false;
+ }
+ if (this.bufferTimeout != null) {
+ clearTimeout(this.bufferTimeout);
+ }
+ this.bufferTimeout = setTimeout(function() {
+ return _this.bufferReady = true;
+ }, this.options.buffer);
+ this.state = state;
+ return this.synchronize(this.options.synchronizeOnUpdate != null);
+ };
+
+ BufferedUndoManager.prototype.trigger = function() {
+ var args, _ref;
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+ return (_ref = $(this)).triggerHandler.apply(_ref, args);
+ };
+
+ BufferedUndoManager.prototype.synchronize = function(options) {
+ this.trigger('change', this.state);
+ if ((this.options.synchronize != null) && callThrough) {
+ return this.options.synchronize(this.state);
+ }
+ };
+
+ return BufferedUndoManager;
+
+})();
134 docs/buffered_undo_manager.html
@@ -0,0 +1,134 @@
+<!DOCTYPE html> <html> <head> <title>buffered_undo_manager.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> buffered_undo_manager.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>The Buffered Undo Manager is a simple way to provide an
+undo/redo stack for a Javascript application.</p>
+
+<p><strong>Prerequisites:</strong> Requires that jQuery and Underscore both
+be installed.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">BufferedUndoManager</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <h2>new BufferedUndoManager(options)</h2>
+
+<p>Initializes a new undo manager with the provided options.
+Options may be specified as follows:</p>
+
+<ul>
+<li><strong>buffer:</strong> The amount of time (in ms) to buffer updates before
+allowing another push to the undo stack. Defaults to <code>1000</code>.</li>
+<li><strong>synchronize:</strong> A callback that is called after undos and redos,
+allowing you to update data or displays. Alternatively see the
+<code>on</code> and <code>off</code> methods for binding to events instead.</li>
+<li><strong>synchronizeOnUpdate:</strong> Whether or not to call the <code>synchronize</code>
+method when <code>update</code> is called in addition to <code>undo</code> and <code>redo</code>.</li>
+<li><strong>comparator:</strong> A function that takes two arguments <code>a</code> and <code>b</code>
+and returns <code>true</code> if they are identical and <code>false</code> if they
+are not. Used to avoid pushing duplicate states to the stack.
+Defaults to naive equality.</li>
+</ul> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(options)-&gt;</span>
+ <span class="vi">@bindings = </span><span class="p">{}</span>
+ <span class="vi">@options = </span><span class="nx">_</span><span class="p">.</span><span class="nx">extend</span>
+ <span class="nv">buffer: </span><span class="mi">1000</span>
+ <span class="nv">synchronizeOnUpdate: </span><span class="kc">false</span>
+ <span class="nv">comparator: </span><span class="nf">(a,b)-&gt;</span>
+ <span class="nx">a</span> <span class="o">==</span> <span class="nx">b</span>
+ <span class="p">,</span> <span class="nx">options</span>
+
+ <span class="nx">@reset</span><span class="p">(</span><span class="nx">@options</span><span class="p">.</span><span class="nx">state</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>manager.reset(state)</h2>
+
+<p>Resets the undo manager to a blank slate.
+Useful if you are using an application-wide
+undo manager and have loaded a different editable
+resource, for instance.</p>
+
+<h3>Arguments</h3>
+
+<ul>
+<li><strong>state:</strong> The 'initial' state to which to reset.</li>
+</ul> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">reset: </span><span class="nf">(state)-&gt;</span>
+ <span class="k">delete</span> <span class="nx">@undos</span>
+ <span class="k">delete</span> <span class="nx">@redos</span>
+ <span class="vi">@undos = </span><span class="p">[]</span>
+ <span class="vi">@redos = </span><span class="p">[]</span>
+ <span class="vi">@bufferReady = </span><span class="kc">true</span>
+ <span class="vi">@state = </span><span class="nx">state</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <h2>manager.undo()</h2>
+
+<p>Performs an undo operation. This operation will
+run the <code>synchronize</code> callback if present and will
+also trigger an <code>undo</code> event.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">undo: </span><span class="o">-&gt;</span>
+ <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s2">&quot;Performing undo...&quot;</span>
+ <span class="k">return</span> <span class="kc">false</span> <span class="nx">unless</span> <span class="nx">@canUndo</span><span class="p">()</span>
+ <span class="nx">@redos</span><span class="p">.</span><span class="nx">push</span> <span class="nx">@state</span>
+ <span class="vi">@state = </span><span class="nx">@undos</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span>
+ <span class="nx">@trigger</span> <span class="s1">&#39;undo&#39;</span><span class="p">,</span> <span class="nx">@state</span>
+ <span class="nx">@synchronize</span><span class="p">()</span>
+ <span class="nx">@undos</span><span class="p">.</span><span class="nx">length</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <h2>manager.redo()</h2>
+
+<p>Performs a redo operation. This operation will
+run the <code>synchronize</code> callback if present and will
+also trigger a <code>redo</code> event.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">redo: </span><span class="o">-&gt;</span>
+ <span class="k">return</span> <span class="kc">false</span> <span class="nx">unless</span> <span class="nx">@canRedo</span><span class="p">()</span>
+ <span class="nx">@undos</span><span class="p">.</span><span class="nx">push</span> <span class="nx">@state</span>
+ <span class="vi">@state = </span><span class="nx">@redos</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span>
+ <span class="nx">@trigger</span> <span class="s1">&#39;redo&#39;</span><span class="p">,</span> <span class="nx">@state</span>
+ <span class="nx">@synchronize</span><span class="p">()</span>
+ <span class="nx">@redos</span><span class="p">.</span><span class="nx">length</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <h2>manager.canUndo()</h2>
+
+<p>Returns <code>true</code> if there are any states to undo
+to, `false otherwise.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">canUndo: </span><span class="o">-&gt;</span>
+ <span class="nx">@undos</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <h2>manager.canRedo()</h2>
+
+<p>Returns <code>true</code> if there are any states to redo
+to, `false otherwise.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">canRedo: </span><span class="o">-&gt;</span>
+ <span class="nx">@redos</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <h2>manager.on(event, handler)</h2>
+
+<p>Takes a string of space-separated events and
+calls the provided handler function when the given
+events trigger. Available events are:</p>
+
+<ul>
+<li>undo</li>
+<li>redo</li>
+<li>change</li>
+</ul> </td> <td class="code"> <div class="highlight"><pre> <span class="kc">on</span><span class="o">:</span> <span class="nf">(args...)-&gt;</span>
+ <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="kc">on</span> <span class="nx">args</span><span class="p">...</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <h2>manager.off(event, [handler])</h2>
+
+<p>Takes an event string and optional handler. If handler
+is provided only that specific handler will be unbound.
+Otherwise all handlers will be unbound.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kc">off</span><span class="o">:</span> <span class="nf">(args...)-&gt;</span>
+ <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="kc">off</span> <span class="nx">args</span><span class="p">...</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <h2>manager.update(state, options)</h2>
+
+<p>Update the state of the undo manager.</p>
+
+<h3>Arguments</h3>
+
+<ul>
+<li><strong>state:</strong> The new state. Can be anything.</li>
+</ul>
+
+<h3>Options</h3>
+
+<ul>
+<li><strong>force:</strong> if set to <code>true</code> then this update
+will ignore buffering and immediately trigger
+a new undo state regardless of current buffering.</li>
+</ul> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">update: </span><span class="nf">(state, options = {})-&gt;</span>
+ <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">@options</span><span class="p">.</span><span class="nx">comparator</span><span class="p">(</span><span class="nx">@state</span><span class="p">,</span> <span class="nx">state</span><span class="p">)</span> <span class="o">and</span> <span class="o">!</span><span class="nx">options</span><span class="p">.</span><span class="nx">force</span>
+
+ <span class="vi">@redos = </span><span class="p">[]</span>
+
+ <span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">force</span> <span class="o">or</span> <span class="nx">@bufferReady</span>
+ <span class="nx">@undos</span><span class="p">.</span><span class="nx">push</span> <span class="nx">@state</span>
+ <span class="nx">@trigger</span> <span class="s1">&#39;push&#39;</span><span class="p">,</span> <span class="nx">@state</span>
+ <span class="vi">@bufferReady = </span><span class="kc">false</span>
+
+ <span class="nx">clearTimeout</span> <span class="nx">@bufferTimeout</span> <span class="k">if</span> <span class="nx">@bufferTimeout</span><span class="o">?</span>
+ <span class="vi">@bufferTimeout = </span><span class="nx">setTimeout</span> <span class="o">=&gt;</span>
+ <span class="vi">@bufferReady = </span><span class="kc">true</span>
+ <span class="p">,</span> <span class="nx">@options</span><span class="p">.</span><span class="nx">buffer</span>
+
+ <span class="vi">@state = </span><span class="nx">state</span>
+ <span class="nx">@synchronize</span><span class="p">(</span><span class="nx">@options</span><span class="p">.</span><span class="nx">synchronizeOnUpdate</span><span class="o">?</span><span class="p">)</span>
+
+ <span class="nv">trigger: </span><span class="nf">(args...)-&gt;</span>
+ <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">triggerHandler</span> <span class="nx">args</span><span class="p">...</span>
+
+ <span class="nv">synchronize: </span><span class="nf">(options)-&gt;</span>
+ <span class="nx">@trigger</span> <span class="s1">&#39;change&#39;</span><span class="p">,</span> <span class="nx">@state</span>
+ <span class="nx">@options</span><span class="p">.</span><span class="nx">synchronize</span> <span class="nx">@state</span> <span class="k">if</span> <span class="nx">@options</span><span class="p">.</span><span class="nx">synchronize</span><span class="o">?</span> <span class="o">and</span> <span class="nx">callThrough</span>
+
+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
186 docs/docco.css
@@ -0,0 +1,186 @@
+/*--------------------- Layout and Typography ----------------------------*/
+body {
+ font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
+ font-size: 15px;
+ line-height: 22px;
+ color: #252519;
+ margin: 0; padding: 0;
+}
+a {
+ color: #261a3b;
+}
+ a:visited {
+ color: #261a3b;
+ }
+p {
+ margin: 0 0 15px 0;
+}
+h1, h2, h3, h4, h5, h6 {
+ margin: 0px 0 15px 0;
+}
+ h1 {
+ margin-top: 40px;
+ }
+#container {
+ position: relative;
+}
+#background {
+ position: fixed;
+ top: 0; left: 525px; right: 0; bottom: 0;
+ background: #f5f5ff;
+ border-left: 1px solid #e5e5ee;
+ z-index: -1;
+}
+#jump_to, #jump_page {
+ background: white;
+ -webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777;
+ -webkit-border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px;
+ font: 10px Arial;
+ text-transform: uppercase;
+ cursor: pointer;
+ text-align: right;
+}
+#jump_to, #jump_wrapper {
+ position: fixed;
+ right: 0; top: 0;
+ padding: 5px 10px;
+}
+ #jump_wrapper {
+ padding: 0;
+ display: none;
+ }
+ #jump_to:hover #jump_wrapper {
+ display: block;
+ }
+ #jump_page {
+ padding: 5px 0 3px;
+ margin: 0 0 25px 25px;
+ }
+ #jump_page .source {
+ display: block;
+ padding: 5px 10px;
+ text-decoration: none;
+ border-top: 1px solid #eee;
+ }
+ #jump_page .source:hover {
+ background: #f5f5ff;
+ }
+ #jump_page .source:first-child {
+ }
+table td {
+ border: 0;
+ outline: 0;
+}
+ td.docs, th.docs {
+ max-width: 450px;
+ min-width: 450px;
+ min-height: 5px;
+ padding: 10px 25px 1px 50px;
+ overflow-x: hidden;
+ vertical-align: top;
+ text-align: left;
+ }
+ .docs pre {
+ margin: 15px 0 15px;
+ padding-left: 15px;
+ }
+ .docs p tt, .docs p code {
+ background: #f8f8ff;
+ border: 1px solid #dedede;
+ font-size: 12px;
+ padding: 0 0.2em;
+ }
+ .pilwrap {
+ position: relative;
+ }
+ .pilcrow {
+ font: 12px Arial;
+ text-decoration: none;
+ color: #454545;
+ position: absolute;
+ top: 3px; left: -20px;
+ padding: 1px 2px;
+ opacity: 0;
+ -webkit-transition: opacity 0.2s linear;
+ }
+ td.docs:hover .pilcrow {
+ opacity: 1;
+ }
+ td.code, th.code {
+ padding: 14px 15px 16px 25px;
+ width: 100%;
+ vertical-align: top;
+ background: #f5f5ff;
+ border-left: 1px solid #e5e5ee;
+ }
+ pre, tt, code {
+ font-size: 12px; line-height: 18px;
+ font-family: Monaco, Consolas, "Lucida Console", monospace;
+ margin: 0; padding: 0;
+ }
+
+
+/*---------------------- Syntax Highlighting -----------------------------*/
+td.linenos { background-color: #f0f0f0; padding-right: 10px; }
+span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
+body .hll { background-color: #ffffcc }
+body .c { color: #408080; font-style: italic } /* Comment */
+body .err { border: 1px solid #FF0000 } /* Error */
+body .k { color: #954121 } /* Keyword */
+body .o { color: #666666 } /* Operator */
+body .cm { color: #408080; font-style: italic } /* Comment.Multiline */
+body .cp { color: #BC7A00 } /* Comment.Preproc */
+body .c1 { color: #408080; font-style: italic } /* Comment.Single */
+body .cs { color: #408080; font-style: italic } /* Comment.Special */
+body .gd { color: #A00000 } /* Generic.Deleted */
+body .ge { font-style: italic } /* Generic.Emph */
+body .gr { color: #FF0000 } /* Generic.Error */
+body .gh { color: #000080; font-weight: bold } /* Generic.Heading */
+body .gi { color: #00A000 } /* Generic.Inserted */
+body .go { color: #808080 } /* Generic.Output */
+body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
+body .gs { font-weight: bold } /* Generic.Strong */
+body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
+body .gt { color: #0040D0 } /* Generic.Traceback */
+body .kc { color: #954121 } /* Keyword.Constant */
+body .kd { color: #954121; font-weight: bold } /* Keyword.Declaration */
+body .kn { color: #954121; font-weight: bold } /* Keyword.Namespace */
+body .kp { color: #954121 } /* Keyword.Pseudo */
+body .kr { color: #954121; font-weight: bold } /* Keyword.Reserved */
+body .kt { color: #B00040 } /* Keyword.Type */
+body .m { color: #666666 } /* Literal.Number */
+body .s { color: #219161 } /* Literal.String */
+body .na { color: #7D9029 } /* Name.Attribute */
+body .nb { color: #954121 } /* Name.Builtin */
+body .nc { color: #0000FF; font-weight: bold } /* Name.Class */
+body .no { color: #880000 } /* Name.Constant */
+body .nd { color: #AA22FF } /* Name.Decorator */
+body .ni { color: #999999; font-weight: bold } /* Name.Entity */
+body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
+body .nf { color: #0000FF } /* Name.Function */
+body .nl { color: #A0A000 } /* Name.Label */
+body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
+body .nt { color: #954121; font-weight: bold } /* Name.Tag */
+body .nv { color: #19469D } /* Name.Variable */
+body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
+body .w { color: #bbbbbb } /* Text.Whitespace */
+body .mf { color: #666666 } /* Literal.Number.Float */
+body .mh { color: #666666 } /* Literal.Number.Hex */
+body .mi { color: #666666 } /* Literal.Number.Integer */
+body .mo { color: #666666 } /* Literal.Number.Oct */
+body .sb { color: #219161 } /* Literal.String.Backtick */
+body .sc { color: #219161 } /* Literal.String.Char */
+body .sd { color: #219161; font-style: italic } /* Literal.String.Doc */
+body .s2 { color: #219161 } /* Literal.String.Double */
+body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
+body .sh { color: #219161 } /* Literal.String.Heredoc */
+body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
+body .sx { color: #954121 } /* Literal.String.Other */
+body .sr { color: #BB6688 } /* Literal.String.Regex */
+body .s1 { color: #219161 } /* Literal.String.Single */
+body .ss { color: #19469D } /* Literal.String.Symbol */
+body .bp { color: #954121 } /* Name.Builtin.Pseudo */
+body .vc { color: #19469D } /* Name.Variable.Class */
+body .vg { color: #19469D } /* Name.Variable.Global */
+body .vi { color: #19469D } /* Name.Variable.Instance */
+body .il { color: #666666 } /* Literal.Number.Integer.Long */
148 src/buffered_undo_manager.coffee
@@ -0,0 +1,148 @@
+# The Buffered Undo Manager is a simple way to provide an
+# undo/redo stack for a Javascript application.
+#
+# **Prerequisites:** Requires that jQuery and Underscore both
+# be installed.
+class BufferedUndoManager
+ # ## new BufferedUndoManager(options)
+ #
+ # Initializes a new undo manager with the provided options.
+ # Options may be specified as follows:
+ #
+ # * **buffer:** The amount of time (in ms) to buffer updates before
+ # allowing another push to the undo stack. Defaults to `1000`.
+ # * **synchronize:** A callback that is called after undos and redos,
+ # allowing you to update data or displays. Alternatively see the
+ # `on` and `off` methods for binding to events instead.
+ # * **synchronizeOnUpdate:** Whether or not to call the `synchronize`
+ # method when `update` is called in addition to `undo` and `redo`.
+ # * **comparator:** A function that takes two arguments `a` and `b`
+ # and returns `true` if they are identical and `false` if they
+ # are not. Used to avoid pushing duplicate states to the stack.
+ # Defaults to naive equality.
+ constructor: (options)->
+ @bindings = {}
+ @options = _.extend
+ buffer: 1000
+ synchronizeOnUpdate: false
+ comparator: (a,b)->
+ a == b
+ , options
+
+ @reset(@options.state)
+
+ # ## manager.reset(state)
+ #
+ # Resets the undo manager to a blank slate.
+ # Useful if you are using an application-wide
+ # undo manager and have loaded a different editable
+ # resource, for instance.
+ #
+ # ### Arguments
+ #
+ # * **state:** The 'initial' state to which to reset.
+ reset: (state)->
+ delete @undos
+ delete @redos
+ @undos = []
+ @redos = []
+ @bufferReady = true
+ @state = state
+
+ # ## manager.undo()
+ #
+ # Performs an undo operation. This operation will
+ # run the `synchronize` callback if present and will
+ # also trigger an `undo` event.
+ undo: ->
+ console.log "Performing undo..."
+ return false unless @canUndo()
+ @redos.push @state
+ @state = @undos.pop()
+ @trigger 'undo', @state
+ @synchronize()
+ @undos.length
+
+ # ## manager.redo()
+ #
+ # Performs a redo operation. This operation will
+ # run the `synchronize` callback if present and will
+ # also trigger a `redo` event.
+ redo: ->
+ return false unless @canRedo()
+ @undos.push @state
+ @state = @redos.pop()
+ @trigger 'redo', @state
+ @synchronize()
+ @redos.length
+
+ # ## manager.canUndo()
+ #
+ # Returns `true` if there are any states to undo
+ # to, `false otherwise.
+ canUndo: ->
+ @undos.length > 0
+ # ## manager.canRedo()
+ #
+ # Returns `true` if there are any states to redo
+ # to, `false otherwise.
+ canRedo: ->
+ @redos.length > 0
+
+ # ## manager.on(event, handler)
+ #
+ # Takes a string of space-separated events and
+ # calls the provided handler function when the given
+ # events trigger. Available events are:
+ #
+ # * undo
+ # * redo
+ # * change
+ # * push
+ on: (args...)->
+ $(this).on args...
+ # ## manager.off(event, [handler])
+ #
+ # Takes an event string and optional handler. If handler
+ # is provided only that specific handler will be unbound.
+ # Otherwise all handlers will be unbound.
+ off: (args...)->
+ $(this).off args...
+
+ # ## manager.update(state, options)
+ #
+ # Update the state of the undo manager.
+ #
+ # ### Arguments
+ #
+ # * **state:** The new state. Can be anything.
+ #
+ # ### Options
+ #
+ # * **force:** if set to `true` then this update
+ # will ignore buffering and immediately trigger
+ # a new undo state regardless of current buffering.
+ update: (state, options = {})->
+ return false if @options.comparator(@state, state) and !options.force
+
+ @redos = []
+
+ if options.force or @bufferReady
+ @undos.push @state
+ @trigger 'push', @state
+ @bufferReady = false
+
+ clearTimeout @bufferTimeout if @bufferTimeout?
+ @bufferTimeout = setTimeout =>
+ @bufferReady = true
+ , @options.buffer
+
+ @state = state
+ @synchronize(@options.synchronizeOnUpdate?)
+
+ trigger: (args...)->
+ $(this).triggerHandler args...
+
+ synchronize: (options)->
+ @trigger 'change', @state
+ @options.synchronize @state if @options.synchronize? and callThrough
Please sign in to comment.
Something went wrong with that request. Please try again.