Skip to content
Snippets Groups Projects
Commit ae9deee2 authored by Xiao Gui's avatar Xiao Gui
Browse files

bugfix: tests

parent 033a43bf
No related branches found
No related tags found
No related merge requests found
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')
......@@ -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()
......
......@@ -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
})
......
......@@ -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
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)
......
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)
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment