-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
619 lines (580 loc) · 26.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
let zh_CN = {
"msgSaved": "已保存",
"msgCopyFailed": "操作失败,未能复制元素链接",
"msgNotElement": "刚刚复制的不是画板元素!",
"msgIDFailed": "未能获取到画板元素的ID",
"msgCopied": "已将链接复制到剪切板",
"msgPreviewMode": "预览模式",
"msgPreviewModeOff": "已关闭预览模式",
"msgAutoSaveOff": "已关闭自动保存!",
"msgAutoSaveOn": "编辑结束约2s后自动保存",
"msgNoRelevantResults": "未查找到相关结果!",
"msgNeedPlugin": "悬浮预览失败,建议先安装插件【开放API】",
"msgSaveFailed": "保存失败,请查看控制台报错信息!",
"msgGetFileFailed": "画板文件获取失败,手动刷新后将新建空的白板",
"msgSetAliasFailed": "设置文档别名失败",
"msgReadFilePathFailed": "未能读取到块属性中白板文件的路径,将自动刷新",
"msgSetMemoFailed": "操作失败!未能将白板中的文本存入文档备注",
"BtnSaveBlockRef": "保存块引用",
"TipSaveBlockRef": "保存白板对其他文档/块的引用关系",
"BtnFixBrokenLinks": "修复块超链接",
"TipFixBrokenLinks": "根据白板的对其他文档/块的引用关系,尝试修复失效的块超链接",
"msgNotEmbeddedBlock": "嵌入块中不支持该操作",
"msgInsertList": "已成功嵌入引用块列表",
"msgListUpdated": "已更新引用块列表",
"msgNoLink": "当前白板未检测到块超链接",
"msgNoList": "未检测到引用块列表",
"msgFixLink": "已尝试修复失效的块超链接",
"msgFixLinkFailed": "操作失败!未能修复失效的块超链接"
}
let en_US = {
"msgSaved": "Saved",
"msgCopyFailed": "Operation failed, unable to copy the element link.",
"msgNotElement": "The copied item is not a canvas element!",
"msgIDFailed": "Failed to get the ID of the canvas element.",
"msgCopied": "The link has been copied to the clipboard.",
"msgPreviewMode": "Preview mode",
"msgPreviewModeOff": "Preview mode has been turned off.",
"msgAutoSaveOff": "Auto-save has been turned off!",
"msgAutoSaveOn": "Automatically save about 2 seconds after editing ends.",
"msgNoRelevantResults": "No relevant results found!",
"msgNeedPlugin": "Hover preview failed, it is recommended to install the plugin [Open API] first.",
"msgSaveFailed": "Save failed, please check the console for error messages!",
"msgGetFileFailed": "Failed to get the canvas file, a new blank whiteboard will be created after manual refresh.",
"msgSetAliasFailed": "Failed to set document alias.",
"msgReadFilePathFailed": "Failed to read the path of the whiteboard file in the block attributes, will automatically refresh.",
"msgSetMemoFailed": "Operation failed! The text in the whiteboard cannot be saved in the document notes.",
"BtnSaveBlockRef": "SaveBlockRef",
"TipSaveBlockRef": "Save the reference relationships of the whiteboard to other documents/blocks.",
"BtnFixBrokenLinks": "FixBrokenLinks",
"TipFixBrokenLinks": "According to the reference relationships of the whiteboard to other documents/blocks, try to repair the invalid block hyperlinks.",
"msgNotEmbeddedBlock": "This operation is not supported in embedded blocks.",
"msgInsertList": "The list of reference blocks has been successfully embedded.",
"msgListUpdated": "The list of reference blocks has been updated.",
"msgNoLink": "No block hyperlinks have been detected in the current whiteboard.",
"msgNoList": "The list of reference blocks has not been detected.",
"msgFixLink": "An attempt has been made to repair the invalid block hyperlinks.",
"msgFixLinkFailed": "Operation failed! The invalid block hyperlinks could not be repaired."
}
// 笔记软件设置语言为简体中文、繁体中文时,左上角弹出中文提示,否者弹出英文提示
window._languages = zh_CN;
let lang = window.top.siyuan.config.lang;
if (lang === "zh_CN" || lang === "zh_CHT") {
window._languages = zh_CN;
} else {
window._languages = en_US;
}
let myMessage = document.getElementById("myMessage");
let saveBtn = document.getElementById('saveBtn');
let refreshBtn = document.getElementById('refreshBtn');
refreshBtn.addEventListener('click', () => { window.location.reload(); })
// 弹出消息弹窗
window.showMessage = function (msg, duration = 2000) {
let dom = document.createElement('div')
dom.innerText = msg + ` (${new Date().toLocaleString()})`;
dom.className = 'message'
myMessage.appendChild(dom);
setTimeout(() => {
if (dom.previousElementSibling) {
myMessage.removeChild(dom.previousElementSibling)
}
}, duration);
}
// 保存时默认将白板中的文本内容写入到文档——备注中,方便全局检索
window._allowSetMemo = true;
// 当前使用的主题,深色/浅色
window._currentThemePath = localStorage?.getItem("excalidraw-theme") === "dark" ? "./theme/dark.css" : "./theme/theme.css";
// 默认关闭悬浮预览
window._allowPreview = !1;
// 是否默认开启自动保存
window._autoSave = true;
// 初始渲染时触发的那次自动保存没有必要,去掉,减少性能消耗
window._state = false;
if (window._autoSave) {
window._state = true;
window._autoSave = !window._autoSave;
}
// 复制内容到剪切板
function patseIntoClipboard(txt) {
let status = false
const input = document.createElement('textarea')
document.body.appendChild(input)
input.innerHTML = txt
input.select()
if (document.execCommand('copy')) {
document.execCommand('copy')
status = true
} else {
showMessage(window._languages["msgCopyFailed"])
}
document.body.removeChild(input)
return status
}
async function getElementIDFromClipboadr() {
let str = await navigator.clipboard.readText()
if (!str.startsWith(`{"type":"excalidraw/clipboard"`)) {
showMessage(window._languages["msgNotElement"])
return null
}
let result = str.match(/\"id\"\:\"(.+?)\"/);
let id;
if (result && result[1]) {
id = result[1]
} else {
showMessage(window._languages["msgIDFailed"])
return null
}
let status = patseIntoClipboard(`excalidraw://${id}`);
showMessage(window._languages["msgCopied"])
}
// 快捷键
document.addEventListener("keydown", (e => {
if (e.altKey && "q" === e.key) {
window._allowPreview = !0;
showMessage(window._languages["msgPreviewMode"])
}
if (e.altKey && "w" === e.key) {
window._allowPreview = !1;
showMessage(window._languages["msgPreviewModeOff"]);
}
if (e.altKey && "f" === e.key) {
window._autoSave = !window._autoSave;
if (!window._autoSave) {
showMessage(window._languages["msgAutoSaveOff"])
} else {
showMessage(window._languages["msgAutoSaveOn"])
}
}
if (e.ctrlKey && e.key === "j") {
getElementIDFromClipboadr();
}
// 全屏
if (e.altKey && "y" === e.key) {
if (window?.frameElement?.parentElement?.parentElement?.parentElement?.parentElement?.parentElement?.classList?.contains("protyle")) {
if (window?.frameElement?.parentElement?.parentElement?.parentElement?.parentElement?.parentElement?.classList?.contains("fullscreen")) {
window.frameElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.remove("fullscreen");
} else {
window.frameElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.add("fullscreen");
}
}
}
}), !0);
// 手动保存
saveBtn.addEventListener('click', (e) => {
if (window._isDarwin) {
// macOS上的保存
document.dispatchEvent(new KeyboardEvent("keydown", { key: "S", metaKey: true, bubbles: true }));
} else {
document.dispatchEvent(new KeyboardEvent("keydown", { key: "S", ctrlKey: true, bubbles: true }));
}
}, false);
function request(url, data = null) { return new Promise((resolve, reject) => { fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }).then((data) => resolve(data.json()), (error) => { reject(error); }).catch((err) => { console.error("请求失败:", err); }); }); };
function debounce(e, t = 500) { let r = null; return function (...n) { clearTimeout(r), r = setTimeout((() => { e.apply(this, n) }), t) } }
// 搜素关键字
(async () => {
const searchPanel = document.getElementById("searchPanel"),
keywordInput = document.getElementById("keywordInput"),
targetArrContainer = document.getElementById("targetArrContainer");
async function itemsContainsKeyword(e) {
let t = []; e = e.trim();
let widgetID = window.frameElement.parentElement.parentElement.getAttribute('data-node-id');
let memoPath = `assets/ExcalidrawFiles/${widgetID}.excalidraw`
let memoData = await request("/api/attr/getBlockAttrs", { id: widgetID });
if (memoData?.data["custom-data-assets"]) {
memoPath = memoData.data["custom-data-assets"];
}
let res = await request("/api/file/getFile", { path: `/data/${memoPath}` })
let r = res.elements;
if (r && e.length > 0) {
let keywordArr = e.split(' ');
keywordArr = keywordArr.filter((item) => item.trim());
keywordArr = Array.from(new Set(keywordArr));
for (let n of r) if ("text" === n.type && keywordArr.every((item) => new RegExp(`${item}`, "i").test(n.text))) {
n.text = n.text.replace(/</g, "<").replace(/>/g, ">");
keywordArr.forEach((item) => {
let r = new RegExp(`${item}`, "ig");
n.text = n.text.replace(r, (function (e) { return ` ${e} ` }));
});
keywordArr.forEach((item) => {
let r = new RegExp(` ${item} `, "ig");
n.text = n.text.replace(r, (function (e) { return `<span style="background-color:#ffe955;color:black;">${e.trim()}</span>` }));
});
t.push([n.text, n.id, n.x, n.y]);
}
}
return t
}
let itemsArr = [], currentIndex = -1;
async function handleKeywordSubmit() {
const e = keywordInput.value.trim();
if (itemsArr = [], e.length > 0) if (targetArrContainer.innerHTML = "", itemsArr = await itemsContainsKeyword(e), itemsArr.length > 0) {
const e = document.createDocumentFragment(); itemsArr.forEach((t => { const r = document.createElement("div"); r.className = "ExcalidrawItem", r.style.marginTop = "6px", r.style.color = "black", r.style.backgroundColor = "#ffffff", r.style.borderRadius = "4px", r.style.padding = "3px", r.setAttribute("data-x", t[2]), r.setAttribute("data-y", t[3]), r.innerHTML = `${t[0]}`, e.appendChild(r) })), targetArrContainer.appendChild(e)
} else targetArrContainer.innerHTML = window._languages["msgNoRelevantResults"], currentIndex = -1; else targetArrContainer.innerHTML = window._languages["msgNoRelevantResults"], currentIndex = -1
}
keywordInput.addEventListener("input", debounce(handleKeywordSubmit), !1),
targetArrContainer.addEventListener("click", (e => {
e.stopPropagation(); let t = targetArrContainer.querySelectorAll("div.ExcalidrawItem");
if (e.target.classList.contains("ExcalidrawItem")) { const r = parseInt(e.target.getAttribute("data-x")), n = parseInt(e.target.getAttribute("data-y")); window.dispatchEvent(new CustomEvent("searchKeyword", { detail: { x: r, y: n } })); for (let r = 0; r < t.length; r++)e.target !== t[r] || t[r].classList.contains("active") ? t[r].classList.remove("active") : (t[r].classList.add("active"), currentIndex = r) }
else if ("SPAN" === e.target.tagName && e.target.parentElement.classList.contains("ExcalidrawItem")) {
const r = parseInt(e.target.parentElement.getAttribute("data-x")), n = parseInt(e.target.parentElement.getAttribute("data-y"));
window.dispatchEvent(new CustomEvent("searchKeyword", { detail: { x: r, y: n } }));
for (let r = 0; r < t.length; r++)e.target.parentElement !== t[r] || t[r].classList.contains("active") ? t[r].classList.remove("active") : t[r].classList.add("active")
}
}), !1),
searchPanel.addEventListener("click", (e => { e.stopPropagation() }), !1), document.addEventListener("keydown", (e => { e.altKey && "t" === e.key && (searchPanel.style.visibility = "hidden" === searchPanel.style.visibility ? "visible" : "hidden", keywordInput.focus(), currentIndex = -1) }), !1), document.addEventListener("click", (e => { "visible" === searchPanel.style.visibility && (searchPanel.style.visibility = "hidden") }), !1), keywordInput.addEventListener("keydown", (e => { if (itemsArr.length > 0 && ("ArrowDown" === e.key || "ArrowUp" === e.key)) { "ArrowDown" === e.key ? currentIndex++ : "ArrowUp" === e.key && (e.preventDefault(), currentIndex--), currentIndex > itemsArr.length - 1 && (currentIndex = 0), currentIndex < 0 && (currentIndex = itemsArr.length - 1); let t = targetArrContainer.querySelectorAll("div.ExcalidrawItem"); for (let e = 0; e < t.length; e++)e !== currentIndex || t[e].classList.contains("active") ? t[e].classList.remove("active") : t[e].classList.add("active"); const r = parseInt(itemsArr[currentIndex][2]), n = parseInt(itemsArr[currentIndex][3]); window.dispatchEvent(new CustomEvent("searchKeyword", { detail: { x: r, y: n } })) } }), !1);
})()
async function SaveBlockRef() {
// 嵌入块中不生效
if (window?.frameElement?.parentElement?.parentElement?.parentElement?.classList?.contains("protyle-wysiwyg__embed")) {
showMessage(window._languages["msgNotEmbeddedBlock"]);
return
}
// 获取白板文件的路径
let widgetID = window.frameElement.parentElement.parentElement.getAttribute('data-node-id');
let memoPath = `assets/ExcalidrawFiles/${widgetID}.excalidraw`
let memoData = await request("/api/attr/getBlockAttrs", { id: widgetID });
if (memoData?.data["custom-data-assets"]) {
memoPath = memoData.data["custom-data-assets"];
}
// 获取白板中的块超链接
let res = await request("/api/file/getFile", { path: `/data/${memoPath}` })
let r = res.elements;
let str = "";
if (r && r.length > 0) {
r.forEach(item => {
if (item.link && item.link.match(/siyuan\:\/\/blocks\/\d{14}-\w{7}/)) {
let id = item.link.split("siyuan://blocks/")[1];
str += `* ((${id} '${id}'))` + "\n"
}
})
}
// 白板中没有块超链接时,需要清空块引用列表
if (!str.trim()) {
str = `* `;
}
// 将块超链接保存到当前文档,以引用块的形式
let nextBlock = window.frameElement.parentElement.parentElement.nextSibling;
if (!nextBlock || nextBlock?.getAttribute('data-subtype') !== "u") {
let response = await request("/api/block/insertBlock", {
"dataType": "markdown",
"data": str,
"previousID": widgetID,
})
if (response.code === 0) {
showMessage(window._languages["msgInsertList"]);
}
} else {
let targetBlockID = nextBlock.getAttribute("data-node-id");
if (targetBlockID) {
let res = await request("/api/block/updateBlock", {
"dataType": "markdown",
"data": str,
"id": targetBlockID
})
if (res.code === 0) {
showMessage(window._languages["msgListUpdated"]);
}
}
}
}
window.addEventListener("SaveBlockRef", () => {
SaveBlockRef().catch(err => { console.error(err) })
})
async function FixBrokenLinks() {
// 嵌入块中不生效
if (window?.frameElement?.parentElement?.parentElement?.parentElement?.classList?.contains("protyle-wysiwyg__embed")) {
showMessage(window._languages["msgNotEmbeddedBlock"]);
return
}
let newIndex = {};
let nextBlock = window?.frameElement?.parentElement?.parentElement?.nextSibling;
let blockType = nextBlock?.getAttribute("data-subtype");
if (!nextBlock || blockType !== "u") {
showMessage(window._languages["msgNoList"])
} else {
let nextID = nextBlock?.getAttribute("data-node-id");
let res = await request("/api/block/getChildBlocks", {
id: nextID
})
if (res?.data?.length > 0) {
res.data.forEach((item) => {
const result = item.markdown.match(/\* \(\((\d{14}-\w{7}) \'(\d{14}-\w{7})\'\)\)/);
if (result) {
newIndex[result[2]] = result[1]
}
})
}
// 导入Siyuan .sy.zip时,块ID被软件重置,导致块超链接失效。需要修正块超链接
let widgetID = window.frameElement.parentElement.parentElement.getAttribute('data-node-id');
let memoPath = `assets/ExcalidrawFiles/${widgetID}.excalidraw`
let memoData = await request("/api/attr/getBlockAttrs", { id: widgetID });
if (memoData?.data["custom-data-assets"]) {
memoPath = memoData.data["custom-data-assets"];
}
// 获取白板中的块超链接
let res2 = await request("/api/file/getFile", { path: `/data/${memoPath}` })
let r = res2.elements;
if (r && r.length > 0) {
r.forEach(item => {
if (item.link && item.link.match(/siyuan\:\/\/blocks\/\d{14}-\w{7}/)) {
let id = item.link.split("siyuan://blocks/")[1];
if (newIndex[id]) {
item.link = "siyuan://blocks/" + newIndex[id]
}
}
})
res2.elements = r;
let DataBlob = new Blob([JSON.stringify(res2)], {
type: "application/json",
});
let tempArr = memoPath.split("/");
let fileName = tempArr[tempArr.length - 1];
let DataFile = new File([DataBlob], fileName);
let reqBody = new FormData();
reqBody.append("path", `/data/${memoPath}`);
reqBody.append("file", DataFile);
fetch("/api/file/putFile", {
body: reqBody,
method: "POST",
headers: {},
})
.then(function () {
if (window?.top?.openAPI?.siyuan?.showMessage) {
window.top.openAPI.siyuan.showMessage(window._languages["msgFixLink"]);
}
window.location.reload();
})
.catch((err) => {
showMessage(window._languages["msgFixLinkFailed"]);
console.error(err);
});
} else {
showMessage(window._languages["msgNoLink"])
}
}
}
window.addEventListener("FixBrokenLinks", () => {
FixBrokenLinks().catch(err => { console.error(err) })
})
// 检索面板
const searchBlocksPanel = document.getElementById('searchBlocksPanel');
const deleteBtn = document.getElementById('delete');
const keywordBtn = document.getElementById('keyword');
const resultList = document.getElementById('result');
const closeBtn = document.getElementById('close')
// 快捷键——显示/隐藏检索面板
document.addEventListener("keydown", (e => {
if (e.altKey && "p" === e.key) {
if (searchBlocksPanel.style.visibility === "hidden") {
searchBlocksPanel.style.visibility = "visible";
keywordBtn.focus();
} else {
searchBlocksPanel.style.visibility = "hidden";
}
}
}), !1)
// 关闭按钮
closeBtn.addEventListener("click", () => {
searchBlocksPanel.style.visibility = "hidden";
})
// 清空输入框
deleteBtn.addEventListener('click', () => {
keywordBtn.value = "";
resultList.innerHTML = "";
keywordBtn.focus();
})
let itemsArr = [], currentIndex = 0;
// 检索文档、标题
async function handleInput() {
resultList.innerHTML = "";
if (keywordBtn.value.trim()) {
let keyword = keywordBtn.value;
let res = await request("/api/search/searchRefBlock", {
"k": keyword,
"id": "10000000000000-buciyqd",
"beforeLen": 24,
"rootID": "10000000000000-buciyqd",
"isDatabase": false,
"isSquareBrackets": true,
"reqId": Date.now()
})
if (res.code === 0 && res?.data?.blocks?.length > 0) {
let docList = res.data.blocks;
let f = document.createDocumentFragment();
docList.forEach(item => {
let alias = "";
let memo = "";
let icon = ""
if (item.alias) {
alias = `<span class="alias"><svg class="icon"><use xlink:href="#iconA"></use></svg>${item.alias}</span>`
}
if (item.memo) {
memo = `<span class="memo"><svg class="icon"><use xlink:href="#iconM"></use></svg>${item.memo}</span>`
}
if (item.type === "NodeHeading") {
switch (item.subType) {
case "h1":
icon = `<svg class="icon"><use xlink:href="#iconHeadings"></use></svg>`
break;
case "h2":
icon = `<svg class="icon"><use xlink:href="#iconH2"></use></svg>`
break;
case "h3":
icon = `<svg class="icon"><use xlink:href="#iconH3"></use></svg>`
break;
case "h4":
icon = `<svg class="icon"><use xlink:href="#iconH4"></use></svg>`
break;
case "h5":
icon = `<svg class="icon"><use xlink:href="#iconH5"></use></svg>`
break;
case "h6":
icon = `<svg class="icon"><use xlink:href="#iconH6"></use></svg>`
break;
default:
icon = `<svg class="icon"><use xlink:href="#iconHeadings"></use></svg>`
}
} else {
switch (item.type) {
case "NodeDocument":
icon = `📄`
break;
case "NodeParagraph":
icon = `<svg class="icon"><use xlink:href="#iconParagraph"></use></svg>`
break;
case "NodeMathBlock":
icon = `<svg class="icon"><use xlink:href="#iconMath"></use></svg>`
break;
case "NodeTable":
icon = `<svg class="icon"><use xlink:href="#iconTable"></use></svg>`
break;
case "NodeCodeBlock":
icon = `<svg class="icon"><use xlink:href="#iconCode"></use></svg>`
break;
case "NodeHTMLBlock":
icon = `<svg class="icon"><use xlink:href="#iconHTML5"></use></svg>`
break;
case "NodeAttributeView":
icon = `<svg class="icon"><use xlink:href="#iconDatabase"></use></svg>`
break;
case "NodeBlockQueryEmbed":
icon = `<svg class="icon"><use xlink:href="#iconSQL"></use></svg>`
break;
case "NodeVideo":
icon = `<svg class="icon"><use xlink:href="#iconVideo"></use></svg>`
break;
case "NodeAudio":
icon = `<svg class="icon"><use xlink:href="#iconRecord"></use></svg>`
break;
case "NodeIFrame":
icon = `<svg class="icon"><use xlink:href="#iconLanguage"></use></svg>`
break;
case "NodeWidget":
icon = `<svg class="icon"><use xlink:href="#iconBoth"></use></svg>`
break;
case "NodeBlockquote":
icon = `<svg class="icon"><use xlink:href="#iconQuote"></use></svg>`
break;
case "NodeSuperBlock":
icon = `<svg class="icon"><use xlink:href="#iconSuper"></use></svg>`
break;
case "NodeList":
icon = `<svg class="icon"><use xlink:href="#iconList"></use></svg>`
break;
case "NodeListItem":
icon = `<svg class="icon"><use xlink:href="#iconListItem"></use></svg>`
break;
}
}
let dom = document.createElement('div');
dom.className = "searchItem";
dom.innerHTML = `${alias}${memo}<div class="path">${item.hPath}</div>
<div class="title"><span class="link" data-href="siyuan://blocks/${item.id}" >${icon}</span> ${item.content}</div> <div class="insertDoc" data-href="siyuan://blocks/${item.id}" >➕</div>`;
f.append(dom);
});
resultList.append(f);
// 更新搜索结果
itemsArr = resultList.querySelectorAll("div.searchItem");
currentIndex = 0;
itemsArr[currentIndex].classList.add("active");
} else {
resultList.innerHTML = "未查询到相关结果";
}
}
}
keywordBtn.addEventListener('input', debounce(handleInput));
// 文档名、标题名前的图标,点击跳转到对应的块
resultList.addEventListener("click", (e) => {
if (e.target.tagName === "SPAN" && e.target.className === "link") {
e.stopPropagation();
let url = e.target.getAttribute("data-href");
window.top.openFileByURL(url);
} else if (e.target.tagName === "svg") {
e.stopPropagation();
let url = e.target.parentElement.getAttribute("data-href");
window.top.openFileByURL(url);
} else if (e.target.tagName === "use") {
e.stopPropagation();
let url = e.target.parentElement.parentElement.getAttribute("data-href");
window.top.openFileByURL(url);
} else if (e.target.tagName === "DIV" && e.target.className === "insertDoc") {
e.stopPropagation();
let url = e.target.getAttribute("data-href");
window.dispatchEvent(new CustomEvent("createEmbedElement", { detail: { link: url } }));
}
}, false)
// 上、下箭头切换搜索结果
keywordBtn.addEventListener("keydown", (e => {
if ("ArrowDown" === e.key) {
itemsArr[currentIndex].classList.remove("active");
if (currentIndex + 1 < itemsArr.length) {
currentIndex += 1;
itemsArr[currentIndex].classList.add('active');
} else {
currentIndex = 0;
itemsArr[currentIndex].classList.add('active');
}
let offsetTop = itemsArr[currentIndex].offsetTop;
resultList.scrollTo(0, offsetTop < 100 ? 0 : offsetTop - 100);
}
if ("ArrowUp" === e.key) {
e.stopPropagation();
e.preventDefault();
itemsArr[currentIndex].classList.remove("active");
if (currentIndex > 0) {
currentIndex -= 1;
itemsArr[currentIndex].classList.add('active');
} else {
currentIndex = itemsArr.length - 1;
itemsArr[currentIndex].classList.add('active');
}
let offsetTop = itemsArr[currentIndex].offsetTop;
resultList.scrollTo(0, offsetTop < 100 ? 0 : offsetTop - 100);
}
// 按Enter嵌入当前已选中的文档块或标题块
if ("Enter" === e.key) {
let selectedResult = resultList.querySelector("div.searchItem.active");
if (selectedResult) {
let url = selectedResult.querySelector("span.link")?.getAttribute("data-href");
if (url) { window.dispatchEvent(new CustomEvent("createEmbedElement", { detail: { link: url } })); }
}
}
}), false);
// 在笔记软件V3.1.20中,悬浮预览API的参数已修改,需要适配
(() => {
window._isNewVersion = false;
let version = window?.top?.siyuan?.config?.system?.kernelVersion;
if (version) {
try {
let arr = version.split(".");
arr[1] = arr[1].padStart(2, '0');
arr[2] = arr[2].padStart(2, '0');
version = parseInt(arr.join(""));
if (version >= 30120) {
window._isNewVersion = true;
}
} catch (err) {
console.error(err)
}
}
})()