aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/CursorField.ts
blob: 28467377bd2e2943655d6b1e219b5cb3026d8c5c (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
61
62
63
64
65
66
import { ObjectField } from "./ObjectField";
import { observable } from "mobx";
import { Deserializable } from "../client/util/SerializationHelper";
import { serializable, createSimpleSchema, object, date } from "serializr";
import { OnUpdate, ToScriptString, ToString, Copy } from "./FieldSymbols";

export type CursorPosition = {
    x: number,
    y: number
};

export type CursorMetadata = {
    id: string,
    identifier: string,
    timestamp: number
};

export type CursorData = {
    metadata: CursorMetadata,
    position: CursorPosition
};

const PositionSchema = createSimpleSchema({
    x: true,
    y: true
});

const MetadataSchema = createSimpleSchema({
    id: true,
    identifier: true,
    timestamp: true
});

const CursorSchema = createSimpleSchema({
    metadata: object(MetadataSchema),
    position: object(PositionSchema)
});

@Deserializable("cursor")
export default class CursorField extends ObjectField {

    @serializable(object(CursorSchema))
    readonly data: CursorData;

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

    setPosition(position: CursorPosition) {
        this.data.position = position;
        this.data.metadata.timestamp = Date.now();
        this[OnUpdate]();
    }

    [Copy]() {
        return new CursorField(this.data);
    }

    [ToScriptString]() {
        return "invalid";
    }
    [ToString]() {
        return "invalid";
    }
}