From c9814075fae11ffa9439a1fd32da4b0560e13587 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Tue, 13 May 2008 22:52:20 +0000 Subject: [PATCH] Added timed autosave. --- NEWS | 3 +++ static/js/Wiki.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/NEWS b/NEWS index 0120d9c..c4c4aa3 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,6 @@ +1.3.25: May 13, 2008 + * Added timed autosave. + 1.3.24: May 12, 2008 * Created different sizes of toolbar buttons. * Made the toolbar auto-resize based on browser window size. diff --git a/static/js/Wiki.js b/static/js/Wiki.js index 79359c2..0c2f854 100644 --- a/static/js/Wiki.js +++ b/static/js/Wiki.js @@ -22,6 +22,7 @@ function Wiki( invoker ) { this.font_size = null; this.small_toolbar = false; this.large_toolbar_bottom = 0; + this.autosaver = Autosaver( this ); var total_notes_count_node = getElement( "total_notes_count" ); if ( total_notes_count_node ) @@ -797,6 +798,8 @@ Wiki.prototype.editor_state_changed = function ( editor, link_clicked ) { if ( !link_clicked ) this.display_link_pulldown( editor ); + + signal( this, "note_state_changed", editor ); } Wiki.prototype.editor_title_changed = function ( editor, old_title, new_title ) { @@ -3256,3 +3259,32 @@ Recent_notes.prototype.update_link = function ( editor ) { replaceChildNodes( link, editor.title || "untitled note" ); insertSiblingNodesAfter( "recent_notes_top", item ); } + +function Autosaver( wiki ) { + this.wiki = wiki; + this.last_state_change_time = null; + this.timer = null; + var INTERVAL_MILLISECONDS = 10000; // 10 seconds + + function save_if_idle() { + // if the note state has changed in the last few seconds (e.g. due to typing), don't save, + // but do reschedule a new timer + if ( this.last_state_change_time + INTERVAL_MILLISECONDS > ( new Date() ).getTime() ) { + if ( this.timer ) clearTimeout( this.timer ); + this.timer = setTimeout( save_if_idle, INTERVAL_MILLISECONDS ); + return; + } + + this.wiki.save_editor(); + } + + // whenever the focused editor's state changes, record the current time, cancel any current + // timer, and schedule a timer to save the editor in several seconds from now + var self = this; + connect( wiki, "note_state_changed", function ( editor ) { + self.last_state_change_time = ( new Date() ).getTime(); + + if ( self.timer ) clearTimeout( self.timer ); + self.timer = setTimeout( save_if_idle, INTERVAL_MILLISECONDS ); + } ); +}