1 Star 0 Fork 34

sky/outline.js

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

搜索帮助

0d507c66 1850385 C8b1a773 1850385