aboutsummaryrefslogtreecommitdiff
path: root/src/util/Scripting.ts
blob: 0e83ab4f06a36b9127b3b671092081fe15af506d (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
// import * as ts from "typescript"
let ts = (window as any).ts;
import { Opt, Field } from "../fields/Field";
import { Document as DocumentImport } from "../fields/Document";
import { NumberField as NumberFieldImport, NumberField } from "../fields/NumberField";
import { ImageField as ImageFieldImport } from "../fields/ImageField";
import { TextField as TextFieldImport, TextField } from "../fields/TextField";
import { RichTextField as RichTextFieldImport } from "../fields/RichTextField";
import { KeyStore as KeyStoreImport } from "../fields/Key";

export interface ExecutableScript {
    (): any;

    compiled: boolean;
}

function ExecScript(script: string, diagnostics: Opt<any[]>): ExecutableScript {
    const compiled = !(diagnostics && diagnostics.some(diag => diag.category == ts.DiagnosticCategory.Error));

    let func: () => Opt<Field>;
    if (compiled) {
        func = function (): Opt<Field> {
            let KeyStore = KeyStoreImport;
            let Document = DocumentImport;
            let NumberField = NumberFieldImport;
            let TextField = TextFieldImport;
            let ImageField = ImageFieldImport;
            let RichTextField = RichTextFieldImport;
            let window = undefined;
            let document = undefined;
            let retVal = eval(script);

            return retVal;
        };
    } else {
        func = () => undefined;
    }

    return Object.assign(func,
        {
            compiled
        });
}

export function CompileScript(script: string): ExecutableScript {
    let result = (window as any).ts.transpileModule(script, {})

    return ExecScript(result.outputText, result.diagnostics);
}

export function ToField(data: any): Opt<Field> {
    if (typeof data == "string") {
        return new TextField(data);
    } else if (typeof data == "number") {
        return new NumberField(data);
    }
    return undefined;
}