1 Star 0 Fork 34

liming0101/outline.js

forked from Yaohaixiao/outline.js 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
navigator.min.js 16.61 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).Navigator=e()}(this,(function(){"use strict";const t=t=>"string"==typeof t,e=t=>Object.prototype.toString.apply(t),i=t=>"function"==typeof t||"[object Function]"===e(t),s=t=>("[object Object]"===e(t)||"object"==typeof t||i(t))&&null!==t,r=t=>!!(s(t)&&t.nodeName&&t.tagName&&1===t.nodeType),n=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:300;return!!i(t)&&setTimeout((()=>{t()}),e)},l=["focusout","blur","focusin","focus","load","unload","mouseenter","mouseleave"],o=(t,e,i)=>{const s=l.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)},a=function(e,i){let s=arguments.length>2&&void 0!==arguments[2]&&arguments[2];const n=t(e)?document.querySelector(e):e,l=n.childNodes,h=((e,i)=>{let s=e._listeners||[];return t(i)&&i&&(s=s.filter((t=>t.type===i))),s})(n,i);h.forEach((t=>{o(n,t.type,t.fn)})),(s||!0===i||1===arguments.length)&&n&&l&&l.forEach((t=>{r(t)&&a(t,i,s)}))},h=(t,e,s)=>{if(!i(s))return a(t,e),!1;o(t,e,s)},c=function(t,e,s,r,n){let o=arguments.length>5&&void 0!==arguments[5]&&arguments[5];const a=l.indexOf(e)>-1,c=function(i){let l=n||t;!0===n&&(l=r),!0===o&&h(t,e,c),s.call(l,i,r)};if(!i(s))return!1;t._listeners||(t._listeners=[]),t._listeners.push({el:t,type:e,fn:c,data:r,context:n,capture:a}),s._delegateListener=c,t.addEventListener(e,c,a)},u=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))},d=t=>t.host&&t!==document&&t.host.nodeType?t.host:t.parentNode,p=function(t,e,i,s,r,n){let o=arguments.length>6&&void 0!==arguments[6]&&arguments[6];const a=l.indexOf(i)>-1,c=function(l){const a=function(t){const e=t.target;return(i=e)&&3===i.nodeType?i.parentNode:i;var i}(l),p=((t,e,i,s)=>{const r=i||document;if(!t)return null;do{if(null!=e&&(e.startsWith(">")?t.parentNode===r&&u(t,e):u(t,e))||s&&t===r)return t;if(t===r)break}while(t=d(t))})(a,e,t);let f=n||t;l.delegateTarget=p,!0===n&&(f=r),p&&(!0===o&&h(t,i,c),s.call(f,l,r))};if(!t)return!1;t._listeners||(t._listeners=[]),t._listeners.push({el:t,selector:e,type:i,fn:c,data:r,context:n,capture:a}),s._delegateListener=c,t.addEventListener(i,c,a)},f=t=>Array.isArray?Array.isArray(t):"[object Array]"===e(t),m=t=>{return!(!s(t)||!(r(t)||(t=>!(!s(t)||"[object NodeList]"!==e(t)))(t)||(i=t,s(i)&&"[object DocumentFragment]"===e(i))||(t=>!(!s(t)||!("[object Text]"===e(t)||t.tagName&&3===t.nodeType)))(t)));var i},g=(t,e)=>{const i=Object.prototype.hasOwnProperty;return t&&i.call(t,e)},v=(e,i,r)=>{const n=document.createDocumentFragment(),l=document.createElement(e),o=e=>m(e)||t(e),a=e=>{let i;if(!o(e))return!1;m(e)?i=e:t(e)&&(i=document.createTextNode(e)),n.appendChild(i)};return s(i)?((t,e)=>{if(!t||!s(e))return!1;Object.keys(e).forEach((i=>{const s=e[i];g(e,i)&&((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,i,s)}))})(l,i):f(i)&&i.every((t=>o(t)))?i.forEach((t=>{a(t)})):m(i)?a(i):t(i)&&a(document.createTextNode(i)),f(r)?r.forEach((t=>{a(t)})):a(r),l.appendChild(n),l},y=t=>t*t,b=function(){let e,i,s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;return s?t(s)?i=document.querySelector(s):r(s)&&(i=s):(e=document.querySelectorAll("html,body"),i=e[0].scrollTop-e[1].scrollTop>=0?e[0]:e[1]),i},$=(t,e,s)=>{const r=b(t);let n=r.scrollTop,l=0;const o=e-n,a=r.scrollHeight,h=e-a<=0?e:a,c=t=>(i(s)&&s(t),!1),u=()=>{if(l+=1,o<0){if(n-=y(l),r.scrollTop=n,n<=e)return r.scrollTop=e,c(e)}else if(n+=y(l),r.scrollTop=n,n>=h)return r.scrollTop=h,c(h);requestAnimationFrame(u)};requestAnimationFrame(u)},T=(t,e)=>{const i=new RegExp("(\\s|^)"+e+"(\\s|$)");let s,n;return!!r(t)&&(s=t.className,!!s&&(n=t.classList,n&&n.contains?t.classList.contains(e):!!i.exec(s)))},_=(t,e)=>{var i;let s,r;if(T(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)},x=(e,i)=>{var s;let r,n=e.className;if(!n||!T(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)},E=t=>{let e=t.offsetTop;return null!==t.offsetParent&&(e+=E(t.offsetParent)),e},k=(t,e)=>getComputedStyle(t)[e],S=(t,e)=>{document.documentElement.style.setProperty(t,e)},w=t=>{let e={};return null===t?null:(f(t)?e=Array.from(t):(e=Object.assign({},t),Object.keys(e).forEach((i=>e[i]=s(t[i])?w(t[i]):t[i]))),e)},C=[];let O,N;function j(t){for(;t.timeRemaining()>0&&C.length;){const t=C.shift();if(!i(t))return!1;t()}C.length?O=requestIdleCallback(j):(O=0,i(N)&&(N(),N=null))}void 0===window.requestIdleCallback&&(window.requestIdleCallback=function(t){const e=Date.now();return n((function(){t({didTimeout:!1,timeRemaining:function(){return Math.max(0,50-(Date.now()-e))}})}),10)},window.cancelIdleCallback=function(t){clearTimeout(t)});const A=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;C.push(t),i(e)&&(N=e),O||requestIdleCallback(j)},L={},I=t=>g(L,t)&&L[t].length>0,q=function(t){return!(arguments.length>1&&void 0!==arguments[1])||arguments[1]?I(t):(t=>{let e=I(t),i=t.lastIndexOf(".");for(;!e&&-1!==i;)i=(t=t.substring(0,i)).lastIndexOf("."),e=I(t);return e})(t)},z=function(t,i){let s=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];const r=t=>{if(!I(t))return!1;L[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(e(i))>-1?i.toString():i;t.callback.call(t.context||t,s)}))},n=()=>{let e=t,i=t.lastIndexOf(".");for(;-1!==i;)e=e.substring(0,i),i=e.lastIndexOf("."),r(e);r(t),r("*")};if(!q(t))return!1;s?setTimeout(n,10):n()},F=(()=>{let t=0;return e=>(t+=1,e?e+"-"+t:"guid-"+t)})(),H=t=>{if(!g(L,t))return!1;delete L[t]},D=(t,e)=>{if(!q(t))return!1;e?(t=>{const e=Object.keys(L);if(!t||e.length<1)return!1;e.forEach((e=>{const i=L[e];let s;i.forEach(((e,r)=>{e.callback!==t&&e.token!==t||(s=e.topic,i.splice(-1,r))})),i.length<1&&H(s)}))})(e):H(t)};class R{constructor(t){this.attrs={},t&&this.initialize(t)}initialize(t){return this.attr(t).render().addListeners(),this}attr(e,i){const r=this.attrs;return t(e)?i&&g(r,e)?(r[e]=i,this):r[e]:s(e)?(n=r,l=e,Object.keys(l).forEach((t=>{g(l,t)&&(n[t]=l[t])})),this):0===arguments.length?r:this;var n,l}render(){return this}refresh(){return this}destroy(){return this.removeListeners(),this}reload(t){let e=this.attr();return t&&(e=this.attr(t)),this.destroy().initialize(e),this}$emit(t,e){return z(t,e),this}$on(t,e){return function(t,e){let s=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;const r=F();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 D(t,e),this}addListeners(){return this}removeListeners(){return this}}class P extends R{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=w(P.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 s,n,l,o;return this.attr(e),s=this.attr("created"),n=this.attr("parentElement"),l=this.attr("scrollElement"),t(n)?o=document.querySelector(n):r(n)&&(o=n),this.$parentElement=o,this.$scrollElement=b(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()}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,a,h,c,u=null;return r?(this.isInside()&&e&&(u=v("h2",{className:"outline-navigator__title"},e),this.$title=u,l.push(u)),h=v("ul",{className:"outline-navigator__list"}),this.$list=h,n.push(h),i&&(c=v("div",{className:"outline-navigator__placeholder"}),this.$placeholder=c,n.push(c)),a=v("div",{className:"outline-navigator__main"},n),this.$main=a,l.push(a),o=v("nav",{id:"outline-navigator",className:"outline-navigator outline-navigator_hidden"},l),this.$el=o,this.isSticky()&&(this.calculateStickyHeight(),_(o,"outline-navigator_sticky")),s&&_(o,s),t.appendChild(o),r.appendChild(t),this):this}render(){const t=this.attr("mounted"),e=this.$parentElement,s=this.chapters,r=this.count();let l;return!e||s.length<1||(this.isInside()&&_(e,"outline-navigator-parent"),this._paintEdge(),l=this.$el,this._paint(s),n((()=>{this.highlight(this.active)}),60),this.offsetTop=E(l),this.offsetWidth=l.offsetWidth,this.isFixed()&&(this.sticky(),S("--outline-navigator-width",`${this.offsetWidth}px`)),i(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.code,o=e.rel,a=[],h=v("span",{className:"outline-navigator__text"},e.text);let c,u,d,p,f;i&&(u=v("span",{className:"outline-navigator__code","data-id":n},e.code),a.push(u)),a.push(h),c=v("a",{id:`chapter__anchor-${n}`,className:"outline-navigator__anchor",href:"#"+o,rel:o,"data-id":n,"data-code":l},a),d=v("li",{id:`chapter-${n}`,className:"outline-navigator__item","data-id":n,"data-code":l},c),-1===r?t.appendChild(d):(f=s(`chapter-${r}`),p=s(`subject-${r}`),p?p.appendChild(d):(p=v("ul",{id:"subject-"+r,className:"outline-navigator__subject"},d),f.appendChild(p)))}))};if(s>400)for(n(r.splice(0,400));r.length>0;){const t=r.splice(0,400);A((()=>{n(t)}))}else n(r)}(this.$list,t,e),x(i,"outline-navigator_hidden"),this}_remove(){return this.$parentElement.removeChild(this.$el),this}refresh(t){const e=this.$el;return x(e,"outline-navigator_hidden"),this.erase()._paint(t),this}_getPlaceholderOffset(t){const e=this.$main,i=this.$list,s=i.querySelector(".outline-navigator__anchor"),r=parseInt(k(e,"padding-top"),10),n=parseInt(k(e,"border-top-width"),10),l=parseInt(k(i,"padding-top"),10),o=parseInt(k(i,"margin-top"),10),a=parseInt(k(i,"border-top-width"),10);let h,c=0;return r&&(c+=r),l&&(c+=l),o&&(c+=o),n&&(c+=n),a&&(c+=a),h=s.offsetHeight*t,c+h}positionPlaceholder(t){const e=this.$list,i=this.$placeholder,s=e.querySelector(".outline-navigator__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-navigator_active",l="outline-navigator_highlight";let o="relative"===this.attr("position")?this.$parentElement:this.$main,a=null,h=0;return e?(a=e.querySelector(`#chapter__anchor-${t}`),a?(this.active=parseInt(a.getAttribute("data-id"),10),this.$active&&(x(this.$active,l),x(this.$active,s)),this.$active=a,_(this.$active,s),i?this.positionPlaceholder(this.active):_(this.$active,l),n((()=>{((t,e)=>{let i,s,n=!1;return r(t)&&r(e)?(i=e.getBoundingClientRect(),s=t.getBoundingClientRect(),console.log("parentRect",i),console.log("childRect",s),n=!!(s.top>=i.top&&s.right<=i.right&&s.bottom<=i.bottom&&s.left>=i.left),console.log("isInBounding",n),n):n})(this.$active,o)||(h=this._getPlaceholderOffset(this.active),$(o,h))})),this):this):this}sticky(){const t=this.attr("afterSticky"),e="outline-navigator_fixed",s=this.$el,r=this.offsetTop,n=this.$scrollElement.scrollTop;let l;return this.isFixed()?(l=n>=r,l?_(s,e):x(s,e),i(t)&&t.call(this,this.isClosed(),l),this):this}calculateStickyHeight(){const t=document.documentElement,e=Math.max(t.clientHeight||0,window.innerHeight||0);return S("--outline-sticky-height",`${e}px`),this}scrollTo(t,e){const i=this.$scrollElement;return $(i,t,e),this}show(){const t="outline-navigator_hidden",e=this.attr("afterOpened"),s=this.count(),r=this.$el,l=this.$parentElement;return this.isInside()?s>800?x(l,t):(x(l,t),n((()=>{x(l,"outline-navigator_folded")}),30)):x(r,t),this.closed=!1,i(e)&&e.call(this),this}hide(){const t="outline-navigator_hidden",e=this.attr("afterClosed"),s=this.count(),r=this.$el,l=this.$parentElement;return this.isInside()?s>800?_(l,t):(_(l,"outline-navigator_folded"),n((()=>{_(l,t)}))):_(r,t),this.closed=!0,i(e)&&e.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)&&n((()=>{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()._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),i(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 s=e.root||null,n=e.selector||".outline-heading",l=e.context||null,o={rootMargin:e.rootMargin||"0px 0px -90% 0px"},a=new IntersectionObserver((e=>{e.forEach((e=>{e.intersectionRatio>0&&i(t)&&t.call(l||e.target,e.target)}))}),o),h=r(s)?s:document;return s&&(o.root=s),h.querySelectorAll(n).forEach((t=>{a.observe(t)})),a}((t=>{const e=t.getAttribute("data-id");if(this.playing)return!1;this.observerTimer&&clearTimeout(this.observerTimer),this.observerTimer=n((()=>{this.highlight(e)}),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],o=document.querySelector(`#${l}`),a=E(o)-(e+10),h=this.$scrollElement.scrollHeight,c=this.attr("afterScroll"),u=()=>{i(c)&&c.call(this,"chapter"),n((()=>{this.playing=!1,this.$emit("toolbar:update",{top:a,min:0,max:h})}))};return this.playing=!0,this.isFixed()?(this.sticky(),n((()=>{this.scrollTo(a,u),this.highlight(r)}),10)):(this.scrollTo(a,u),this.highlight(r)),function(t){t.stopPropagation(),t.preventDefault()}(t),this}onScroll(){const t=this.$scrollElement;return this.scrollTimer&&clearTimeout(this.scrollTimer),this.scrollTimer=n((()=>{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=n((()=>{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),p(t,".outline-navigator__anchor","click",this.onSelect,this,!0),c(s,"scroll",this.onScroll,this,!0),this.isSticky()&&c(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),h(e,"click",this.onSelect),h(r,"scroll",this.onScroll),this.isSticky()&&h(window,"resize",this.onResize),this.$off("anchors:all:paint"),this.Observer&&document.querySelectorAll(t).forEach((t=>{this.Observer.unobserve(t)}))),this}}return P.DEFAULTS=w({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}),P}));
//# sourceMappingURL=navigator.min.js.map
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/liming0101/outline.js.git
git@gitee.com:liming0101/outline.js.git
liming0101
outline.js
outline.js
master

搜索帮助