-
Notifications
You must be signed in to change notification settings - Fork 0
/
cantooApi.js
399 lines (354 loc) · 11.2 KB
/
cantooApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//@ts-check
const hosts = {
develop: 'https://develop.cantoo.fr',
preprod: 'https://preprod.cantoo.fr',
prod: 'https://cantoo.fr'
}
/**
* @typedef {Object} ConnectProps
* @property {HTMLElement} domElement The DOM node the iframe should be attached to
*/
/**
* @typedef {'ready' | 'completed' | 'destroyed' | 'logout'} EventType
*/
/**
* @typedef {Object} ReadyEvent
* @property {string} userId The id of the user currently logged in the app
* @property {string} fileId The id of the file being viewed/edited
*/
/**
* @typedef {Object} LogoutEvent
* @property {string} userId The id of the user logging out of the app
*/
/**
* @typedef {Object} CompletedEvent
* @property {string} userId The id of the user currently logged in the app
* @property {string} fileId The id of the file being viewed/edited
* @property {string} title The title of the file being viewed/edited
*/
/**
* @callback ReadyHandler
* @param {ReadyEvent} event The "ready" event
* @return {void}
*/
/**
* @callback LogoutHandler
* @param {LogoutEvent} event The "ready" event
* @return {void}
*/
/**
* @callback CompletedHandler
* @param {CompletedEvent} event The "completed" event
* @return {void}
*/
/**
* @callback BasicEventHandler
* @return {void}
*/
/**
* @template {EventType} EventName
* @typedef {EventName extends 'ready' ? ReadyHandler : EventName extends 'completed' ? CompletedEvent : BasicEventHandler} EventHandler
*/
/**
* @template {EventType} EventName
* @callback EventListener
* @param {EventName} eventName The Event to listen to
* @param {EventHandler<EventName>} listener The event listener
* @returns {void}
*/
/**
* @typedef {Object} UrlProps
* @property {string} userId The useId that will own the edited document
* @property {string} idEnt The idEnt of the user. Will be used for authentication.
* @property {string} uai The uai of the user. Will be used for authentication.
* @property {'develop'|'preprod'|'prod'} env The current environment
* @property {boolean} readOnly Should we open a viewer or an editor?
*/
/**
* @typedef {Object} FileCreationProp
* @property {undefined=} fileId The id of the document to edit.
* @property {string} title The title of the new document. This is required if no fileId is provided
* @property {false=} readOnly This should be false when creating a new document
* @property {'cabri'=} template The template model to load in the editor
*/
/**
* @typedef {Object} FileLoadingProp
* @property {string} fileId The id of the document to edit.
* @property {string=} title The title of the new document. This is required if no fileId is provided
* @property {boolean=} readOnly The title of the new document. This is required if no fileId is provided
* @property {undefined=} template The template model to load in the editor
*/
/**
*
* @typedef {Object} Callbacks
* @property {((id: string, userId: string) => void)[]} ready
* @property {((id: string, title: string, userId: string) => void)[]} completed
* @property {(() => void)[]} destroyed
*/
/**
* @param {UrlProps & (FileCreationProp | FileLoadingProp)} props
* @return {string}
*/
function buildUrl({ env, ...props }) {
const host = hosts[env]
if (!host) throw new Error(`${env} is not a valid environment value. Try 'develop', 'preprod' or 'prod'.`)
const query = Object.entries(props)
.map(([key, value]) => value === true ? key : value ? `${key}=${value}` : undefined)
.filter(entry => !!entry)
.join('&')
return `${host}/api/embed?${query}`
}
class CantooAPI {
/**
* The current state of the iFrame
* @type {'launching'|'ready'|'completed'|'destroyed'}
* @readonly
*/
state = 'launching'
/**
* Holds the listeners for each event type
* @type {{
* ready: ReadyHandler[]
* completed: CompletedHandler[]
* destroyed: BasicEventHandler[]
* logout: LogoutHandler[]
* }}
*/
callbacks = {
/** @type {ReadyHandler[]} */
ready: [],
/** @type {CompletedHandler[]} */
completed: [],
/** @type {BasicEventHandler[]} */
destroyed: [],
/** @type {LogoutHandler[]} */
logout: []
}
/**
* The parent DOM element the iframe is attatched to
* @type {ConnectProps['domElement']}
* @private
*/
domElement
/**
* The current environment
* @type {UrlProps['env']}
* @private
*/
env
/**
* The Id of the file currently shown in the iFrame
* @type {FileCreationProp['title']=}
* @private
*/
title
/**
* The template to be loaded when create a document
* @type {FileCreationProp['template']=}
* @private
*/
template
/**
* The Id of the file currently shown in the iFrame
* @type {FileLoadingProp['fileId']=}
* @private
*/
fileId
/**
* The Id of the user's ENT
* @type {UrlProps['idEnt']}
* @private
*/
idEnt
/**
* The establishment Id
* @type {UrlProps['uai']}
* @private
*/
uai
/**
* The user Id currently logged in the app
* @type {UrlProps['userId']}
* @private
*/
userId
/**
* The iframe DOM node
* @type {HTMLIFrameElement}
* @private
*/
iframe
/**
* Should the document be made read only?
* @type {boolean}
* @private
*/
readOnly
/**
* The listener listening to postMessage events
* @type {(event: MessageEvent) => void}
* @private
*/
postMessageListener
/**
* Create a CantooApi object that you can use to create and control a Cantoo Scribe iframe
* @param {ConnectProps & UrlProps & (FileCreationProp | FileLoadingProp)} params
*/
constructor({ domElement, env, idEnt, uai, userId, readOnly, ...props }) {
this.domElement = domElement
this.env = env
this.idEnt = idEnt
this.uai = uai
this.userId = userId
this.readOnly = readOnly
this.title = /** @type {FileCreationProp} */(props).title
this.template = /** @type {FileCreationProp} */(props).template
this.fileId = /** @type {FileLoadingProp} */(props).fileId
this.iframe = document.createElement('iframe')
this.iframe.allow = "fullscreen *; camera *; display-capture *; local-fonts *; microphone *"
this.iframe.src = this._buildUrl()
this.iframe.setAttribute('style', 'flex: 1 1 0;align-self: stretch;')
domElement.appendChild(this.iframe)
this.postMessageListener = event => {
const type = event.data?.type
if (['ready', 'completed', 'destroyed'].includes(type)) {
this.setState(type)
if(type === 'destroyed') this._doDestroy()
else this.callbacks[type].forEach(listener => listener(event.data))
}
}
// this might cause a memory leak if destroy is not called
window.addEventListener('message', this.postMessageListener)
}
/**
* @param {'launching'|'ready'|'completed'|'destroyed'} state
* @private
*/
setState = (state) => {
// @ts-ignore
this.state = state
}
/**
* Create an iframe running Cantoo Scribe for the provided user
* @param {ConnectProps & UrlProps & (FileCreationProp | FileLoadingProp)} props
* @return {Promise<CantooAPI>} Returns a CantooApi object that lets you interact with the iframe. The
*/
static async connect(props) {
const api = new CantooAPI(props)
return /** @type {Promise<CantooAPI>} */(new Promise((resolve, reject) => {
/** @type {ReadyHandler} */
const callback = (event) => {
api.removeEventListener('ready', callback)
api.fileId = event.fileId
resolve(api)
}
api.addEventListener('ready', callback)
setTimeout(() =>
reject(new Error('The iframe took more than 5 minute to open. Timeout.'))
, 5 * 60000)
}).catch(err => {
// If the iframe failed to load, we destroy it
api.destroy()
throw err
}))
}
_buildUrl = () => buildUrl({
env: this.env,
idEnt: this.idEnt,
readOnly: this.readOnly,
uai: this.uai,
userId: this.userId,
// We have to type as if we were creating or loading a file (here, loading)
fileId: /** @type {string} **/(this.fileId),
title: this.title,
template: /** @type {undefined} **/(this.template)
})
/**
* Load the specified document in Cantoo.
* @param {string} fileId The document id
* @param {boolean=} readOnly Should this viewer be made read only?
* @return {Promise<void>} A promise that will resolve when the document was loaded and the ready event was received.
*/
loadDocument = (fileId, readOnly) => {
this.fileId = fileId
this.title = undefined
this.readOnly = !!readOnly
this.template = undefined
this.iframe.src = this._buildUrl()
return /** @type {Promise<void>} */(new Promise((resolve, reject) => {
const callback = () => {
this.removeEventListener('ready', callback)
resolve()
}
this.addEventListener('ready', callback)
setTimeout(() => {
reject(new Error('Loading the document took more than 60s. Timeout.'))
this.removeEventListener('ready', callback)
}, 60000)
})).catch(err => {
this.destroy()
throw err
})
}
/**
* This method will destroy the iframe, remove existing event listeners and release all resources. Don't forget to call it when you get rid of CantooAPI object.
* @returns {Promise<void>}
*/
async destroy () {
return /** @type {Promise<void>} */(new Promise(((resolve, reject) => {
try {
if(!this.iframe?.contentWindow) return reject(new Error('iframe doesn\'t exist'))
const onDestroy = () => {
resolve()
this.removeEventListener('destroyed', onDestroy)
}
this.addEventListener('destroyed', onDestroy)
this.iframe.contentWindow.postMessage({ type: 'close' }, '*')
// Reject after 10s timeout
setTimeout(() => reject(new Error('The iframe didn\'t respond within 10s. Destroying anyway')), 10000)
} catch (err) {
reject(err)
}
}).bind(this))).catch(err => {
this._doDestroy()
throw err
})
}
/**
* Remove the listeners and destroy the iframe, cutting all comunications with Cantoo Scribe
* @private
*/
_doDestroy = () => {
window.removeEventListener('message', this.postMessageListener)
this.domElement.removeChild(this.iframe)
this.setState('destroyed')
this.callbacks['destroyed'].forEach(callback => callback())
this.callbacks = {
ready: [],
completed: [],
destroyed: [],
logout: []
}
}
/**
* Add an event listener to one of the events
* @template {EventType} EventName
* @param {EventName} eventName The event to listen to
* @param {EventHandler<EventName>} listener The event listener
* @return {void}
*/
addEventListener = (eventName, listener) => {
this.callbacks[eventName].push(/** @type {BasicEventHandler} */(listener))
}
/**
* Remove an existing event listener. Be careful, if you added the same listener twice, all of them will be removed.
* @template {EventType} EventName
* @param {EventName} eventName The event the listener was recording
* @param {EventHandler<EventName>} listener The event listener
* @return {void}
*/
removeEventListener = (eventName, listener) => {
this.callbacks[/** @type {'destroyed'} **/(eventName)] = /** @type {BasicEventHandler[]} */(this.callbacks[eventName].filter(c => c !== listener))
}
}
export default CantooAPI