aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/NewPaste.js
blob: 8012b999dc5f5c2efab9b0a0970950a0d2e88ecf (plain) (blame)
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
172
173
174
import React from 'react';
import { TitleInput, PasteInput } from './Inputs'
import OptionsContainer from './Options'
import Error from './Err'
import { PostNewPaste } from '../helpers/httpHelper'
import PasteModal from './modals/PasteModal'
import { LANGS } from './renderers/Code'
import styled from 'styled-components'
import CodeRenderer from './renderers/Code'
import Latex from './renderers/Latex'

const Button = styled.button`
    margin-right: 0 !important;
    margin-left: 2em !important;
    height: calc(16px + 1.6em + 2px);
`

const Flex = styled.div`
    display: flex;
    flex-direction: row;
`

const FlexLeft = styled.div`
    flex: 0 0 50%;
`

const FlexRight = styled.div`
    flex: 0 0 50%;
    max-width: calc(50% - 1em + 2px);
    margin-left: 2em;
`

const LatexWrapper = styled.div`
    margin: 2em;
`

class NewPaste extends React.Component {
    constructor(props) {
        super(props);
        this.state = { 
            title: '',
            content: '',
            pass: '',
            language: LANGS.raw,
            expiry: '',
            hash: '',
            error: '',
            preview: false,
        };

        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
        this.togglePreview = this.togglePreview.bind(this);
        this.renderPreview = this.renderPreview.bind(this);
        this.ErrorLabel = React.createRef();
    }

    componentDidUpdate() {
        if (this.state.title === "") {
            document.title = `ctrl-v`;
        } else {
            document.title = `ctrl-v | ${this.state.title}`;
        }
    }

    handleChange(event) {
        const target = event.target;
        const name = target.name;

        this.setState({
            [name]: target.value
        });
    }

    togglePreview() {
        const state = this.state.preview
        this.setState({ preview: !state })
    }

    handleSubmit(event) {
        event.preventDefault();

        // prevent resubmission
        if (!this.state.hash) {
            PostNewPaste(this.state)
                .then((response) => {
                    // on success, redir
                    this.setState({ hash: response.data.hash })
                }).catch((error) => {
                    const resp = error.response
    
                    // some weird err
                    if (resp !== undefined) {
                        const errTxt = `${resp.statusText}: ${resp.data}`
                        this.ErrorLabel.current.showMessage(errTxt)
                    } else {
                        // some weird err (e.g. network)
                        this.ErrorLabel.current.showMessage(error)
                    }
                });
        }
    }

    renderPreview() {
        const pasteInput = <PasteInput
            onChange={this.handleChange}
            content={this.state.content}
            maxLength="100000"
            id="pasteInput" />

        var preview
        switch (this.state.language) {
            case 'latex':
                preview = 
                    <LatexWrapper>
                        <Latex
                            content={this.state.content} />
                    </LatexWrapper>
                break
            default:
                preview = 
                    <CodeRenderer
                        lang={this.state.language}
                        theme='atom'
                        content={this.state.content} />
        }

        if (this.state.preview) {
            return (
                <Flex>
                    <FlexLeft>
                        {pasteInput}
                    </FlexLeft>
                    <FlexRight className='preview' >
                        {preview}
                    </FlexRight>
                </Flex>
            );
        } else {
            return (
                pasteInput
            );
        }
    }

    render() {
        return (
            <form onSubmit={this.handleSubmit}>
                <PasteModal hash={this.state.hash} />
                <TitleInput
                    onChange={this.handleChange}
                    value={this.state.title}
                    maxLength="100"
                    id="titleInput" />
                {this.renderPreview()}
                <OptionsContainer
                    pass={this.state.pass}
                    expiry={this.state.expiry}
                    lang={this.state.language}
                    onChange={this.handleChange} />
                <input className="lt-button lt-shadow lt-hover" type="submit" value="new paste" />
                <Button
                    className="lt-shadow lt-hover"
                    type="button"
                    onClick={this.togglePreview} >
                    preview
                </Button>
                <Error ref={this.ErrorLabel} />
            </form>
        );
    }
}

export default NewPaste