@mytools-all/mytools-rename 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +21 -3
- package/app.mjs +244 -0
- package/common.mjs +69 -0
- package/index.html +182 -0
- package/package.json +40 -6
- package/plugin.json +27 -0
- package/tool.css +295 -0
- package/utils.mjs +201 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MyTools Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# 文件批量重命名
|
|
2
|
+
|
|
3
|
+
文件批量重命名
|
|
4
|
+
|
|
5
|
+
版本:0.1.0;适用于 MyTools,不能作为独立网页调用宿主能力。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
在 MyTools 插件管理的 npm 安装入口输入 `@mytools-all/mytools-rename@0.1.0`。也可以下载本仓库,在开发者页面选择“安装本地目录”。
|
|
10
|
+
|
|
11
|
+
## 开发
|
|
12
|
+
|
|
13
|
+
直接编辑本目录的 HTML、JavaScript 和样式,无需依赖宿主源码或其他插件仓库。修改后重新安装本地目录。运行 `npm test` 检查包内契约和逻辑。插件 API 规范见 [mytools](https://github.com/mytools-all/mytools/tree/main/docs)。
|
|
14
|
+
|
|
15
|
+
## 权限
|
|
16
|
+
|
|
17
|
+
声明的宿主能力:fs.pickdirectory、fs.pickfiles、fs.search、fs.renamemany、clipboard.write、fs.importdropped。目录和危险操作仍需用户授权。
|
|
18
|
+
|
|
19
|
+
## 许可
|
|
20
|
+
|
|
21
|
+
项目代码使用 MIT;随附的第三方代码遵循各自随附的许可证。
|
package/app.mjs
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import {
|
|
2
|
+
$,
|
|
3
|
+
$$,
|
|
4
|
+
action,
|
|
5
|
+
status,
|
|
6
|
+
ready,
|
|
7
|
+
call,
|
|
8
|
+
escapeHtml,
|
|
9
|
+
copy,
|
|
10
|
+
} from "./common.mjs";
|
|
11
|
+
import { renamePreview } from "./utils.mjs";
|
|
12
|
+
let files = [],
|
|
13
|
+
rows = [],
|
|
14
|
+
mode = "replace",
|
|
15
|
+
undo = [],
|
|
16
|
+
busy = false,
|
|
17
|
+
confirmed = null;
|
|
18
|
+
const templates = {
|
|
19
|
+
replace:
|
|
20
|
+
'<label>查找 <input id="find" placeholder="要替换的内容"></label><span>→</span><label>替换为 <input id="replace" placeholder="新内容"></label><label><input id="regex" type="checkbox">正则表达式</label>',
|
|
21
|
+
append:
|
|
22
|
+
'<label>追加内容 <input id="text" value="_{n}" placeholder="文本,可用 {n} 插入序号"></label><label>位置 <select id="position"><option value="before">名称前</option><option value="after" selected>名称后</option><option value="at">指定位置</option></select></label><label>第 <input id="index" type="number" value="1" min="0" style="width:58px">位后</label>',
|
|
23
|
+
sequence:
|
|
24
|
+
'<label>前面字符 <input id="prefix" value="文件_"></label><label>后面字符 <input id="suffix"></label>',
|
|
25
|
+
case: '<label>转换 <select id="case"><option value="lower">全部小写</option><option value="upper">全部大写</option><option value="title">单词首字母大写</option></select></label>',
|
|
26
|
+
manual:
|
|
27
|
+
'<span class="muted">可直接编辑表格中的新名称,或粘贴多行名称(每行对应一个条目)</span><textarea id="lines" placeholder="粘贴来自 Excel / 文本的名称,每行一项"></textarea>',
|
|
28
|
+
};
|
|
29
|
+
function config() {
|
|
30
|
+
const rule = { mode, extension: $("#extension").checked };
|
|
31
|
+
for (const el of $$("#rules input,#rules select,#rules textarea"))
|
|
32
|
+
rule[el.id] = el.type === "checkbox" ? el.checked : el.value;
|
|
33
|
+
return rule;
|
|
34
|
+
}
|
|
35
|
+
function resetConfirmation() {
|
|
36
|
+
$("#confirm-bar").classList.add("hidden");
|
|
37
|
+
for (const id of ["summary", "undo", "commit"])
|
|
38
|
+
$("#" + id).classList.remove("hidden");
|
|
39
|
+
confirmed = null;
|
|
40
|
+
}
|
|
41
|
+
function render() {
|
|
42
|
+
resetConfirmation();
|
|
43
|
+
const invalid = rows.some((r) => r.error);
|
|
44
|
+
const changed = rows.filter((r) => r.changed);
|
|
45
|
+
$("#empty").classList.toggle("hidden", files.length > 0);
|
|
46
|
+
$("#table").classList.toggle("hidden", !files.length);
|
|
47
|
+
$("#files").innerHTML = rows
|
|
48
|
+
.map(
|
|
49
|
+
(r, i) =>
|
|
50
|
+
'<tr><td class="row-number">' +
|
|
51
|
+
(i + 1) +
|
|
52
|
+
"</td><td>" +
|
|
53
|
+
escapeHtml(r.name) +
|
|
54
|
+
(r.directory ? " <small>文件夹</small>" : "") +
|
|
55
|
+
'</td><td class="' +
|
|
56
|
+
(r.error ? "danger" : "new-name") +
|
|
57
|
+
'">' +
|
|
58
|
+
(mode === "manual"
|
|
59
|
+
? '<input data-edit="' +
|
|
60
|
+
i +
|
|
61
|
+
'" value="' +
|
|
62
|
+
escapeHtml(r.newName) +
|
|
63
|
+
'" aria-label="新文件名">'
|
|
64
|
+
: escapeHtml(r.newName)) +
|
|
65
|
+
'</td><td class="path" title="' +
|
|
66
|
+
escapeHtml(r.path) +
|
|
67
|
+
'">' +
|
|
68
|
+
escapeHtml(r.path.slice(0, -r.name.length)) +
|
|
69
|
+
"</td><td>" +
|
|
70
|
+
escapeHtml(r.error || (r.changed ? "待修改" : "未变化")) +
|
|
71
|
+
'</td><td><button class="remove-file" data-remove="' +
|
|
72
|
+
i +
|
|
73
|
+
'" title="移出列表">×</button></td></tr>',
|
|
74
|
+
)
|
|
75
|
+
.join("");
|
|
76
|
+
$$("[data-remove]").forEach(
|
|
77
|
+
(b) =>
|
|
78
|
+
(b.onclick = () => {
|
|
79
|
+
files.splice(Number(b.dataset.remove), 1);
|
|
80
|
+
preview();
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
$$("[data-edit]").forEach(
|
|
84
|
+
(input) =>
|
|
85
|
+
(input.onchange = () => {
|
|
86
|
+
const names = rows.map((r) => r.newName);
|
|
87
|
+
names[Number(input.dataset.edit)] = input.value;
|
|
88
|
+
$("#extension").checked = true;
|
|
89
|
+
$("#lines").value = names.join("\n");
|
|
90
|
+
preview();
|
|
91
|
+
}),
|
|
92
|
+
);
|
|
93
|
+
$("#summary").textContent =
|
|
94
|
+
"共 " +
|
|
95
|
+
files.length +
|
|
96
|
+
" 项 · 修改 " +
|
|
97
|
+
changed.length +
|
|
98
|
+
" 项" +
|
|
99
|
+
(invalid ? " · 存在冲突" : "");
|
|
100
|
+
$("#commit").disabled = busy || !changed.length || invalid;
|
|
101
|
+
$("#undo").disabled = busy || !undo.length;
|
|
102
|
+
}
|
|
103
|
+
function preview() {
|
|
104
|
+
try {
|
|
105
|
+
rows = renamePreview(files, config());
|
|
106
|
+
render();
|
|
107
|
+
status("检查新旧名称后执行;原文件不会被覆盖");
|
|
108
|
+
} catch (e) {
|
|
109
|
+
$("#commit").disabled = true;
|
|
110
|
+
status(e.message, true);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function rules() {
|
|
114
|
+
$("#rules").innerHTML =
|
|
115
|
+
templates[mode] +
|
|
116
|
+
(["append", "sequence"].includes(mode)
|
|
117
|
+
? '<label>开始序号 <input id="start" type="number" min="0" value="1" style="width:64px"></label><label>步长 <input id="step" type="number" min="1" value="1" style="width:55px"></label><label>固定位数 <input id="pad" type="number" min="1" max="10" value="3" style="width:55px"></label>'
|
|
118
|
+
: "");
|
|
119
|
+
$$("#rules input,#rules select,#rules textarea").forEach(
|
|
120
|
+
(el) => (el.oninput = preview),
|
|
121
|
+
);
|
|
122
|
+
preview();
|
|
123
|
+
}
|
|
124
|
+
function add(entries) {
|
|
125
|
+
files = [
|
|
126
|
+
...new Map(
|
|
127
|
+
[...files, ...entries].map((f) => [f.path.toLowerCase(), f]),
|
|
128
|
+
).values(),
|
|
129
|
+
];
|
|
130
|
+
if (files.length > 1000) {
|
|
131
|
+
files = files.slice(0, 1000);
|
|
132
|
+
status("单批最多 1000 项,请分批操作", true);
|
|
133
|
+
}
|
|
134
|
+
sort();
|
|
135
|
+
}
|
|
136
|
+
$("#add-files").onclick = action(async () => {
|
|
137
|
+
const r = await call("fs.pickfiles");
|
|
138
|
+
add(r.entries);
|
|
139
|
+
});
|
|
140
|
+
window.mytools?.onFilesDropped?.(action(async () => {
|
|
141
|
+
status("正在导入拖放条目,请完成宿主授权…");
|
|
142
|
+
const result = await call("fs.importdropped");
|
|
143
|
+
add(result.entries);
|
|
144
|
+
status("已导入拖放条目;所在目录已授权,执行前请核对预览");
|
|
145
|
+
}));
|
|
146
|
+
$("#add-folder").onclick = action(async () => {
|
|
147
|
+
const r = await call("fs.pickfiles", { directories: true });
|
|
148
|
+
add(r.entries);
|
|
149
|
+
});
|
|
150
|
+
$("#directory").onclick = action(async () => {
|
|
151
|
+
const r = await call("fs.pickdirectory");
|
|
152
|
+
const found = await call("fs.search", {
|
|
153
|
+
path: r.path,
|
|
154
|
+
maxResults: 1000,
|
|
155
|
+
maxDepth: $("#recursive").checked ? 12 : 1,
|
|
156
|
+
});
|
|
157
|
+
add(found.entries);
|
|
158
|
+
if (found.truncated) status("目录超出单批上限,请分批导入", true);
|
|
159
|
+
});
|
|
160
|
+
function sort() {
|
|
161
|
+
files.sort((a, b) =>
|
|
162
|
+
$("#sort").value === "modified"
|
|
163
|
+
? b.modified - a.modified
|
|
164
|
+
: ($("#sort").value === "desc" ? -1 : 1) *
|
|
165
|
+
a.name.localeCompare(b.name, "zh-CN", { numeric: true }),
|
|
166
|
+
);
|
|
167
|
+
preview();
|
|
168
|
+
}
|
|
169
|
+
$("#sort").onchange = sort;
|
|
170
|
+
$("#extension").onchange = preview;
|
|
171
|
+
$$("#modes button[data-mode]").forEach(
|
|
172
|
+
(b) =>
|
|
173
|
+
(b.onclick = () => {
|
|
174
|
+
mode = b.dataset.mode;
|
|
175
|
+
$$("#modes button[data-mode]").forEach((x) =>
|
|
176
|
+
x.classList.toggle("active", x === b),
|
|
177
|
+
);
|
|
178
|
+
rules();
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
$("#clear").onclick = () => {
|
|
182
|
+
files = [];
|
|
183
|
+
preview();
|
|
184
|
+
};
|
|
185
|
+
$("#extract").onclick = action(() => copy(files.map((f) => f.name).join("\n")));
|
|
186
|
+
function confirmOperation(entries, isUndo) {
|
|
187
|
+
if (!entries.length || busy) return;
|
|
188
|
+
confirmed = { entries, isUndo };
|
|
189
|
+
$("#confirm-text").textContent =
|
|
190
|
+
(isUndo ? "撤销上次操作,恢复 " : "确认修改 ") +
|
|
191
|
+
entries.length +
|
|
192
|
+
" 项名称?";
|
|
193
|
+
for (const id of ["summary", "undo", "commit"])
|
|
194
|
+
$("#" + id).classList.add("hidden");
|
|
195
|
+
$("#confirm-bar").classList.remove("hidden");
|
|
196
|
+
}
|
|
197
|
+
$("#commit").onclick = () => {
|
|
198
|
+
if (rows.some((r) => r.error)) return;
|
|
199
|
+
confirmOperation(
|
|
200
|
+
rows.filter((r) => r.changed).map((r) => ({ from: r.path, to: r.to })),
|
|
201
|
+
false,
|
|
202
|
+
);
|
|
203
|
+
};
|
|
204
|
+
$("#undo").onclick = () => confirmOperation(undo, true);
|
|
205
|
+
$("#cancel-confirm").onclick = resetConfirmation;
|
|
206
|
+
$("#apply-confirm").onclick = action(async () => {
|
|
207
|
+
if (!confirmed || busy) return;
|
|
208
|
+
const operation = confirmed;
|
|
209
|
+
busy = true;
|
|
210
|
+
$$("button,input,select,textarea").forEach((el) => (el.disabled = true));
|
|
211
|
+
try {
|
|
212
|
+
const result = await call("fs.renamemany", { entries: operation.entries });
|
|
213
|
+
for (const item of result.renamed) {
|
|
214
|
+
const f = files.find((f) => f.path === item.from);
|
|
215
|
+
if (f) {
|
|
216
|
+
f.path = item.to;
|
|
217
|
+
f.name = item.to.split(/[\\/]/).pop();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
undo = operation.isUndo
|
|
221
|
+
? operation.entries.filter(
|
|
222
|
+
(e) => !result.renamed.some((done) => done.from === e.from),
|
|
223
|
+
)
|
|
224
|
+
: result.renamed.map((e) => ({ from: e.to, to: e.from })).reverse();
|
|
225
|
+
busy = false;
|
|
226
|
+
preview();
|
|
227
|
+
status(
|
|
228
|
+
(operation.isUndo ? "已恢复 " : "已修改 ") +
|
|
229
|
+
result.renamed.length +
|
|
230
|
+
" 项" +
|
|
231
|
+
(result.failed.length ? " · 停止:" + result.failed[0].reason : ""),
|
|
232
|
+
!!result.failed.length,
|
|
233
|
+
);
|
|
234
|
+
} finally {
|
|
235
|
+
busy = false;
|
|
236
|
+
$$("button,input,select,textarea").forEach((el) => (el.disabled = false));
|
|
237
|
+
$("#commit").disabled =
|
|
238
|
+
!rows.some((r) => r.changed) || rows.some((r) => r.error);
|
|
239
|
+
$("#undo").disabled = !undo.length;
|
|
240
|
+
resetConfirmation();
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
rules();
|
|
244
|
+
ready(() => status("选择文件或文件夹 → 设置规则 → 预览 → 执行;支持撤销"));
|
package/common.mjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export const $ = (selector) => document.querySelector(selector);
|
|
2
|
+
export const $$ = (selector) => [...document.querySelectorAll(selector)];
|
|
3
|
+
export function status(text, error = false) {
|
|
4
|
+
const el = $("#status");
|
|
5
|
+
if (el) {
|
|
6
|
+
el.textContent = text;
|
|
7
|
+
el.dataset.error = String(error);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function action(fn) {
|
|
11
|
+
return async (...args) => {
|
|
12
|
+
try {
|
|
13
|
+
return await fn(...args);
|
|
14
|
+
} catch (e) {
|
|
15
|
+
status(e.message || String(e), true);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export async function call(id, args = {}) {
|
|
20
|
+
if (!window.mytools) throw Error("请从 MyTools 打开此工具");
|
|
21
|
+
return window.mytools.call(id, args);
|
|
22
|
+
}
|
|
23
|
+
export const copy = async (text) => {
|
|
24
|
+
await call("clipboard.write", { text: String(text) });
|
|
25
|
+
status("已复制到剪贴板");
|
|
26
|
+
};
|
|
27
|
+
export function ready(fn) {
|
|
28
|
+
if (window.mytools) window.mytools.onPluginReady(action(fn));
|
|
29
|
+
else status("预览模式:系统能力需从 MyTools 使用");
|
|
30
|
+
}
|
|
31
|
+
export const escapeHtml = (text) =>
|
|
32
|
+
String(text).replace(
|
|
33
|
+
/[&<>"']/g,
|
|
34
|
+
(c) =>
|
|
35
|
+
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[
|
|
36
|
+
c
|
|
37
|
+
],
|
|
38
|
+
);
|
|
39
|
+
export function download(name, text, type = "text/plain") {
|
|
40
|
+
const url = URL.createObjectURL(new Blob([text], { type }));
|
|
41
|
+
const a = document.createElement("a");
|
|
42
|
+
a.href = url;
|
|
43
|
+
a.download = name;
|
|
44
|
+
a.click();
|
|
45
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
46
|
+
}
|
|
47
|
+
export function debounce(fn, ms = 200) {
|
|
48
|
+
let timer;
|
|
49
|
+
return (...args) => {
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
timer = setTimeout(() => fn(...args), ms);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export const formatBytes = (n) =>
|
|
55
|
+
n < 1024
|
|
56
|
+
? n + " B"
|
|
57
|
+
: n < 1048576
|
|
58
|
+
? (n / 1024).toFixed(1) + " KB"
|
|
59
|
+
: (n / 1048576).toFixed(1) + " MB";
|
|
60
|
+
const savedTheme = localStorage.getItem("tool-theme");
|
|
61
|
+
document.documentElement.dataset.theme =
|
|
62
|
+
savedTheme ||
|
|
63
|
+
(matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
|
|
64
|
+
$("#theme")?.addEventListener("click", () => {
|
|
65
|
+
const theme =
|
|
66
|
+
document.documentElement.dataset.theme === "dark" ? "light" : "dark";
|
|
67
|
+
document.documentElement.dataset.theme = theme;
|
|
68
|
+
localStorage.setItem("tool-theme", theme);
|
|
69
|
+
});
|
package/index.html
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
|
6
|
+
<title>批量重命名</title>
|
|
7
|
+
<link rel="stylesheet" href="tool.css" />
|
|
8
|
+
<style>
|
|
9
|
+
.rename-tabs {
|
|
10
|
+
padding: 10px 16px;
|
|
11
|
+
border-bottom: 1px solid var(--border);
|
|
12
|
+
display: flex;
|
|
13
|
+
gap: 8px;
|
|
14
|
+
}
|
|
15
|
+
.rename-tabs button {
|
|
16
|
+
border: 0;
|
|
17
|
+
padding: 8px 16px;
|
|
18
|
+
}
|
|
19
|
+
.rename-tabs .active {
|
|
20
|
+
color: var(--accent);
|
|
21
|
+
background: var(--soft);
|
|
22
|
+
}
|
|
23
|
+
.rule-area {
|
|
24
|
+
padding: 16px;
|
|
25
|
+
border-bottom: 1px solid var(--border);
|
|
26
|
+
min-height: 98px;
|
|
27
|
+
background: var(--surface);
|
|
28
|
+
}
|
|
29
|
+
#rules {
|
|
30
|
+
display: flex;
|
|
31
|
+
gap: 12px;
|
|
32
|
+
flex-wrap: wrap;
|
|
33
|
+
align-items: center;
|
|
34
|
+
}
|
|
35
|
+
#rules input {
|
|
36
|
+
width: 130px;
|
|
37
|
+
}
|
|
38
|
+
#rules label {
|
|
39
|
+
gap: 6px;
|
|
40
|
+
}
|
|
41
|
+
#rules textarea {
|
|
42
|
+
width: 100%;
|
|
43
|
+
min-height: 80px;
|
|
44
|
+
}
|
|
45
|
+
#file-pane {
|
|
46
|
+
flex: 1;
|
|
47
|
+
min-height: 0;
|
|
48
|
+
overflow: auto;
|
|
49
|
+
}
|
|
50
|
+
.file-actions {
|
|
51
|
+
display: flex;
|
|
52
|
+
gap: 8px;
|
|
53
|
+
padding: 10px 16px;
|
|
54
|
+
align-items: center;
|
|
55
|
+
border-bottom: 1px solid var(--border);
|
|
56
|
+
flex-wrap: wrap;
|
|
57
|
+
}
|
|
58
|
+
.empty-box {
|
|
59
|
+
padding: 45px 18px;
|
|
60
|
+
text-align: center;
|
|
61
|
+
color: var(--muted);
|
|
62
|
+
border: 1px dashed var(--border);
|
|
63
|
+
margin: 20px;
|
|
64
|
+
}
|
|
65
|
+
.new-name {
|
|
66
|
+
color: var(--accent);
|
|
67
|
+
}
|
|
68
|
+
td {
|
|
69
|
+
font-size: 12px;
|
|
70
|
+
}
|
|
71
|
+
td input {
|
|
72
|
+
padding: 3px 5px;
|
|
73
|
+
width: 100%;
|
|
74
|
+
min-width: 120px;
|
|
75
|
+
}
|
|
76
|
+
.path {
|
|
77
|
+
max-width: 210px;
|
|
78
|
+
white-space: nowrap;
|
|
79
|
+
overflow: hidden;
|
|
80
|
+
text-overflow: ellipsis;
|
|
81
|
+
color: var(--muted);
|
|
82
|
+
}
|
|
83
|
+
.remove-file {
|
|
84
|
+
border: 0;
|
|
85
|
+
padding: 2px 7px;
|
|
86
|
+
}
|
|
87
|
+
.footer-count {
|
|
88
|
+
font-size: 12px;
|
|
89
|
+
color: var(--muted);
|
|
90
|
+
}
|
|
91
|
+
.confirm-box {
|
|
92
|
+
background: var(--surface);
|
|
93
|
+
padding: 10px 16px;
|
|
94
|
+
border-top: 1px solid var(--border);
|
|
95
|
+
display: flex;
|
|
96
|
+
gap: 8px;
|
|
97
|
+
align-items: center;
|
|
98
|
+
}
|
|
99
|
+
#summary {
|
|
100
|
+
flex: 1;
|
|
101
|
+
}
|
|
102
|
+
.danger {
|
|
103
|
+
color: var(--danger);
|
|
104
|
+
}
|
|
105
|
+
.row-number {
|
|
106
|
+
color: var(--muted);
|
|
107
|
+
width: 40px;
|
|
108
|
+
}
|
|
109
|
+
#confirm-bar {
|
|
110
|
+
display: flex;
|
|
111
|
+
gap: 8px;
|
|
112
|
+
align-items: center;
|
|
113
|
+
width: 100%;
|
|
114
|
+
}
|
|
115
|
+
#confirm-bar strong {
|
|
116
|
+
flex: 1;
|
|
117
|
+
}
|
|
118
|
+
label input[type="checkbox"] {
|
|
119
|
+
width: auto !important;
|
|
120
|
+
}
|
|
121
|
+
</style>
|
|
122
|
+
</head>
|
|
123
|
+
<body>
|
|
124
|
+
<main>
|
|
125
|
+
<nav id="modes" class="rename-tabs">
|
|
126
|
+
<button data-mode="replace" class="active">查找替换</button
|
|
127
|
+
><button data-mode="append">追加文本</button
|
|
128
|
+
><button data-mode="sequence">自动序号</button
|
|
129
|
+
><button data-mode="case">大小写</button
|
|
130
|
+
><button data-mode="manual">编辑</button><span class="spacer"></span
|
|
131
|
+
><button id="theme">◐</button>
|
|
132
|
+
</nav>
|
|
133
|
+
<section class="rule-area"><div id="rules"></div></section>
|
|
134
|
+
<div class="file-actions">
|
|
135
|
+
<button id="add-files">+ 选择文件</button
|
|
136
|
+
><button id="add-folder">+ 选择文件夹</button
|
|
137
|
+
><button id="directory">导入目录中的文件</button
|
|
138
|
+
><label><input id="recursive" type="checkbox" />含子目录</label
|
|
139
|
+
><span class="spacer"></span
|
|
140
|
+
><label><input id="extension" type="checkbox" />修改扩展名</label
|
|
141
|
+
><select id="sort">
|
|
142
|
+
<option value="name">名称 ↑</option>
|
|
143
|
+
<option value="desc">名称 ↓</option>
|
|
144
|
+
<option value="modified">修改时间 ↓</option></select
|
|
145
|
+
><button id="extract">提取名称</button
|
|
146
|
+
><button id="clear">清空列表</button>
|
|
147
|
+
</div>
|
|
148
|
+
<section id="file-pane">
|
|
149
|
+
<div id="empty" class="empty-box">
|
|
150
|
+
<strong>拖入或选择需要重命名的文件 / 文件夹</strong>
|
|
151
|
+
<p>支持多选文件、导入目录;设置规则后实时预览新名称</p>
|
|
152
|
+
<span>选择文件时将请求授权所在目录,执行前会显示确认</span>
|
|
153
|
+
</div>
|
|
154
|
+
<table id="table" class="hidden">
|
|
155
|
+
<thead>
|
|
156
|
+
<tr>
|
|
157
|
+
<th class="row-number">序号</th>
|
|
158
|
+
<th>原名称</th>
|
|
159
|
+
<th>新名称</th>
|
|
160
|
+
<th>所在目录</th>
|
|
161
|
+
<th>状态</th>
|
|
162
|
+
<th></th>
|
|
163
|
+
</tr>
|
|
164
|
+
</thead>
|
|
165
|
+
<tbody id="files"></tbody>
|
|
166
|
+
</table>
|
|
167
|
+
</section>
|
|
168
|
+
<footer class="confirm-box">
|
|
169
|
+
<span id="summary" class="footer-count">共 0 项</span
|
|
170
|
+
><button id="undo" disabled>撤销上次重命名</button
|
|
171
|
+
><button id="commit" class="primary" disabled>执行重命名</button>
|
|
172
|
+
<div id="confirm-bar" class="hidden">
|
|
173
|
+
<strong id="confirm-text"></strong
|
|
174
|
+
><button id="cancel-confirm">取消</button
|
|
175
|
+
><button id="apply-confirm" class="primary">确认执行</button>
|
|
176
|
+
</div>
|
|
177
|
+
</footer>
|
|
178
|
+
<footer id="status" class="status" role="status"></footer>
|
|
179
|
+
</main>
|
|
180
|
+
<script type="module" src="app.mjs"></script>
|
|
181
|
+
</body>
|
|
182
|
+
</html>
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@mytools-all/mytools-rename",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@mytools-all/mytools-rename",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "文件批量重命名",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.html",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "MyTools Contributors",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/mytools-all/mytools-rename.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/mytools-all/mytools-rename#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/mytools-all/mytools-rename/issues"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public",
|
|
19
|
+
"registry": "https://registry.npmjs.org/"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"plugin.json",
|
|
23
|
+
"*.html",
|
|
24
|
+
"*.css",
|
|
25
|
+
"*.svg",
|
|
26
|
+
"*.png",
|
|
27
|
+
"vendor/",
|
|
28
|
+
"LICENSE",
|
|
29
|
+
"THIRD_PARTY.md",
|
|
30
|
+
"README.md",
|
|
31
|
+
"app.mjs",
|
|
32
|
+
"common.mjs",
|
|
33
|
+
"utils.mjs",
|
|
34
|
+
"*.js",
|
|
35
|
+
"*.LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"test": "node --test"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/plugin.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mytools-rename",
|
|
3
|
+
"description": "文件批量重命名",
|
|
4
|
+
"apiVersion": 1,
|
|
5
|
+
"main": "index.html",
|
|
6
|
+
"pluginName": "文件批量重命名",
|
|
7
|
+
"version": "0.1.0",
|
|
8
|
+
"capabilities": [
|
|
9
|
+
"fs.pickdirectory",
|
|
10
|
+
"fs.pickfiles",
|
|
11
|
+
"fs.search",
|
|
12
|
+
"fs.renamemany",
|
|
13
|
+
"clipboard.write",
|
|
14
|
+
"fs.importdropped"
|
|
15
|
+
],
|
|
16
|
+
"features": [
|
|
17
|
+
{
|
|
18
|
+
"code": "rename",
|
|
19
|
+
"explain": "打开文件批量重命名",
|
|
20
|
+
"cmds": [
|
|
21
|
+
"重命名",
|
|
22
|
+
"批量重命名",
|
|
23
|
+
"rename"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
}
|
package/tool.css
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
font:
|
|
3
|
+
13px/1.6 "Segoe UI",
|
|
4
|
+
"Microsoft YaHei",
|
|
5
|
+
sans-serif;
|
|
6
|
+
color-scheme: light;
|
|
7
|
+
--bg: #fff;
|
|
8
|
+
--surface: #f6f7f9;
|
|
9
|
+
--fg: #333b48;
|
|
10
|
+
--muted: #89919e;
|
|
11
|
+
--border: #e8ebf0;
|
|
12
|
+
--accent: #377cf6;
|
|
13
|
+
--soft: #edf3ff;
|
|
14
|
+
--danger: #db4552;
|
|
15
|
+
}
|
|
16
|
+
* {
|
|
17
|
+
box-sizing: border-box;
|
|
18
|
+
}
|
|
19
|
+
body {
|
|
20
|
+
margin: 0;
|
|
21
|
+
color: var(--fg);
|
|
22
|
+
background: var(--bg);
|
|
23
|
+
height: 100vh;
|
|
24
|
+
overflow: hidden;
|
|
25
|
+
}
|
|
26
|
+
button,
|
|
27
|
+
input,
|
|
28
|
+
textarea,
|
|
29
|
+
select {
|
|
30
|
+
font: inherit;
|
|
31
|
+
color: inherit;
|
|
32
|
+
}
|
|
33
|
+
button {
|
|
34
|
+
border: 1px solid var(--border);
|
|
35
|
+
border-radius: 5px;
|
|
36
|
+
background: var(--bg);
|
|
37
|
+
padding: 5px 12px;
|
|
38
|
+
cursor: pointer;
|
|
39
|
+
white-space: nowrap;
|
|
40
|
+
}
|
|
41
|
+
button:hover {
|
|
42
|
+
border-color: var(--accent);
|
|
43
|
+
color: var(--accent);
|
|
44
|
+
}
|
|
45
|
+
button:disabled {
|
|
46
|
+
opacity: 0.45;
|
|
47
|
+
cursor: default;
|
|
48
|
+
}
|
|
49
|
+
.primary {
|
|
50
|
+
background: var(--accent);
|
|
51
|
+
border-color: var(--accent);
|
|
52
|
+
color: white;
|
|
53
|
+
}
|
|
54
|
+
.primary:hover {
|
|
55
|
+
color: white;
|
|
56
|
+
filter: brightness(1.06);
|
|
57
|
+
}
|
|
58
|
+
input,
|
|
59
|
+
textarea,
|
|
60
|
+
select {
|
|
61
|
+
border: 1px solid var(--border);
|
|
62
|
+
border-radius: 4px;
|
|
63
|
+
padding: 7px 9px;
|
|
64
|
+
background: var(--bg);
|
|
65
|
+
min-width: 0;
|
|
66
|
+
}
|
|
67
|
+
textarea {
|
|
68
|
+
resize: none;
|
|
69
|
+
line-height: 1.7;
|
|
70
|
+
tab-size: 2;
|
|
71
|
+
font-family: Consolas, "Microsoft YaHei", monospace;
|
|
72
|
+
}
|
|
73
|
+
input:focus,
|
|
74
|
+
textarea:focus,
|
|
75
|
+
select:focus {
|
|
76
|
+
outline: 1px solid var(--accent);
|
|
77
|
+
}
|
|
78
|
+
button:focus-visible {
|
|
79
|
+
outline: 2px solid var(--accent);
|
|
80
|
+
outline-offset: 2px;
|
|
81
|
+
}
|
|
82
|
+
main {
|
|
83
|
+
display: flex;
|
|
84
|
+
flex-direction: column;
|
|
85
|
+
height: 100%;
|
|
86
|
+
min-height: 0;
|
|
87
|
+
}
|
|
88
|
+
.toolbar {
|
|
89
|
+
display: flex;
|
|
90
|
+
align-items: center;
|
|
91
|
+
gap: 8px;
|
|
92
|
+
padding: 10px 14px;
|
|
93
|
+
border-bottom: 1px solid var(--border);
|
|
94
|
+
flex-wrap: wrap;
|
|
95
|
+
}
|
|
96
|
+
.toolbar strong {
|
|
97
|
+
font-size: 14px;
|
|
98
|
+
margin-right: 10px;
|
|
99
|
+
}
|
|
100
|
+
.spacer {
|
|
101
|
+
flex: 1;
|
|
102
|
+
}
|
|
103
|
+
.content {
|
|
104
|
+
flex: 1;
|
|
105
|
+
min-height: 0;
|
|
106
|
+
overflow: auto;
|
|
107
|
+
padding: 16px;
|
|
108
|
+
}
|
|
109
|
+
.split {
|
|
110
|
+
display: grid;
|
|
111
|
+
grid-template-columns: 1fr 1fr;
|
|
112
|
+
flex: 1;
|
|
113
|
+
min-height: 0;
|
|
114
|
+
}
|
|
115
|
+
.split > * {
|
|
116
|
+
min-width: 0;
|
|
117
|
+
overflow: auto;
|
|
118
|
+
}
|
|
119
|
+
.split > :first-child {
|
|
120
|
+
border-right: 1px solid var(--border);
|
|
121
|
+
}
|
|
122
|
+
.editor {
|
|
123
|
+
width: 100%;
|
|
124
|
+
height: 100%;
|
|
125
|
+
border: 0;
|
|
126
|
+
border-radius: 0;
|
|
127
|
+
padding: 16px;
|
|
128
|
+
outline: none !important;
|
|
129
|
+
background: var(--bg);
|
|
130
|
+
}
|
|
131
|
+
.muted,
|
|
132
|
+
small {
|
|
133
|
+
color: var(--muted);
|
|
134
|
+
}
|
|
135
|
+
.status {
|
|
136
|
+
font-size: 12px;
|
|
137
|
+
min-height: 26px;
|
|
138
|
+
border-top: 1px solid var(--border);
|
|
139
|
+
padding: 3px 14px;
|
|
140
|
+
color: var(--muted);
|
|
141
|
+
}
|
|
142
|
+
.status[data-error="true"] {
|
|
143
|
+
color: var(--danger);
|
|
144
|
+
}
|
|
145
|
+
.tabs {
|
|
146
|
+
display: flex;
|
|
147
|
+
gap: 5px;
|
|
148
|
+
align-items: center;
|
|
149
|
+
}
|
|
150
|
+
.tabs button {
|
|
151
|
+
border: 0;
|
|
152
|
+
background: transparent;
|
|
153
|
+
}
|
|
154
|
+
.tabs .active {
|
|
155
|
+
color: var(--accent);
|
|
156
|
+
background: var(--soft);
|
|
157
|
+
}
|
|
158
|
+
.empty {
|
|
159
|
+
padding: 48px 16px;
|
|
160
|
+
text-align: center;
|
|
161
|
+
color: var(--muted);
|
|
162
|
+
}
|
|
163
|
+
table {
|
|
164
|
+
border-collapse: collapse;
|
|
165
|
+
width: 100%;
|
|
166
|
+
text-align: left;
|
|
167
|
+
}
|
|
168
|
+
th {
|
|
169
|
+
font-size: 12px;
|
|
170
|
+
color: var(--muted);
|
|
171
|
+
font-weight: 400;
|
|
172
|
+
background: var(--surface);
|
|
173
|
+
position: sticky;
|
|
174
|
+
top: 0;
|
|
175
|
+
}
|
|
176
|
+
th,
|
|
177
|
+
td {
|
|
178
|
+
padding: 9px 12px;
|
|
179
|
+
border-bottom: 1px solid var(--border);
|
|
180
|
+
}
|
|
181
|
+
tr.selected {
|
|
182
|
+
background: var(--soft);
|
|
183
|
+
}
|
|
184
|
+
pre {
|
|
185
|
+
white-space: pre-wrap;
|
|
186
|
+
overflow-wrap: anywhere;
|
|
187
|
+
margin: 0;
|
|
188
|
+
}
|
|
189
|
+
label {
|
|
190
|
+
display: flex;
|
|
191
|
+
gap: 8px;
|
|
192
|
+
align-items: center;
|
|
193
|
+
}
|
|
194
|
+
.stack {
|
|
195
|
+
display: flex;
|
|
196
|
+
flex-direction: column;
|
|
197
|
+
gap: 12px;
|
|
198
|
+
}
|
|
199
|
+
.row {
|
|
200
|
+
display: flex;
|
|
201
|
+
gap: 8px;
|
|
202
|
+
align-items: center;
|
|
203
|
+
}
|
|
204
|
+
.panel {
|
|
205
|
+
border: 1px solid var(--border);
|
|
206
|
+
border-radius: 6px;
|
|
207
|
+
padding: 14px;
|
|
208
|
+
}
|
|
209
|
+
.badge {
|
|
210
|
+
font-size: 11px;
|
|
211
|
+
padding: 1px 7px;
|
|
212
|
+
border-radius: 4px;
|
|
213
|
+
background: var(--surface);
|
|
214
|
+
color: var(--muted);
|
|
215
|
+
}
|
|
216
|
+
.danger {
|
|
217
|
+
color: var(--danger);
|
|
218
|
+
}
|
|
219
|
+
.hidden {
|
|
220
|
+
display: none !important;
|
|
221
|
+
}
|
|
222
|
+
#theme {
|
|
223
|
+
padding: 3px 8px;
|
|
224
|
+
}
|
|
225
|
+
.sidebar {
|
|
226
|
+
width: 155px;
|
|
227
|
+
background: var(--surface);
|
|
228
|
+
padding: 14px 10px;
|
|
229
|
+
border-right: 1px solid var(--border);
|
|
230
|
+
overflow: auto;
|
|
231
|
+
}
|
|
232
|
+
.sidebar button {
|
|
233
|
+
display: block;
|
|
234
|
+
width: 100%;
|
|
235
|
+
text-align: left;
|
|
236
|
+
border: 0;
|
|
237
|
+
background: transparent;
|
|
238
|
+
margin-bottom: 4px;
|
|
239
|
+
}
|
|
240
|
+
.sidebar .active {
|
|
241
|
+
background: var(--soft);
|
|
242
|
+
color: var(--accent);
|
|
243
|
+
}
|
|
244
|
+
.workspace {
|
|
245
|
+
display: flex;
|
|
246
|
+
flex: 1;
|
|
247
|
+
min-height: 0;
|
|
248
|
+
}
|
|
249
|
+
.workspace > .content {
|
|
250
|
+
flex: 1;
|
|
251
|
+
}
|
|
252
|
+
details {
|
|
253
|
+
border-bottom: 1px solid var(--border);
|
|
254
|
+
padding: 7px 0;
|
|
255
|
+
}
|
|
256
|
+
summary {
|
|
257
|
+
cursor: pointer;
|
|
258
|
+
}
|
|
259
|
+
::-webkit-scrollbar {
|
|
260
|
+
width: 6px;
|
|
261
|
+
height: 6px;
|
|
262
|
+
}
|
|
263
|
+
::-webkit-scrollbar-thumb {
|
|
264
|
+
background: #b9c0cb60;
|
|
265
|
+
border-radius: 8px;
|
|
266
|
+
}
|
|
267
|
+
html[data-theme="dark"] {
|
|
268
|
+
color-scheme: dark;
|
|
269
|
+
--bg: #1b1e24;
|
|
270
|
+
--surface: #222731;
|
|
271
|
+
--fg: #dce1eb;
|
|
272
|
+
--muted: #8894a8;
|
|
273
|
+
--border: #333a47;
|
|
274
|
+
--accent: #6ca7ff;
|
|
275
|
+
--soft: #27364c;
|
|
276
|
+
--danger: #ff8490;
|
|
277
|
+
}
|
|
278
|
+
@media (max-width: 560px) {
|
|
279
|
+
.sidebar {
|
|
280
|
+
width: 115px;
|
|
281
|
+
}
|
|
282
|
+
.toolbar {
|
|
283
|
+
padding: 8px;
|
|
284
|
+
gap: 5px;
|
|
285
|
+
}
|
|
286
|
+
button {
|
|
287
|
+
padding: 5px 8px;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
@media (prefers-reduced-motion: reduce) {
|
|
291
|
+
* {
|
|
292
|
+
scroll-behavior: auto !important;
|
|
293
|
+
animation: none !important;
|
|
294
|
+
}
|
|
295
|
+
}
|
package/utils.mjs
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
export function evaluate(source) {
|
|
2
|
+
const text = String(source)
|
|
3
|
+
.replace(/[,,\s]/g, "")
|
|
4
|
+
.replace(/×/g, "*")
|
|
5
|
+
.replace(/÷/g, "/");
|
|
6
|
+
let p = 0;
|
|
7
|
+
const eat = (c) => (text[p] === c ? (p++, true) : false);
|
|
8
|
+
function expr() {
|
|
9
|
+
let v = term();
|
|
10
|
+
for (;;) {
|
|
11
|
+
if (eat("+")) v += term();
|
|
12
|
+
else if (eat("-")) v -= term();
|
|
13
|
+
else return v;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function term() {
|
|
17
|
+
let v = unary();
|
|
18
|
+
for (;;) {
|
|
19
|
+
if (eat("*")) v *= unary();
|
|
20
|
+
else if (eat("/")) {
|
|
21
|
+
const d = unary();
|
|
22
|
+
if (d === 0) throw Error("不能除以零");
|
|
23
|
+
v /= d;
|
|
24
|
+
} else return v;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function unary() {
|
|
28
|
+
if (eat("+")) return unary();
|
|
29
|
+
if (eat("-")) return -unary();
|
|
30
|
+
return power();
|
|
31
|
+
}
|
|
32
|
+
function power() {
|
|
33
|
+
let v = primary();
|
|
34
|
+
while (eat("%")) v /= 100;
|
|
35
|
+
if (eat("^")) v = Math.pow(v, unary());
|
|
36
|
+
return v;
|
|
37
|
+
}
|
|
38
|
+
function primary() {
|
|
39
|
+
if (eat("(")) {
|
|
40
|
+
const v = expr();
|
|
41
|
+
if (!eat(")")) throw Error("缺少右括号");
|
|
42
|
+
return v;
|
|
43
|
+
}
|
|
44
|
+
const m = text.slice(p).match(/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?/i);
|
|
45
|
+
if (!m) throw Error("请输入有效的计算公式");
|
|
46
|
+
p += m[0].length;
|
|
47
|
+
return Number(m[0]);
|
|
48
|
+
}
|
|
49
|
+
if (!text) throw Error("表达式为空");
|
|
50
|
+
const v = expr();
|
|
51
|
+
if (p !== text.length || !Number.isFinite(v)) throw Error("计算公式不完整");
|
|
52
|
+
return Number(v.toPrecision(14));
|
|
53
|
+
}
|
|
54
|
+
export function renamePreview(files, rule) {
|
|
55
|
+
const targets = new Map();
|
|
56
|
+
const rows = files.map((file, i) => {
|
|
57
|
+
const dot = file.directory ? -1 : file.name.lastIndexOf(".");
|
|
58
|
+
const ext = dot > 0 ? file.name.slice(dot) : "";
|
|
59
|
+
let base = rule.extension
|
|
60
|
+
? file.name
|
|
61
|
+
: dot > 0
|
|
62
|
+
? file.name.slice(0, dot)
|
|
63
|
+
: file.name;
|
|
64
|
+
if (rule.mode === "replace") {
|
|
65
|
+
if (rule.find) {
|
|
66
|
+
base = rule.regex
|
|
67
|
+
? base.replace(new RegExp(rule.find, "g"), rule.replace)
|
|
68
|
+
: base.split(rule.find).join(rule.replace);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (rule.mode === "append") {
|
|
72
|
+
const seq = String(
|
|
73
|
+
(Number.isFinite(Number(rule.start)) ? Number(rule.start) : 1) +
|
|
74
|
+
i * (Number(rule.step) || 1),
|
|
75
|
+
).padStart(Number(rule.pad) || 1, "0");
|
|
76
|
+
const text = String(rule.text || "").replaceAll("{n}", seq);
|
|
77
|
+
base =
|
|
78
|
+
rule.position === "before"
|
|
79
|
+
? text + base
|
|
80
|
+
: rule.position === "at"
|
|
81
|
+
? base.slice(0, Number(rule.index) || 0) +
|
|
82
|
+
text +
|
|
83
|
+
base.slice(Number(rule.index) || 0)
|
|
84
|
+
: base + text;
|
|
85
|
+
}
|
|
86
|
+
if (rule.mode === "sequence")
|
|
87
|
+
base =
|
|
88
|
+
(rule.prefix || "") +
|
|
89
|
+
String(
|
|
90
|
+
(Number.isFinite(Number(rule.start)) ? Number(rule.start) : 1) +
|
|
91
|
+
i * (Number(rule.step) || 1),
|
|
92
|
+
).padStart(Number(rule.pad) || 1, "0") +
|
|
93
|
+
(rule.suffix || "");
|
|
94
|
+
if (rule.mode === "case")
|
|
95
|
+
base =
|
|
96
|
+
rule.case === "upper"
|
|
97
|
+
? base.toUpperCase()
|
|
98
|
+
: rule.case === "title"
|
|
99
|
+
? base.replace(/\b\p{L}/gu, (c) => c.toUpperCase())
|
|
100
|
+
: base.toLowerCase();
|
|
101
|
+
if (rule.mode === "manual")
|
|
102
|
+
base =
|
|
103
|
+
String(rule.lines || "")
|
|
104
|
+
.split("\n")
|
|
105
|
+
[i]?.trim() || base;
|
|
106
|
+
const name = base + (rule.extension ? "" : ext);
|
|
107
|
+
const stem = name.split(".")[0];
|
|
108
|
+
const invalid =
|
|
109
|
+
/[\\/:*?"<>|\x00-\x1f]/.test(name) ||
|
|
110
|
+
/[. ]$/.test(name) ||
|
|
111
|
+
!name ||
|
|
112
|
+
/^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i.test(stem);
|
|
113
|
+
const to = file.path.slice(0, file.path.length - file.name.length) + name;
|
|
114
|
+
const row = {
|
|
115
|
+
...file,
|
|
116
|
+
to,
|
|
117
|
+
newName: name,
|
|
118
|
+
error: invalid ? "文件名无效" : "",
|
|
119
|
+
changed: name !== file.name,
|
|
120
|
+
};
|
|
121
|
+
const key = to.toLowerCase();
|
|
122
|
+
if (targets.has(key)) {
|
|
123
|
+
row.error = "目标重名";
|
|
124
|
+
targets.get(key).error = "目标重名";
|
|
125
|
+
}
|
|
126
|
+
targets.set(key, row);
|
|
127
|
+
return row;
|
|
128
|
+
});
|
|
129
|
+
const existing = new Set(files.map((f) => f.path.toLowerCase()));
|
|
130
|
+
for (const row of rows)
|
|
131
|
+
if (row.changed && existing.has(row.to.toLowerCase()))
|
|
132
|
+
row.error = "目标已存在";
|
|
133
|
+
return rows;
|
|
134
|
+
}
|
|
135
|
+
export function jsonPath(value, path) {
|
|
136
|
+
const parts = String(path)
|
|
137
|
+
.trim()
|
|
138
|
+
.replace(/^\$\.?/, "")
|
|
139
|
+
.replace(/\[(\d+)\]/g, ".$1")
|
|
140
|
+
.split(".")
|
|
141
|
+
.filter(Boolean);
|
|
142
|
+
if (!parts.length) return value;
|
|
143
|
+
let values = [value];
|
|
144
|
+
let wildcard = false;
|
|
145
|
+
for (const part of parts) {
|
|
146
|
+
if (part === "*") {
|
|
147
|
+
wildcard = true;
|
|
148
|
+
values = values.flatMap((v) =>
|
|
149
|
+
v && typeof v === "object" ? Object.values(v) : [],
|
|
150
|
+
);
|
|
151
|
+
} else
|
|
152
|
+
values = values
|
|
153
|
+
.map((v) =>
|
|
154
|
+
v != null && Object.hasOwn(Object(v), part) ? v[part] : undefined,
|
|
155
|
+
)
|
|
156
|
+
.filter((v) => v !== undefined);
|
|
157
|
+
}
|
|
158
|
+
if (!values.length) throw Error("路径未匹配任何值");
|
|
159
|
+
return wildcard ? values : values[0];
|
|
160
|
+
}
|
|
161
|
+
export function toXml(value, name = "root") {
|
|
162
|
+
const esc = (s) =>
|
|
163
|
+
String(s ?? "").replace(
|
|
164
|
+
/[<>&"']/g,
|
|
165
|
+
(c) =>
|
|
166
|
+
({
|
|
167
|
+
"<": "<",
|
|
168
|
+
">": ">",
|
|
169
|
+
"&": "&",
|
|
170
|
+
'"': """,
|
|
171
|
+
"'": "'",
|
|
172
|
+
})[c],
|
|
173
|
+
);
|
|
174
|
+
const tag = /^[A-Za-z_][\w.-]*$/.test(name) ? name : "item";
|
|
175
|
+
if (Array.isArray(value))
|
|
176
|
+
return `<${tag}>${value.map((v) => toXml(v, "item")).join("")}</${tag}>`;
|
|
177
|
+
if (value && typeof value === "object")
|
|
178
|
+
return `<${tag}>${Object.entries(value)
|
|
179
|
+
.map(([k, v]) => toXml(v, k))
|
|
180
|
+
.join("")}</${tag}>`;
|
|
181
|
+
return `<${tag}>${esc(value)}</${tag}>`;
|
|
182
|
+
}
|
|
183
|
+
export function toTypescript(value) {
|
|
184
|
+
function type(v) {
|
|
185
|
+
if (v === null) return "null";
|
|
186
|
+
if (Array.isArray(v))
|
|
187
|
+
return v.length
|
|
188
|
+
? "(" + [...new Set(v.map(type))].join(" | ") + ")[]"
|
|
189
|
+
: "unknown[]";
|
|
190
|
+
if (typeof v === "object")
|
|
191
|
+
return (
|
|
192
|
+
"{\n" +
|
|
193
|
+
Object.entries(v)
|
|
194
|
+
.map(([k, x]) => " " + JSON.stringify(k) + ": " + type(x) + ";")
|
|
195
|
+
.join("\n") +
|
|
196
|
+
"\n}"
|
|
197
|
+
);
|
|
198
|
+
return typeof v;
|
|
199
|
+
}
|
|
200
|
+
return "type Root = " + type(value) + ";";
|
|
201
|
+
}
|