primereact
Version:
PrimeReact is an open source UI library for React featuring a rich set of 90+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with Prime
2 lines (1 loc) • 4 kB
JavaScript
import*as e from"react";import{TerminalService as t}from"primereact/terminalservice";import{ObjectUtils as r,DomHandler as n,classNames as a}from"primereact/utils";function o(){return o=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},o.apply(this,arguments)}function l(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function i(e){if(Array.isArray(e))return l(e)}function u(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function c(e,t){if(e){if("string"==typeof e)return l(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?l(e,t):void 0}}function s(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function f(e){return i(e)||u(e)||c(e)||s()}function m(e){if(Array.isArray(e))return e}function p(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,a,o,l,i=[],u=!0,c=!1;try{if(o=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;u=!1}else for(;!(u=(n=o.call(r)).done)&&(i.push(n.value),i.length!==t);u=!0);}catch(e){c=!0,a=e}finally{try{if(!u&&null!=r.return&&(l=r.return(),Object(l)!==l))return}finally{if(c)throw a}}return i}}function d(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function y(e,t){return m(e)||p(e,t)||c(e,t)||d()}var v={defaultProps:{__TYPE:"Terminal",id:null,style:null,className:null,welcomeMessage:null,prompt:null,children:void 0},getProps:function(e){return r.getMergedProps(e,v.defaultProps)},getOtherProps:function(e){return r.getDiffProps(e,v.defaultProps)}},h=e.memo(e.forwardRef((function(r,l){var i=v.getProps(r),u=y(e.useState(""),2),c=u[0],s=u[1],m=y(e.useState([]),2),p=m[0],d=m[1],h=y(e.useState(0),2),g=h[0],b=h[1],E=y(e.useState(""),2),w=E[0],S=E[1],N=e.useRef(null),P=e.useRef(null),O=e.useRef(!1),j=function(e){s(e.target.value)},A=function(e){switch(e.which||e.keyCode){case 38:if(p&&p.length){var t=g-1<0?p.length-1:g-1,r=p[t];b(t),s(r.text)}break;case 13:if(c){var n=f(p);n.push({text:c}),b((function(e){return e+1})),s(""),d(n),S(c),O.current=!0}}};e.useImperativeHandle(l,(function(){return{props:i,focus:function(){return n.focus(P.current)},getElement:function(){return N.current}}})),e.useEffect((function(){var e=function(e){if(p&&p.length>0){var t=f(p);t[t.length-1].response=e,d(t)}},r=function(){d([]),b(0)};return t.on("response",e),t.on("clear",r),function(){t.off("response",e),t.off("clear",r)}}),[p]),e.useEffect((function(){O.current&&(t.emit("command",w),O.current=!1),N.current.scrollTop=N.current.scrollHeight}));var x=function(t,r){var n=t.text,a=t.response;return e.createElement("div",{key:n+"_"+r},e.createElement("span",{className:"p-terminal-prompt"},i.prompt," "),e.createElement("span",{className:"p-terminal-command"},n),e.createElement("div",{className:"p-terminal-response"},a))},I=v.getOtherProps(i),T=a("p-terminal p-component",i.className),C=i.welcomeMessage?e.createElement("div",null,i.welcomeMessage):null,M=function(){var t=p.map(x);return e.createElement("div",{className:"p-terminal-content"},t)}(),k=e.createElement("div",{className:"p-terminal-prompt-container"},e.createElement("span",{className:"p-terminal-prompt"},i.prompt," "),e.createElement("input",{ref:P,type:"text",value:c,className:"p-terminal-input",autoComplete:"off",onChange:j,onKeyDown:A}));return e.createElement("div",o({ref:N,id:i.id,className:T,style:i.style},I,{onClick:function(){n.focus(P.current)}}),C,M,k)})));h.displayName="Terminal";export{h as Terminal};