@mytools-all/mytools-search 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 +391 -0
- package/common.mjs +69 -0
- package/index.html +231 -0
- package/package.json +41 -6
- package/plugin.json +30 -0
- package/searchQuery.mjs +22 -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-search@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.searchstatus、fs.globalsearch、fs.searchpreview、shell.open、shell.reveal、clipboard.write、app.list、app.launch。目录和危险操作仍需用户授权。
|
|
18
|
+
|
|
19
|
+
## 许可
|
|
20
|
+
|
|
21
|
+
项目代码使用 MIT;随附的第三方代码遵循各自随附的许可证。
|
package/app.mjs
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import {
|
|
2
|
+
$,
|
|
3
|
+
$$,
|
|
4
|
+
action,
|
|
5
|
+
copy,
|
|
6
|
+
status,
|
|
7
|
+
ready,
|
|
8
|
+
call,
|
|
9
|
+
escapeHtml,
|
|
10
|
+
formatBytes,
|
|
11
|
+
debounce,
|
|
12
|
+
} from "./common.mjs";
|
|
13
|
+
import { buildSearchQuery } from "./searchQuery.mjs";
|
|
14
|
+
const filters = [
|
|
15
|
+
["all", "全部", ""],
|
|
16
|
+
["folder", "文件夹", "folder:"],
|
|
17
|
+
["doc", "文档", "ext:doc;docx;xls;xlsx;ppt;pptx;txt;md;csv"],
|
|
18
|
+
["pdf", "PDF", "ext:pdf"],
|
|
19
|
+
["image", "图片", "ext:png;jpg;jpeg;gif;bmp;webp;svg"],
|
|
20
|
+
["video", "视频", "ext:mp4;mkv;mov;avi;webm"],
|
|
21
|
+
["audio", "音频", "ext:mp3;wav;flac;aac;ogg"],
|
|
22
|
+
["archive", "压缩文件", "ext:zip;rar;7z;gz;tar"],
|
|
23
|
+
["exe", "程序", "ext:exe;msi;bat;cmd"],
|
|
24
|
+
];
|
|
25
|
+
let entries = [],
|
|
26
|
+
selected = -1,
|
|
27
|
+
total = 0,
|
|
28
|
+
offset = 0,
|
|
29
|
+
type = "all",
|
|
30
|
+
busy = false,
|
|
31
|
+
pending = false,
|
|
32
|
+
generation = 0,
|
|
33
|
+
previewUrl = "",
|
|
34
|
+
previewGeneration = 0,
|
|
35
|
+
history = [];
|
|
36
|
+
const limit = 100;
|
|
37
|
+
try {
|
|
38
|
+
const saved = JSON.parse(localStorage.getItem("search-custom-filters") || "[]");
|
|
39
|
+
if (Array.isArray(saved)) filters.push(...saved.filter(f=>Array.isArray(f) && f.length===3 && f.every(x=>typeof x === "string") && f[0].startsWith("custom-")).slice(0,30));
|
|
40
|
+
} catch {}
|
|
41
|
+
function renderFilters() {
|
|
42
|
+
$("#types").innerHTML = filters
|
|
43
|
+
.map(
|
|
44
|
+
([id, label]) =>
|
|
45
|
+
'<button data-type="' +
|
|
46
|
+
escapeHtml(id) +
|
|
47
|
+
'" class="' +
|
|
48
|
+
(id === type ? "active" : "") +
|
|
49
|
+
'">' +
|
|
50
|
+
escapeHtml(label) +
|
|
51
|
+
"</button>",
|
|
52
|
+
)
|
|
53
|
+
.join("");
|
|
54
|
+
$("#custom-filter-list").innerHTML = filters.filter(f=>f[0].startsWith("custom-")).map(([id,label])=>'<div>'+escapeHtml(label)+' <button data-remove-filter="'+escapeHtml(id)+'" aria-label="删除过滤项">×</button></div>').join("");
|
|
55
|
+
}
|
|
56
|
+
renderFilters();
|
|
57
|
+
function saveFilters() {
|
|
58
|
+
localStorage.setItem("search-custom-filters", JSON.stringify(filters.filter(f=>f[0].startsWith("custom-"))));
|
|
59
|
+
renderFilters();
|
|
60
|
+
}
|
|
61
|
+
$("#save-filter").onclick = action(() => {
|
|
62
|
+
const label=$("#filter-name").value.trim(), query=$("#filter-query").value.trim();
|
|
63
|
+
if (!label || !query) throw Error("请填写名称和 Everything 条件");
|
|
64
|
+
if (filters.length>=39) throw Error("最多保存30个自定义过滤项");
|
|
65
|
+
filters.push(["custom-"+crypto.randomUUID(),label.slice(0,30),query.slice(0,1000)]);
|
|
66
|
+
saveFilters();$("#filter-name").value="";$("#filter-query").value="";
|
|
67
|
+
});
|
|
68
|
+
$("#custom-filter-list").onclick = e => {
|
|
69
|
+
const id=e.target.closest("[data-remove-filter]")?.dataset.removeFilter;
|
|
70
|
+
const at=filters.findIndex(f=>f[0]===id);if(at<0)return;
|
|
71
|
+
filters.splice(at,1);if(type===id){type="all";offset=0;void action(search)();}saveFilters();
|
|
72
|
+
};
|
|
73
|
+
function picked() {
|
|
74
|
+
if (!entries[selected]) throw Error("请先选择结果");
|
|
75
|
+
return entries[selected];
|
|
76
|
+
}
|
|
77
|
+
function select(index, focus = false) {
|
|
78
|
+
selected = Math.max(0, Math.min(entries.length - 1, index));
|
|
79
|
+
$$(".result").forEach((el, i) => {
|
|
80
|
+
el.classList.toggle("selected", i === selected);
|
|
81
|
+
el.setAttribute("aria-selected", String(i === selected));
|
|
82
|
+
});
|
|
83
|
+
const row = $$(".result")[selected];
|
|
84
|
+
row?.scrollIntoView({ block: "nearest" });
|
|
85
|
+
if (focus) $("#results").focus();
|
|
86
|
+
if (!$("#preview").classList.contains("hidden")) void preview();
|
|
87
|
+
}
|
|
88
|
+
function render() {
|
|
89
|
+
const q = $("#query").value.trim();
|
|
90
|
+
const words = q
|
|
91
|
+
.split(/\s+/)
|
|
92
|
+
.filter((w) => w && !w.includes(":") && !/[|<>"*?]/.test(w));
|
|
93
|
+
const highlight = (name) => {
|
|
94
|
+
if (!words.length) return escapeHtml(name);
|
|
95
|
+
const regex = new RegExp(
|
|
96
|
+
words
|
|
97
|
+
.map((word) => word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
|
|
98
|
+
.join("|"),
|
|
99
|
+
"gi",
|
|
100
|
+
);
|
|
101
|
+
let output = "",
|
|
102
|
+
at = 0;
|
|
103
|
+
for (const match of name.matchAll(regex)) {
|
|
104
|
+
output +=
|
|
105
|
+
escapeHtml(name.slice(at, match.index)) +
|
|
106
|
+
"<mark>" +
|
|
107
|
+
escapeHtml(match[0]) +
|
|
108
|
+
"</mark>";
|
|
109
|
+
at = match.index + match[0].length;
|
|
110
|
+
}
|
|
111
|
+
return output + escapeHtml(name.slice(at));
|
|
112
|
+
};
|
|
113
|
+
$("#results").innerHTML = entries.length
|
|
114
|
+
? entries
|
|
115
|
+
.map(
|
|
116
|
+
(e, i) =>
|
|
117
|
+
'<div role="option" class="result" data-index="' +
|
|
118
|
+
i +
|
|
119
|
+
'"><span class="file-icon">' +
|
|
120
|
+
(e.directory ? "▱" : "▤") +
|
|
121
|
+
'</span><div><div class="file-name">' +
|
|
122
|
+
highlight(e.name) +
|
|
123
|
+
'</div><div class="file-path" title="' +
|
|
124
|
+
escapeHtml(e.path) +
|
|
125
|
+
'">' +
|
|
126
|
+
escapeHtml(e.path.replace(/[\\/][^\\/]+$/, "")) +
|
|
127
|
+
'</div></div><div class="meta">' +
|
|
128
|
+
(e.directory ? "文件夹" : formatBytes(e.size)) +
|
|
129
|
+
"<br>" +
|
|
130
|
+
(e.modified ? new Date(e.modified * 1000).toLocaleString() : "") +
|
|
131
|
+
"</div></div>",
|
|
132
|
+
)
|
|
133
|
+
.join("")
|
|
134
|
+
: '<div class="empty">没有匹配的文件</div>';
|
|
135
|
+
$$(".result").forEach((el) => {
|
|
136
|
+
el.onclick = () => select(Number(el.dataset.index), true);
|
|
137
|
+
el.ondblclick = action(() =>
|
|
138
|
+
call("shell.open", { path: entries[Number(el.dataset.index)].path }),
|
|
139
|
+
);
|
|
140
|
+
el.oncontextmenu = (e) => {
|
|
141
|
+
e.preventDefault();
|
|
142
|
+
select(Number(el.dataset.index), true);
|
|
143
|
+
void action(() => call("shell.reveal", { path: picked().path }))();
|
|
144
|
+
};
|
|
145
|
+
});
|
|
146
|
+
$("#count").textContent =
|
|
147
|
+
(total ? offset + 1 : 0) + "–" + (offset + entries.length) + " / " + total;
|
|
148
|
+
$("#previous").disabled = offset === 0;
|
|
149
|
+
$("#next").disabled = offset + entries.length >= total;
|
|
150
|
+
select(0);
|
|
151
|
+
}
|
|
152
|
+
function showConnection(message, title = "未连接到 Everything") {
|
|
153
|
+
$("#connection strong").textContent = title;
|
|
154
|
+
$("#connection").classList.remove("hidden");
|
|
155
|
+
$("#connection-message").textContent = message;
|
|
156
|
+
}
|
|
157
|
+
async function search() {
|
|
158
|
+
if (busy) {
|
|
159
|
+
pending = true;
|
|
160
|
+
generation++;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
busy = true;
|
|
164
|
+
const token = ++generation;
|
|
165
|
+
$("#refresh").disabled = true;
|
|
166
|
+
status("正在查询 Everything…");
|
|
167
|
+
const q = $("#query").value.trim();
|
|
168
|
+
try {
|
|
169
|
+
const filter = filters.find((f) => f[0] === type)[2];
|
|
170
|
+
const query = buildSearchQuery({query:q,category:filter,extension:$("#extension-filter").value,excluded:$("#exclude-filter").value,period:$("#modified-period").value,from:$("#modified-from").value,to:$("#modified-to").value});
|
|
171
|
+
const result = await call("fs.globalsearch", {
|
|
172
|
+
query,
|
|
173
|
+
offset,
|
|
174
|
+
limit,
|
|
175
|
+
sort: Number($("#sort").value),
|
|
176
|
+
});
|
|
177
|
+
if (token !== generation) return;
|
|
178
|
+
entries = result.entries;
|
|
179
|
+
total = result.total;
|
|
180
|
+
$("#connection").classList.add("hidden");
|
|
181
|
+
render();
|
|
182
|
+
status("Everything · " + total.toLocaleString() + " 个结果");
|
|
183
|
+
} catch (e) {
|
|
184
|
+
if (token === generation) {
|
|
185
|
+
entries = [];
|
|
186
|
+
total = 0;
|
|
187
|
+
render();
|
|
188
|
+
showConnection(
|
|
189
|
+
e.message || String(e),
|
|
190
|
+
/授权|拒绝|denied|permission/i.test(e.message || "")
|
|
191
|
+
? "搜索权限未获允许"
|
|
192
|
+
: "Everything 查询失败",
|
|
193
|
+
);
|
|
194
|
+
status(e.message || String(e), true);
|
|
195
|
+
}
|
|
196
|
+
} finally {
|
|
197
|
+
busy = false;
|
|
198
|
+
$("#refresh").disabled = false;
|
|
199
|
+
if (pending) {
|
|
200
|
+
pending = false;
|
|
201
|
+
void action(search)();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function remember() {
|
|
206
|
+
const q = $("#query").value.trim();
|
|
207
|
+
if (!q) return;
|
|
208
|
+
history = [q, ...history.filter((x) => x !== q)].slice(0, 20);
|
|
209
|
+
$("#history").innerHTML =
|
|
210
|
+
'<option value="">搜索历史</option>' +
|
|
211
|
+
history.map((q) => "<option>" + escapeHtml(q) + "</option>").join("");
|
|
212
|
+
localStorage.setItem("search-history", JSON.stringify(history));
|
|
213
|
+
}
|
|
214
|
+
const schedule = debounce(() => {
|
|
215
|
+
offset = 0;
|
|
216
|
+
void action(search)();
|
|
217
|
+
}, 160);
|
|
218
|
+
window.mytools?.onSearch?.(event => {
|
|
219
|
+
$("#query").value = event.query;
|
|
220
|
+
if (event.key) $("#query").dispatchEvent(new KeyboardEvent("keydown", {key:event.key,bubbles:true}));
|
|
221
|
+
else { generation++; schedule(); }
|
|
222
|
+
});
|
|
223
|
+
$("#query").oninput = () => {
|
|
224
|
+
generation++;
|
|
225
|
+
schedule();
|
|
226
|
+
};
|
|
227
|
+
$("#query").onkeydown = (e) => {
|
|
228
|
+
if (e.isComposing) return;
|
|
229
|
+
if (e.key === "ArrowDown") {
|
|
230
|
+
e.preventDefault();
|
|
231
|
+
select(0, true);
|
|
232
|
+
}
|
|
233
|
+
if (e.key === "Enter") {
|
|
234
|
+
e.preventDefault();
|
|
235
|
+
remember();
|
|
236
|
+
offset = 0;
|
|
237
|
+
void action(search)();
|
|
238
|
+
}
|
|
239
|
+
if (e.key === "Escape") $("#query").select();
|
|
240
|
+
};
|
|
241
|
+
$("#results").onkeydown = action(async (e) => {
|
|
242
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
243
|
+
e.preventDefault();
|
|
244
|
+
select(selected + (e.key === "ArrowDown" ? 1 : -1));
|
|
245
|
+
} else if (e.key === "Enter") {
|
|
246
|
+
e.preventDefault();
|
|
247
|
+
await call(e.ctrlKey ? "shell.reveal" : "shell.open", {
|
|
248
|
+
path: picked().path,
|
|
249
|
+
});
|
|
250
|
+
} else if (e.key === " ") {
|
|
251
|
+
e.preventDefault();
|
|
252
|
+
$("#preview-toggle").click();
|
|
253
|
+
} else if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "c") {
|
|
254
|
+
e.preventDefault();
|
|
255
|
+
await copy(picked().path);
|
|
256
|
+
} else if (e.key === "Escape") {
|
|
257
|
+
$("#query").focus();
|
|
258
|
+
$("#query").select();
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
$("#types").onclick = e => {
|
|
262
|
+
const b = e.target.closest("[data-type]");
|
|
263
|
+
if (!b) return;
|
|
264
|
+
type = b.dataset.type;
|
|
265
|
+
$$("#types button").forEach((x) => x.classList.toggle("active", x === b));
|
|
266
|
+
offset = 0;
|
|
267
|
+
void action(search)();
|
|
268
|
+
};
|
|
269
|
+
$("#sort").onchange = () => {
|
|
270
|
+
offset = 0;
|
|
271
|
+
void action(search)();
|
|
272
|
+
};
|
|
273
|
+
for (const control of $$("#advanced-filters input,#advanced-filters select")) control.onchange = () => {
|
|
274
|
+
$("#date-range").classList.toggle("hidden", $("#modified-period").value !== "custom");
|
|
275
|
+
offset = 0;
|
|
276
|
+
void action(search)();
|
|
277
|
+
};
|
|
278
|
+
$("#filter-toggle").onclick = () => {
|
|
279
|
+
const expanded = $("#advanced-filters").classList.toggle("hidden") === false;
|
|
280
|
+
$("#filter-toggle").setAttribute("aria-expanded", String(expanded));
|
|
281
|
+
};
|
|
282
|
+
$("#reset-filters").onclick = () => {
|
|
283
|
+
for (const input of $$("#advanced-filters input")) input.value = "";
|
|
284
|
+
$("#modified-period").value = "";
|
|
285
|
+
$("#date-range").classList.add("hidden");
|
|
286
|
+
offset = 0;
|
|
287
|
+
void action(search)();
|
|
288
|
+
};
|
|
289
|
+
$("#history").onchange = () => {
|
|
290
|
+
if ($("#history").value) {
|
|
291
|
+
$("#query").value = $("#history").value;
|
|
292
|
+
offset = 0;
|
|
293
|
+
void action(search)();
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
$("#previous").onclick = () => {
|
|
297
|
+
offset = Math.max(0, offset - limit);
|
|
298
|
+
void action(search)();
|
|
299
|
+
};
|
|
300
|
+
$("#next").onclick = () => {
|
|
301
|
+
offset += limit;
|
|
302
|
+
void action(search)();
|
|
303
|
+
};
|
|
304
|
+
$("#refresh").onclick = action(search);
|
|
305
|
+
$("#retry").onclick = action(search);
|
|
306
|
+
$("#start").onclick = action(async () => {
|
|
307
|
+
const apps = await call("app.list", { query: "Everything" });
|
|
308
|
+
const candidates = Array.isArray(apps) ? apps : apps.apps || [];
|
|
309
|
+
const app = candidates.find((a) =>
|
|
310
|
+
/everything/i.test(a.name || a.title || ""),
|
|
311
|
+
);
|
|
312
|
+
if (!app)
|
|
313
|
+
throw Error(
|
|
314
|
+
"没有找到已安装的 Everything。请先安装或手动启动 Everything 后重新连接。",
|
|
315
|
+
);
|
|
316
|
+
await call("app.launch", { target: app.target || app.id });
|
|
317
|
+
status("已启动 Everything,请稍后点击重新连接");
|
|
318
|
+
});
|
|
319
|
+
$("#open").onclick = action(() => call("shell.open", { path: picked().path }));
|
|
320
|
+
$("#reveal").onclick = action(() =>
|
|
321
|
+
call("shell.reveal", { path: picked().path }),
|
|
322
|
+
);
|
|
323
|
+
$("#copy").onclick = action(() => copy(picked().path));
|
|
324
|
+
async function preview() {
|
|
325
|
+
const token = ++previewGeneration;
|
|
326
|
+
try {
|
|
327
|
+
if (!entries[selected]) { $("#preview").textContent="选择文件查看预览"; return; }
|
|
328
|
+
const file = picked();
|
|
329
|
+
$("#preview").textContent = "正在读取预览…";
|
|
330
|
+
const result = await call("fs.searchpreview", { path: file.path });
|
|
331
|
+
if (token !== previewGeneration) return;
|
|
332
|
+
if (previewUrl) {
|
|
333
|
+
URL.revokeObjectURL(previewUrl);
|
|
334
|
+
previewUrl = "";
|
|
335
|
+
}
|
|
336
|
+
$("#preview").replaceChildren();
|
|
337
|
+
const heading = document.createElement("strong");
|
|
338
|
+
heading.textContent = file.name;
|
|
339
|
+
$("#preview").append(heading);
|
|
340
|
+
const metadata = document.createElement("div");
|
|
341
|
+
metadata.className = "preview-meta";
|
|
342
|
+
metadata.textContent = [file.path, file.directory ? "文件夹" : formatBytes(file.size),file.modified ? new Date(file.modified * 1000).toLocaleString() : ""].filter(Boolean).join("\n");
|
|
343
|
+
$("#preview").append(metadata);
|
|
344
|
+
if (["image","pdf","audio","video"].includes(result.kind)) {
|
|
345
|
+
previewUrl = URL.createObjectURL(
|
|
346
|
+
new Blob([new Uint8Array(result.bytes)], { type: result.mime }),
|
|
347
|
+
);
|
|
348
|
+
const media = result.kind === "image" ? new Image() : document.createElement(result.kind === "pdf" ? "iframe" : result.kind);
|
|
349
|
+
media.src = previewUrl; media.title = file.name;
|
|
350
|
+
if (result.kind === "image") media.alt = file.name;
|
|
351
|
+
if (["audio","video"].includes(result.kind)) media.controls = true;
|
|
352
|
+
$("#preview").append(media);
|
|
353
|
+
} else {
|
|
354
|
+
const pre = document.createElement("pre");
|
|
355
|
+
pre.textContent = result.text;
|
|
356
|
+
$("#preview").append(pre);
|
|
357
|
+
if (result.truncated)
|
|
358
|
+
$("#preview").append(document.createTextNode("\n仅预览前 20 KB"));
|
|
359
|
+
}
|
|
360
|
+
} catch (e) {
|
|
361
|
+
if (token === previewGeneration)
|
|
362
|
+
$("#preview").textContent = e.message || String(e);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
$("#preview-toggle").onclick = () => {
|
|
366
|
+
$("#preview").classList.toggle("hidden");
|
|
367
|
+
if (!$("#preview").classList.contains("hidden")) void preview();
|
|
368
|
+
else { previewGeneration++; if (previewUrl) { URL.revokeObjectURL(previewUrl);previewUrl=""; } }
|
|
369
|
+
};
|
|
370
|
+
try {
|
|
371
|
+
history = JSON.parse(localStorage.getItem("search-history") || "[]");
|
|
372
|
+
if (!Array.isArray(history)) history = [];
|
|
373
|
+
} catch {
|
|
374
|
+
history = [];
|
|
375
|
+
}
|
|
376
|
+
$("#history").innerHTML =
|
|
377
|
+
'<option value="">搜索历史</option>' +
|
|
378
|
+
history.map((q) => "<option>" + escapeHtml(q) + "</option>").join("");
|
|
379
|
+
ready(async (env) => {
|
|
380
|
+
if (env?.hostSearch) {
|
|
381
|
+
$("#query").style.display = "none";
|
|
382
|
+
$("#query").setAttribute("aria-hidden", "true");
|
|
383
|
+
}
|
|
384
|
+
const s = await call("fs.searchstatus");
|
|
385
|
+
if (s.running) await search();
|
|
386
|
+
else
|
|
387
|
+
showConnection(
|
|
388
|
+
"请启动 Everything 1.4 / 1.5a 并等待索引完成。此插件直接使用 Everything 索引,不扫描磁盘目录。",
|
|
389
|
+
);
|
|
390
|
+
if (!env?.hostSearch) $("#query").focus();
|
|
391
|
+
});
|
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,231 @@
|
|
|
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
|
+
.search-layout {
|
|
10
|
+
display: grid;
|
|
11
|
+
grid-template-columns: 142px minmax(0, 1fr);
|
|
12
|
+
flex: 1;
|
|
13
|
+
min-height: 0;
|
|
14
|
+
}
|
|
15
|
+
.filters {
|
|
16
|
+
border-right: 1px solid var(--border);
|
|
17
|
+
background: var(--bg);
|
|
18
|
+
padding: 14px 0;
|
|
19
|
+
overflow: auto;
|
|
20
|
+
}
|
|
21
|
+
.filters strong {
|
|
22
|
+
display: block;
|
|
23
|
+
padding: 0 14px 14px;
|
|
24
|
+
}
|
|
25
|
+
.filters button {
|
|
26
|
+
display: block;
|
|
27
|
+
text-align: left;
|
|
28
|
+
width: 100%;
|
|
29
|
+
border: 0;
|
|
30
|
+
border-radius: 0;
|
|
31
|
+
padding: 10px 18px;
|
|
32
|
+
color: var(--muted);
|
|
33
|
+
}
|
|
34
|
+
.filters button.active {
|
|
35
|
+
color: var(--accent);
|
|
36
|
+
background: var(--soft);
|
|
37
|
+
}
|
|
38
|
+
.search-main {
|
|
39
|
+
display: flex;
|
|
40
|
+
flex-direction: column;
|
|
41
|
+
min-width: 0;
|
|
42
|
+
min-height: 0;
|
|
43
|
+
}
|
|
44
|
+
.search-field {
|
|
45
|
+
display: flex;
|
|
46
|
+
gap: 8px;
|
|
47
|
+
padding: 10px;
|
|
48
|
+
border-bottom: 1px solid var(--border);
|
|
49
|
+
}
|
|
50
|
+
.search-field input {
|
|
51
|
+
flex: 1;
|
|
52
|
+
font-size: 15px;
|
|
53
|
+
}
|
|
54
|
+
.results {
|
|
55
|
+
flex: 1;
|
|
56
|
+
overflow: auto;
|
|
57
|
+
outline: none;
|
|
58
|
+
min-height: 0;
|
|
59
|
+
}
|
|
60
|
+
.result {
|
|
61
|
+
display: grid;
|
|
62
|
+
grid-template-columns: 34px minmax(0, 1fr) 110px;
|
|
63
|
+
gap: 10px;
|
|
64
|
+
align-items: center;
|
|
65
|
+
padding: 10px 14px;
|
|
66
|
+
border-bottom: 1px solid var(--border);
|
|
67
|
+
cursor: default;
|
|
68
|
+
}
|
|
69
|
+
.result.selected {
|
|
70
|
+
background: var(--soft);
|
|
71
|
+
}
|
|
72
|
+
.file-icon {
|
|
73
|
+
font-size: 25px;
|
|
74
|
+
opacity: 0.7;
|
|
75
|
+
}
|
|
76
|
+
.file-name {
|
|
77
|
+
font-size: 14px;
|
|
78
|
+
overflow: hidden;
|
|
79
|
+
text-overflow: ellipsis;
|
|
80
|
+
white-space: nowrap;
|
|
81
|
+
}
|
|
82
|
+
.file-path {
|
|
83
|
+
font-size: 12px;
|
|
84
|
+
color: var(--muted);
|
|
85
|
+
overflow: hidden;
|
|
86
|
+
text-overflow: ellipsis;
|
|
87
|
+
white-space: nowrap;
|
|
88
|
+
}
|
|
89
|
+
.meta {
|
|
90
|
+
font-size: 11px;
|
|
91
|
+
color: var(--muted);
|
|
92
|
+
text-align: right;
|
|
93
|
+
}
|
|
94
|
+
.body-row {
|
|
95
|
+
display: flex;
|
|
96
|
+
flex: 1;
|
|
97
|
+
min-height: 0;
|
|
98
|
+
}
|
|
99
|
+
.preview {
|
|
100
|
+
flex: 0 0 35%;
|
|
101
|
+
padding: 14px;
|
|
102
|
+
border-left: 1px solid var(--border);
|
|
103
|
+
overflow: auto;
|
|
104
|
+
min-width: 0;
|
|
105
|
+
}
|
|
106
|
+
.preview img {
|
|
107
|
+
max-width: 100%;
|
|
108
|
+
}
|
|
109
|
+
#connection {
|
|
110
|
+
padding: 16px;
|
|
111
|
+
margin: 20px;
|
|
112
|
+
border: 1px solid var(--border);
|
|
113
|
+
border-radius: 6px;
|
|
114
|
+
}
|
|
115
|
+
.bottom {
|
|
116
|
+
padding: 6px 10px;
|
|
117
|
+
gap: 6px;
|
|
118
|
+
font-size: 12px;
|
|
119
|
+
}
|
|
120
|
+
.bottom button,
|
|
121
|
+
.bottom select {
|
|
122
|
+
padding: 4px 8px;
|
|
123
|
+
}
|
|
124
|
+
mark {
|
|
125
|
+
background: #ffe87d;
|
|
126
|
+
color: #333;
|
|
127
|
+
}
|
|
128
|
+
.hintline {
|
|
129
|
+
padding: 3px 12px;
|
|
130
|
+
color: var(--muted);
|
|
131
|
+
font-size: 11px;
|
|
132
|
+
}
|
|
133
|
+
#history {
|
|
134
|
+
max-width: 150px;
|
|
135
|
+
}
|
|
136
|
+
.filter-settings { padding:12px; font-size:12px; }.filter-settings input { width:100%; box-sizing:border-box; margin:5px 0; }.filter-settings button { font-size:11px; }.filter-settings summary { cursor:pointer; color:var(--muted); }
|
|
137
|
+
.preview iframe { width:100%; min-height:320px; border:0; }.preview video,.preview audio { width:100%; }
|
|
138
|
+
.advanced-filters { display:flex; flex-wrap:wrap; gap:10px; padding:10px; border-bottom:1px solid var(--border); background:var(--bg); }
|
|
139
|
+
.advanced-filters label { display:flex; align-items:center; gap:6px; font-size:12px; }
|
|
140
|
+
.advanced-filters input { width:140px; }
|
|
141
|
+
.preview-meta { white-space:pre-wrap; overflow-wrap:anywhere; color:var(--muted); font-size:11px; margin:10px 0 16px; }
|
|
142
|
+
.bottom { flex-wrap:wrap; }
|
|
143
|
+
@media (max-width: 650px) {
|
|
144
|
+
.search-layout {
|
|
145
|
+
grid-template-columns: 100px 1fr;
|
|
146
|
+
}
|
|
147
|
+
.filters button {
|
|
148
|
+
padding: 9px 12px;
|
|
149
|
+
}
|
|
150
|
+
.result {
|
|
151
|
+
grid-template-columns: 26px minmax(0, 1fr);
|
|
152
|
+
}
|
|
153
|
+
.meta {
|
|
154
|
+
display: none;
|
|
155
|
+
}
|
|
156
|
+
.preview {
|
|
157
|
+
flex-basis: 45%;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
</style>
|
|
161
|
+
</head>
|
|
162
|
+
<body>
|
|
163
|
+
<main>
|
|
164
|
+
<div class="search-layout">
|
|
165
|
+
<aside class="filters">
|
|
166
|
+
<strong>过滤项</strong>
|
|
167
|
+
<nav id="types"></nav>
|
|
168
|
+
<details class="filter-settings"><summary>管理过滤项</summary><input id="filter-name" placeholder="名称" aria-label="过滤项名称" /><input id="filter-query" placeholder="例如 ext:rs;ts" aria-label="过滤项条件" /><button id="save-filter">+ 新增过滤项</button><div id="custom-filter-list"></div></details>
|
|
169
|
+
</aside>
|
|
170
|
+
<section class="search-main">
|
|
171
|
+
<div class="search-field">
|
|
172
|
+
<input
|
|
173
|
+
id="query"
|
|
174
|
+
placeholder="搜索文件、文件夹…(支持 Everything 语法)"
|
|
175
|
+
aria-label="搜索文件"
|
|
176
|
+
autocomplete="off"
|
|
177
|
+
/><button id="filter-toggle" aria-expanded="false">筛选</button><button id="refresh" title="刷新">↻</button
|
|
178
|
+
><button id="theme" title="切换主题">◐</button>
|
|
179
|
+
</div>
|
|
180
|
+
<div id="advanced-filters" class="advanced-filters hidden">
|
|
181
|
+
<label>扩展名 <input id="extension-filter" placeholder="txt;md;pdf" /></label>
|
|
182
|
+
<label>排除 <input id="exclude-filter" placeholder="名称或路径,分号分隔" /></label>
|
|
183
|
+
<label>修改时间 <select id="modified-period"><option value="">不限</option><option value="today">今天</option><option value="yesterday">昨天</option><option value="thisweek">本周</option><option value="thismonth">本月</option><option value="custom">自定义日期</option></select></label>
|
|
184
|
+
<span id="date-range" class="hidden"><input id="modified-from" type="date" aria-label="开始日期" /> 至 <input id="modified-to" type="date" aria-label="结束日期" /></span>
|
|
185
|
+
<button id="reset-filters">重置筛选</button>
|
|
186
|
+
</div>
|
|
187
|
+
<div class="hintline">
|
|
188
|
+
↑↓ 选择 · Enter 打开 · Ctrl+Enter 定位 · 空格预览
|
|
189
|
+
</div>
|
|
190
|
+
<div id="connection" class="hidden">
|
|
191
|
+
<strong>未连接到 Everything</strong>
|
|
192
|
+
<p id="connection-message">请启动 Everything 并等待索引就绪。</p>
|
|
193
|
+
<div class="row">
|
|
194
|
+
<button id="start" class="primary">启动已安装的 Everything</button
|
|
195
|
+
><button id="retry">重新连接</button>
|
|
196
|
+
</div>
|
|
197
|
+
</div>
|
|
198
|
+
<div class="body-row">
|
|
199
|
+
<div
|
|
200
|
+
id="results"
|
|
201
|
+
class="results"
|
|
202
|
+
tabindex="0"
|
|
203
|
+
role="listbox"
|
|
204
|
+
aria-label="搜索结果"
|
|
205
|
+
></div>
|
|
206
|
+
<section id="preview" class="preview hidden"></section>
|
|
207
|
+
</div>
|
|
208
|
+
<div class="toolbar bottom">
|
|
209
|
+
<select id="sort" aria-label="排序">
|
|
210
|
+
<option value="1">按名称升序</option>
|
|
211
|
+
<option value="2">按名称降序</option>
|
|
212
|
+
<option value="13">按修改时间升序</option>
|
|
213
|
+
<option value="14">按修改时间降序</option>
|
|
214
|
+
<option value="5">按大小升序</option>
|
|
215
|
+
<option value="6">按大小降序</option>
|
|
216
|
+
<option value="3">按路径升序</option><option value="4">按路径降序</option><option value="7">按扩展名升序</option><option value="8">按扩展名降序</option></select
|
|
217
|
+
><select id="history" aria-label="搜索历史">
|
|
218
|
+
<option value="">搜索历史</option></select
|
|
219
|
+
><span class="spacer"></span><button id="previous">‹</button
|
|
220
|
+
><span id="count">0 / 0</span><button id="next">›</button
|
|
221
|
+
><button id="preview-toggle">预览面板</button
|
|
222
|
+
><button id="open">打开</button><button id="reveal">定位</button
|
|
223
|
+
><button id="copy">复制路径</button>
|
|
224
|
+
</div>
|
|
225
|
+
</section>
|
|
226
|
+
</div>
|
|
227
|
+
<footer id="status" class="status" role="status"></footer>
|
|
228
|
+
</main>
|
|
229
|
+
<script type="module" src="app.mjs"></script>
|
|
230
|
+
</body>
|
|
231
|
+
</html>
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@mytools-all/mytools-search",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@mytools-all/mytools-search",
|
|
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-search.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/mytools-all/mytools-search#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/mytools-all/mytools-search/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
|
+
"searchQuery.mjs",
|
|
34
|
+
"utils.mjs",
|
|
35
|
+
"*.js",
|
|
36
|
+
"*.LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"scripts": {
|
|
39
|
+
"test": "node --test"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/plugin.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mytools-search",
|
|
3
|
+
"description": "本地搜索",
|
|
4
|
+
"apiVersion": 1,
|
|
5
|
+
"main": "index.html",
|
|
6
|
+
"pluginName": "本地搜索",
|
|
7
|
+
"version": "0.1.0",
|
|
8
|
+
"capabilities": [
|
|
9
|
+
"fs.searchstatus",
|
|
10
|
+
"fs.globalsearch",
|
|
11
|
+
"fs.searchpreview",
|
|
12
|
+
"shell.open",
|
|
13
|
+
"shell.reveal",
|
|
14
|
+
"clipboard.write",
|
|
15
|
+
"app.list",
|
|
16
|
+
"app.launch"
|
|
17
|
+
],
|
|
18
|
+
"features": [
|
|
19
|
+
{
|
|
20
|
+
"code": "search",
|
|
21
|
+
"explain": "打开本地搜索",
|
|
22
|
+
"cmds": [
|
|
23
|
+
"本地搜索",
|
|
24
|
+
"文件搜索",
|
|
25
|
+
"everything"
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
"hostSearch": "搜索文件、文件夹…"
|
|
30
|
+
}
|
package/searchQuery.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function buildSearchQuery({ query = "", category = "", extension = "", excluded = "", from = "", to = "", period = "" }) {
|
|
2
|
+
if ((!period || period === "custom") && from && to && from > to) throw Error("开始日期不能晚于结束日期");
|
|
3
|
+
const parts = [category, query.trim() ? `<${query.trim()}>` : ""];
|
|
4
|
+
const extensions = extension.split(/[;,,\s]+/).map(x => x.replace(/^\./, "")).filter(Boolean);
|
|
5
|
+
if (extensions.some(x => !/^[\w-]+$/.test(x))) throw Error("扩展名仅填写字母、数字,例如 txt;md");
|
|
6
|
+
if (extensions.length) parts.push("ext:" + extensions.join(";"));
|
|
7
|
+
for (const word of excluded.split(/[;;\n]+/).map(x => x.trim()).filter(Boolean)) {
|
|
8
|
+
if (word.includes('"')) throw Error("排除内容不能包含双引号");
|
|
9
|
+
parts.push('!"' + word + '"');
|
|
10
|
+
}
|
|
11
|
+
if (period && period !== "custom") parts.push("dm:" + period);
|
|
12
|
+
else {
|
|
13
|
+
if (from) parts.push("dm:>=" + from);
|
|
14
|
+
if (to) parts.push("dm:<" + nextDay(to));
|
|
15
|
+
}
|
|
16
|
+
return parts.filter(Boolean).join(" ");
|
|
17
|
+
}
|
|
18
|
+
function nextDay(value) {
|
|
19
|
+
const date = new Date(value + "T00:00:00Z");
|
|
20
|
+
date.setUTCDate(date.getUTCDate() + 1);
|
|
21
|
+
return date.toISOString().slice(0, 10);
|
|
22
|
+
}
|
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
|
+
}
|