@netless/fastboard-core 0.3.2-canary.5 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,555 +1,602 @@
1
- import type { AddPageParams, PublicEvent, MountParams } from "@netless/window-manager";
2
- import type {
3
- AnimationMode,
4
- ApplianceNames,
5
- Camera,
6
- Color,
7
- ConversionResponse,
8
- HotKey,
9
- HotKeys,
10
- JoinRoomParams,
11
- MemberState,
12
- Rectangle,
13
- Room,
14
- RoomPhase as RoomPhaseEnum,
15
- RoomCallbacks,
16
- RoomState,
17
- SceneDefinition,
18
- ShapeType,
19
- ViewCallbacks,
20
- WhiteWebSdkConfiguration,
21
- } from "white-web-sdk";
22
-
23
- import { DefaultHotKeys, WhiteWebSdk, contentModeScale } from "white-web-sdk";
24
- import { BuiltinApps, WindowManager } from "@netless/window-manager";
25
- import {
26
- getImageSize,
27
- genUID,
28
- convertedFileToScene,
29
- makeSlideParams,
30
- readable,
31
- writable,
32
- warn,
33
- } from "../utils";
34
- import { ensure_window_manager, transform_app_status } from "../internal";
35
-
36
- class FastboardAppBase {
37
- public constructor(
38
- readonly sdk: WhiteWebSdk,
39
- readonly room: Room,
40
- readonly manager: WindowManager,
41
- readonly hotKeys: Partial<HotKeys>
42
- ) {}
43
-
44
- protected _destroyed = false;
45
- protected _assertNotDestroyed() {
46
- if (this._destroyed) {
47
- throw new Error("FastboardApp has been destroyed");
48
- }
49
- }
50
-
51
- protected _addRoomListener<K extends keyof RoomCallbacks>(name: K, listener: RoomCallbacks[K]) {
52
- this._assertNotDestroyed();
53
- this.room.callbacks.on(name, listener);
54
- return () => this.room.callbacks.off(name, listener);
55
- }
56
-
57
- protected _addManagerListener<K extends keyof PublicEvent>(
58
- name: K,
59
- listener: (value: PublicEvent[K]) => void
60
- ) {
61
- this._assertNotDestroyed();
62
- this.manager.emitter.on(name, listener);
63
- return () => this.manager.emitter.off(name, listener);
64
- }
65
-
66
- protected _addMainViewListener<K extends keyof ViewCallbacks>(name: K, listener: ViewCallbacks[K]) {
67
- this._assertNotDestroyed();
68
- this.manager.mainView.callbacks.on(name, listener);
69
- return () => this.manager.mainView.callbacks.off(name, listener);
70
- }
71
-
72
- public destroy() {
73
- this._destroyed = true;
74
- this.manager.destroy();
75
- return this.room.disconnect();
76
- }
77
- }
78
-
79
- type RoomPhase = `${RoomPhaseEnum}`;
80
-
81
- export type {
82
- AddPageParams,
83
- AnimationMode,
84
- ApplianceNames,
85
- Camera,
86
- Color,
87
- ConversionResponse,
88
- HotKey,
89
- HotKeys,
90
- JoinRoomParams,
91
- MemberState,
92
- MountParams,
93
- PublicEvent,
94
- Rectangle,
95
- Room,
96
- RoomPhase,
97
- RoomCallbacks,
98
- RoomState,
99
- SceneDefinition,
100
- ShapeType,
101
- ViewCallbacks,
102
- WhiteWebSdk,
103
- WhiteWebSdkConfiguration,
104
- WindowManager,
105
- };
106
-
107
- export type Appliance = `${ApplianceNames}`;
108
- export type Shape = `${ShapeType}`;
109
-
110
- export interface InsertDocsStatic {
111
- readonly fileType: "pdf";
112
- readonly scenePath: string;
113
- readonly scenes: SceneDefinition[];
114
- readonly title?: string;
115
- }
116
-
117
- export interface InsertDocsDynamic {
118
- readonly fileType: "pptx";
119
- readonly scenePath: string;
120
- readonly taskId: string;
121
- readonly title?: string;
122
- readonly url?: string;
123
- /** @example [{ name: '1' }, { name: '2' }, { name: '3' }] */
124
- readonly scenes?: SceneDefinition[];
125
- }
126
-
127
- export type InsertDocsParams = InsertDocsStatic | InsertDocsDynamic;
128
-
129
- export type SetMemberStateFn = (partialMemberState: Partial<MemberState>) => void;
130
-
131
- export type RoomStateChanged = (diff: Partial<RoomState>) => void;
132
-
133
- export interface AppsStatus {
134
- [kind: string]: {
135
- status: "idle" | "loading" | "failed";
136
- reason?: string;
137
- };
138
- }
139
-
140
- export class FastboardApp extends FastboardAppBase {
141
- /**
142
- * Render this app to some DOM.
143
- */
144
- bindContainer(container: HTMLElement) {
145
- this._assertNotDestroyed();
146
- this.manager.bindContainer(container);
147
- }
148
-
149
- /**
150
- * Move window-manager's collector to some place.
151
- */
152
- bindCollector(container: HTMLElement) {
153
- this._assertNotDestroyed();
154
- this.manager.bindCollectorContainer(container);
155
- }
156
-
157
- /**
158
- * Is current room writable?
159
- */
160
- readonly writable = writable(
161
- this.room.isWritable,
162
- set => {
163
- this._addRoomListener("onEnableWriteNowChanged", () => set(this.room.isWritable));
164
- set(this.room.isWritable);
165
- },
166
- this.room.setWritable.bind(this.room)
167
- );
168
-
169
- /**
170
- * Is current room online?
171
- */
172
- readonly phase = readable<RoomPhase>(this.room.phase, set => {
173
- this._addRoomListener("onPhaseChanged", set);
174
- set(this.room.phase);
175
- });
176
-
177
- /**
178
- * Current window-manager's windows' state (is it maximized?).
179
- */
180
- readonly boxState = readable(this.manager.boxState, set => {
181
- this._addManagerListener("boxStateChange", set);
182
- set(this.manager.boxState);
183
- });
184
-
185
- /**
186
- * Current window-manager's focused app's id.
187
- * @example "HelloWorld-1A2b3C4d"
188
- */
189
- readonly focusedApp = readable(this.manager.focused, set => {
190
- this._addManagerListener("focusedChange", set);
191
- set(this.manager.focused);
192
- });
193
-
194
- /**
195
- * How many times can I call `app.redo()`?
196
- */
197
- readonly canRedoSteps = readable(this.manager.canRedoSteps, set => {
198
- this._addManagerListener("canRedoStepsChange", set);
199
- set(this.manager.canRedoSteps);
200
- });
201
-
202
- /**
203
- * How many times can I call `app.undo()`?
204
- */
205
- readonly canUndoSteps = readable(this.manager.canUndoSteps, set => {
206
- this._addManagerListener("canUndoStepsChange", set);
207
- set(this.manager.canUndoSteps);
208
- });
209
-
210
- /**
211
- * Current camera information of main view.
212
- *
213
- * Change the camera position by `app.moveCamera()`.
214
- */
215
- readonly camera = readable(this.manager.camera, set => {
216
- this._addMainViewListener("onCameraUpdated", set);
217
- set(this.manager.camera);
218
- });
219
-
220
- /**
221
- * Current tool's info, like "is using pencil?", "what color?".
222
- *
223
- * Change the tool by `app.setAppliance()`.
224
- */
225
- readonly memberState = readable(this.room.state.memberState, set => {
226
- this._addRoomListener("onRoomStateChanged", ({ memberState: m }) => m && set(m));
227
- set(this.room.state.memberState);
228
- });
229
-
230
- /**
231
- * 0..n-1, current index of main view scenes.
232
- */
233
- readonly sceneIndex = writable(
234
- this.manager.mainViewSceneIndex,
235
- set => {
236
- this._addManagerListener("mainViewSceneIndexChange", set);
237
- set(this.manager.mainViewSceneIndex);
238
- },
239
- this.manager.setMainViewSceneIndex.bind(this.manager)
240
- );
241
-
242
- /**
243
- * How many pages are in the main view?
244
- */
245
- readonly sceneLength = readable(this.manager.mainViewScenesLength, set => {
246
- this._addManagerListener("mainViewScenesLengthChange", set);
247
- set(this.manager.mainViewScenesLength);
248
- });
249
-
250
- private _appsStatus: AppsStatus = {};
251
- /**
252
- * Apps status.
253
- */
254
- readonly appsStatus = readable<AppsStatus>({}, set =>
255
- this._addManagerListener("loadApp", ({ kind, status, reason }) => {
256
- this._appsStatus[kind] = { status: transform_app_status(status), reason };
257
- set(this._appsStatus);
258
- })
259
- );
260
-
261
- /**
262
- * Undo a step on main view.
263
- */
264
- undo() {
265
- this._assertNotDestroyed();
266
- this.manager.undo();
267
- }
268
-
269
- /**
270
- * Redo a step on main view.
271
- */
272
- redo() {
273
- this._assertNotDestroyed();
274
- this.manager.redo();
275
- }
276
-
277
- /**
278
- * Move current main view's camera position.
279
- */
280
- moveCamera(camera: Partial<Camera> & { animationMode?: AnimationMode | undefined }) {
281
- this._assertNotDestroyed();
282
- this.manager.moveCamera(camera);
283
- }
284
-
285
- /**
286
- * Move current main view's camera to include a rectangle.
287
- */
288
- moveCameraToContain(rectangle: Rectangle & { animationMode?: AnimationMode }) {
289
- this._assertNotDestroyed();
290
- this.manager.moveCameraToContain(rectangle);
291
- }
292
-
293
- /**
294
- * Delete all things on the main view.
295
- */
296
- cleanCurrentScene() {
297
- this._assertNotDestroyed();
298
- this.manager.cleanCurrentScene();
299
- }
300
-
301
- /**
302
- * Set current tool, like "pencil".
303
- */
304
- setAppliance(appliance: ApplianceNames | Appliance, shape?: ShapeType | Shape) {
305
- this._assertNotDestroyed();
306
- this.manager.mainView.setMemberState({
307
- currentApplianceName: appliance as ApplianceNames,
308
- shapeType: shape as ShapeType,
309
- });
310
- }
311
-
312
- setStrokeWidth(strokeWidth: number) {
313
- this._assertNotDestroyed();
314
- this.manager.mainView.setMemberState({ strokeWidth });
315
- }
316
-
317
- setStrokeColor(strokeColor: Color) {
318
- this._assertNotDestroyed();
319
- this.manager.mainView.setMemberState({ strokeColor });
320
- }
321
-
322
- setTextSize(textSize: number) {
323
- this._assertNotDestroyed();
324
- this.manager.mainView.setMemberState({ textSize });
325
- }
326
-
327
- setTextColor(textColor: Color) {
328
- this._assertNotDestroyed();
329
- this.manager.mainView.setMemberState({ textColor });
330
- }
331
-
332
- prevPage() {
333
- this._assertNotDestroyed();
334
- return this.manager.prevPage();
335
- }
336
-
337
- nextPage() {
338
- this._assertNotDestroyed();
339
- return this.manager.nextPage();
340
- }
341
-
342
- addPage(params?: AddPageParams) {
343
- this._assertNotDestroyed();
344
- return this.manager.addPage(params);
345
- }
346
-
347
- /**
348
- * Insert an image to the main view.
349
- */
350
- async insertImage(url: string) {
351
- this._assertNotDestroyed();
352
- await this.manager.switchMainViewToWriter();
353
-
354
- const { divElement } = this.manager.mainView;
355
- const containerSize = {
356
- width: divElement?.scrollWidth || window.innerWidth,
357
- height: divElement?.scrollHeight || window.innerHeight,
358
- };
359
-
360
- // 1. shrink the image a little to fit container **width**
361
- const maxWidth = containerSize.width * 0.8;
362
- let { width, height } = await getImageSize(url, containerSize);
363
- const scale = Math.min(maxWidth / width, 1);
364
- const uuid = genUID();
365
- const { centerX, centerY } = this.manager.camera;
366
- width *= scale;
367
- height *= scale;
368
- this.manager.mainView.insertImage({ uuid, centerX, centerY, width, height, locked: false });
369
- this.manager.mainView.completeImageUpload(uuid, url);
370
-
371
- // 2. move camera to fit image **height**
372
- width /= 0.8;
373
- height /= 0.8;
374
- const originX = centerX - width / 2;
375
- const originY = centerY - height / 2;
376
- this.manager.moveCameraToContain({ originX, originY, width, height });
377
- }
378
-
379
- /**
380
- * Insert PDF/PPTX from conversion result.
381
- * @param status https://developer.netless.link/server-en/home/server-conversion#get-query-task-conversion-progress
382
- */
383
- insertDocs(filename: string, status: ConversionResponse): Promise<string | undefined>;
384
-
385
- /**
386
- * Manual way.
387
- * @example
388
- * app.insertDocs({
389
- * fileType: 'pptx',
390
- * scenePath: `/pptx/${conversion.taskId}`,
391
- * taskId: conversion.taskId,
392
- * title: 'Title',
393
- * })
394
- */
395
- insertDocs(params: InsertDocsParams): Promise<string | undefined>;
396
-
397
- insertDocs(arg1: string | InsertDocsParams, arg2?: ConversionResponse) {
398
- this._assertNotDestroyed();
399
- if (typeof arg1 === "object" && "fileType" in arg1) {
400
- return this._insertDocsImpl(arg1);
401
- } else if (arg2 && arg2.status !== "Finished") {
402
- throw new Error("FastboardApp cannot insert a converting doc.");
403
- } else if (arg2 && arg2.progress) {
404
- const title = arg1;
405
- const scenePath = `/${arg2.uuid}/${genUID()}`;
406
- const scenes1 = arg2.progress.convertedFileList.map(convertedFileToScene);
407
- const { scenes, taskId, url } = makeSlideParams(scenes1);
408
- if (taskId && url) {
409
- return this._insertDocsImpl({ fileType: "pptx", scenePath, scenes, title, taskId, url });
410
- } else {
411
- return this._insertDocsImpl({ fileType: "pdf", scenePath, scenes: scenes1, title });
412
- }
413
- }
414
- }
415
-
416
- private _insertDocsImpl({ fileType, scenePath, title, scenes, ...attributes }: InsertDocsParams) {
417
- this._assertNotDestroyed();
418
- switch (fileType) {
419
- case "pdf":
420
- return this.manager.addApp({
421
- kind: "DocsViewer",
422
- options: { scenePath, title, scenes },
423
- });
424
- case "pptx":
425
- if (scenes && scenes[0].ppt) {
426
- warn("no-ppt-in-scenes");
427
- }
428
- return this.manager.addApp({
429
- kind: "Slide",
430
- options: { scenePath, title, scenes },
431
- attributes,
432
- });
433
- }
434
- }
435
-
436
- /**
437
- * Insert the Media Player app.
438
- */
439
- insertMedia(title: string, src: string) {
440
- this._assertNotDestroyed();
441
- return this.manager.addApp({
442
- kind: BuiltinApps.MediaPlayer,
443
- options: { title },
444
- attributes: { src },
445
- });
446
- }
447
-
448
- /**
449
- * Insert the Monaco Code Editor app.
450
- */
451
- insertCodeEditor() {
452
- this._assertNotDestroyed();
453
- return this.manager.addApp({
454
- kind: "Monaco",
455
- options: { title: "Code Editor" },
456
- });
457
- }
458
-
459
- /**
460
- * Insert the Countdown app.
461
- */
462
- insertCountdown() {
463
- this._assertNotDestroyed();
464
- return this.manager.addApp({
465
- kind: "Countdown",
466
- options: { title: "Countdown" },
467
- });
468
- }
469
-
470
- /**
471
- * Insert the GeoGebra app.
472
- */
473
- insertGeoGebra() {
474
- this._assertNotDestroyed();
475
- return this.manager.addApp({
476
- kind: "GeoGebra",
477
- options: { title: "GeoGebra" },
478
- });
479
- }
480
- }
481
-
482
- export interface FastboardOptions {
483
- sdkConfig: Omit<WhiteWebSdkConfiguration, "useMobXState"> & {
484
- region: NonNullable<WhiteWebSdkConfiguration["region"]>;
485
- };
486
- joinRoom: Omit<JoinRoomParams, "useMultiViews" | "disableNewPencil" | "disableMagixEventDispatchLimit"> & {
487
- callbacks?: Partial<RoomCallbacks>;
488
- };
489
- managerConfig?: Omit<MountParams, "room">;
490
- }
491
-
492
- /**
493
- * Create a FastboardApp instance.
494
- * @example
495
- * let app = await createFastboard({
496
- * sdkConfig: {
497
- * appIdentifier: import.meta.env.VITE_APPID,
498
- * region: 'cn-hz',
499
- * },
500
- * joinRoom: {
501
- * uid: unique_id,
502
- * uuid: import.meta.env.VITE_ROOM_UUID,
503
- * roomToken: import.meta.env.VITE_ROOM_TOKEN,
504
- * },
505
- * })
506
- */
507
- export async function createFastboard({
508
- sdkConfig,
509
- joinRoom: { callbacks, ...joinRoomParams },
510
- managerConfig,
511
- }: FastboardOptions) {
512
- const sdk = new WhiteWebSdk({
513
- ...sdkConfig,
514
- useMobXState: true,
515
- });
516
-
517
- const hotKeys: Partial<HotKeys> = {
518
- ...DefaultHotKeys,
519
- changeToSelector: "s",
520
- changeToLaserPointer: "z",
521
- changeToPencil: "p",
522
- changeToRectangle: "r",
523
- changeToEllipse: "c",
524
- changeToEraser: "e",
525
- changeToText: "t",
526
- changeToStraight: "l",
527
- changeToArrow: "a",
528
- changeToHand: "h",
529
- };
530
-
531
- const room = await sdk.joinRoom(
532
- {
533
- floatBar: true,
534
- hotKeys,
535
- ...ensure_window_manager(joinRoomParams),
536
- useMultiViews: true,
537
- disableNewPencil: false,
538
- disableMagixEventDispatchLimit: true,
539
- },
540
- callbacks
541
- );
542
-
543
- const manager = await WindowManager.mount({
544
- cursor: true,
545
- ...managerConfig,
546
- room,
547
- });
548
-
549
- manager.mainView.setCameraBound({
550
- minContentMode: contentModeScale(0.3),
551
- maxContentMode: contentModeScale(3),
552
- });
553
-
554
- return new FastboardApp(sdk, room, manager, hotKeys);
555
- }
1
+ import type { AddPageParams, PublicEvent, MountParams } from "@netless/window-manager";
2
+ import type {
3
+ AnimationMode,
4
+ ApplianceNames,
5
+ Camera,
6
+ Color,
7
+ ConversionResponse,
8
+ HotKey,
9
+ HotKeys,
10
+ JoinRoomParams,
11
+ MemberState,
12
+ Rectangle,
13
+ Room,
14
+ RoomPhase as RoomPhaseEnum,
15
+ RoomCallbacks,
16
+ RoomState,
17
+ SceneDefinition,
18
+ ShapeType,
19
+ ViewCallbacks,
20
+ WhiteWebSdkConfiguration,
21
+ } from "white-web-sdk";
22
+
23
+ import { DefaultHotKeys, WhiteWebSdk, contentModeScale } from "white-web-sdk";
24
+ import { BuiltinApps, WindowManager } from "@netless/window-manager";
25
+ import {
26
+ getImageSize,
27
+ genUID,
28
+ convertedFileToScene,
29
+ makeSlideParams,
30
+ readable,
31
+ writable,
32
+ warn,
33
+ } from "../utils";
34
+ import { ensure_window_manager, transform_app_status } from "../internal";
35
+
36
+ class FastboardAppBase {
37
+ public constructor(
38
+ readonly sdk: WhiteWebSdk,
39
+ readonly room: Room,
40
+ readonly manager: WindowManager,
41
+ readonly hotKeys: Partial<HotKeys>
42
+ ) {}
43
+
44
+ protected _destroyed = false;
45
+ protected _assertNotDestroyed() {
46
+ if (this._destroyed) {
47
+ throw new Error("FastboardApp has been destroyed");
48
+ }
49
+ }
50
+
51
+ protected _addRoomListener<K extends keyof RoomCallbacks>(name: K, listener: RoomCallbacks[K]) {
52
+ this._assertNotDestroyed();
53
+ this.room.callbacks.on(name, listener);
54
+ return () => this.room.callbacks.off(name, listener);
55
+ }
56
+
57
+ protected _addManagerListener<K extends keyof PublicEvent>(
58
+ name: K,
59
+ listener: (value: PublicEvent[K]) => void
60
+ ) {
61
+ this._assertNotDestroyed();
62
+ this.manager.emitter.on(name, listener);
63
+ return () => this.manager.emitter.off(name, listener);
64
+ }
65
+
66
+ protected _addMainViewListener<K extends keyof ViewCallbacks>(name: K, listener: ViewCallbacks[K]) {
67
+ this._assertNotDestroyed();
68
+ this.manager.mainView.callbacks.on(name, listener);
69
+ return () => this.manager.mainView.callbacks.off(name, listener);
70
+ }
71
+
72
+ /**
73
+ * Destroy fastboard (disconnect from the whiteboard room).
74
+ */
75
+ public destroy() {
76
+ this._destroyed = true;
77
+ this.manager.destroy();
78
+ return this.room.disconnect();
79
+ }
80
+ }
81
+
82
+ type RoomPhase = `${RoomPhaseEnum}`;
83
+
84
+ export type {
85
+ AddPageParams,
86
+ AnimationMode,
87
+ ApplianceNames,
88
+ Camera,
89
+ Color,
90
+ ConversionResponse,
91
+ HotKey,
92
+ HotKeys,
93
+ JoinRoomParams,
94
+ MemberState,
95
+ MountParams,
96
+ PublicEvent,
97
+ Rectangle,
98
+ Room,
99
+ RoomPhase,
100
+ RoomCallbacks,
101
+ RoomState,
102
+ SceneDefinition,
103
+ ShapeType,
104
+ ViewCallbacks,
105
+ WhiteWebSdk,
106
+ WhiteWebSdkConfiguration,
107
+ WindowManager,
108
+ };
109
+
110
+ /** pencil, eraser, rectangle... */
111
+ export type Appliance = `${ApplianceNames}`;
112
+ /** triangle, star... */
113
+ export type Shape = `${ShapeType}`;
114
+
115
+ /** Params for static docs, they are rendered as many images. */
116
+ export interface InsertDocsStatic {
117
+ readonly fileType: "pdf";
118
+ /** Unique string for binding whiteboard view to the doc. Must start with `/`. */
119
+ readonly scenePath: string;
120
+ /** @example [{ name: '1', ppt: { src: 'url/to/ppt/1.png' } }] */
121
+ readonly scenes: SceneDefinition[];
122
+ /** Window title. */
123
+ readonly title?: string;
124
+ }
125
+
126
+ /** Params for slides, they are rendered in @netless/app-slide with animations. */
127
+ export interface InsertDocsDynamic {
128
+ readonly fileType: "pptx";
129
+ /** Unique string for binding whiteboard view to the doc. Must start with `/`. */
130
+ readonly scenePath: string;
131
+ /** Conversion task id, see https://developer.netless.link/server-en/home/server-conversion#get-query-task-conversion-progress. */
132
+ readonly taskId: string;
133
+ /** Window title. */
134
+ readonly title?: string;
135
+ /** Where the slide resource placed. @default `https://convertcdn.netless.link/dynamicConvert` */
136
+ readonly url?: string;
137
+ /** @example [{ name: '1' }, { name: '2' }, { name: '3' }] */
138
+ readonly scenes?: SceneDefinition[];
139
+ }
140
+
141
+ export type InsertDocsParams = InsertDocsStatic | InsertDocsDynamic;
142
+
143
+ export type SetMemberStateFn = (partialMemberState: Partial<MemberState>) => void;
144
+
145
+ export type RoomStateChanged = (diff: Partial<RoomState>) => void;
146
+
147
+ /** App download progress. */
148
+ export interface AppsStatus {
149
+ [kind: string]: {
150
+ status: "idle" | "loading" | "failed";
151
+ /** Exist if status is `failed`. */
152
+ reason?: string;
153
+ };
154
+ }
155
+
156
+ export class FastboardApp extends FastboardAppBase {
157
+ /**
158
+ * Render this app to some DOM.
159
+ */
160
+ bindContainer(container: HTMLElement) {
161
+ this._assertNotDestroyed();
162
+ this.manager.bindContainer(container);
163
+ }
164
+
165
+ /**
166
+ * Move window-manager's collector to some place.
167
+ */
168
+ bindCollector(container: HTMLElement) {
169
+ this._assertNotDestroyed();
170
+ this.manager.bindCollectorContainer(container);
171
+ }
172
+
173
+ /**
174
+ * Is current room writable?
175
+ */
176
+ readonly writable = writable(
177
+ this.room.isWritable,
178
+ set => {
179
+ this._addRoomListener("onEnableWriteNowChanged", () => set(this.room.isWritable));
180
+ set(this.room.isWritable);
181
+ },
182
+ this.room.setWritable.bind(this.room)
183
+ );
184
+
185
+ /**
186
+ * Is current room online?
187
+ */
188
+ readonly phase = readable<RoomPhase>(this.room.phase, set => {
189
+ this._addRoomListener("onPhaseChanged", set);
190
+ set(this.room.phase);
191
+ });
192
+
193
+ /**
194
+ * Current window-manager's windows' state (is it maximized?).
195
+ */
196
+ readonly boxState = readable(this.manager.boxState, set => {
197
+ this._addManagerListener("boxStateChange", set);
198
+ set(this.manager.boxState);
199
+ });
200
+
201
+ /**
202
+ * Current window-manager's focused app's id.
203
+ * @example "HelloWorld-1A2b3C4d"
204
+ */
205
+ readonly focusedApp = readable(this.manager.focused, set => {
206
+ this._addManagerListener("focusedChange", set);
207
+ set(this.manager.focused);
208
+ });
209
+
210
+ /**
211
+ * How many times can I call `app.redo()`?
212
+ */
213
+ readonly canRedoSteps = readable(this.manager.canRedoSteps, set => {
214
+ this._addManagerListener("canRedoStepsChange", set);
215
+ set(this.manager.canRedoSteps);
216
+ });
217
+
218
+ /**
219
+ * How many times can I call `app.undo()`?
220
+ */
221
+ readonly canUndoSteps = readable(this.manager.canUndoSteps, set => {
222
+ this._addManagerListener("canUndoStepsChange", set);
223
+ set(this.manager.canUndoSteps);
224
+ });
225
+
226
+ /**
227
+ * Current camera information of main view.
228
+ *
229
+ * Change the camera position by `app.moveCamera()`.
230
+ */
231
+ readonly camera = readable(this.manager.camera, set => {
232
+ this._addMainViewListener("onCameraUpdated", set);
233
+ set(this.manager.camera);
234
+ });
235
+
236
+ /**
237
+ * Current tool's info, like "is using pencil?", "what color?".
238
+ *
239
+ * Change the tool by `app.setAppliance()`.
240
+ */
241
+ readonly memberState = readable(this.room.state.memberState, set => {
242
+ this._addRoomListener("onRoomStateChanged", ({ memberState: m }) => m && set(m));
243
+ set(this.room.state.memberState);
244
+ });
245
+
246
+ /**
247
+ * 0..n-1, current index of main view scenes.
248
+ */
249
+ readonly sceneIndex = writable(
250
+ this.manager.mainViewSceneIndex,
251
+ set => {
252
+ this._addManagerListener("mainViewSceneIndexChange", set);
253
+ set(this.manager.mainViewSceneIndex);
254
+ },
255
+ this.manager.setMainViewSceneIndex.bind(this.manager)
256
+ );
257
+
258
+ /**
259
+ * How many pages are in the main view?
260
+ */
261
+ readonly sceneLength = readable(this.manager.mainViewScenesLength, set => {
262
+ this._addManagerListener("mainViewScenesLengthChange", set);
263
+ set(this.manager.mainViewScenesLength);
264
+ });
265
+
266
+ private _appsStatus: AppsStatus = {};
267
+ /**
268
+ * Apps status.
269
+ */
270
+ readonly appsStatus = readable<AppsStatus>({}, set =>
271
+ this._addManagerListener("loadApp", ({ kind, status, reason }) => {
272
+ this._appsStatus[kind] = { status: transform_app_status(status), reason };
273
+ set(this._appsStatus);
274
+ })
275
+ );
276
+
277
+ /**
278
+ * Undo a step on main view.
279
+ */
280
+ undo() {
281
+ this._assertNotDestroyed();
282
+ this.manager.undo();
283
+ }
284
+
285
+ /**
286
+ * Redo a step on main view.
287
+ */
288
+ redo() {
289
+ this._assertNotDestroyed();
290
+ this.manager.redo();
291
+ }
292
+
293
+ /**
294
+ * Move current main view's camera position.
295
+ */
296
+ moveCamera(camera: Partial<Camera> & { animationMode?: AnimationMode | undefined }) {
297
+ this._assertNotDestroyed();
298
+ this.manager.moveCamera(camera);
299
+ }
300
+
301
+ /**
302
+ * Move current main view's camera to include a rectangle.
303
+ */
304
+ moveCameraToContain(rectangle: Rectangle & { animationMode?: AnimationMode }) {
305
+ this._assertNotDestroyed();
306
+ this.manager.moveCameraToContain(rectangle);
307
+ }
308
+
309
+ /**
310
+ * Delete all things on the main view.
311
+ */
312
+ cleanCurrentScene() {
313
+ this._assertNotDestroyed();
314
+ this.manager.cleanCurrentScene();
315
+ }
316
+
317
+ /**
318
+ * Set current tool, like "pencil".
319
+ */
320
+ setAppliance(appliance: ApplianceNames | Appliance, shape?: ShapeType | Shape) {
321
+ this._assertNotDestroyed();
322
+ this.manager.mainView.setMemberState({
323
+ currentApplianceName: appliance as ApplianceNames,
324
+ shapeType: shape as ShapeType,
325
+ });
326
+ }
327
+
328
+ /**
329
+ * Set pencil and shape's thickness.
330
+ */
331
+ setStrokeWidth(strokeWidth: number) {
332
+ this._assertNotDestroyed();
333
+ this.manager.mainView.setMemberState({ strokeWidth });
334
+ }
335
+
336
+ /**
337
+ * Set pencil and shape's color.
338
+ */
339
+ setStrokeColor(strokeColor: Color) {
340
+ this._assertNotDestroyed();
341
+ this.manager.mainView.setMemberState({ strokeColor });
342
+ }
343
+
344
+ /**
345
+ * Set text size. Default is 16.
346
+ */
347
+ setTextSize(textSize: number) {
348
+ this._assertNotDestroyed();
349
+ this.manager.mainView.setMemberState({ textSize });
350
+ }
351
+
352
+ /**
353
+ * Set text color.
354
+ *
355
+ * @example
356
+ * setTextColor([0x66, 0xcc, 0xff])
357
+ */
358
+ setTextColor(textColor: Color) {
359
+ this._assertNotDestroyed();
360
+ this.manager.mainView.setMemberState({ textColor });
361
+ }
362
+
363
+ /**
364
+ * Goto previous page (the main whiteboard view).
365
+ */
366
+ prevPage() {
367
+ this._assertNotDestroyed();
368
+ return this.manager.prevPage();
369
+ }
370
+
371
+ /**
372
+ * Goto next page (the main whiteboard view).
373
+ */
374
+ nextPage() {
375
+ this._assertNotDestroyed();
376
+ return this.manager.nextPage();
377
+ }
378
+
379
+ /**
380
+ * Add one page to the main whiteboard view.
381
+ *
382
+ * @example
383
+ * addPage({ after: true }) // add one page right after current one.
384
+ * nextPage() // then, goto that page.
385
+ */
386
+ addPage(params?: AddPageParams) {
387
+ this._assertNotDestroyed();
388
+ return this.manager.addPage(params);
389
+ }
390
+
391
+ /**
392
+ * Insert an image to the main view.
393
+ *
394
+ * @example
395
+ * insertImage("https://i.imgur.com/CzXTtJV.jpg")
396
+ */
397
+ async insertImage(url: string) {
398
+ this._assertNotDestroyed();
399
+ await this.manager.switchMainViewToWriter();
400
+
401
+ const { divElement } = this.manager.mainView;
402
+ const containerSize = {
403
+ width: divElement?.scrollWidth || window.innerWidth,
404
+ height: divElement?.scrollHeight || window.innerHeight,
405
+ };
406
+
407
+ // 1. shrink the image a little to fit container **width**
408
+ const maxWidth = containerSize.width * 0.8;
409
+ let { width, height } = await getImageSize(url, containerSize);
410
+ const scale = Math.min(maxWidth / width, 1);
411
+ const uuid = genUID();
412
+ const { centerX, centerY } = this.manager.camera;
413
+ width *= scale;
414
+ height *= scale;
415
+ this.manager.mainView.insertImage({ uuid, centerX, centerY, width, height, locked: false });
416
+ this.manager.mainView.completeImageUpload(uuid, url);
417
+
418
+ // 2. move camera to fit image **height**
419
+ width /= 0.8;
420
+ height /= 0.8;
421
+ const originX = centerX - width / 2;
422
+ const originY = centerY - height / 2;
423
+ this.manager.moveCameraToContain({ originX, originY, width, height });
424
+ }
425
+
426
+ /**
427
+ * Insert PDF/PPTX from conversion result.
428
+ * @param status https://developer.netless.link/server-en/home/server-conversion#get-query-task-conversion-progress
429
+ */
430
+ insertDocs(filename: string, status: ConversionResponse): Promise<string | undefined>;
431
+
432
+ /**
433
+ * Manual way.
434
+ * @example
435
+ * app.insertDocs({
436
+ * fileType: 'pptx',
437
+ * scenePath: `/pptx/${conversion.taskId}`,
438
+ * taskId: conversion.taskId,
439
+ * title: 'Title',
440
+ * })
441
+ */
442
+ insertDocs(params: InsertDocsParams): Promise<string | undefined>;
443
+
444
+ insertDocs(arg1: string | InsertDocsParams, arg2?: ConversionResponse) {
445
+ this._assertNotDestroyed();
446
+ if (typeof arg1 === "object" && "fileType" in arg1) {
447
+ return this._insertDocsImpl(arg1);
448
+ } else if (arg2 && arg2.status !== "Finished") {
449
+ throw new Error("FastboardApp cannot insert a converting doc.");
450
+ } else if (arg2 && arg2.progress) {
451
+ const title = arg1;
452
+ const scenePath = `/${arg2.uuid}/${genUID()}`;
453
+ const scenes1 = arg2.progress.convertedFileList.map(convertedFileToScene);
454
+ const { scenes, taskId, url } = makeSlideParams(scenes1);
455
+ if (taskId && url) {
456
+ return this._insertDocsImpl({ fileType: "pptx", scenePath, scenes, title, taskId, url });
457
+ } else {
458
+ return this._insertDocsImpl({ fileType: "pdf", scenePath, scenes: scenes1, title });
459
+ }
460
+ }
461
+ }
462
+
463
+ private _insertDocsImpl({ fileType, scenePath, title, scenes, ...attributes }: InsertDocsParams) {
464
+ this._assertNotDestroyed();
465
+ switch (fileType) {
466
+ case "pdf":
467
+ return this.manager.addApp({
468
+ kind: "DocsViewer",
469
+ options: { scenePath, title, scenes },
470
+ });
471
+ case "pptx":
472
+ if (scenes && scenes[0].ppt) {
473
+ warn("no-ppt-in-scenes");
474
+ }
475
+ return this.manager.addApp({
476
+ kind: "Slide",
477
+ options: { scenePath, title, scenes },
478
+ attributes,
479
+ });
480
+ }
481
+ }
482
+
483
+ /**
484
+ * Insert the Media Player app.
485
+ */
486
+ insertMedia(title: string, src: string) {
487
+ this._assertNotDestroyed();
488
+ return this.manager.addApp({
489
+ kind: BuiltinApps.MediaPlayer,
490
+ options: { title },
491
+ attributes: { src },
492
+ });
493
+ }
494
+
495
+ /**
496
+ * Insert the Monaco Code Editor app.
497
+ */
498
+ insertCodeEditor() {
499
+ this._assertNotDestroyed();
500
+ return this.manager.addApp({
501
+ kind: "Monaco",
502
+ options: { title: "Code Editor" },
503
+ });
504
+ }
505
+
506
+ /**
507
+ * Insert the Countdown app.
508
+ */
509
+ insertCountdown() {
510
+ this._assertNotDestroyed();
511
+ return this.manager.addApp({
512
+ kind: "Countdown",
513
+ options: { title: "Countdown" },
514
+ });
515
+ }
516
+
517
+ /**
518
+ * Insert the GeoGebra app.
519
+ */
520
+ insertGeoGebra() {
521
+ this._assertNotDestroyed();
522
+ return this.manager.addApp({
523
+ kind: "GeoGebra",
524
+ options: { title: "GeoGebra" },
525
+ });
526
+ }
527
+ }
528
+
529
+ export interface FastboardOptions {
530
+ sdkConfig: Omit<WhiteWebSdkConfiguration, "useMobXState"> & {
531
+ region: NonNullable<WhiteWebSdkConfiguration["region"]>;
532
+ };
533
+ joinRoom: Omit<JoinRoomParams, "useMultiViews" | "disableNewPencil" | "disableMagixEventDispatchLimit"> & {
534
+ callbacks?: Partial<RoomCallbacks>;
535
+ };
536
+ managerConfig?: Omit<MountParams, "room">;
537
+ }
538
+
539
+ /**
540
+ * Create a FastboardApp instance.
541
+ * @example
542
+ * let app = await createFastboard({
543
+ * sdkConfig: {
544
+ * appIdentifier: import.meta.env.VITE_APPID,
545
+ * region: 'cn-hz',
546
+ * },
547
+ * joinRoom: {
548
+ * uid: unique_id,
549
+ * uuid: import.meta.env.VITE_ROOM_UUID,
550
+ * roomToken: import.meta.env.VITE_ROOM_TOKEN,
551
+ * },
552
+ * })
553
+ */
554
+ export async function createFastboard({
555
+ sdkConfig,
556
+ joinRoom: { callbacks, ...joinRoomParams },
557
+ managerConfig,
558
+ }: FastboardOptions) {
559
+ const sdk = new WhiteWebSdk({
560
+ ...sdkConfig,
561
+ useMobXState: true,
562
+ });
563
+
564
+ const hotKeys: Partial<HotKeys> = {
565
+ ...DefaultHotKeys,
566
+ changeToSelector: "s",
567
+ changeToLaserPointer: "z",
568
+ changeToPencil: "p",
569
+ changeToRectangle: "r",
570
+ changeToEllipse: "c",
571
+ changeToEraser: "e",
572
+ changeToText: "t",
573
+ changeToStraight: "l",
574
+ changeToArrow: "a",
575
+ changeToHand: "h",
576
+ };
577
+
578
+ const room = await sdk.joinRoom(
579
+ {
580
+ floatBar: true,
581
+ hotKeys,
582
+ ...ensure_window_manager(joinRoomParams),
583
+ useMultiViews: true,
584
+ disableNewPencil: false,
585
+ disableMagixEventDispatchLimit: true,
586
+ },
587
+ callbacks
588
+ );
589
+
590
+ const manager = await WindowManager.mount({
591
+ cursor: true,
592
+ ...managerConfig,
593
+ room,
594
+ });
595
+
596
+ manager.mainView.setCameraBound({
597
+ minContentMode: contentModeScale(0.3),
598
+ maxContentMode: contentModeScale(3),
599
+ });
600
+
601
+ return new FastboardApp(sdk, room, manager, hotKeys);
602
+ }