react-terminal-plus
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
import*as e from"react";import{CancelablePromise as t}from"cancelable-promise";!function(e,t){void 0===t&&(t={});var n=t.insertAt;if(e&&"undefined"!=typeof document){var r=document.head||document.getElementsByTagName("head")[0],o=document.createElement("style");o.type="text/css","top"===n&&r.firstChild?r.insertBefore(o,r.firstChild):r.appendChild(o),o.styleSheet?o.styleSheet.cssText=e:o.appendChild(document.createTextNode(e))}}('@import url("https://fonts.googleapis.com/css?family=Source+Code+Pro&display=swap");\n#index-module_terminalContainer__i6yCT {\n --control-default-bg-color: #ccc;\n --control-close-bg-color: #fc5b57;\n --control-minimize-bg-color: #e5bf3c;\n --control-maximize-bg-color: #57c038;\n}\n\n.index-module_controls__-Pz-7 {\n text-align: left;\n height: 26px;\n position: relative;\n top: 4px;\n margin-left: 4px;\n}\n.index-module_controls__-Pz-7 .index-module_consoleCtrl__wASVf {\n display: inline-block;\n width: 13px;\n height: 13px;\n margin: 0 3px;\n border-radius: 50%;\n background: var(--control-default-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_close__Etu85 {\n background: var(--control-close-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_minimize__bT0-I {\n background: var(--control-minimize-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_maximize__mPPF7 {\n background: var(--control-maximize-bg-color);\n}\n\n.index-module_editor__CBDCJ {\n text-align: left;\n height: 100%;\n padding: 10px 15px;\n overflow-wrap: break-word;\n overflow-y: scroll;\n outline: none;\n font-family: "Source Code Pro", monospace;\n font-size: 18px;\n line-height: 22px;\n caret-color: transparent;\n --scroll-bar-thumb-color: transparent;\n --scroll-bar-track-color: transparent;\n --scroll-bar-width: 10px;\n scrollbar-color: var(--scroll-bar-thumb-color) var(--scroll-bar-track-color);\n scrollbar-width: var(--scroll-bar-width);\n}\n.index-module_editor--rounded__-VOd4 {\n border-radius: 5px;\n}\n.index-module_editor__CBDCJ::selection {\n background: transparent;\n}\n.index-module_editor__CBDCJ:focus, .index-module_editor__CBDCJ:hover {\n --scroll-bar-thumb-color: rgb(211 211 211 / 40%);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar {\n width: var(--scroll-bar-width);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar-track {\n background-color: var(--scroll-bar-track-color);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar-thumb {\n background-color: var(--scroll-bar-thumb-color);\n}\n.index-module_editor__CBDCJ .index-module_lineText__avQ48 {\n display: inline;\n position: relative;\n top: -1px;\n margin-left: 8px;\n}\n.index-module_editor__CBDCJ .index-module_preWhiteSpace__v4IYf {\n white-space: pre-wrap;\n}\n.index-module_editor__CBDCJ .index-module_preWhiteSpace__v4IYf:focus-visible {\n outline: none;\n}\n.index-module_editor__CBDCJ .index-module_charUnderCaret__-M0qT {\n z-index: 10;\n position: relative;\n}\n.index-module_editor__CBDCJ .index-module_caret__YobLn {\n position: relative;\n}\n.index-module_editor__CBDCJ .index-module_caret__YobLn .index-module_caretAfter__SMMlh {\n content: "";\n position: absolute;\n top: 2px;\n width: 10px;\n height: 22px;\n z-index: 10;\n}\n@-moz-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@-webkit-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@-o-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n.index-module_editor__CBDCJ a {\n color: #1a87b5;\n}\n\n.index-module_editorWithTopBar__opXWp {\n height: calc(100% - 46px);\n}\n\n#index-module_terminalContainer__i6yCT {\n height: 100%;\n}\n\n.index-module_terminal__EWHbJ {\n height: 100%;\n width: 100%;\n margin: auto;\n}\n.index-module_terminal--rounded__DZuxh {\n border-radius: 5px;\n}');const n={terminalContainer:"index-module_terminalContainer__i6yCT",controls:"index-module_controls__-Pz-7",consoleCtrl:"index-module_consoleCtrl__wASVf",close:"index-module_close__Etu85",minimize:"index-module_minimize__bT0-I",maximize:"index-module_maximize__mPPF7",editor:"index-module_editor__CBDCJ","editor--rounded":"index-module_editor--rounded__-VOd4",lineText:"index-module_lineText__avQ48",preWhiteSpace:"index-module_preWhiteSpace__v4IYf",charUnderCaret:"index-module_charUnderCaret__-M0qT",caret:"index-module_caret__YobLn",caretAfter:"index-module_caretAfter__SMMlh",editorWithTopBar:"index-module_editorWithTopBar__opXWp",terminal:"index-module_terminal__EWHbJ","terminal--rounded":"index-module_terminal--rounded__DZuxh",blink:"index-module_blink__l83VF"},r=e.createContext(null),o=t=>{const{children:o}=t;return e.createElement(r.Provider,{value:n},o)};const a={dark:{themeBGColor:"rgb(2, 40, 51)",themeToolbarColor:"rgb(19, 21, 25)",themeColor:"rgb(131, 148, 150)",themePromptColor:"rgb(169, 23, 168)"},light:{themeBGColor:"#fdf6e4",themeToolbarColor:"#d8d8d8",themeColor:"#333333",themePromptColor:"#a917a8"},dracula:{themeBGColor:"#272B36",themeToolbarColor:"#DBDBDB",themeColor:"#FFFEFC",themePromptColor:"#a917a8"},"material-dark":{themeBGColor:"#151515",themeToolbarColor:"#424242",themeColor:"#fff",themePromptColor:"#42A5F5"},"material-light":{themeBGColor:"#fafafa",themeToolbarColor:"#424242",themeColor:"#151515",themePromptColor:"#42A5F5"},"material-ocean":{themeBGColor:"#263238",themeToolbarColor:"#37474F",themeColor:"#fff",themePromptColor:"#C3D82C"},matrix:{themeBGColor:"#0D0208",themeToolbarColor:"#0D0208",themeColor:"#00FF41",themePromptColor:"#008F11"}},l=e.createContext(null),i=t=>{const{children:n}=t,r=Object.assign(Object.assign({},a),n.props.themes||{}),o=r[n.props.theme]||r.light;return e.createElement(l.Provider,{value:o},n)},s=(t,n,r)=>{const o=e=>{t.current&&!t.current.contains(e.target)?r(!1):r(!0)};e.useEffect((()=>(document.addEventListener("mousedown",o),()=>{document.removeEventListener("mousedown",o)})))};function c(t){const n=e.useContext(r),{controlButtonLabels:o}=t,{showControlButtons:a}=t,l=a?o.map((t=>e.createElement("div",{key:t,className:`${n.consoleCtrl} ${n[t]}`}))):null;return e.createElement("div",{className:n.controls},l)}const d=["clear","cls"],u=e=>d.includes(e);class m{static splitStringAtIndex(e,t){return e?[e.substring(0,t),e.substring(t)]:["",""]}}const p="react-terminal-plus-commandHistory",f=()=>{var e;try{const t=null!==(e=localStorage.getItem(p))&&void 0!==e?e:"[]";return JSON.parse(t)}catch(e){return console.error(e),[]}},C=e=>{let t=f();t.push(e);try{JSON.stringify(t).length>52e5&&(t=t.slice(50)),localStorage.setItem(p,JSON.stringify(t))}catch(n){console.error(n),t=[e],localStorage.setItem(p,JSON.stringify(t))}},_=e.createContext(null);function x(e,t){switch(e.currentLineStatus){case"processing":switch(t.type){case"CANCEL":return Object.assign(Object.assign({},e),{bufferedContent:t.cancelNode,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});case"SUBMIT_SUCCESS":return Object.assign(Object.assign({},e),{bufferedContent:t.successNode,currentLineStatus:"success",editorInput:"",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});default:return e}case"success":case"error":case"idle":switch(t.type){case"CLEAR":return Object.assign(Object.assign({},e),{bufferedContent:null});case"CANCEL":return Object.assign(Object.assign({},e),{bufferedContent:t.cancelNode,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});case"SUBMIT":{const{command:n}=t,r=[...e.commandsHistory];return n&&n!==r[r.length-1]&&(r.push(n),C(n)),u(n)?Object.assign(Object.assign({},e),{commandsHistory:r,bufferedContent:null,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""}):Object.assign(Object.assign({},e),{commandsHistory:r,bufferedContent:t.loaderNode,currentLineStatus:"processing",editorInput:"",caretPosition:0,textBeforeCaret:"",textAfterCaret:""})}case"TYPE":{const[n,r]=h(e),o=n+t.text+r,a=e.caretPosition+1,[l,i]=b(o,a);return Object.assign(Object.assign({},e),{caretPosition:a,editorInput:o,textAfterCaret:i,textBeforeCaret:l})}case"DELETE":{const[t,n]=h(e),r=t.slice(0,-1)+n,o=e.caretPosition-1,[a,l]=b(r,o);return Object.assign(Object.assign({},e),{editorInput:r,caretPosition:o,textAfterCaret:l,textBeforeCaret:a})}case"COPY":return e;case"PASTE":{const[n,r]=h(e),o=n+t.text+r,a=e.caretPosition+t.text.length,[l,i]=b(o,a);return Object.assign(Object.assign({},e),{editorInput:o,caretPosition:a,textAfterCaret:i,textBeforeCaret:l})}case"ARROW_UP":{const n=t.previousCommand,r=t.previousCommand.length,[o,a]=b(n,r);return Object.assign(Object.assign({},e),{editorInput:n,caretPosition:r,textAfterCaret:a,textBeforeCaret:o})}case"ARROW_DOWN":{const n=t.nextCommand,r=t.nextCommand.length,[o,a]=b(n,r);return Object.assign(Object.assign({},e),{editorInput:n,caretPosition:r,textAfterCaret:a,textBeforeCaret:o})}case"RESET_CARET_POSITION":return Object.assign(Object.assign({},e),{editorInput:"",textBeforeCaret:"",textAfterCaret:"",caretPosition:0});case"ARROW_LEFT":{const t=e.caretPosition-1,[n,r]=b(e.editorInput,t);return Object.assign(Object.assign({},e),{caretPosition:t,textAfterCaret:r,textBeforeCaret:n})}case"ARROW_RIGHT":{const t=e.caretPosition+1,[n,r]=b(e.editorInput,t);return Object.assign(Object.assign({},e),{caretPosition:t,textAfterCaret:r,textBeforeCaret:n})}default:throw new Error(`Unhandled action type: ${JSON.stringify(t)}`)}default:return e}}function h(e){const[t,n]=m.splitStringAtIndex(e.editorInput,e.caretPosition);return[t,n]}function b(e,t){const[n,r]=m.splitStringAtIndex(e,t);return[n,r]}function g(){const t=e.useContext(_);if(void 0===t)throw new Error("useTerminal must be used within a TerminalContextProvider");return t}function y(e,t,n,r){return new(n||(n=Promise))((function(o,a){function l(e){try{s(r.next(e))}catch(e){a(e)}}function i(e){try{s(r.throw(e))}catch(e){a(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(l,i)}s((r=r.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const E=(n,o,a,i,s,c,d)=>{const m=e.useContext(r),p=e.useContext(l),{store:f}=g(),C=f.textAfterCaret[0],_=f.textAfterCaret.slice(1),x=o&&n,h="processing"!==f.currentLineStatus?e.createElement(e.Fragment,null,e.createElement("span",{style:{color:p.themePromptColor}},a),e.createElement("div",{className:m.lineText},e.createElement("span",{className:m.preWhiteSpace},f.textBeforeCaret),x?e.createElement("span",{className:m.caret},e.createElement("span",{className:m.caretAfter,style:{background:p.themeColor}})):null,e.createElement("span",{className:`${m.preWhiteSpace}`},e.createElement("span",{style:{color:x?p.themeBGColor:p.themeColor},className:m.charUnderCaret},C),_))):e.createElement(e.Fragment,null,e.createElement("div",{className:m.lineText},x?e.createElement("span",{className:m.caret},e.createElement("span",{className:m.caretAfter,style:{background:p.themeColor}})):null));return(({consoleFocused:n,enableInput:o,commands:a,defaultHandler:i,errorMessage:s,prompt:c})=>{const d=e.useContext(r),m=e.useContext(l),{getPreviousCommand:p,getNextCommand:f,store:C,send:_}=g(),x=e.useRef(null),h=e.useCallback((()=>{x.current&&x.current.cancel();let t=e.createElement(e.Fragment,null,C.bufferedContent,e.createElement("span",{style:{color:m.themePromptColor}},c),e.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),e.createElement("br",null));"processing"===C.currentLineStatus&&(t=e.createElement(e.Fragment,null,C.bufferedContent)),_({type:"CANCEL",cancelNode:t})}),[c,_,C.bufferedContent,C.currentLineStatus,C.editorInput,d.lineText,d.preWhiteSpace,m.themePromptColor]),b=e.useCallback((()=>y(void 0,void 0,void 0,(function*(){const[n,...r]=C.editorInput.trim().split(" "),o=C.editorInput.trim();let l="";const p=e.createElement(e.Fragment,null,C.bufferedContent,e.createElement("span",{style:{color:m.themePromptColor}},c),e.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),e.createElement("br",null));if(_({type:"SUBMIT",loaderNode:p,command:o}),u(o))return;if(C.editorInput){const e=r.join(" ");if(o&&a[o]){const n=a[o];"function"==typeof n?(x.current=new t((t=>{t(n(e))})),l=yield x.current):l=n}else"function"==typeof i?(x.current=new t((t=>{t(i(n,e))})),l=yield x.current):"function"==typeof s?(x.current=new t((t=>{t(s(n,e))})),l=yield x.current):l=s}const f=e.createElement(e.Fragment,null,C.bufferedContent,e.createElement("span",{style:{color:m.themePromptColor}},c),e.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),l?e.createElement("span",null,e.createElement("br",null),l):null,e.createElement("br",null));_({type:"SUBMIT_SUCCESS",successNode:f})}))),[_,C.bufferedContent,C.editorInput]),E=e.useCallback((e=>{if(!n)return;if(!o)return;e.preventDefault();const t=e.key;let r=null;if("Enter"===t)"processing"!==C.currentLineStatus&&b();else if("Backspace"===t)C.editorInput&&0!==C.editorInput.length&&_({type:"DELETE"});else if("ArrowUp"===t)r=p(),r&&_({type:"ARROW_UP",previousCommand:r});else if("ArrowDown"===t)r=f(),_(r?{type:"ARROW_DOWN",nextCommand:r}:{type:"RESET_CARET_POSITION"});else if("ArrowLeft"===t)C.caretPosition>0&&_({type:"ARROW_LEFT"}),r=C.editorInput;else if("ArrowRight"===t)C.caretPosition<C.editorInput.length&&_({type:"ARROW_RIGHT"}),r=C.editorInput;else if((e.metaKey||e.ctrlKey)&&"l"===t.toLowerCase())_({type:"CLEAR"});else if((e.metaKey||e.ctrlKey)&&"v"===t.toLowerCase())navigator.clipboard.readText().then((e=>{_({type:"PASTE",text:e})}));else if((e.metaKey||e.ctrlKey)&&"c"===t.toLowerCase()){const e=window.getSelection().toString();e?navigator.clipboard.writeText(e).then((()=>{_({type:"COPY"})})):h()}else t&&1===t.length&&_({type:"TYPE",text:t})}),[h,n,o,f,p,b,_,C.caretPosition,C.currentLineStatus,C.editorInput]);e.useEffect((()=>(document.addEventListener("keydown",E),()=>{document.removeEventListener("keydown",E)})),[E])})({commands:i,errorMessage:s,defaultHandler:d,enableInput:c,prompt:a,consoleFocused:o}),h},v=(t,n)=>{e.useEffect((()=>{n.current&&(n.current.scrollTop=n.current.scrollHeight)}),[t])};function P(t){const n=e.useRef(null),o=e.useContext(r),a=e.useContext(l),{store:i,send:s}=g();v(i.bufferedContent,n);const{enableInput:c,caret:d,consoleFocused:u,prompt:m,commands:p,welcomeMessage:f,errorMessage:C,showControlBar:_,defaultHandler:x,rounded:h}=t,b=h?o["terminal--rounded"]:"",y=E(d,u,m,p,C,c,x);return e.createElement("div",{id:"terminalEditor",tabIndex:0,ref:n,onMouseDown:e=>{n.current&&2===e.button&&(n.current.contentEditable="true",setTimeout((()=>{n.current.contentEditable="false"}),20))},onPaste:e=>{e.preventDefault(),s({type:"PASTE",text:e.clipboardData.getData("text")})},className:`${o.editor} ${b} ${_?o.editorWithTopBar:null}`,style:{background:a.themeBGColor}},e.createElement(e.Fragment,null,f,i.bufferedContent,y))}function S(t){const n=e.useRef(null),[o,a]=e.useState(!0),i=e.useContext(r),d=e.useContext(l);s(n,o,a);const{caret:u=!0,theme:m="light",showControlBar:p=!0,showControlButtons:f=!0,controlButtonLabels:C=["close","minimize","maximize"],prompt:_=">>>",commands:x={},welcomeMessage:h="",errorMessage:b="not found!",enableInput:g=!0,defaultHandler:y=null,rounded:E=!1}=t,v=E?i["terminal--rounded"]:"",S=p?e.createElement(c,{consoleFocused:o,showControlButtons:f,controlButtonLabels:C}):null,T=e.createElement(P,{caret:u,consoleFocused:o,prompt:_,commands:x,welcomeMessage:h,errorMessage:b,enableInput:g,showControlBar:p,defaultHandler:y,rounded:E});return e.createElement("div",{ref:n,id:i.terminalContainer,className:i[`theme--${m}`],"data-testid":"terminal"},e.createElement("div",{className:`${i.terminal} ${v}`,style:{background:d.themeToolbarColor,color:d.themeColor}},S,T))}function T(t){const{children:n}=t;return e.createElement(o,null,e.createElement(i,null,n))}function B(t){return e.createElement(T,null,e.createElement(S,Object.assign({},t)))}const I=function({children:t,useLocalStorage:n=!0}){const[r,o]=e.useState(null),a={bufferedContent:null,commandsHistory:n?f():[],editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""},[l,i]=e.useReducer(x,a),s=e.useCallback((e=>{"SUBMIT"===e.type&&o(l.commandsHistory.length),i(e)}),[l.commandsHistory.length]);e.useEffect((()=>{o(l.commandsHistory.length)}),[l.commandsHistory.length]);const c=e.useMemo((()=>({send:s,store:l,getPreviousCommand:()=>{if(0===r)return 0===l.commandsHistory.length?"":l.commandsHistory[0];const e=l.commandsHistory[r-1];return r>0&&o(r-1),e},getNextCommand:()=>{if(r+1<=l.commandsHistory.length){const e=l.commandsHistory[r+1];return o(r+1),e}return""}})),[r,s,l]);return e.createElement(_.Provider,{value:c},t)};var w={ReactTerminal:B,TerminalContextProvider:I};export{B as ReactTerminal,I as TerminalContextProvider,w as default}; | ||
import*as e from"react";import{CancelablePromise as t}from"cancelable-promise";!function(e,t){void 0===t&&(t={});var n=t.insertAt;if(e&&"undefined"!=typeof document){var r=document.head||document.getElementsByTagName("head")[0],o=document.createElement("style");o.type="text/css","top"===n&&r.firstChild?r.insertBefore(o,r.firstChild):r.appendChild(o),o.styleSheet?o.styleSheet.cssText=e:o.appendChild(document.createTextNode(e))}}('@import url("https://fonts.googleapis.com/css?family=Source+Code+Pro&display=swap");\n#index-module_terminalContainer__i6yCT {\n --control-default-bg-color: #ccc;\n --control-close-bg-color: #fc5b57;\n --control-minimize-bg-color: #e5bf3c;\n --control-maximize-bg-color: #57c038;\n}\n\n.index-module_controls__-Pz-7 {\n text-align: left;\n height: 26px;\n position: relative;\n top: 4px;\n margin-left: 4px;\n}\n.index-module_controls__-Pz-7 .index-module_consoleCtrl__wASVf {\n display: inline-block;\n width: 13px;\n height: 13px;\n margin: 0 3px;\n border-radius: 50%;\n background: var(--control-default-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_close__Etu85 {\n background: var(--control-close-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_minimize__bT0-I {\n background: var(--control-minimize-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_maximize__mPPF7 {\n background: var(--control-maximize-bg-color);\n}\n\n.index-module_editor__CBDCJ {\n text-align: left;\n height: 100%;\n padding: 10px 15px;\n overflow-wrap: break-word;\n overflow-y: scroll;\n outline: none;\n font-family: "Source Code Pro", monospace;\n font-size: 18px;\n line-height: 22px;\n caret-color: transparent;\n --scroll-bar-thumb-color: transparent;\n --scroll-bar-track-color: transparent;\n --scroll-bar-width: 10px;\n scrollbar-color: var(--scroll-bar-thumb-color) var(--scroll-bar-track-color);\n scrollbar-width: var(--scroll-bar-width);\n}\n.index-module_editor--rounded__-VOd4 {\n border-radius: 5px;\n}\n.index-module_editor__CBDCJ::selection {\n background: transparent;\n}\n.index-module_editor__CBDCJ:focus, .index-module_editor__CBDCJ:hover {\n --scroll-bar-thumb-color: rgb(211 211 211 / 40%);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar {\n width: var(--scroll-bar-width);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar-track {\n background-color: var(--scroll-bar-track-color);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar-thumb {\n background-color: var(--scroll-bar-thumb-color);\n}\n.index-module_editor__CBDCJ .index-module_lineText__avQ48 {\n display: inline;\n position: relative;\n top: -1px;\n margin-left: 8px;\n}\n.index-module_editor__CBDCJ .index-module_preWhiteSpace__v4IYf {\n white-space: pre-wrap;\n}\n.index-module_editor__CBDCJ .index-module_preWhiteSpace__v4IYf:focus-visible {\n outline: none;\n}\n.index-module_editor__CBDCJ .index-module_charUnderCaret__-M0qT {\n z-index: 10;\n position: relative;\n}\n.index-module_editor__CBDCJ .index-module_caret__YobLn {\n position: relative;\n}\n.index-module_editor__CBDCJ .index-module_caret__YobLn .index-module_caretAfter__SMMlh {\n content: "";\n position: absolute;\n top: 2px;\n width: 10px;\n height: 22px;\n z-index: 10;\n}\n@-moz-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@-webkit-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@-o-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n.index-module_editor__CBDCJ a {\n color: #1a87b5;\n}\n\n.index-module_editorWithTopBar__opXWp {\n height: calc(100% - 46px);\n}\n\n#index-module_terminalContainer__i6yCT {\n height: 100%;\n}\n\n.index-module_terminal__EWHbJ {\n height: 100%;\n width: 100%;\n margin: auto;\n}\n.index-module_terminal--rounded__DZuxh {\n border-radius: 5px;\n}');const n={terminalContainer:"index-module_terminalContainer__i6yCT",controls:"index-module_controls__-Pz-7",consoleCtrl:"index-module_consoleCtrl__wASVf",close:"index-module_close__Etu85",minimize:"index-module_minimize__bT0-I",maximize:"index-module_maximize__mPPF7",editor:"index-module_editor__CBDCJ","editor--rounded":"index-module_editor--rounded__-VOd4",lineText:"index-module_lineText__avQ48",preWhiteSpace:"index-module_preWhiteSpace__v4IYf",charUnderCaret:"index-module_charUnderCaret__-M0qT",caret:"index-module_caret__YobLn",caretAfter:"index-module_caretAfter__SMMlh",editorWithTopBar:"index-module_editorWithTopBar__opXWp",terminal:"index-module_terminal__EWHbJ","terminal--rounded":"index-module_terminal--rounded__DZuxh",blink:"index-module_blink__l83VF"},r=e.createContext(null),o=t=>{const{children:o}=t;return e.createElement(r.Provider,{value:n},o)};const a={dark:{themeBGColor:"rgb(2, 40, 51)",themeToolbarColor:"rgb(19, 21, 25)",themeColor:"rgb(131, 148, 150)",themePromptColor:"rgb(169, 23, 168)"},light:{themeBGColor:"#fdf6e4",themeToolbarColor:"#d8d8d8",themeColor:"#333333",themePromptColor:"#a917a8"},dracula:{themeBGColor:"#272B36",themeToolbarColor:"#DBDBDB",themeColor:"#FFFEFC",themePromptColor:"#a917a8"},"material-dark":{themeBGColor:"#151515",themeToolbarColor:"#424242",themeColor:"#fff",themePromptColor:"#42A5F5"},"material-light":{themeBGColor:"#fafafa",themeToolbarColor:"#424242",themeColor:"#151515",themePromptColor:"#42A5F5"},"material-ocean":{themeBGColor:"#263238",themeToolbarColor:"#37474F",themeColor:"#fff",themePromptColor:"#C3D82C"},matrix:{themeBGColor:"#0D0208",themeToolbarColor:"#0D0208",themeColor:"#00FF41",themePromptColor:"#008F11"}},i=e.createContext(null),l=t=>{const{children:n}=t,r=Object.assign(Object.assign({},a),n.props.themes||{}),o=r[n.props.theme]||r.light;return e.createElement(i.Provider,{value:o},n)},s=(t,n,r)=>{const o=e=>{t.current&&!t.current.contains(e.target)?r(!1):r(!0)};e.useEffect((()=>(document.addEventListener("mousedown",o),()=>{document.removeEventListener("mousedown",o)})))};function c(t){const n=e.useContext(r),{controlButtonLabels:o}=t,{showControlButtons:a}=t,i=a?o.map((t=>e.createElement("div",{key:t,className:`${n.consoleCtrl} ${n[t]}`}))):null;return e.createElement("div",{className:n.controls},i)}const d=["clear","cls"],u=e=>d.includes(e);class m{static splitStringAtIndex(e,t){return e?[e.substring(0,t),e.substring(t)]:["",""]}static splitStringAtNextSpace(e,t){if(!e)return["",""];let n=e.indexOf(" ",t);for(-1===n&&(n=e.length);n<e.length&&" "===e[n];)n++;return[e.substring(0,n),e.substring(n)]}static splitStringAtPreviousSpace(e,t){if(!e)return["",""];let n=e.substring(0,t).trimEnd().lastIndexOf(" ");return-1===n||0===n?n=0:n+=1,[e.substring(0,n),e.substring(n)]}}const p="react-terminal-plus-commandHistory",f=()=>{var e;try{const t=null!==(e=localStorage.getItem(p))&&void 0!==e?e:"[]";return JSON.parse(t)}catch(e){return console.error(e),[]}},C=e=>{let t=f();t.push(e);try{JSON.stringify(t).length>52e5&&(t=t.slice(50)),localStorage.setItem(p,JSON.stringify(t))}catch(n){console.error(n),t=[e],localStorage.setItem(p,JSON.stringify(t))}},_=e.createContext(null);function x(e,t){switch(e.currentLineStatus){case"processing":switch(t.type){case"CANCEL":return Object.assign(Object.assign({},e),{bufferedContent:t.cancelNode,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});case"SUBMIT_SUCCESS":return Object.assign(Object.assign({},e),{bufferedContent:t.successNode,currentLineStatus:"success",editorInput:"",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});default:return e}case"success":case"error":case"idle":switch(t.type){case"CLEAR":return Object.assign(Object.assign({},e),{bufferedContent:null});case"CANCEL":return Object.assign(Object.assign({},e),{bufferedContent:t.cancelNode,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});case"SUBMIT":{const{command:n}=t,r=[...e.commandsHistory];return n&&n!==r[r.length-1]&&(r.push(n),C(n)),u(n)?Object.assign(Object.assign({},e),{commandsHistory:r,bufferedContent:null,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""}):Object.assign(Object.assign({},e),{commandsHistory:r,bufferedContent:t.loaderNode,currentLineStatus:"processing",editorInput:"",caretPosition:0,textBeforeCaret:"",textAfterCaret:""})}case"TYPE":{const[n,r]=g(e),o=n+t.text+r,a=e.caretPosition+1,[i,l]=h(o,a);return Object.assign(Object.assign({},e),{caretPosition:a,editorInput:o,textAfterCaret:l,textBeforeCaret:i})}case"DELETE":{const[t,n]=g(e),r=t.slice(0,-1)+n,o=e.caretPosition-1,[a,i]=h(r,o);return Object.assign(Object.assign({},e),{editorInput:r,caretPosition:o,textAfterCaret:i,textBeforeCaret:a})}case"COPY":return e;case"PASTE":{const[n,r]=g(e),o=n+t.text+r,a=e.caretPosition+t.text.length,[i,l]=h(o,a);return Object.assign(Object.assign({},e),{editorInput:o,caretPosition:a,textAfterCaret:l,textBeforeCaret:i})}case"ARROW_UP":{const n=t.previousCommand,r=t.previousCommand.length,[o,a]=h(n,r);return Object.assign(Object.assign({},e),{editorInput:n,caretPosition:r,textAfterCaret:a,textBeforeCaret:o})}case"ARROW_DOWN":{const n=t.nextCommand,r=t.nextCommand.length,[o,a]=h(n,r);return Object.assign(Object.assign({},e),{editorInput:n,caretPosition:r,textAfterCaret:a,textBeforeCaret:o})}case"RESET_CARET_POSITION":return Object.assign(Object.assign({},e),{editorInput:"",textBeforeCaret:"",textAfterCaret:"",caretPosition:0});case"ARROW_LEFT":{const t=e.caretPosition-1,[n,r]=h(e.editorInput,t);return Object.assign(Object.assign({},e),{caretPosition:t,textAfterCaret:r,textBeforeCaret:n})}case"ARROW_RIGHT":{const t=e.caretPosition+1,[n,r]=h(e.editorInput,t);return Object.assign(Object.assign({},e),{caretPosition:t,textAfterCaret:r,textBeforeCaret:n})}case"FORWARD_WORD":{const[t,n]=function(e,t){const[n,r]=m.splitStringAtNextSpace(e,t);return[n,r]}(e.editorInput,e.caretPosition);return Object.assign(Object.assign({},e),{caretPosition:t.length,textAfterCaret:n,textBeforeCaret:t})}case"BACKWARD_WORD":{const[t,n]=function(e,t){const[n,r]=m.splitStringAtPreviousSpace(e,t);return[n,r]}(e.editorInput,e.caretPosition);return Object.assign(Object.assign({},e),{caretPosition:t.length,textAfterCaret:n,textBeforeCaret:t})}case"MOVE_CARET_TO_START":return Object.assign(Object.assign({},e),{caretPosition:0,textAfterCaret:e.editorInput,textBeforeCaret:""});case"MOVE_CARET_TO_END":return Object.assign(Object.assign({},e),{caretPosition:e.editorInput.length,textAfterCaret:"",textBeforeCaret:e.editorInput});default:throw new Error(`Unhandled action type: ${JSON.stringify(t)}`)}default:return e}}function g(e){const[t,n]=m.splitStringAtIndex(e.editorInput,e.caretPosition);return[t,n]}function h(e,t){const[n,r]=m.splitStringAtIndex(e,t);return[n,r]}function b(){const t=e.useContext(_);if(void 0===t)throw new Error("useTerminal must be used within a TerminalContextProvider");return t}function y(e,t,n,r){return new(n||(n=Promise))((function(o,a){function i(e){try{s(r.next(e))}catch(e){a(e)}}function l(e){try{s(r.throw(e))}catch(e){a(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(i,l)}s((r=r.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const E=(n,o,a,l,s,c,d)=>{const m=e.useContext(r),p=e.useContext(i),{store:f}=b(),C=f.textAfterCaret[0],_=f.textAfterCaret.slice(1),x=o&&n,g="processing"!==f.currentLineStatus?e.createElement(e.Fragment,null,e.createElement("span",{style:{color:p.themePromptColor}},a),e.createElement("div",{className:m.lineText},e.createElement("span",{className:m.preWhiteSpace},f.textBeforeCaret),x?e.createElement("span",{className:m.caret},e.createElement("span",{className:m.caretAfter,style:{background:p.themeColor}})):null,e.createElement("span",{className:`${m.preWhiteSpace}`},e.createElement("span",{style:{color:x?p.themeBGColor:p.themeColor},className:m.charUnderCaret},C),_))):e.createElement(e.Fragment,null,e.createElement("div",{className:m.lineText},x?e.createElement("span",{className:m.caret},e.createElement("span",{className:m.caretAfter,style:{background:p.themeColor}})):null));return(({consoleFocused:n,enableInput:o,commands:a,defaultHandler:l,errorMessage:s,prompt:c})=>{const d=e.useContext(r),m=e.useContext(i),{getPreviousCommand:p,getNextCommand:f,store:C,send:_}=b(),x=e.useRef(null),g=e.useCallback((()=>{x.current&&x.current.cancel();let t=e.createElement(e.Fragment,null,C.bufferedContent,e.createElement("span",{style:{color:m.themePromptColor}},c),e.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),e.createElement("br",null));"processing"===C.currentLineStatus&&(t=e.createElement(e.Fragment,null,C.bufferedContent)),_({type:"CANCEL",cancelNode:t})}),[c,_,C.bufferedContent,C.currentLineStatus,C.editorInput,d.lineText,d.preWhiteSpace,m.themePromptColor]),h=e.useCallback((()=>y(void 0,void 0,void 0,(function*(){const[n,...r]=C.editorInput.trim().split(" "),o=C.editorInput.trim();let i="";const p=e.createElement(e.Fragment,null,C.bufferedContent,e.createElement("span",{style:{color:m.themePromptColor}},c),e.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),e.createElement("br",null));if(_({type:"SUBMIT",loaderNode:p,command:o}),u(o))return;if(C.editorInput){const e=r.join(" ");if(o&&a[o]){const n=a[o];"function"==typeof n?(x.current=new t((t=>{t(n(e))})),i=yield x.current):i=n}else"function"==typeof l?(x.current=new t((t=>{t(l(n,e))})),i=yield x.current):"function"==typeof s?(x.current=new t((t=>{t(s(n,e))})),i=yield x.current):i=s}const f=e.createElement(e.Fragment,null,C.bufferedContent,e.createElement("span",{style:{color:m.themePromptColor}},c),e.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),i?e.createElement("span",null,e.createElement("br",null),i):null,e.createElement("br",null));_({type:"SUBMIT_SUCCESS",successNode:f})}))),[_,C.bufferedContent,C.editorInput]),E=e.useCallback((e=>{if(!n)return;if(!o)return;e.preventDefault();const t=e.key;let r=null;if("Enter"===t)"processing"!==C.currentLineStatus&&h();else if("Backspace"===t)C.editorInput&&0!==C.editorInput.length&&_({type:"DELETE"});else if("ArrowUp"===t)r=p(),r&&_({type:"ARROW_UP",previousCommand:r});else if("ArrowDown"===t)r=f(),_(r?{type:"ARROW_DOWN",nextCommand:r}:{type:"RESET_CARET_POSITION"});else if((e.metaKey||e.ctrlKey)&&"ArrowLeft"===t)_({type:"BACKWARD_WORD"});else if((e.metaKey||e.ctrlKey)&&"ArrowRight"==t)_({type:"FORWARD_WORD"});else if("ArrowLeft"===t)C.caretPosition>0&&_({type:"ARROW_LEFT"}),r=C.editorInput;else if("ArrowRight"===t)C.caretPosition<C.editorInput.length&&_({type:"ARROW_RIGHT"}),r=C.editorInput;else if((e.metaKey||e.ctrlKey)&&"l"===t.toLowerCase())_({type:"CLEAR"});else if((e.metaKey||e.ctrlKey)&&"v"===t.toLowerCase())navigator.clipboard.readText().then((e=>{_({type:"PASTE",text:e})}));else if((e.metaKey||e.ctrlKey)&&"c"===t.toLowerCase()){const e=window.getSelection().toString();e?navigator.clipboard.writeText(e).then((()=>{_({type:"COPY"})})):g()}else(e.metaKey||e.ctrlKey)&&"a"==t.toLowerCase()||"Home"===t?_({type:"MOVE_CARET_TO_START"}):(e.metaKey||e.ctrlKey)&&"e"==t.toLowerCase()||"End"===t?_({type:"MOVE_CARET_TO_END"}):t&&1===t.length&&_({type:"TYPE",text:t})}),[g,n,o,f,p,h,_,C.caretPosition,C.currentLineStatus,C.editorInput]);e.useEffect((()=>(document.addEventListener("keydown",E),()=>{document.removeEventListener("keydown",E)})),[E])})({commands:l,errorMessage:s,defaultHandler:d,enableInput:c,prompt:a,consoleFocused:o}),g},O=(t,n)=>{e.useEffect((()=>{n.current&&(n.current.scrollTop=n.current.scrollHeight)}),[t])};function A(t){const n=e.useRef(null),o=e.useContext(r),a=e.useContext(i),{store:l,send:s}=b();O(l.bufferedContent,n);const{enableInput:c,caret:d,consoleFocused:u,prompt:m,commands:p,welcomeMessage:f,errorMessage:C,showControlBar:_,defaultHandler:x,rounded:g}=t,h=g?o["terminal--rounded"]:"",y=E(d,u,m,p,C,c,x);return e.createElement("div",{id:"terminalEditor",tabIndex:0,ref:n,onMouseDown:e=>{n.current&&2===e.button&&(n.current.contentEditable="true",setTimeout((()=>{n.current.contentEditable="false"}),20))},onPaste:e=>{e.preventDefault(),s({type:"PASTE",text:e.clipboardData.getData("text")})},className:`${o.editor} ${h} ${_?o.editorWithTopBar:null}`,style:{background:a.themeBGColor}},e.createElement(e.Fragment,null,f,l.bufferedContent,y))}function T(t){const n=e.useRef(null),[o,a]=e.useState(!0),l=e.useContext(r),d=e.useContext(i);s(n,o,a);const{caret:u=!0,theme:m="light",showControlBar:p=!0,showControlButtons:f=!0,controlButtonLabels:C=["close","minimize","maximize"],prompt:_=">>>",commands:x={},welcomeMessage:g="",errorMessage:h="not found!",enableInput:b=!0,defaultHandler:y=null,rounded:E=!1}=t,O=E?l["terminal--rounded"]:"",T=p?e.createElement(c,{consoleFocused:o,showControlButtons:f,controlButtonLabels:C}):null,S=e.createElement(A,{caret:u,consoleFocused:o,prompt:_,commands:x,welcomeMessage:g,errorMessage:h,enableInput:b,showControlBar:p,defaultHandler:y,rounded:E});return e.createElement("div",{ref:n,id:l.terminalContainer,className:l[`theme--${m}`],"data-testid":"terminal"},e.createElement("div",{className:`${l.terminal} ${O}`,style:{background:d.themeToolbarColor,color:d.themeColor}},T,S))}function S(t){const{children:n}=t;return e.createElement(o,null,e.createElement(l,null,n))}function P(t){return e.createElement(S,null,e.createElement(T,Object.assign({},t)))}const B=function({children:t,useLocalStorage:n=!0}){const[r,o]=e.useState(null),a={bufferedContent:null,commandsHistory:n?f():[],editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""},[i,l]=e.useReducer(x,a),s=e.useCallback((e=>{"SUBMIT"===e.type&&o(i.commandsHistory.length),l(e)}),[i.commandsHistory.length]);e.useEffect((()=>{o(i.commandsHistory.length)}),[i.commandsHistory.length]);const c=e.useMemo((()=>({send:s,store:i,getPreviousCommand:()=>{if(0===r)return 0===i.commandsHistory.length?"":i.commandsHistory[0];const e=i.commandsHistory[r-1];return r>0&&o(r-1),e},getNextCommand:()=>{if(r+1<=i.commandsHistory.length){const e=i.commandsHistory[r+1];return o(r+1),e}return""}})),[r,s,i]);return e.createElement(_.Provider,{value:c},t)};var v={ReactTerminal:P,TerminalContextProvider:B};export{P as ReactTerminal,B as TerminalContextProvider,v as default}; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("cancelable-promise");function n(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var r=n(e);!function(e,t){void 0===t&&(t={});var n=t.insertAt;if(e&&"undefined"!=typeof document){var r=document.head||document.getElementsByTagName("head")[0],o=document.createElement("style");o.type="text/css","top"===n&&r.firstChild?r.insertBefore(o,r.firstChild):r.appendChild(o),o.styleSheet?o.styleSheet.cssText=e:o.appendChild(document.createTextNode(e))}}('@import url("https://fonts.googleapis.com/css?family=Source+Code+Pro&display=swap");\n#index-module_terminalContainer__i6yCT {\n --control-default-bg-color: #ccc;\n --control-close-bg-color: #fc5b57;\n --control-minimize-bg-color: #e5bf3c;\n --control-maximize-bg-color: #57c038;\n}\n\n.index-module_controls__-Pz-7 {\n text-align: left;\n height: 26px;\n position: relative;\n top: 4px;\n margin-left: 4px;\n}\n.index-module_controls__-Pz-7 .index-module_consoleCtrl__wASVf {\n display: inline-block;\n width: 13px;\n height: 13px;\n margin: 0 3px;\n border-radius: 50%;\n background: var(--control-default-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_close__Etu85 {\n background: var(--control-close-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_minimize__bT0-I {\n background: var(--control-minimize-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_maximize__mPPF7 {\n background: var(--control-maximize-bg-color);\n}\n\n.index-module_editor__CBDCJ {\n text-align: left;\n height: 100%;\n padding: 10px 15px;\n overflow-wrap: break-word;\n overflow-y: scroll;\n outline: none;\n font-family: "Source Code Pro", monospace;\n font-size: 18px;\n line-height: 22px;\n caret-color: transparent;\n --scroll-bar-thumb-color: transparent;\n --scroll-bar-track-color: transparent;\n --scroll-bar-width: 10px;\n scrollbar-color: var(--scroll-bar-thumb-color) var(--scroll-bar-track-color);\n scrollbar-width: var(--scroll-bar-width);\n}\n.index-module_editor--rounded__-VOd4 {\n border-radius: 5px;\n}\n.index-module_editor__CBDCJ::selection {\n background: transparent;\n}\n.index-module_editor__CBDCJ:focus, .index-module_editor__CBDCJ:hover {\n --scroll-bar-thumb-color: rgb(211 211 211 / 40%);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar {\n width: var(--scroll-bar-width);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar-track {\n background-color: var(--scroll-bar-track-color);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar-thumb {\n background-color: var(--scroll-bar-thumb-color);\n}\n.index-module_editor__CBDCJ .index-module_lineText__avQ48 {\n display: inline;\n position: relative;\n top: -1px;\n margin-left: 8px;\n}\n.index-module_editor__CBDCJ .index-module_preWhiteSpace__v4IYf {\n white-space: pre-wrap;\n}\n.index-module_editor__CBDCJ .index-module_preWhiteSpace__v4IYf:focus-visible {\n outline: none;\n}\n.index-module_editor__CBDCJ .index-module_charUnderCaret__-M0qT {\n z-index: 10;\n position: relative;\n}\n.index-module_editor__CBDCJ .index-module_caret__YobLn {\n position: relative;\n}\n.index-module_editor__CBDCJ .index-module_caret__YobLn .index-module_caretAfter__SMMlh {\n content: "";\n position: absolute;\n top: 2px;\n width: 10px;\n height: 22px;\n z-index: 10;\n}\n@-moz-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@-webkit-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@-o-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n.index-module_editor__CBDCJ a {\n color: #1a87b5;\n}\n\n.index-module_editorWithTopBar__opXWp {\n height: calc(100% - 46px);\n}\n\n#index-module_terminalContainer__i6yCT {\n height: 100%;\n}\n\n.index-module_terminal__EWHbJ {\n height: 100%;\n width: 100%;\n margin: auto;\n}\n.index-module_terminal--rounded__DZuxh {\n border-radius: 5px;\n}');const o={terminalContainer:"index-module_terminalContainer__i6yCT",controls:"index-module_controls__-Pz-7",consoleCtrl:"index-module_consoleCtrl__wASVf",close:"index-module_close__Etu85",minimize:"index-module_minimize__bT0-I",maximize:"index-module_maximize__mPPF7",editor:"index-module_editor__CBDCJ","editor--rounded":"index-module_editor--rounded__-VOd4",lineText:"index-module_lineText__avQ48",preWhiteSpace:"index-module_preWhiteSpace__v4IYf",charUnderCaret:"index-module_charUnderCaret__-M0qT",caret:"index-module_caret__YobLn",caretAfter:"index-module_caretAfter__SMMlh",editorWithTopBar:"index-module_editorWithTopBar__opXWp",terminal:"index-module_terminal__EWHbJ","terminal--rounded":"index-module_terminal--rounded__DZuxh",blink:"index-module_blink__l83VF"},a=r.createContext(null),l=e=>{const{children:t}=e;return r.createElement(a.Provider,{value:o},t)};const i={dark:{themeBGColor:"rgb(2, 40, 51)",themeToolbarColor:"rgb(19, 21, 25)",themeColor:"rgb(131, 148, 150)",themePromptColor:"rgb(169, 23, 168)"},light:{themeBGColor:"#fdf6e4",themeToolbarColor:"#d8d8d8",themeColor:"#333333",themePromptColor:"#a917a8"},dracula:{themeBGColor:"#272B36",themeToolbarColor:"#DBDBDB",themeColor:"#FFFEFC",themePromptColor:"#a917a8"},"material-dark":{themeBGColor:"#151515",themeToolbarColor:"#424242",themeColor:"#fff",themePromptColor:"#42A5F5"},"material-light":{themeBGColor:"#fafafa",themeToolbarColor:"#424242",themeColor:"#151515",themePromptColor:"#42A5F5"},"material-ocean":{themeBGColor:"#263238",themeToolbarColor:"#37474F",themeColor:"#fff",themePromptColor:"#C3D82C"},matrix:{themeBGColor:"#0D0208",themeToolbarColor:"#0D0208",themeColor:"#00FF41",themePromptColor:"#008F11"}},c=r.createContext(null),s=e=>{const{children:t}=e,n=Object.assign(Object.assign({},i),t.props.themes||{}),o=n[t.props.theme]||n.light;return r.createElement(c.Provider,{value:o},t)},d=(e,t,n)=>{const o=t=>{e.current&&!e.current.contains(t.target)?n(!1):n(!0)};r.useEffect((()=>(document.addEventListener("mousedown",o),()=>{document.removeEventListener("mousedown",o)})))};function u(e){const t=r.useContext(a),{controlButtonLabels:n}=e,{showControlButtons:o}=e,l=o?n.map((e=>r.createElement("div",{key:e,className:`${t.consoleCtrl} ${t[e]}`}))):null;return r.createElement("div",{className:t.controls},l)}const m=["clear","cls"],p=e=>m.includes(e);class f{static splitStringAtIndex(e,t){return e?[e.substring(0,t),e.substring(t)]:["",""]}}const C="react-terminal-plus-commandHistory",_=()=>{var e;try{const t=null!==(e=localStorage.getItem(C))&&void 0!==e?e:"[]";return JSON.parse(t)}catch(e){return console.error(e),[]}},x=e=>{let t=_();t.push(e);try{JSON.stringify(t).length>52e5&&(t=t.slice(50)),localStorage.setItem(C,JSON.stringify(t))}catch(n){console.error(n),t=[e],localStorage.setItem(C,JSON.stringify(t))}},b=r.createContext(null);function h(e,t){switch(e.currentLineStatus){case"processing":switch(t.type){case"CANCEL":return Object.assign(Object.assign({},e),{bufferedContent:t.cancelNode,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});case"SUBMIT_SUCCESS":return Object.assign(Object.assign({},e),{bufferedContent:t.successNode,currentLineStatus:"success",editorInput:"",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});default:return e}case"success":case"error":case"idle":switch(t.type){case"CLEAR":return Object.assign(Object.assign({},e),{bufferedContent:null});case"CANCEL":return Object.assign(Object.assign({},e),{bufferedContent:t.cancelNode,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});case"SUBMIT":{const{command:n}=t,r=[...e.commandsHistory];return n&&n!==r[r.length-1]&&(r.push(n),x(n)),p(n)?Object.assign(Object.assign({},e),{commandsHistory:r,bufferedContent:null,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""}):Object.assign(Object.assign({},e),{commandsHistory:r,bufferedContent:t.loaderNode,currentLineStatus:"processing",editorInput:"",caretPosition:0,textBeforeCaret:"",textAfterCaret:""})}case"TYPE":{const[n,r]=g(e),o=n+t.text+r,a=e.caretPosition+1,[l,i]=y(o,a);return Object.assign(Object.assign({},e),{caretPosition:a,editorInput:o,textAfterCaret:i,textBeforeCaret:l})}case"DELETE":{const[t,n]=g(e),r=t.slice(0,-1)+n,o=e.caretPosition-1,[a,l]=y(r,o);return Object.assign(Object.assign({},e),{editorInput:r,caretPosition:o,textAfterCaret:l,textBeforeCaret:a})}case"COPY":return e;case"PASTE":{const[n,r]=g(e),o=n+t.text+r,a=e.caretPosition+t.text.length,[l,i]=y(o,a);return Object.assign(Object.assign({},e),{editorInput:o,caretPosition:a,textAfterCaret:i,textBeforeCaret:l})}case"ARROW_UP":{const n=t.previousCommand,r=t.previousCommand.length,[o,a]=y(n,r);return Object.assign(Object.assign({},e),{editorInput:n,caretPosition:r,textAfterCaret:a,textBeforeCaret:o})}case"ARROW_DOWN":{const n=t.nextCommand,r=t.nextCommand.length,[o,a]=y(n,r);return Object.assign(Object.assign({},e),{editorInput:n,caretPosition:r,textAfterCaret:a,textBeforeCaret:o})}case"RESET_CARET_POSITION":return Object.assign(Object.assign({},e),{editorInput:"",textBeforeCaret:"",textAfterCaret:"",caretPosition:0});case"ARROW_LEFT":{const t=e.caretPosition-1,[n,r]=y(e.editorInput,t);return Object.assign(Object.assign({},e),{caretPosition:t,textAfterCaret:r,textBeforeCaret:n})}case"ARROW_RIGHT":{const t=e.caretPosition+1,[n,r]=y(e.editorInput,t);return Object.assign(Object.assign({},e),{caretPosition:t,textAfterCaret:r,textBeforeCaret:n})}default:throw new Error(`Unhandled action type: ${JSON.stringify(t)}`)}default:return e}}function g(e){const[t,n]=f.splitStringAtIndex(e.editorInput,e.caretPosition);return[t,n]}function y(e,t){const[n,r]=f.splitStringAtIndex(e,t);return[n,r]}function E(){const e=r.useContext(b);if(void 0===e)throw new Error("useTerminal must be used within a TerminalContextProvider");return e}function P(e,t,n,r){return new(n||(n=Promise))((function(o,a){function l(e){try{c(r.next(e))}catch(e){a(e)}}function i(e){try{c(r.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(l,i)}c((r=r.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const v=(e,n,o,l,i,s,d)=>{const u=r.useContext(a),m=r.useContext(c),{store:f}=E(),C=f.textAfterCaret[0],_=f.textAfterCaret.slice(1),x=n&&e,b="processing"!==f.currentLineStatus?r.createElement(r.Fragment,null,r.createElement("span",{style:{color:m.themePromptColor}},o),r.createElement("div",{className:u.lineText},r.createElement("span",{className:u.preWhiteSpace},f.textBeforeCaret),x?r.createElement("span",{className:u.caret},r.createElement("span",{className:u.caretAfter,style:{background:m.themeColor}})):null,r.createElement("span",{className:`${u.preWhiteSpace}`},r.createElement("span",{style:{color:x?m.themeBGColor:m.themeColor},className:u.charUnderCaret},C),_))):r.createElement(r.Fragment,null,r.createElement("div",{className:u.lineText},x?r.createElement("span",{className:u.caret},r.createElement("span",{className:u.caretAfter,style:{background:m.themeColor}})):null));return(({consoleFocused:e,enableInput:n,commands:o,defaultHandler:l,errorMessage:i,prompt:s})=>{const d=r.useContext(a),u=r.useContext(c),{getPreviousCommand:m,getNextCommand:f,store:C,send:_}=E(),x=r.useRef(null),b=r.useCallback((()=>{x.current&&x.current.cancel();let e=r.createElement(r.Fragment,null,C.bufferedContent,r.createElement("span",{style:{color:u.themePromptColor}},s),r.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),r.createElement("br",null));"processing"===C.currentLineStatus&&(e=r.createElement(r.Fragment,null,C.bufferedContent)),_({type:"CANCEL",cancelNode:e})}),[s,_,C.bufferedContent,C.currentLineStatus,C.editorInput,d.lineText,d.preWhiteSpace,u.themePromptColor]),h=r.useCallback((()=>P(void 0,void 0,void 0,(function*(){const[e,...n]=C.editorInput.trim().split(" "),a=C.editorInput.trim();let c="";const m=r.createElement(r.Fragment,null,C.bufferedContent,r.createElement("span",{style:{color:u.themePromptColor}},s),r.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),r.createElement("br",null));if(_({type:"SUBMIT",loaderNode:m,command:a}),p(a))return;if(C.editorInput){const r=n.join(" ");if(a&&o[a]){const e=o[a];"function"==typeof e?(x.current=new t.CancelablePromise((t=>{t(e(r))})),c=yield x.current):c=e}else"function"==typeof l?(x.current=new t.CancelablePromise((t=>{t(l(e,r))})),c=yield x.current):"function"==typeof i?(x.current=new t.CancelablePromise((t=>{t(i(e,r))})),c=yield x.current):c=i}const f=r.createElement(r.Fragment,null,C.bufferedContent,r.createElement("span",{style:{color:u.themePromptColor}},s),r.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),c?r.createElement("span",null,r.createElement("br",null),c):null,r.createElement("br",null));_({type:"SUBMIT_SUCCESS",successNode:f})}))),[_,C.bufferedContent,C.editorInput]),g=r.useCallback((t=>{if(!e)return;if(!n)return;t.preventDefault();const r=t.key;let o=null;if("Enter"===r)"processing"!==C.currentLineStatus&&h();else if("Backspace"===r)C.editorInput&&0!==C.editorInput.length&&_({type:"DELETE"});else if("ArrowUp"===r)o=m(),o&&_({type:"ARROW_UP",previousCommand:o});else if("ArrowDown"===r)o=f(),_(o?{type:"ARROW_DOWN",nextCommand:o}:{type:"RESET_CARET_POSITION"});else if("ArrowLeft"===r)C.caretPosition>0&&_({type:"ARROW_LEFT"}),o=C.editorInput;else if("ArrowRight"===r)C.caretPosition<C.editorInput.length&&_({type:"ARROW_RIGHT"}),o=C.editorInput;else if((t.metaKey||t.ctrlKey)&&"l"===r.toLowerCase())_({type:"CLEAR"});else if((t.metaKey||t.ctrlKey)&&"v"===r.toLowerCase())navigator.clipboard.readText().then((e=>{_({type:"PASTE",text:e})}));else if((t.metaKey||t.ctrlKey)&&"c"===r.toLowerCase()){const e=window.getSelection().toString();e?navigator.clipboard.writeText(e).then((()=>{_({type:"COPY"})})):b()}else r&&1===r.length&&_({type:"TYPE",text:r})}),[b,e,n,f,m,h,_,C.caretPosition,C.currentLineStatus,C.editorInput]);r.useEffect((()=>(document.addEventListener("keydown",g),()=>{document.removeEventListener("keydown",g)})),[g])})({commands:l,errorMessage:i,defaultHandler:d,enableInput:s,prompt:o,consoleFocused:n}),b},T=(e,t)=>{r.useEffect((()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}),[e])};function S(e){const t=r.useRef(null),n=r.useContext(a),o=r.useContext(c),{store:l,send:i}=E();T(l.bufferedContent,t);const{enableInput:s,caret:d,consoleFocused:u,prompt:m,commands:p,welcomeMessage:f,errorMessage:C,showControlBar:_,defaultHandler:x,rounded:b}=e,h=b?n["terminal--rounded"]:"",g=v(d,u,m,p,C,s,x);return r.createElement("div",{id:"terminalEditor",tabIndex:0,ref:t,onMouseDown:e=>{t.current&&2===e.button&&(t.current.contentEditable="true",setTimeout((()=>{t.current.contentEditable="false"}),20))},onPaste:e=>{e.preventDefault(),i({type:"PASTE",text:e.clipboardData.getData("text")})},className:`${n.editor} ${h} ${_?n.editorWithTopBar:null}`,style:{background:o.themeBGColor}},r.createElement(r.Fragment,null,f,l.bufferedContent,g))}function O(e){const t=r.useRef(null),[n,o]=r.useState(!0),l=r.useContext(a),i=r.useContext(c);d(t,n,o);const{caret:s=!0,theme:m="light",showControlBar:p=!0,showControlButtons:f=!0,controlButtonLabels:C=["close","minimize","maximize"],prompt:_=">>>",commands:x={},welcomeMessage:b="",errorMessage:h="not found!",enableInput:g=!0,defaultHandler:y=null,rounded:E=!1}=e,P=E?l["terminal--rounded"]:"",v=p?r.createElement(u,{consoleFocused:n,showControlButtons:f,controlButtonLabels:C}):null,T=r.createElement(S,{caret:s,consoleFocused:n,prompt:_,commands:x,welcomeMessage:b,errorMessage:h,enableInput:g,showControlBar:p,defaultHandler:y,rounded:E});return r.createElement("div",{ref:t,id:l.terminalContainer,className:l[`theme--${m}`],"data-testid":"terminal"},r.createElement("div",{className:`${l.terminal} ${P}`,style:{background:i.themeToolbarColor,color:i.themeColor}},v,T))}function B(e){const{children:t}=e;return r.createElement(l,null,r.createElement(s,null,t))}function w(e){return r.createElement(B,null,r.createElement(O,Object.assign({},e)))}const I=function({children:e,useLocalStorage:t=!0}){const[n,o]=r.useState(null),a={bufferedContent:null,commandsHistory:t?_():[],editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""},[l,i]=r.useReducer(h,a),c=r.useCallback((e=>{"SUBMIT"===e.type&&o(l.commandsHistory.length),i(e)}),[l.commandsHistory.length]);r.useEffect((()=>{o(l.commandsHistory.length)}),[l.commandsHistory.length]);const s=r.useMemo((()=>({send:c,store:l,getPreviousCommand:()=>{if(0===n)return 0===l.commandsHistory.length?"":l.commandsHistory[0];const e=l.commandsHistory[n-1];return n>0&&o(n-1),e},getNextCommand:()=>{if(n+1<=l.commandsHistory.length){const e=l.commandsHistory[n+1];return o(n+1),e}return""}})),[n,c,l]);return r.createElement(b.Provider,{value:s},e)};var A={ReactTerminal:w,TerminalContextProvider:I};exports.ReactTerminal=w,exports.TerminalContextProvider=I,exports.default=A; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("cancelable-promise");function n(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var r=n(e);!function(e,t){void 0===t&&(t={});var n=t.insertAt;if(e&&"undefined"!=typeof document){var r=document.head||document.getElementsByTagName("head")[0],o=document.createElement("style");o.type="text/css","top"===n&&r.firstChild?r.insertBefore(o,r.firstChild):r.appendChild(o),o.styleSheet?o.styleSheet.cssText=e:o.appendChild(document.createTextNode(e))}}('@import url("https://fonts.googleapis.com/css?family=Source+Code+Pro&display=swap");\n#index-module_terminalContainer__i6yCT {\n --control-default-bg-color: #ccc;\n --control-close-bg-color: #fc5b57;\n --control-minimize-bg-color: #e5bf3c;\n --control-maximize-bg-color: #57c038;\n}\n\n.index-module_controls__-Pz-7 {\n text-align: left;\n height: 26px;\n position: relative;\n top: 4px;\n margin-left: 4px;\n}\n.index-module_controls__-Pz-7 .index-module_consoleCtrl__wASVf {\n display: inline-block;\n width: 13px;\n height: 13px;\n margin: 0 3px;\n border-radius: 50%;\n background: var(--control-default-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_close__Etu85 {\n background: var(--control-close-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_minimize__bT0-I {\n background: var(--control-minimize-bg-color);\n}\n.index-module_controls__-Pz-7 .index-module_maximize__mPPF7 {\n background: var(--control-maximize-bg-color);\n}\n\n.index-module_editor__CBDCJ {\n text-align: left;\n height: 100%;\n padding: 10px 15px;\n overflow-wrap: break-word;\n overflow-y: scroll;\n outline: none;\n font-family: "Source Code Pro", monospace;\n font-size: 18px;\n line-height: 22px;\n caret-color: transparent;\n --scroll-bar-thumb-color: transparent;\n --scroll-bar-track-color: transparent;\n --scroll-bar-width: 10px;\n scrollbar-color: var(--scroll-bar-thumb-color) var(--scroll-bar-track-color);\n scrollbar-width: var(--scroll-bar-width);\n}\n.index-module_editor--rounded__-VOd4 {\n border-radius: 5px;\n}\n.index-module_editor__CBDCJ::selection {\n background: transparent;\n}\n.index-module_editor__CBDCJ:focus, .index-module_editor__CBDCJ:hover {\n --scroll-bar-thumb-color: rgb(211 211 211 / 40%);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar {\n width: var(--scroll-bar-width);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar-track {\n background-color: var(--scroll-bar-track-color);\n}\n.index-module_editor__CBDCJ::-webkit-scrollbar-thumb {\n background-color: var(--scroll-bar-thumb-color);\n}\n.index-module_editor__CBDCJ .index-module_lineText__avQ48 {\n display: inline;\n position: relative;\n top: -1px;\n margin-left: 8px;\n}\n.index-module_editor__CBDCJ .index-module_preWhiteSpace__v4IYf {\n white-space: pre-wrap;\n}\n.index-module_editor__CBDCJ .index-module_preWhiteSpace__v4IYf:focus-visible {\n outline: none;\n}\n.index-module_editor__CBDCJ .index-module_charUnderCaret__-M0qT {\n z-index: 10;\n position: relative;\n}\n.index-module_editor__CBDCJ .index-module_caret__YobLn {\n position: relative;\n}\n.index-module_editor__CBDCJ .index-module_caret__YobLn .index-module_caretAfter__SMMlh {\n content: "";\n position: absolute;\n top: 2px;\n width: 10px;\n height: 22px;\n z-index: 10;\n}\n@-moz-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@-webkit-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@-o-keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n@keyframes index-module_blink__l83VF {\n 0% {\n opacity: 0;\n -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";\n filter: alpha(opacity=0);\n }\n 100% {\n opacity: 1;\n -ms-filter: none;\n filter: none;\n }\n}\n.index-module_editor__CBDCJ a {\n color: #1a87b5;\n}\n\n.index-module_editorWithTopBar__opXWp {\n height: calc(100% - 46px);\n}\n\n#index-module_terminalContainer__i6yCT {\n height: 100%;\n}\n\n.index-module_terminal__EWHbJ {\n height: 100%;\n width: 100%;\n margin: auto;\n}\n.index-module_terminal--rounded__DZuxh {\n border-radius: 5px;\n}');const o={terminalContainer:"index-module_terminalContainer__i6yCT",controls:"index-module_controls__-Pz-7",consoleCtrl:"index-module_consoleCtrl__wASVf",close:"index-module_close__Etu85",minimize:"index-module_minimize__bT0-I",maximize:"index-module_maximize__mPPF7",editor:"index-module_editor__CBDCJ","editor--rounded":"index-module_editor--rounded__-VOd4",lineText:"index-module_lineText__avQ48",preWhiteSpace:"index-module_preWhiteSpace__v4IYf",charUnderCaret:"index-module_charUnderCaret__-M0qT",caret:"index-module_caret__YobLn",caretAfter:"index-module_caretAfter__SMMlh",editorWithTopBar:"index-module_editorWithTopBar__opXWp",terminal:"index-module_terminal__EWHbJ","terminal--rounded":"index-module_terminal--rounded__DZuxh",blink:"index-module_blink__l83VF"},a=r.createContext(null),i=e=>{const{children:t}=e;return r.createElement(a.Provider,{value:o},t)};const l={dark:{themeBGColor:"rgb(2, 40, 51)",themeToolbarColor:"rgb(19, 21, 25)",themeColor:"rgb(131, 148, 150)",themePromptColor:"rgb(169, 23, 168)"},light:{themeBGColor:"#fdf6e4",themeToolbarColor:"#d8d8d8",themeColor:"#333333",themePromptColor:"#a917a8"},dracula:{themeBGColor:"#272B36",themeToolbarColor:"#DBDBDB",themeColor:"#FFFEFC",themePromptColor:"#a917a8"},"material-dark":{themeBGColor:"#151515",themeToolbarColor:"#424242",themeColor:"#fff",themePromptColor:"#42A5F5"},"material-light":{themeBGColor:"#fafafa",themeToolbarColor:"#424242",themeColor:"#151515",themePromptColor:"#42A5F5"},"material-ocean":{themeBGColor:"#263238",themeToolbarColor:"#37474F",themeColor:"#fff",themePromptColor:"#C3D82C"},matrix:{themeBGColor:"#0D0208",themeToolbarColor:"#0D0208",themeColor:"#00FF41",themePromptColor:"#008F11"}},s=r.createContext(null),c=e=>{const{children:t}=e,n=Object.assign(Object.assign({},l),t.props.themes||{}),o=n[t.props.theme]||n.light;return r.createElement(s.Provider,{value:o},t)},d=(e,t,n)=>{const o=t=>{e.current&&!e.current.contains(t.target)?n(!1):n(!0)};r.useEffect((()=>(document.addEventListener("mousedown",o),()=>{document.removeEventListener("mousedown",o)})))};function u(e){const t=r.useContext(a),{controlButtonLabels:n}=e,{showControlButtons:o}=e,i=o?n.map((e=>r.createElement("div",{key:e,className:`${t.consoleCtrl} ${t[e]}`}))):null;return r.createElement("div",{className:t.controls},i)}const m=["clear","cls"],p=e=>m.includes(e);class f{static splitStringAtIndex(e,t){return e?[e.substring(0,t),e.substring(t)]:["",""]}static splitStringAtNextSpace(e,t){if(!e)return["",""];let n=e.indexOf(" ",t);for(-1===n&&(n=e.length);n<e.length&&" "===e[n];)n++;return[e.substring(0,n),e.substring(n)]}static splitStringAtPreviousSpace(e,t){if(!e)return["",""];let n=e.substring(0,t).trimEnd().lastIndexOf(" ");return-1===n||0===n?n=0:n+=1,[e.substring(0,n),e.substring(n)]}}const C="react-terminal-plus-commandHistory",_=()=>{var e;try{const t=null!==(e=localStorage.getItem(C))&&void 0!==e?e:"[]";return JSON.parse(t)}catch(e){return console.error(e),[]}},x=e=>{let t=_();t.push(e);try{JSON.stringify(t).length>52e5&&(t=t.slice(50)),localStorage.setItem(C,JSON.stringify(t))}catch(n){console.error(n),t=[e],localStorage.setItem(C,JSON.stringify(t))}},b=r.createContext(null);function g(e,t){switch(e.currentLineStatus){case"processing":switch(t.type){case"CANCEL":return Object.assign(Object.assign({},e),{bufferedContent:t.cancelNode,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});case"SUBMIT_SUCCESS":return Object.assign(Object.assign({},e),{bufferedContent:t.successNode,currentLineStatus:"success",editorInput:"",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});default:return e}case"success":case"error":case"idle":switch(t.type){case"CLEAR":return Object.assign(Object.assign({},e),{bufferedContent:null});case"CANCEL":return Object.assign(Object.assign({},e),{bufferedContent:t.cancelNode,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""});case"SUBMIT":{const{command:n}=t,r=[...e.commandsHistory];return n&&n!==r[r.length-1]&&(r.push(n),x(n)),p(n)?Object.assign(Object.assign({},e),{commandsHistory:r,bufferedContent:null,editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""}):Object.assign(Object.assign({},e),{commandsHistory:r,bufferedContent:t.loaderNode,currentLineStatus:"processing",editorInput:"",caretPosition:0,textBeforeCaret:"",textAfterCaret:""})}case"TYPE":{const[n,r]=h(e),o=n+t.text+r,a=e.caretPosition+1,[i,l]=y(o,a);return Object.assign(Object.assign({},e),{caretPosition:a,editorInput:o,textAfterCaret:l,textBeforeCaret:i})}case"DELETE":{const[t,n]=h(e),r=t.slice(0,-1)+n,o=e.caretPosition-1,[a,i]=y(r,o);return Object.assign(Object.assign({},e),{editorInput:r,caretPosition:o,textAfterCaret:i,textBeforeCaret:a})}case"COPY":return e;case"PASTE":{const[n,r]=h(e),o=n+t.text+r,a=e.caretPosition+t.text.length,[i,l]=y(o,a);return Object.assign(Object.assign({},e),{editorInput:o,caretPosition:a,textAfterCaret:l,textBeforeCaret:i})}case"ARROW_UP":{const n=t.previousCommand,r=t.previousCommand.length,[o,a]=y(n,r);return Object.assign(Object.assign({},e),{editorInput:n,caretPosition:r,textAfterCaret:a,textBeforeCaret:o})}case"ARROW_DOWN":{const n=t.nextCommand,r=t.nextCommand.length,[o,a]=y(n,r);return Object.assign(Object.assign({},e),{editorInput:n,caretPosition:r,textAfterCaret:a,textBeforeCaret:o})}case"RESET_CARET_POSITION":return Object.assign(Object.assign({},e),{editorInput:"",textBeforeCaret:"",textAfterCaret:"",caretPosition:0});case"ARROW_LEFT":{const t=e.caretPosition-1,[n,r]=y(e.editorInput,t);return Object.assign(Object.assign({},e),{caretPosition:t,textAfterCaret:r,textBeforeCaret:n})}case"ARROW_RIGHT":{const t=e.caretPosition+1,[n,r]=y(e.editorInput,t);return Object.assign(Object.assign({},e),{caretPosition:t,textAfterCaret:r,textBeforeCaret:n})}case"FORWARD_WORD":{const[t,n]=function(e,t){const[n,r]=f.splitStringAtNextSpace(e,t);return[n,r]}(e.editorInput,e.caretPosition);return Object.assign(Object.assign({},e),{caretPosition:t.length,textAfterCaret:n,textBeforeCaret:t})}case"BACKWARD_WORD":{const[t,n]=function(e,t){const[n,r]=f.splitStringAtPreviousSpace(e,t);return[n,r]}(e.editorInput,e.caretPosition);return Object.assign(Object.assign({},e),{caretPosition:t.length,textAfterCaret:n,textBeforeCaret:t})}case"MOVE_CARET_TO_START":return Object.assign(Object.assign({},e),{caretPosition:0,textAfterCaret:e.editorInput,textBeforeCaret:""});case"MOVE_CARET_TO_END":return Object.assign(Object.assign({},e),{caretPosition:e.editorInput.length,textAfterCaret:"",textBeforeCaret:e.editorInput});default:throw new Error(`Unhandled action type: ${JSON.stringify(t)}`)}default:return e}}function h(e){const[t,n]=f.splitStringAtIndex(e.editorInput,e.caretPosition);return[t,n]}function y(e,t){const[n,r]=f.splitStringAtIndex(e,t);return[n,r]}function E(){const e=r.useContext(b);if(void 0===e)throw new Error("useTerminal must be used within a TerminalContextProvider");return e}function O(e,t,n,r){return new(n||(n=Promise))((function(o,a){function i(e){try{s(r.next(e))}catch(e){a(e)}}function l(e){try{s(r.throw(e))}catch(e){a(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(i,l)}s((r=r.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const P=(e,n,o,i,l,c,d)=>{const u=r.useContext(a),m=r.useContext(s),{store:f}=E(),C=f.textAfterCaret[0],_=f.textAfterCaret.slice(1),x=n&&e,b="processing"!==f.currentLineStatus?r.createElement(r.Fragment,null,r.createElement("span",{style:{color:m.themePromptColor}},o),r.createElement("div",{className:u.lineText},r.createElement("span",{className:u.preWhiteSpace},f.textBeforeCaret),x?r.createElement("span",{className:u.caret},r.createElement("span",{className:u.caretAfter,style:{background:m.themeColor}})):null,r.createElement("span",{className:`${u.preWhiteSpace}`},r.createElement("span",{style:{color:x?m.themeBGColor:m.themeColor},className:u.charUnderCaret},C),_))):r.createElement(r.Fragment,null,r.createElement("div",{className:u.lineText},x?r.createElement("span",{className:u.caret},r.createElement("span",{className:u.caretAfter,style:{background:m.themeColor}})):null));return(({consoleFocused:e,enableInput:n,commands:o,defaultHandler:i,errorMessage:l,prompt:c})=>{const d=r.useContext(a),u=r.useContext(s),{getPreviousCommand:m,getNextCommand:f,store:C,send:_}=E(),x=r.useRef(null),b=r.useCallback((()=>{x.current&&x.current.cancel();let e=r.createElement(r.Fragment,null,C.bufferedContent,r.createElement("span",{style:{color:u.themePromptColor}},c),r.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),r.createElement("br",null));"processing"===C.currentLineStatus&&(e=r.createElement(r.Fragment,null,C.bufferedContent)),_({type:"CANCEL",cancelNode:e})}),[c,_,C.bufferedContent,C.currentLineStatus,C.editorInput,d.lineText,d.preWhiteSpace,u.themePromptColor]),g=r.useCallback((()=>O(void 0,void 0,void 0,(function*(){const[e,...n]=C.editorInput.trim().split(" "),a=C.editorInput.trim();let s="";const m=r.createElement(r.Fragment,null,C.bufferedContent,r.createElement("span",{style:{color:u.themePromptColor}},c),r.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),r.createElement("br",null));if(_({type:"SUBMIT",loaderNode:m,command:a}),p(a))return;if(C.editorInput){const r=n.join(" ");if(a&&o[a]){const e=o[a];"function"==typeof e?(x.current=new t.CancelablePromise((t=>{t(e(r))})),s=yield x.current):s=e}else"function"==typeof i?(x.current=new t.CancelablePromise((t=>{t(i(e,r))})),s=yield x.current):"function"==typeof l?(x.current=new t.CancelablePromise((t=>{t(l(e,r))})),s=yield x.current):s=l}const f=r.createElement(r.Fragment,null,C.bufferedContent,r.createElement("span",{style:{color:u.themePromptColor}},c),r.createElement("span",{className:`${d.lineText} ${d.preWhiteSpace}`},C.editorInput),s?r.createElement("span",null,r.createElement("br",null),s):null,r.createElement("br",null));_({type:"SUBMIT_SUCCESS",successNode:f})}))),[_,C.bufferedContent,C.editorInput]),h=r.useCallback((t=>{if(!e)return;if(!n)return;t.preventDefault();const r=t.key;let o=null;if("Enter"===r)"processing"!==C.currentLineStatus&&g();else if("Backspace"===r)C.editorInput&&0!==C.editorInput.length&&_({type:"DELETE"});else if("ArrowUp"===r)o=m(),o&&_({type:"ARROW_UP",previousCommand:o});else if("ArrowDown"===r)o=f(),_(o?{type:"ARROW_DOWN",nextCommand:o}:{type:"RESET_CARET_POSITION"});else if((t.metaKey||t.ctrlKey)&&"ArrowLeft"===r)_({type:"BACKWARD_WORD"});else if((t.metaKey||t.ctrlKey)&&"ArrowRight"==r)_({type:"FORWARD_WORD"});else if("ArrowLeft"===r)C.caretPosition>0&&_({type:"ARROW_LEFT"}),o=C.editorInput;else if("ArrowRight"===r)C.caretPosition<C.editorInput.length&&_({type:"ARROW_RIGHT"}),o=C.editorInput;else if((t.metaKey||t.ctrlKey)&&"l"===r.toLowerCase())_({type:"CLEAR"});else if((t.metaKey||t.ctrlKey)&&"v"===r.toLowerCase())navigator.clipboard.readText().then((e=>{_({type:"PASTE",text:e})}));else if((t.metaKey||t.ctrlKey)&&"c"===r.toLowerCase()){const e=window.getSelection().toString();e?navigator.clipboard.writeText(e).then((()=>{_({type:"COPY"})})):b()}else(t.metaKey||t.ctrlKey)&&"a"==r.toLowerCase()||"Home"===r?_({type:"MOVE_CARET_TO_START"}):(t.metaKey||t.ctrlKey)&&"e"==r.toLowerCase()||"End"===r?_({type:"MOVE_CARET_TO_END"}):r&&1===r.length&&_({type:"TYPE",text:r})}),[b,e,n,f,m,g,_,C.caretPosition,C.currentLineStatus,C.editorInput]);r.useEffect((()=>(document.addEventListener("keydown",h),()=>{document.removeEventListener("keydown",h)})),[h])})({commands:i,errorMessage:l,defaultHandler:d,enableInput:c,prompt:o,consoleFocused:n}),b},T=(e,t)=>{r.useEffect((()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}),[e])};function A(e){const t=r.useRef(null),n=r.useContext(a),o=r.useContext(s),{store:i,send:l}=E();T(i.bufferedContent,t);const{enableInput:c,caret:d,consoleFocused:u,prompt:m,commands:p,welcomeMessage:f,errorMessage:C,showControlBar:_,defaultHandler:x,rounded:b}=e,g=b?n["terminal--rounded"]:"",h=P(d,u,m,p,C,c,x);return r.createElement("div",{id:"terminalEditor",tabIndex:0,ref:t,onMouseDown:e=>{t.current&&2===e.button&&(t.current.contentEditable="true",setTimeout((()=>{t.current.contentEditable="false"}),20))},onPaste:e=>{e.preventDefault(),l({type:"PASTE",text:e.clipboardData.getData("text")})},className:`${n.editor} ${g} ${_?n.editorWithTopBar:null}`,style:{background:o.themeBGColor}},r.createElement(r.Fragment,null,f,i.bufferedContent,h))}function S(e){const t=r.useRef(null),[n,o]=r.useState(!0),i=r.useContext(a),l=r.useContext(s);d(t,n,o);const{caret:c=!0,theme:m="light",showControlBar:p=!0,showControlButtons:f=!0,controlButtonLabels:C=["close","minimize","maximize"],prompt:_=">>>",commands:x={},welcomeMessage:b="",errorMessage:g="not found!",enableInput:h=!0,defaultHandler:y=null,rounded:E=!1}=e,O=E?i["terminal--rounded"]:"",P=p?r.createElement(u,{consoleFocused:n,showControlButtons:f,controlButtonLabels:C}):null,T=r.createElement(A,{caret:c,consoleFocused:n,prompt:_,commands:x,welcomeMessage:b,errorMessage:g,enableInput:h,showControlBar:p,defaultHandler:y,rounded:E});return r.createElement("div",{ref:t,id:i.terminalContainer,className:i[`theme--${m}`],"data-testid":"terminal"},r.createElement("div",{className:`${i.terminal} ${O}`,style:{background:l.themeToolbarColor,color:l.themeColor}},P,T))}function v(e){const{children:t}=e;return r.createElement(i,null,r.createElement(c,null,t))}function B(e){return r.createElement(v,null,r.createElement(S,Object.assign({},e)))}const I=function({children:e,useLocalStorage:t=!0}){const[n,o]=r.useState(null),a={bufferedContent:null,commandsHistory:t?_():[],editorInput:"",currentLineStatus:"idle",caretPosition:0,textBeforeCaret:"",textAfterCaret:""},[i,l]=r.useReducer(g,a),s=r.useCallback((e=>{"SUBMIT"===e.type&&o(i.commandsHistory.length),l(e)}),[i.commandsHistory.length]);r.useEffect((()=>{o(i.commandsHistory.length)}),[i.commandsHistory.length]);const c=r.useMemo((()=>({send:s,store:i,getPreviousCommand:()=>{if(0===n)return 0===i.commandsHistory.length?"":i.commandsHistory[0];const e=i.commandsHistory[n-1];return n>0&&o(n-1),e},getNextCommand:()=>{if(n+1<=i.commandsHistory.length){const e=i.commandsHistory[n+1];return o(n+1),e}return""}})),[n,s,i]);return r.createElement(b.Provider,{value:c},e)};var w={ReactTerminal:B,TerminalContextProvider:I};exports.ReactTerminal=B,exports.TerminalContextProvider=I,exports.default=w; |
export default class Utils { | ||
static splitStringAtIndex(value: string, index: number): string[]; | ||
static splitStringAtNextSpace(value: string, index: number): string[]; | ||
static splitStringAtPreviousSpace(value: string, index: number): string[]; | ||
} |
@@ -47,2 +47,10 @@ import * as React from "react"; | ||
} | { | ||
type: "MOVE_CARET_TO_START"; | ||
} | { | ||
type: "MOVE_CARET_TO_END"; | ||
} | { | ||
type: "FORWARD_WORD"; | ||
} | { | ||
type: "BACKWARD_WORD"; | ||
} | { | ||
type: "UPDATE_BUFFERED_CONTENT"; | ||
@@ -49,0 +57,0 @@ payload: React.ReactNode; |
{ | ||
"name": "react-terminal-plus", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"description": "A improved version of `react-terminal` - react-terminal-plus", | ||
@@ -78,3 +78,3 @@ "keywords": [ | ||
"np": { | ||
"tests": true | ||
"tests": false | ||
}, | ||
@@ -81,0 +81,0 @@ "scripts": { |
@@ -110,7 +110,13 @@ # react-terminal-plus | ||
| shortcut | description | | ||
| -------- | ----------------------------------------------------------- | | ||
| ctrl + c | copy OR cancel running command if there is no selected text | | ||
| ctrl + v | paste | | ||
| ctrl + l | clear the console | | ||
| shortcut | description | | ||
| ------------------ | ----------------------------------------------------------- | | ||
| ctrl + c | copy OR cancel running command if there is no selected text | | ||
| ctrl + v | paste | | ||
| ctrl + l | clear the console | | ||
| ctrl + a | moves the cursor to the beginning of the line | | ||
| ctrl + e | moves the cursor to the end of the line | | ||
| Home | moves the cursor to the beginning of the line | | ||
| End | moves the cursor to the end of the line | | ||
| ctrl + left-arrow | moves the cursor to the backward word | | ||
| ctrl + right-arrow | moves the cursor to the forward word | | ||
@@ -117,0 +123,0 @@ ## Development |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
56794
6.97%422
5.24%130
4.84%3
50%