diff --git a/src/plugin/const.ts b/src/plugin/const.ts index 68104badfa030957d049a7acdfb58286fd254680..474bc36bf2cc119f845074735cc3073368db6a13 100644 --- a/src/plugin/const.ts +++ b/src/plugin/const.ts @@ -1,3 +1,5 @@ +import { InjectionToken } from "@angular/core" + const PLUGIN_SRC_KEY = "x-plugin-portal-src" export function setPluginSrc(src: string, record: Record<string, unknown> = {}){ @@ -10,3 +12,5 @@ export function setPluginSrc(src: string, record: Record<string, unknown> = {}){ export function getPluginSrc(record: Record<string, string> = {}){ return record[PLUGIN_SRC_KEY] } + +export const SET_PLUGIN_NAME = new InjectionToken('SET_PLUGIN_NAME') diff --git a/src/plugin/pluginPortal/pluginPortal.component.ts b/src/plugin/pluginPortal/pluginPortal.component.ts index 7d5b4d4214babf9c9af7ceda85e6e2e205530851..58bd58993f3c2423ed278fee3c64b330b78b4af2 100644 --- a/src/plugin/pluginPortal/pluginPortal.component.ts +++ b/src/plugin/pluginPortal/pluginPortal.component.ts @@ -5,8 +5,7 @@ import { BoothVisitor, JRPCRequest, JRPCSuccessResp, ListenerChannel } from "src import { ApiBoothEvents, ApiService, BroadCastingApiEvents, HeartbeatEvents, namespace } from "src/api/service"; import { getUuid } from "src/util/fn"; import { WIDGET_PORTAL_TOKEN } from "src/widget/constants"; -import { getPluginSrc } from "../const"; -import { PluginService } from "../service"; +import { getPluginSrc, SET_PLUGIN_NAME } from "../const"; @Component({ selector: 'sxplr-plugin-portal', @@ -44,8 +43,8 @@ export class PluginPortal implements AfterViewInit, OnDestroy, ListenerChannel{ constructor( private apiService: ApiService, - private pluginSvc: PluginService, public vcr: ViewContainerRef, + @Optional() @Inject(SET_PLUGIN_NAME) private setPluginName: (inst: unknown, pluginName: string) => void, @Optional() @Inject(WIDGET_PORTAL_TOKEN) portalData: Record<string, string> ){ if (portalData){ @@ -91,7 +90,7 @@ export class PluginPortal implements AfterViewInit, OnDestroy, ListenerChannel{ const data = event.data as JRPCSuccessResp<HeartbeatEvents['init']['response']> this.srcName = data.result.name || 'Untitled Pluging' - this.pluginSvc.setPluginName(this, this.srcName) + this.setPluginName(this, this.srcName) while (this.handshakeSub.length > 0) this.handshakeSub.pop().unsubscribe() diff --git a/src/plugin/service.ts b/src/plugin/service.ts index 4f5a5e74f5a6bfae42a6fd445b68fcd3c115e1f2..19f183189fac54e16b9b5a33be58de632276327a 100644 --- a/src/plugin/service.ts +++ b/src/plugin/service.ts @@ -3,7 +3,7 @@ import { Injectable, Injector, NgZone } from "@angular/core"; import { WIDGET_PORTAL_TOKEN } from "src/widget/constants"; import { WidgetService } from "src/widget/service"; import { WidgetPortal } from "src/widget/widgetPortal/widgetPortal.component"; -import { setPluginSrc } from "./const"; +import { setPluginSrc, SET_PLUGIN_NAME } from "./const"; import { PluginPortal } from "./pluginPortal/pluginPortal.component"; import { environment } from "src/environments/environment" @@ -33,6 +33,9 @@ export class PluginService { providers: [{ provide: WIDGET_PORTAL_TOKEN, useValue: setPluginSrc(htmlSrc, {}) + }, { + provide: SET_PLUGIN_NAME, + useValue: (inst: PluginPortal, pluginName: string) => this.setPluginName(inst, pluginName) }], parent: this.injector }) diff --git a/src/widget/constants.ts b/src/widget/constants.ts index 412bccd405d746982f24081f8d405cd7011aeb53..a779b079ad01afafeff6bf9e52f0675e4fdad6e4 100644 --- a/src/widget/constants.ts +++ b/src/widget/constants.ts @@ -21,3 +21,5 @@ interface TypeActionWidgetReturnVal<T>{ export type TypeActionToWidget<T> = (type: EnumActionToWidget, obj: T, option: IActionWidgetOption) => TypeActionWidgetReturnVal<T> export const WIDGET_PORTAL_TOKEN = new InjectionToken<Record<string, unknown>>("WIDGET_PORTAL_TOKEN") + +export const RM_WIDGET = new InjectionToken('RM_WIDGET') \ No newline at end of file diff --git a/src/widget/service.ts b/src/widget/service.ts index 75be508867a61aa48951701ff91a43dbab8045cd..b20ee1f278be7991b2f0728af198e0624546eb29 100644 --- a/src/widget/service.ts +++ b/src/widget/service.ts @@ -1,5 +1,6 @@ import { ComponentPortal } from "@angular/cdk/portal"; import { ComponentFactory, ComponentFactoryResolver, ComponentRef, Injectable, Injector, ViewContainerRef } from "@angular/core"; +import { RM_WIDGET } from "./constants"; import { WidgetPortal } from "./widgetPortal/widgetPortal.component"; @Injectable({ @@ -19,7 +20,14 @@ export class WidgetService { public addNewWidget<T>(Component: new (...arg: any) => T, injector: Injector): WidgetPortal<T> { const widgetPortal = this.vcr.createComponent(this.cf, 0, injector) as ComponentRef<WidgetPortal<T>> - const cmpPortal = new ComponentPortal<T>(Component, this.vcr, injector) + const inj = Injector.create({ + providers: [{ + provide: RM_WIDGET, + useValue: (cmp: WidgetPortal<T>) => this.rmWidget(cmp) + }], + parent: injector + }) + const cmpPortal = new ComponentPortal<T>(Component, this.vcr, inj) this.viewRefMap.set(widgetPortal.instance, widgetPortal) diff --git a/src/widget/widgetPortal/widgetPortal.component.ts b/src/widget/widgetPortal/widgetPortal.component.ts index a1351e15e370318c5e9bf363c4c36f2f0b8d37e9..5e5cc05fa5fa6f15a139fb415097c3fff4078c48 100644 --- a/src/widget/widgetPortal/widgetPortal.component.ts +++ b/src/widget/widgetPortal/widgetPortal.component.ts @@ -1,6 +1,6 @@ import { ComponentPortal } from "@angular/cdk/portal"; -import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from "@angular/core"; -import { WidgetService } from "../service"; +import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, Optional } from "@angular/core"; +import { RM_WIDGET } from "../constants"; @Component({ selector: 'sxplr-widget-portal', @@ -30,12 +30,12 @@ export class WidgetPortal<T>{ } constructor( - private wSvc: WidgetService, private cdr: ChangeDetectorRef, + @Optional() @Inject(RM_WIDGET) private rmWidget: (inst: unknown) => void ){ } exit(){ - this.wSvc.rmWidget(this) + if (this.rmWidget) this.rmWidget(this) } }