Files
fuwari/_astro/SwupScrollPlugin.M9KUzlP6.js
afoim 33d483e8f3 feat: 添加对暗色主题的Cookie Consent支持
- 更新Cookie Consent脚本以动态适应暗色/亮色主题
- 添加CSS类切换功能,当文档主题变化时自动更新Cookie Consent界面
- 标记Cookie Consent相关资源为持久化,防止页面切换时被清理
- 优化Google Adsense脚本的格式和属性
- 添加@types/hast类型依赖以支持相关功能 53e134f106
2026-01-30 14:24:19 +00:00

2 lines
7.8 KiB
JavaScript

import{e as p}from"./index.modern.BC8Oj8jT.js";import{m as d}from"./Swup.DZDn2d2T.js";function a(){return a=Object.assign?Object.assign.bind():function(n){for(var o=1;o<arguments.length;o++){var t=arguments[o];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(n[s]=t[s])}return n},a.apply(this,arguments)}class g{constructor(o){this._raf=null,this._positionY=0,this._velocityY=0,this._targetPositionY=0,this._targetPositionYWithOffset=0,this._direction=0,this.scrollTo=t=>{if(t&&t.nodeType)this._targetPositionY=Math.round(t.getBoundingClientRect().top+window.pageYOffset);else{if(parseInt(this._targetPositionY)!==this._targetPositionY)return void console.error("Argument must be a number or an element.");this._targetPositionY=Math.round(t)}this._targetPositionY>document.documentElement.scrollHeight-window.innerHeight&&(this._targetPositionY=document.documentElement.scrollHeight-window.innerHeight),this._positionY=document.body.scrollTop||document.documentElement.scrollTop,this._direction=this._positionY>this._targetPositionY?-1:1,this._targetPositionYWithOffset=this._targetPositionY+this._direction,this._velocityY=0,this._positionY!==this._targetPositionY?(this.options.onStart(),this._animate()):this.options.onAlreadyAtPositions()},this._animate=()=>{this._update(),this._render(),this._direction===1&&this._targetPositionY>this._positionY||this._direction===-1&&this._targetPositionY<this._positionY?(this._raf=requestAnimationFrame(this._animate),this.options.onTick()):(this._positionY=this._targetPositionY,this._render(),this._raf=null,this.options.onTick(),this.options.onEnd())},this._update=()=>{const t=this._targetPositionYWithOffset-this._positionY;return this._velocityY+=t*this.options.acceleration,this._velocityY*=this.options.friction,this._positionY+=this._velocityY,Math.abs(t)},this._render=()=>{window.scrollTo(0,this._positionY)},this.options=a({},{onAlreadyAtPositions:()=>{},onCancel:()=>{},onEnd:()=>{},onStart:()=>{},onTick:()=>{},friction:.7,acceleration:.04},o),o&&o.friction&&(this.options.friction=1-o.friction),window.addEventListener("mousewheel",t=>{this._raf&&(this.options.onCancel(),cancelAnimationFrame(this._raf),this._raf=null)},{passive:!0})}}function c(){return c=Object.assign?Object.assign.bind():function(n){for(var o=1;o<arguments.length;o++){var t=arguments[o];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(n[s]=t[s])}return n},c.apply(this,arguments)}class S extends p{constructor(o={}){super(),this.name="SwupScrollPlugin",this.requires={swup:">=4.2.0"},this.scrl=void 0,this.defaults={doScrollingRightAway:!1,animateScroll:{betweenPages:!0,samePageWithHash:!0,samePage:!0},scrollFriction:.3,scrollAcceleration:.04,getAnchorElement:void 0,offset:0,scrollContainers:"[data-swup-scroll-container]",shouldResetScrollPosition:()=>!0,markScrollTarget:!1},this.options=void 0,this.cachedScrollPositions={},this.previousScrollRestoration=void 0,this.currentCacheKey=void 0,this.getAnchorElement=(t="")=>typeof this.options.getAnchorElement=="function"?this.options.getAnchorElement(t):this.swup.getAnchorElement(t),this.getOffset=t=>t?typeof this.options.offset=="function"?parseInt(String(this.options.offset(t)),10):parseInt(String(this.options.offset),10):0,this.onBeforeLinkToSelf=t=>{t.scroll.animate=this.shouldAnimate("samePage")},this.handleScrollToTop=t=>{var s,i;return(s=(i=this.swup).scrollTo)==null||s.call(i,0,t.scroll.animate),!0},this.onBeforeLinkToAnchor=t=>{t.scroll.animate=this.shouldAnimate("samePageWithHash")},this.handleScrollToAnchor=(t,{hash:s})=>this.maybeScrollToAnchor(s,t.scroll.animate),this.onBeforeVisitStart=t=>{t.scroll.scrolledToContent=!1,t.scroll.animate=this.shouldAnimate("betweenPages")},this.onVisitStart=t=>{var s;this.cacheScrollPositions(t.from.url),this.maybeResetScrollPositions(t);const i=(s=t.scroll.target)!=null?s:t.to.hash;t.scroll.animate&&this.options.doScrollingRightAway&&!i&&this.doScrollingBetweenPages(t)},this.handleScrollToContent=t=>{t.scroll.scrolledToContent||this.doScrollingBetweenPages(t),this.restoreScrollContainers(t.to.url)},this.doScrollingBetweenPages=t=>{var s,i;if(t.history.popstate&&!t.animation.animate)return;const e=(s=t.scroll.target)!=null?s:t.to.hash;if(e&&this.maybeScrollToAnchor(e,t.scroll.animate)||!t.scroll.reset)return;const r=this.getCachedScrollPositions(t.to.url),l=(r==null||(i=r.window)==null?void 0:i.top)||0;requestAnimationFrame(()=>{var h,u;return(h=(u=this.swup).scrollTo)==null?void 0:h.call(u,l,t.scroll.animate)}),t.scroll.scrolledToContent=!0},this.maybeResetScrollPositions=t=>{const{popstate:s}=t.history,{url:i}=t.to,{el:e}=t.trigger;s||e&&!this.options.shouldResetScrollPosition(e)||this.resetScrollPositions(i)},this.options=c({},this.defaults,o)}mount(){const o=this.swup;o.hooks.create("scroll:start"),o.hooks.create("scroll:end");const t=this.swup.createVisit({to:this.swup.currentPageUrl});this.scrl=new g({onStart:()=>o.hooks.callSync("scroll:start",t,void 0),onEnd:()=>o.hooks.callSync("scroll:end",t,void 0),onCancel:()=>o.hooks.callSync("scroll:end",t,void 0),friction:this.options.scrollFriction,acceleration:this.options.scrollAcceleration}),o.scrollTo=(s,i=!0)=>{i?this.scrl.scrollTo(s):(o.hooks.callSync("scroll:start",t,void 0),window.scrollTo(0,s),o.hooks.callSync("scroll:end",t,void 0))},this.previousScrollRestoration=window.history.scrollRestoration,o.options.animateHistoryBrowsing&&(window.history.scrollRestoration="manual"),this.updateScrollTarget=this.updateScrollTarget.bind(this),this.options.markScrollTarget&&(window.addEventListener("popstate",this.updateScrollTarget),window.addEventListener("hashchange",this.updateScrollTarget),this.on("page:view",this.updateScrollTarget),this.on("link:anchor",this.updateScrollTarget),this.on("link:self",this.updateScrollTarget),this.updateScrollTarget()),this.before("visit:start",this.onBeforeVisitStart,{priority:-1}),this.on("visit:start",this.onVisitStart,{priority:1}),this.replace("content:scroll",this.handleScrollToContent),this.before("link:self",this.onBeforeLinkToSelf,{priority:-1}),this.replace("scroll:top",this.handleScrollToTop),this.before("link:anchor",this.onBeforeLinkToAnchor,{priority:-1}),this.replace("scroll:anchor",this.handleScrollToAnchor)}unmount(){super.unmount(),this.previousScrollRestoration&&(window.history.scrollRestoration=this.previousScrollRestoration),window.removeEventListener("popstate",this.updateScrollTarget),window.removeEventListener("hashchange",this.updateScrollTarget),this.cachedScrollPositions={},delete this.swup.scrollTo,delete this.scrl}shouldAnimate(o){return typeof this.options.animateScroll=="boolean"?this.options.animateScroll:this.options.animateScroll[o]}maybeScrollToAnchor(o,t=!1){var s,i;if(!o)return!1;const e=this.getAnchorElement(o);if(!e)return console.warn(`Anchor target ${o} not found`),!1;if(!(e instanceof Element))return console.warn(`Anchor target ${o} is not a DOM node`),!1;const{top:r}=e.getBoundingClientRect(),l=r+window.scrollY-this.getOffset(e);return(s=(i=this.swup).scrollTo)==null||s.call(i,l,t),!0}cacheScrollPositions(o){const t=this.swup.resolveUrl(o),s=d(this.options.scrollContainers).map(e=>({top:e.scrollTop,left:e.scrollLeft})),i={window:{top:window.scrollY,left:window.scrollX},containers:s};this.cachedScrollPositions[t]=i}resetScrollPositions(o){const t=this.swup.resolveUrl(o);delete this.cachedScrollPositions[t]}getCachedScrollPositions(o){const t=this.swup.resolveUrl(o);return this.cachedScrollPositions[t]}restoreScrollContainers(o){const t=this.getCachedScrollPositions(o);t&&t.containers.length!==0&&d(this.options.scrollContainers).forEach((s,i)=>{const e=t.containers[i];e!=null&&(s.scrollTop=e.top,s.scrollLeft=e.left)})}updateScrollTarget(){var o;const{hash:t}=window.location,s=document.querySelector("[data-swup-scroll-target]");let i=this.getAnchorElement(t);i instanceof HTMLBodyElement&&(i=null),s!==i&&(s?.removeAttribute("data-swup-scroll-target"),(o=i)==null||o.setAttribute("data-swup-scroll-target",""))}}export{S as default};