Skip to content
Browse files

Add save and load.

  • Loading branch information...
1 parent 347260f commit 7d4427a5d3f99d9fd5261a9d8061b03c775fd548 @bootstraponline bootstraponline committed May 3, 2012
Showing with 56 additions and 0 deletions.
  1. +8 −0 lib/gollum/frontend/app.rb
  2. +48 −0 lib/gollum/frontend/public/gollum/livepreview/index.html
View
8 lib/gollum/frontend/app.rb
@@ -42,6 +42,14 @@ class App < Sinatra::Base
show_page_or_file('Home')
end
+ get '/data/*' do
+ @name = params[:splat].first
+ wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
+ if page = wiki.page(@name)
+ page.raw_data
+ end
+ end
+
get '/edit/*' do
@name = params[:splat].first
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
View
48 lib/gollum/frontend/public/gollum/livepreview/index.html
@@ -91,6 +91,54 @@
editor.setBehavioursEnabled(true);
window.onload = function() {
+ // RegExp from http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
+ $.key = function(key){
+ var value = new RegExp('[\\?&]' + key + '=([^&#]*)').exec(window.location.href);
+ return (!value) ? 0 : value[1] || 0;
+ }
+
+ /* Load markdown from /data/page into the ace editor. */
+ jQuery.ajax({
+ type: "GET",
+ url: "/data/" + $.key("page"),
+ success: function(data) {
+ editorSession.setValue(data);
+ }
+ });
+
+ $.save = function() {
+ // if &create=true then handle create instead of edit.
+ var create = $.key("create");
+
+ if (create) {
+ jQuery.ajax({
+ type: "POST",
+ url: "/create",
+ data: { page: $.key("page"), format: "markdown", content: editorSession.getValue() },
+ success: function() {
+ // on success, load the new page.
+ window.location = window.location.origin + "/" + $.key("page");
+ }
+ });
+ } else {
+ jQuery.ajax({
+ type: "POST",
+ url: "/edit/" + $.key("page"),
+ data: { format: "markdown", content: editorSession.getValue() },
+ success: function() {
+ // on success, load the new page.
+ window.location = window.location.origin + "/" + $.key("page");
+ }
+ });
+ } // end else
+ }
+
+ $('#save').click(function() {
+ console.log("save clicked!");
+ // Save page and navigate to the new page on success.
+ $.save();
+ });
+
// onChange calls applyTimeout which rate limits the calling of makePreviewHtml based on render time.
editor.on('change', applyTimeout);
makePreviewHtml(); // preview default text on load

0 comments on commit 7d4427a

Please sign in to comment.
Something went wrong with that request. Please try again.