1
- import { Element } from " ./Element" ;
2
- import { knownFolders , path , File , ImageSource } from " @nativescript/core" ;
3
- import { ImageAsset } from '@nativescript/canvas' ;
1
+ import { Element } from ' ./Element' ;
2
+ import { knownFolders , path , File , ImageSource } from ' @nativescript/core' ;
3
+ import { ImageAsset } from '@nativescript/canvas' ;
4
4
5
5
const background_queue = global . isIOS ? dispatch_get_global_queue ( qos_class_t . QOS_CLASS_DEFAULT , 0 ) : undefined ;
6
6
const main_queue = global . isIOS ? dispatch_get_current_queue ( ) : undefined ;
7
7
declare var NSUUID , java , NSData , android ;
8
8
const b64Extensions = {
9
- "/" : " jpg" ,
10
- i : " png" ,
11
- R : " gif" ,
12
- U : " webp" ,
9
+ '/' : ' jpg' ,
10
+ i : ' png' ,
11
+ R : ' gif' ,
12
+ U : ' webp' ,
13
13
} ;
14
14
15
15
function b64WithoutPrefix ( b64 ) {
16
- return b64 . split ( "," ) [ 1 ] ;
16
+ return b64 . split ( ',' ) [ 1 ] ;
17
17
}
18
18
19
19
function getMIMEforBase64String ( b64 ) {
20
20
let input = b64 ;
21
- if ( b64 . includes ( "," ) ) {
21
+ if ( b64 . includes ( ',' ) ) {
22
22
input = b64WithoutPrefix ( b64 ) ;
23
23
}
24
24
const first = input . charAt ( 0 ) ;
25
25
const mime = b64Extensions [ first ] ;
26
26
if ( ! mime ) {
27
- throw new Error ( " Unknown Base64 MIME type: " + b64 ) ;
27
+ throw new Error ( ' Unknown Base64 MIME type: ' + b64 ) ;
28
28
}
29
29
return mime ;
30
30
}
@@ -69,20 +69,18 @@ export class HTMLImageElement extends Element {
69
69
set complete ( value ) {
70
70
this . _complete = value ;
71
71
if ( value ) {
72
- this . emitter . emit ( " load" , this ) ;
72
+ this . emitter . emit ( ' load' , this ) ;
73
73
this . onload ( ) ;
74
74
}
75
75
}
76
76
77
-
78
77
constructor ( props ?) {
79
- super ( " image" ) ;
78
+ super ( ' image' ) ;
80
79
this . _asset = new ImageAsset ( ) ;
81
80
this . __id = getUUID ( ) ;
82
81
// this._load = this._load.bind(this);
83
- this . _onload = ( ) => {
84
- } ;
85
- if ( props !== null && typeof props === "object" ) {
82
+ this . _onload = ( ) => { } ;
83
+ if ( props !== null && typeof props === 'object' ) {
86
84
this . src = props . localUri ;
87
85
this . width = props . width ;
88
86
this . height = props . height ;
@@ -92,14 +90,10 @@ export class HTMLImageElement extends Element {
92
90
93
91
_load ( ) {
94
92
if ( this . src ) {
95
- if (
96
- typeof this . src === "string" &&
97
- this . src . startsWith &&
98
- this . src . startsWith ( "data:" )
99
- ) {
93
+ if ( typeof this . src === 'string' && this . src . startsWith && this . src . startsWith ( 'data:' ) ) {
100
94
// is base64 - convert and try again;
101
95
this . _base64 = this . src ;
102
- const base64result = this . src . split ( "," ) [ 1 ] ;
96
+ const base64result = this . src . split ( ',' ) [ 1 ] ;
103
97
if ( ! base64result ) {
104
98
return ;
105
99
}
@@ -109,25 +103,16 @@ export class HTMLImageElement extends Element {
109
103
const dir = knownFolders . documents ( ) . path ;
110
104
if ( global . isIOS ) {
111
105
dispatch_async ( background_queue , ( ) => {
112
- this . localUri = path . join (
113
- dir ,
114
- `${ getUUID ( ) } -b64image.${ MIME } `
115
- ) ;
106
+ this . localUri = path . join ( dir , `${ getUUID ( ) } -b64image.${ MIME } ` ) ;
116
107
const file = File . fromPath ( this . localUri ) ;
117
- const toWrite = NSData . alloc ( ) . initWithBase64EncodedStringOptions (
118
- base64result ,
119
- 0
120
- ) ;
108
+ const toWrite = NSData . alloc ( ) . initWithBase64EncodedStringOptions ( base64result , 0 ) ;
121
109
let hasError = false ;
122
110
file . writeSync ( toWrite , ( error ) => {
123
111
hasError = true ;
124
112
if ( ( global as any ) . __debug_browser_polyfill_image ) {
125
- console . log (
126
- `nativescript-browser-polyfill: Error:` ,
127
- error . message
128
- ) ;
113
+ console . log ( `nativescript-browser-polyfill: Error:` , error . message ) ;
129
114
}
130
- this . emitter . emit ( " error" , { target : this , error} ) ;
115
+ this . emitter . emit ( ' error' , { target : this , error } ) ;
131
116
} ) ;
132
117
if ( ! hasError ) {
133
118
dispatch_async ( main_queue , ( ) => {
@@ -138,33 +123,34 @@ export class HTMLImageElement extends Element {
138
123
}
139
124
if ( global . isAndroid ) {
140
125
const ref = new WeakRef ( this ) ;
141
- ( com as any ) . github . triniwiz . async . Async . base64ToFile ( base64result , dir + '/' , new ( com as any ) . github . triniwiz . async . Async . Callback ( {
142
- success ( response ) {
143
- const owner = ref . get ( ) ;
144
- if ( owner ) {
145
- owner . localUri = response . toString ( ) ;
146
- owner . _load ( ) ;
147
- }
148
- } ,
149
- error ( error , message ) {
150
- if ( ( global as any ) . __debug_browser_polyfill_image ) {
151
- console . log (
152
- `nativescript-browser-polyfill: Error:` ,
153
- message ,
154
- ) ;
155
- }
156
- ref . get ( ) ?. emitter . emit ( "error" , { target : ref . get ( ) , message} ) ;
157
- }
158
- } ) )
126
+ ( com as any ) . github . triniwiz . async . Async2 . Base64 . base64ToFile (
127
+ base64result ,
128
+ dir + '/' ,
129
+ new ( com as any ) . github . triniwiz . async . Async2 . Base64 . Callback ( {
130
+ success ( response ) {
131
+ const owner = ref . get ( ) ;
132
+ if ( owner ) {
133
+ owner . localUri = response . toString ( ) ;
134
+ owner . _load ( ) ;
135
+ }
136
+ } ,
137
+ error ( error , message ) {
138
+ if ( ( global as any ) . __debug_browser_polyfill_image ) {
139
+ console . log ( `nativescript-browser-polyfill: Error:` , message ) ;
140
+ }
141
+ const owner = ref . get ( ) ;
142
+ if ( owner ) {
143
+ owner . emitter . emit ( 'error' , { target : ref . get ( ) , message } ) ;
144
+ }
145
+ } ,
146
+ } )
147
+ ) ;
159
148
}
160
149
} catch ( error ) {
161
150
if ( ( global as any ) . __debug_browser_polyfill_image ) {
162
- console . log (
163
- `nativescript-browser-polyfill: Error:` ,
164
- error . message
165
- ) ;
151
+ console . log ( `nativescript-browser-polyfill: Error:` , error . message ) ;
166
152
}
167
- this . emitter . emit ( " error" , { target : this , error} ) ;
153
+ this . emitter . emit ( ' error' , { target : this , error } ) ;
168
154
}
169
155
} ) ( ) ;
170
156
return ;
@@ -181,14 +167,15 @@ export class HTMLImageElement extends Element {
181
167
.catch(e => {
182
168
this.emitter.emit("error", {target: this});
183
169
});*/
184
- this . _asset . loadFileAsync ( this . src )
170
+ this . _asset
171
+ . loadFileAsync ( this . src )
185
172
. then ( ( ) => {
186
173
this . width = this . _asset . width ;
187
174
this . height = this . _asset . height ;
188
175
this . complete = true ;
189
176
} )
190
- . catch ( e => {
191
- this . emitter . emit ( " error" , { target : this } ) ;
177
+ . catch ( ( e ) => {
178
+ this . emitter . emit ( ' error' , { target : this } ) ;
192
179
} ) ;
193
180
} else {
194
181
/*ImageSource.fromFile(this.src)
@@ -201,8 +188,7 @@ export class HTMLImageElement extends Element {
201
188
.catch(e => {
202
189
this.emitter.emit("error", {target: this});
203
190
});*/
204
-
205
- /* this._asset.loadFileAsync(this.src)
191
+ /* this._asset.loadFileAsync(this.src)
206
192
.then(() => {
207
193
this.width = this._asset.width;
208
194
this.height = this._asset.height;
@@ -216,4 +202,3 @@ export class HTMLImageElement extends Element {
216
202
}
217
203
}
218
204
}
219
-
0 commit comments