|
@@ -43,7 +43,7 @@
|
|
|
<div v-if="pinyinType == 2 && fileList.length == 0" class="upload-area">
|
|
|
<div class="file-area" @click="pickFile()" id="drag-table">
|
|
|
<img src="../assets/image/upload.png" style="width: 220px;"/>
|
|
|
- <p style="font-size: 16px;">点击添加TXT文件或拖拽TXT文件到此</p>
|
|
|
+ <p style="font-size: 16px;">点击添加TXT/DOCX文件或拖拽TXT/DOCX文件到此</p>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
@@ -449,16 +449,6 @@
|
|
|
}
|
|
|
this.inputChange(this.txt1);
|
|
|
},
|
|
|
- // 格式化html
|
|
|
- dealHtml(htmls){
|
|
|
- let regex1 = new RegExp('py-non-chinese-item"> </span>', "g");
|
|
|
- let regex2 = new RegExp('py-non-chinese-item">\n</span>', "g");
|
|
|
- let regex3 = new RegExp('</span><span class="py-result-item">', "g");
|
|
|
- htmls = htmls.replace(regex1, 'py-non-chinese-item"> </span>')
|
|
|
- .replace(regex2, 'py-non-chinese-item">\n</span><br>')
|
|
|
- .replace(regex3, '</span>'+this.pinSetting.separator+'<span class="py-result-item">');
|
|
|
- return htmls;
|
|
|
- },
|
|
|
// 导出
|
|
|
async exportFile(flag) {
|
|
|
this.loading = true;
|
|
@@ -500,7 +490,7 @@
|
|
|
|
|
|
}else{ // 文件批量注音
|
|
|
if(this.fileList.length <= 0){
|
|
|
- this.$message({message: '请选择需要注音的TXT文档', type: 'warning'});
|
|
|
+ this.$message({message: '请选择需要注音的TXT/DOCX文档', type: 'warning'});
|
|
|
this.loading = false;
|
|
|
return false;
|
|
|
}else{
|
|
@@ -524,14 +514,6 @@
|
|
|
let item = fileList[i];
|
|
|
let lastIndex = item.path.lastIndexOf('.');
|
|
|
let suffix = this.exportFormat;
|
|
|
-
|
|
|
- if (fs.existsSync(this.downloadDir + separator + pjson.softInfo.softName)) {
|
|
|
- this.handleData.newPath = this.downloadDir + separator + pjson.softInfo.softName;
|
|
|
- } else {
|
|
|
- fs.mkdirSync(this.downloadDir + separator + pjson.softInfo.softName);
|
|
|
- this.handleData.newPath = this.downloadDir + separator + pjson.softInfo.softName;
|
|
|
- }
|
|
|
-
|
|
|
newPath = this.handleData.newPath + separator + item.name.slice(0, item.name.lastIndexOf('.')) + '.' + suffix;
|
|
|
// 处理文件
|
|
|
let task = this.dealFile(i, item, newPath);
|
|
@@ -559,6 +541,7 @@
|
|
|
|
|
|
// 打开文件夹
|
|
|
if (fileList.length > 0) {
|
|
|
+ this.loading = false;
|
|
|
this.$message({message: "恭喜你,任务已完成!", type: 'success'});
|
|
|
electronApi.call('showItemInfolder', [this.handleData.newPath + '\\xy.txt'])
|
|
|
}
|
|
@@ -568,13 +551,23 @@
|
|
|
|
|
|
setTimeout(() => {
|
|
|
this.loading = false;
|
|
|
- }, 3000)
|
|
|
+ }, 30000)
|
|
|
}
|
|
|
},
|
|
|
showPercent(num, index, item){
|
|
|
item.percent = num;
|
|
|
this.fileList.splice(index, 1, item);
|
|
|
},
|
|
|
+ // 格式化html
|
|
|
+ dealHtml(htmls){
|
|
|
+ let regex1 = new RegExp('py-non-chinese-item"> </span>', "g");
|
|
|
+ let regex2 = new RegExp('py-non-chinese-item">\n</span>', "g");
|
|
|
+ let regex3 = new RegExp('</span><span class="py-result-item">', "g");
|
|
|
+ htmls = htmls.replace(regex1, 'py-non-chinese-item"> </span>')
|
|
|
+ .replace(regex2, 'py-non-chinese-item">\n</span><br>')
|
|
|
+ .replace(regex3, '</span>'+this.pinSetting.separator+'<span class="py-result-item">');
|
|
|
+ return htmls;
|
|
|
+ },
|
|
|
async blockFile(index, item, textArr){
|
|
|
for(let i = 0; i < textArr.length; i++){
|
|
|
let htmls = this.dealHtml(html(textArr[i], {toneType : this.pinSetting.toneType, wrapNonChinese: true}));
|
|
@@ -598,126 +591,122 @@
|
|
|
throw err;
|
|
|
return false;
|
|
|
}
|
|
|
- this.showPercent(100, index, item);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ this.showPercent(100, index, item);
|
|
|
return true;
|
|
|
},
|
|
|
dealFile(index, item, newPath){ // 开始注音
|
|
|
- let path = item.path;
|
|
|
- this.showPercent(10, index, item);
|
|
|
- if(item.type.toUpperCase() == 'DOCX'){ // word文档
|
|
|
- mammoth.extractRawText({path: path}).then((result) =>{
|
|
|
- let data = result.value;
|
|
|
- return this.pinFile(index, item, newPath, data)
|
|
|
- }).catch((error) => {
|
|
|
- console.error(error);
|
|
|
- this.$notify.error({
|
|
|
- title: '错误',
|
|
|
- message: "出现错误,请重试!" + filePath
|
|
|
- });
|
|
|
- reject(false);
|
|
|
-
|
|
|
- });
|
|
|
- }else{
|
|
|
- fs.readFile(path, 'utf8', (err, data) => {
|
|
|
- if (err) {
|
|
|
- console.error(err);
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ let path = item.path;
|
|
|
+ this.showPercent(10, index, item);
|
|
|
+ if(item.type.toUpperCase() == 'DOCX'){ // word文档
|
|
|
+ mammoth.extractRawText({path: path}).then(async(res) =>{
|
|
|
+ let data = res.value;
|
|
|
+ let result = await this.pinFile(index, item, newPath, data);
|
|
|
+ if(result){
|
|
|
+ resolve(true);
|
|
|
+ }else{
|
|
|
+ reject(false);
|
|
|
+ }
|
|
|
+ }).catch((error) => {
|
|
|
+ console.error(error);
|
|
|
this.$notify.error({
|
|
|
title: '错误',
|
|
|
message: "出现错误,请重试!" + filePath
|
|
|
});
|
|
|
reject(false);
|
|
|
- }
|
|
|
- return this.pinFile(index, item, newPath, data);
|
|
|
- });
|
|
|
- }
|
|
|
- },
|
|
|
- // 文件注音
|
|
|
- pinFile(index, item, newPath, data) {
|
|
|
- return new Promise(async(resolve, reject) => {
|
|
|
- // fs.readFile(path, 'utf8', async(err, data) => {
|
|
|
- // if (err) {
|
|
|
- // console.error(err);
|
|
|
- // this.$notify.error({
|
|
|
- // title: '错误',
|
|
|
- // message: "出现错误,请重试!" + filePath
|
|
|
- // });
|
|
|
- // reject(false);
|
|
|
- // }
|
|
|
-
|
|
|
- if (!this.$refs.headerRef.authority.isAuthority) {
|
|
|
- if(data.length > 20){
|
|
|
- data = data.slice(0, 20);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- let textLength = data.length;
|
|
|
- let blockLength = Math.ceil(textLength / 100000);
|
|
|
- let textArr = [];
|
|
|
- if(textLength > 100000 && this.pinBuild != 3){ // 文字长度大于10万字且是结构输出,分块处理
|
|
|
- for(let i=0; i<blockLength; i++){
|
|
|
- let startIndex = i * 100000;
|
|
|
- let endIndex = (i+1) * 100000;
|
|
|
- textArr.push(data.slice(startIndex ,endIndex));
|
|
|
- }
|
|
|
- let result = await this.blockFile(index, item, textArr);
|
|
|
- if(result){
|
|
|
- this.showPercent(100, index, item);
|
|
|
- resolve(true);
|
|
|
- }else{
|
|
|
+ });
|
|
|
+ }else{
|
|
|
+ fs.readFile(path, 'utf8', async(err, data) => {
|
|
|
+ if (err) {
|
|
|
+ console.error(err);
|
|
|
this.$notify.error({
|
|
|
title: '错误',
|
|
|
message: "出现错误,请重试!" + filePath
|
|
|
});
|
|
|
reject(false);
|
|
|
}
|
|
|
-
|
|
|
- }else{
|
|
|
- let outContent = '';
|
|
|
- this.pinSetting.separator = this.separator == '' ? ' ' : this.separator;
|
|
|
- if(this.pinBuild == 3){ // 只要拼音
|
|
|
- outContent = pinyin(data, this.pinSetting);
|
|
|
- }else{
|
|
|
- let htmls = this.dealHtml(html(data, {toneType : this.pinSetting.toneType, wrapNonChinese: true}));
|
|
|
- if(this.pinBuild == 2){ // 左右结构
|
|
|
- let regex4 = /<ruby><span class="py-chinese-item">|<\/span><rp>|<\/rp><rt class="py-pinyin-item">|<\/rt><rp>|<\/rp><\/ruby>/g;
|
|
|
- htmls = htmls.replace(regex4, '');
|
|
|
- }
|
|
|
- outContent = htmls;
|
|
|
- }
|
|
|
-
|
|
|
- this.showPercent(50, index, item);
|
|
|
-
|
|
|
- let buffer;
|
|
|
- if(this.pinBuild == 3){ // 只要拼音
|
|
|
- buffer = outContent;
|
|
|
+ let result = await this.pinFile(index, item, newPath, data);
|
|
|
+ if(result){
|
|
|
+ resolve(true);
|
|
|
}else{
|
|
|
- let blob = htmlDocx.asBlob(outContent);
|
|
|
- buffer = Buffer.from(await blob.arrayBuffer());
|
|
|
+ reject(false);
|
|
|
}
|
|
|
-
|
|
|
- this.showPercent(70, index, item);
|
|
|
-
|
|
|
- let filePath = newPath;
|
|
|
- fs.writeFile(filePath, buffer, (err) => {
|
|
|
- if (err){
|
|
|
- this.loading = false;
|
|
|
- this.$notify.error({
|
|
|
- title: '错误',
|
|
|
- message: err.message + filePath
|
|
|
- });
|
|
|
- reject(false);
|
|
|
- throw err;
|
|
|
- }
|
|
|
- this.showPercent(100, index, item);
|
|
|
- resolve(true);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ // 文件注音
|
|
|
+ async pinFile(index, item, newPath, data) {
|
|
|
+ if (!this.$refs.headerRef.authority.isAuthority) {
|
|
|
+ if(data.length > 20){
|
|
|
+ data = data.slice(0, 20);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let textLength = data.length;
|
|
|
+ let blockLength = Math.ceil(textLength / 100000);
|
|
|
+ let textArr = [];
|
|
|
+ if(textLength > 100000 && this.pinBuild != 3){ // 文字长度大于10万字且是结构输出,分块处理
|
|
|
+ for(let i=0; i<blockLength; i++){
|
|
|
+ let startIndex = i * 100000;
|
|
|
+ let endIndex = (i+1) * 100000;
|
|
|
+ textArr.push(data.slice(startIndex ,endIndex));
|
|
|
+ }
|
|
|
+ let result = await this.blockFile(index, item, textArr);
|
|
|
+ if(result){
|
|
|
+ this.showPercent(100, index, item);
|
|
|
+ return true;
|
|
|
+ }else{
|
|
|
+ this.$notify.error({
|
|
|
+ title: '错误',
|
|
|
+ message: "出现错误,请重试!" + filePath
|
|
|
+ });
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ let outContent = '';
|
|
|
+ this.pinSetting.separator = this.separator == '' ? ' ' : this.separator;
|
|
|
+ if(this.pinBuild == 3){ // 只要拼音
|
|
|
+ outContent = pinyin(data, this.pinSetting);
|
|
|
+ }else{
|
|
|
+ let htmls = this.dealHtml(html(data, {toneType : this.pinSetting.toneType, wrapNonChinese: true}));
|
|
|
+ if(this.pinBuild == 2){ // 左右结构
|
|
|
+ let regex4 = /<ruby><span class="py-chinese-item">|<\/span><rp>|<\/rp><rt class="py-pinyin-item">|<\/rt><rp>|<\/rp><\/ruby>/g;
|
|
|
+ htmls = htmls.replace(regex4, '');
|
|
|
+ }
|
|
|
+ outContent = htmls;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.showPercent(50, index, item);
|
|
|
+
|
|
|
+ let buffer;
|
|
|
+ if(this.pinBuild == 3){ // 只要拼音
|
|
|
+ buffer = outContent;
|
|
|
+ }else{
|
|
|
+ let blob = htmlDocx.asBlob(outContent);
|
|
|
+ buffer = Buffer.from(await blob.arrayBuffer());
|
|
|
+ }
|
|
|
+
|
|
|
+ this.showPercent(70, index, item);
|
|
|
+
|
|
|
+ let filePath = newPath;
|
|
|
+ fs.writeFile(filePath, buffer, (err) => {
|
|
|
+ if (err){
|
|
|
+ this.loading = false;
|
|
|
+ this.$notify.error({
|
|
|
+ title: '错误',
|
|
|
+ message: err.message + filePath
|
|
|
});
|
|
|
+ return false;
|
|
|
+ throw err;
|
|
|
}
|
|
|
-
|
|
|
- // });
|
|
|
- });
|
|
|
+ this.showPercent(100, index, item);
|
|
|
+ return true;
|
|
|
+ });
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
|