1 Star 0 Fork 34

lvhaiyan888/outline.js

forked from Yaohaixiao/outline.js 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
chapters.min.js 13.50 KB
一键复制 编辑 原始数据 按行查看 历史
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Chapters=e()}(this,(function(){"use strict";const t=t=>"string"==typeof t,e=(t,e)=>{const s=Object.prototype.hasOwnProperty;return t&&s.call(t,e)},s=t=>Object.prototype.toString.apply(t),i=t=>"function"==typeof t||"[object Function]"===s(t),r=t=>("[object Object]"===s(t)||"object"==typeof t||i(t))&&null!==t,l={},n=t=>e(l,t)&&l[t].length>0,o=(t,e=!0)=>e?n(t):(t=>{let e=n(t),s=t.lastIndexOf(".");for(;!e&&-1!==s;)s=(t=t.substring(0,s)).lastIndexOf("."),e=n(t);return e})(t),a=(t,e,i=!0)=>{const r=t=>{if(!n(t))return!1;l[t].forEach((t=>{const i=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]","[object BigInt64Array]","[object BigUint64Array]"].indexOf(s(e))>-1?e.toString():e;t.callback.call(t.context||t,i)}))},a=()=>{let e=t,s=t.lastIndexOf(".");for(;-1!==s;)e=e.substring(0,s),s=e.lastIndexOf("."),r(e);r(t),r("*")};if(!o(t))return!1;i?setTimeout(a,10):a()},c=(()=>{let t=0;return e=>(t+=1,e?e+"-"+t:"guid-"+t)})(),h=t=>{if(!e(l,t))return!1;delete l[t]},u=(t,e)=>{if(!o(t))return!1;e?(t=>{const e=Object.keys(l);if(!t||e.length<1)return!1;e.forEach((e=>{const s=l[e];let i;s.forEach(((e,r)=>{e.callback!==t&&e.token!==t||(i=e.topic,s.splice(-1,r))})),s.length<1&&h(i)}))})(e):h(t)};class d{constructor(t){this.attrs={},t&&this.initialize(t)}initialize(t){return this.attr(t).render().addListeners(),this}attr(s,i){const l=this.attrs;return t(s)?i&&e(l,s)?(l[s]=i,this):l[s]:r(s)?(n=l,o=s,Object.keys(o).forEach((t=>{e(o,t)&&(n[t]=o[t])})),this):0===arguments.length?l:this;var n,o}render(){return this}destroy(){return this.removeListeners(),this}reload(t){return this.destroy().initialize(this.attr(t)),this}$emit(t,e){return a(t,e),this}$on(t,e){return((t,e,s=null)=>{const r=c();let n="symbol"==typeof t?t.toString():t;i(e)&&(l[n]||(l[n]=[]),l[n].push({topic:n,callback:e,context:s,token:r}))})(t,e,this),this}$off(t,e){return u(t,e),this}addListeners(){return this}removeListeners(){return this}}const p=t=>!!(r(t)&&t.nodeName&&t.tagName&&1===t.nodeType),m=(t,e=300)=>!!i(t)&&setTimeout((()=>{t()}),e),f=["focusout","blur","focusin","focus","load","unload","mouseenter","mouseleave"],y=(t,e,s)=>{const i=f.indexOf(e)>-1;s._delegateListener&&delete(s=s._delegateListener)._delegateListener,function(t,e,s){const i=t._listeners;let r=-1;if(i.length<1)return!1;i.forEach(((t,i)=>{const l=t.fn;e===t.type&&(r=i,l===s&&(r=i))})),r>-1&&i.splice(r,1)}(t,e,s),t.removeEventListener(e,s,i)},g=function(e,s,i=!1){const r=t(e)?document.querySelector(e):e,l=r.childNodes,n=((e,s)=>{let i=e._listeners||[];return t(s)&&s&&(i=i.filter((t=>t.type===s))),i})(r,s);n.forEach((t=>{y(r,t.type,t.fn)})),(i||!0===s||1===arguments.length)&&r&&l&&l.forEach((t=>{p(t)&&g(t,s,i)}))},b=(t,e,s)=>{if(!i(s))return g(t,e);y(t,e,s)},$=(t,e,s,r,l,n=!1)=>{const o=f.indexOf(e)>-1,a=function(i){let o=l||t;!0===l&&(o=r),!0===n&&b(t,e,a),s.call(o,i,r)};if(!i(s))return!1;t._listeners||(t._listeners=[]),t._listeners.push({el:t,type:e,fn:a,data:r,context:l,capture:o}),s._delegateListener=a,t.addEventListener(e,a,o)},T=(t,e="")=>{const s=e.replace(/^>/i,"");return!!(e&&s&&t)&&(t.matches?t.matches(s):!!t.msMatchesSelector&&t.msMatchesSelector(s))},_=t=>t.host&&t!==document&&t.host.nodeType?t.host:t.parentNode,v=(t,e,s,i,r,l,n=!1)=>{const o=f.indexOf(s)>-1,a=function(o){const c=function(t){const e=t.target;return(s=e)&&3===s.nodeType?s.parentNode:s;var s}(o),h=((t,e,s,i)=>{const r=s||document;if(!t)return null;do{if(null!=e&&(e.startsWith(">")?t.parentNode===r&&T(t,e):T(t,e))||i&&t===r)return t;if(t===r)break}while(t=_(t))})(c,e,t);let u=l||t;o.delegateTarget=h,!0===l&&(u=r),h&&(!0===n&&b(t,s,a),i.call(u,o,r))};t._listeners||(t._listeners=[]),t._listeners.push({el:t,selector:e,type:s,fn:a,data:r,context:l,capture:o}),i._delegateListener=a,t.addEventListener(s,a,o)},E=t=>Array.isArray?Array.isArray(t):"[object Array]"===s(t),x=t=>{return!(!r(t)||!(p(t)||(t=>!(!r(t)||"[object NodeList]"!==s(t)))(t)||(e=t,r(e)&&"[object DocumentFragment]"===s(e))||(t=>!(!r(t)||!("[object Text]"===s(t)||t.tagName&&3===t.nodeType)))(t)));var e},S=(s,i,l)=>{const n=document.createDocumentFragment(),o=document.createElement(s),a=e=>x(e)||t(e),c=e=>{let s;if(!a(e))return!1;x(e)?s=e:t(e)&&(s=document.createTextNode(e)),n.appendChild(s)};return r(i)?Object.keys(i).forEach((t=>{e(i,t)&&((t,e,s)=>{let i=t.tagName.toLowerCase();switch(e){case"style":t.style.cssText=s;break;case"value":"input"===i||"textarea"===i?t.value=s:t.setAttribute(e,s);break;case"className":t.className=s;break;default:t.setAttribute(e,s)}})(o,t,i[t])})):E(i)&&i.every((t=>a(t)))&&i.forEach((t=>{c(t)})),E(l)?l.forEach((t=>{c(t)})):c(l),o.appendChild(n),o},k=t=>t*t,j=(e=null)=>{let s,i;return e?t(e)?i=document.querySelector(e):p(e)&&(i=e):(s=document.querySelectorAll("html,body"),i=s[0].scrollTop-s[1].scrollTop>=0?s[0]:s[1]),i},A=(t,e)=>{const s=new RegExp("(\\s|^)"+e+"(\\s|$)");let i,r;return!!p(t)&&(i=t.className,!!i&&(r=t.classList,r?.contains?t.classList.contains(e):!!s.exec(i)))},N=(t,e)=>{let s,i;if(A(t,e))return!1;s=t.classList,s?.add?s.add(e):(i=t.className,i+=i.length>0?" "+e:e,t.className=i)},O=(e,s)=>{let i,r=e.className;if(!r||!A(e,s))return!1;var l;i=e.classList,i?.remove?i.remove(s):(l=r.replace(s,""),r=!!t(l)&&l.replace(/(^\s+)|(\s+$)/g,""),e.className=r)},L=t=>{let e=t.offsetTop;return null!==t.offsetParent&&(e+=L(t.offsetParent)),e},w=(t,e)=>getComputedStyle(t)[e],C=(t,e)=>{document.documentElement.style.setProperty(t,e)},I=t=>{let e;return null===t?null:(e=Object.assign({},t),Object.keys(e).forEach((s=>e[s]="object"==typeof t[s]?I(t[s]):t[s])),E(t)?(e.length=t.length,Array.from(e)):e)};class z extends d{constructor(t){super(),this.attrs=I(z.DEFAULTS),this._reset(),this.offsetWidth=0,this.playing=!1,this.scrollTimer=null,this.resizeTimer=null,this.Observer=null,t&&this.initialize(t)}_reset(){return this.$el=null,this.$title=null,this.$main=null,this.$list=null,this.$placeholder=null,this.$parentElement=null,this.$scrollElement=null,this.$active=null,this.chapters=[],this.active=0,this.offsetTop=0,this.closed=!1,this}initialize(e){let s,r,l,n;return this.attr(e),s=this.attr("created"),r=this.attr("parentElement"),l=this.attr("scrollElement"),t(r)?n=document.querySelector(r):p(r)&&(n=r),this.$parentElement=n,this.$scrollElement=j(l),this.chapters=this.attr("chapters"),this.closed=this.attr("closed"),this.active=this.attr("active"),i(s)&&s.call(this),this.chapters.length<1||(this.render().addListeners(),this.$active=document.querySelector(`#chapter-${this.active}`)),this}isClosed(){return this.closed}isSticky(){return"sticky"===this.attr("position")}isFixed(){return"fixed"===this.attr("position")}isInside(){return this.isFixed()||this.isSticky()}isOutside(){return!this.isInside()}_paintEdge(){const t=document.createDocumentFragment(),e=this.attr("title"),s=this.attr("customClass"),i=this.$parentElement,r=[];let l,n,o,a,c=null;return i?(this.isInside()&&e&&(c=S("h2",{className:"outline-chapters__title"},e),this.$title=c,r.push(c)),o=S("ul",{className:"outline-chapters__list"}),this.$list=o,a=S("div",{className:"outline-chapters__placeholder"}),this.$placeholder=a,n=S("div",{className:"outline-chapters__main"},[o,a]),this.$main=n,r.push(n),l=S("nav",{id:"outline-chapters",className:"outline-chapters outline-chapters_hidden"},r),this.$el=l,this.isSticky()&&(this.calculateStickyHeight(),N(l,"outline-chapters_sticky")),s&&N(l,s),t.appendChild(l),i.appendChild(t),this):this}render(){const t=this.attr("showCode"),e=this.attr("mounted"),s=this.$parentElement,r=this.chapters;let l,n;return!s||r.length<1||(this._paintEdge(),l=this.$el,n=this.$list,((t,e,s=!1)=>{const i=e=>t.querySelector(`#${e}`);e.forEach((e=>{const r=e.pid,l=e.id,n=e.rel,o=[],a=S("span",{className:"outline-chapters__text"},e.text);let c,h,u,d,p;s&&(h=S("span",{className:"outline-chapters__code","data-id":l},e.code),o.push(h)),o.push(a),c=S("a",{id:`chapter__anchor-${l}`,className:"outline-chapters__anchor",href:"#"+n,rel:n,"data-id":l},o),u=S("li",{id:`chapter-${l}`,className:"outline-chapters__item","data-id":l},c),-1===r?t.appendChild(u):(p=i(`chapter-${r}`),d=i(`subject-${r}`),d?d.appendChild(u):(d=S("ul",{id:"subject-"+r,className:"outline-chapters__subject"},u),p.appendChild(d)))}))})(n,r,t),O(l,"outline-chapters_hidden"),this.positionPlaceholder(this.active),this.offsetTop=L(l),this.offsetWidth=l.offsetWidth,this.isFixed()&&(this.sticky(),C("--outline-chapters-width",`${this.offsetWidth}px`)),i(e)&&e.call(this),this.onObserver()),this}positionPlaceholder(t){const e=this.$main,s=this.$list,i=this.$placeholder,r=s.querySelector(".outline-chapters__anchor"),l=parseInt(w(e,"padding-top"),10),n=parseInt(w(e,"border-top-width"),10),o=parseInt(w(s,"padding-top"),10),a=parseInt(w(s,"margin-top"),10),c=parseInt(w(s,"border-top-width"),10);let h,u=r.offsetHeight,d=0;return l&&(d+=l),o&&(d+=o),a&&(d+=a),n&&(d+=n),c&&(d+=c),h=u*t,i.style.cssText=`transform: translateY(${d+h}px);height:${u}px;`,this}highlight(t){const e=this.$el.querySelector(`#chapter__anchor-${t}`),s="outline-chapters_active";return e?(this.$active&&O(this.$active,s),this.active=parseInt(e.getAttribute("data-id"),10),this.$active=e,N(this.$active,s),this.positionPlaceholder(this.active),this):this}sticky(){const t=this.attr("afterSticky"),e="outline-chapters_fixed",s=this.$el,r=this.offsetTop,l=this.$scrollElement.scrollTop;let n;return this.isFixed()?(n=l>=r,n?N(s,e):O(s,e),i(t)&&t.call(this,this.isClosed(),n),this):this}calculateStickyHeight(){const t=document.documentElement,e=Math.max(t.clientHeight||0,window.innerHeight||0);return C("--outline-sticky-height",`${e}px`),this}scrollTo(t,e){return((t,e,s)=>{const r=j(t);let l=r.scrollTop,n=0;const o=e-l,a=r.scrollHeight,c=e-a<=0?e:a,h=t=>(i(s)&&s(t),!1),u=()=>{if(n+=1,o<0){if(l-=k(n),r.scrollTop=l,l<=e)return r.scrollTop=e,h(e)}else if(l+=k(n),r.scrollTop=l,l>=c)return r.scrollTop=c,h(c);requestAnimationFrame(u)};requestAnimationFrame(u)})(this.$scrollElement,t,e),this}show(){const t="outline-chapters_folded",e="outline-chapters_hidden",s=this.attr("afterOpened"),r=this.$el,l=this.$parentElement;return this.isInside()?(O(l,e),O(r,e),m((()=>{O(l,t),O(r,t)}),30)):O(r,e),this.closed=!1,i(s)&&s.call(this),this}hide(){const t="outline-chapters_folded",e="outline-chapters_hidden",s=this.attr("afterClosed"),r=this.$el,l=this.$parentElement;return this.isInside()?(N(l,t),N(r,t),m((()=>{N(l,e),N(r,e)}))):N(r,e),this.closed=!0,i(s)&&s.call(this),this}toggle(){const t=this.attr("afterToggle"),e=this.offsetTop,s=this.$scrollElement.scrollTop;let r;return this.isClosed()?this.show():this.hide(),i(t)&&m((()=>{r=s>=e,t.call(this,this.isClosed(),r)})),this}destroy(){const t=this.attr("beforeDestroy"),e=this.attr("afterDestroy");return i(t)&&t.call(this),this.removeListeners(),this.$parentElement.removeChild(this.$el),this.attr(z.DEFAULTS)._reset(),this.scrollTimer&&(clearTimeout(this.scrollTimer),this.scrollTimer=null),this.resizeTimer&&(clearTimeout(this.resizeTimer),this.resizeTimer=null),i(e)&&e.call(this),this.Observer&&(this.Observer=null),this}onObserver(){const t=this.attr("selector");let e=null;return this.Observer=((t,e={})=>{const s=e.root||null,r=e.selector||".outline-heading",l=e.context||null,n={rootMargin:e.rootMargin||"0px 0px -90% 0px"},o=new IntersectionObserver((e=>{e.forEach((e=>{e.intersectionRatio>0&&i(t)&&t.call(l||e.target,e.target)}))}),n),a=p(s)?s:document;return s&&(n.root=s),a.querySelectorAll(r).forEach((t=>{o.observe(t)})),o})((t=>{const s=t.getAttribute("data-id");if(this.playing)return!1;e&&clearTimeout(e),e=m((()=>{this.highlight(s)}),100)}),{selector:t,context:this}),this}onSelect(t){const e=this.attr("stickyHeight"),s=t.delegateTarget,r=s.getAttribute("data-id"),l=s.href.split("#")[1],n=document.querySelector(`#${l}`),o=L(n)-(e+10),a=this.$scrollElement.scrollHeight,c=this.attr("afterScroll"),h=()=>{i(c)&&c.call(this,"chapter"),m((()=>{this.playing=!1,this.$emit("toolbar:update",{top:o,min:0,max:a})}))};return this.playing=!0,this.isFixed()?(this.sticky(),m((()=>{this.scrollTo(o,h),this.highlight(r)}),10)):(this.scrollTo(o,h),this.highlight(r)),function(t){t.stopPropagation(),t.preventDefault()}(t),this}onScroll(){const t=this.$scrollElement;return this.scrollTimer&&clearTimeout(this.scrollTimer),this.scrollTimer=m((()=>{const e=t.scrollTop,s=t.scrollHeight-t.clientHeight;this.isFixed()&&this.sticky(),this.$emit("toolbar:update",{top:e,min:0,max:s})}),100),this}onResize(){return this.resizeTimer&&clearTimeout(this.resizeTimer),this.resizeTimer=m((()=>{this.calculateStickyHeight()})),this}addListeners(){const t=this.$el,e=this.$scrollElement,s=e.tagName.toLowerCase();let i=e;return"html"!==s&&"body"!==s||(i=window),v(t,".outline-chapters__anchor","click",this.onSelect,this,!0),$(i,"scroll",this.onScroll,this,!0),this.isSticky()&&$(window,"resize",this.onResize,this,!0),this}removeListeners(){const t=this.attr("selector"),e=this.$el,s=this.$scrollElement,i=s.tagName.toLowerCase();let r=s;return"html"!==i&&"body"!==i||(r=window),b(e,"click",this.onSelect),b(r,"scroll",this.onScroll),this.isSticky()&&$(window,"resize",this.onResize),this.Observer&&document.querySelectorAll(t).forEach((t=>{this.Observer.unobserve(t)})),this}}return z.DEFAULTS={parentElement:"",scrollElement:"",selector:".outline-heading",active:0,closed:!1,showCode:!0,position:"relative",stickyHeight:0,chapters:[],created:null,mounted:null,afterClosed:null,afterOpened:null,afterScroll:null,beforeDestroy:null,afterDestroy:null,afterSticky:null},z}));
//# sourceMappingURL=chapters.min.js.map
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/lvhaiyan/outline.js.git
[email protected]:lvhaiyan/outline.js.git
lvhaiyan
outline.js
outline.js
master

搜索帮助