Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
191 lines (185 loc) · 4.56 KB
/
Copy pathmain.js
File metadata and controls
191 lines (185 loc) · 4.56 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
import readFile from "./modules/fileReader.js";
import baiduTrans from "./modules/request/request.js";
import pushAlert from "./modules/alert.js";
import { splitFile, encode, decode } from "./modules/utils.js";
import stores from "./modules/stores.js";
const RootComponent = {
data() {
return {
appid: "",
key: "",
from: "auto",
to: "zh",
files: [],
languages: [],
canTranslate: true,
canSwap: false,
isDisplayBothLanguage: false,
savedFields: ["appid", "key"],
tmpSaveFields: ["from", "to", "files"],
};
},
methods: {
onCloneResult(){
this.$refs.output.select(); // 选中output框的文本
document.execCommand("copy");
},
onToggleResult(idx){
this.isDisplayBothLanguage = !this.isDisplayBothLanguage;
if (this.isDisplayBothLanguage) {
this.files[idx].displayResult = this.files[idx].result;
} else {
this.files[idx].displayResult = this.files[idx].dst;
}
},
getDownloadLink(txt) {
return "data:text/paint; utf-8," + encodeURIComponent(txt);
},
onSwapLanguage(){
if (this.canSwap) {
[this.from, this.to] = [this.to, this.from];
}
},
onImportFiles() {
const fileList = this.$refs.fileInput.files;
this.readFiles(fileList);
},
onDropFile(event) {
event.preventDefault();
if (event.type == "drop") {
const fileList = event.dataTransfer.files;
this.readFiles(fileList);
}
},
onRemoveFile(idx) {
this.files.splice(idx, 1);
},
readFiles(fileList) {
// 把文件保存为对象
for (const file of fileList) {
if (this.files.map((f) => f.name).includes(file.name)) {
pushAlert(`文件【${file.name}】重复。`);
} else {
// 用一个对象保存文件的meta信息
this.files.unshift({
obj: file,
name: file.name,
read: false,
encodingGot: false,
encoding: "utf-8",
});
}
}
// 读取文件内容
for (const file of this.files) {
if (file.read) continue;
readFile(file).catch(({ error }) => {
pushAlert(`${error}`, "warning", 5000);
});
}
},
async onTranslate(file) {
if (!file.content) return;
this.canTranslate = false;
try {
const result = [];
const dst = [];
for (const batch of splitFile(file.content)) {
const x = await this.translateBatch(batch);
result.push(x[0]);
dst.push(x[1]);
}
file.dst = decode(dst.join("\n"));
file.result = decode(result.join("\n"));
file.displayResult = file.dst;
} catch (error) {
pushAlert(`${error}`, "warning", 5000);
} finally {
this.canTranslate = true;
}
},
async translateBatch(query) {
if (!query) return [];
const result = [];
const dst = [];
const response = await baiduTrans({
appid: this.appid,
key: this.key,
query: encode(query),
from: this.from,
to: this.to,
});
while (response.length) {
const x = response.shift();
result.push([x.src, x.dst]);
dst.push(x.dst);
}
return [result.flat().join("\n"), dst.join("\n")];
},
initLanguages() {
const languageMap = {
en: "英语",
zh: "中文",
yue: "中文(粤语)",
cht: "繁体中文",
wyw: "中文(文言文)",
de: "德语",
jp: "日语",
kor: "韩语",
ara: "阿拉伯语",
bul: "保加利亚语",
cs: "捷克语",
dan: "丹麦语",
el: "希腊语",
est: "爱沙尼亚语",
fin: "芬兰语",
fra: "法语",
hu: "匈牙利语",
it: "意大利语",
nl: "荷兰语",
pl: "波兰语",
pt: "葡萄牙语",
rom: "罗马尼亚语",
ru: "俄语",
slo: "斯洛文尼亚语",
spa: "西班牙语",
swe: "瑞典语",
th: "泰语",
vie: "越南语",
};
for (const [code, name] of Object.entries(languageMap)) {
this.languages.push({ code, name });
}
},
readDataFromStorage() {
for (const field of [...this.savedFields, ...this.tmpSaveFields]) {
this[field] = stores.get(field) || this[field];
}
},
saveDataToStorage() {
for (const field of this.savedFields) {
stores.setLocal(field, this[field]);
}
for (const field of this.tmpSaveFields) {
stores.setSession(field, this[field]);
}
},
},
updated() {
this.saveDataToStorage();
if (this.from == "auto") {
this.canSwap = false;
} else {
this.canSwap = true;
}
},
mounted() {
this.readDataFromStorage();
this.initLanguages();
const dropbox = this.$refs.dropTarget;
dropbox.addEventListener("dragenter", this.onDropFile)
dropbox.addEventListener("dragover", this.onDropFile)
dropbox.addEventListener("drop", this.onDropFile)
},
};
Vue.createApp(RootComponent).mount("#app");