@mytools-all/mytools-search 0.0.0-stage → 0.1.1
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 +26 -2
- package/app.mjs +476 -0
- package/common.mjs +69 -0
- package/index.html +246 -0
- package/package.json +39 -3
- package/plugin.json +31 -0
- package/resultPresentation.mjs +22 -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,27 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 本地搜索
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
本地搜索
|
|
4
|
+
|
|
5
|
+
版本:0.1.1;适用于 MyTools,不能作为独立网页调用宿主能力。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
在 MyTools 插件管理的 npm 安装入口输入 `@mytools-all/mytools-search@0.1.1`。也可以下载本仓库,在开发者页面选择“安装本地目录”。
|
|
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、fs.searchicons、shell.open、shell.reveal、clipboard.write、app.list、app.launch。目录和危险操作仍需用户授权。
|
|
18
|
+
|
|
19
|
+
## 许可
|
|
20
|
+
|
|
21
|
+
项目代码使用 MIT;随附的第三方代码遵循各自随附的许可证。
|
|
22
|
+
|
|
23
|
+
## 0.1.1 更新
|
|
24
|
+
|
|
25
|
+
已同步空搜索不显示结果的源码改动:打开页面、刷新或调整筛选时,搜索词为空均不发起文件搜索;清空搜索词立即清空结果,并忽略尚未返回的旧搜索或预览。保留结果展示改进。
|
|
26
|
+
|
|
27
|
+
本地安装使用独立插件目录;重新构建主程序不会自动同步本仓库。源码目录与已安装目录相互独立,更新后请关闭并重新打开插件。
|
package/app.mjs
ADDED
|
@@ -0,0 +1,476 @@
|
|
|
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
|
+
import { resultName, resultParent, placeholderIcon } from "./resultPresentation.mjs";
|
|
15
|
+
const filters = [
|
|
16
|
+
["all", "全部", ""],
|
|
17
|
+
["folder", "文件夹", "folder:"],
|
|
18
|
+
["doc", "文档", "ext:doc;docx;xls;xlsx;ppt;pptx;txt;md;csv"],
|
|
19
|
+
["pdf", "PDF", "ext:pdf"],
|
|
20
|
+
["image", "图片", "ext:png;jpg;jpeg;gif;bmp;webp;svg"],
|
|
21
|
+
["video", "视频", "ext:mp4;mkv;mov;avi;webm"],
|
|
22
|
+
["audio", "音频", "ext:mp3;wav;flac;aac;ogg"],
|
|
23
|
+
["archive", "压缩文件", "ext:zip;rar;7z;gz;tar"],
|
|
24
|
+
["exe", "程序", "ext:exe;msi;bat;cmd"],
|
|
25
|
+
];
|
|
26
|
+
let entries = [],
|
|
27
|
+
selected = -1,
|
|
28
|
+
total = 0,
|
|
29
|
+
offset = 0,
|
|
30
|
+
type = "all",
|
|
31
|
+
busy = false,
|
|
32
|
+
pending = false,
|
|
33
|
+
generation = 0,
|
|
34
|
+
previewUrl = "",
|
|
35
|
+
previewGeneration = 0,
|
|
36
|
+
history = [];
|
|
37
|
+
const limit = 100;
|
|
38
|
+
const iconCache = new Map();
|
|
39
|
+
let iconObserver;
|
|
40
|
+
let iconGeneration = 0;
|
|
41
|
+
try {
|
|
42
|
+
const saved = JSON.parse(localStorage.getItem("search-custom-filters") || "[]");
|
|
43
|
+
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));
|
|
44
|
+
} catch {}
|
|
45
|
+
function renderFilters() {
|
|
46
|
+
$("#types").innerHTML = filters
|
|
47
|
+
.map(
|
|
48
|
+
([id, label]) =>
|
|
49
|
+
'<button data-type="' +
|
|
50
|
+
escapeHtml(id) +
|
|
51
|
+
'" class="' +
|
|
52
|
+
(id === type ? "active" : "") +
|
|
53
|
+
'">' +
|
|
54
|
+
escapeHtml(label) +
|
|
55
|
+
"</button>",
|
|
56
|
+
)
|
|
57
|
+
.join("");
|
|
58
|
+
const custom = filters.filter(f=>f[0].startsWith("custom-"));
|
|
59
|
+
$("#custom-filter-list").innerHTML = custom.length ? custom.map(([id,label,query])=>'<div class="custom-filter"><div class="custom-filter__text"><strong>'+escapeHtml(label)+'</strong><code title="'+escapeHtml(query)+'">'+escapeHtml(query)+'</code></div><button class="filter-delete" data-remove-filter="'+escapeHtml(id)+'" aria-label="删除过滤项:'+escapeHtml(label)+'" title="删除此过滤项"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 7h16M9 7V4h6v3M6 7l1 13h10l1-13M10 11v5M14 11v5"/></svg><span>删除</span></button></div>').join("") : '<p class="filter-empty">暂无自定义过滤项</p>';
|
|
60
|
+
}
|
|
61
|
+
renderFilters();
|
|
62
|
+
function saveFilters() {
|
|
63
|
+
localStorage.setItem("search-custom-filters", JSON.stringify(filters.filter(f=>f[0].startsWith("custom-"))));
|
|
64
|
+
renderFilters();
|
|
65
|
+
}
|
|
66
|
+
$("#save-filter").onclick = action(() => {
|
|
67
|
+
const label=$("#filter-name").value.trim(), query=$("#filter-query").value.trim();
|
|
68
|
+
if (!label || !query) throw Error("请填写名称和 Everything 条件");
|
|
69
|
+
if (filters.length>=39) throw Error("最多保存30个自定义过滤项");
|
|
70
|
+
filters.push(["custom-"+crypto.randomUUID(),label.slice(0,30),query.slice(0,1000)]);
|
|
71
|
+
saveFilters();$("#filter-name").value="";$("#filter-query").value="";
|
|
72
|
+
});
|
|
73
|
+
$("#custom-filter-list").onclick = e => {
|
|
74
|
+
const id=e.target.closest("[data-remove-filter]")?.dataset.removeFilter;
|
|
75
|
+
const at=filters.findIndex(f=>f[0]===id);if(at<0)return;
|
|
76
|
+
const label=filters[at][1];
|
|
77
|
+
filters.splice(at,1);if(type===id){type="all";offset=0;void action(search)();}saveFilters();
|
|
78
|
+
status("已删除过滤项:"+label);$("#filter-name").focus();
|
|
79
|
+
};
|
|
80
|
+
function picked() {
|
|
81
|
+
if (!entries[selected]) throw Error("请先选择结果");
|
|
82
|
+
return entries[selected];
|
|
83
|
+
}
|
|
84
|
+
function clearSearch() {
|
|
85
|
+
generation++;
|
|
86
|
+
pending = false;
|
|
87
|
+
entries = [];
|
|
88
|
+
selected = -1;
|
|
89
|
+
total = 0;
|
|
90
|
+
offset = 0;
|
|
91
|
+
iconGeneration++;
|
|
92
|
+
iconObserver?.disconnect();
|
|
93
|
+
previewGeneration++;
|
|
94
|
+
for (const media of $("#preview").querySelectorAll("audio,video")) {
|
|
95
|
+
media.pause();
|
|
96
|
+
media.removeAttribute("src");
|
|
97
|
+
media.load();
|
|
98
|
+
}
|
|
99
|
+
if (previewUrl) { URL.revokeObjectURL(previewUrl); previewUrl = ""; }
|
|
100
|
+
$("#results").replaceChildren();
|
|
101
|
+
$("#preview").replaceChildren();
|
|
102
|
+
$("#preview").classList.add("hidden");
|
|
103
|
+
$("#connection").classList.add("hidden");
|
|
104
|
+
$("#count").textContent = "0 / 0";
|
|
105
|
+
$("#previous").disabled = true;
|
|
106
|
+
$("#next").disabled = true;
|
|
107
|
+
for (const id of ["open","reveal","copy","preview-toggle"]) $("#"+id).disabled = true;
|
|
108
|
+
$("#refresh").disabled = false;
|
|
109
|
+
status("输入关键词开始搜索");
|
|
110
|
+
}
|
|
111
|
+
function select(index, focus = false) {
|
|
112
|
+
if (!entries.length) { selected = -1; return; }
|
|
113
|
+
selected = Math.max(0, Math.min(entries.length - 1, index));
|
|
114
|
+
$$(".result").forEach((el, i) => {
|
|
115
|
+
el.classList.toggle("selected", i === selected);
|
|
116
|
+
el.setAttribute("aria-selected", String(i === selected));
|
|
117
|
+
});
|
|
118
|
+
const row = $$(".result")[selected];
|
|
119
|
+
row?.scrollIntoView({ block: "nearest" });
|
|
120
|
+
if (focus) $("#results").focus();
|
|
121
|
+
if (!$("#preview").classList.contains("hidden")) void preview();
|
|
122
|
+
}
|
|
123
|
+
function render() {
|
|
124
|
+
const q = $("#query").value.trim();
|
|
125
|
+
const words = q
|
|
126
|
+
.split(/\s+/)
|
|
127
|
+
.filter((w) => w && !w.includes(":") && !/[|<>"*?]/.test(w));
|
|
128
|
+
const highlight = (name) => {
|
|
129
|
+
if (!words.length) return escapeHtml(name);
|
|
130
|
+
const regex = new RegExp(
|
|
131
|
+
words
|
|
132
|
+
.map((word) => word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
|
|
133
|
+
.join("|"),
|
|
134
|
+
"gi",
|
|
135
|
+
);
|
|
136
|
+
let output = "",
|
|
137
|
+
at = 0;
|
|
138
|
+
for (const match of name.matchAll(regex)) {
|
|
139
|
+
output +=
|
|
140
|
+
escapeHtml(name.slice(at, match.index)) +
|
|
141
|
+
"<mark>" +
|
|
142
|
+
escapeHtml(match[0]) +
|
|
143
|
+
"</mark>";
|
|
144
|
+
at = match.index + match[0].length;
|
|
145
|
+
}
|
|
146
|
+
return output + escapeHtml(name.slice(at));
|
|
147
|
+
};
|
|
148
|
+
$("#results").innerHTML = entries.length
|
|
149
|
+
? entries
|
|
150
|
+
.map(
|
|
151
|
+
(e, i) =>
|
|
152
|
+
'<div role="option" class="result" data-index="' +
|
|
153
|
+
i +
|
|
154
|
+
'"><span class="file-icon">' +
|
|
155
|
+
'<img alt="" src="'+(iconCache.get(e.path+"\0"+e.modified) || placeholderIcon(e.directory))+'" />' +
|
|
156
|
+
'</span><div><div class="file-name">' +
|
|
157
|
+
highlight(resultName(e)) +
|
|
158
|
+
'</div><div class="file-path" title="' +
|
|
159
|
+
escapeHtml(e.path) +
|
|
160
|
+
'">' +
|
|
161
|
+
escapeHtml(resultParent(e)) +
|
|
162
|
+
'</div></div><div class="meta">' +
|
|
163
|
+
(e.directory ? "文件夹" : formatBytes(e.size)) +
|
|
164
|
+
"<br>" +
|
|
165
|
+
(e.modified ? new Date(e.modified * 1000).toLocaleString() : "") +
|
|
166
|
+
"</div></div>",
|
|
167
|
+
)
|
|
168
|
+
.join("")
|
|
169
|
+
: '<div class="empty">没有匹配的文件</div>';
|
|
170
|
+
$$(".result").forEach((el) => {
|
|
171
|
+
el.onclick = () => select(Number(el.dataset.index), true);
|
|
172
|
+
el.ondblclick = action(() =>
|
|
173
|
+
call("shell.open", { path: entries[Number(el.dataset.index)].path }),
|
|
174
|
+
);
|
|
175
|
+
el.oncontextmenu = (e) => {
|
|
176
|
+
e.preventDefault();
|
|
177
|
+
select(Number(el.dataset.index), true);
|
|
178
|
+
void action(() => call("shell.reveal", { path: picked().path }))();
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
$("#count").textContent =
|
|
182
|
+
(total ? offset + 1 : 0) + "–" + (offset + entries.length) + " / " + total;
|
|
183
|
+
$("#previous").disabled = offset === 0;
|
|
184
|
+
$("#next").disabled = offset + entries.length >= total;
|
|
185
|
+
for (const id of ["open","reveal","copy","preview-toggle"]) $("#"+id).disabled = !entries.length;
|
|
186
|
+
select(0);
|
|
187
|
+
hydrateIcons();
|
|
188
|
+
}
|
|
189
|
+
function hydrateIcons() {
|
|
190
|
+
const token = ++iconGeneration;
|
|
191
|
+
iconObserver?.disconnect();
|
|
192
|
+
const queue = new Map();
|
|
193
|
+
let running = false;
|
|
194
|
+
const applyIcons = () => {
|
|
195
|
+
$$(".result").forEach(row => {
|
|
196
|
+
const entry = entries[Number(row.dataset.index)];
|
|
197
|
+
const image = row.querySelector(".file-icon img");
|
|
198
|
+
if (!entry || !image) return;
|
|
199
|
+
image.src = iconCache.get(entry.path+"\0"+entry.modified) || placeholderIcon(entry.directory);
|
|
200
|
+
image.onerror = () => { image.onerror = null; image.src = placeholderIcon(entry.directory); };
|
|
201
|
+
});
|
|
202
|
+
};
|
|
203
|
+
const load = async () => {
|
|
204
|
+
if (running) return;
|
|
205
|
+
running = true;
|
|
206
|
+
try {
|
|
207
|
+
while (queue.size && token === iconGeneration) {
|
|
208
|
+
const batch = [...queue.values()].slice(0,8);
|
|
209
|
+
batch.forEach(entry => queue.delete(entry.path));
|
|
210
|
+
let icons;
|
|
211
|
+
try { icons = await call("fs.searchicons", {paths:batch.map(entry=>entry.path)}); }
|
|
212
|
+
catch { queue.clear(); return; }
|
|
213
|
+
if (!Array.isArray(icons)) { queue.clear(); return; }
|
|
214
|
+
if (token !== iconGeneration) return;
|
|
215
|
+
for (const entry of batch) {
|
|
216
|
+
const icon = icons.find(item=>item.path===entry.path)?.icon;
|
|
217
|
+
iconCache.set(entry.path+"\0"+entry.modified, typeof icon === "string" && icon.startsWith("data:image/png;base64,") ? icon : null);
|
|
218
|
+
}
|
|
219
|
+
while (iconCache.size > 256) iconCache.delete(iconCache.keys().next().value);
|
|
220
|
+
applyIcons();
|
|
221
|
+
}
|
|
222
|
+
} finally { running = false; }
|
|
223
|
+
};
|
|
224
|
+
const enqueue = row => {
|
|
225
|
+
const entry = entries[Number(row.dataset.index)];
|
|
226
|
+
if (entry && !iconCache.has(entry.path+"\0"+entry.modified)) queue.set(entry.path,entry);
|
|
227
|
+
};
|
|
228
|
+
if (typeof IntersectionObserver === "undefined") {
|
|
229
|
+
$$(".result").slice(0,16).forEach(enqueue);
|
|
230
|
+
void load();
|
|
231
|
+
} else {
|
|
232
|
+
iconObserver = new IntersectionObserver(changes=>{
|
|
233
|
+
for (const change of changes) if(change.isIntersecting) {enqueue(change.target);iconObserver.unobserve(change.target);}
|
|
234
|
+
void load();
|
|
235
|
+
},{root:$("#results"),rootMargin:"48px"});
|
|
236
|
+
$$(".result").forEach(row=>iconObserver.observe(row));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function showConnection(message, title = "未连接到 Everything") {
|
|
240
|
+
$("#connection strong").textContent = title;
|
|
241
|
+
$("#connection").classList.remove("hidden");
|
|
242
|
+
$("#connection-message").textContent = message;
|
|
243
|
+
}
|
|
244
|
+
async function search() {
|
|
245
|
+
const q = $("#query").value.trim();
|
|
246
|
+
if (!q) { clearSearch(); return; }
|
|
247
|
+
if (busy) {
|
|
248
|
+
pending = true;
|
|
249
|
+
generation++;
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
busy = true;
|
|
253
|
+
const token = ++generation;
|
|
254
|
+
$("#refresh").disabled = true;
|
|
255
|
+
status("正在查询 Everything…");
|
|
256
|
+
try {
|
|
257
|
+
const filter = filters.find((f) => f[0] === type)[2];
|
|
258
|
+
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});
|
|
259
|
+
const result = await call("fs.globalsearch", {
|
|
260
|
+
query,
|
|
261
|
+
offset,
|
|
262
|
+
limit,
|
|
263
|
+
sort: Number($("#sort").value),
|
|
264
|
+
});
|
|
265
|
+
if (token !== generation) return;
|
|
266
|
+
entries = result.entries;
|
|
267
|
+
total = result.total;
|
|
268
|
+
$("#connection").classList.add("hidden");
|
|
269
|
+
render();
|
|
270
|
+
status("Everything · " + total.toLocaleString() + " 个结果");
|
|
271
|
+
} catch (e) {
|
|
272
|
+
if (token === generation) {
|
|
273
|
+
entries = [];
|
|
274
|
+
total = 0;
|
|
275
|
+
render();
|
|
276
|
+
showConnection(
|
|
277
|
+
e.message || String(e),
|
|
278
|
+
/授权|拒绝|denied|permission/i.test(e.message || "")
|
|
279
|
+
? "搜索权限未获允许"
|
|
280
|
+
: "Everything 查询失败",
|
|
281
|
+
);
|
|
282
|
+
status(e.message || String(e), true);
|
|
283
|
+
}
|
|
284
|
+
} finally {
|
|
285
|
+
busy = false;
|
|
286
|
+
$("#refresh").disabled = false;
|
|
287
|
+
if (pending) {
|
|
288
|
+
pending = false;
|
|
289
|
+
void action(search)();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
function remember() {
|
|
294
|
+
const q = $("#query").value.trim();
|
|
295
|
+
if (!q) return;
|
|
296
|
+
history = [q, ...history.filter((x) => x !== q)].slice(0, 20);
|
|
297
|
+
$("#history").innerHTML =
|
|
298
|
+
'<option value="">搜索历史</option>' +
|
|
299
|
+
history.map((q) => "<option>" + escapeHtml(q) + "</option>").join("");
|
|
300
|
+
localStorage.setItem("search-history", JSON.stringify(history));
|
|
301
|
+
}
|
|
302
|
+
const schedule = debounce(() => {
|
|
303
|
+
offset = 0;
|
|
304
|
+
void action(search)();
|
|
305
|
+
}, 160);
|
|
306
|
+
window.mytools?.onSearch?.(event => {
|
|
307
|
+
$("#query").value = event.query;
|
|
308
|
+
if (!$("#query").value.trim()) { clearSearch(); return; }
|
|
309
|
+
if (event.key) $("#query").dispatchEvent(new KeyboardEvent("keydown", {key:event.key,bubbles:true}));
|
|
310
|
+
else { generation++; schedule(); }
|
|
311
|
+
});
|
|
312
|
+
$("#query").oninput = () => {
|
|
313
|
+
if (!$("#query").value.trim()) { clearSearch(); return; }
|
|
314
|
+
generation++;
|
|
315
|
+
schedule();
|
|
316
|
+
};
|
|
317
|
+
$("#query").onkeydown = (e) => {
|
|
318
|
+
if (e.isComposing) return;
|
|
319
|
+
if (e.key === "ArrowDown") {
|
|
320
|
+
e.preventDefault();
|
|
321
|
+
select(0, true);
|
|
322
|
+
}
|
|
323
|
+
if (e.key === "Enter") {
|
|
324
|
+
e.preventDefault();
|
|
325
|
+
remember();
|
|
326
|
+
offset = 0;
|
|
327
|
+
void action(search)();
|
|
328
|
+
}
|
|
329
|
+
if (e.key === "Escape") $("#query").select();
|
|
330
|
+
};
|
|
331
|
+
$("#results").onkeydown = action(async (e) => {
|
|
332
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
333
|
+
e.preventDefault();
|
|
334
|
+
select(selected + (e.key === "ArrowDown" ? 1 : -1));
|
|
335
|
+
} else if (e.key === "Enter") {
|
|
336
|
+
e.preventDefault();
|
|
337
|
+
await call(e.ctrlKey ? "shell.reveal" : "shell.open", {
|
|
338
|
+
path: picked().path,
|
|
339
|
+
});
|
|
340
|
+
} else if (e.key === " ") {
|
|
341
|
+
e.preventDefault();
|
|
342
|
+
$("#preview-toggle").click();
|
|
343
|
+
} else if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "c") {
|
|
344
|
+
e.preventDefault();
|
|
345
|
+
await copy(picked().path);
|
|
346
|
+
} else if (e.key === "Escape") {
|
|
347
|
+
$("#query").focus();
|
|
348
|
+
$("#query").select();
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
$("#types").onclick = e => {
|
|
352
|
+
const b = e.target.closest("[data-type]");
|
|
353
|
+
if (!b) return;
|
|
354
|
+
type = b.dataset.type;
|
|
355
|
+
$$("#types button").forEach((x) => x.classList.toggle("active", x === b));
|
|
356
|
+
offset = 0;
|
|
357
|
+
void action(search)();
|
|
358
|
+
};
|
|
359
|
+
$("#sort").onchange = () => {
|
|
360
|
+
offset = 0;
|
|
361
|
+
void action(search)();
|
|
362
|
+
};
|
|
363
|
+
for (const control of $$("#advanced-filters input,#advanced-filters select")) control.onchange = () => {
|
|
364
|
+
$("#date-range").classList.toggle("hidden", $("#modified-period").value !== "custom");
|
|
365
|
+
offset = 0;
|
|
366
|
+
void action(search)();
|
|
367
|
+
};
|
|
368
|
+
$("#filter-toggle").onclick = () => {
|
|
369
|
+
const expanded = $("#advanced-filters").classList.toggle("hidden") === false;
|
|
370
|
+
$("#filter-toggle").setAttribute("aria-expanded", String(expanded));
|
|
371
|
+
};
|
|
372
|
+
$("#reset-filters").onclick = () => {
|
|
373
|
+
for (const input of $$("#advanced-filters input")) input.value = "";
|
|
374
|
+
$("#modified-period").value = "";
|
|
375
|
+
$("#date-range").classList.add("hidden");
|
|
376
|
+
offset = 0;
|
|
377
|
+
void action(search)();
|
|
378
|
+
};
|
|
379
|
+
$("#history").onchange = () => {
|
|
380
|
+
if ($("#history").value) {
|
|
381
|
+
$("#query").value = $("#history").value;
|
|
382
|
+
offset = 0;
|
|
383
|
+
void action(search)();
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
$("#previous").onclick = () => {
|
|
387
|
+
offset = Math.max(0, offset - limit);
|
|
388
|
+
void action(search)();
|
|
389
|
+
};
|
|
390
|
+
$("#next").onclick = () => {
|
|
391
|
+
offset += limit;
|
|
392
|
+
void action(search)();
|
|
393
|
+
};
|
|
394
|
+
$("#refresh").onclick = action(search);
|
|
395
|
+
$("#retry").onclick = action(search);
|
|
396
|
+
$("#start").onclick = action(async () => {
|
|
397
|
+
const apps = await call("app.list", { query: "Everything" });
|
|
398
|
+
const candidates = Array.isArray(apps) ? apps : apps.apps || [];
|
|
399
|
+
const app = candidates.find((a) =>
|
|
400
|
+
/everything/i.test(a.name || a.title || ""),
|
|
401
|
+
);
|
|
402
|
+
if (!app)
|
|
403
|
+
throw Error(
|
|
404
|
+
"没有找到已安装的 Everything。请先安装或手动启动 Everything 后重新连接。",
|
|
405
|
+
);
|
|
406
|
+
await call("app.launch", { target: app.target || app.id });
|
|
407
|
+
status("已启动 Everything,请稍后点击重新连接");
|
|
408
|
+
});
|
|
409
|
+
$("#open").onclick = action(() => call("shell.open", { path: picked().path }));
|
|
410
|
+
$("#reveal").onclick = action(() =>
|
|
411
|
+
call("shell.reveal", { path: picked().path }),
|
|
412
|
+
);
|
|
413
|
+
$("#copy").onclick = action(() => copy(picked().path));
|
|
414
|
+
async function preview() {
|
|
415
|
+
const token = ++previewGeneration;
|
|
416
|
+
try {
|
|
417
|
+
if (!entries[selected]) { $("#preview").textContent="选择文件查看预览"; return; }
|
|
418
|
+
const file = picked();
|
|
419
|
+
$("#preview").textContent = "正在读取预览…";
|
|
420
|
+
const result = await call("fs.searchpreview", { path: file.path });
|
|
421
|
+
if (token !== previewGeneration) return;
|
|
422
|
+
if (previewUrl) {
|
|
423
|
+
URL.revokeObjectURL(previewUrl);
|
|
424
|
+
previewUrl = "";
|
|
425
|
+
}
|
|
426
|
+
$("#preview").replaceChildren();
|
|
427
|
+
const heading = document.createElement("strong");
|
|
428
|
+
heading.textContent = resultName(file);
|
|
429
|
+
$("#preview").append(heading);
|
|
430
|
+
const metadata = document.createElement("div");
|
|
431
|
+
metadata.className = "preview-meta";
|
|
432
|
+
metadata.textContent = [file.path, file.directory ? "文件夹" : formatBytes(file.size),file.modified ? new Date(file.modified * 1000).toLocaleString() : ""].filter(Boolean).join("\n");
|
|
433
|
+
$("#preview").append(metadata);
|
|
434
|
+
if (["image","pdf","audio","video"].includes(result.kind)) {
|
|
435
|
+
previewUrl = URL.createObjectURL(
|
|
436
|
+
new Blob([new Uint8Array(result.bytes)], { type: result.mime }),
|
|
437
|
+
);
|
|
438
|
+
const media = result.kind === "image" ? new Image() : document.createElement(result.kind === "pdf" ? "iframe" : result.kind);
|
|
439
|
+
media.src = previewUrl; media.title = file.name;
|
|
440
|
+
if (result.kind === "image") media.alt = file.name;
|
|
441
|
+
if (["audio","video"].includes(result.kind)) media.controls = true;
|
|
442
|
+
$("#preview").append(media);
|
|
443
|
+
} else {
|
|
444
|
+
const pre = document.createElement("pre");
|
|
445
|
+
pre.textContent = result.text;
|
|
446
|
+
$("#preview").append(pre);
|
|
447
|
+
if (result.truncated)
|
|
448
|
+
$("#preview").append(document.createTextNode("\n仅预览前 20 KB"));
|
|
449
|
+
}
|
|
450
|
+
} catch (e) {
|
|
451
|
+
if (token === previewGeneration)
|
|
452
|
+
$("#preview").textContent = e.message || String(e);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
$("#preview-toggle").onclick = () => {
|
|
456
|
+
$("#preview").classList.toggle("hidden");
|
|
457
|
+
if (!$("#preview").classList.contains("hidden")) void preview();
|
|
458
|
+
else { previewGeneration++; if (previewUrl) { URL.revokeObjectURL(previewUrl);previewUrl=""; } }
|
|
459
|
+
};
|
|
460
|
+
try {
|
|
461
|
+
history = JSON.parse(localStorage.getItem("search-history") || "[]");
|
|
462
|
+
if (!Array.isArray(history)) history = [];
|
|
463
|
+
} catch {
|
|
464
|
+
history = [];
|
|
465
|
+
}
|
|
466
|
+
$("#history").innerHTML =
|
|
467
|
+
'<option value="">搜索历史</option>' +
|
|
468
|
+
history.map((q) => "<option>" + escapeHtml(q) + "</option>").join("");
|
|
469
|
+
ready(async (env) => {
|
|
470
|
+
if (env?.hostSearch) {
|
|
471
|
+
$("#query").style.display = "none";
|
|
472
|
+
$("#query").setAttribute("aria-hidden", "true");
|
|
473
|
+
}
|
|
474
|
+
await search();
|
|
475
|
+
if (!env?.hostSearch) $("#query").focus();
|
|
476
|
+
});
|
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
|
+
});
|