码桶
发现社区成员的开源项目
index-WdvJbv7c.js8.7 KB
import{Y as B,r as K,a5 as _,R as $,aD as R,a8 as s,a9 as V}from"./index-GWuvg2UH.js";import{A as X}from"./Animation-CU6YFe7Y.js";const Y={PREFIX:`${B}-progress`},h={types:["line","circle"],DEFAULT_TYPE:"line",STROKE_DEFAULT:"var(--semi-color-success)",strokeLineCap:["square","round"],DEFAULT_LINECAP:"round",sizes:["default","small","large"],DEFAULT_SIZE:"default",directions:["vertical","horizontal"],DEFAULT_DIRECTION:"horizontal"};function Z(e,t,r){try{const n=Q(e,t,r);if(n.length!==0)return n}catch{return}}function Q(e,t,r){if(e.sort((i,o)=>i.percent-o.percent),e[0].percent>t)return h.STROKE_DEFAULT;const n=e[e.length-1];if(n.percent<t)return w(n.color);for(const[i,o]of e.entries()){if(o.percent===t)return w(o.color);if(t>o.percent)continue;const a=e[i-1];return r?q({startColor:w(a.color),endColor:w(o.color),size:o.percent-a.percent-1},t-a.percent-1):w(a.color)}}function q(e,t){const{startColor:r,endColor:n,size:i}=e,o=r.split(""),a=n.split(""),d=[parseInt(`${o[1]}${o[2]}`,16),parseInt(`${a[1]}${a[2]}`,16)],g=[parseInt(`${o[3]}${o[4]}`,16),parseInt(`${a[3]}${a[4]}`,16)],l=[parseInt(`${o[5]}${o[6]}`,16),parseInt(`${a[5]}${a[6]}`,16)],c=[parseInt(`${o[7]}${o[8]}`,16),parseInt(`${a[7]}${a[8]}`,16)],p=(d[0]-d[1])/(i+1),D=(g[0]-g[1])/(i+1),k=(l[0]-l[1])/(i+1),y=(c[0]-c[1])/(i+1);function T(u){const S=Math.round(d[0]-p*(u+1)).toString(16),L=Math.round(g[0]-D*(u+1)).toString(16),E=Math.round(l[0]-k*(u+1)).toString(16),O=`${N(S)}${N(L)}${N(E)}`,I=Math.floor(y*(u+1)+c[1]).toString(16);return m.Hex(`#${O}`,I)}function N(u){return u.length===1?`0${u}`:u.length===0?"00":u}if(typeof t>"u"){const u=[r];for(let S=0;S<i;S+=1)u.push(T(S));return u}return T(t)}function w(e){if(e=e.trim().toLowerCase(),C.hex.test(e))return m.Hex(e,void 0);if(C.hslA.test(e))return m.Hex(m.HslA(e),void 0);if(C.rgbA.test(e))return m.Hex(m.RgbA(e),void 0);if(C.semiDesignTokens.test(e))return z.ALONG.indexOf(e)!==-1?m.SemiDesignToken(e):z.SEQUENCE.indexOf(e)!==-1?m.SemiDesignToken(`${e}-5`):m.SemiDesignToken(`${e}`)}const m={Hex(e,t){return e=e.replace("#",""),e.length===8?`#${e}`:e.length===6?`#${e}${t||"ff"}`:(e.length===3&&(e=e.split("").map(r=>r+r).join("")),`#${e}${t||"ff"}`)},SemiDesignToken(e){if(typeof window>"u")return;const t=getComputedStyle(document.body).getPropertyValue(`--semi-${e}`);if(t==="")return;const r=`rgba(${t}, 1)`;return m.RgbA(r)},HslA(e){const t=C.hslA.exec(e),r=parseInt(t[2]),n=parseInt(t[3])/100,i=parseInt(t[4])/100,o=t[5],a=(1-Math.abs(2*i-1))*n,d=a*(1-Math.abs(r/60%2-1)),g=i-a/2;let l=0,c=0,p=0;return 0<=r&&r<60?(l=a,c=d,p=0):60<=r&&r<120?(l=d,c=a,p=0):120<=r&&r<180?(l=0,c=a,p=d):180<=r&&r<240?(l=0,c=d,p=a):240<=r&&r<300?(l=d,c=0,p=a):300<=r&&r<360&&(l=a,c=0,p=d),l=Math.round((l+g)*255).toString(16),c=Math.round((c+g)*255).toString(16),p=Math.round((p+g)*255).toString(16),m.utils.pAL(l,c,p,o)},RgbA(e){const t=C.rgbA.exec(e),r=parseInt(t[2],10).toString(16),n=parseInt(t[3],10).toString(16),i=parseInt(t[4],10).toString(16),o=t[5];return m.utils.pAL(r,n,i,o)},utils:{pAL(e,t,r,n){return e.length==1&&(e="0"+e),t.length==1&&(t="0"+t),r.length==1&&(r="0"+r),typeof n<"u"?(n=Math.round(parseInt(n)*255).toString(16),n.length==1&&(n="0"+n),"#"+e+t+r+n):"#"+e+t+r}}},C={hex:/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/,hslA:/(hsl)a?\(\s*?(\d+),?\s*?(\d+)%,?\s*?(\d+)%,?\s*?\/?(\s*?[\d.]+)?\s*?\)/,rgbA:/(rgb)a?\(\s*?(\d+),?\s*?(\d+),?\s*?(\d+),?\s*?\/?(\s*?[\d.]+)?\s*?\)/,semiDesignTokens:/(\w+)?-?(\w+)-?(\d)?/},z={ALONG:["black","white"],SEQUENCE:["amber","blue","cyan","green","grey","indigo","light-blue","light-green","lime","orange","pink","purple","red","teal","violet","yellow"]};var G=function(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(e!=null&&typeof Object.getOwnPropertySymbols=="function")for(var i=0,n=Object.getOwnPropertySymbols(e);i<n.length;i++)t.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(e,n[i])&&(r[n[i]]=e[n[i]]);return r};const f=Y.PREFIX;class P extends K.Component{constructor(t){super(t),this._mounted=!0,this._mounted=!0,this.state={percentNumber:this.props.percent}}componentDidUpdate(t){if(isNaN(this.props.percent)||isNaN(t.percent))throw new Error("[Semi Progress]:percent can not be NaN");if(t.percent!==this.props.percent){if(!this.props.motion){this.setState({percentNumber:this.props.percent});return}this.animation&&this.animation.destroy&&this.animation.destroy(),this.animation=new X({from:{value:t.percent},to:{value:this.props.percent}},{easing:"linear",duration:300}),this.animation.on("frame",r=>{if(this._mounted===!1)return;const n=parseInt(r.value);this.setState({percentNumber:n})}),this.animation.on("rest",()=>{this._mounted!==!1&&this.setState({percentNumber:this.props.percent})}),this.animation.start()}}componentWillUnmount(){this.animation&&this.animation.destroy(),this._mounted=!1}renderCircleProgress(){const t=this.props,{strokeLinecap:r,style:n,className:i,strokeWidth:o,format:a,size:d,stroke:g,strokeGradient:l,showInfo:c,percent:p,orbitStroke:D,id:k,indeterminate:y}=t,T=G(t,["strokeLinecap","style","className","strokeWidth","format","size","stroke","strokeGradient","showInfo","percent","orbitStroke","id","indeterminate"]),N=this.props["aria-label"],u=this.props["aria-labelledby"],S=this.props["aria-valuetext"],{percentNumber:L}=this.state,E={wrapper:_(`${f}-circle`,i),svg:_(`${f}-circle-ring`),circle:_(`${f}-circle-ring-inner`,{[`${f}-circle-ring-inner-indeterminate`]:y}),track:_(`${f}-circle-ring-track`)},O=this.calcPercent(p),I=this.calcPercent(L);let b;this.props.width?b=this.props.width:d===h.DEFAULT_SIZE?b=72:b=24;const U=this.selectStroke(g,p,l),x=b/2,v=b/2,F=(b-o)/2,A=F*2*Math.PI,M=y?0:(1-O/100)*A,H=y?`${A*.3} ${A}`:`${A} ${A}`,j=`${A} ${A}`,W=a(I);return $.createElement("div",Object.assign({id:k,className:E.wrapper,style:n,role:"progressbar","aria-valuemin":0,"aria-valuemax":100,"aria-valuenow":y?void 0:I,"aria-labelledby":u,"aria-label":N,"aria-valuetext":S},R(T)),$.createElement("svg",{key:d,className:E.svg,height:b,width:b,"aria-hidden":!0},$.createElement("circle",{className:E.track,strokeDashoffset:0,strokeWidth:o,strokeDasharray:j,strokeLinecap:r,fill:"transparent",style:{stroke:D},r:F,cx:v,cy:x,"aria-hidden":!0}),$.createElement("circle",{className:E.circle,strokeDashoffset:M,strokeWidth:o,strokeDasharray:H,strokeLinecap:r,fill:"transparent",style:{stroke:U},r:F,cx:v,cy:x,"aria-hidden":!0})),c&&d!=="small"&&!y?$.createElement("span",{className:`${f}-circle-text`},W):null)}calcPercent(t){let r;return t>100?r=100:t<0?r=0:r=t,r}selectStroke(t,r,n){if(typeof t=="string")return t;const i=Z(t,r,n);return typeof i<"u"?i:null}renderLineProgress(){const t=this.props,{className:r,style:n,stroke:i,strokeGradient:o,direction:a,format:d,showInfo:g,size:l,percent:c,orbitStroke:p,id:D,indeterminate:k}=t,y=G(t,["className","style","stroke","strokeGradient","direction","format","showInfo","size","percent","orbitStroke","id","indeterminate"]),T=this.props["aria-label"],N=this.props["aria-labelledby"],u=this.props["aria-valuetext"],{percentNumber:S}=this.state,L=_(f,r,{[`${f}-horizontal`]:a===h.DEFAULT_DIRECTION,[`${f}-vertical`]:a!==h.DEFAULT_DIRECTION,[`${f}-large`]:l==="large",[`${f}-indeterminate`]:k}),E=_({[`${f}-track`]:!0}),O=_(`${f}-track-inner`,{[`${f}-track-inner-indeterminate`]:k}),I=this.calcPercent(c),b=this.calcPercent(S),x={background:this.selectStroke(i,c,o)};k||(a===h.DEFAULT_DIRECTION?x.width=`${I}%`:x.height=`${I}%`);const v=d(b);return $.createElement("div",Object.assign({id:D,className:L,style:n,role:"progressbar","aria-valuemin":0,"aria-valuemax":100,"aria-valuenow":k?void 0:I,"aria-labelledby":N,"aria-label":T,"aria-valuetext":u},R(y)),$.createElement("div",{className:E,style:p?{backgroundColor:p}:{},"aria-hidden":!0},$.createElement("div",{className:O,style:x,"aria-hidden":!0})),g&&!k?$.createElement("div",{className:`${f}-line-text`},v):null)}render(){const{type:t}=this.props;return t==="line"?this.renderLineProgress():this.renderCircleProgress()}}P.__SemiComponentName__="Progress";P.propTypes={"aria-label":s.string,"aria-labelledby":s.string,"aria-valuetext":s.string,className:s.string,direction:s.oneOf(h.directions),format:s.oneOfType([s.func,s.node]),id:s.string,indeterminate:s.bool,motion:s.oneOfType([s.bool,s.func,s.object]),orbitStroke:s.string,percent:s.number,scale:s.number,showInfo:s.bool,size:s.oneOf(h.sizes),stroke:s.oneOfType([s.string,s.arrayOf(s.shape({percent:s.number,color:s.string}))]),strokeGradient:s.bool,strokeLinecap:s.oneOf(h.strokeLineCap),strokeWidth:s.number,style:s.object,type:s.oneOf(h.types),width:s.number};P.defaultProps=V(P.__SemiComponentName__,{className:"",direction:h.DEFAULT_DIRECTION,format:e=>`${e}%`,indeterminate:!1,motion:!0,percent:0,showInfo:!1,size:h.DEFAULT_SIZE,strokeGradient:!1,strokeLinecap:h.DEFAULT_LINECAP,strokeWidth:4,style:{},type:h.DEFAULT_TYPE});export{P};