Skip to content

Commit 86ff1a7

Browse files
committed
fix(soba): adjust html with new renderer
1 parent 441a5f0 commit 86ff1a7

File tree

3 files changed

+65
-47
lines changed

3 files changed

+65
-47
lines changed

libs/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * from './lib/directives/args';
22
export * from './lib/directives/parent';
33
export * from './lib/directives/selection';
4-
// export * from './lib/html';
4+
export * from './lib/html';
55
export * from './lib/instance';
66
export * from './lib/loader';
77
export * from './lib/loop';

libs/core/src/lib/html.ts

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,66 @@
1313
// import { DOM_PARENT } from './renderer-old/constants';
1414
// import { injectStore } from './store';
1515
// import { NgtAnyRecord } from './types';
16+
17+
import {
18+
AbstractType,
19+
DestroyRef,
20+
Directive,
21+
ElementRef,
22+
inject,
23+
InjectionToken,
24+
Provider,
25+
ProviderToken,
26+
Type,
27+
} from '@angular/core';
28+
import { NGT_DOM_PARENT_FLAG, NGT_HTML_FLAG } from './renderer/constants';
29+
import { injectStore } from './store';
30+
import { NgtAnyRecord } from './types';
31+
1632
//
17-
// const NGT_HTML_DOM_ELEMENT = new InjectionToken<'gl' | HTMLElement>('NGT_HTML_DOM_ELEMENT');
18-
//
19-
// export function provideHTMLDomElement(): Provider;
20-
// export function provideHTMLDomElement(factory: () => HTMLElement): Provider;
21-
// export function provideHTMLDomElement<
22-
// TDeps extends Array<ProviderToken<any>>,
23-
// TValues extends {
24-
// [K in keyof TDeps]: TDeps[K] extends Type<infer T> | AbstractType<infer T> | InjectionToken<infer T> ? T : never;
25-
// },
26-
// >(deps: TDeps, factory: (...args: TValues) => HTMLElement): Provider;
27-
// export function provideHTMLDomElement(...args: any[]) {
28-
// if (args.length === 0) {
29-
// return { provide: NGT_HTML_DOM_ELEMENT, useFactory: () => 'gl' };
30-
// }
31-
//
32-
// if (args.length === 1) {
33-
// return { provide: NGT_HTML_DOM_ELEMENT, useFactory: args[0] };
34-
// }
35-
//
36-
// return { provide: NGT_HTML_DOM_ELEMENT, useFactory: args.pop(), deps: args };
37-
// }
38-
//
39-
// @Directive()
40-
// export abstract class NgtHTML {
41-
// static [HTML] = true;
42-
//
43-
// protected store = injectStore();
44-
// protected destroyRef = inject(DestroyRef);
45-
// protected host = inject<ElementRef<HTMLElement>>(ElementRef);
46-
// protected domElement = inject(NGT_HTML_DOM_ELEMENT, { self: true, optional: true });
47-
//
48-
// constructor() {
49-
// if (this.domElement === 'gl') {
50-
// Object.assign(this.host.nativeElement, {
51-
// [DOM_PARENT]: this.store.snapshot.gl.domElement.parentElement,
52-
// });
53-
// } else if (this.domElement) {
54-
// Object.assign(this.host.nativeElement, { [DOM_PARENT]: this.domElement });
55-
// }
56-
//
57-
// this.destroyRef.onDestroy(() => {
58-
// (this.host.nativeElement as NgtAnyRecord)[DOM_PARENT] = null;
59-
// delete (this.host.nativeElement as NgtAnyRecord)[DOM_PARENT];
60-
// });
61-
// }
62-
// }
33+
const NGT_HTML_DOM_ELEMENT = new InjectionToken<'gl' | HTMLElement>('NGT_HTML_DOM_ELEMENT');
34+
35+
export function provideHTMLDomElement(): Provider;
36+
export function provideHTMLDomElement(factory: () => HTMLElement): Provider;
37+
export function provideHTMLDomElement<
38+
TDeps extends Array<ProviderToken<any>>,
39+
TValues extends {
40+
[K in keyof TDeps]: TDeps[K] extends Type<infer T> | AbstractType<infer T> | InjectionToken<infer T> ? T : never;
41+
},
42+
>(deps: TDeps, factory: (...args: TValues) => HTMLElement): Provider;
43+
export function provideHTMLDomElement(...args: any[]) {
44+
if (args.length === 0) {
45+
return { provide: NGT_HTML_DOM_ELEMENT, useFactory: () => 'gl' };
46+
}
47+
48+
if (args.length === 1) {
49+
return { provide: NGT_HTML_DOM_ELEMENT, useFactory: args[0] };
50+
}
51+
52+
return { provide: NGT_HTML_DOM_ELEMENT, useFactory: args.pop(), deps: args };
53+
}
54+
55+
@Directive()
56+
export abstract class NgtHTML {
57+
static [NGT_HTML_FLAG] = true;
58+
59+
protected domElement = inject(NGT_HTML_DOM_ELEMENT, { self: true, optional: true });
60+
61+
constructor() {
62+
const host = inject<ElementRef<HTMLElement>>(ElementRef);
63+
const store = injectStore();
64+
65+
if (this.domElement === 'gl') {
66+
Object.assign(host.nativeElement, {
67+
[NGT_DOM_PARENT_FLAG]: store.snapshot.gl.domElement.parentElement,
68+
});
69+
} else if (this.domElement) {
70+
Object.assign(host.nativeElement, { [NGT_DOM_PARENT_FLAG]: this.domElement });
71+
}
72+
73+
inject(DestroyRef).onDestroy(() => {
74+
(host.nativeElement as NgtAnyRecord)[NGT_DOM_PARENT_FLAG] = null;
75+
delete (host.nativeElement as NgtAnyRecord)[NGT_DOM_PARENT_FLAG];
76+
});
77+
}
78+
}

libs/core/src/lib/renderer/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ export const NGT_MANUAL_INJECTED_STORE = '__ngt_manual_injected_store__';
99
export const NGT_GET_NODE_ATTRIBUTE_FLAG = '__ngt_get_node_attribute__';
1010
export const NGT_DOM_PARENT_FLAG = '__ngt_dom_parent__';
1111

12+
export const NGT_HTML_FLAG = '__ngt_html__';
13+
1214
export const THREE_NATIVE_EVENTS = ['added', 'removed', 'childadded', 'childremoved', 'disposed'];

0 commit comments

Comments
 (0)