From f85890bda82b77dc8d49e5be1794be8c21216a5f Mon Sep 17 00:00:00 2001 From: fanxb Date: Thu, 7 Nov 2024 23:18:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=88=A0=E9=99=A4=E8=A7=84=E5=88=99?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=AD=A3=E5=88=99=E8=A1=A8=E8=BE=BE=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E5=90=8C=E6=97=B6=E4=BD=8D=E7=BD=AE=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=B4=9F=E6=95=B0=EF=BC=88=E4=BB=8E=E6=9C=AB=E5=B0=BE=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=EF=BC=8C-1=E8=A1=A8=E7=A4=BA=E5=88=B0=E5=80=92?= =?UTF-8?q?=E6=95=B0=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=AD=97=E7=AC=A6=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/bo/rules/DeleteRule.ts | 164 ++++++++++-------- .../src/components/rules/DeleteRule.vue | 63 ++++--- 2 files changed, 132 insertions(+), 95 deletions(-) diff --git a/openRenamerBackend/entity/bo/rules/DeleteRule.ts b/openRenamerBackend/entity/bo/rules/DeleteRule.ts index 530f9a1..4896157 100644 --- a/openRenamerBackend/entity/bo/rules/DeleteRule.ts +++ b/openRenamerBackend/entity/bo/rules/DeleteRule.ts @@ -3,90 +3,104 @@ import FileObj from "../../vo/FileObj"; import path from 'path'; export default class DeleteRule implements RuleInterface { - /** - * 类别:deletePart:部分删除,deleteAll:全部删除 - */ - type: string; - /** - * 部分删除时的开始信息 - */ - start: DeleteRuleItem; - /** - * 部分删除时的结束信息 + /** + * 类别:deletePart:部分删除,deleteAll:全部删除 + */ + type: string; + /** + * 部分删除时的开始信息 + */ + start: DeleteRuleItem; + /** + * 部分删除时的结束信息 - */ - end: DeleteRuleItem; - /** - * 忽略拓展名,true:忽略,false:不忽略 - */ - ignorePostfix: boolean; + */ + end: DeleteRuleItem; + /** + * 忽略拓展名,true:忽略,false:不忽略 + */ + ignorePostfix: boolean; + regI: boolean; - constructor(data: any) { - this.type = data.type; - this.start = new DeleteRuleItem(data.start); - this.end = new DeleteRuleItem(data.end); - this.ignorePostfix = data.ignorePostfix; - } + constructor(data: any) { + this.type = data.type; + this.regI = data.regI != undefined && data.regI; + this.start = new DeleteRuleItem(data.start, this.regI); + this.end = new DeleteRuleItem(data.end, this.regI); + this.ignorePostfix = data.ignorePostfix; + } + deal(file: FileObj): void { + if (this.type === 'deleteAll') { + file.realName = ""; + if (!this.ignorePostfix) { + file.expandName = ""; + } + } else { + let str = file.realName + (this.ignorePostfix ? "" : file.expandName); + let startIndex = this.start.calIndex(str, false); + let endIndex = this.end.calIndex(str, true); + if (startIndex < 0 || endIndex < 0 || startIndex > endIndex) { + return; + } + str = str.substring(0, startIndex) + str.substring(endIndex + 1); + if (this.ignorePostfix) { + file.realName = str; + } else { + file.expandName = path.extname(str); + if (file.expandName.length > 0) { + file.realName = str.substring(0, str.lastIndexOf(".")); + } else { + file.realName = str; + } + } + } - deal(file: FileObj): void { - if (this.type === 'deleteAll') { - file.realName = ""; - if (!this.ignorePostfix) { - file.expandName = ""; - } - } else { - let str = file.realName + (this.ignorePostfix ? "" : file.expandName); - let startIndex = this.start.calIndex(str); - let endIndex = this.end.calIndex(str); - if (startIndex < 0 || endIndex < 0) { - return; - } - str = str.substring(0, startIndex) + str.substring(endIndex + 1); - if (this.ignorePostfix) { - file.realName = str; - } else { - file.expandName = path.extname(str); - if (file.expandName.length > 0) { - file.realName = str.substring(0, str.lastIndexOf(".")); - } else { - file.realName = str; - } - } - } - - file.name = file.realName + file.expandName; - } + file.name = file.realName + file.expandName; + } } class DeleteRuleItem { - /** - * location:位置,text:文本,end:直到末尾 - */ - type: string; - /** - * 对应的值 - */ - value: string; + /** + * location:位置,text:文本,end:直到末尾 + */ + type: string; + /** + * 对应的值 + */ + value: string; + /** + * 正则对象 + */ + reg: RegExp; - constructor(data: any) { - this.type = data.type; - this.value = data.value; - } + constructor(data: any, regI: boolean) { + this.type = data.type; + this.value = data.value; + if (this.type === 'reg') { + this.reg = regI ? new RegExp(this.value) : new RegExp(this.value, 'i'); + } + } - /** - * 计算位置 - */ - calIndex(str: string): number { - if (this.type === 'location') { - return parseInt(this.value) - 1; - } else if (this.type === 'text') { - return str.indexOf(this.value); - } else if (this.type === 'end') { - return str.length - 1; - } - return -1; - } + /** + * 计算位置 + * @param str 字符串 + * @param end 是否末尾计算 + */ + calIndex(str: string, end: boolean): number { + if (this.type === 'location') { + let val = parseInt(this.value); + return val > 0 ? val - 1 : str.length + val; + } else if (this.type === 'text') { + return str.indexOf(this.value); + } else if (this.type === 'end') { + return str.length - 1; + } else if (this.type === 'reg') { + let res = this.reg.exec(str); + return res == null ? -1 : (res.index + (end ? 0 : res[0].length)); + } + return -1; + } } diff --git a/openRenamerFront/src/components/rules/DeleteRule.vue b/openRenamerFront/src/components/rules/DeleteRule.vue index c3189e4..df8792e 100644 --- a/openRenamerFront/src/components/rules/DeleteRule.vue +++ b/openRenamerFront/src/components/rules/DeleteRule.vue @@ -6,22 +6,30 @@
开始
位置: - +
文本: - + +
+
+ 正则: +
结束
位置: - +
文本: - + +
+
+ 正则: +
直到末尾 @@ -29,14 +37,18 @@
+
+
区分大小写:
+ +
全部删除:
- +
忽略拓展名:
- +
@@ -60,28 +72,36 @@ export default { value: "", }, ignorePostfix: true, + regI: false,//reg是否区分大小写 }, }, startIndex: 1, endIndex: 1, startText: "", + startReg: "", endText: "", + endReg: "", deleteAll: false, }; }, created() { if (this.editRule) { this.ruleObj = JSON.parse(JSON.stringify(this.editRule)); - if (this.ruleObj.data.type == "deletePart") { - if (this.ruleObj.data.start.type == "location") { + if (this.ruleObj.data.type === "deletePart") { + if (this.ruleObj.data.start.type === "location") { this.startIndex = parseInt(this.ruleObj.data.start.value); - } else { + } else if (this.ruleObj.data.start.type === "text") { this.startText = this.ruleObj.data.start.value; - } - if (this.ruleObj.data.end.type == "location") { - this.endIndex = parseInt(this.ruleObj.data.end.value); } else { + this.startReg = this.ruleObj.data.start.value; + } + + if (this.ruleObj.data.end.type === "location") { + this.endIndex = parseInt(this.ruleObj.data.end.value); + } else if (this.ruleObj.data.end.type === "text") { this.endText = this.ruleObj.data.end.value; + } else { + this.endReg = this.ruleObj.data.end.value; } } else { this.deleteAll = true; @@ -91,13 +111,15 @@ export default { methods: { exportObj() { if (this.ruleObj.data.type.length == 0) { - this.$message({ message: "请填写完整", type: "warning" }); + this.$message({message: "请填写完整", type: "warning"}); return null; } - if (this.ruleObj.data.type == "deletePart") { + if (this.ruleObj.data.type === "deletePart") { if ( - (this.ruleObj.data.start.type == "text" && this.startText.length == 0) || - (this.ruleObj.data.start.type == "text" && this.startText.length == 0) + ('text' === this.ruleObj.data.start.type && this.startText.length === 0) || + ('text' === this.ruleObj.data.end.type && this.endText.length === 0) || + ('reg' === this.ruleObj.data.start.type && this.startReg.length === 0) || + ('reg' === this.ruleObj.data.end.type && this.endReg.length === 0) ) { this.$message({ message: "开始或者结束文本不能为空", @@ -106,19 +128,20 @@ export default { return null; } } - this.ruleObj.data.start.value = this.ruleObj.data.start.type == "location" ? this.startIndex.toString() : this.startText; - this.ruleObj.data.end.value = this.ruleObj.data.end.type == "location" ? this.endIndex.toString() : this.endText; + let startType = this.ruleObj.data.start.type; + this.ruleObj.data.start.value = startType === "location" ? this.startIndex.toString() : startType === 'text' ? this.startText : this.startReg; + let endType = this.ruleObj.data.end.type; + this.ruleObj.data.end.value = endType === "location" ? this.endIndex.toString() : endType === 'text' ? this.endText : this.endReg; let message = `删除:`; if (this.deleteAll) { message += "全部删除"; } else { - message += `从"${this.ruleObj.data.start.value}"到"${this.ruleObj.data.end.type == "untilEnd" ? "末尾" : this.ruleObj.data.end.value}"`; + message += `从"${this.ruleObj.data.start.value}"到"${this.ruleObj.data.end.type === "end" ? "末尾" : this.ruleObj.data.end.value}"`; } this.ruleObj.message = message; return this.ruleObj; }, allDeleteChange(val) { - console.log(val); this.deleteAll = val; this.ruleObj.data.type = val ? "deleteAll" : "deletePart"; },