import { EventEmitter } from "events" ; import { FluidObject , IFluidHandle , IFluidLoadable , } from "@fluidframework/core-interfaces" ; import { ISharedDirectory , IDirectory , IValueChanged , } from "@fluidframework/map" ; import { IFluidDataStoreFactory } from "@fluidframework/runtime-definitions" ; import { v4 as uuid } from "uuid" ; import { IFluidHTMLView } from "@fluidframework/view-interfaces" ; import { IFluidObjectInternalRegistry } from "../../interfaces" ; export interface ITabsTypes { friendlyName : string ; fabricIconName : string ; factory : IFluidDataStoreFactory ; } export interface ITabsModel { type : string ; handleOrId ? : IFluidHandle | string ; } export interface ITabsDataModel extends EventEmitter { getFluidObjectTab ( id ) : Promise < FluidObject | undefined > ; getTabIds ( ) : string [ ] ; createTab ( factory ) : Promise < string > ; getNewTabTypes ( ) : ITabsTypes [ ] ; } export class TabsDataModel extends EventEmitter implements ITabsDataModel { private readonly tabs : IDirectory ; constructor ( public root , private readonly internalRegistry , private readonly createSubObject , private readonly getFluidObjectFromDirectory ) { super ( ) ; const tabs = "tab-ids" ; if ( ! root . hasSubDirectory ( tabs ) ) { root . createSubDirectory ( tabs ) ; } this . tabs = root . getSubDirectory ( tabs ) ! ; this . tabs . on ( "containedValueChanged" , ( changed , local ) => { this . emit ( "newTab" , local ) ; } ) ; } public getTabIds ( ) { return Array . from ( this . tabs . keys ( ) ) ; } public async createTab ( factory ) { const newKey = uuid ( ) ; const fluidObject = await this . createSubObject ( factory ) ; this . tabs . set ( newKey , { type : factory . type , handleOrId : fluidObject . handle , } ) ; return newKey ; } private getObjectFromDirectory ( id , directory ) { const data = directory . get < ITabsModel > ( id ) ; return data ?. handleOrId ; } public async getFluidObjectTab ( id ) { return this . getFluidObjectFromDirectory ( id , this . tabs , this . getObjectFromDirectory ) ; } public getNewTabTypes ( ) { const response = [ ] ; this . internalRegistry . getFromCapability ( IFluidHTMLView ) . forEach ( ( e ) => { response . push ( { friendlyName : e . friendlyName , fabricIconName : e . fabricIconName , factory : e . factory , } ) ; } ) ; return response ; } }