aboutsummaryrefslogtreecommitdiff
path: root/src/fields/CursorField.ts
blob: 84917ae539cd1a25bd3449db39e650734ed46c9c (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
import { createSimpleSchema, object, serializable } from 'serializr';
import { Deserializable } from '../client/util/SerializationHelper';
import { Copy, FieldChanged, ToScriptString, ToString } from './FieldSymbols';
import { ObjectField } from './ObjectField';

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[FieldChanged]?.();
    }

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

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