diff --git a/scripthaus.md b/scripthaus.md index f3c9caf9..1a8dd7bd 100644 --- a/scripthaus.md +++ b/scripthaus.md @@ -11,3 +11,9 @@ node_modules/.bin/webpack --watch --config webpack.dev.js # @scripthaus cd :current node_modules/.bin/webpack-dev-server --config webpack.dev.js --host 0.0.0.0 ``` + +```bash +# @scripthaus command typecheck +# @scripthaus cd :current +node_modules/.bin/tsc --jsx preserve --noEmit --esModuleInterop --target ES5 --experimentalDecorators --downlevelIteration src/sh2.ts +``` diff --git a/src/main.tsx b/src/main.tsx index aa18d163..9d0299f0 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -3,11 +3,12 @@ import * as mobxReact from "mobx-react"; import * as mobx from "mobx"; import {sprintf} from "sprintf-js"; import {boundMethod} from "autobind-decorator"; -import * as dayjs from 'dayjs' +import dayjs from 'dayjs' import {If, For, When, Otherwise, Choose} from "tsx-control-statements/components"; import cn from "classnames" import {TermWrap} from "./term"; -import {getDefaultSession, getLineId} from "./session"; +import {getDefaultSession, getLineId, Session} from "./session"; +import type {LineType} from "./session"; import localizedFormat from 'dayjs/plugin/localizedFormat'; dayjs.extend(localizedFormat) @@ -70,7 +71,7 @@ class LineText extends React.Component<{line : LineType, session : Session}, {}> } @mobxReact.observer -class LineCmd extends React.Component<{line : LineType, session : Session, changeSizeCallback : () => void}, {}> { +class LineCmd extends React.Component<{line : LineType, session : Session, changeSizeCallback? : (term : TermWrap) => void}, {}> { constructor(props) { super(props); } @@ -127,7 +128,7 @@ class LineCmd extends React.Component<{line : LineType, session : Session, chang let termSize = termWrap.getSize(); let formattedTime = getLineDateStr(line.ts); let cellHeightPx = 17; - let totalHeight = cellHeightPx * termWrap.usedRows; + let totalHeight = cellHeightPx * termWrap.usedRows.get(); return (
= 5}, {"running": running})}> @@ -161,7 +162,7 @@ class LineCmd extends React.Component<{line : LineType, session : Session, chang } @mobxReact.observer -class Line extends React.Component<{line : LineType, session : Session}, {}> { +class Line extends React.Component<{line : LineType, session : Session, changeSizeCallback? : (term : TermWrap) => void}, {}> { render() { let line = this.props.line; if (line.linetype == "text") { @@ -273,7 +274,7 @@ class CmdInput extends React.Component<{session : Session, windowid : string}, {
mike@local
- +
@@ -289,8 +290,8 @@ class CmdInput extends React.Component<{session : Session, windowid : string}, { } @mobxReact.observer -class SessionView extends React.Component<{session : SessionType}, {}> { - shouldFollow : IObservableValue = mobx.observable.box(true); +class SessionView extends React.Component<{session : Session}, {}> { + shouldFollow : mobx.IObservableValue = mobx.observable.box(true); @boundMethod scrollHandler(event : any) { @@ -325,6 +326,7 @@ class SessionView extends React.Component<{session : SessionType}, {}> { return
(loading)
; } let idx = 0; + let line : LineType = null; return (
diff --git a/src/session.ts b/src/session.ts index f953b548..86e504b5 100644 --- a/src/session.ts +++ b/src/session.ts @@ -49,7 +49,8 @@ type WindowDataType = { windowid : string, name : string, curremote : string, - lines : mobx.IObservableValue, + lines : mobx.IObservableArray, + linesLoading : mobx.IObservableValue, version : number, }; @@ -64,6 +65,9 @@ type SessionDataType = { cmds : CmdDataType[], }; +type CmdDataType = { +}; + class Session { sessionId : string; name : string; @@ -112,7 +116,7 @@ class Session { } getCurWindow() : WindowDataType { - return this.getWindowById(this.activeWindowId); + return this.getWindowById(this.activeWindowId.get()); } loadWindowLines(windowid : string) { @@ -127,7 +131,7 @@ class Session { window.linesLoading.set(true); let usp = new URLSearchParams({sessionid: this.sessionId, windowid: windowid}); - let url = sprintf("http://localhost:8080/api/get-window-lines?") + usp.toString(); + let url = new URL(sprintf("http://localhost:8080/api/get-window-lines?") + usp.toString()); fetch(url).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { mobx.action(() => { window.lines.replace(data.data || []); @@ -146,15 +150,16 @@ class Session { submitCommand(windowid : string, commandStr : string) { let url = sprintf("http://localhost:8080/api/run-command"); - let data = {type: "fecmd", sessionid: this.sessionId, windowid: windowid, cmdstr: commandStr, userid: GlobalUser}; + let data = {type: "fecmd", sessionid: this.sessionId, windowid: windowid, cmdstr: commandStr, userid: GlobalUser, remotestate: null}; let curWindow = this.getCurWindow(); if (curWindow == null) { throw new Error(sprintf("invalid current window=%s", this.activeWindowId)); } - data.remotestate = this.getWindowCurRemoteData(this.activeWindowId); - if (data.remotestate == null) { - throw new Error(sprintf("no remotestate found for windowid:%s (remote=%s), cannot submit command", windowid, window.curremote)); + let rstate = this.getWindowCurRemoteData(this.activeWindowId.get()); + if (rstate == null) { + throw new Error(sprintf("no remotestate found for windowid:%s (remote=%s), cannot submit command", windowid, curWindow.curremote)); } + data.remotestate = rstate; fetch(url, {method: "post", body: JSON.stringify(data)}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { mobx.action(() => { if (data.data != null && data.data.line != null) { @@ -280,7 +285,7 @@ function initSession() { }); let usp = new URLSearchParams({name: "default"}); - let url = sprintf("http://localhost:8080/api/get-session?") + usp.toString(); + let url = new URL(sprintf("http://localhost:8080/api/get-session?") + usp.toString()); fetch(url).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { mobx.action(() => { let sdata = data.data; @@ -307,7 +312,7 @@ function getDefaultSession() : Session { return DefaultSession; } -window.getDefaultSession = getDefaultSession; +(window as any).getDefaultSession = getDefaultSession; export {Session, getDefaultSession, getLineId, initSession}; export type {LineType, WindowDataType}; diff --git a/src/sh2.ts b/src/sh2.ts index 5dba839d..fdb4319c 100644 --- a/src/sh2.ts +++ b/src/sh2.ts @@ -7,9 +7,10 @@ import {GlobalWS} from "./ws"; import {v4 as uuidv4} from "uuid"; import {initSession} from "./session"; +// @ts-ignore let VERSION = __SHVERSION__; -window.ScriptHausClientId = uuidv4(); +(window as any).ScriptHausClientId = uuidv4(); document.addEventListener("DOMContentLoaded", () => { initSession(); diff --git a/src/term.ts b/src/term.ts index 5fffacd5..8a09b2a2 100644 --- a/src/term.ts +++ b/src/term.ts @@ -19,7 +19,7 @@ function loadPtyOut(term : Terminal, sessionId : string, cmdId : string, delayMs } class TermWrap { - terminal : Terminal; + terminal : any; termId : string; sessionId : string; cmdId : string; @@ -33,7 +33,7 @@ class TermWrap { atRowMax : boolean = false; initialized : boolean = false; changeSizeCallback : (TermWrap) => void = null; - usedRows : number = null; + usedRows : mobx.IObservableValue = null; constructor(sessionId : string, cmdId : string) { this.termId = uuidv4(); @@ -109,7 +109,7 @@ class TermWrap { usedRows = i+1; } } - this.usedRows = usedRows; + this.usedRows.set(usedRows); return; } @@ -117,11 +117,11 @@ class TermWrap { this.flexRows = true; this.maxRows = rows; if (!flexRows) { - term.resize(rows, cols); - setTimeout(() => incRenderVersion(), 10); + this.terminal.resize(rows, cols); + setTimeout(() => this.incRenderVersion(), 10); return; } - resizeToContent(); + this.resizeToContent(); } getSize() : {rows : number, cols : number} { diff --git a/src/util.ts b/src/util.ts index 71b10660..29b3bf10 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,3 +1,5 @@ +import {sprintf} from "sprintf-js"; + function fetchJsonData(resp : any, ctErr : boolean) : Promise { let contentType = resp.headers.get("Content-Type"); if (contentType != null && contentType.startsWith("application/json")) { diff --git a/src/ws.ts b/src/ws.ts index b5396c10..cf1bda2e 100644 --- a/src/ws.ts +++ b/src/ws.ts @@ -2,11 +2,13 @@ import * as mobx from "mobx"; import {sprintf} from "sprintf-js"; import {boundMethod} from "autobind-decorator"; +declare var window : any; + class WSControl { wsConn : any; open : mobx.IObservableValue; opening : boolean = false; - reconnectTimes : int = 0; + reconnectTimes : number = 0; msgQueue : any[] = []; reqMap : Record void> = {}; @@ -109,7 +111,7 @@ class WSControl { return; } if (eventData.type == "ping") { - this.wsConn.send(JSON.stringify({type: "pong", stime: parseInt(Date.now()/1000)})); + this.wsConn.send(JSON.stringify({type: "pong", stime: Date.now()})); return; } if (eventData.type == "pong") {