Skip to content

Instantly share code, notes, and snippets.

@Wizmann
Created April 25, 2014 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wizmann/11285463 to your computer and use it in GitHub Desktop.
Save Wizmann/11285463 to your computer and use it in GitHub Desktop.
dillinger-0.1.6.4.min.js
function getScrollHeight(a){return void 0!==a[0].scrollHeight?a[0].scrollHeight:void 0!==a.find("html")[0].scrollHeight&&0!==a.find("html")[0].scrollHeight?a.find("html")[0].scrollHeight:a.find("body")[0].scrollHeight}function syncPreview(){var a=window.ace.edit("editor"),b=$("#preview"),c=a.getSession().getLength()-a.getLastVisibleRow()+a.getFirstVisibleRow(),d=getScrollHeight(b),e=a.getFirstVisibleRow()/c;setTimeout(function(){b.scrollTop(e*(d-b.height()))})}$(function(){function a(a,b){!function(c,d){var e=c.createElement(d),f=c.getElementsByTagName(d)[0];e.async=1,e.src=a,f.parentNode.insertBefore(e,f),e.onload=function(){b&&b()}}(document,"script")}function b(){var a;try{localStorage.getItem&&(a=localStorage)}catch(b){}return a}function c(){var a;try{a=JSON.parse(localStorage.profile),a=$.extend(!0,W,a)}catch(b){a=W}W=a}function d(a){localStorage.profile=JSON.stringify($.extend(!0,W,a))}function e(a){return g(a,"pfx")}function f(a,b){for(var c in a)if(void 0!==Z[a[c]])return"pfx"===b?a[c]:!0;return!1}function g(a,b){var c=a.charAt(0).toUpperCase()+a.substr(1),d=(a+" "+_.join(c+" ")+c).split(" ");return f(d,b)}function h(){var a={WebkitTransition:"webkitTransitionEnd",MozTransition:"transitionend",OTransition:"oTransitionEnd",msTransition:"msTransitionEnd",transition:"transitionend"};return a[e("transition")]}function i(a){return"dillinger_"+(new Date).toISOString().replace(/[\.:-]/g,"_")+"."+a}function j(){return $('#filename > span[contenteditable="true"]').text()}function k(a){$('#filename > span[contenteditable="true"]').text(a||W.current_filename||"Untitled Document")}function l(){b()?($.support.transitionEnd=h(),c(),p(),o(),q(),marked.setOptions({gfm:!0,tables:!0,breaks:!1,pedantic:!1,sanitize:!0,smartLists:!0,smartypants:!1,langPrefix:"lang-"}),N(),O(),P(),Q(),M(),t()):F()}function m(){"update"==U.mode?U.delta?$("#file_status .label").html("! 已创建,但有修改").addClass("label-info"):$("#file_status .label").html("&#10003; 已创建").addClass("label-success"):$("#file_status .label").html("&#10033; 尚未保存").addClass("label-warning")}function n(){"update"==U.mode?window.location.href="/":("init"==U.mode||confirm("是否放弃当前未保存的文档?"))&&(U.content="",U.mode="create",U.guid=null,R.getSession().setValue(U.content),m(),z(),TagsPlugin.init())}function o(){function a(a){var b={};return b.guid=(a.find("en-note > div").eq(0).find("a")[0].href.match(/guid=(.*)/)||[])[1],b.content=unescape(a.find("en-note > center").text()),b}if(window.location.search.indexOf("guid")<0)W.currentMd?(U.content=W.currentMd,U.mode="create"):(U.content=$("#hidden_content").text(),U.mode="init");else{var b=a($("#hidden_content"));U.content=b.content,U.last_save=b.content.length,U.delta=!1,U.guid=b.guid,U.mode="update"}m(),ImageManager.get_images()}function p(){R=ace.edit("editor")}function q(){x(W.theme,function(){ab.find('li > a[data-value="'+W.theme+'"]').addClass("selected"),R.getSession().setUseWrapMode(!0),R.setShowPrintMargin(!1),R.getSession().setMode("ace/mode/markdown"),R.getSession().setValue(U.content),R.setBehavioursEnabled(!0),R.setFontSize("13px"),z()}),cb.html(W.autosave.enabled?'<i class="icon-remove"></i>&nbsp;Disable Autosave':'<i class="icon-ok"></i>&nbsp;Enable Autosave'),T=db.attr("data-github-username"),T&&gb.showMessage("What's Up "+T,1e3),k(),$(".dropdown-toggle").dropdown();var a=$('a.dropdown-toggle[data-status="link"]'),b=a.data("provider");if(b){var c=a.data("providerlabel");U.provider=b,W.provider=b,U.provider_label=c,$("#provider-list").hide(),$("#simple-save").find("span").text(c).end().show().click(function(){jb.save()}),$("#unlink").attr("href",a.next().find("[data-link='Unlink']").attr("href"))}else $("#simple-save").hide(),$("#unlink").hide();var d=$(".ace_content")[0];d.ondragover=function(){return this.className="hover",!1},d.ondragend=function(){return this.className="",!1},d.ondrop=function(a){a.preventDefault(),this.className="";var b=a.dataTransfer.files;console.log(b),$.map(b,function(a){a.type.indexOf("image")>=0&&ImageManager.insert(a)})},document.onpaste=function(a){var b=a.clipboardData.items;console.log(JSON.stringify(b)),$.map(b,function(a){if(a.type.indexOf("image")>=0){var b=a.getAsFile();ImageManager.insert(b)}})};var e=parseInt((/msie (\d+)/.exec(navigator.userAgent.toLowerCase())||[])[1]);e&&$("#modal-generic").removeClass("fade");var f=function(a){var b={};if($.map(a,function(a){b[a.name]=a}),a&&a.length){U.notebooks=b;var c=$.map(a,function(a){return a.name});if(W.notebook){var d=W.notebook.name,e=c.indexOf(d);e>0&&(c.splice(e,1),c.unshift(d))}$("#notebooks-hint").data("typeahead").source=c}};U.provider&&$.get("/notebooks/"+U.provider).done(function(a){console.log("notebooks",a),f(a)}),$("#notebooks-hint").typeahead(),W.notebook&&W.notebook.guid&&f([W.notebook]),$("#spliter").click(function(){$("#app_wrap").is(".no-preview")?($("#app_wrap").removeClass("no-preview"),$(this).attr("title","隐藏预览")):($("#app_wrap").addClass("no-preview"),$(this).attr("title","展开预览")),R.resize()})}function r(){R.getSession().setValue(""),z()}function s(a,b){if("update"!=U.mode){if(b=null!=b?b:R.getSession().getValue(),!a&&b.indexOf("使用说明")>=0&&b.indexOf("hustgock")>0&&b.indexOf("dillinger")>0)return;d({currentMd:b}),a&&gb.showMessage(gb.messages.docSavedLocal)}}function t(){W.autosave.enabled?S=setInterval(function(){s()},W.autosave.interval):clearInterval(S),window.onbeforeunload=function(){if(clearInterval(S),"update"==U.mode){if(s(!1,""),U.delta)return"尚未保存到"+U.provider_label+",是否放弃修改?"}else s()}}function u(a){W.filelist[a.guid]=a,d({})}function v(){W.autosave.enabled=!1,delete localStorage.profile,window.location.reload()}function w(a){var b=$(a.target);if(b.attr("data-value")!==W.theme){ab.find("li > a.selected").removeClass("selected"),b.addClass("selected");var c=b.attr("data-value");$(a.target).blur(),x(c,function(){})}}function x(b,c){var e=b.split("/").pop();a("/js/theme-"+e+".js",function(){R.setTheme(b),c&&c(),y(e),d({theme:b})})}function y(a){document.body.style.backgroundColor=eb[a]}function z(){var a=R.getSession().getValue(),b=TagsPlugin.process(marked(a));bb.html("").html(b),ImageManager.process(bb),bb.find("code").map(function(a,b){var c=$(b).text(),d=c.match(/^\s*\$(.*?)\$\s*$/);d&&$(b).addClass("lang-mathjax inline").text(d[1]).wrap('<span class="inline-mathjax"></span>')}),bb.find("pre, .inline-mathjax").map(function(a,b){var c=$(b.childNodes[0]);if(window.MathJax&&c.is(".lang-mathjax")){var d,e=c.html(),f=md5(e),g=fb.get(f),h=c.is(".inline")?" style='display:inline-block;margin:0'":"",i=h?"span":"div";g?(console.log("cache hit",f),$(b).replaceWith("<"+i+" class='mathjax'"+h+">"+g+"</"+i+">")):(console.log("cache nohit",f,e),d=$("<"+i+" class='mathjax' "+h+">$#"+e+"#$</"+i+">")[0],MathJax.Hub.Queue(["Typeset",MathJax.Hub,d]),MathJax.Hub.Queue(function(){var a=$(d).children()[1];a&&0!=$(a).text().length&&!$(a).is(".MathJax_Error")&&($(d).find("script").remove(),$(d).find("[id]").map(function(a,b){$(b).removeAttr("id").removeAttr("class").removeAttr("role").removeAttr("aria-readonly")}),$(d).find("nobr").replaceWith(function(){return $("<"+i+" style='white-space: nowrap;'></"+i+">").append($(this).html().replace(/font-family: (.*?);/g,function(a,b){return"font-family:"+b+", serif;"}))}),fb.set(f,d.innerHTML),$(b).replaceWith(d))}))}else hljs.highlightBlock(b)}),window.MathJax&&MathJax.Hub.Queue(function(){fb.clean()}),bb.find("a").attr("target","_blank").each(function(a,b){$(b).attr("href",b.href)}),"update"==U.mode&&a.length!=U.last_save&&(U.delta=!0,m())}function A(a){var b;b="string"==typeof a?a:j(),d({current_filename:b})}function B(){function a(a,b,c){a=b=null;var d=JSON.parse(c.responseText);document.getElementById("downloader").src="/files/md/"+d.data}function b(){alert("Roh-roh. Something went wrong. :(")}var c=R.getSession().getValue(),d={type:"POST",data:"unmd="+encodeURIComponent(c),dataType:"json",url:"/factory/fetch_markdown",error:b,success:a};$.ajax(d)}function C(){function a(a,b,c){var d=JSON.parse(c.responseText);document.getElementById("downloader").src="/files/html/"+d.data}function b(){alert("Roh-roh. Something went wrong. :(")}var c=R.getSession().getValue(),d={type:"POST",data:"unmd="+encodeURIComponent(c),dataType:"json",url:"/factory/fetch_html",error:b,success:a};$.ajax(d)}function D(){function a(a,b,c){var d=JSON.parse(c.responseText);document.getElementById("downloader").src="/files/pdf/"+d.data}function b(){alert("Roh-roh. Something went wrong. :(")}var c=R.getSession().getValue(),d={type:"POST",data:"unmd="+encodeURIComponent(c),dataType:"json",url:"/factory/fetch_pdf",error:b,success:a};$.ajax(d)}function E(){z(),$(".modal-body").html($("<pre></pre>").text(bb.html())),$("#modal-generic").modal({keyboard:!0,backdrop:!0,show:!0})}function F(){alert("Sad Panda - No localStorage for you!")}function G(){$(".modal-header h3").text("过往文件");var a=[];a=$.map(W.filelist,function(a){var b="/?provider="+a.provider+"&guid="+a.guid;return"<li><a href='"+b+"'>"+a.title+"</a></li>"}).join(""),$(".modal-body").html("<ul>"+a+"</ul>"),$("#modal-generic").modal({keyboard:!0,backdrop:!0,show:!0})}function H(){var a=$("#readme").text();$(".modal-body").html("<pre>"+a+"</pre>"),$("#modal-generic").modal({keyboard:!0,backdrop:!0,show:!0})}function I(){$(".modal-header h3").text("What's the deal with Dillinger?");var a="<p>Dillinger is an online cloud-enabled, HTML5, buzzword-filled Markdown editor.</p><p>Dillinger was designed and developed by <a href='http://twitter.com/joemccann'>@joemccann</a> because he needed a decent Markdown editor.</p><p>Dillinger is a 100% open source project so <a href='https://github.com/joemccann/dillinger'>fork the code</a> and contribute!</p><p>Follow Dillinger on Twitter at <a href='http://twitter.com/dillingerapp'>@dillingerapp</a></p><p>Follow Joe McCann on Twitter at <a href='http://twitter.com/joemccann'>@joemccann</a></p>";$(".modal-body").html(a),$("#modal-generic").modal({keyboard:!0,backdrop:!0,show:!0})}function J(){$(".modal-header h3").text("Preferences");var a='<div><ul><li><a href="#" id="paper">Toggle Paper</a></li><li><a href="#" id="reset">Reset Profile</a></li></ul></div>';$(".modal-body").html(a),$("#modal-generic").modal({keyboard:!0,backdrop:!0,show:!0})}function K(){bb.css("backgroundImage",W.showPaper?'url("")':'url("'+V+'")'),d({showPaper:!W.showPaper}),gb.showMessage(gb.messages.profileUpdated)}function L(){cb.html(W.autosave.enabled?'<i class="icon-remove"></i>&nbsp;Disable Autosave':'<i class="icon-ok"></i>&nbsp;Enable Autosave'),d({autosave:{enabled:!W.autosave.enabled}}),t()}function M(){$('#filename > span[contenteditable="true"]').bind("keyup",A)}function N(){$("#editor").bind("keyup",z)}function O(){ab.find("li > a").bind("click",function(a){return w(a),!1}),$("#clear").on("click",function(){return r(),!1}),$("#save_evernote").on("click",function(){jb.save()}),$("#save_dropbox").on("click",function(){return W.current_filename=W.current_filename||"/Dillinger/"+i("md"),kb.putMarkdownFile(),s(),!1}),$("#save_googledrive").on("click",function(){ib.save(),s()}),$(".modal-body").delegate("#paper","click",function(){return K(),!1}),$("#autosave").on("click",function(){return L(),!1}),$("#reset").on("click",function(){return v(),!1}),db.on("click",function(){return hb.fetchRepos(),!1}),$("#import_dropbox").on("click",function(){return kb.searchDropbox(),!1}),$("#import_googledrive").on("click",function(){return ib.search(),!1}),$("#export_md").on("click",function(){return B(),$(".dropdown").removeClass("open"),!1}),$("#export_html").on("click",function(){return C(),$(".dropdown").removeClass("open"),!1}),$("#export_pdf").on("click",function(){return D(),$(".dropdown").removeClass("open"),!1}),$("#show_html").on("click",function(){return E(),$(".dropdown").removeClass("open"),!1}),$("#preferences").on("click",function(){return J(),!1}),$("#about").on("click",function(){return I(),!1}),$("#cheat").on("click",function(){return window.open("http://wowubuntu.com/markdown/","_blank"),!1}),$("#show_filelist").on("click",function(){console.log(W.filelist),G()}),$("#show_manual").on("click",function(){H()}),$("#uploader").on("change",function(a){console.log(a);var b=a.currentTarget.files;$.map(b,function(a){ImageManager.insert(a)})}),$("#insert_img_url").on("click",function(){var a=prompt("请输入图片URL:")||"",b="读取图片失败";return a.indexOf("http")<0?void(a&&alert("错误的URL")):(gb.showMessage("正在读取图片...",3e4),void $.ajax({type:"get",url:"/imageproxy?url="+decodeURIComponent(a),error:function(){gb.showMessage(b)},success:function(b,c,d){var e=JSON.parse(d.responseText);console.log("imageproxy",e),ImageManager.insert_image(a,e.type,e.data),gb.showMessage("读取成功")}}))})}function P(){key("command+s, ctrl+s",function(a){s(!0),a.preventDefault()});var a={name:"save",bindKey:{mac:"Command-S",win:"Ctrl-S"},exec:function(){s(!0)}},b={name:"filenamer",bindKey:{mac:"Command-Shift-M",win:"Ctrl-Shift-M"},exec:function(){var a=JSON.parse(localStorage.profile);alert(a.current_filename.replace(/\s/g,"-").toLowerCase())}};R.commands.addCommand(a),R.commands.addCommand(b)}function Q(){$(document).on("click","#new-file",function(){n()}).on("click",".repo",function(){var a=$(this).parent("li").attr("data-repo-name");return hb.isRepoPrivate="true"===$(this).parent("li").attr("data-repo-private")?!0:!1,hb.fetchBranches(a),!1}).on("click",".branch",function(){var a=$(this).parent("li").attr("data-repo-name"),b=$(this).parent("li").attr("data-commit-sha");return hb.currentBranch=$(this).text(),hb.fetchTreeFiles(a,b),!1}).on("click",".tree_file",function(){var a=$(this).parent("li").attr("data-tree-file");return hb.fetchMarkdownFile(a),!1}).on("click",".dropbox_file",function(){var a=$(this).parent("li").attr("data-file-path");return W.current_filename=a.split("/").pop().replace(".md",""),kb.setFilePath(a),kb.fetchMarkdownFile(a),!1}).on("click",".googledrive_file",function(){var a=$(this).parent("li").attr("data-file-id");return W.current_filename=$(this).html(),ib.fileId=a,ib.get(),!1}),"draggable"in document.createElement("span")&&$("#editor").on("dragover",function(a){a.preventDefault(),a.stopPropagation()}).on("drop",function(a){a.preventDefault(),a.stopPropagation();var b,c=a.originalEvent,d=c.target.files||c.dataTransfer.files,e=new FileReader,f=0;do b=d[f++];while(b&&"text"!==b.type.substr(0,4)&&".md"!==b.name.substr(b.name.length-3));b&&(e.onload=function(a){R.getSession().setValue(a.target.result),z()},e.readAsText(b))})}var R,S,T,U={},V="/img/notebook_paper_200x200.gif",W={theme:"ace/theme/solarized_dark",showPaper:!1,currentMd:"",autosave:{enabled:!0,interval:3e3},current_filename:"Untitled Document",dropbox:{filepath:"/Dillinger/"},filelist:{},provider:"",images:{}},X="dillinger",Y=document.createElement(X),Z=Y.style,_="Webkit Moz O ms Khtml".split(" "),ab=$("#theme-list"),bb=$("#preview"),cb=$("#autosave"),db=$("#import_github"),eb={chrome:"#bbbbbb",clouds:"#7AC9E3",clouds_midnight:"#5F9EA0",cobalt:"#4d586b",crimson_editor:"#ffffff",dawn:"#DADCAD",eclipse:"#6C7B8A",idle_fingers:"#DEB887",kr_theme:"#434343",merbivore:"#3E353E",merbivore_soft:"#565156",mono_industrial:"#C0C0C0",monokai:"#F5DEB3",pastel_on_dark:"#676565","solarized-dark":"#0E4B5A",solarized_light:"#dfcb96",textmate:"#fff",tomorrow:"#0e9211",tomorrow_night:"#333536",tomorrow_night_blue:"#3a4150",tomorrow_night_bright:"#3A3A3A",tomorrow_night_eighties:"#474646",twilight:"#534746",vibrant_ink:"#363636"};ImageManager=function(){function a(){var a=$.map(bb.find("img[longdesc]"),function(a){return $(a).attr("longdesc")});console.log("used:",a),$.each(W.images,function(b){a.indexOf(b)<0&&(delete W.images[b],console.log("deleted:",b))})}function b(b,c){a(),R.insert("![Alt text]("+b+")"),W.images[b]=c,d({}),z()}function c(a){return a.replace(/(%|\(|\))/g,"")}var e=$("#images_area"),f={insert:function(a){var b=new FileReader;b.onload=function(b){var d=b.target.result,e=c("data:image,local://"+(a.name||(new Date).getTime().toString()));f.insert_image(e,a.type,d)},console.log(a),b.readAsDataURL(a)},insert_image:function(a,c,d){var f=new Image;f.src=d,$.attr(f,"longdesc",a),$.attr(f,"type",c),e.append(f),b(a,d)},process:function(a){$.map(a.find("img[src]"),function(a){var b=a.src;return $(a).replaceWith(e.find('img[longdesc="'+decodeURIComponent(b)+'"]').first().attr("title",$.attr(a,"title")).attr("alt",$.attr(a,"alt")).clone()),b});a.find('img[src^="http"]').remove()},get_images:function(){$.each(W.images,function(a,b){var c=new Image;c.src=b,$(c).attr("type",b.match(/^data:(.*?);base64/)[1]),$(c).attr("longdesc",a),e.append(c)})}};return f}(),TagsPlugin=function(){var a=/<p>@(\((.*)\))?(\[(.*?)\])?\s*<\/p>/i,b=[];return{init:function(){b=[]},process:function(c){return c.replace(a,function(a,c,e,f,g){var h=["<p><span>tags: </span>"];if(g=g||"",b=$.map(g.split(/[\|,]/),function(a){return a.trim()}),U.notebooks&&c&&!e){var i=R.getCursorPosition();$("#notebooks-hint").show().focus().css("top",20*i.row+30+"px");var j=$("#notebooks-hint ").data("typeahead");if(!j)return;j.show_all(),j.oninput=function(){},j.onselect=function(a){R.insert(a),U.notebook=U.notebooks[a],d({notebook:U.notebook}),$("#notebooks-hint").blur().hide(),R.focus()},j.oncancel=function(){$("#notebooks-hint").blur().hide().val(""),R.undo()}}return $.map(b,function(a){h.push("<code>",a,"</code> ")}),h.push("</p>"),h.join("")})},get_tags:function(){return b}}}();var fb=function(){var a={},b=[];return{get:function(c){return b.push(c),a[c]},set:function(c,d){a[c]=d,b.push(c)},clean:function(){Object.keys(a).filter(function(a){return b.indexOf(a)<0}).map(function(b){console.log("cache clean",b),delete a[b]}),b=[]}}}(),gb=function(){var a=$("#notify");return{messages:{profileUpdated:"Profile updated",profileCleared:"Profile cleared",docSavedLocal:"已在本地缓存",docSavedServer:"已保存到印象笔记",docSaving:"正在保存...",saveFailed:"抱歉,保存失败。请联系hustgock@gmail.com",saveFailedDetail:"抱歉,保存失败。原因: ",docSavedDropbox:"Document saved on dropbox",dropboxImportNeeded:"Please import a file from dropbox first."},showMessage:function(b,c){a.text("").stop(!1,!0).html(b).slideDown(250,function(){a.delay(c||1e3).slideUp(250)})}}}(),hb=function(){function a(a,b){var c=a.url.toLowerCase(),d=b.url.toLowerCase();return c===d?0:isNaN(a)||isNaN(b)?c>d?1:-1:a-b}function b(a){return/(\.md)|(\.markdown)/i.test(a)}function c(a,c){var d=[],e="https://raw.github.com",f="/";return c.forEach(function(c){if(b(c.path)){var g;g=hb.isRepoPrivate?c.url:e+f+T+f+a+f+hb.currentBranch+f+c.path;var h={link:g,path:c.path,sha:c.sha};d.push(h)}}),d}function d(b){var c="<ul>";return b.sort(a),b.forEach(function(a){c+='<li data-repo-name="'+a.name+'" data-repo-private="'+a["private"]+'"><a class="repo" href="#">'+a.name+"</a></li>"}),c+="</ul>",$(".modal-header h3").text("Your Github Repos"),$(".modal-body").html(c),$("#modal-generic").modal({keyboard:!0,backdrop:!0,show:!0}),!1}function e(a,b){var c="";b.forEach(function(b){var d=b.name,e=b.commit.sha;c+='<li data-repo-name="'+a+'" data-commit-sha="'+e+'"><a class="branch" href="#">'+d+"</a></li>"}),$(".modal-header h3").text(a),$(".modal-body").find("ul").find("li").remove().end().append(c)}function f(a,b){var d=c(a,b),e="";d.forEach(function(a){e+=hb.isRepoPrivate?'<li data-tree-file-sha="'+a.sha+'" data-tree-file="'+a.link+'" class="private_repo"><a class="tree_file" href="#">'+a.path+"</a></li>":'<li data-tree-file="'+a.link+'"><a class="tree_file" href="#">'+a.path+"</a></li>"}),$(".modal-header h3").text(a),$(".modal-body").find("ul").find("li").remove().end().append(e)}return{currentBranch:"",isRepoPrivate:!1,fetchRepos:function(){function a(){gb.showMessage("Fetching Repos...")}function b(a,b,c){a=b=null,c=JSON.parse(c.responseText),c.length?d(c):gb.showMessage("No repos available!")}function c(a){alert(a.responseText||"Roh-roh. Something went wrong. :(")}var e={type:"POST",dataType:"text",url:"/import/github/repos",beforeSend:a,error:c,success:b};$.ajax(e)},fetchBranches:function(a){function b(){gb.showMessage("Fetching Branches for Repo "+a)}function c(b,c,d){b=c=null,d=JSON.parse(d.responseText),d.length?e(a,d):(gb.showMessage("No branches available!"),$("#modal-generic").modal("hide"))}function d(){alert("Roh-roh. Something went wrong. :(")}var f={type:"POST",dataType:"json",data:"repo="+a,url:"/import/github/branches",beforeSend:b,error:d,success:c};$.ajax(f)},fetchTreeFiles:function(a,b){function c(){gb.showMessage("Fetching Tree for Repo "+a)}function d(b,c,d){b=c=null,d=JSON.parse(d.responseText),d.tree.length?f(a,d.tree):(gb.showMessage("No tree files available!"),$("#modal-generic").modal("hide"))}function e(){alert("Roh-roh. Something went wrong. :(")}var g={type:"POST",dataType:"json",data:"repo="+a+"&sha="+b,url:"/import/github/tree_files",beforeSend:c,error:e,success:d};$.ajax(g)},fetchMarkdownFile:function(a){function b(b,c,d){if(b=c=null,d=JSON.parse(d.responseText),d.error)gb.showMessage("No markdown for you!"),$("#modal-generic").modal("hide");else{$("#modal-generic").modal("hide"),R.getSession().setValue(d.data);var e=a.split("/").pop();A(e),k(e),z()}}function c(){alert("Roh-roh. Something went wrong. :(")}function d(){$(".dropdown").removeClass("open")}var e={type:"POST",dataType:"json",data:"mdFile="+a,url:"/import/github/file",error:c,success:b,complete:d};$.ajax(e)}}}(),ib=function(){function a(a,b,c){gb.showMessage(c.responseText)}function b(b,c,d){var e=JSON.parse(d.responseText),f="<ul>";return Array.isArray(e.items)?(e.items.forEach(function(a){f+='<li data-file-id="'+a.id+'"><a class="googledrive_file" href="#">'+a.title+"</a></li>"}),f+="</ul>",$(".modal-header h3").text("Your Google Drive Files"),$(".modal-body").html(f),void $("#modal-generic").modal({keyboard:!0,backdrop:!0,show:!0})):a(null,null,{responseText:"No Markdown files found!"})}function c(a,b,c){var d=JSON.parse(c.responseText);$("#modal-generic").modal("hide"),R.getSession().setValue(d.content),z()}return{fileId:null,search:function(){$.ajax({dataType:"json",url:"/import/googledrive",beforeSend:function(){gb.showMessage("Searching for .md files")},error:a,success:b})},get:function(){$.ajax({dataType:"json",url:"/fetch/googledrive?fileId="+this.fileId,error:a,success:c})},save:function(){var b=encodeURIComponent(R.getSession().getValue()),c="title="+encodeURIComponent(W.current_filename)+".md&content="+b;$.ajax({dataType:"json",type:"post",data:c,url:"/save/googledrive?fileId="+(ib.fileId||""),error:a,success:function(a,b,c){var d=JSON.parse(c.responseText);d.id?(ib.fileId=d.id,gb.showMessage("Document saved on Google Drive")):gb.showMessage("An error occurred!")}})}}}(),jb=function(){function a(){gb.showMessage(gb.messages.saveFailed)}var b="area br col command hr img keygen source wbr".split(" ").join("|"),c=new RegExp("<("+b+")([^>]*?)(/>|>)","gi"),d={sanitize:function(a){return a.replace(c,"<$1 $2 />")},save:function(){var b=R.getSession().getValue();$("#preview").find("pre").removeAttr("data-initialized").removeAttr("data-gclp-id");{var c=$("#preview").html();"<center style='display:none'>{{md}}</center><div>{{content}}</div>".replace("{{md}}",b).replace("{{content}}",c)}gb.showMessage(gb.messages.docSaving,2e5),$.ajax({type:"post",data:{title:((c.match(/<h1>(.*?)<\/h1>/)||c.match(/<h2>(.*?)<\/h2>/)||[])[1]||"").trim(),content:d.sanitize(c),md:b,tags:TagsPlugin.get_tags(),guid:U.guid,provider:U.provider,notebook:U.notebook},url:"/save/"+U.provider,error:a,success:function(a,c,d){var e=JSON.parse(d.responseText);if(console.log(e),e.guid)gb.showMessage("已保存到"+U.provider_label,5e3),U.guid=e.guid,s(!1,""),U.mode="update",U.last_save=b.length,U.delta=!1,m(),u({title:e.title,guid:e.guid,provider:{evernote:"evernote_int",yinxiang:"evernote"}[U.provider]});else{var f=e.message?gb.messages.saveFailedDetail+e.message:gb.messages.saveFailed;e.action&&(f+=e.action),gb.showMessage(f,5e4)}}})}};return d}(),kb=function(){function a(a,b){var c=a.path.toLowerCase(),d=b.path.toLowerCase();return c===d?0:isNaN(a)||isNaN(b)?c>d?1:-1:a-b}function b(b){var c="<ul>";return b.sort(a),b.forEach(function(a){c+='<li data-file-path="'+a.path+'"><a class="dropbox_file" href="#">'+a.path+"</a></li>"}),c+="</ul>",$(".modal-header h3").text("Your Dropbox Files"),$(".modal-body").html(c),$("#modal-generic").modal({keyboard:!0,backdrop:!0,show:!0}),!1}function c(a){return encodeURIComponent(a.split("/").pop())}function e(a){var b=a.split("/").pop();return a.replace(b,"")}return{fetchAccountInfo:function(){function a(){gb.showMessage("Fetching User Info from Dropbox")}function b(a,b,c){var d=JSON.parse(c.responseText);gb.showMessage("Sup "+d.display_name)}function c(){alert("Roh-roh. Something went wrong. :(")}var d={type:"GET",dataType:"json",url:"/account/dropbox",beforeSend:a,error:c,success:b};$.ajax(d)},fetchMetadata:function(){function a(){gb.showMessage("Fetching Metadata")}function b(a,b,c){var d=JSON.parse(c.responseText);window.console&&window.console.log&&console.dir(d)}function c(){alert("Roh-roh. Something went wrong. :(")}var d={type:"GET",dataType:"json",url:"/dropbox/metadata",beforeSend:a,error:c,success:b};$.ajax(d)},searchDropbox:function(){function a(){gb.showMessage("Searching for .md Files")}function c(a,c,d){a=c=null;var e=JSON.parse(d.responseText);if(e.hasOwnProperty("statusCode")&&401===e.statusCode){var f=JSON.parse(e.data);return gb.showMessage("Error! "+f.error,1e3),setTimeout(function(){gb.showMessage("Reloading!"),window.location.reload()},1250)}e.length?b(e):gb.showMessage("No .md files found!")}function d(a){alert(a.responseText||"Roh-roh. Something went wrong. :(")}var e={type:"GET",dataType:"json",url:"/import/dropbox",beforeSend:a,error:d,success:c};$.ajax(e)},fetchMarkdownFile:function(a){function b(a,b,c){if(c=JSON.parse(c.responseText),404===c.statusCode){var d=JSON.parse(c.data);gb.showMessage(d.error)}else $("#modal-generic").modal("hide"),A(W.current_filename),k(),R.getSession().setValue(c.data),z()}function d(){alert("Roh-roh. Something went wrong. :(")}var f=c(a),g=e(a);a=g+f;var h={type:"POST",dataType:"json",data:"mdFile="+a,url:"/fetch/dropbox",error:d,success:b};$.ajax(h)},setFilePath:function(a){a=e(a),d({dropbox:{filepath:a}})},putMarkdownFile:function(){function a(a,b,c){if(a=b=null,c=JSON.parse(c.responseText),c.statusCode>=204){var d=JSON.parse(c.data);gb.showMessage(d.error,5e3)}else $("#modal-generic").modal("hide"),gb.showMessage(gb.messages.docSavedDropbox)}function b(){alert("Roh-roh. Something went wrong. :(")}var c=encodeURIComponent(R.getSession().getValue()),d="pathToMdFile="+W.dropbox.filepath+encodeURIComponent(W.current_filename)+".md&fileContents="+c,e={type:"POST",dataType:"json",data:d,url:"/save/dropbox",error:b,success:a};$.ajax(e)}}}();l(),window.update_md=z}),window.onload=function(){var a=$("#loading");a.bind($.support.transitionEnd,function(){$("#main").removeClass("bye"),a.remove()}).addClass("fade_slow"),window.ace.edit("editor").session.on("changeScrollTop",syncPreview),$.getScript("http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML",function(){setTimeout(window.update_md,1e3)})};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment