Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / docs-core / lib / Bridge / EditorToClientRequestHandler.ts
blob34c0066041922bde2e368e156b90e3f07972c256
1 import type {
2   BroadcastSource,
3   CommentInterface,
4   CommentThreadInterface,
5   EditorRequiresClientMethods,
6   InternalEventBusInterface,
7   RtsMessagePayload,
8   SuggestionSummaryType,
9 } from '@proton/docs-shared'
10 import type { WordCountInfoCollection } from '@proton/docs-shared'
11 import type { UserState } from '@lexical/yjs'
12 import type { EditorOrchestratorInterface } from '../Services/Orchestrator/EditorOrchestratorInterface'
13 import type { ErrorInfo } from 'react'
14 import { ApplicationEvent } from '../Application/ApplicationEvent'
15 import { WordCountEvent } from './WordCountEvent'
16 import type { EditorEvent, EditorEventData } from '@proton/docs-shared'
18 /** Handle messages sent by the editor to the client */
19 export class EditorToClientRequestHandler implements EditorRequiresClientMethods {
20   constructor(
21     private editorFrame: HTMLIFrameElement,
22     private readonly docOrchestrator: EditorOrchestratorInterface,
23     private readonly eventBus: InternalEventBusInterface,
24   ) {}
26   async editorRequestsPropagationOfUpdate(message: RtsMessagePayload, debugSource: BroadcastSource): Promise<void> {
27     return this.docOrchestrator.editorRequestsPropagationOfUpdate(message, debugSource)
28   }
30   async editorReportingEvent(event: EditorEvent, data: EditorEventData[EditorEvent]): Promise<void> {
31     return this.docOrchestrator.editorReportingEvent(event, data)
32   }
34   async getTypersExcludingSelf(threadId: string): Promise<string[]> {
35     return this.docOrchestrator.getTypersExcludingSelf(threadId)
36   }
38   async createComment(content: string, threadID: string): Promise<CommentInterface | undefined> {
39     return this.docOrchestrator.createComment(content, threadID)
40   }
42   async beganTypingInThread(threadID: string): Promise<void> {
43     return this.docOrchestrator.beganTypingInThread(threadID)
44   }
46   async stoppedTypingInThread(threadID: string): Promise<void> {
47     return this.docOrchestrator.stoppedTypingInThread(threadID)
48   }
50   async unresolveThread(threadId: string): Promise<boolean> {
51     return this.docOrchestrator.unresolveThread(threadId)
52   }
54   async editComment(threadID: string, commentID: string, content: string): Promise<boolean> {
55     return this.docOrchestrator.editComment(threadID, commentID, content)
56   }
58   async deleteComment(threadID: string, commentID: string): Promise<boolean> {
59     return this.docOrchestrator.deleteComment(threadID, commentID)
60   }
62   async getAllThreads(): Promise<CommentThreadInterface[]> {
63     return this.docOrchestrator.getAllThreads()
64   }
66   async createCommentThread(
67     commentContent: string,
68     markID?: string,
69     createMarkNode?: boolean,
70   ): Promise<CommentThreadInterface | undefined> {
71     return this.docOrchestrator.createCommentThread(commentContent, markID, createMarkNode)
72   }
74   async createSuggestionThread(
75     suggestionID: string,
76     commentContent: string,
77     suggestionType: SuggestionSummaryType,
78   ): Promise<CommentThreadInterface | undefined> {
79     return this.docOrchestrator.createSuggestionThread(suggestionID, commentContent, suggestionType)
80   }
82   async resolveThread(threadId: string): Promise<boolean> {
83     return this.docOrchestrator.resolveThread(threadId)
84   }
86   async acceptSuggestion(threadId: string, summary: string): Promise<boolean> {
87     return this.docOrchestrator.acceptSuggestion(threadId, summary)
88   }
90   async rejectSuggestion(threadId: string, summary?: string): Promise<boolean> {
91     return this.docOrchestrator.rejectSuggestion(threadId, summary)
92   }
94   async reopenSuggestion(threadId: string): Promise<boolean> {
95     return this.docOrchestrator.reopenSuggestion(threadId)
96   }
98   async deleteThread(id: string): Promise<boolean> {
99     return this.docOrchestrator.deleteThread(id)
100   }
102   async markThreadAsRead(id: string): Promise<void> {
103     return this.docOrchestrator.markThreadAsRead(id)
104   }
106   async handleAwarenessStateUpdate(states: UserState[]): Promise<void> {
107     return this.docOrchestrator.handleAwarenessStateUpdate(states)
108   }
110   async openLink(url: string): Promise<void> {
111     const link = document.createElement('a')
112     link.href = url
113     link.target = '_blank'
114     link.rel = 'noopener noreferrer'
115     link.click()
116     link.remove()
117   }
119   async reportUserInterfaceError(
120     error: Error,
121     extraInfo?: { irrecoverable?: boolean; errorInfo?: ErrorInfo; lockEditor?: boolean },
122   ): Promise<void> {
123     this.docOrchestrator.editorReportingError(error.message, {
124       irrecoverable: extraInfo?.irrecoverable,
125       lockEditor: extraInfo?.lockEditor,
126     })
127   }
129   async reportWordCount(wordCountInfo: WordCountInfoCollection): Promise<void> {
130     this.eventBus.publish({
131       type: WordCountEvent,
132       payload: wordCountInfo,
133     })
134   }
136   updateFrameSize(size: number): void {
137     if (this.editorFrame) {
138       this.editorFrame.style.setProperty('--print-min-height', `${size}px`)
139     }
140   }
142   showGenericAlertModal(message: string): void {
143     this.eventBus.publish({
144       type: ApplicationEvent.GeneralUserDisplayableErrorOccurred,
145       payload: {
146         translatedError: message,
147       },
148     })
149   }
151   async fetchExternalImageAsBase64(url: string): Promise<string | undefined> {
152     return this.docOrchestrator.fetchExternalImageAsBase64(url)
153   }