@netless/fastboard-core 0.2.0 → 0.2.4

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,4 +1,4 @@
1
- import type { ConvertedFile, JoinRoomParams, SceneDefinition, Size } from "white-web-sdk";
1
+ import type { ConvertedFile, JoinRoomParams, ReplayRoomParams, SceneDefinition, Size } from "white-web-sdk";
2
2
  import { WindowManager } from "@netless/window-manager";
3
3
 
4
4
  export function noop() {
@@ -53,7 +53,7 @@ export function convertedFileToScene(f: ConvertedFile, i: number) {
53
53
  };
54
54
  }
55
55
 
56
- export function ensureWindowManager(joinRoom: JoinRoomParams) {
56
+ export function ensureWindowManager<T extends JoinRoomParams | ReplayRoomParams>(joinRoom: T): T {
57
57
  if (!joinRoom.invisiblePlugins || !joinRoom.invisiblePlugins.includes(WindowManager)) {
58
58
  joinRoom.invisiblePlugins = [...(joinRoom.invisiblePlugins || []), WindowManager];
59
59
  }
File without changes
@@ -1,19 +1,76 @@
1
+ import type { PublicEvent, WindowManager } from "@netless/window-manager";
1
2
  import type {
2
3
  AnimationMode,
3
4
  ApplianceNames,
4
5
  Camera,
5
6
  Color,
6
7
  ConversionResponse,
8
+ HotKeys,
7
9
  MemberState,
8
10
  Rectangle,
11
+ Room,
12
+ RoomCallbacks,
9
13
  RoomState,
10
14
  SceneDefinition,
11
15
  ShapeType,
16
+ ViewCallbacks,
17
+ WhiteWebSdk,
12
18
  } from "white-web-sdk";
19
+ import type { FastboardDisposer, FastboardInternalValue } from "../helpers/value";
13
20
 
14
21
  import { BuiltinApps } from "@netless/window-manager";
15
- import { FastboardAppBase } from "./base";
16
- import { convertedFileToScene, genUID, getImageSize, makeSlideParams } from "./utils";
22
+ import { convertedFileToScene, genUID, getImageSize, makeSlideParams } from "../helpers/utils";
23
+ import { createValue } from "../helpers/value";
24
+
25
+ class FastboardAppBase {
26
+ public constructor(
27
+ readonly sdk: WhiteWebSdk,
28
+ readonly room: Room,
29
+ readonly manager: WindowManager,
30
+ readonly hotKeys: Partial<HotKeys>
31
+ ) {}
32
+
33
+ protected readonly _disposers: FastboardDisposer[] = [];
34
+ protected _destroyed = false;
35
+ protected _assertNotDestroyed() {
36
+ if (this._destroyed) {
37
+ throw new Error("[FastboardApp] Can not call any method on destroyed FastboardApp.");
38
+ }
39
+ }
40
+
41
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
+ protected createValue: typeof createValue = (...args: [any, any]): any => {
43
+ const value = createValue(...args);
44
+ this._disposers.push((value as FastboardInternalValue<unknown>).dispose);
45
+ return value;
46
+ };
47
+
48
+ protected _addRoomListener<K extends keyof RoomCallbacks, T = RoomCallbacks[K]>(name: K, listener: T) {
49
+ this._assertNotDestroyed();
50
+ this.room.callbacks.on(name, listener);
51
+ return () => this.room.callbacks.off(name, listener);
52
+ }
53
+
54
+ protected _addManagerListener<K extends keyof PublicEvent>(name: K, set: (value: PublicEvent[K]) => void) {
55
+ this._assertNotDestroyed();
56
+ this.manager.emitter.on(name, set);
57
+ return () => this.manager.emitter.off(name, set);
58
+ }
59
+
60
+ protected _addMainViewListener<K extends keyof ViewCallbacks, T = ViewCallbacks[K]>(name: K, listener: T) {
61
+ this._assertNotDestroyed();
62
+ this.manager.mainView.callbacks.on(name, listener);
63
+ return () => this.manager.mainView.callbacks.off(name, listener);
64
+ }
65
+
66
+ public destroy() {
67
+ this._disposers.forEach(dispose => dispose());
68
+ this._disposers.length = 0;
69
+ this._destroyed = true;
70
+ this.manager.destroy();
71
+ return this.room.disconnect();
72
+ }
73
+ }
17
74
 
18
75
  export interface InsertDocsStatic {
19
76
  readonly fileType: "pdf" | "ppt";
@@ -82,33 +139,29 @@ export class FastboardApp extends FastboardAppBase {
82
139
  /**
83
140
  * How many times can I call `app.redo()`?
84
141
  */
85
- readonly canRedoSteps = this.createValue(this.manager.mainView.canRedoSteps, set =>
86
- this._addMainViewListener("onCanRedoStepsUpdate", set)
142
+ readonly canRedoSteps = this.createValue(this.manager.canRedoSteps, set =>
143
+ this._addManagerListener("canRedoStepsChange", set)
87
144
  );
88
145
 
89
146
  /**
90
147
  * How many times can I call `app.undo()`?
91
148
  */
92
- readonly canUndoSteps = this.createValue(this.manager.mainView.canUndoSteps, set =>
93
- this._addMainViewListener("onCanUndoStepsUpdate", set)
149
+ readonly canUndoSteps = this.createValue(this.manager.canUndoSteps, set =>
150
+ this._addManagerListener("canUndoStepsChange", set)
94
151
  );
95
152
 
96
153
  /**
97
154
  * Current camera information of main view.
98
155
  */
99
- readonly camera = this.createValue(
100
- this.manager.mainView.camera,
101
- set => this._addMainViewListener("onCameraUpdated", set),
102
- this.manager.moveCamera.bind(this.manager)
156
+ readonly camera = this.createValue(this.manager.mainView.camera, set =>
157
+ this._addMainViewListener("onCameraUpdated", set)
103
158
  );
104
159
 
105
160
  /**
106
161
  * Current tool's info, like "is using pencil?", "what color?".
107
162
  */
108
- readonly memberState = this.createValue<MemberState, SetMemberStateFn>(
109
- this.room.state.memberState,
110
- set => this._addRoomListener<RoomStateChanged>("onRoomStateChanged", ({ memberState: m }) => m && set(m)),
111
- this.manager.mainView.setMemberState.bind(this.manager.mainView)
163
+ readonly memberState = this.createValue(this.room.state.memberState, set =>
164
+ this._addRoomListener("onRoomStateChanged", ({ memberState: m }) => m && set(m))
112
165
  );
113
166
 
114
167
  /**
@@ -0,0 +1,122 @@
1
+ import type { Player, PlayerCallbacks, PlayerState, ViewCallbacks, WhiteWebSdk } from "white-web-sdk";
2
+ import type { PublicEvent, WindowManager } from "@netless/window-manager";
3
+ import type { FastboardDisposer, FastboardInternalValue } from "../helpers/value";
4
+
5
+ import { createValue } from "../helpers/value";
6
+
7
+ class FastboardPlayerBase {
8
+ public constructor(readonly sdk: WhiteWebSdk, readonly player: Player, readonly manager: WindowManager) {}
9
+
10
+ protected readonly _disposers: FastboardDisposer[] = [];
11
+ protected _destroyed = false;
12
+ protected _assertNotDestroyed() {
13
+ if (this._destroyed) {
14
+ throw new Error("[FastboardPlayer] Can not call any method on destroyed FastboardPlayer.");
15
+ }
16
+ }
17
+
18
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
+ protected createValue: typeof createValue = (...args: [any, any]): any => {
20
+ const value = createValue(...args);
21
+ this._disposers.push((value as FastboardInternalValue<unknown>).dispose);
22
+ return value;
23
+ };
24
+
25
+ protected _addPlayerListener<K extends keyof PlayerCallbacks, T = PlayerCallbacks[K]>(
26
+ name: K,
27
+ listener: T
28
+ ) {
29
+ this._assertNotDestroyed();
30
+ this.player.callbacks.on(name, listener);
31
+ return () => this.player.callbacks.off(name, listener);
32
+ }
33
+
34
+ protected _addManagerListener<K extends keyof PublicEvent>(name: K, set: (value: PublicEvent[K]) => void) {
35
+ this._assertNotDestroyed();
36
+ this.manager.emitter.on(name, set);
37
+ return () => this.manager.emitter.off(name, set);
38
+ }
39
+
40
+ protected _addMainViewListener<K extends keyof ViewCallbacks, T = ViewCallbacks[K]>(name: K, listener: T) {
41
+ this._assertNotDestroyed();
42
+ this.manager.mainView.callbacks.on(name, listener);
43
+ return () => this.manager.mainView.callbacks.off(name, listener);
44
+ }
45
+
46
+ public destroy() {
47
+ this._disposers.forEach(dispose => dispose());
48
+ this._disposers.length = 0;
49
+ this._destroyed = true;
50
+ this.manager.destroy();
51
+ this.player.callbacks.off();
52
+ }
53
+ }
54
+
55
+ export class FastboardPlayer extends FastboardPlayerBase {
56
+ /**
57
+ * Render this player to some DOM.
58
+ */
59
+ bindContainer(container: HTMLElement) {
60
+ this._assertNotDestroyed();
61
+ this.manager.bindContainer(container);
62
+ }
63
+
64
+ /**
65
+ * Move window-manager's collector to some place.
66
+ */
67
+ bindCollector(container: HTMLElement) {
68
+ this._assertNotDestroyed();
69
+ this.manager.bindCollectorContainer(container);
70
+ }
71
+
72
+ readonly currentTime = this.createValue(
73
+ this.player.progressTime,
74
+ set => this._addPlayerListener("onProgressTimeChanged", set),
75
+ this.player.seekToProgressTime.bind(this.player)
76
+ );
77
+
78
+ readonly phase = this.createValue(this.player.phase, set => this._addPlayerListener("onPhaseChanged", set));
79
+
80
+ readonly canplay = this.createValue(this.player.isPlayable, set =>
81
+ this._addPlayerListener("onIsPlayableChanged", set)
82
+ );
83
+
84
+ private _setSpeed!: (value: number) => void;
85
+ readonly speed = this.createValue(
86
+ this.player.playbackSpeed,
87
+ set => {
88
+ this._setSpeed = set;
89
+ },
90
+ value => {
91
+ this.player.playbackSpeed = value;
92
+ this._setSpeed(value);
93
+ }
94
+ );
95
+
96
+ readonly state = this.createValue<PlayerState | null>(null, set => {
97
+ const update = () => set(this.player.state);
98
+ const dispose1 = this._addPlayerListener("onLoadFirstFrame", update);
99
+ const dispose2 = this._addPlayerListener("onPlayerStateChanged", update);
100
+ return () => (dispose1(), dispose2());
101
+ });
102
+
103
+ seek(timestamp: number) {
104
+ this._assertNotDestroyed();
105
+ return this.player.seekToProgressTime(timestamp);
106
+ }
107
+
108
+ play() {
109
+ this._assertNotDestroyed();
110
+ this.player.play();
111
+ }
112
+
113
+ pause() {
114
+ this._assertNotDestroyed();
115
+ this.player.pause();
116
+ }
117
+
118
+ stop() {
119
+ this._assertNotDestroyed();
120
+ this.player.stop();
121
+ }
122
+ }
package/src/index.ts CHANGED
@@ -1,80 +1,3 @@
1
- import type { HotKeys, JoinRoomParams, RoomCallbacks, WhiteWebSdkConfiguration } from "white-web-sdk";
2
- import type { MountParams } from "@netless/window-manager";
1
+ import "./behaviors/register-slide";
3
2
 
4
- import { DefaultHotKeys, WhiteWebSdk } from "white-web-sdk";
5
- import { WindowManager } from "@netless/window-manager";
6
-
7
- import "./behaviors/register-apps";
8
- import { FastboardApp } from "./core";
9
- import { ensureWindowManager } from "./utils";
10
-
11
- export type { FastboardReadable, FastboardWritable } from "./value";
12
-
13
- export type { FastboardApp };
14
-
15
- export interface FastboardOptions {
16
- sdkConfig: Omit<WhiteWebSdkConfiguration, "useMobXState">;
17
- joinRoom: Omit<JoinRoomParams, "useMultiViews" | "disableNewPencil" | "disableMagixEventDispatchLimit"> & {
18
- callbacks?: Partial<RoomCallbacks>;
19
- };
20
- managerConfig?: Omit<MountParams, "room">;
21
- }
22
-
23
- /**
24
- * Create a FastboardApp instance.
25
- * @example
26
- * let app = await createFastboard({
27
- * sdkConfig: {
28
- * appIdentifier: import.meta.env.VITE_APPID,
29
- * },
30
- * joinRoom: {
31
- * uid: unique_id,
32
- * uuid: import.meta.env.VITE_ROOM_UUID,
33
- * roomToken: import.meta.env.VITE_ROOM_TOKEN,
34
- * },
35
- * })
36
- */
37
- export async function createFastboard({
38
- sdkConfig,
39
- joinRoom: { callbacks, ...joinRoomParams },
40
- managerConfig,
41
- }: FastboardOptions) {
42
- const sdk = new WhiteWebSdk({
43
- ...sdkConfig,
44
- useMobXState: true,
45
- });
46
-
47
- const hotKeys: Partial<HotKeys> = {
48
- ...DefaultHotKeys,
49
- changeToSelector: "s",
50
- changeToLaserPointer: "z",
51
- changeToPencil: "p",
52
- changeToRectangle: "r",
53
- changeToEllipse: "c",
54
- changeToEraser: "e",
55
- changeToText: "t",
56
- changeToStraight: "l",
57
- changeToArrow: "a",
58
- changeToHand: "h",
59
- };
60
-
61
- const room = await sdk.joinRoom(
62
- {
63
- floatBar: true,
64
- hotKeys,
65
- ...ensureWindowManager(joinRoomParams),
66
- useMultiViews: true,
67
- disableNewPencil: false,
68
- disableMagixEventDispatchLimit: true,
69
- },
70
- callbacks
71
- );
72
-
73
- const manager = await WindowManager.mount({
74
- cursor: true,
75
- ...managerConfig,
76
- room,
77
- });
78
-
79
- return new FastboardApp(sdk, room, manager, hotKeys);
80
- }
3
+ export * from "./minimal";
package/src/minimal.ts ADDED
@@ -0,0 +1,139 @@
1
+ import type {
2
+ Displayer,
3
+ HotKeys,
4
+ JoinRoomParams,
5
+ PlayerCallbacks,
6
+ ReplayRoomParams,
7
+ Room,
8
+ RoomCallbacks,
9
+ WhiteWebSdkConfiguration,
10
+ } from "white-web-sdk";
11
+ import type { MountParams } from "@netless/window-manager";
12
+
13
+ import { DefaultHotKeys, WhiteWebSdk } from "white-web-sdk";
14
+ import { WindowManager } from "@netless/window-manager";
15
+
16
+ import "./behaviors/register-apps";
17
+ import { ensureWindowManager } from "./helpers/utils";
18
+ import { FastboardApp } from "./impl/app";
19
+ import { FastboardPlayer } from "./impl/player";
20
+
21
+ export type { FastboardReadable, FastboardWritable } from "./helpers/value";
22
+
23
+ export type { FastboardApp };
24
+
25
+ export interface FastboardOptions {
26
+ sdkConfig: Omit<WhiteWebSdkConfiguration, "useMobXState">;
27
+ joinRoom: Omit<JoinRoomParams, "useMultiViews" | "disableNewPencil" | "disableMagixEventDispatchLimit"> & {
28
+ callbacks?: Partial<RoomCallbacks>;
29
+ };
30
+ managerConfig?: Omit<MountParams, "room">;
31
+ }
32
+
33
+ /**
34
+ * Create a FastboardApp instance.
35
+ * @example
36
+ * let app = await createFastboard({
37
+ * sdkConfig: {
38
+ * appIdentifier: import.meta.env.VITE_APPID,
39
+ * },
40
+ * joinRoom: {
41
+ * uid: unique_id,
42
+ * uuid: import.meta.env.VITE_ROOM_UUID,
43
+ * roomToken: import.meta.env.VITE_ROOM_TOKEN,
44
+ * },
45
+ * })
46
+ */
47
+ export async function createFastboard({
48
+ sdkConfig,
49
+ joinRoom: { callbacks, ...joinRoomParams },
50
+ managerConfig,
51
+ }: FastboardOptions) {
52
+ const sdk = new WhiteWebSdk({
53
+ ...sdkConfig,
54
+ useMobXState: true,
55
+ });
56
+
57
+ const hotKeys: Partial<HotKeys> = {
58
+ ...DefaultHotKeys,
59
+ changeToSelector: "s",
60
+ changeToLaserPointer: "z",
61
+ changeToPencil: "p",
62
+ changeToRectangle: "r",
63
+ changeToEllipse: "c",
64
+ changeToEraser: "e",
65
+ changeToText: "t",
66
+ changeToStraight: "l",
67
+ changeToArrow: "a",
68
+ changeToHand: "h",
69
+ };
70
+
71
+ const room = await sdk.joinRoom(
72
+ {
73
+ floatBar: true,
74
+ hotKeys,
75
+ ...ensureWindowManager(joinRoomParams),
76
+ useMultiViews: true,
77
+ disableNewPencil: false,
78
+ disableMagixEventDispatchLimit: true,
79
+ },
80
+ callbacks
81
+ );
82
+
83
+ const manager = await WindowManager.mount({
84
+ cursor: true,
85
+ ...managerConfig,
86
+ room,
87
+ });
88
+
89
+ return new FastboardApp(sdk, room, manager, hotKeys);
90
+ }
91
+
92
+ export interface FastboardReplayOptions {
93
+ sdkConfig: Omit<WhiteWebSdkConfiguration, "useMobXState">;
94
+ replayRoom: Omit<ReplayRoomParams, "useMultiViews"> & {
95
+ callbacks?: Partial<PlayerCallbacks>;
96
+ };
97
+ managerConfig?: Omit<MountParams, "room">;
98
+ }
99
+
100
+ /**
101
+ * Create a FastboardPlayer instance.
102
+ * @example
103
+ * let app = await replayFastboard({
104
+ * sdkConfig: {
105
+ * appIdentifier: import.meta.env.VITE_APPID,
106
+ * },
107
+ * replayRoom: {
108
+ * uid: unique_id,
109
+ * uuid: import.meta.env.VITE_ROOM_UUID,
110
+ * roomToken: import.meta.env.VITE_ROOM_TOKEN,
111
+ * },
112
+ * })
113
+ */
114
+ export async function replayFastboard({
115
+ sdkConfig,
116
+ replayRoom: { callbacks, ...replayRoomParams },
117
+ managerConfig,
118
+ }: FastboardReplayOptions) {
119
+ const sdk = new WhiteWebSdk({
120
+ ...sdkConfig,
121
+ useMobXState: true,
122
+ });
123
+
124
+ const player = await sdk.replayRoom(
125
+ {
126
+ ...ensureWindowManager(replayRoomParams),
127
+ useMultiViews: true,
128
+ },
129
+ callbacks
130
+ );
131
+
132
+ const manager = await WindowManager.mount({
133
+ cursor: true,
134
+ ...managerConfig,
135
+ room: player as Displayer as Room,
136
+ });
137
+
138
+ return new FastboardPlayer(sdk, player, manager);
139
+ }
package/src/base.ts DELETED
@@ -1,55 +0,0 @@
1
- import type { PublicEvent, Room, WindowManager } from "@netless/window-manager";
2
- import type { HotKeys, WhiteWebSdk } from "white-web-sdk";
3
- import type { FastboardDisposer, FastboardInternalValue } from "./value";
4
-
5
- import { createValue } from "./value";
6
-
7
- export class FastboardAppBase {
8
- public constructor(
9
- readonly sdk: WhiteWebSdk,
10
- readonly room: Room,
11
- readonly manager: WindowManager,
12
- readonly hotKeys: Partial<HotKeys>
13
- ) {}
14
-
15
- protected readonly _disposers: FastboardDisposer[] = [];
16
- protected _destroyed = false;
17
- protected _assertNotDestroyed() {
18
- if (this._destroyed) {
19
- throw new Error("[FastboardApp] Can not call any method on destroyed FastboardApp.");
20
- }
21
- }
22
-
23
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
- protected createValue: typeof createValue = (...args: [any, any]): any => {
25
- const value = createValue(...args);
26
- this._disposers.push((value as FastboardInternalValue<unknown>).dispose);
27
- return value;
28
- };
29
-
30
- protected _addRoomListener<T = unknown>(name: string, listener: T) {
31
- this._assertNotDestroyed();
32
- this.room.callbacks.on(name, listener);
33
- return () => this.room.callbacks.off(name, listener);
34
- }
35
-
36
- protected _addManagerListener<K extends keyof PublicEvent>(name: K, set: (value: PublicEvent[K]) => void) {
37
- this._assertNotDestroyed();
38
- this.manager.emitter.on(name, set);
39
- return () => this.manager.emitter.off(name, set);
40
- }
41
-
42
- protected _addMainViewListener<T = unknown>(name: string, listener: T) {
43
- this._assertNotDestroyed();
44
- this.manager.mainView.callbacks.on(name, listener);
45
- return () => this.manager.mainView.callbacks.off(name, listener);
46
- }
47
-
48
- public destroy() {
49
- this._disposers.forEach(dispose => dispose());
50
- this._disposers.length = 0;
51
- this._destroyed = true;
52
- this.manager.destroy();
53
- return this.room.disconnect();
54
- }
55
- }