aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/OverlayView.tsx
blob: f8fc94274b52f3df6da022ea49d519f82bafdd24 (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
import * as React from "react";
import { observer } from "mobx-react";
import { observable, action } from "mobx";
import { Utils } from "../../Utils";

export type OverlayDisposer = () => void;

export type OverlayElementOptions = {
    x: number;
    y: number;
    width?: number;
    height?: number;
};

@observer
export class OverlayView extends React.Component {
    public static Instance: OverlayView;
    @observable.shallow
    private _elements: { ele: JSX.Element, id: string, options: OverlayElementOptions }[] = [];

    constructor(props: any) {
        super(props);
        if (!OverlayView.Instance) {
            OverlayView.Instance = this;
        }
    }

    @action
    addElement(ele: JSX.Element, options: OverlayElementOptions): OverlayDisposer {
        const eleWithPosition = { ele, options, id: Utils.GenerateGuid() };
        this._elements.push(eleWithPosition);
        return action(() => {
            const index = this._elements.indexOf(eleWithPosition);
            if (index !== -1) this._elements.splice(index, 1);
        });
    }

    render() {
        return (
            <div>
                {this._elements.map(({ ele, options: { x, y, width, height }, id }) => (
                    <div key={id} style={{ position: "absolute", transform: `translate(${x}px, ${y}px)`, width, height }}>{ele}</div>
                ))}
            </div>
        );
    }
}