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
|
import React, { useEffect, useState, useRef } 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'
import Markdown from './renderers/Markdown'
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 calc(50% - 1em - 2px);
`
const FlexRight = styled.div`
flex: 0 0 50%;
max-width: calc(50% - 1em + 2px);
margin-left: 2em;
`
const PreviewWrapper = styled.div`
margin: 2em;
`
const NewPaste = () => {
const [title, setTitle] = useState('');
const [content, setContent] = useState('');
const [pass, setPass] = useState('');
const [language, setLanguage] = useState(LANGS.raw);
const [expiry, setExpiry] = useState('');
const [hash, setHash] = useState('');
const [isPreview, setIsPreview] = useState(false);
const ErrorLabel = useRef(null);
useEffect(() => {
if (title === "") {
document.title = `ctrl-v`;
} else {
document.title = `ctrl-v | ${title}`;
}
}, [title])
function handleSubmit(e) {
e.preventDefault();
// prevent resubmission
if (!hash) {
PostNewPaste(title, content, language, pass, expiry)
.then((response) => {
// on success, redir
setHash(response.data.hash)
}).catch((error) => {
const resp = error.response
// some weird err
if (resp !== undefined) {
const errTxt = `${resp.status}: ${resp.data}`
ErrorLabel.current.showMessage(errTxt)
} else {
// some weird err (e.g. network)
ErrorLabel.current.showMessage(error)
}
});
}
}
function renderPreview() {
const pasteInput = <PasteInput
onChange={(e) => { setContent(e.target.value) }}
content={content}
maxLength="100000"
id="pasteInput" />
if (isPreview) {
var preview
switch (language) {
case 'latex':
preview =
<PreviewWrapper>
<Latex
content={content} />
</PreviewWrapper>
break
case 'markdown':
preview =
<PreviewWrapper className='md' >
<Markdown
content={content} />
</PreviewWrapper>
break
default:
preview =
<CodeRenderer
lang={language}
theme='atom'
content={content} />
}
return (
<Flex>
<FlexLeft>
{pasteInput}
</FlexLeft>
<FlexRight className='preview' >
{preview}
</FlexRight>
</Flex>
);
} else {
return (
pasteInput
);
}
}
return (
<form onSubmit={handleSubmit}>
<PasteModal hash={hash} />
<TitleInput
onChange={(e) => {setTitle(e.target.value)}}
value={title}
maxLength="100"
id="titleInput" />
{renderPreview()}
<OptionsContainer
pass={pass}
expiry={expiry}
lang={language}
onPassChange={(e) => { setPass(e.target.value) }}
onLangChange={(e) => { setLanguage(e.target.value) }}
onExpiryChange={(e) => { setExpiry(e.target.value) }} />
<input className="lt-button lt-shadow lt-hover" type="submit" value="new paste" />
<Button
className="lt-shadow lt-hover"
type="button"
onClick={() => setIsPreview(!isPreview)}>
preview
</Button>
<br />
<Error ref={ErrorLabel} />
</form>
);
}
export default NewPaste
|