-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.tsx
More file actions
226 lines (215 loc) · 7.4 KB
/
App.tsx
File metadata and controls
226 lines (215 loc) · 7.4 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import React, { useEffect, useState } from "react";
import {
Box,
Button,
AppBar,
Toolbar,
IconButton,
Typography,
TextField,
FormControlLabel,
Switch,
} from "@mui/material";
import MenuIcon from "@mui/icons-material/Menu";
import { useTheme } from "@mui/material/styles";
import { API_ENDPOINT } from "./commons/config";
import { unicodeToBes } from "./modules/unicodeToBes";
function App() {
const [displaySourceText, setDisplaySourceText] = useState(
"今日の天気は晴天ですね。\n今日の天気は晴天ですね。",
);
const [sourceText, setSourceText] = useState(
"今日の天気は晴天ですね。\n今日の天気は晴天ですね。",
);
const [displayWakatiText, setDisplayWakatiText] = useState("");
const [wakatiText, setWakatiText] = useState("");
//const [displayTargetText, setDisplayTargetText] = useState("");
const [targetText, setTargetText] = useState("");
const [isPageNumberOn, setIsPageNumberOn] = useState(false);
const [fileName, setFileName] = useState("");
const [showWarning, setShowWarning] = useState(false);
async function source2wakati(text: string) {
const response = await fetch(
`${API_ENDPOINT}/source2wakati?sourceText=` + text,
);
const data = await response.json();
return data;
}
async function wakati2target(text: string) {
const response = await fetch(
`${API_ENDPOINT}/wakati2target?wakatiText=` + text,
);
const data = await response.json();
return data;
}
useEffect(() => {
source2wakati(sourceText.replace(/\n/g, "\\n")).then((data) => {
setWakatiText(data.wakatiText);
setDisplayWakatiText(data.wakatiText);
});
}, [sourceText]);
useEffect(() => {
wakati2target(wakatiText.replace(/\n/g, "\\n")).then((data) => {
setTargetText(data.targetText);
//setDisplayTargetText(data.targetText);
});
}, [wakatiText]);
const theme = useTheme();
return (
<div className="App">
<AppBar position="static">
<Toolbar variant="dense">
<IconButton edge="start" color="inherit" aria-label="menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit">
ut.code(); 点字翻訳アプリ
</Typography>
</Toolbar>
</AppBar>
<div>
<Box sx={{ justifyContent: "space-around" }}>
<Box sx={{ mx: 2.5, pt: 1.5 }}>
<Typography align="center" variant="h6" gutterBottom>
翻訳元のテキスト
</Typography>
<TextField
style={{ backgroundColor: "white" }}
multiline
variant="outlined"
rows={6}
fullWidth
value={displaySourceText}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
setDisplaySourceText(e.target.value);
setSourceText(e.target.value.replace(/\n/g, "\\n"));
}}
/>
<div style={{ display: "flex", justifyContent: "flex-end" }}>
<Button
onClick={() => {
navigator.clipboard.writeText(sourceText);
}}
>
Copy
</Button>
</div>{" "}
</Box>
<Box sx={{ mx: 2.5 }}>
<Box>
{/* <TextField sx={{mt: 2}} style={{backgroundColor: theme.palette.secondary.main}} label="readonly" multiline variant="outlined" rows={4} fullWidth value={wakatiReference} /> */}
<Typography align="center" variant="h6" gutterBottom>
分かち書きのテキスト
</Typography>
<TextField
style={{ backgroundColor: "white" }}
multiline
variant="outlined"
rows={6}
fullWidth
value={displayWakatiText}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
setDisplayWakatiText(e.target.value);
setWakatiText(e.target.value.replace(/\n/g, "\\n"));
}}
/>
</Box>
<Box></Box>
<div style={{ display: "flex", justifyContent: "flex-end" }}>
<Button
onClick={() => {
navigator.clipboard.writeText(wakatiText);
}}
>
Copy
</Button>
</div>
</Box>
<Box sx={{ mx: 2.5, pb: 6 }}>
<Typography align="center" variant="h6" gutterBottom>
翻訳後のテキスト
</Typography>
<TextField
style={{ backgroundColor: theme.palette.secondary.main }}
label="readonly"
multiline
variant="outlined"
rows={6}
fullWidth
value={targetText}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
//setDisplayTargetText(e.target.value)
setTargetText(e.target.value.replace(/\n/g, "\\n"));
}}
/>
<div style={{ display: "flex", justifyContent: "flex-end" }}>
<Button
onClick={() => {
navigator.clipboard.writeText(targetText);
}}
>
Copy
</Button>
</div>
<div>
<label>
ファイル名:
<input
type="text"
value={fileName}
onChange={(name) => {
setFileName(name.target.value);
setShowWarning(false);
}} // ユーザーの入力に反応してファイル名を更新
/>
.BES
</label>
{showWarning && (
<div style={{ color: "red" }}>
ファイル名を入力してください。
</div>
)}
</div>
<div style={{ display: "flex", justifyContent: "flex-end" }}>
<FormControlLabel
control={
<Switch
checked={isPageNumberOn}
onChange={() => {
setIsPageNumberOn(!isPageNumberOn);
}}
/>
}
label="ページ番号あり"
labelPlacement="start"
/>
</div>
<div style={{ display: "flex", justifyContent: "flex-end" }}>
<Button
onClick={() => {
const buffer = unicodeToBes(
targetText.replace(/\n/g, "\\n"),
isPageNumberOn,
);
const blob = new Blob([buffer], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
if (fileName.trim() === "") {
setShowWarning(true); // ファイル名が空の場合、警告を表示
return;
}
a.href = url;
a.download = fileName + ".BES";
a.click();
}}
>
Output
</Button>
</div>
</Box>
</Box>
</div>
</div>
);
}
export default App;