Files
ALC27/mathjax/ui/lazy.js
2025-12-22 12:20:15 +08:00

1 line
6.2 KiB
JavaScript

(()=>{"use strict";var t={d:(e,s)=>{for(var a in s)t.o(s,a)&&!t.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:s[a]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{LAZYID:()=>c,LazyHandler:()=>d,LazyList:()=>h,LazyMathDocumentMixin:()=>u,LazyMathItemMixin:()=>z});const s=("undefined"!=typeof window?window:global).MathJax._.components.global,a=(s.GLOBAL,s.isObject,s.combineConfig,s.combineDefaults,s.combineWithMathJax),i=(s.MathJax,MathJax._.core.MathItem),l=(i.protoItem,i.AbstractMathItem,i.STATE),r=i.newState,n=MathJax._.util.Retries,o=n.handleRetriesFor;n.retryAfter;var y=function(t,e,s,a){return new(s||(s=Promise))((function(i,l){function r(t){try{o(a.next(t))}catch(t){l(t)}}function n(t){try{o(a.throw(t))}catch(t){l(t)}}function o(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(r,n)}o((a=a.apply(t,e||[])).next())}))};class h{constructor(){this.id=0,this.items=new Map}add(t){const e=String(this.id++);return this.items.set(e,t),e}get(t){return this.items.get(t)}delete(t){return this.items.delete(t)}}r("LAZYALWAYS",l.FINDMATH+3);const c="data-mjx-lazy";function z(t){return class extends t{constructor(...t){super(...t),this.lazyCompile=!0,this.lazyTypeset=!0,this.lazyTex=!1,this.attachSpeech=t=>{var e;this.state()>=l.ATTACHSPEECH||(this.lazyTypeset||null===(e=super.attachSpeech)||void 0===e||e.call(this,t),this.state(l.ATTACHSPEECH))},this.end.node||(this.lazyCompile=this.lazyTypeset=!1)}compile(t){this.lazyCompile?this.state()<l.COMPILED&&(this.lazyTex="TeX"===this.inputJax.name,this.root=t.mmlFactory.create("math"),this.state(l.COMPILED)):super.compile(t)}state(t=null,e=!1){return null!==e&&super.state(t,e),super.state()}typeset(t){if(this.lazyTypeset){if(this.state()<l.TYPESET){const e=t.adaptor;if(!this.lazyMarker){const s=t.lazyList.add(this);this.lazyMarker=e.node("mjx-lazy",{[c]:s}),this.typesetRoot=e.node("mjx-container",{},[this.lazyMarker])}this.state(l.TYPESET)}}else super.typeset(t)}updateDocument(t){super.updateDocument(t),this.lazyTypeset&&t.lazyObserver.observe(this.lazyMarker)}}}function u(t){var e;return(e=class extends t{constructor(...t){super(...t),this.lazyAlwaysContainers=null,this.lazyAlwaysIndex=0,this.lazyPromise=Promise.resolve(),this.lazyIdle=!1,this.lazySet=new Set,this.options.MathItem=z(this.options.MathItem);const e=this.constructor.ProcessBits;e.has("lazyAlways")||e.allocate("lazyAlways"),this.lazyObserver=new IntersectionObserver(this.lazyObserve.bind(this),{rootMargin:this.options.lazyMargin}),this.lazyList=new h;const s=this.lazyHandleSet.bind(this);if(this.lazyProcessSet=window&&window.requestIdleCallback?()=>window.requestIdleCallback(s):()=>setTimeout(s,10),window){let t=!1;const e=()=>{t||this.lazyTypesetAll(),t=!0};window.matchMedia("print").addListener(e),window.addEventListener("beforeprint",e)}}lazyAlways(){if(this.lazyAlwaysContainers&&!this.processed.isSet("lazyAlways")){for(const t of this.math){const e=t;e.lazyTypeset&&this.lazyIsAlways(e)&&(e.lazyCompile=e.lazyTypeset=!1)}this.processed.set("lazyAlways")}}lazyIsAlways(t){if(t.state()<l.LAZYALWAYS){t.state(l.LAZYALWAYS);const e=t.start.node,s=this.adaptor,a=this.lazyAlwaysIndex,i=this.lazyAlwaysContainers.length;do{const t=this.lazyAlwaysContainers[this.lazyAlwaysIndex];if(s.contains(t,e))return!0;++this.lazyAlwaysIndex>=i&&(this.lazyAlwaysIndex=0)}while(this.lazyAlwaysIndex!==a)}return!1}state(t,e=!1){return super.state(t,e),t<l.LAZYALWAYS&&this.processed.clear("lazyAlways"),this}lazyTypesetAll(){return y(this,void 0,void 0,(function*(){let t=l.LAST;for(const e of this.math){const s=e;(s.lazyCompile||s.lazyTypeset)&&(s.lazyCompile?(s.state(l.COMPILED-1),t=l.COMPILED):(s.state(l.TYPESET-1),l.TYPESET<t&&(t=l.TYPESET)),s.lazyCompile=s.lazyTypeset=!1,s.lazyMarker&&this.lazyObserver.unobserve(s.lazyMarker))}if(t===l.LAST)return Promise.resolve();this.state(t-1,null);const e=this.outputJax.options.fontCache;return e&&(this.outputJax.options.fontCache="none"),this.reset(),o((()=>this.render())).then((()=>{e&&(this.outputJax.options.fontCache=e)}))}))}lazyObserve(t){for(const e of t){const t=this.adaptor.getAttribute(e.target,c);this.lazyList.get(t)&&(e.isIntersecting?(this.lazySet.add(t),this.lazyIdle||(this.lazyIdle=!0,this.lazyProcessSet())):this.lazySet.delete(t))}}lazyHandleSet(){const t=this.lazySet;this.lazySet=new Set,this.lazyPromise=this.lazyPromise.then((()=>{let e=this.compileEarlierItems(t)?l.COMPILED:l.TYPESET;return e=this.resetStates(t,e),this.state(e-1,null),o((()=>{this.render(),this.lazyIdle=!1}))}))}resetStates(t,e){for(const s of t.values()){const t=this.lazyList.get(s);t.lazyCompile?(t.state(l.COMPILED-1),e=l.COMPILED):Object.hasOwn(t.metrics,"em")?t.state(l.TYPESET-1):(t.state(l.METRICS-1),e=l.METRICS),t.lazyCompile=t.lazyTypeset=!1,t.lazyMarker&&this.lazyObserver.unobserve(t.lazyMarker)}return e}compileEarlierItems(t){const e=this.earliestTex(t);if(!e)return!1;let s=!1;for(const t of this.math){const a=t;if(a===e||!(null==a?void 0:a.lazyCompile)||!a.lazyTex)break;a.lazyCompile=!1,a.lazyMarker&&this.lazyObserver.unobserve(a.lazyMarker),a.state(l.COMPILED-1),s=!0}return s}earliestTex(t){let e=null,s=null;for(const a of t.values()){const t=this.lazyList.get(a);t.lazyTex&&((null===e||parseInt(a)<e)&&(e=parseInt(a),s=t))}return s}clearMathItemsWithin(t){const e=super.clearMathItemsWithin(t);for(const t of e){const e=t.lazyMarker;e&&(this.lazyObserver.unobserve(e),this.lazyList.delete(this.adaptor.getAttribute(e,c)))}return e}render(){const t=this.options.lazyAlwaysTypeset;return this.lazyAlwaysContainers=t?this.adaptor.getElements(Array.isArray(t)?t:[t],this.document):null,this.lazyAlwaysIndex=0,super.render(),this}}).OPTIONS=Object.assign(Object.assign({},t.OPTIONS),{lazyMargin:"500px",lazyAlwaysTypeset:null,renderActions:Object.assign(Object.assign({},t.OPTIONS.renderActions),{lazyAlways:[l.LAZYALWAYS,"lazyAlways","",!1]})}),e}function d(t){return"undefined"!=typeof IntersectionObserver&&(t.documentClass=u(t.documentClass)),t}MathJax.loader&&MathJax.loader.checkVersion("ui/lazy","4.0.0","ui"),a({_:{ui:{lazy:{LazyHandler:e}}}}),MathJax.startup&&MathJax.startup.extendHandler((t=>d(t)))})();