|
| 1 | +import { |
| 2 | + ChangeDetectionStrategy, |
| 3 | + Component, |
| 4 | + contentChild, |
| 5 | + Directive, |
| 6 | + effect, |
| 7 | + EmbeddedViewRef, |
| 8 | + inject, |
| 9 | + Injector, |
| 10 | + input, |
| 11 | + signal, |
| 12 | + SkipSelf, |
| 13 | + TemplateRef, |
| 14 | + untracked, |
| 15 | + viewChild, |
| 16 | + ViewContainerRef, |
| 17 | +} from '@angular/core'; |
| 18 | +import * as THREE from 'three'; |
| 19 | +import { getInstanceState, prepare } from './instance'; |
| 20 | +import { SPECIAL_INTERNAL_ADD_COMMENT_FLAG } from './renderer/constants'; |
| 21 | +import { injectStore, NGT_STORE } from './store'; |
| 22 | +import type { NgtComputeFunction, NgtEventManager, NgtSize, NgtState, NgtViewport } from './types'; |
| 23 | +import { is } from './utils/is'; |
| 24 | +import { omit, pick } from './utils/parameters'; |
| 25 | +import { signalState, SignalState } from './utils/signal-state'; |
| 26 | +import { updateCamera } from './utils/update'; |
| 27 | + |
| 28 | +@Directive({ selector: 'ng-template[portalContent]' }) |
| 29 | +export class NgtPortalContent { |
| 30 | + static ngTemplateContextGuard(_: NgtPortalContent, ctx: unknown): ctx is { injector: Injector } { |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + constructor() { |
| 35 | + const { element } = inject(ViewContainerRef); |
| 36 | + const { element: parentComment } = inject(ViewContainerRef, { skipSelf: true }); |
| 37 | + const commentNode = element.nativeElement; |
| 38 | + |
| 39 | + commentNode.data = 'portal-content-container'; |
| 40 | + |
| 41 | + if (commentNode[SPECIAL_INTERNAL_ADD_COMMENT_FLAG]) { |
| 42 | + commentNode[SPECIAL_INTERNAL_ADD_COMMENT_FLAG](parentComment.nativeElement); |
| 43 | + delete commentNode[SPECIAL_INTERNAL_ADD_COMMENT_FLAG]; |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export interface NgtPortalState extends Omit<NgtState, 'events'> { |
| 49 | + events: { |
| 50 | + enabled?: boolean; |
| 51 | + priority?: number; |
| 52 | + compute?: NgtComputeFunction; |
| 53 | + connected?: any; |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +function mergeState( |
| 58 | + previousRoot: SignalState<NgtState>, |
| 59 | + store: SignalState<NgtState>, |
| 60 | + container: THREE.Object3D, |
| 61 | + pointer: THREE.Vector2, |
| 62 | + raycaster: THREE.Raycaster, |
| 63 | + events?: NgtPortalState['events'], |
| 64 | + size?: NgtSize, |
| 65 | +) { |
| 66 | + const previousState = previousRoot.snapshot; |
| 67 | + const state = store.snapshot; |
| 68 | + |
| 69 | + let viewport: Omit<NgtViewport, 'dpr' | 'initialDpr'> | undefined = undefined; |
| 70 | + |
| 71 | + if (state.camera && size) { |
| 72 | + const camera = state.camera; |
| 73 | + // calculate the override viewport, if present |
| 74 | + viewport = previousState.viewport.getCurrentViewport(camera, new THREE.Vector3(), size); |
| 75 | + // update the portal camera, if it differs from the previous layer |
| 76 | + if (camera !== previousState.camera) updateCamera(camera, size); |
| 77 | + } |
| 78 | + |
| 79 | + return { |
| 80 | + // the intersect consists of the previous root state |
| 81 | + ...previousState, |
| 82 | + ...state, |
| 83 | + // portals have their own scene, which forms the root, a raycaster and a pointer |
| 84 | + scene: container as THREE.Scene, |
| 85 | + pointer, |
| 86 | + raycaster, |
| 87 | + // their previous root is the layer before it |
| 88 | + previousRoot, |
| 89 | + events: { ...previousState.events, ...state.events, ...events }, |
| 90 | + size: { ...previousState.size, ...size }, |
| 91 | + viewport: { ...previousState.viewport, ...viewport }, |
| 92 | + // layers are allowed to override events |
| 93 | + setEvents: (events: Partial<NgtEventManager<any>>) => |
| 94 | + store.update((state) => ({ ...state, events: { ...state.events, ...events } })), |
| 95 | + } as NgtState; |
| 96 | +} |
| 97 | + |
| 98 | +@Component({ |
| 99 | + selector: 'ngt-portal', |
| 100 | + template: ` |
| 101 | + <ng-container #anchor /> |
| 102 | + `, |
| 103 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 104 | + providers: [ |
| 105 | + { |
| 106 | + provide: NGT_STORE, |
| 107 | + useFactory: (previousStore: SignalState<NgtState>) => { |
| 108 | + const store = signalState({} as NgtState); |
| 109 | + store.update(mergeState(previousStore, store, null!, new THREE.Vector2(), new THREE.Raycaster())); |
| 110 | + return store; |
| 111 | + }, |
| 112 | + deps: [[new SkipSelf(), NGT_STORE]], |
| 113 | + }, |
| 114 | + ], |
| 115 | +}) |
| 116 | +export class NgtPortal { |
| 117 | + container = input.required<THREE.Object3D>(); |
| 118 | + state = input<Partial<NgtPortalState>>({}); |
| 119 | + |
| 120 | + private contentRef = contentChild.required(NgtPortalContent, { read: TemplateRef }); |
| 121 | + private anchorRef = viewChild.required('anchor', { read: ViewContainerRef }); |
| 122 | + |
| 123 | + private previousStore = injectStore({ skipSelf: true }); |
| 124 | + private portalStore = injectStore(); |
| 125 | + private injector = inject(Injector); |
| 126 | + |
| 127 | + private size = pick(this.state, 'size'); |
| 128 | + private events = pick(this.state, 'events'); |
| 129 | + private restState = omit(this.state, ['size', 'events']); |
| 130 | + |
| 131 | + protected portalContentRendered = signal(false); |
| 132 | + |
| 133 | + private portalViewRef?: EmbeddedViewRef<unknown>; |
| 134 | + |
| 135 | + constructor() { |
| 136 | + effect(() => { |
| 137 | + let [container, prevState] = [this.container(), this.previousStore()]; |
| 138 | + |
| 139 | + const [size, events, restState] = [untracked(this.size), untracked(this.events), untracked(this.restState)]; |
| 140 | + |
| 141 | + if (!is.instance(container)) { |
| 142 | + container = prepare(container, this.portalStore, 'ngt-portal'); |
| 143 | + } |
| 144 | + |
| 145 | + const instanceState = getInstanceState(container); |
| 146 | + if (instanceState && instanceState.store !== this.portalStore) { |
| 147 | + instanceState.store = this.portalStore; |
| 148 | + } |
| 149 | + |
| 150 | + this.portalStore.update( |
| 151 | + restState, |
| 152 | + mergeState( |
| 153 | + this.previousStore, |
| 154 | + this.portalStore, |
| 155 | + container, |
| 156 | + this.portalStore.snapshot.pointer, |
| 157 | + this.portalStore.snapshot.raycaster, |
| 158 | + events, |
| 159 | + size, |
| 160 | + ), |
| 161 | + ); |
| 162 | + |
| 163 | + if (this.portalViewRef) { |
| 164 | + this.portalViewRef.detectChanges(); |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + this.portalViewRef = untracked(this.anchorRef).createEmbeddedView( |
| 169 | + untracked(this.contentRef), |
| 170 | + { injector: this.injector }, |
| 171 | + { injector: this.injector }, |
| 172 | + ); |
| 173 | + this.portalViewRef.detectChanges(); |
| 174 | + this.portalContentRendered.set(true); |
| 175 | + }); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +export const NgtPortalDeclarations = [NgtPortal, NgtPortalContent] as const; |
| 180 | + |
1 | 181 | // import {
|
2 | 182 | // afterNextRender,
|
3 | 183 | // ChangeDetectionStrategy,
|
|
0 commit comments