From 28f5e9bf05d60e1c44969eda3aab7bdacf225c76 Mon Sep 17 00:00:00 2001 From: sawka Date: Tue, 12 Jul 2022 00:45:10 -0700 Subject: [PATCH] submit command --- src/main.tsx | 4 -- src/model.ts | 122 +++++++++++++++++++++++++++++++++++++++++++++++++ src/preload.js | 2 - src/types.ts | 1 - 4 files changed, 122 insertions(+), 7 deletions(-) diff --git a/src/main.tsx b/src/main.tsx index 166e81c3..737e0faf 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -88,10 +88,6 @@ class LineCmd extends React.Component<{line : LineType}, {}> { let termElem = document.getElementById("term-" + getLineId(line)); cmd.connectElem(termElem); } - if (line.isnew) { - setTimeout(() => this.scrollIntoView(), 100); - line.isnew = false; - } } componentWillUnmount() { diff --git a/src/model.ts b/src/model.ts index 3604fbb0..d31f795e 100644 --- a/src/model.ts +++ b/src/model.ts @@ -7,6 +7,8 @@ import {v4 as uuidv4} from "uuid"; import type {SessionDataType, WindowDataType, LineType, RemoteType, HistoryItem, RemoteInstanceType, CmdDataType, FeCmdPacketType, TermOptsType, RemoteStateType} from "./types"; import {WSControl} from "./ws"; +var GlobalUser = "sawka"; + type OV = mobx.IObservableValue; type OArr = mobx.IObservableArray; @@ -198,6 +200,7 @@ class Window { history : any[] = []; cmds : Record = {}; shouldFollow : OV = mobx.observable.box(true); + remoteInstances : OArr = mobx.observable.array([]); constructor(wdata : WindowDataType) { this.sessionId = wdata.sessionid; @@ -238,6 +241,64 @@ class Window { getCmd(cmdId : string) { return this.cmds[cmdId]; } + + getCurRemoteInstance() : RemoteInstanceType { + let rname = this.curRemote.get(); + let sessionScope = false; + if (rname.startsWith("^")) { + rname = rname.substr(1); + sessionScope = true; + } + if (sessionScope) { + let session = GlobalModel.getSessionById(this.sessionId); + let rdata = session.getRemoteInstance(rname); + return rdata; + } + return this.getRemoteInstance(rname); + } + + getRemoteInstance(rname : string) : RemoteInstanceType { + for (let i=0; i { + if (cmd != null) { + this.cmds[cmd.cmdid] = new Cmd(cmd, this.windowId); + } + let lines = this.lines; + let lineIdx = 0; + for (lineIdx=0; lineIdx line.lineid) { + break; + } + } + if (lineIdx == lines.length) { + this.lines.push(line); + return; + } + this.lines.splice(lineIdx, 0, line); + })(); + } }; class Session { @@ -246,6 +307,7 @@ class Session { curWindowId : OV; windows : OArr; notifyNum : OV = mobx.observable.box(0); + remoteInstances : OArr = mobx.observable.array([]); constructor(sdata : SessionDataType) { this.sessionId = sdata.sessionid; @@ -295,6 +357,28 @@ class Session { getActiveWindow() : Window { return this.getWindowById(this.curWindowId.get()); } + + getRemoteInstance(rname : string) : RemoteInstanceType { + for (let i=0; i", cmdStr); + let win = this.getActiveWindow(); + if (win == null) { + this.errorHandler("cannot submit command, no active window", null) + return; + } + let data : FeCmdPacketType = {type: "fecmd", sessionid: win.sessionId, windowid: win.windowId, cmdstr: cmdStr, userid: GlobalUser, remotestate: null}; + let rstate = win.getCurRemoteInstance(); + if (rstate == null) { + this.errorHandler("cannot submit command, no remote state found", null); + return; + } + data.remotestate = {remoteid: rstate.remoteid, remotename: rstate.name, ...rstate.state}; + let url = sprintf("http://localhost:8080/api/run-command"); + 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) { + this.addLineCmd(data.data.line, data.data.cmd, true); + } + })(); + }).catch((err) => { + this.errorHandler("calling run-command", err); + }); } updateWindow(win : WindowDataType) { @@ -423,6 +536,15 @@ class Model { return null; } + getRemoteByName(name : string) : RemoteType { + for (let i=0; i ipcRenderer.sendSync("get-id"), onCmdT: (callback) => ipcRenderer.on("cmd-t"), diff --git a/src/types.ts b/src/types.ts index b2efd11c..b9719868 100644 --- a/src/types.ts +++ b/src/types.ts @@ -17,7 +17,6 @@ type LineType = { linetype : string, text : string, cmdid : string, - isnew : boolean, }; type RemoteType = {