add revert page

This commit is contained in:
Matthew Scragg 2013-10-15 15:32:17 -05:00
parent 9fed2175de
commit a16b45cdba
10 changed files with 79 additions and 47 deletions

View file

@ -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;

View file

@ -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();
});
}

View file

@ -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<mods.length;mi++)mods[mi]=_MODIFIERS[mods[mi]];return mods}function addEvent(object,event,method){if(object.addEventListener)object.addEventListener(event,method,false);else if(object.attachEvent)object.attachEvent("on"+event,function(){method(window.event)})}
addEvent(document,"keydown",function(event){dispatch(event)});addEvent(document,"keyup",clearModifier);addEvent(window,"focus",resetModifiers);var previousKey=global.key;function noConflict(){var k=global.key;global.key=previousKey;return k}global.key=assignKey;global.key.setScope=setScope;global.key.getScope=getScope;global.key.deleteScope=deleteScope;global.key.filter=filter;global.key.isPressed=isPressed;global.key.getPressedKeyCodes=getPressedKeyCodes;global.key.noConflict=noConflict;global.key.unbind=
unbindKey;if(typeof module!=="undefined")module.exports=key})(this);
$(function(){var $theme=$("#theme-list"),$preview=$("#preview"),$autosave=$("#autosave"),$wordcount=$("#wordcount"),$wordcounter=$("#wordcounter"),$pagename=$("#page-name");var editor,autoInterval,keyCheck,profile={theme:"ace/theme/idle_fingers",currentMd:"",autosave:{enabled:true,interval:3E3},current_filename:$pagename.val()};var dillinger="dillinger",dillingerElem=document.createElement(dillinger),dillingerStyle=dillingerElem.style,domPrefixes="Webkit Moz O ms Khtml".split(" ");function asyncLoad(filename,
$(function(){var $theme=$("#theme-list"),$preview=$("#preview"),$autosave=$("#autosave"),$wordcount=$("#wordcount"),$wordcounter=$("#wordcounter"),$pagename=$("#page-name");var editor,autoInterval,profile={theme:"ace/theme/idle_fingers",currentMd:"",autosave:{enabled:true,interval:3E3},current_filename:$pagename.val()};var dillinger="dillinger",dillingerElem=document.createElement(dillinger),dillingerStyle=dillingerElem.style,domPrefixes="Webkit Moz O ms Khtml".split(" ");function asyncLoad(filename,
cb){(function(d,t){var leScript=d.createElement(t),scripts=d.getElementsByTagName(t)[0];leScript.async=1;leScript.src=filename;scripts.parentNode.insertBefore(leScript,scripts);leScript.onload=function(){cb&&cb()}})(document,"script")}function hasLocalStorage(){var storage;try{if(localStorage.getItem)storage=localStorage}catch(e){}return storage}function getUserProfile(){var p;try{p=JSON.parse(localStorage.profile);p=$.extend(true,profile,p)}catch(e){p=profile}if(p.filename!=$pagename.val())updateUserProfile({filename:$pagename.val(),
currentMd:""});profile=p}function updateUserProfile(obj){localStorage.clear();localStorage.profile=JSON.stringify($.extend(true,profile,obj))}function prefixed(prop){return testPropsAll(prop,"pfx")}function testProps(props,prefixed){for(var i in props)if(dillingerStyle[props[i]]!==undefined)return prefixed==="pfx"?props[i]:true;return false}function testPropsAll(prop,prefixed){var ucProp=prop.charAt(0).toUpperCase()+prop.substr(1),props=(prop+" "+domPrefixes.join(ucProp+" ")+ucProp).split(" ");return testProps(props,
prefixed)}function normalizeTransitionEnd(){var transEndEventNames={"WebkitTransition":"webkitTransitionEnd","MozTransition":"transitionend","OTransition":"oTransitionEnd","msTransition":"msTransitionEnd","transition":"transitionend"};return transEndEventNames[prefixed("transition")]}function getCurrentFilenameFromField(){return $('#filename > 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?'<i class="icon-remove"></i>&nbsp;Disable Autosave':'<i class="icon-ok"></i>&nbsp;Enable Autosave');
$wordcount.html(!profile.wordcount?'<i class="icon-remove"></i>&nbsp;Disabled Word Count':'<i class="icon-ok"></i>&nbsp;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?'<i class="icon-remove"></i>&nbsp;Disable Autosave':'<i class="icon-ok"></i>&nbsp;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?'<i class="icon-remove"></i>&nbsp;Disabled Word Count':'<i class="icon-ok"></i>&nbsp;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?'<i class="icon-remove"></i>&nbsp;Disable Autosave':'<i class="icon-ok"></i>&nbsp;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)};