-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathutil.ts
More file actions
757 lines (741 loc) · 30.9 KB
/
util.ts
File metadata and controls
757 lines (741 loc) · 30.9 KB
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
import {Tab} from "../layout/Tab";
import {Editor} from "./index";
import {Wnd} from "../layout/Wnd";
import {getInstanceById, getWndByLayout, pdfIsLoading, setPanelFocus} from "../layout/util";
import {getDockByType} from "../layout/tabUtil";
import {getAllModels, getAllTabs} from "../layout/getAll";
import {highlightById, scrollCenter} from "../util/highlightById";
import {getDisplayName, useShell, pathPosix} from "../util/pathName";
import {Constants} from "../constants";
import {setEditMode} from "../protyle/util/setEditMode";
import {Files} from "../layout/dock/Files";
import {fetchPost, fetchSyncPost} from "../util/fetch";
import {focusBlock, focusByOffset, focusByRange} from "../protyle/util/selection";
import {onGet} from "../protyle/util/onGet";
/// #if !BROWSER
import {ipcRenderer} from "electron";
/// #endif
import {pushBack} from "../util/backForward";
import {Asset} from "../asset";
import {Layout} from "../layout";
import {
hasClosestBlock,
hasClosestByAttribute,
hasClosestByClassName,
isInEmbedBlock,
} from "../protyle/util/hasClosest";
import {zoomOut} from "../menus/protyle";
import {countBlockWord, countSelectWord} from "../layout/status";
import {showMessage} from "../dialog/message";
import {objEquals} from "../util/functions";
import {resize} from "../protyle/util/resize";
import {Search} from "../search";
import {App} from "../index";
import {newCardModel} from "../card/newCardTab";
import {preventScroll} from "../protyle/scroll/preventScroll";
import {clearOBG} from "../layout/dock/util";
import {Model} from "../layout/Model";
import {hideElements} from "../protyle/ui/hideElements";
import {emitToEventBus} from "../plugin/EventBus";
export const openFileById = async (options: {
app: App,
id: string,
position?: string,
mode?: TEditorMode,
action?: TProtyleAction[]
keepCursor?: boolean
zoomIn?: boolean
removeCurrentTab?: boolean
openNewTab?: boolean
afterOpen?: (model: Model) => void,
scrollPosition?: ScrollLogicalPosition
}) => {
const response = await fetchSyncPost("/api/block/getBlockInfo", {id: options.id});
if (response.code === -1) {
return;
}
if (response.code === 3) {
showMessage(response.msg);
return;
}
return openFile({
app: options.app,
fileName: response.data.rootTitle,
rootIcon: response.data.rootIcon,
rootID: response.data.rootID,
id: options.id,
position: options.position,
mode: options.mode,
action: options.action,
zoomIn: options.zoomIn,
keepCursor: options.keepCursor,
removeCurrentTab: options.removeCurrentTab,
afterOpen: options.afterOpen,
openNewTab: options.openNewTab,
scrollPosition: options.scrollPosition,
});
};
export const openAsset = (app: App, assetPath: string, page: number | string, position?: string) => {
const suffix = pathPosix().extname(assetPath).split("?")[0];
if (!Constants.SIYUAN_ASSETS_EXTS.includes(suffix)) {
return;
}
openFile({
app,
assetPath,
page,
position,
removeCurrentTab: true
});
};
export const openFile = async (options: IOpenFileOptions) => {
if (typeof options.removeCurrentTab === "undefined") {
options.removeCurrentTab = true;
}
// https://github.com/siyuan-note/siyuan/issues/10168
document.querySelectorAll(".av__panel, .av__mask").forEach(item => {
item.remove();
});
// 打开 PDF 时移除文档光标
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
const allModels = getAllModels();
// 文档已打开
if (options.assetPath) {
clearOBG();
const asset = allModels.asset.find((item) => {
if (item.path == options.assetPath) {
if (!pdfIsLoading(item.parent.parent.element)) {
item.parent.parent.switchTab(item.parent.headElement);
item.parent.parent.showHeading();
item.goToPage(options.page);
}
return true;
}
});
if (asset) {
if (options.afterOpen) {
options.afterOpen(asset);
}
return asset.parent;
}
} else if (options.custom) {
clearOBG();
const custom = allModels.custom.find((item) => {
if (objEquals(item.data, options.custom.data) && (!options.custom.id || options.custom.id === item.type)) {
if (!pdfIsLoading(item.parent.parent.element)) {
item.parent.parent.switchTab(item.parent.headElement);
item.parent.parent.showHeading();
}
return true;
}
});
if (custom) {
if (options.afterOpen) {
options.afterOpen(custom);
}
return custom.parent;
}
const hasModel = getUnInitTab(options);
if (hasModel) {
if (options.afterOpen) {
options.afterOpen(hasModel.model);
}
return hasModel;
}
} else if (options.searchData) {
clearOBG();
const search = allModels.search.find((item) => {
if (objEquals(item.config, options.searchData)) {
if (!pdfIsLoading(item.parent.parent.element)) {
item.parent.parent.switchTab(item.parent.headElement);
item.parent.parent.showHeading();
}
return true;
}
});
if (search) {
return search.parent;
}
} else if (!options.position && !options.openNewTab) {
let editor: Editor;
let activeEditor: Editor;
allModels.editor.find((item) => {
if (item.editor.protyle.block.rootID === options.rootID) {
if (hasClosestByClassName(item.element, "layout__wnd--active")) {
activeEditor = item;
}
if (!editor || item.headElement.getAttribute("data-activetime") > editor.headElement.getAttribute("data-activetime")) {
// https://github.com/siyuan-note/siyuan/issues/11981#issuecomment-2351939812
editor = item;
}
}
if (activeEditor) {
return true;
}
});
if (activeEditor) {
editor = activeEditor;
}
if (editor) {
if (!pdfIsLoading(editor.parent.parent.element)) {
switchEditor(editor, options, allModels);
}
if (options.afterOpen) {
options.afterOpen(editor);
}
return editor.parent;
}
// 没有初始化的页签无法检测到
const hasEditor = getUnInitTab(options);
if (hasEditor) {
if (options.afterOpen) {
options.afterOpen(hasEditor.model);
}
return hasEditor;
}
}
/// #if !BROWSER
// https://github.com/siyuan-note/siyuan/issues/7491
if (!options.position || (options.position === "right" && options.assetPath)) {
let hasMatch = false;
const optionsClone: IObject = {};
Object.keys(options).forEach((key: keyof IOpenFileOptions) => {
if (key !== "app" && options[key] && typeof options[key] !== "function") {
optionsClone[key] = JSON.parse(JSON.stringify(options[key]));
}
});
hasMatch = await ipcRenderer.invoke(Constants.SIYUAN_GET, {
cmd: Constants.SIYUAN_OPEN_FILE,
options: JSON.stringify(optionsClone),
port: location.port,
});
if (hasMatch) {
if (options.afterOpen) {
options.afterOpen();
}
return;
}
}
/// #endif
let wnd: Wnd = undefined;
// 获取光标所在 tab
const element = document.querySelector(".layout__wnd--active");
if (element) {
wnd = getInstanceById(element.getAttribute("data-id")) as Wnd;
}
if (!wnd) {
// 中心 tab
wnd = getWndByLayout(window.siyuan.layout.centerLayout);
}
if (wnd) {
let createdTab: Tab;
if ((options.position === "right" || options.position === "bottom") && wnd.children[0].headElement) {
const direction = options.position === "right" ? "lr" : "tb";
let targetWnd: Wnd;
if (wnd.parent.children.length > 1 && wnd.parent instanceof Layout && wnd.parent.direction === direction) {
wnd.parent.children.find((item, index) => {
if (item.id === wnd.id) {
let nextWnd = wnd.parent.children[index + 1];
if (!nextWnd) {
// wnd 为右侧时,应设置其为目标
nextWnd = wnd;
}
while (nextWnd instanceof Layout) {
nextWnd = nextWnd.children[0];
}
targetWnd = nextWnd;
return true;
}
});
}
if (targetWnd) {
if (pdfIsLoading(targetWnd.element)) {
if (options.afterOpen) {
options.afterOpen();
}
return;
}
// 在右侧/下侧打开已有页签将进行页签切换 https://github.com/siyuan-note/siyuan/issues/5366
let hasEditor = targetWnd.children.find(item => {
if (item.model && item.model instanceof Editor && item.model.editor.protyle.block.rootID === options.rootID) {
switchEditor(item.model, options, allModels);
return true;
}
});
if (!hasEditor) {
hasEditor = getUnInitTab(options);
createdTab = hasEditor;
}
if (!hasEditor) {
createdTab = newTab(options);
targetWnd.addTab(createdTab);
}
} else {
createdTab = newTab(options);
wnd.split(direction).addTab(createdTab);
}
wnd.showHeading();
if (options.afterOpen) {
options.afterOpen(createdTab ? createdTab.model : undefined);
}
return createdTab;
}
if (pdfIsLoading(wnd.element)) {
if (options.afterOpen) {
options.afterOpen();
}
return;
}
if (options.keepCursor && wnd.children[0].headElement) {
createdTab = newTab(options);
createdTab.headElement.setAttribute("keep-cursor", options.id);
wnd.addTab(createdTab, options.keepCursor);
} else if (window.siyuan.config.fileTree.openFilesUseCurrentTab) {
let unUpdateTab: Tab;
// 不能 reverse, 找到也不能提前退出循环,否则 https://github.com/siyuan-note/siyuan/issues/3271
wnd.children.find((item) => {
if (item.headElement && item.headElement.classList.contains("item--unupdate") && !item.headElement.classList.contains("item--pin")) {
unUpdateTab = item;
if (item.headElement.classList.contains("item--focus")) {
// https://ld246.com/article/1658979494658
return true;
}
}
});
createdTab = newTab(options);
wnd.addTab(createdTab);
if (unUpdateTab && options.removeCurrentTab) {
wnd.removeTab(unUpdateTab.id, false, false);
}
} else {
createdTab = newTab(options);
wnd.addTab(createdTab);
}
wnd.showHeading();
if (options.afterOpen) {
options.afterOpen(createdTab.model);
}
return createdTab;
}
};
// 没有初始化的页签无法检测到
const getUnInitTab = (options: IOpenFileOptions) => {
return getAllTabs().find(item => {
const initData = item.headElement?.getAttribute("data-initdata");
if (initData) {
const initObj = JSON.parse(initData);
if (initObj.instance === "Editor" &&
(initObj.rootId === options.rootID || initObj.blockId === options.rootID)) {
initObj.blockId = options.id;
initObj.mode = options.mode;
if (options.zoomIn) {
initObj.action = [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS];
} else {
initObj.action = options.action;
}
initObj.scrollPosition = options.scrollPosition;
item.headElement.setAttribute("data-initdata", JSON.stringify(initObj));
item.parent.switchTab(item.headElement);
return true;
} else if (initObj.instance === "Custom" && options.custom && objEquals(initObj.customModelData, options.custom.data)) {
item.parent.switchTab(item.headElement);
return true;
}
}
});
};
const switchEditor = (editor: Editor, options: IOpenFileOptions, allModels: IModels) => {
if (options.keepCursor) {
editor.parent.headElement.setAttribute("keep-cursor", options.id);
return true;
}
editor.parent.parent.switchTab(editor.parent.headElement);
editor.parent.parent.showHeading();
if (options.mode !== "preview" && !editor.editor.protyle.preview.element.classList.contains("fn__none")) {
// TODO https://github.com/siyuan-note/siyuan/issues/3059
return true;
}
if (options.zoomIn) {
zoomOut({protyle: editor.editor.protyle, id: options.id});
return true;
}
let nodeElement: Element;
Array.from(editor.editor.protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${options.id}"]`)).find(item => {
if (!isInEmbedBlock(item)) {
nodeElement = item;
return true;
}
});
if ((!nodeElement || nodeElement?.clientHeight === 0) && options.id !== options.rootID) {
fetchPost("/api/filetree/getDoc", {
id: options.id,
mode: (options.action && options.action.includes(Constants.CB_GET_CONTEXT)) ? 3 : 0,
size: window.siyuan.config.editor.dynamicLoadBlocks,
}, getResponse => {
onGet({
data: getResponse,
protyle: editor.editor.protyle,
action: options.action,
scrollPosition: options.scrollPosition
});
// 大纲点击折叠标题下的内容时,需更新反链面板
updateBacklinkGraph(allModels, editor.editor.protyle);
});
} else {
// 点击大纲产生滚动时会动态加载内容,最终导致定位不准确
preventScroll(editor.editor.protyle);
editor.editor.protyle.observerLoad?.disconnect();
if (options.action?.includes(Constants.CB_GET_HL)) {
highlightById(editor.editor.protyle, options.id, "start");
}
if (options.action?.includes(Constants.CB_GET_FOCUS)) {
if (nodeElement) {
let scrollTop: number;
if (options.action.includes(Constants.CB_GET_SEARCH)) {
const scrollAttr = window.siyuan.storage[Constants.LOCAL_FILEPOSITION][editor.editor.protyle.block.rootID];
focusByOffset(nodeElement, scrollAttr.focusStart, scrollAttr.focusEnd);
scrollTop = scrollAttr.scrollTop;
} else {
const newRange = focusBlock(nodeElement, undefined, !options.action?.includes(Constants.CB_GET_OUTLINE));
if (newRange) {
editor.editor.protyle.toolbar.range = newRange;
}
}
if (typeof scrollTop === "number") {
editor.editor.protyle.contentElement.scrollTop = scrollTop;
} else {
scrollCenter(editor.editor.protyle, (editor.editor.protyle.disabled || options.scrollPosition) ? nodeElement : null, options.scrollPosition);
}
editor.editor.protyle.observerLoad = new ResizeObserver(() => {
if (document.contains(nodeElement)) {
if (typeof scrollTop === "number") {
editor.editor.protyle.contentElement.scrollTop = scrollTop;
} else {
scrollCenter(editor.editor.protyle, (editor.editor.protyle.disabled || options.scrollPosition) ? nodeElement : null, options.scrollPosition);
}
}
});
setTimeout(() => {
editor.editor.protyle.observerLoad.disconnect();
}, 1000 * 3);
editor.editor.protyle.observerLoad.observe(editor.editor.protyle.wysiwyg.element);
} else if (editor.editor.protyle.block.rootID === options.id) {
// 由于 https://github.com/siyuan-note/siyuan/issues/5420,移除定位
} else if (editor.editor.protyle.toolbar.range) {
nodeElement = hasClosestBlock(editor.editor.protyle.toolbar.range.startContainer) as Element;
focusByRange(editor.editor.protyle.toolbar.range);
scrollCenter(editor.editor.protyle, undefined, options.scrollPosition);
}
}
pushBack(editor.editor.protyle, editor.editor.protyle.toolbar.range);
}
// https://github.com/siyuan-note/siyuan/issues/16445
if (options.action?.includes(Constants.CB_GET_OUTLINE)) {
hideElements(["select"], editor.editor.protyle);
}
if (options.mode) {
setEditMode(editor.editor.protyle, options.mode);
}
};
const newTab = (options: IOpenFileOptions) => {
let tab: Tab;
if (options.assetPath) {
const suffix = pathPosix().extname(options.assetPath).split("?")[0];
if (Constants.SIYUAN_ASSETS_EXTS.includes(suffix)) {
let icon = "iconPDF";
if (Constants.SIYUAN_ASSETS_IMAGE.includes(suffix)) {
icon = "iconImage";
} else if (Constants.SIYUAN_ASSETS_AUDIO.includes(suffix)) {
icon = "iconRecord";
} else if (Constants.SIYUAN_ASSETS_VIDEO.includes(suffix)) {
icon = "iconVideo";
}
tab = new Tab({
icon,
title: getDisplayName(options.assetPath),
callback(tab) {
tab.addModel(new Asset({
app: options.app,
tab,
path: options.assetPath,
page: options.page,
}));
setPanelFocus(tab.panelElement.parentElement.parentElement);
}
});
}
} else if (options.custom) {
tab = new Tab({
icon: options.custom.icon,
title: options.custom.title,
callback(tab) {
if (options.custom.id) {
if (options.custom.id === "siyuan-card") {
tab.addModel(newCardModel({
app: options.app,
tab,
data: options.custom.data
}));
} else {
options.app.plugins.find(p => {
if (p.models[options.custom.id]) {
tab.addModel(p.models[options.custom.id]({
tab,
data: options.custom.data
}));
return true;
}
});
}
} else {
// plugin 0.8.3 历史兼容
console.warn("0.8.3 将移除 custom.fn 参数,请参照 https://github.com/siyuan-note/plugin-sample/blob/91a716358941791b4269241f21db25fd22ae5ff5/src/index.ts 将其修改为 custom.id");
tab.addModel(options.custom.fn({
tab,
data: options.custom.data
}));
}
setPanelFocus(tab.panelElement.parentElement.parentElement);
}
});
} else if (options.searchData) {
tab = new Tab({
icon: "iconSearch",
title: window.siyuan.languages.search,
callback(tab) {
tab.addModel(new Search({
app: options.app,
tab,
config: options.searchData
}));
setPanelFocus(tab.panelElement.parentElement.parentElement);
}
});
} else {
tab = new Tab({
title: getDisplayName(options.fileName, true, true),
docIcon: options.rootIcon,
callback(tab) {
let editor;
if (options.zoomIn) {
editor = new Editor({
app: options.app,
tab,
blockId: options.id,
rootId: options.rootID,
action: [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS],
scrollPosition: options.scrollPosition,
});
} else {
editor = new Editor({
app: options.app,
tab,
blockId: options.id,
rootId: options.rootID,
mode: options.mode,
action: options.action,
scrollPosition: options.scrollPosition,
});
}
tab.addModel(editor);
}
});
}
return tab;
};
export const updatePanelByEditor = (options: {
protyle?: IProtyle,
focus: boolean,
pushBackStack: boolean,
reload: boolean,
resize: boolean
}) => {
if (options.protyle && options.protyle.path) {
// https://ld246.com/article/1637636106054/comment/1641485541929#comments
if (options.protyle.element.classList.contains("fn__none") ||
(!hasClosestByClassName(options.protyle.element, "layout__wnd--active") &&
document.querySelector(".layout__wnd--active") // https://github.com/siyuan-note/siyuan/issues/4414
)
) {
return;
}
if (options.resize) {
resize(options.protyle);
}
if (options.focus) {
if (options.protyle.toolbar.range) {
focusByRange(options.protyle.toolbar.range);
countSelectWord(options.protyle.toolbar.range, options.protyle.block.rootID);
if (options.pushBackStack && options.protyle.preview.element.classList.contains("fn__none")) {
pushBack(options.protyle, options.protyle.toolbar.range);
}
} else {
focusBlock(options.protyle.wysiwyg.element.firstElementChild);
if (options.pushBackStack && options.protyle.preview.element.classList.contains("fn__none")) {
pushBack(options.protyle, undefined, options.protyle.wysiwyg.element.firstElementChild);
}
countBlockWord([], options.protyle.block.rootID);
}
}
if (window.siyuan.config.fileTree.alwaysSelectOpenedFile && options.protyle) {
const fileModel = getDockByType("file")?.data.file;
if (fileModel instanceof Files) {
const target = fileModel.element.querySelector(`li[data-path="${options.protyle.path}"]`);
if (!target || (target && !target.classList.contains("b3-list-item--focus"))) {
fileModel.selectItem(options.protyle.notebookId, options.protyle.path);
}
}
}
emitToEventBus("switch-protyle", {protyle: options.protyle});
}
// 切换页签或关闭所有页签时,需更新对应的面板
const models = getAllModels();
updateOutline(models, options.protyle, options.reload);
updateBacklinkGraph(models, options.protyle);
};
export const isCurrentEditor = (blockId: string) => {
const activeElement = document.querySelector(".layout__wnd--active > .fn__flex > .layout-tab-bar > .item--focus");
if (activeElement) {
const tab = getInstanceById(activeElement.getAttribute("data-id"));
if (tab instanceof Tab && tab.model instanceof Editor) {
if (tab.model.editor.protyle.block.rootID === blockId ||
tab.model.editor.protyle.block.parentID === blockId || // updateBacklinkGraph 时会传入 parentID
tab.model.editor.protyle.block.id === blockId) {
return true;
}
}
}
return false;
};
export const updateOutline = (models: IModels, protyle: IProtyle, reload = false) => {
models.outline.find(item => {
if (reload ||
(item.type === "pin" &&
(!protyle || item.blockId !== protyle.block?.rootID ||
item.isPreview === protyle.preview.element.classList.contains("fn__none"))
)
) {
let blockId = "";
if (protyle && protyle.block) {
blockId = protyle.block.rootID;
}
if (blockId === item.blockId && !reload && item.isPreview !== protyle.preview.element.classList.contains("fn__none")) {
return;
}
fetchPost("/api/outline/getDocOutline", {
id: blockId,
preview: !protyle.preview.element.classList.contains("fn__none")
}, response => {
if (!reload && (!isCurrentEditor(blockId) || item.blockId === blockId) &&
item.isPreview !== protyle.preview.element.classList.contains("fn__none")) {
return;
}
item.isPreview = !protyle.preview.element.classList.contains("fn__none");
item.update(response, blockId);
if (protyle) {
item.updateDocTitle(protyle.background.ial, response.data?.length || 0);
if (getSelection().rangeCount > 0) {
const startContainer = getSelection().getRangeAt(0).startContainer;
if (protyle.wysiwyg.element.contains(startContainer)) {
const currentElement = hasClosestByAttribute(startContainer, "data-node-id", null);
if (currentElement) {
item.setCurrent(currentElement);
}
}
}
} else {
item.updateDocTitle();
}
});
}
});
};
export const updateBacklinkGraph = (models: IModels, protyle: IProtyle) => {
// https://ld246.com/article/1637636106054/comment/1641485541929#comments
if (protyle && protyle.element.classList.contains("fn__none") ||
(protyle && !hasClosestByClassName(protyle.element, "layout__wnd--active") &&
document.querySelector(".layout__wnd--active") // https://github.com/siyuan-note/siyuan/issues/4414
)
) {
return;
}
models.graph.forEach(item => {
if (item.type !== "global" && (!protyle || item.blockId !== protyle.block?.id)) {
if (item.type === "local" && item.rootId !== protyle?.block?.rootID) {
return;
}
let blockId = "";
if (protyle && protyle.block) {
blockId = protyle.block.showAll ? protyle.block.id : protyle.block.parentID;
}
if (blockId === item.blockId) {
return;
}
item.searchGraph(true, blockId);
}
});
models.backlink.forEach(item => {
if (item.type === "local" && item.rootId !== protyle?.block?.rootID) {
return;
}
let blockId = "";
if (protyle && protyle.block) {
blockId = protyle.block.showAll ? protyle.block.id : protyle.block.parentID;
}
if (blockId === item.blockId) {
return;
}
item.element.querySelector('.block__icon[data-type="refresh"] svg').classList.add("fn__rotate");
fetchPost("/api/ref/getBacklink2", {
sort: item.status[blockId] ? item.status[blockId].sort.toString() : window.siyuan.config.editor.backlinkSort.toString(),
mSort: item.status[blockId] ? item.status[blockId].mSort.toString() : window.siyuan.config.editor.backmentionSort.toString(),
id: blockId || "",
k: item.inputsElement[0].value,
mk: item.inputsElement[1].value,
}, response => {
if (!isCurrentEditor(blockId) || item.blockId === blockId) {
item.element.querySelector('.block__icon[data-type="refresh"] svg').classList.remove("fn__rotate");
return;
}
item.saveStatus();
item.blockId = blockId;
item.render(response.data);
});
});
};
export const openBy = (url: string, type: "folder" | "app") => {
/// #if !BROWSER
if (url.startsWith("assets/")) {
fetchPost("/api/asset/resolveAssetPath", {path: url.replace(/\.pdf\?page=\d{1,}$/, ".pdf")}, (response) => {
if (type === "app") {
useShell("openPath", response.data);
} else if (type === "folder") {
useShell("showItemInFolder", response.data);
}
});
return;
}
let address = "";
if ("windows" === window.siyuan.config.system.os) {
// `file://` 协议兼容 Window 平台使用 `/` 作为目录分割线 https://github.com/siyuan-note/siyuan/issues/5681
address = url.replace("file:///", "").replace("file://\\", "").replace("file://", "").replace(/\//g, "\\");
} else {
address = url.replace("file://", "");
}
// 拖入文件名包含 `)` 、`(` 的文件以 `file://` 插入后链接解析错误 https://github.com/siyuan-note/siyuan/issues/5786
address = address.replace(/\\\)/g, ")").replace(/\\\(/g, "(");
if (type === "app") {
useShell("openPath", address);
} else if (type === "folder") {
if ("windows" === window.siyuan.config.system.os) {
if (!address.startsWith("\\\\")) { // \\ 开头的路径是 Windows 网络共享路径 https://github.com/siyuan-note/siyuan/issues/5980
// Windows 端打开本地文件所在位置失效 https://github.com/siyuan-note/siyuan/issues/5808
address = address.replace(/\\\\/g, "\\");
}
}
useShell("showItemInFolder", address);
}
/// #endif
};