Skip to content

Commit 129aa21

Browse files
committed
adjust key
1 parent 0e9bd05 commit 129aa21

File tree

3 files changed

+24
-45
lines changed

3 files changed

+24
-45
lines changed

libs/core/src/lib/canvas.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import {
99
Injector,
1010
Input,
1111
NgZone,
12-
OnChanges,
13-
OnInit,
1412
Output,
15-
SimpleChanges,
1613
ViewChild,
1714
ViewContainerRef,
1815
computed,
@@ -21,6 +18,9 @@ import {
2118
inject,
2219
type ComponentRef,
2320
type EffectRef,
21+
type OnChanges,
22+
type OnInit,
23+
type SimpleChanges,
2424
type Type,
2525
} from '@angular/core';
2626
import { NgxResize, provideNgxResizeOptions, type NgxResizeResult } from 'ngx-resize';
@@ -233,7 +233,7 @@ export class NgtCanvas implements OnInit, OnChanges {
233233
}
234234

235235
ngOnInit() {
236-
// NOTE: we resolve glCanvas now, setup the configurator
236+
// NOTE: we resolve glCanvas at this point, setup the configurator
237237
this.configurator = this.initRoot(this.glCanvas.nativeElement);
238238

239239
this.destroyRef.onDestroy(() => {
@@ -245,7 +245,7 @@ export class NgtCanvas implements OnInit, OnChanges {
245245
});
246246
}
247247

248-
// NOTE: runs outside of Zone due to emitInZone
248+
// NOTE: runs outside of Zone due to emitInZone: false
249249
onResize(result: NgxResizeResult) {
250250
if (result.width > 0 && result.height > 0) {
251251
this.resizeEffectRef?.destroy();
@@ -261,7 +261,7 @@ export class NgtCanvas implements OnInit, OnChanges {
261261
this.configurator.configure({ ...inputs(), size: result });
262262

263263
if (this.glRef) {
264-
this.glRef.changeDetectorRef.detectChanges();
264+
this.cdr.detectChanges();
265265
} else {
266266
this.render();
267267
}

libs/core/src/lib/directives/common.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { safeDetectChanges } from '../utils/safe-detect-changes';
1212

1313
@Directive()
1414
export abstract class NgtCommonDirective {
15+
protected static processComment = true;
16+
1517
private vcr = inject(ViewContainerRef);
1618
private zone = inject(NgZone);
1719
private template = inject(TemplateRef);
@@ -21,10 +23,12 @@ export abstract class NgtCommonDirective {
2123
private view?: EmbeddedViewRef<unknown>;
2224

2325
constructor() {
24-
const commentNode = this.vcr.element.nativeElement;
25-
if (commentNode[SPECIAL_INTERNAL_ADD_COMMENT]) {
26-
commentNode[SPECIAL_INTERNAL_ADD_COMMENT]();
27-
delete commentNode[SPECIAL_INTERNAL_ADD_COMMENT];
26+
if (NgtCommonDirective.processComment) {
27+
const commentNode = this.vcr.element.nativeElement;
28+
if (commentNode[SPECIAL_INTERNAL_ADD_COMMENT]) {
29+
commentNode[SPECIAL_INTERNAL_ADD_COMMENT]();
30+
delete commentNode[SPECIAL_INTERNAL_ADD_COMMENT];
31+
}
2832
}
2933

3034
inject(DestroyRef).onDestroy(() => {

libs/core/src/lib/directives/key.ts

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,21 @@
1-
import {
2-
DestroyRef,
3-
Directive,
4-
Input,
5-
NgZone,
6-
TemplateRef,
7-
ViewContainerRef,
8-
inject,
9-
type EmbeddedViewRef,
10-
} from '@angular/core';
11-
import { safeDetectChanges } from '../utils/safe-detect-changes';
1+
import { Directive, Input } from '@angular/core';
2+
import { NgtCommonDirective } from './common';
123

134
@Directive({ selector: 'ng-template[key]', standalone: true })
14-
export class NgtKey {
15-
private vcr = inject(ViewContainerRef);
16-
private templateRef = inject(TemplateRef);
17-
private zone = inject(NgZone);
5+
export class NgtKey extends NgtCommonDirective {
6+
static override processComment = false;
187

198
private lastKey = '';
20-
private viewRef?: EmbeddedViewRef<unknown>;
9+
10+
override validate(): boolean {
11+
return false;
12+
}
2113

2214
@Input() set key(key: string | number | object) {
23-
const normalizedKey = key.toString();
15+
const normalizedKey = JSON.stringify(key);
2416
if (this.lastKey !== normalizedKey) {
25-
this.createView();
2617
this.lastKey = normalizedKey;
18+
this.createView();
2719
}
2820
}
29-
30-
constructor() {
31-
inject(DestroyRef).onDestroy(() => {
32-
this.viewRef?.destroy();
33-
});
34-
}
35-
36-
private createView() {
37-
if (!this.viewRef?.destroyed) {
38-
this.viewRef?.destroy();
39-
}
40-
41-
this.zone.runOutsideAngular(() => {
42-
this.viewRef = this.vcr.createEmbeddedView(this.templateRef);
43-
safeDetectChanges(this.viewRef);
44-
});
45-
}
4621
}

0 commit comments

Comments
 (0)