12 |
- import{s as H,f as i,w as N,X as _,V as k,E as D,G as O,c_ as T,y as x,L as C,c3 as F,z as V,D as U,c7 as z}from"./index-DT8qTvyx.js";var W={30:"black",31:"red",32:"green",33:"yellow",34:"blue",35:"magenta",36:"cyan",37:"white",90:"grey"},A={40:"black",41:"red",42:"green",43:"yellow",44:"blue",45:"magenta",46:"cyan",47:"white"},B=function(y){H(o,y);function o(t){var e=y.call(this,t)||this;return e.isDone=!1,e.autoScroll=!1,e.state={lastLine:"",logs:[],originLastLine:"",originLogs:[],refresh:!0,showLineNumber:!1,filterWord:""},e.refresh=function(r){var s=e.state.refresh;e.setState({refresh:!s}),s||(e.clear(r),e.loadLogs()),r.preventDefault()},e.clear=function(r){e.setState({logs:e.logs=[],lastLine:e.lastLine="",originLogs:[],originLastLine:""}),r==null||r.preventDefault()},e.filterWord=function(r,s,a){var n=r,g=s;a!==""&&a!==void 0&&a!==null&&a.length>0&&(r=r.filter(function(v){return v.includes(a)}),s.includes(a)||(s="")),e.setState({filterWord:a,lastLine:e.lastLine=s,logs:e.logs=r,originLogs:n,originLastLine:g})},e.addLines=function(r){r=r.concat();var s=e.props.maxLength,a=e.lastLine||"",n=(e.logs||[]).concat();r.length===1?(a+=r[0],e.setState({lastLine:e.lastLine=a})):(r[0]=a+(r[0]||""),a=r.pop()||"",s&&n.length+r.length>s&&n.splice(0,n.length+r.length-s),n=n.concat(r),e.filterWord(n,a,e.state.filterWord))},e.logRef=i.createRef(),e.autoScroll=t.autoScroll||!1,e.pauseOrResumeScrolling=e.pauseOrResumeScrolling.bind(e),e}return o.prototype.componentWillUnmount=function(){this.logRef&&this.logRef.current&&this.logRef.current.removeEventListener("scroll",this.pauseOrResumeScrolling)},o.prototype.componentDidMount=function(){if(this.autoScroll&&this.logRef&&this.logRef.current&&this.logRef.current.addEventListener("scroll",this.pauseOrResumeScrolling),this.props.source){var t=typeof this.props.source=="string"?N(this.props.source,this.props.data,"| raw"):this.props.source;t&&_(t)?this.loadLogs():(typeof t=="string"||Array.isArray(t)&&t.every(function(e){return typeof e=="string"}))&&(this.clear(),this.addLines(Array.isArray(t)?t:[t]))}},o.prototype.componentDidUpdate=function(t){if(this.autoScroll&&this.logRef&&this.logRef.current&&(this.logRef.current.scrollTop=this.logRef.current.scrollHeight),!!this.props.source){var e=typeof this.props.source=="string"?N(this.props.source,this.props.data,"| raw"):this.props.source;if(e&&_(e))k(t.source,this.props.source,t.data,this.props.data)&&this.loadLogs();else if(typeof e=="string"||Array.isArray(e)&&e.every(function(s){return typeof s=="string"})){var r=N(t.source,t.data,"| raw");r!==e&&e&&(this.clear(),this.addLines(Array.isArray(e)?e:[e]))}}},o.prototype.pauseOrResumeScrolling=function(){if(this.logRef&&this.logRef.current){var t=this.logRef.current,e=t.scrollHeight,r=t.scrollTop,s=t.offsetHeight;this.autoScroll=e-(r+s)<50}},o.prototype.loadLogs=function(){var t,e,r;return D(this,void 0,void 0,function(){var s,a,n,g,v,S,u,c,l,b,f,E,d,R,L,h,p,w=this;return O(this,function(m){switch(m.label){case 0:return s=this.props,a=s.source,n=s.data,g=s.env,v=s.translate,S=s.encoding,s.maxLength,u=s.credentials,c=u===void 0?"include":u,l=z(a,n),l.url?[4,fetch(l.url,{method:((t=l.method)===null||t===void 0?void 0:t.toLocaleUpperCase())||"GET",headers:l.headers||void 0,body:l.data?JSON.stringify(l.data):void 0,credentials:c})]:[2];case 1:if(b=m.sent(),b.status!==200)return[3,8];if(f=b.body,!f)return[2];E=f.getReader(),m.label=2;case 2:return this.state.refresh?[3,4]:[4,E.cancel("click cancel button").then(function(){w.props.env.notify("success","日志已经停止刷新")})];case 3:m.sent(),m.label=4;case 4:return[4,E.read()];case 5:if(d=m.sent(),R=d.done,L=d.value,L&&(h=new TextDecoder(S).decode(L,{stream:!0}),p=h.split(`
- `),this.addLines(p)),R)return this.isDone=!0,[2];m.label=6;case 6:return[3,2];case 7:return[3,9];case 8:!l.silent&&g.notify("error",(r=(e=l==null?void 0:l.messages)===null||e===void 0?void 0:e.failed)!==null&&r!==void 0?r:v("fetchFailed")),m.label=9;case 9:return[2]}})})},o.prototype.ansiColrToHtml=function(t){var e=this.props.disableColor;if(e===!0)return t;var r=t.match(/\u001b\[([^m]+)m/);if(r){var s=r[1];if(s){if(t=t.replace(/\u001b[^m]*?m/g,""),s in W)return i.createElement("span",{style:{color:W[s]}},t);if(s in A)return i.createElement("span",{style:{backgroundColor:A[s]}},t.replace(/\u001b[^m]*?m/g,""))}}return t},o.prototype.renderHighlightWord=function(t){var e=this,r=this.props.classnames,s=this.state.filterWord;if(s==="")return this.ansiColrToHtml(t);var a=t.split(s);return a.map(function(n,g){return g<a.length-1?i.createElement("span",null,e.ansiColrToHtml(n),i.createElement("span",{className:r("Log-line-highlight")},s)):n})},o.prototype.renderLine=function(t,e,r){var s=this.props,a=s.classnames;return s.disableColor,i.createElement("div",{className:a("Log-line"),key:t},r&&i.createElement("span",{className:a("Log-line-number")},t+1," "),this.renderHighlightWord(e))},o.prototype.render=function(){var t=this,e=this.props,r=e.source,s=e.className,a=e.style,n=e.classnames,g=e.placeholder,v=e.height,S=e.rowHeight;e.disableColor;var u=e.translate,c=e.operation,l=this.state,b=l.refresh,f=l.showLineNumber,E=u(g);r||(E=u("Log.mustHaveSource"));var d,R=this.state.lastLine?this.state.logs.concat([this.state.lastLine]):this.state.logs,L=S;return L?d=i.createElement(T,{height:v,itemCount:R.length,itemSize:S,renderItem:function(h){var p=h.index,w=h.style;return i.createElement("div",{className:n("Log-line"),key:p,style:x(x({},w),{whiteSpace:"nowrap"})},f&&i.createElement("span",{className:n("Log-line-number")},p+1," "),t.renderHighlightWord(R[p]))}}):d=R.map(function(h,p){return t.renderLine(p,h,f)}),i.createElement("div",{className:n("Log",s),style:a},i.createElement("div",{className:n("Log-operation")},c&&(c==null?void 0:c.length)>0&&i.createElement(i.Fragment,null,c.includes("stop")&&i.createElement("a",{title:u("stop"),className:b?"":"is-disabled",onClick:this.refresh},i.createElement(C,{icon:"pause"})),c.includes("restart")&&i.createElement("a",{title:u("reload"),className:b?"is-disabled":"",onClick:this.refresh},i.createElement(C,{icon:"refresh"})),c.includes("showLineNumber")&&i.createElement("a",{title:u(f?"Log.notShowLineNumber":"Log.showLineNumber"),onClick:function(h){t.setState({showLineNumber:!f}),h.preventDefault()}},i.createElement(C,{icon:f?"invisible":"view"})),c.includes("clear")&&i.createElement("a",{onClick:this.clear,title:u("clear")},i.createElement(C,{icon:"remove"})),c&&c.includes("filter")&&i.createElement(F,{className:n("Log-filter-box"),placeholder:"过滤词",onChange:function(h){return t.filterWord(t.state.originLogs,t.state.lastLine,h)},value:this.state.filterWord}))),i.createElement("div",{ref:this.logRef,className:n("Log-body"),style:{height:L?"auto":v}},L||d.length?d:E))},o.defaultProps={height:500,autoScroll:!0,placeholder:"loading",encoding:"utf-8"},o}(i.Component),I=function(y){H(o,y);function o(){return y!==null&&y.apply(this,arguments)||this}return o=V([U({type:"log"})],o),o}(B);export{B as Log,I as LogRenderer};
|