overleaf/services/web/frontend/js/features/ide-react/create-ide-event-emitter.ts
Alf Eaton 66c55b0647 Streamline the project references context provider (#19387)
GitOrigin-RevId: 9af00459ec121a605b84809b349a0bc258896048
2024-07-16 08:04:30 +00:00

56 lines
1.9 KiB
TypeScript

import { Project } from '../../../../types/project'
import { PermissionsLevel } from '@/features/ide-react/types/permissions'
import { ShareJsDoc } from '@/features/ide-react/editor/share-js-doc'
import { GotoLineOptions } from '@/features/ide-react/types/goto-line-options'
import { CursorPosition } from '@/features/ide-react/types/cursor-position'
import { FileTreeFindResult } from '@/features/ide-react/types/file-tree'
export type IdeEvents = {
'project:joined': [{ project: Project; permissionsLevel: PermissionsLevel }]
'document:closed': [doc: ShareJsDoc]
'doc:changed': [{ doc_id: string }]
'doc:saved': [{ doc_id: string }]
'doc:opened': []
'ide:opAcknowledged': [{ doc_id: string; op: any }]
'store-doc-position': []
'editor:gotoOffset': [gotoOffset: number]
'editor:gotoLine': [options: GotoLineOptions]
'cursor:editor:update': [position: CursorPosition]
'outline-toggled': [isOpen: boolean]
'cursor:editor:syncToPdf': []
'scroll:editor:update': []
'comment:start_adding': []
'history:toggle': []
'entity:deleted': [entity: FileTreeFindResult]
}
export class IdeEventEmitter extends EventTarget {
emit<T extends keyof IdeEvents>(eventName: T, ...detail: IdeEvents[T]) {
this.dispatchEvent(new CustomEvent<IdeEvents[T]>(eventName, { detail }))
}
on<T extends keyof IdeEvents>(
eventName: T,
listener: (event: CustomEvent<IdeEvents[T]>) => void
) {
this.addEventListener(eventName, listener as EventListener)
}
once<T extends keyof IdeEvents>(
eventName: T,
listener: (event: CustomEvent<IdeEvents[T]>) => void
) {
this.addEventListener(eventName, listener as EventListener, { once: true })
}
off<T extends keyof IdeEvents>(
eventName: T,
listener: (event: CustomEvent<IdeEvents[T]>) => void
) {
this.removeEventListener(eventName, listener as EventListener)
}
}
export function createIdeEventEmitter() {
return new IdeEventEmitter()
}