diff --git a/lib/gollum/frontend/app.rb b/lib/gollum/frontend/app.rb index 0d962e30..d380e87d 100644 --- a/lib/gollum/frontend/app.rb +++ b/lib/gollum/frontend/app.rb @@ -42,6 +42,14 @@ module Precious 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) diff --git a/lib/gollum/frontend/public/gollum/livepreview/index.html b/lib/gollum/frontend/public/gollum/livepreview/index.html index 5c761985..e6a3dfc5 100644 --- a/lib/gollum/frontend/public/gollum/livepreview/index.html +++ b/lib/gollum/frontend/public/gollum/livepreview/index.html @@ -91,6 +91,54 @@ editor.setShowPrintMargin(false); 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