From a16b45cdba0faa1824e3b4c6d262ba6974f48440 Mon Sep 17 00:00:00 2001 From: Matthew Scragg Date: Tue, 15 Oct 2013 15:32:17 -0500 Subject: [PATCH] add revert page --- realms/__init__.py | 37 ++++++++++++++++++++---------- realms/models.py | 3 --- realms/static/css/style.css | 6 +++++ realms/static/js/dillinger.js | 9 +------- realms/static/packed-editor.js | 17 +++++++------- realms/templates/layout.html | 6 ++--- realms/templates/page/compare.html | 10 +++++--- realms/templates/page/history.html | 7 +++--- realms/templates/page/page.html | 14 ++++++++--- realms/wiki.py | 17 +++++++++++--- 10 files changed, 79 insertions(+), 47 deletions(-) diff --git a/realms/__init__.py b/realms/__init__.py index 94a36a1..cfcaffc 100644 --- a/realms/__init__.py +++ b/realms/__init__.py @@ -226,20 +226,30 @@ def create_app(subdomain=None): User.logout() return redirect(url_for('root')) - @app.route("/commit//") + @app.route("/_commit//") def commit_sha(name, sha): cname = to_canonical(name) data = w.get_page(cname, sha=sha) if data: - return render_template('page/page.html', page=data) + return render_template('page/page.html', name=name, page=data, commit=sha) else: return redirect('/create/'+cname) - @app.route("/compare//") + @app.route("/_compare//") def compare(name, fsha, dots, lsha): diff = w.compare(name, fsha, lsha) - return render_template('page/compare.html', name=name, diff=diff) + return render_template('page/compare.html', name=name, diff=diff, old=fsha, new=lsha) + + @app.route("/_revert", methods=['POST']) + def revert(): + if request.method == 'POST': + name = request.form.get('name') + commit = request.form.get('commit') + cname = to_canonical(name) + w.revert_page(name, commit, message="Reverting %s" % cname, username=CurrentUser.get('username')) + flash('Page reverted', 'success') + return redirect("/" + cname) @app.route("/register", methods=['GET', 'POST']) def register(): @@ -263,12 +273,12 @@ def create_app(subdomain=None): else: return render_template('account/login.html') - @app.route("/history/") + @app.route("/_history/") def history(name): history = w.get_history(name) return render_template('page/history.html', name=name, history=history) - @app.route("/edit/", methods=['GET', 'POST']) + @app.route("/_edit/", methods=['GET', 'POST']) @login_required def edit(name): data = w.get_page(name) @@ -277,7 +287,8 @@ def create_app(subdomain=None): edit_cname = to_canonical(request.form['name']) if edit_cname.lower() != cname.lower(): w.rename_page(cname, edit_cname) - w.write_page(edit_cname, request.form['content'], message=request.form['message'], + w.write_page(edit_cname, request.form['content'], + message=request.form['message'], username=CurrentUser.get('username')) return redirect("/" + edit_cname) else: @@ -288,13 +299,13 @@ def create_app(subdomain=None): else: return redirect('/create/'+cname) - @app.route("/delete/", methods=['POST']) + @app.route("/_delete/", methods=['POST']) @login_required def delete(name): pass - @app.route("/create/", methods=['GET', 'POST']) - @app.route("/create/", methods=['GET', 'POST']) + @app.route("/_create/", methods=['GET', 'POST']) + @app.route("/_create/", methods=['GET', 'POST']) @login_required def create(name=None): cname = "" @@ -304,12 +315,14 @@ def create_app(subdomain=None): # Page exists, edit instead return redirect("/edit/" + cname) if request.method == 'POST': - w.write_page(request.form['name'], request.form['content'], message=request.form['message'], create=True) + w.write_page(request.form['name'], request.form['content'], + message=request.form['message'], + create=True, + username=CurrentUser.get('username')) return redirect("/" + cname) else: return render_template('page/edit.html', name=cname, content="") - @app.route("/") def render(name): cname = to_canonical(name) diff --git a/realms/models.py b/realms/models.py index f8e4b4e..f00afc1 100644 --- a/realms/models.py +++ b/realms/models.py @@ -102,9 +102,6 @@ class CurrentUser(): if id: user = User() session['user'] = user.get_by_id(id) - #import sys - #sys.exit() - #session['user'] = to_dict(user.get_one(id, 'id'), True) def get_id(self): return self.id diff --git a/realms/static/css/style.css b/realms/static/css/style.css index dd9c433..8993577 100644 --- a/realms/static/css/style.css +++ b/realms/static/css/style.css @@ -93,6 +93,12 @@ body { box-shadow: 0 1px 2px rgba(0,0,0,.15); } +#page-action-bar { + position: relative; + z-index: 100; + float: right; +} + .user-avatar a img { width: 24px; height: 24px; diff --git a/realms/static/js/dillinger.js b/realms/static/js/dillinger.js index 6fc17d2..1528f1e 100644 --- a/realms/static/js/dillinger.js +++ b/realms/static/js/dillinger.js @@ -10,7 +10,6 @@ $(function(){ var editor , autoInterval - , keyCheck // used to detect changes not made via keyup , profile = { theme: 'ace/theme/idle_fingers' @@ -331,11 +330,6 @@ $(function(){ * @return {Void} */ function saveFile(isManual){ - if (!keyCheck && profile.currentMd != editor.getSession().getValue()) { - previewMd(); - console.log(keyCheck); - } - keyCheck = false; updateUserProfile({currentMd: editor.getSession().getValue()}); if (isManual) { @@ -536,8 +530,7 @@ $(function(){ * @return {Void} */ function bindPreview(){ - $('#editor').bind('keyup', function() { - keyCheck = true; + editor.getSession().on('change', function(e) { previewMd(); }); } diff --git a/realms/static/packed-editor.js b/realms/static/packed-editor.js index d3b75fc..fe852c4 100644 --- a/realms/static/packed-editor.js +++ b/realms/static/packed-editor.js @@ -635,19 +635,18 @@ scope:scope,method:method,key:keys[i],mods:mods})}}function unbindKey(key,scope) scope)handlers.splice(i,1);else i++}}function getKeys(key){var keys;key=key.replace(/\s/g,"");keys=key.split(",");if(keys[keys.length-1]=="")keys[keys.length-2]+=",";return keys}function getMods(key){var mods=key.slice(0,key.length-1);for(var mi=0;mi span[contenteditable="true"]').text()}function setCurrentFilenameField(str){$('#filename > span[contenteditable="true"]').text(str||profile.current_filename|| "Untitled Document")}function getTextInElement(node){if(node.nodeType===3)return node.data;var txt="";if(node=node.firstChild){do txt+=getTextInElement(node);while(node=node.nextSibling)}return txt}function countWords(string){var words=string.replace(/W+/g," ").match(/\S+/g);return words&&words.length||0}function init(){if(!hasLocalStorage())sadPanda();else{$.support.transitionEnd=normalizeTransitionEnd();getUserProfile();initAce();initUi();bindPreview();bindNav();bindKeyboard();autoSave()}}function initAce(){editor= ace.edit("editor")}function initUi(){fetchTheme(profile.theme,function(){$theme.find('li > a[data-value="'+profile.theme+'"]').addClass("selected");editor.getSession().setUseWrapMode(true);editor.setShowPrintMargin(false);editor.getSession().setMode("ace/mode/markdown");editor.getSession().setValue(profile.currentMd||editor.getSession().getValue());previewMd()});$autosave.html(profile.autosave.enabled?' Disable Autosave':' Enable Autosave'); -$wordcount.html(!profile.wordcount?' Disabled Word Count':' Enabled Word Count');setCurrentFilenameField();$(".dropdown-toggle").dropdown()}function clearSelection(){editor.getSession().setValue("");previewMd()}function saveFile(isManual){if(!keyCheck&&profile.currentMd!=editor.getSession().getValue()){previewMd();console.log(keyCheck)}keyCheck=false;updateUserProfile({currentMd:editor.getSession().getValue()});if(isManual){updateUserProfile({currentMd:""}); -var data={name:$pagename.val(),message:$("#page-message").val(),content:editor.getSession().getValue()};$.post(window.location,data,function(){location.href="/"+data["name"]})}}function autoSave(){if(profile.autosave.enabled)autoInterval=setInterval(function(){saveFile()},profile.autosave.interval);else clearInterval(autoInterval)}$("#save-native").on("click",function(){saveFile(true)});function resetProfile(){localStorage.clear();profile.autosave.enabled=false;delete localStorage.profile;window.location.reload()} -function changeTheme(e){var $target=$(e.target);if($target.attr("data-value")===profile.theme)return;else{$theme.find("li > a.selected").removeClass("selected");$target.addClass("selected");var newTheme=$target.attr("data-value");$(e.target).blur();fetchTheme(newTheme,function(){})}}function fetchTheme(th,cb){var name=th.split("/").pop();asyncLoad("/static/js/ace/theme-"+name+".js",function(){editor.setTheme(th);cb&&cb();updateBg(name);updateUserProfile({theme:th})})}function updateBg(name){}function previewMd(){var unmd= -editor.getSession().getValue(),md=MDR.convert(unmd,true);$preview.html("").html(md)}function updateFilename(str){var f;if(typeof str==="string")f=str;else f=getCurrentFilenameFromField();updateUserProfile({current_filename:f})}function showHtml(){var unmd=editor.getSession().getValue();function _doneHandler(jqXHR,data,response){var resp=JSON.parse(response.responseText);$("#myModalBody").text(resp.data);$("#myModal").modal()}function _failHandler(){alert("Roh-roh. Something went wrong. :(")}var config= -{type:"POST",data:"unmd="+encodeURIComponent(unmd),dataType:"json",url:"/factory/fetch_html_direct",error:_failHandler,success:_doneHandler};$.ajax(config)}function sadPanda(){alert("Sad Panda - No localStorage for you!")}function toggleAutoSave(){$autosave.html(profile.autosave.enabled?' Disable Autosave':' Enable Autosave');updateUserProfile({autosave:{enabled:!profile.autosave.enabled}});autoSave()}function bindPreview(){$("#editor").bind("keyup", -function(){keyCheck=true;previewMd()})}function bindNav(){$theme.find("li > a").bind("click",function(e){changeTheme(e);return false});$("#clear").on("click",function(){clearSelection();return false});$("#autosave").on("click",function(){toggleAutoSave();return false});$("#reset").on("click",function(){resetProfile();return false});$("#cheat").on("click",function(){window.open("https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet","_blank");return false})}function bindKeyboard(){key("command+s, ctrl+s", -function(e){saveFile(true);e.preventDefault()});var saveCommand={name:"save",bindKey:{mac:"Command-S",win:"Ctrl-S"},exec:function(){saveFile(true)}};editor.commands.addCommand(saveCommand)}init()}); -function getScrollHeight($prevFrame){if($prevFrame[0].scrollHeight!==undefined)return $prevFrame[0].scrollHeight;else if($prevFrame.find("html")[0].scrollHeight!==undefined&&$prevFrame.find("html")[0].scrollHeight!==0)return $prevFrame.find("html")[0].scrollHeight;else return $prevFrame.find("body")[0].scrollHeight} +$wordcount.html(!profile.wordcount?' Disabled Word Count':' Enabled Word Count');setCurrentFilenameField();$(".dropdown-toggle").dropdown()}function clearSelection(){editor.getSession().setValue("");previewMd()}function saveFile(isManual){updateUserProfile({currentMd:editor.getSession().getValue()});if(isManual){updateUserProfile({currentMd:""});var data={name:$pagename.val(),message:$("#page-message").val(),content:editor.getSession().getValue()}; +$.post(window.location,data,function(){location.href="/"+data["name"]})}}function autoSave(){if(profile.autosave.enabled)autoInterval=setInterval(function(){saveFile()},profile.autosave.interval);else clearInterval(autoInterval)}$("#save-native").on("click",function(){saveFile(true)});function resetProfile(){localStorage.clear();profile.autosave.enabled=false;delete localStorage.profile;window.location.reload()}function changeTheme(e){var $target=$(e.target);if($target.attr("data-value")===profile.theme)return; +else{$theme.find("li > a.selected").removeClass("selected");$target.addClass("selected");var newTheme=$target.attr("data-value");$(e.target).blur();fetchTheme(newTheme,function(){})}}function fetchTheme(th,cb){var name=th.split("/").pop();asyncLoad("/static/js/ace/theme-"+name+".js",function(){editor.setTheme(th);cb&&cb();updateBg(name);updateUserProfile({theme:th})})}function updateBg(name){}function previewMd(){var unmd=editor.getSession().getValue(),md=MDR.convert(unmd,true);$preview.html("").html(md)} +function updateFilename(str){var f;if(typeof str==="string")f=str;else f=getCurrentFilenameFromField();updateUserProfile({current_filename:f})}function showHtml(){var unmd=editor.getSession().getValue();function _doneHandler(jqXHR,data,response){var resp=JSON.parse(response.responseText);$("#myModalBody").text(resp.data);$("#myModal").modal()}function _failHandler(){alert("Roh-roh. Something went wrong. :(")}var config={type:"POST",data:"unmd="+encodeURIComponent(unmd),dataType:"json",url:"/factory/fetch_html_direct", +error:_failHandler,success:_doneHandler};$.ajax(config)}function sadPanda(){alert("Sad Panda - No localStorage for you!")}function toggleAutoSave(){$autosave.html(profile.autosave.enabled?' Disable Autosave':' Enable Autosave');updateUserProfile({autosave:{enabled:!profile.autosave.enabled}});autoSave()}function bindPreview(){editor.getSession().on("change",function(e){previewMd()})}function bindNav(){$theme.find("li > a").bind("click",function(e){changeTheme(e); +return false});$("#clear").on("click",function(){clearSelection();return false});$("#autosave").on("click",function(){toggleAutoSave();return false});$("#reset").on("click",function(){resetProfile();return false});$("#cheat").on("click",function(){window.open("https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet","_blank");return false})}function bindKeyboard(){key("command+s, ctrl+s",function(e){saveFile(true);e.preventDefault()});var saveCommand={name:"save",bindKey:{mac:"Command-S", +win:"Ctrl-S"},exec:function(){saveFile(true)}};editor.commands.addCommand(saveCommand)}init()});function getScrollHeight($prevFrame){if($prevFrame[0].scrollHeight!==undefined)return $prevFrame[0].scrollHeight;else if($prevFrame.find("html")[0].scrollHeight!==undefined&&$prevFrame.find("html")[0].scrollHeight!==0)return $prevFrame.find("html")[0].scrollHeight;else return $prevFrame.find("body")[0].scrollHeight} function syncPreview(){var $ed=window.ace.edit("editor");var $prev=$("#preview");var editorScrollRange=$ed.getSession().getLength();var previewScrollRange=getScrollHeight($prev);var scrollFactor=$ed.getFirstVisibleRow()/editorScrollRange;$prev.scrollTop(scrollFactor*previewScrollRange)} window.onload=function(){var $loading=$("#loading");if($.support.transition)$loading.bind($.support.transitionEnd,function(){$("#main").removeClass("bye");$loading.remove()}).addClass("fade_slow");else{$("#main").removeClass("bye");$loading.remove()}window.ace.edit("editor").session.on("changeScrollTop",syncPreview);window.ace.edit("editor").session.selection.on("changeCursor",syncPreview)}; diff --git a/realms/templates/layout.html b/realms/templates/layout.html index bc90667..360207a 100644 --- a/realms/templates/layout.html +++ b/realms/templates/layout.html @@ -43,10 +43,10 @@