Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
73 lines (55 loc) · 1.84 KB
/
Copy pathindex.ts
File metadata and controls
73 lines (55 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Frame } from 'tns-core-modules/ui/frame'
import { run, on, launchEvent } from 'tns-core-modules/application'
import { DocumentNode, ElementNode, ViewNode } from './dom'
import { registerNativeElements } from './nativescript-elements'
declare global {
export class SvelteComponent {
constructor(options: { target?: ViewNode, props?: any });
$set(props: any): void;
}
}
function installGlobalShims() {
//expose our fake dom as global document for svelte components
let window = global as any;
window.window = global;
window.document = new DocumentNode();
window.requestAnimationFrame = (action: ()=>{}) => {
setTimeout(action, 33); //about 30 fps
}
window.getComputedStyle = (node: ViewNode) => {
return node.nativeView.style;
}
window.performance = {
now() {
return Date.now();
}
};
window.CustomEvent = class {
detail: any;
eventName: string;
type: string;
constructor(name: string, detail: any = null) {
this.eventName = name; //event name for nativescript
this.type = name; // type for svelte
this.detail = detail;
}
}
}
export function svelteNative(startPage: typeof SvelteComponent, data: any) {
registerNativeElements();
installGlobalShims();
const document = (global as any).document as DocumentNode;
//our application main navigation frame
let frame = new ElementNode('frame');
document.appendChild(frame);
//wait for launch
on(launchEvent, ()=>{
let page = new startPage({
target: frame,
props: data || {}
});
//dirty way to find page's native view
(frame.nativeView as Frame).navigate({ create: () => frame.firstElement().nativeView});
})
run({create: () => frame.nativeView});
}