aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/InkingCanvas.tsx
blob: 0d87c1239d1aad6908a511ffdf226e8777d296e5 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { observer } from "mobx-react";
import { action, computed } from "mobx";
import { InkingControl } from "./InkingControl";
import React = require("react");
import { Transform } from "../util/Transform";
import { Document } from "../../fields/Document";
import { KeyStore } from "../../fields/KeyStore";
import { InkField, InkTool, StrokeData, StrokeMap } from "../../fields/InkField";
import { JsxArgs } from "./nodes/DocumentView";
import { InkingStroke } from "./InkingStroke";
import "./InkingCanvas.scss"
import { CollectionDockingView } from "./collections/CollectionDockingView";
import { Utils } from "../../Utils";
import { FieldWaiting } from "../../fields/Field";
import { getMapLikeKeys } from "mobx/lib/internal";


interface InkCanvasProps {
    getScreenTransform: () => Transform;
    Document: Document;
}

@observer
export class InkingCanvas extends React.Component<InkCanvasProps> {

    private _isDrawing: boolean = false;
    private _idGenerator: string = "";

    constructor(props: Readonly<InkCanvasProps>) {
        super(props);
    }

    @computed
    get inkData(): StrokeMap {
        let map = this.props.Document.GetT(KeyStore.Ink, InkField);
        if (!map || map === FieldWaiting) {
            return new Map;
        }
        return new Map(map.Data);
    }

    set inkData(value: StrokeMap) {
        this.props.Document.SetData(KeyStore.Ink, value, InkField);
    }

    componentDidMount() {
        document.addEventListener("mouseup", this.handleMouseUp);
    }

    componentWillUnmount() {
        document.removeEventListener("mouseup", this.handleMouseUp);
    }


    @action
    handleMouseDown = (e: React.PointerEvent): void => {
        if (e.button != 0 ||
            InkingControl.Instance.selectedTool === InkTool.None) {
            return;
        }
        e.stopPropagation()
        if (InkingControl.Instance.selectedTool === InkTool.Eraser) {
            return
        }
        e.stopPropagation()
        const point = this.relativeCoordinatesForEvent(e);

        // start the new line, saves a uuid to represent the field of the stroke
        this._idGenerator = Utils.GenerateGuid();
        let data = this.inkData;
        data.set(this._idGenerator,
            {
                pathData: [point],
                color: InkingControl.Instance.selectedColor,
                width: InkingControl.Instance.selectedWidth,
                tool: InkingControl.Instance.selectedTool,
                page: this.props.Document.GetNumber(KeyStore.CurPage, 0)
            });
        this.inkData = data;
        this._isDrawing = true;
    }

    @action
    handleMouseMove = (e: React.PointerEvent): void => {
        if (!this._isDrawing ||
            InkingControl.Instance.selectedTool === InkTool.None) {
            return;
        }
        e.stopPropagation()
        if (InkingControl.Instance.selectedTool === InkTool.Eraser) {
            return
        }
        const point = this.relativeCoordinatesForEvent(e);

        // add points to new line as it is being drawn
        let data = this.inkData;
        let strokeData = data.get(this._idGenerator);
        if (strokeData) {
            strokeData.pathData.push(point);
            data.set(this._idGenerator, strokeData);
        }

        this.inkData = data;
    }

    @action
    handleMouseUp = (e: MouseEvent): void => {
        this._isDrawing = false;
    }

    relativeCoordinatesForEvent = (e: React.MouseEvent): { x: number, y: number } => {
        let [x, y] = this.props.getScreenTransform().transformPoint(e.clientX, e.clientY);
        x += 50000
        y += 50000
        return { x, y };
    }

    @action
    removeLine = (id: string): void => {
        let data = this.inkData;
        data.delete(id);
        this.inkData = data;
    }

    render() {
        // styling for cursor
        let canvasStyle = {};
        if (InkingControl.Instance.selectedTool === InkTool.None) {
            canvasStyle = { pointerEvents: "none" };
        } else {
            canvasStyle = { pointerEvents: "auto", cursor: "crosshair" };
        }

        // get data from server
        // let inkField = this.props.Document.GetT(KeyStore.Ink, InkField);
        // if (!inkField || inkField == "<Waiting>") {
        //     return (<div className="inking-canvas" style={canvasStyle}
        //         onMouseDown={this.handleMouseDown} onMouseMove={this.handleMouseMove} >
        //         <svg>
        //         </svg>
        //     </div >)
        // }

        let lines = this.inkData;

        // parse data from server
        let paths: Array<JSX.Element> = []
        let curPage = this.props.Document.GetNumber(KeyStore.CurPage, 0)
        Array.from(lines).map(item => {
            let id = item[0];
            let strokeData = item[1];
            if (strokeData.page == 0 || strokeData.page == curPage)
                paths.push(<InkingStroke key={id} id={id}
                    line={strokeData.pathData}
                    color={strokeData.color}
                    width={strokeData.width}
                    tool={strokeData.tool}
                    deleteCallback={this.removeLine} />)
        })

        return (

            <div className="inking-canvas" style={canvasStyle}
                onPointerDown={this.handleMouseDown} onPointerMove={this.handleMouseMove} >
                <svg>
                    {paths}
                </svg>
            </div >
        )
    }
}