-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import {IframeMessageName} from "./enums"; | ||
import {IEventFunc, ITriggerIframeMessage} from "./interface/iframe"; | ||
|
||
declare let window: any; | ||
|
||
export class IframeManager { | ||
event: Map<string, Set<IEventFunc>>; | ||
|
||
private listener = (event: any) => { | ||
const data = event.data; | ||
const message = data.message; | ||
const _data = data.data; | ||
const stack = this.event.get(message); | ||
if (!stack) { | ||
return; | ||
} | ||
stack.forEach(func => { | ||
func(_data); | ||
}); | ||
}; | ||
|
||
constructor() { | ||
console.log('init iframe manager'); | ||
|
||
this.event = new Map(); | ||
|
||
if (typeof window == 'undefined') { | ||
console.log('window is undefined'); | ||
return; | ||
} | ||
|
||
window.addEventListener('message', this.listener); | ||
} | ||
|
||
subscribe(eventName: IframeMessageName, func: IEventFunc) { | ||
if (!this.event.has(eventName)) { | ||
this.event.set(eventName, new Set([func])); | ||
return; | ||
} | ||
|
||
const stack = this.event.get(eventName) || new Set(); | ||
|
||
if (stack.has(func)) { | ||
console.error('has same function'); | ||
return; | ||
} | ||
|
||
stack.add(func); | ||
this.event.set(eventName, stack); | ||
} | ||
|
||
unSubscribe(eventName: IframeMessageName, func: IEventFunc) { | ||
if (!this.event.has(eventName)) { | ||
console.error('there are no subscribe'); | ||
return; | ||
} | ||
|
||
const stack = this.event.get(eventName) || new Set(); | ||
stack.delete(func); | ||
this.event.set(eventName, stack); | ||
} | ||
|
||
unSubscribeAll() { | ||
this.event = new Map(); | ||
} | ||
|
||
triggerEvent({ iframeRef, eventName, data }: ITriggerIframeMessage) { | ||
iframeRef?.contentWindow?.postMessage( | ||
{ | ||
msg: eventName, | ||
data | ||
}, | ||
'*', | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { IframeMessageName, Network, TriggerIframeMessageName } from '../enums'; | ||
|
||
export type IEventFunc = (data: any) => void; | ||
|
||
export interface IIframeMessageForCollaborators { | ||
message: IframeMessageName.Collaborators; | ||
data: { | ||
roomId: string; | ||
collaborators: ICollaborator[]; | ||
}; | ||
} | ||
|
||
interface ICollaborator { | ||
name: string; | ||
avatar: string; | ||
} | ||
|
||
export interface IIframeMessageForSocketStatus { | ||
message: IframeMessageName.SocketStatus; | ||
data: { | ||
roomId: string; | ||
status: Network; | ||
}; | ||
} | ||
|
||
export interface IIframeMessageForSocketError { | ||
message: IframeMessageName.SocketError; | ||
data: IRoomSocketError; | ||
} | ||
|
||
interface IRoomSocketError { | ||
errorCode: number; | ||
message: string; | ||
roomId: string; | ||
messageId: string; | ||
} | ||
|
||
export interface IIframeMessageForChangeView { | ||
message: IframeMessageName.ChangeView; | ||
data: { | ||
roomId: string; | ||
nextViewId: string; | ||
}; | ||
} | ||
|
||
export interface IIframeMessageForChangeNodeName { | ||
message: IframeMessageName.ChangeNodeName; | ||
data: { | ||
roomId: string; | ||
nodeName: string; | ||
}; | ||
} | ||
|
||
export interface IIframeMessageForPageLoaded { | ||
message: IframeMessageName.PageLoaded; | ||
} | ||
|
||
export interface IIframeMessageForEmbedLinkFail { | ||
message: IframeMessageName.EmbedLinkFail; | ||
} | ||
|
||
export interface IIframeMessageForPageCrash { | ||
message: IframeMessageName.PageCrash; | ||
} | ||
|
||
export interface IIframeMessageForChangeViewName { | ||
message: IframeMessageName.ChangeViewName; | ||
data: { | ||
roomId: string; | ||
viewId: string; | ||
viewName: string; | ||
}; | ||
} | ||
|
||
export interface IIframeMessageForTriggerEventResult { | ||
message: IframeMessageName.TriggerEventResult; | ||
data: { | ||
eventName: TriggerIframeMessageName; | ||
success: boolean; | ||
message?: string; | ||
}; | ||
} | ||
|
||
interface ITriggerIframeMessageBase { | ||
iframeRef: any; | ||
eventName: TriggerIframeMessageName; | ||
} | ||
|
||
export interface ITriggerIframeMessageForExportData extends ITriggerIframeMessageBase { | ||
eventName: TriggerIframeMessageName.ExportData; | ||
data: { | ||
nodeId: string; | ||
/** | ||
* @default 'xlsx' | ||
*/ | ||
fileType?: 'csv' | 'xlsx' | 'png' | ||
viewId?: string; | ||
}; | ||
} | ||
|
||
export type ITriggerIframeMessage = ITriggerIframeMessageForExportData |