1
0
DavidZhang73.github.io/js/obsidian.min.js

1 line
14 KiB
JavaScript

function scrollSpy(menuSelector,options){var menu=$(menuSelector);if(menu){var offset=(options=options||{}).offset||0,activeClassName=options.activeClassName||"active",scollTarget=$(".content :header").find("a.headerlink"),lastId=null,active=$();$(window).scroll((function(){var fromTop=$(this).scrollTop()+offset,id=scollTarget.filter((function(){return $(this).offset().top<fromTop})).last().parent().attr("id")||"";if(lastId!==id){active.removeClass(activeClassName);for(var newActive=[],target=menu.find("[href='#"+id+"']");target.length&&!target.is(menu);target=target.parent())target.is("li")&&newActive.push(target[0]);active=$(newActive).addClass(activeClassName).trigger("scrollspy"),lastId=id}}))}}function utiliseBgColor(){setTimeout((function(){$("#single").length?$("html").css("background","#fff"):$("html").css("background","#100e17")}),500)}function buildImgCaption(){var images;$(".content").find("img").each((function(){var caption=$(this).attr("alt");$(".content").find("[alt='"+caption+"']").parent().append('<p class="image-caption">"'+caption+'"</p>')}))}var Home=location.href,Pages=4,xhr,xhrUrl="",Obsidian={L:function(url,f,err){if(url==xhrUrl)return!1;xhrUrl=url,xhr&&xhr.abort(),xhr=$.ajax({type:"GET",url:url,timeout:1e4,success:function(data){f(data),xhrUrl=""},error:function(a,b,c){"abort"==b?err&&err():window.location.href=url,xhrUrl=""}})},P:function(){return!!("ontouchstart"in window)},PS:function(){window.history&&history.pushState&&(history.replaceState({u:Home,t:document.title},document.title,Home),window.addEventListener("popstate",(function(e){var state=e.state;state&&(document.title=state.t,state.u==Home?($("#preview").css("position","fixed"),setTimeout((function(){$("#preview").removeClass("show"),$("#container").show(),window.scrollTo(0,parseInt($("#container").data("scroll"))),setTimeout((function(){$("#preview").html(""),$(window).trigger("resize")}),300)}),0)):(Obsidian.loading(),Obsidian.L(state.u,(function(data){document.title=state.t,$("#preview").html($(data).filter("#single")),Obsidian.preview(),setTimeout((function(){Obsidian.player()}),0)}))))})))},HS:function(tag,flag){var id=tag.data("id")||0,url=tag.attr("href"),title=tag.attr("title")+" - "+$("#config-title").text();$("#preview").length&&window.history&&history.pushState||(location.href=url),Obsidian.loading();var state={d:id,t:title,u:url};Obsidian.L(url,(function(data){if($(data).filter("#single").length){switch(flag){case"push":history.pushState(state,title,url),$("#preview").html($(data).filter("#single"));break;case"replace":history.replaceState(state,title,url),$("#preview").html($(data).filter("#single"))}switch(document.title=title,$("#preview").html($(data).filter("#single")),flag){case"push":Obsidian.preview();break;case"replace":Obsidian.initArticleJs(),window.scrollTo(0,0),Obsidian.loaded()}setTimeout((function(){Obsidian.player(),$("#top").show(),comment=$("#gitalk-container"),1==comment.data("ae")&&comment.click()}),0)}else location.href=url}))},preview:function(){$("#preview").one("transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd",(function(){var previewVisible;$("#preview").hasClass("show")?$("#container").hide():$("#container").show(),Obsidian.loaded()})),setTimeout((function(){$("#preview").addClass("show"),$("#container").data("scroll",window.scrollY),setTimeout((function(){$("#preview").css({position:"static"}),console.log("preivew","initArticleJs"),Obsidian.initArticleJs()}),500)}),0)},player:function(){var p=$("#audio"),sourceSrc;p.length?(""==$("#audio source").eq(0).attr("src")&&""==p[0].src&&(audiolist=$("#audio-list li"),mp3=audiolist.eq([Math.floor(Math.random()*audiolist.size())]),p[0].src=mp3.data("url")),1==p.eq(0).data("autoplay")&&p[0].play(),p.on({timeupdate:function(){var progress=p[0].currentTime/p[0].duration*100;$(".bar").css("width",progress+"%"),p[0].volume=progress/5<=1?progress/5:1},ended:function(){$(".icon-pause").removeClass("icon-pause").addClass("icon-play")},playing:function(){$(".icon-play").removeClass("icon-play").addClass("icon-pause")}})):$(".icon-play").css({color:"#dedede",cursor:"not-allowed"})},loading:function(){var w=window.innerWidth,css='<style class="loaderstyle" id="loaderstyle'+w+'">@-moz-keyframes loader'+w+"{100%{background-position:"+w+"px 0}}@-webkit-keyframes loader"+w+"{100%{background-position:"+w+"px 0}}.loader"+w+"{-webkit-animation:loader"+w+" 3s linear infinite;-moz-animation:loader"+w+" 3s linear infinite;}</style>";$(".loaderstyle").remove(),$("head").append(css),$("#loader").removeClass().addClass("loader"+w).show()},loaded:function(){$("#loader").removeClass().hide()},F:function(id,w,h){var _height=$(id).parent().height(),_width=$(id).parent().width(),ratio=h/w;_height/_width>ratio?(id.style.height=_height+"px",id.style.width=_height/ratio+"px"):(id.style.width=_width+"px",id.style.height=_width*ratio+"px"),id.style.left=(_width-parseInt(id.style.width))/2+"px",id.style.top=(_height-parseInt(id.style.height))/2+"px"},initArticleJs:function(){document.querySelectorAll("pre code").forEach(block=>{hljs.highlightBlock(block)}),Obsidian.setCodeRowWithLang(),Obsidian.tocSpy(200),initValine(),buildImgCaption(),utiliseBgColor("article")},setCodeRowWithLang:function(){let code=$("code");code&&code.length&&code.each((function(){var item=$(this),lang="",langMap={html:"HTML",xml:"XML",svg:"SVG",mathml:"MathML",css:"CSS",clike:"C-like",js:"JavaScript",abap:"ABAP",apacheconf:"Apache Configuration",apl:"APL",arff:"ARFF",asciidoc:"AsciiDoc",adoc:"AsciiDoc",asm6502:"6502 Assembly",aspnet:"ASP.NET (C#)",autohotkey:"AutoHotkey",autoit:"AutoIt",shell:"BASH",bash:"BASH",basic:"BASIC",csharp:"C#",dotnet:"C#",cpp:"C++",cil:"CIL",csp:"Content-Security-Policy","css-extras":"CSS Extras",django:"Django/Jinja2",jinja2:"Django/Jinja2",dockerfile:"Docker",erb:"ERB",fsharp:"F#",gcode:"G-code",gedcom:"GEDCOM",glsl:"GLSL",gml:"GameMaker Language",gamemakerlanguage:"GameMaker Language",graphql:"GraphQL",hcl:"HCL",http:"HTTP",hpkp:"HTTP Public-Key-Pins",hsts:"HTTP Strict-Transport-Security",ichigojam:"IchigoJam",inform7:"Inform 7",javastacktrace:"Java stack trace",json:"JSON",jsonp:"JSONP",latex:"LaTeX",emacs:"Lisp",elisp:"Lisp","emacs-lisp":"Lisp",lolcode:"LOLCODE","markup-templating":"Markup templating",matlab:"MATLAB",mel:"MEL",n1ql:"N1QL",n4js:"N4JS",n4jsd:"N4JS","nand2tetris-hdl":"Nand To Tetris HDL",nasm:"NASM",nginx:"nginx",nsis:"NSIS",objectivec:"Objective-C",ocaml:"OCaml",opencl:"OpenCL",parigp:"PARI/GP",objectpascal:"Object Pascal",php:"PHP","php-extras":"PHP Extras",plsql:"PL/SQL",powershell:"PowerShell",properties:".properties",protobuf:"Protocol Buffers",q:"Q (kdb+ database)",jsx:"React JSX",tsx:"React TSX",renpy:"Ren'py",rest:"reST (reStructuredText)",sas:"SAS",sass:"SASS (Sass)",scss:"SASS (Scss)",sql:"SQL",soy:"Soy (Closure Template)",tap:"TAP",toml:"TOML",tt2:"Template Toolkit 2",ts:"TypeScript",vbnet:"VB.Net",vhdl:"VHDL",vim:"vim","visual-basic":"Visual Basic",vb:"Visual Basic",wasm:"WebAssembly",wiki:"Wiki markup",xeoracube:"XeoraCube",xojo:"Xojo (REALbasic)",xquery:"XQuery",yaml:"YAML"},displayLangText="";displayLangText=(lang=item[0].className.indexOf(" ")>-1?item[0].className.split(" ")[0]:item[0].className)in langMap?langMap[lang]:lang,item.find(".language-mark").length<=0&&item.prepend('<span class="language-mark" ref='+lang+">"+displayLangText+"</span>")}))},tocSpy:function(offset){var tocContainer=$("#toc"),toc=tocContainer,tocHeight=toc.height();scrollSpy(tocContainer,{offset:200}),$(".toc-item").on("scrollspy",(function(){var tocTop=toc.scrollTop(),link=$(this).children(".toc-link"),thisTop=link.position().top;$(this).height()==link.height()&&(thisTop<=0?toc.scrollTop(tocTop+thisTop):tocHeight<=thisTop&&toc.scrollTop(tocTop+thisTop+link.outerHeight()-tocHeight))}))}};$((function(){Obsidian.P()&&$("body").addClass("touch"),$("#preview").length?(Obsidian.PS(),$(".pview a").addClass("pviewa"),setTimeout((function(){$("html, body").removeClass("loading")}),500)):($("#single").css("min-height",window.innerHeight),setTimeout((function(){$("html, body").removeClass("loading")}),500),window.addEventListener("popstate",(function(e){e.state&&(location.href=e.state.u)})),Obsidian.player(),$(".icon-icon, .image-icon").attr("href","/"),$("#top").show()),(()=>{"use strict";let refOffset=0,articleRefOffset=0;const articleMenuHeight=51,menuHeight=70,header=document.querySelector("#header"),handler=()=>{const newOffset=window.scrollY||window.pageYOffset;$("#header").length&&!$(".scrollbar").length&&(newOffset>70?(newOffset>refOffset?(header.classList.remove("animateIn"),header.classList.add("animateOut")):(header.classList.remove("animateOut"),header.classList.add("animateIn")),header.style.paddingTop="20px",header.style.background="rgba(16,14,23,.86)",header.style.borderBottom="1px solid #201c29",refOffset=newOffset):(header.style.paddingTop="70px",header.style.background="transparent",header.style.borderBottom="0px"));const topHeader=document.querySelector("#top");if(topHeader&&$(".scrollbar").length&&!Obsidian.P()&&!$(".icon-images").hasClass("active")){newOffset>51?(newOffset>articleRefOffset?(topHeader.classList.remove("animateIn"),topHeader.classList.add("animateOut"),$(".subtitle").fadeOut()):(topHeader.classList.remove("animateOut"),topHeader.classList.add("animateIn"),$(".subtitle").fadeIn()),articleRefOffset=newOffset):$(".subtitle").fadeOut();var wt=$(window).scrollTop(),tw,dh,wh,width=$("#top").width()/(document.body.scrollHeight-$(window).height())*wt;$(".scrollbar").width(width)}var scrollTop=$(window).scrollTop(),docHeight=$(document).height(),winHeight=$(window).height(),winWidth=$(window).width(),scrollPercent=scrollTop/(docHeight-winHeight),scrollPercentRounded=Math.round(100*scrollPercent),backToTopState=$("#back-to-top").css("display");$("#back-to-top").find(".percentage").html(scrollPercentRounded+"%"),$("#back-to-top").find(".flow").css("height",scrollPercentRounded+"%"),winWidth>=920&&(scrollPercentRounded>10?"none"===backToTopState&&($("#back-to-top").removeClass("fadeOutRight"),$("#back-to-top").addClass("fadeInRight"),$("#back-to-top").css("display","block")):"block"===backToTopState&&(setTimeout((function(){$("#back-to-top").css("display","none")}),400),$("#back-to-top").removeClass("fadeInRight"),$("#back-to-top").addClass("fadeOutRight")))};window.addEventListener("scroll",handler,!1)})($),$(window).on("touchmove",(function(e){$("body").hasClass("mu")&&e.preventDefault()})),$("body").on("click",(function(e){var tag=$(e.target).attr("class")||"",rel=$(e.target).attr("rel")||"";if("IMG"==e.target.nodeName&&$(e.target).parents("div.content").length>0&&(tag="pimg"),tag||rel)switch(!0){case-1!=tag.indexOf("icon-top02"):$("html,body").animate({scrollTop:0},300);break;case-1!=tag.indexOf("switchmenu"):return window.scrollTo(0,0),$("html, body").toggleClass("mu"),!1;case-1!=tag.indexOf("more"):if("loading"==(tag=$(".more")).data("status"))return!1;var num=parseInt(tag.data("page"))||1;if(1==num&&tag.data("page",1),num>=Pages)return;return tag.html("加载中...").data("status","loading"),Obsidian.loading(),Obsidian.L(tag.attr("href"),(function(data){var link=$(data).find(".more").attr("href");null!=link?(tag.attr("href",link).html("加载更多").data("status","loaded"),tag.data("page",parseInt(tag.data("page"))+1)):$("#pager").remove();var tempScrollTop=$(window).scrollTop();$("#primary").append($(data).find(".post")),$(window).scrollTop(tempScrollTop+100),Obsidian.loaded(),$("html,body").animate({scrollTop:tempScrollTop+400},500),document.querySelectorAll("pre code").forEach(block=>{hljs.highlightBlock(block)}),Obsidian.setCodeRowWithLang(),initValine()}),(function(){tag.html("加载更多").data("status","loaded")})),!1;case-1!=tag.indexOf("icon-home"):return $(".toc").fadeOut(100),$("#preview").hasClass("show")?history.back():location.href=$(".icon-home").data("url"),!1;case-1!=tag.indexOf("icon-scan"):return $(".icon-scan").hasClass("tg")?$("#qr").toggle():($(".icon-scan").addClass("tg"),$("#qr").qrcode({width:128,height:128,text:location.href}).toggle()),!1;case-1!=tag.indexOf("icon-play"):return $("#audio")[0].play(),$(".icon-play").removeClass("icon-play").addClass("icon-pause"),!1;case-1!=tag.indexOf("icon-pause"):return $("#audio")[0].pause(),$(".icon-pause").removeClass("icon-pause").addClass("icon-play"),!1;case-1!=tag.indexOf("posttitle"):case-1!=tag.indexOf("menu-link"):return Obsidian.HS($(e.target),"push"),!1;case"prev"==rel||"next"==rel:if("prev"==rel)var t=$("#prev_next a")[0].text;else var t=$("#prev_next a")[1].text;return $(e.target).attr("title",t),Obsidian.HS($(e.target),"replace"),!1;case-1!=tag.indexOf("toc-text")||-1!=tag.indexOf("toc-link")||-1!=tag.indexOf("toc-number"):return hash="","SPAN"==e.target.nodeName?hash=$(e.target).parent().attr("href"):hash=$(e.target).attr("href"),to=$(".content :header").find("[href="+hash+"]"),$("html,body").animate({scrollTop:to.offset().top-80},300),!1;case-1!=tag.indexOf("pviewa"):return $("body").removeClass("mu"),setTimeout((function(){Obsidian.HS($(e.target),"push"),$(".toc").fadeIn(1e3)}),300),!1;case-1!=tag.indexOf("pimg"):var pswpElement=$(".pswp").get(0);if(pswpElement){var items=[],index=0,imgs=[],options,lightBox;$(".content img").each((function(i,v){e.target.src==v.src&&(index=i);var item={src:v.src,w:v.naturalWidth,h:v.naturalHeight};imgs.push(v),items.push(item)})),new PhotoSwipe(pswpElement,PhotoSwipeUI_Default,items,{index:index,shareEl:!1,zoomEl:!1,allowRotationOnUserZoom:!0,history:!1,getThumbBoundsFn:function(index){var thumbnail=imgs[index],pageYScroll=window.pageYOffset||document.documentElement.scrollTop,rect=thumbnail.getBoundingClientRect();return{x:rect.left,y:rect.top+pageYScroll,w:rect.width}}}).init()}return!1;case-1!=tag.indexOf("comment"):return Obsidian.loading(),comment=$("#gitalk-container"),gitalk=new Gitalk({clientID:comment.data("ci"),clientSecret:comment.data("cs"),repo:comment.data("r"),owner:comment.data("o"),admin:comment.data("a"),id:decodeURI(window.location.pathname),distractionFreeMode:comment.data("d")}),$(".comment").removeClass("link"),gitalk.render("gitalk-container"),Obsidian.loaded(),!1;default:return!0}})),comment=$("#gitalk-container"),1==comment.data("ae")&&comment.click(),$(".article").length&&(Obsidian.tocSpy(200),buildImgCaption()),window.onpopstate=function(event){utiliseBgColor()},utiliseBgColor(),initialTyped(),Obsidian.setCodeRowWithLang(),console.log("%c Github %c","background:#24272A; color:#73ddd7","","https://github.com/TriDiamond/hexo-theme-obsidian")}));