2023-10-24 14:57:17 +00:00
|
|
|
import { Project } from '../../../../types/project'
|
2023-11-23 12:03:22 +00:00
|
|
|
import { PermissionsLevel } from '@/features/ide-react/types/permissions'
|
2023-10-26 08:57:00 +00:00
|
|
|
import { ShareJsDoc } from '@/features/ide-react/editor/share-js-doc'
|
2023-10-24 14:57:17 +00:00
|
|
|
import { GotoLineOptions } from '@/features/ide-react/types/goto-line-options'
|
|
|
|
import { CursorPosition } from '@/features/ide-react/types/cursor-position'
|
2023-11-02 11:36:04 +00:00
|
|
|
import { FileTreeFindResult } from '@/features/ide-react/types/file-tree'
|
2023-10-24 14:57:17 +00:00
|
|
|
|
|
|
|
export type IdeEvents = {
|
|
|
|
'project:joined': [{ project: Project; permissionsLevel: PermissionsLevel }]
|
2023-10-26 08:57:00 +00:00
|
|
|
'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': []
|
2023-10-24 14:57:17 +00:00
|
|
|
'editor:gotoOffset': [gotoOffset: number]
|
|
|
|
'editor:gotoLine': [options: GotoLineOptions]
|
|
|
|
'cursor:editor:update': [position: CursorPosition]
|
2023-10-26 08:57:00 +00:00
|
|
|
'outline-toggled': [isOpen: boolean]
|
2023-10-24 14:57:17 +00:00
|
|
|
'cursor:editor:syncToPdf': []
|
|
|
|
'scroll:editor:update': []
|
|
|
|
'comment:start_adding': []
|
2023-10-26 08:57:00 +00:00
|
|
|
'references:should-reindex': []
|
2023-11-28 11:24:23 +00:00
|
|
|
'history:toggle': []
|
2023-11-02 11:36:04 +00:00
|
|
|
'entity:deleted': [entity: FileTreeFindResult]
|
2023-10-24 14:57:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 10:05:07 +00:00
|
|
|
export class IdeEventEmitter extends EventTarget {
|
|
|
|
emit<T extends keyof IdeEvents>(eventName: T, ...detail: IdeEvents[T]) {
|
|
|
|
this.dispatchEvent(new CustomEvent<IdeEvents[T]>(eventName, { detail }))
|
|
|
|
}
|
2023-10-24 14:57:17 +00:00
|
|
|
|
2024-01-05 10:05:07 +00:00
|
|
|
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()
|
2023-10-24 14:57:17 +00:00
|
|
|
}
|