@@ -2,61 +2,69 @@ import "@logseq/libs";
2
2
import { checkUpdates } from "./CheckUpdatesUtils" ;
3
3
import { checkAndUploadBlock } from "./ImageUploadUtils" ;
4
4
5
- async function main ( ) {
6
- setTimeout ( ( ) => {
7
- checkUpdates ( ) ;
8
- } , 5000 ) ;
9
-
5
+ async function getGraphPath ( ) {
10
6
const graphInfo = await logseq . App . getCurrentGraph ( ) ;
11
7
const graphPath = graphInfo ?. path ;
12
8
if ( ! graphPath ) {
13
9
const errorMsg = "Failed to get graph root path." ;
14
- console . error ( ' Error:' , errorMsg ) ;
10
+ console . error ( " Error:" , errorMsg ) ;
15
11
logseq . App . showMsg ( errorMsg , "error" ) ;
16
- return
17
- } else {
18
- // Adding a context menu item to upload images in the block.
19
- logseq . Editor . registerBlockContextMenuItem ( "Upload image" , async ( e ) => {
20
- const block = await logseq . Editor . getBlock ( e . uuid ) ;
21
- if ( block && block . content ) {
12
+ return ;
13
+ }
14
+ return graphPath ;
15
+ }
16
+
17
+ async function main ( ) {
18
+ setTimeout ( ( ) => {
19
+ checkUpdates ( ) ;
20
+ } , 5000 ) ;
21
+
22
+ // Adding a context menu item to upload images in the block.
23
+ logseq . Editor . registerBlockContextMenuItem ( "Upload image" , async ( e ) => {
24
+ const block = await logseq . Editor . getBlock ( e . uuid ) ;
25
+ if ( block && block . content ) {
26
+ const graphPath = await getGraphPath ( ) ;
27
+ if ( graphPath ) {
22
28
checkAndUploadBlock ( block , graphPath ) ;
23
29
}
24
- } )
30
+ }
31
+ } ) ;
25
32
26
- // Adding pasting event listener to upload images in the block.
27
- // Note: Paste event won't trigger when pastring an image, so we're using the keydown event instead.
28
- parent . document . addEventListener ( "keydown" , async ( e ) => {
29
- let autoUploading = logseq . settings ?. autoUploading ?? true ;
30
- if ( ! autoUploading ) {
31
- return
32
- }
33
-
34
- if ( e . ctrlKey || e . metaKey ) {
35
- if ( e . code === "KeyV" ) {
36
- let currentBlock = await logseq . Editor . getCurrentBlock ( ) ;
37
- if ( currentBlock ) {
38
- let uuid = currentBlock . uuid ;
39
- let checkLater = function ( ) {
40
- setTimeout ( async ( ) => {
41
- let isEditing = await logseq . Editor . checkEditing ( ) ;
42
- if ( typeof isEditing === "string" && isEditing === uuid ) {
43
- // logseq.App.showMsg("Still editing, check it later.", "warning");
44
- checkLater ( ) ;
45
- } else {
46
- let block = await logseq . Editor . getBlock ( uuid ) ;
47
- if ( block && block . content ) {
48
- // logseq.App.showMsg(block.content);
33
+ // Adding pasting event listener to upload images in the block.
34
+ // Note: Paste event won't trigger when pastring an image, so we're using the keydown event instead.
35
+ parent . document . addEventListener ( "keydown" , async ( e ) => {
36
+ let autoUploading = logseq . settings ?. autoUploading ?? true ;
37
+ if ( ! autoUploading ) {
38
+ return ;
39
+ }
40
+
41
+ if ( e . ctrlKey || e . metaKey ) {
42
+ if ( e . code === "KeyV" ) {
43
+ let currentBlock = await logseq . Editor . getCurrentBlock ( ) ;
44
+ if ( currentBlock ) {
45
+ let uuid = currentBlock . uuid ;
46
+ let checkLater = function ( ) {
47
+ setTimeout ( async ( ) => {
48
+ let isEditing = await logseq . Editor . checkEditing ( ) ;
49
+ if ( typeof isEditing === "string" && isEditing === uuid ) {
50
+ // logseq.App.showMsg("Still editing, check it later.", "warning");
51
+ checkLater ( ) ;
52
+ } else {
53
+ let block = await logseq . Editor . getBlock ( uuid ) ;
54
+ if ( block && block . content ) {
55
+ const graphPath = await getGraphPath ( ) ;
56
+ if ( graphPath ) {
49
57
checkAndUploadBlock ( block , graphPath ) ;
50
58
}
51
59
}
52
- } , 1000 ) ;
53
- }
54
- checkLater ( ) ;
55
- }
60
+ }
61
+ } , 1000 ) ;
62
+ } ;
63
+ checkLater ( ) ;
56
64
}
57
65
}
58
- } ) ;
59
- }
66
+ }
67
+ } ) ;
60
68
}
61
69
62
- logseq . ready ( main ) . catch ( console . error ) ;
70
+ logseq . ready ( main ) . catch ( console . error ) ;
0 commit comments