Commits

David Chambers committed 78e8f88

store documents in localStorage when saved

Comments (0)

Files changed (3)

src/location.coffee

   args unless /^unpack:/.test args[0]
 
 subscribe 'hashchange', (hash, options = {}) ->
-  publish 'textchange', decode hash
+  publish 'textchange', text = decode hash
+  publish 'save', hash, text if options.save
   path = '/' + hash + (Hashify.location.components().query or '')
   if window.history?.pushState
     method = if options.save then 'pushState' else 'replaceState'
 {components} = Hashify.location
 {bitlyLimit, maxHashLength, root} = Hashify.settings
-{$, addEvent, publish, sendRequest, subscribe} = Hashify.utils
+{$, addEvent, parseJSON, publish, sendRequest, subscribe} = Hashify.utils
 
 qrcode  = $('qrcode')
 shorten = $('shorten')
 wrapper = $('wrapper')
 
-lastSavedDocument = undefined
-
 subscribe 'request:error', (data) ->
   message = data.status_txt.toLowerCase().replace(/_/g, ' ')
   wrapper.className = ''
     encodeURIComponent tweetText
   wrapper.insertBefore tweet, shorturl
   wrapper.className = ''
-  publish 'hashchange', lastSavedDocument = components().hash, save: yes
+  publish 'hashchange', components().hash, save: yes
+
+subscribe 'save', (hash, text) ->
+  quote = (text) -> '"' + text + '"'
+  data = [
+    ['date', new Date().toISOString()]
+    ['shorturl', shorturl.href]
+    ['text', text]
+  ]
+  localStorage["document:#{hash}"] =
+    '{' + ("#{quote k}: #{quote v}" for [k, v] in data).join(', ') + '}'
 
 addEvent shorten, 'click', (event) ->
   (event or window.event).preventDefault()
   shorten.style.display = 'none'
 
 subscribe 'hashchange', (hash) ->
-  shorten.style.display =
-    if hash is lastSavedDocument then 'none' else 'block'
+  if document = localStorage["document:#{hash}"]
+    setShortUrl url: parseJSON(document).shorturl
+    shorten.style.display = 'none'
+  else
+    shorten.style.display = 'block'
     s args... for s in subscriptions["post:#{event}"] or []
   stack.pop()
 
-Hashify.utils = {$, addEvent, decode, publish, sendRequest, subscribe}
+Hashify.utils =
+  {$, addEvent, decode, parseJSON, publish, sendRequest, subscribe}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.