1
1
import { App , ItemView , Modal , Notice , requireApiVersion , Setting , TAbstractFile , TFile } from 'obsidian' ;
2
2
3
+ const width = 400 ;
4
+ const height = 400 ;
5
+ const between = 40 ;
6
+
3
7
/**
4
8
* 弹窗确认要添加到Canvas的文件
5
9
*/
@@ -29,13 +33,18 @@ export class AddToCanvasConfirmModal extends Modal {
29
33
. setCta ( )
30
34
. onClick ( async ( ) => {
31
35
this . close ( ) ;
32
- let targetFile = this . app . vault . getAbstractFileByPath ( this . targetFilePath ) ;
33
- if ( targetFile == null ) {
34
- console . log ( "can not find " + this . targetFilePath )
35
- targetFile = await this . app . vault . create ( this . targetFilePath , "" ) ;
36
+ let activeFile = this . app . workspace . getActiveFile ( ) ;
37
+
38
+ // 如果当前活动文档与目标文档不同,则尝试打开目标文档
39
+ if ( activeFile == null || this . targetFilePath != activeFile . path ) {
40
+ let targetFile = this . app . vault . getAbstractFileByPath ( this . targetFilePath ) ;
41
+ if ( targetFile == null ) {
42
+ console . log ( "can not find " + this . targetFilePath )
43
+ targetFile = await this . app . vault . create ( this . targetFilePath , "" ) ;
44
+ }
45
+ // 如果打开的不是目标文件,则尝试在新窗口打开;如果没有打开任何文件,则在当前窗口打开
46
+ await this . app . workspace . getLeaf ( activeFile != null ) . openFile ( targetFile as TFile ) ;
36
47
}
37
- console . log ( targetFile ) ;
38
- await this . app . workspace . getLeaf ( true ) . openFile ( targetFile as TFile ) ;
39
48
40
49
// Conditions to check
41
50
const canvasView = app . workspace . getActiveViewOfType ( ItemView ) ;
@@ -48,21 +57,21 @@ export class AddToCanvasConfirmModal extends Modal {
48
57
let file = this . resultArr [ key ] as TFile ;
49
58
let tempChildNode ;
50
59
if ( ! requireApiVersion ( "1.1.10" ) ) {
51
- tempChildNode = canvas . createFileNode ( file , "" , { x : 20 + ( 220 * idx ) , y : 20 , height : 200 , width : 200 } , true ) ;
60
+ tempChildNode = canvas . createFileNode ( file , "" , { x : between + ( ( width + between ) * idx ) , y : between , height : height , width : width } , true ) ;
52
61
} else {
53
62
tempChildNode = canvas . createFileNode ( {
54
63
file : file ,
55
64
pos : {
56
- x : 20 + ( 220 * idx ) ,
57
- y : 20 ,
58
- width : 200 ,
59
- height : 200
65
+ x : between + ( ( width + between ) * idx ) ,
66
+ y : between ,
67
+ width : width ,
68
+ height : height
60
69
} ,
61
70
size : {
62
- x : 20 ,
63
- y : 20 ,
64
- width : 200 ,
65
- height : 200
71
+ x : between ,
72
+ y : between ,
73
+ width : width ,
74
+ height : height
66
75
} ,
67
76
save : true ,
68
77
focus : false ,
0 commit comments