aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/ScriptingRepl.tsx
blob: bd6fc9dfb1c1d9719a4e4122fcb4e1070175fdac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as React from 'react';
import { observer } from 'mobx-react';
import { observable, action } from 'mobx';
import './ScriptingRepl.scss';
import { Scripting, CompileScript } from '../util/Scripting';

@observer
export class ScriptingRepl extends React.Component {
    @observable private commands: { command: string, result: any }[] = [];

    @observable private commandString: string = "";

    private args: any = {};

    @action
    onKeyDown = (e: React.KeyboardEvent) => {
        if (e.key === "Enter") {
            e.stopPropagation();

            const script = CompileScript(this.commandString, { typecheck: false, addReturn: true, editable: true, params: { args: "any" } });
            if (!script.compiled) {
                return;
            }
            const result = script.run({ args: this.args });
            if (!result.success) {
                return;
            }
            this.commands.push({ command: this.commandString, result: result.result });

            this.commandString = "";
        }
    }

    @action
    onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
        this.commandString = e.target.value;
    }

    render() {
        return (
            <div className="scriptingRepl-outerContainer">
                <div className="scriptingRepl-commandsContainer">
                    {this.commands.map(({ command, result }) => {
                        return (
                            <div className="scriptingRepl-resultContainer">
                                <div className="scriptingRepl-commandString">{command}</div>
                                <div className="scriptingRepl-commandResult">{String(result)}</div>
                            </div>
                        );
                    })}
                </div>
                <input
                    className="scriptingRepl-commandInput"
                    value={this.commandString}
                    onChange={this.onChange}
                    onKeyDown={this.onKeyDown}></input>
            </div>
        );
    }
}