@@ -8,7 +8,7 @@ import { getCurrentTheme } from 'src/lib/themes/getCurrentTheme';
8
8
9
9
import { normalizePath , requestUrl , Notice } from 'obsidian' ;
10
10
import { i18n } from '../i18n' ;
11
- import * as path from 'path ' ;
11
+ import { githubAsset } from 'typings/githubAsset ' ;
12
12
13
13
export default class RDKitCore implements ChemCore {
14
14
id : 'rdkit' ;
@@ -24,7 +24,19 @@ export default class RDKitCore implements ChemCore {
24
24
}
25
25
26
26
static async init ( settings : ChemPluginSettings ) {
27
- if ( ! window . RDKit ) window . RDKit = await loadRDKit ( ) ;
27
+ if ( ! window . RDKit ) {
28
+ try {
29
+ window . RDKit = await loadRDKit ( ) ;
30
+ } catch ( e ) {
31
+ try {
32
+ window . RDKit = await loadRDKitUnpkg ( ) ;
33
+ } catch ( e ) {
34
+ throw Error (
35
+ "Initializing rdkit failed: Can't fetch resources from unpkg."
36
+ ) ;
37
+ }
38
+ }
39
+ }
28
40
return new RDKitCore ( settings , window . RDKit ) ;
29
41
}
30
42
@@ -39,7 +51,7 @@ export default class RDKitCore implements ChemCore {
39
51
if ( ! rxn ) return this . logError ( source ) ;
40
52
svgstr = await this . drawReaction ( rxn ) ;
41
53
} else {
42
- const mol = this . core . get_mol ( source ) ;
54
+ const mol = this . core . get_mol ( source , JSON . stringify ( { } ) ) ;
43
55
if ( ! mol ) return this . logError ( source ) ;
44
56
45
57
// https://greglandrum.github.io/rdkit-blog/posts/2024-01-11-using-abbreviations.html
@@ -119,9 +131,13 @@ export default class RDKitCore implements ChemCore {
119
131
120
132
private logError = ( source : string ) => {
121
133
const div = createDiv ( ) ;
122
- div . createDiv ( ) . setText ( i18n . t ( 'errors.source.title' , { source } ) ) ;
134
+ div . createDiv ( 'error-source' ) . setText (
135
+ i18n . t ( 'errors.source.title' , { source } )
136
+ ) ;
123
137
div . createEl ( 'br' ) ;
124
138
div . createDiv ( ) . setText ( i18n . t ( 'errors.rdkit.title' ) ) ;
139
+ div . style . wordBreak = `break-word` ;
140
+ div . style . userSelect = `text` ;
125
141
return div ;
126
142
} ;
127
143
}
@@ -130,77 +146,88 @@ export default class RDKitCore implements ChemCore {
130
146
// Initialize reference: https://github.com/rdkit/rdkit-js/tree/master/typescript
131
147
const loadRDKit = async ( ) => {
132
148
const assetPath = normalizePath (
133
- path . join ( app . vault . configDir , 'plugins' , 'chem' , 'rdkit' )
149
+ [ app . vault . configDir , 'plugins' , 'chem' , 'rdkit' ] . join ( '/ ')
134
150
) ;
135
151
if ( ! ( await app . vault . adapter . exists ( assetPath ) ) ) {
136
- console . log ( assetPath ) ;
137
152
await app . vault . adapter . mkdir ( assetPath ) ;
138
153
}
139
154
140
- const jsPath = path . join ( assetPath , 'RDKit_minimal.js' ) ;
155
+ const jsPath = [ assetPath , 'RDKit_minimal.js' ] . join ( '/ ') ;
141
156
await checkOrDownload ( 'RDKit_minimal.js' ) ;
142
157
143
- const wasmPath = path . join ( assetPath , 'RDKit_minimal.wasm' ) ;
158
+ const wasmPath = [ assetPath , 'RDKit_minimal.wasm' ] . join ( '/ ') ;
144
159
await checkOrDownload ( 'RDKit_minimal.wasm' ) ;
145
160
146
161
const rdkitBundler = document . body . createEl ( 'script' ) ;
147
162
rdkitBundler . type = 'text/javascript' ;
148
163
rdkitBundler . id = 'chem-rdkit-bundler' ;
149
164
rdkitBundler . innerHTML = await app . vault . adapter . read ( jsPath ) ;
150
- // backup rdkitBundler.src = 'https://unpkg.com/@rdkit/rdkit/dist/RDKit_minimal.js'
151
165
152
166
const getWasmURL = async ( ) =>
153
167
URL . createObjectURL (
154
168
new Blob ( [ await app . vault . adapter . readBinary ( wasmPath ) ] , {
155
169
type : 'application/wasm' ,
156
170
} )
157
171
) ;
158
- const url = await getWasmURL ( ) ; //backup: https://unpkg.com/ @rdkit /rdkit/dist/RDKit_minimal.wasm
172
+ const url = await getWasmURL ( ) ;
159
173
const RDKit = await window . initRDKitModule ( {
160
174
locateFile : ( ) => url ,
161
175
} ) ;
162
176
URL . revokeObjectURL ( url ) ;
163
177
return RDKit ;
164
178
} ;
165
179
166
- const fetchAsset = async ( target : string , localPath : string ) => {
167
- let res ;
168
- let data ;
180
+ // See https://github.com/rdkit/rdkit-js/issues/160
181
+ const loadRDKitUnpkg = async ( ) => {
182
+ const rdkitBundler = document . body . createEl ( 'script' ) ;
183
+ new Notice ( 'Fetching RDKit.js from unpkg...' ) ;
184
+
185
+ rdkitBundler . innerHTML = await requestUrl (
186
+ 'https://unpkg.com/@rdkit/rdkit/dist/RDKit_minimal.js'
187
+ ) . text ;
188
+
189
+ const RDKit = await window . initRDKitModule ( {
190
+ locateFile : ( ) =>
191
+ 'https://unpkg.com/@rdkit/rdkit/dist/RDKit_minimal.wasm' ,
192
+ } ) ;
193
+ new Notice ( 'RDKit.js has been successfully loaded.' ) ;
194
+ return RDKit ;
195
+ } ;
169
196
170
- res = requestUrl (
197
+ const fetchAsset = async ( target : string , localPath : string ) => {
198
+ const assetInfo = await requestUrl (
171
199
`https://api.github.com/repos/acylation/obsidian-chem/releases/tags/${
172
200
app . plugins . getPlugin ( 'chem' ) ?. manifest . version ?? '0.4.0'
173
201
} `
174
- ) ;
175
- data = await res . json ;
176
- const asset = data . assets . find ( ( v : any ) => v . name == target ) ;
177
- if ( asset == undefined ) {
178
- throw 'Could not find the online asset!' ;
179
- }
180
- res = requestUrl ( {
202
+ ) . json ;
203
+ const asset = assetInfo . assets . find ( ( a : githubAsset ) => a . name == target ) ;
204
+ if ( asset === undefined ) throw Error ( 'Could not find the online asset!' ) ;
205
+
206
+ const data = await requestUrl ( {
181
207
url : asset . url ,
182
208
headers : { Accept : 'application/octet-stream' } ,
183
- } ) ;
184
- data = await res . arrayBuffer ;
209
+ } ) . arrayBuffer ;
185
210
await app . vault . adapter . writeBinary ( localPath , data ) ;
186
211
} ;
187
212
188
- // TODO: i18n
189
213
const checkOrDownload = async ( target : string ) => {
190
214
const assetPath = normalizePath (
191
- path . join ( app . vault . configDir , 'plugins' , 'chem' , 'rdkit' , target )
215
+ [ app . vault . configDir , 'plugins' , 'chem' , 'rdkit' , target ] . join ( '/' )
192
216
) ;
193
217
194
218
if ( ! ( await app . vault . adapter . exists ( assetPath ) ) ) {
195
219
new Notice ( `Chem: Downloading ${ target } !` , 5000 ) ;
196
220
try {
197
221
await fetchAsset ( target , assetPath ) ;
198
222
new Notice (
199
- `Chem: Resource ${ target } successfully downloaded!` ,
223
+ `Chem: Resource ${ target } successfully downloaded! ✔️ ` ,
200
224
5000
201
225
) ;
202
226
} catch ( error ) {
203
- new Notice ( `Chem: Failed to fetch ${ target } : ` + error ) ;
227
+ new Notice ( `Chem: Failed to fetch ${ target } : ${ error } ❌` ) ;
228
+ throw Error (
229
+ `Failed to fetch resource ${ target } from GitHub release.`
230
+ ) ;
204
231
}
205
232
}
206
233
} ;
0 commit comments