1- /**
2- * Sample plugin.
3- */
1+ // load zotero api
2+ var script = document . createElement ( 'script' ) ;
3+ script . src = "https://unpkg.com/zotero-api-client" ;
4+ document . head . appendChild ( script ) ;
5+
6+ var zoteroApi ; // will be set after load
7+ const str_token = '::' ;
8+
9+ /* get author information */
10+ function get_author ( authors ) {
11+ if ( typeof authors === 'undefined' || authors . length <= 0 )
12+ return "Unknown author"
13+ if ( authors . length >= 3 )
14+ return authors [ 0 ] . lastName + " et.al."
15+ if ( authors . length == 2 )
16+ return authors [ 0 ] . lastName + " & " + authors [ 1 ] . lastName
17+ return authors [ 0 ] . lastName ;
18+ }
19+
20+ /* get year information */
21+ function get_year ( date ) {
22+ function filter_year ( tokens ) {
23+ for ( const t of tokens ) {
24+ y = parseInt ( t )
25+ if ( ! isNaN ( y ) && y > 50 && y < 10000 )
26+ return t
27+ }
28+ }
29+ if ( typeof date === 'undefined' || date == "" )
30+ return "Unknown year"
31+ return filter_year ( date . split ( / [ - / , ] / ) )
32+ }
33+
34+ /* get details*/
35+ function get_details ( collection_name , title ) {
36+ return collection_name + str_token + title
37+ }
38+
39+ /* Retrieve library from Zotero API */
40+ async function retreive ( ApiKey , Uid , callback ) {
41+ counter = 1
42+ drawio_tags = [ ] ;
43+ const myapi = zoteroApi ( ApiKey , {
44+ 'limit' : 100
45+ } ) . library ( 'user' , Uid )
46+
47+ try {
48+ const collectionsRes = await myapi . collections ( ) . get ( ) ;
49+
50+ collection_names = { }
51+
52+ console . log ( collectionsRes )
53+ promises = [ ]
54+
55+ // Retrieve collection information
56+ for ( const [ i , c ] of collectionsRes . raw . entries ( ) ) {
57+ console . log ( c )
58+ collection_names [ c . key ] = c . data . name
59+
60+ // Add promises to request for the items in collection
61+ promises . push ( new Promise ( ( resolve , reject ) => {
62+ const itemRes = myapi . collections ( c . key ) . items ( ) . get ( )
63+ resolve ( itemRes )
64+ } ) )
65+ console . log ( c . data . name )
66+ }
67+
68+ // Append the content when the data available
69+ for ( const p of promises ) {
70+ p . then ( ( itemRes ) => {
71+ const items = itemRes . getData ( )
72+ items . forEach ( item => {
73+ if ( item . itemType != "attachment" ) {
74+ console . log ( item )
75+ number = ( typeof item . callNumber === 'undefined' ) ? ( '' ) : ( String ( item . callNumber ) )
76+
77+ item . collections . forEach ( ( ckey ) => {
78+ collection_name = collection_names [ ckey ]
79+
80+ // Generate metadata for drawio plugin
81+ kname = '[' + number + str_token
82+ + get_details ( collection_name , item . title ) + str_token
83+ + get_author ( item . creators ) + " "
84+ + get_year ( item . date ) + str_token
85+ + item . key + ']'
86+ // \u4e00-\u9fa5 is used to match Chinese character
87+ kname = kname . replace ( / [ ^ a - z A - Z 0 - 9 / . , & : \] \[ \u4e00 - \u9fa5 ] / g, "_" )
88+ drawio_tags . push ( kname )
89+ } )
90+ counter += 1
91+ }
92+ } )
93+ } )
94+ }
95+
96+ // Wait for all promise to finish no matter if it succeeded or rejected
97+ Promise . allSettled ( promises ) . then ( ( result ) => {
98+ callback ( drawio_tags )
99+ } )
100+ }
101+ catch ( err ) {
102+ console . log ( err )
103+ alert ( "Error: " + String ( err ) + '\nPlease check the UID and API key!' )
104+ }
105+ }
106+
107+ function loadZoteroTags ( ui ) {
108+ // disable button
109+ let action = ui . actions . get ( 'reloadZotero' ) ;
110+ action . enabled = false ;
111+
112+ // load from Zotero Api and add them to the list of tags (tags for the root)
113+ config = JSON . parse ( localStorage . getItem ( ".configuration" ) ) ;
114+ zotero_uid = parseInt ( config [ 'zotero_uid' ] , 10 ) ;
115+ zotero_api_key = config [ 'zotero_api_key' ] ;
116+
117+ graph = ui . editor . graph ;
118+ root = graph . model . getRoot ( ) ;
119+
120+ retreive ( zotero_api_key , zotero_uid , ( citations ) => {
121+ graph . addTagsForCells ( [ root ] , citations )
122+ action . enabled = true ;
123+ } ) ;
124+ }
125+
126+ function setupZoretoMenu ( ui ) {
127+ // Adds resource for action
128+ mxResources . parse ( 'reloadZotero=Reload Zotero...' ) ;
129+
130+ // Adds action
131+ ui . actions . addAction ( 'reloadZotero' , ( ) => {
132+ loadZoteroTags ( ui ) ;
133+ } ) ;
134+
135+ // Adds menu item for refreshing
136+ let menu = ui . menus . get ( 'extras' ) ;
137+ let oldFunct = menu . funct ;
138+
139+ menu . funct = function ( menu , parent )
140+ {
141+ oldFunct . apply ( this , arguments ) ;
142+ ui . menus . addMenuItems ( menu , [ '-' , 'reloadZotero' ] , parent , ) ;
143+ } ;
144+ }
145+
146+ script . onload = ( ) => {
147+ zoteroApi = ZoteroApiClient . default ;
4148Draw . loadPlugin ( function ( ui ) {
149+ setupZoretoMenu ( ui ) ;
5150
6151 // Adds numbered toggle property
7152 Editor . commonVertexProperties . push ( {
@@ -16,7 +161,6 @@ Draw.loadPlugin(function (ui) {
16161
17162 var graph = ui . editor . graph ;
18163 var enabled = true ;
19- var counter = 0 ;
20164
21165 var graphViewResetValidationState = graph . view . resetValidationState ;
22166
@@ -141,4 +285,5 @@ Draw.loadPlugin(function (ui) {
141285 if ( ui . getCurrentFile ( ) != null ) {
142286 graph . refresh ( ) ;
143287 }
144- } ) ;
288+ } ) ;
289+ } ;
0 commit comments