aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/collectionFreeForm/CollectionFreeFormRemoteCursors.tsx
blob: 45e24bbb208948642dcff7d2489846b8b2436010 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { computed } from 'mobx';
import { observer } from 'mobx-react';
import * as mobxUtils from 'mobx-utils';
import CursorField from '../../../../fields/CursorField';
import { Doc, FieldResult } from '../../../../fields/Doc';
import { Id } from '../../../../fields/FieldSymbols';
import { List } from '../../../../fields/List';
import { listSpec } from '../../../../fields/Schema';
import { Cast } from '../../../../fields/Types';
import { CollectionViewProps } from '../CollectionView';
import './CollectionFreeFormView.scss';
import * as React from 'react';
import * as uuid from 'uuid';

@observer
export class CollectionFreeFormRemoteCursors extends React.Component<CollectionViewProps> {
    @computed protected get cursors(): CursorField[] {
        const doc = this.props.Document;

        let cursors: FieldResult<List<CursorField>>;
        const id = Doc.UserDoc()[Id];
        if (!id || !(cursors = Cast(doc.cursors, listSpec(CursorField)))) {
            return [];
        }
        const now = mobxUtils.now();
        return (cursors || []).filter(({ data: { metadata } }) => metadata.id !== id && now - metadata.timestamp < 1000);
    }

    @computed get renderedCursors() {
        return this.cursors.map(
            ({
                data: {
                    metadata,
                    position: { x, y },
                },
            }) => {
                return (
                    <div key={metadata.id} className="collectionFreeFormRemoteCursors-cont" style={{ transform: `translate(${x - 10}px, ${y - 10}px)` }}>
                        <canvas
                            className="collectionFreeFormRemoteCursors-canvas"
                            ref={el => {
                                if (el) {
                                    const ctx = el.getContext('2d');
                                    if (ctx) {
                                        ctx.fillStyle = '#' + uuid.v5(metadata.id, uuid.v5.URL).substring(0, 6).toUpperCase() + '22';
                                        ctx.fillRect(0, 0, 20, 20);

                                        ctx.fillStyle = 'black';
                                        ctx.lineWidth = 0.5;

                                        ctx.beginPath();

                                        ctx.moveTo(10, 0);
                                        ctx.lineTo(10, 8);

                                        ctx.moveTo(10, 20);
                                        ctx.lineTo(10, 12);

                                        ctx.moveTo(0, 10);
                                        ctx.lineTo(8, 10);

                                        ctx.moveTo(20, 10);
                                        ctx.lineTo(12, 10);

                                        ctx.stroke();
                                    }
                                }
                            }}
                            width={20}
                            height={20}
                        />
                        <p className="collectionFreeFormRemoteCursors-symbol">{metadata.identifier[0].toUpperCase()}</p>
                    </div>
                );
            }
        );
    }

    render() {
        return this.renderedCursors;
    }
}