1
1
import { knownFolders , path , File , Utils , Image , Screen } from '@nativescript/core' ;
2
2
// @ts -ignore
3
- import { ImageAsset } from '@nativescript/canvas' ;
3
+ import { ImageAsset , TextDecoder } from '@nativescript/canvas' ;
4
4
import { HTMLElement } from './HTMLElement' ;
5
5
import { Svg } from '@nativescript/canvas-svg' ;
6
6
@@ -169,64 +169,102 @@ export class HTMLImageElement extends HTMLElement {
169
169
170
170
_load ( ) {
171
171
this . _loading = true ;
172
+ const async = this . decoding !== 'sync' ;
172
173
if ( this . src && typeof this . src === 'string' ) {
173
174
if ( this . src . startsWith ( 'blob:nativescript/' ) ) {
174
175
const data = ( < any > URL ) . InternalAccessor . getData ( this . src ) ;
175
176
const buffer = ( < any > Blob ) . InternalAccessor . getBuffer ( data . blob ) ;
176
177
177
178
let isSvg = data ?. type ?. indexOf ( 'svg' ) > - 1 ;
178
- let src ;
179
- let d ;
180
- if ( ! isSvg ) {
181
- try {
182
- d = new TextDecoder ( ) ;
183
- src = d . decode ( buffer ) ;
184
- if ( typeof src === 'string' ) {
185
- isSvg = src . indexOf ( '<svg' ) > - 1 ;
186
- }
187
- } catch ( error ) { }
188
- }
179
+ const d = new TextDecoder ( ) ;
189
180
190
- if ( isSvg ) {
191
- d = d ?? new TextDecoder ( ) ;
192
- src = src ?? d . decode ( buffer ) ;
193
- Svg . fromSrc ( src )
194
- . then ( ( svg ) => {
195
- const data = svg . data ;
196
- return this . _asset . loadFromBytes ( svg . width , svg . height , data as any ) ;
181
+ if ( async ) {
182
+ d . decodeAsync ( buffer )
183
+ . then ( ( res ) => {
184
+ if ( typeof res === 'string' ) {
185
+ isSvg = res . indexOf ( '< svg' ) > - 1 ;
186
+ }
187
+ return isSvg ? res : null ;
197
188
} )
198
- . then ( ( done : boolean ) => {
199
- this . width = this . _asset . width ;
200
- this . height = this . _asset . height ;
201
- this . complete = done ;
202
- this . _loading = false ;
203
- this . _dispatchDecode ( done ) ;
189
+ . then ( ( res ) => {
190
+ if ( typeof res === 'string' ) {
191
+ return Svg . fromSrc ( res )
192
+ . then ( ( svg ) => {
193
+ const data = svg . data ;
194
+ return this . _asset . loadFromBytes ( svg . width , svg . height , data as any ) ;
195
+ } )
196
+ . then ( ( done : boolean ) => {
197
+ this . width = this . _asset . width ;
198
+ this . height = this . _asset . height ;
199
+ this . complete = done ;
200
+ this . _loading = false ;
201
+ this . _dispatchDecode ( done ) ;
202
+ } ) ;
203
+ }
204
204
} )
205
205
. catch ( ( e ) => {
206
- this . dispatchEvent ( { type : 'error' , target : this , e } ) ;
207
- this . _onerror ?.( ) ;
208
- this . _loading = false ;
209
- this . _dispatchDecode ( ) ;
206
+ this . _asset
207
+ . loadFromEncodedBytes ( buffer )
208
+ . then ( ( done : boolean ) => {
209
+ this . width = this . _asset . width ;
210
+ this . height = this . _asset . height ;
211
+ this . complete = done ;
212
+ this . _loading = false ;
213
+ this . _dispatchDecode ( done ) ;
214
+ } )
215
+ . catch ( ( e ) => {
216
+ this . dispatchEvent ( { type : 'error' , target : this , error : e , e } ) ;
217
+ this . _onerror ?.( ) ;
218
+ this . _loading = false ;
219
+ this . _dispatchDecode ( ) ;
220
+ } ) ;
210
221
} ) ;
211
- return ;
212
222
} else {
213
- this . _asset
214
- . loadFromEncodedBytes ( buffer )
215
- . then ( ( done : boolean ) => {
216
- this . width = this . _asset . width ;
217
- this . height = this . _asset . height ;
218
- this . complete = done ;
219
- this . _loading = false ;
220
- this . _dispatchDecode ( done ) ;
221
- } )
222
- . catch ( ( e ) => {
223
- this . dispatchEvent ( { type : 'error' , target : this , e } ) ;
224
- this . _onerror ?.( ) ;
225
- this . _loading = false ;
226
- this . _dispatchDecode ( ) ;
227
- } ) ;
228
- return ;
223
+ try {
224
+ const src = d . decode ( buffer ) ;
225
+ if ( typeof src === 'string' ) {
226
+ isSvg = src . indexOf ( '<svg' ) > - 1 ;
227
+ }
228
+
229
+ const svg = Svg . fromSrcSync ( this . src ) ;
230
+
231
+ if ( svg ) {
232
+ if ( this . _asset . loadFromBytesSync ( svg . width , svg . height , svg . data as any ) ) {
233
+ this . width = this . _asset . width ;
234
+ this . height = this . _asset . height ;
235
+ this . complete = true ;
236
+ this . _loading = false ;
237
+ this . _dispatchDecode ( true ) ;
238
+ } else {
239
+ this . dispatchEvent ( { type : 'error' , target : this } ) ;
240
+ this . _onerror ?.( ) ;
241
+ this . _loading = false ;
242
+ this . _dispatchDecode ( ) ;
243
+ }
244
+ } else {
245
+ const loaded = this . _asset . fromFileSync ( src ) ;
246
+ if ( loaded ) {
247
+ this . width = this . _asset . width ;
248
+ this . height = this . _asset . height ;
249
+ this . complete = true ;
250
+ this . _loading = false ;
251
+ this . _dispatchDecode ( true ) ;
252
+ } else {
253
+ this . dispatchEvent ( { type : 'error' , target : this } ) ;
254
+ this . _onerror ?.( ) ;
255
+ this . _loading = false ;
256
+ this . _dispatchDecode ( ) ;
257
+ }
258
+ }
259
+ } catch ( e ) {
260
+ this . dispatchEvent ( { type : 'error' , target : this , error : e , e } ) ;
261
+ this . _onerror ?.( ) ;
262
+ this . _loading = false ;
263
+ this . _dispatchDecode ( ) ;
264
+ }
229
265
}
266
+
267
+ return ;
230
268
}
231
269
232
270
if ( this . src . startsWith && this . src . startsWith ( 'data:' ) ) {
@@ -304,7 +342,6 @@ export class HTMLImageElement extends HTMLElement {
304
342
}
305
343
306
344
this . dispatchEvent ( { type : 'loading' , target : this } ) ;
307
- let async = this . decoding !== 'sync' ;
308
345
if ( this . src . startsWith ( 'http' ) ) {
309
346
if ( ! async ) {
310
347
const loaded = this . _asset . fromUrlSync ( this . src ) ;
@@ -315,11 +352,29 @@ export class HTMLImageElement extends HTMLElement {
315
352
this . complete = true ;
316
353
success = true ;
317
354
} else {
318
- this . dispatchEvent ( { type : 'error' , target : this } ) ;
319
- this . _onerror ?.( ) ;
355
+ // try svg ?
356
+ const svg = Svg . fromSrcSync ( this . src ) ;
357
+
358
+ if ( svg ) {
359
+ if ( this . _asset . loadFromBytesSync ( svg . width , svg . height , svg . data as any ) ) {
360
+ this . width = this . _asset . width ;
361
+ this . height = this . _asset . height ;
362
+ this . complete = true ;
363
+ this . _loading = false ;
364
+ this . _dispatchDecode ( true ) ;
365
+ } else {
366
+ this . dispatchEvent ( { type : 'error' , target : this } ) ;
367
+ this . _onerror ?.( ) ;
368
+ this . _loading = false ;
369
+ this . _dispatchDecode ( success ) ;
370
+ }
371
+ } else {
372
+ this . dispatchEvent ( { type : 'error' , target : this } ) ;
373
+ this . _onerror ?.( ) ;
374
+ this . _loading = false ;
375
+ this . _dispatchDecode ( success ) ;
376
+ }
320
377
}
321
- this . _loading = false ;
322
- this . _dispatchDecode ( success ) ;
323
378
} else {
324
379
this . _asset
325
380
. fromUrl ( this . src )
@@ -331,10 +386,24 @@ export class HTMLImageElement extends HTMLElement {
331
386
this . _dispatchDecode ( true ) ;
332
387
} )
333
388
. catch ( ( e ) => {
334
- this . dispatchEvent ( { type : 'error' , target : this , error : e } ) ;
335
- this . _onerror ?.( ) ;
336
- this . _loading = false ;
337
- this . _dispatchDecode ( ) ;
389
+ Svg . fromSrc ( this . src )
390
+ . then ( ( svg ) => {
391
+ const data = svg . data ;
392
+ return this . _asset . loadFromBytes ( svg . width , svg . height , data as any ) ;
393
+ } )
394
+ . then ( ( done : boolean ) => {
395
+ this . width = this . _asset . width ;
396
+ this . height = this . _asset . height ;
397
+ this . complete = done ;
398
+ this . _loading = false ;
399
+ this . _dispatchDecode ( done ) ;
400
+ } )
401
+ . catch ( ( _ ) => {
402
+ this . dispatchEvent ( { type : 'error' , target : this , error : e } ) ;
403
+ this . _onerror ?.( ) ;
404
+ this . _loading = false ;
405
+ this . _dispatchDecode ( ) ;
406
+ } ) ;
338
407
} ) ;
339
408
}
340
409
} else {
@@ -352,10 +421,28 @@ export class HTMLImageElement extends HTMLElement {
352
421
this . _loading = false ;
353
422
this . _dispatchDecode ( true ) ;
354
423
} else {
355
- this . dispatchEvent ( { type : 'error' , target : this } ) ;
356
- this . _onerror ?.( ) ;
357
- this . _loading = false ;
358
- this . _dispatchDecode ( ) ;
424
+ // try svg ?
425
+ const svg = Svg . fromSrcSync ( this . src ) ;
426
+
427
+ if ( svg ) {
428
+ if ( this . _asset . loadFromBytesSync ( svg . width , svg . height , svg . data as any ) ) {
429
+ this . width = this . _asset . width ;
430
+ this . height = this . _asset . height ;
431
+ this . complete = true ;
432
+ this . _loading = false ;
433
+ this . _dispatchDecode ( true ) ;
434
+ } else {
435
+ this . dispatchEvent ( { type : 'error' , target : this } ) ;
436
+ this . _onerror ?.( ) ;
437
+ this . _loading = false ;
438
+ this . _dispatchDecode ( ) ;
439
+ }
440
+ } else {
441
+ this . dispatchEvent ( { type : 'error' , target : this } ) ;
442
+ this . _onerror ?.( ) ;
443
+ this . _loading = false ;
444
+ this . _dispatchDecode ( ) ;
445
+ }
359
446
}
360
447
} else {
361
448
this . _asset
@@ -368,10 +455,24 @@ export class HTMLImageElement extends HTMLElement {
368
455
this . _dispatchDecode ( true ) ;
369
456
} )
370
457
. catch ( ( e ) => {
371
- this . dispatchEvent ( { type : 'error' , target : this , e } ) ;
372
- this . _onerror ?.( ) ;
373
- this . _loading = false ;
374
- this . _dispatchDecode ( ) ;
458
+ Svg . fromSrc ( this . src )
459
+ . then ( ( svg ) => {
460
+ const data = svg . data ;
461
+ return this . _asset . loadFromBytes ( svg . width , svg . height , data as any ) ;
462
+ } )
463
+ . then ( ( done : boolean ) => {
464
+ this . width = this . _asset . width ;
465
+ this . height = this . _asset . height ;
466
+ this . complete = done ;
467
+ this . _loading = false ;
468
+ this . _dispatchDecode ( done ) ;
469
+ } )
470
+ . catch ( ( _ ) => {
471
+ this . dispatchEvent ( { type : 'error' , target : this , e } ) ;
472
+ this . _onerror ?.( ) ;
473
+ this . _loading = false ;
474
+ this . _dispatchDecode ( ) ;
475
+ } ) ;
375
476
} ) ;
376
477
}
377
478
}
0 commit comments