aboutsummaryrefslogtreecommitdiff
path: root/src/fields/InkField.ts
blob: 2a4ed18e755a86629d44afa29dd8f97cc822a9d7 (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
import { BasicField } from "./BasicField";
import { Types } from "../server/Message";
import { FieldId } from "./Field";
import { observable, ObservableMap } from "mobx";

export enum InkTool {
    None,
    Pen,
    Highlighter,
    Eraser
}
export interface StrokeData {
    pathData: Array<{ x: number, y: number }>;
    color: string;
    width: string;
    tool: InkTool;
    page: number;
}
export type StrokeMap = Map<string, StrokeData>;

export class InkField extends BasicField<StrokeMap> {
    constructor(data: StrokeMap = new Map, id?: FieldId, save: boolean = true) {
        super(data, save, id);
    }

    ToScriptString(): string {
        return `new InkField("${this.Data}")`;
    }

    Copy() {
        return new InkField(this.Data);
    }

    ToJson(): { _id: string; type: Types; data: any; } {
        return {
            type: Types.Ink,
            data: this.Data,
            _id: this.Id,
        }
    }

    UpdateFromServer(data: any) {
        this.data = new ObservableMap(data);
    }

    static FromJson(id: string, data: any): InkField {
        let map: StrokeMap = new Map<string, StrokeData>();
        Object.keys(data).forEach(key => {
            map.set(key, data[key]);
        });
        return new InkField(map, id, false);
    }
}