Skip to content

Commit d4b9e1d

Browse files
committed
feat(polyfill): impl URL, expose Element ,update android lib
1 parent 0ba86aa commit d4b9e1d

File tree

8 files changed

+74
-47
lines changed

8 files changed

+74
-47
lines changed

packages/canvas-polyfill/DOM/HTMLImageElement.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,6 @@ export class HTMLImageElement extends Element {
157157
}
158158
if (!this.width || !this.height) {
159159
this.complete = false;
160-
/*ImageSource.fromFile(this.src)
161-
.then((asset) => {
162-
this._imageSource = asset;
163-
this.width = asset.width;
164-
this.height = asset.height;
165-
this.complete = true;
166-
})
167-
.catch(e => {
168-
this.emitter.emit("error", {target: this});
169-
});*/
170160
this._asset
171161
.loadFileAsync(this.src)
172162
.then(() => {
@@ -177,27 +167,6 @@ export class HTMLImageElement extends Element {
177167
.catch((e) => {
178168
this.emitter.emit('error', { target: this });
179169
});
180-
} else {
181-
/*ImageSource.fromFile(this.src)
182-
.then((asset) => {
183-
this._imageSource = asset;
184-
this.width = asset.width;
185-
this.height = asset.height;
186-
this.complete = true;
187-
})
188-
.catch(e => {
189-
this.emitter.emit("error", {target: this});
190-
});*/
191-
/* this._asset.loadFileAsync(this.src)
192-
.then(() => {
193-
this.width = this._asset.width;
194-
this.height = this._asset.height;
195-
this.complete = true;
196-
})
197-
.catch(e => {
198-
this.emitter.emit("error", {target: this});
199-
});
200-
*/
201170
}
202171
}
203172
}

packages/canvas-polyfill/URL.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {knownFolders, File as NSFile, isIOS, path} from '@nativescript/core';
2+
import {FileManager} from './async/async'
3+
4+
export class URL {
5+
public static createObjectURL(object: any): string {
6+
if (object instanceof Blob || object instanceof File) {
7+
const filePath = path.join(knownFolders.temp().path, this.getUUID() + '.js');
8+
const buf = (Blob as any).InternalAccessor.getBuffer(object);
9+
if (isIOS) {
10+
NSFile.fromPath(filePath).writeSync(NSData.dataWithData(buf));
11+
} else {
12+
try {
13+
const fos = new java.io.FileOutputStream(
14+
new java.io.File(filePath)
15+
);
16+
fos.write(Array.from(buf) as any);
17+
fos.flush();
18+
fos.close();
19+
} catch (e) {
20+
return null;
21+
}
22+
}
23+
return filePath;
24+
}
25+
return null;
26+
}
27+
28+
public static revokeObjectURL(url: string){
29+
if(typeof url === 'string'){
30+
if(NSFile.exists(url)){
31+
const file = NSFile.fromPath(url);
32+
file.removeSync()
33+
}
34+
}
35+
}
36+
37+
private static getUUID() {
38+
if (isIOS) {
39+
return NSUUID.UUID().UUIDString;
40+
}
41+
return java.util.UUID.randomUUID().toString()
42+
}
43+
}

packages/canvas-polyfill/async/xhr/TNSXMLHttpRequest.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ export class TNSXMLHttpRequest {
421421
this.emitEvent('loadstart', startEvent);
422422

423423
this._updateReadyStateChange(this.LOADING);
424-
424+
425425
FileManager.readFile(path, {}, (error, data) => {
426426
if (error) {
427427
const errorEvent = new ProgressEvent(
@@ -456,6 +456,7 @@ export class TNSXMLHttpRequest {
456456
this._httpContent = data;
457457
this._responseURL = responseURL;
458458

459+
459460
if (this.responseType === XMLHttpRequestResponseType.json) {
460461
try {
461462
if ((global as any).isAndroid) {
@@ -508,7 +509,6 @@ export class TNSXMLHttpRequest {
508509
) {
509510
if ((global as any).isIOS) {
510511
let code = NSUTF8StringEncoding; // long:4
511-
512512
let encodedString = NSString.alloc().initWithDataEncoding(
513513
data,
514514
code
@@ -547,9 +547,8 @@ export class TNSXMLHttpRequest {
547547
if ((global as any).isIOS) {
548548
buffer = interop.bufferFromData(data);
549549
} else {
550-
buffer = (ArrayBuffer as any).from(
551-
java.nio.ByteBuffer.wrap(data)
552-
);
550+
const buf = java.nio.ByteBuffer.wrap(data);
551+
buffer = (ArrayBuffer as any).from(buf);
553552
}
554553

555554
this._response = new Blob([buffer]);

packages/canvas-polyfill/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
require('globals');
22
import {TNSXMLHttpRequest, FileReader, Blob} from './async/async';
3+
import {Element} from './DOM/Element';
34
import {Document} from './DOM/Document';
45
import './window';
56
import './resize';
67
import './process';
78
import {TextDecoder, TextEncoder} from '@nativescript/canvas';
8-
9+
import {URL} from './URL';
910
(global as any).document = (global as any).window.document = (global as any).document || new Document();
11+
Object.defineProperty(global, 'Element', {
12+
value: Element,
13+
configurable: true,
14+
writable: true,
15+
});
1016
Object.defineProperty(global, 'XMLHttpRequest', {
1117
value: TNSXMLHttpRequest,
1218
configurable: true,
@@ -38,6 +44,14 @@ if (!((global as any).TextEncoder instanceof TextEncoder)) {
3844
configurable: true,
3945
writable: true,
4046
});
41-
4247
}
4348

49+
50+
if (!((global as any).URL instanceof URL)) {
51+
Object.defineProperty(global, 'URL', {
52+
value: URL,
53+
configurable: true,
54+
writable: true,
55+
});
56+
(global as any).window.URL = (global as any).URL;
57+
}

packages/canvas-polyfill/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/canvas-polyfill",
3-
"version": "1.0.0-alpha14",
3+
"version": "1.0.0-alpha15",
44
"description": "Polyfill for making NativeScript compatible with web libs like pixi.js, three.js, phaser.js, babylon.js, etc....",
55
"main": "index",
66
"typings": "index.d.ts",
@@ -33,7 +33,7 @@
3333
"readmeFilename": "README.md",
3434
"bootstrapper": "@nativescript/plugin-seed",
3535
"peerDependencies": {
36-
"@nativescript/canvas": "~0.9.2"
36+
"@nativescript/canvas": "~0.9.4"
3737
},
3838
"dependencies": {
3939
"xmldom-qsa": "^1.0.3"
Binary file not shown.

packages/canvas-polyfill/platforms/android/include.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ dependencies {
1111
implementation 'com.squareup.okio:okio:2.4.2'
1212
implementation 'com.squareup.okhttp3:okhttp:3.14.2'
1313
implementation "androidx.annotation:annotation:$androidxVersion"
14+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
1415
}

packages/canvas-polyfill/resize.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ import {Application} from '@nativescript/core';
55
*/
66

77
const scale = Screen.mainScreen.scale;
8-
8+
const width = Screen.mainScreen.widthPixels;
9+
const height = Screen.mainScreen.heightPixels;
910
(global as any).window.devicePixelRatio = (global as any).devicePixelRatio = 1;
10-
(global as any).window.innerWidth = (global as any).innerWidth = Screen.mainScreen.widthPixels;
11-
(global as any).window.clientWidth = (global as any).clientWidth = Screen.mainScreen.widthPixels;
12-
(global as any).window.innerHeight = (global as any).innerHeight = Screen.mainScreen.heightPixels;
13-
(global as any).window.clientHeight = (global as any).clientHeight = Screen.mainScreen.heightPixels;
11+
(global as any).window.innerWidth = (global as any).innerWidth = width;
12+
(global as any).window.clientWidth = (global as any).clientWidth = width;
13+
(global as any).window.innerHeight = (global as any).innerHeight = height;
14+
(global as any).window.clientHeight = (global as any).clientHeight = height;
1415
(global as any).window.screen = (global as any).screen = (global as any).screen || {};
1516
(global as any).window.screen.orientation = (global as any).screen.orientation = (global as any).screen.orientation || (global as any).clientWidth < (global as any).clientHeight ? 0 : 90;
1617
if (!(global as any).__TNS_BROWSER_POLYFILL_RESIZE) {
1718
(global as any).__TNS_BROWSER_POLYFILL_RESIZE = true;
1819
Application.on(Application.orientationChangedEvent, (args) => {
19-
let width = Screen.mainScreen.widthPixels;
20-
let height = Screen.mainScreen.heightPixels;
20+
const width = Screen.mainScreen.widthPixels;
21+
const height = Screen.mainScreen.heightDIPs;
2122
(global as any).window.devicePixelRatio = (global as any).devicePixelRatio = 1;
2223
(global as any).window.innerWidth = (global as any).innerWidth = width;
2324
(global as any).window.clientWidth = (global as any).clientWidth = width;

0 commit comments

Comments
 (0)