Skip to content

Commit 9a56354

Browse files
committed
fix(polyfill): update
1 parent dfcff44 commit 9a56354

File tree

7 files changed

+1353
-1365
lines changed

7 files changed

+1353
-1365
lines changed
Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
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';
44

55
const background_queue = global.isIOS ? dispatch_get_global_queue(qos_class_t.QOS_CLASS_DEFAULT, 0) : undefined;
66
const main_queue = global.isIOS ? dispatch_get_current_queue() : undefined;
77
declare var NSUUID, java, NSData, android;
88
const b64Extensions = {
9-
"/": "jpg",
10-
i: "png",
11-
R: "gif",
12-
U: "webp",
9+
'/': 'jpg',
10+
i: 'png',
11+
R: 'gif',
12+
U: 'webp',
1313
};
1414

1515
function b64WithoutPrefix(b64) {
16-
return b64.split(",")[1];
16+
return b64.split(',')[1];
1717
}
1818

1919
function getMIMEforBase64String(b64) {
2020
let input = b64;
21-
if (b64.includes(",")) {
21+
if (b64.includes(',')) {
2222
input = b64WithoutPrefix(b64);
2323
}
2424
const first = input.charAt(0);
2525
const mime = b64Extensions[first];
2626
if (!mime) {
27-
throw new Error("Unknown Base64 MIME type: " + b64);
27+
throw new Error('Unknown Base64 MIME type: ' + b64);
2828
}
2929
return mime;
3030
}
@@ -69,20 +69,18 @@ export class HTMLImageElement extends Element {
6969
set complete(value) {
7070
this._complete = value;
7171
if (value) {
72-
this.emitter.emit("load", this);
72+
this.emitter.emit('load', this);
7373
this.onload();
7474
}
7575
}
7676

77-
7877
constructor(props?) {
79-
super("image");
78+
super('image');
8079
this._asset = new ImageAsset();
8180
this.__id = getUUID();
8281
// 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') {
8684
this.src = props.localUri;
8785
this.width = props.width;
8886
this.height = props.height;
@@ -92,14 +90,10 @@ export class HTMLImageElement extends Element {
9290

9391
_load() {
9492
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:')) {
10094
// is base64 - convert and try again;
10195
this._base64 = this.src;
102-
const base64result = this.src.split(",")[1];
96+
const base64result = this.src.split(',')[1];
10397
if (!base64result) {
10498
return;
10599
}
@@ -109,25 +103,16 @@ export class HTMLImageElement extends Element {
109103
const dir = knownFolders.documents().path;
110104
if (global.isIOS) {
111105
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}`);
116107
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);
121109
let hasError = false;
122110
file.writeSync(toWrite, (error) => {
123111
hasError = true;
124112
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);
129114
}
130-
this.emitter.emit("error", {target: this, error});
115+
this.emitter.emit('error', { target: this, error });
131116
});
132117
if (!hasError) {
133118
dispatch_async(main_queue, () => {
@@ -138,33 +123,34 @@ export class HTMLImageElement extends Element {
138123
}
139124
if (global.isAndroid) {
140125
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+
);
159148
}
160149
} catch (error) {
161150
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);
166152
}
167-
this.emitter.emit("error", {target: this, error});
153+
this.emitter.emit('error', { target: this, error });
168154
}
169155
})();
170156
return;
@@ -181,14 +167,15 @@ export class HTMLImageElement extends Element {
181167
.catch(e => {
182168
this.emitter.emit("error", {target: this});
183169
});*/
184-
this._asset.loadFileAsync(this.src)
170+
this._asset
171+
.loadFileAsync(this.src)
185172
.then(() => {
186173
this.width = this._asset.width;
187174
this.height = this._asset.height;
188175
this.complete = true;
189176
})
190-
.catch(e => {
191-
this.emitter.emit("error", {target: this});
177+
.catch((e) => {
178+
this.emitter.emit('error', { target: this });
192179
});
193180
} else {
194181
/*ImageSource.fromFile(this.src)
@@ -201,8 +188,7 @@ export class HTMLImageElement extends Element {
201188
.catch(e => {
202189
this.emitter.emit("error", {target: this});
203190
});*/
204-
205-
/* this._asset.loadFileAsync(this.src)
191+
/* this._asset.loadFileAsync(this.src)
206192
.then(() => {
207193
this.width = this._asset.width;
208194
this.height = this._asset.height;
@@ -216,4 +202,3 @@ export class HTMLImageElement extends Element {
216202
}
217203
}
218204
}
219-

0 commit comments

Comments
 (0)