aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/RichTextField.ts
blob: dc66813e043862eb3de3c509f85f6ad3f4412a5f (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
import { ObjectField } from "./ObjectField";
import { serializable } from "serializr";
import { Deserializable } from "../client/util/SerializationHelper";
import { Copy, ToScriptString } from "./FieldSymbols";
import { scriptingGlobal } from "../client/util/Scripting";

@scriptingGlobal
@Deserializable("RichTextField")
export class RichTextField extends ObjectField {
    @serializable(true)
    readonly Data: string;
    private Extractor = /,\"text\":\"([^\"\}]*)\"\}/g;

    constructor(data: string) {
        super();
        this.Data = data;
    }

    [Copy]() {
        return new RichTextField(this.Data);
    }

    [ToScriptString]() {
        return `new RichTextField("${this.Data}")`;
    }

    plainText = () => {
        let contents = "";
        let matches: RegExpExecArray | null;
        let considering = this.Data;
        while ((matches = this.Extractor.exec(considering)) !== null) {
            contents += matches[1];
            considering = considering.substring(matches.index + matches[0].length);
            this.Extractor.lastIndex = 0;
        }
        return contents.length ? contents : undefined;
    }
}