aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/formattedText/ImageResizeView.tsx
blob: 401ecd7e6414184486d8be3c03089e88dcad2b5d (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
import { NodeSelection } from "prosemirror-state";
import { Doc } from "../../../../fields/Doc";
import { DocServer } from "../../../DocServer";
import { DocumentManager } from "../../../util/DocumentManager";
import React = require("react");

import { schema } from "./schema_rts";

interface IImageResizeView {
    node: any;
    view: any;
    getPos: any;
    addDocTab: any;
}

export class ImageResizeView extends React.Component<IImageResizeView> {
    constructor(props: IImageResizeView) {
        super(props);
    }

    onClickImg = (e: any) => {
        e.stopPropagation();
        e.preventDefault();
        if (this.props.view.state.selection.node && this.props.view.state.selection.node.type !== this.props.view.state.schema.nodes.image) {
            this.props.view.dispatch(this.props.view.state.tr.setSelection(new NodeSelection(this.props.view.state.doc.resolve(this.props.view.state.selection.from - 2))));
        }
    }

    onPointerDownImg = (e: any) => {
        if (e.ctrlKey) {
            e.preventDefault();
            e.stopPropagation();
            DocServer.GetRefField(this.props.node.attrs.docid).then(async linkDoc =>
                (linkDoc instanceof Doc) &&
                DocumentManager.Instance.FollowLink(linkDoc, this.props.view.state.schema.Document,
                    document => this.props.addDocTab(document, this.props.node.attrs.location ? this.props.node.attrs.location : "inTab"), false));
        }
    }

    onPointerDownHandle = (e: any) => {
        e.preventDefault();
        e.stopPropagation();
        const elementImage = document.getElementById("imageId") as HTMLElement;
        const wid = Number(getComputedStyle(elementImage).width.replace(/px/, ""));
        const hgt = Number(getComputedStyle(elementImage).height.replace(/px/, ""));
        const startX = e.pageX;
        const startWidth = parseFloat(this.props.node.attrs.width);

        const onpointermove = (e: any) => {
            const elementOuter = document.getElementById("outerId") as HTMLElement;

            const currentX = e.pageX;
            const diffInPx = currentX - startX;
            elementOuter.style.width = `${startWidth + diffInPx}`;
            elementOuter.style.height = `${(startWidth + diffInPx) * hgt / wid}`;
        };

        const onpointerup = () => {
            document.removeEventListener("pointermove", onpointermove);
            document.removeEventListener("pointerup", onpointerup);
            const pos = this.props.view.state.selection.from;
            const elementOuter = document.getElementById("outerId") as HTMLElement;
            this.props.view.dispatch(this.props.view.state.tr.setNodeMarkup(this.props.getPos(), null, { ...this.props.node.attrs, width: elementOuter.style.width, height: elementOuter.style.height }));
            this.props.view.dispatch(this.props.view.state.tr.setSelection(new NodeSelection(this.props.view.state.doc.resolve(pos))));
        };

        document.addEventListener("pointermove", onpointermove);
        document.addEventListener("pointerup", onpointerup);
    }

    selectNode() {
        const elementImage = document.getElementById("imageId") as HTMLElement;
        const elementHandle = document.getElementById("handleId") as HTMLElement;

        elementImage.classList.add("ProseMirror-selectednode");
        elementHandle.style.display = "";
    }

    deselectNode() {
        const elementImage = document.getElementById("imageId") as HTMLElement;
        const elementHandle = document.getElementById("handleId") as HTMLElement;

        elementImage.classList.remove("ProseMirror-selectednode");
        elementHandle.style.display = "none";
    }


    render() {

        const outerStyle = {
            width: this.props.node.attrs.width,
            height: this.props.node.attrs.height,
            display: "inline-block",
            overflow: "hidden",
            float: this.props.node.attrs.float
        };

        const imageStyle = {
            width: "100%",
        };

        const handleStyle = {
            position: "absolute",
            width: "20px",
            heiht: "20px",
            backgroundColor: "blue",
            borderRadius: "15px",
            display: "none",
            bottom: "-10px",
            right: "-10px"

        };



        return (
            <div id="outer"
                style={outerStyle}
            >
                <img
                    id="imageId"
                    style={imageStyle}
                    src={this.props.node.src}
                    onClick={this.onClickImg}
                    onPointerDown={this.onPointerDownImg}

                >
                </img>
                <span
                    id="handleId"
                    onPointerDown={this.onPointerDownHandle}
                >

                </span>
            </div >
        );
    }
}