@netless/window-manager 0.4.31 → 1.0.0-canary.1

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,5 +1,4 @@
1
1
  import App from "./Cursor.svelte";
2
- import { ApplianceMap } from "./icons";
3
2
  import { ApplianceNames } from "white-web-sdk";
4
3
  import { findMemberByUid } from "../Helper";
5
4
  import { omit } from "lodash";
@@ -159,8 +158,9 @@ export class Cursor {
159
158
 
160
159
  private getIcon() {
161
160
  if (this.member) {
162
- const applianceSrc = ApplianceMap[this.memberApplianceName || ApplianceNames.shape];
163
- return applianceSrc || ApplianceMap[ApplianceNames.shape];
161
+ const icons = this.cursorManager.applianceIcons;
162
+ const applianceSrc = icons[this.memberApplianceName || ApplianceNames.shape];
163
+ return applianceSrc || icons[ApplianceNames.shape];
164
164
  }
165
165
  }
166
166
 
@@ -5,10 +5,11 @@ import { emitter } from "../InternalEmitter";
5
5
  import { SideEffectManager } from "side-effect-manager";
6
6
  import { throttle } from "lodash";
7
7
  import { WindowManager } from "../index";
8
- import type { CursorMovePayload } from "../index";
8
+ import type { CursorMovePayload , ApplianceIcons} from "../index";
9
9
  import type { PositionType } from "../AttributesDelegate";
10
10
  import type { Point, RoomMember, View } from "white-web-sdk";
11
11
  import type { AppManager } from "../AppManager";
12
+ import { ApplianceMap } from "./icons";
12
13
 
13
14
  export type EventType = {
14
15
  type: PositionType;
@@ -20,6 +21,7 @@ export type MoveCursorParams = {
20
21
  x: number;
21
22
  y: number;
22
23
  };
24
+
23
25
  export class CursorManager {
24
26
  public containerRect?: DOMRect;
25
27
  public wrapperRect?: DOMRect;
@@ -28,12 +30,13 @@ export class CursorManager {
28
30
  private mainViewElement?: HTMLDivElement;
29
31
  private sideEffectManager = new SideEffectManager();
30
32
  private store = this.manager.store;
33
+ public applianceIcons: ApplianceIcons = ApplianceMap;
31
34
 
32
- constructor(private manager: AppManager, private enableCursor: boolean) {
35
+ constructor(private manager: AppManager, private enableCursor: boolean, applianceIcons?: ApplianceIcons) {
33
36
  this.roomMembers = this.manager.room?.state.roomMembers;
34
- const wrapper = WindowManager.wrapper;
35
- if (wrapper) {
36
- this.setupWrapper(wrapper);
37
+ const playground = WindowManager.playground;
38
+ if (playground) {
39
+ this.setupWrapper(playground);
37
40
  }
38
41
  this.sideEffectManager.add(() => {
39
42
  return emitter.on("cursorMove", this.onCursorMove);
@@ -42,6 +45,9 @@ export class CursorManager {
42
45
  this.sideEffectManager.add(() => {
43
46
  return emitter.on("playgroundSizeChange", () => this.updateContainerRect());
44
47
  });
48
+ if (applianceIcons) {
49
+ this.applianceIcons = { ...ApplianceMap, ...applianceIcons };
50
+ }
45
51
  }
46
52
 
47
53
  private onCursorMove = (payload: CursorMovePayload) => {
@@ -59,7 +65,7 @@ export class CursorManager {
59
65
  private initCursorInstance = (uid: string) => {
60
66
  let cursorInstance = this.cursorInstances.get(uid);
61
67
  if (!cursorInstance) {
62
- cursorInstance = new Cursor(this.manager, uid, this, WindowManager.wrapper);
68
+ cursorInstance = new Cursor(this.manager, uid, this, WindowManager.playground);
63
69
  this.cursorInstances.set(uid, cursorInstance);
64
70
  }
65
71
  return cursorInstance;
@@ -163,7 +169,7 @@ export class CursorManager {
163
169
 
164
170
  public updateContainerRect() {
165
171
  this.containerRect = WindowManager.container?.getBoundingClientRect();
166
- this.wrapperRect = WindowManager.wrapper?.getBoundingClientRect();
172
+ this.wrapperRect = WindowManager.playground?.getBoundingClientRect();
167
173
  }
168
174
 
169
175
  public deleteCursor(uid: string) {
package/src/Helper.ts CHANGED
@@ -2,36 +2,23 @@ import { getVersionNumber } from "./Utils/Common";
2
2
  import { REQUIRE_VERSION } from "./constants";
3
3
  import { WhiteVersion } from "white-web-sdk";
4
4
  import { WhiteWebSDKInvalidError } from "./Utils/error";
5
- import { WindowManager } from "./index";
6
5
  import type { Room } from "white-web-sdk";
7
6
 
8
7
  export const setupWrapper = (
9
8
  root: HTMLElement
10
9
  ): {
11
10
  playground: HTMLDivElement;
12
- wrapper: HTMLDivElement;
13
- sizer: HTMLDivElement;
14
11
  mainViewElement: HTMLDivElement;
15
12
  } => {
16
13
  const playground = document.createElement("div");
17
14
  playground.className = "netless-window-manager-playground";
18
15
 
19
- const sizer = document.createElement("div");
20
- sizer.className = "netless-window-manager-sizer";
21
-
22
- const wrapper = document.createElement("div");
23
- wrapper.className = "netless-window-manager-wrapper";
24
-
25
16
  const mainViewElement = document.createElement("div");
26
17
  mainViewElement.className = "netless-window-manager-main-view";
27
-
28
- playground.appendChild(sizer);
29
- sizer.appendChild(wrapper);
30
- wrapper.appendChild(mainViewElement);
18
+ playground.appendChild(mainViewElement);
31
19
  root.appendChild(playground);
32
- WindowManager.wrapper = wrapper;
33
20
 
34
- return { playground, wrapper, sizer, mainViewElement };
21
+ return { playground, mainViewElement };
35
22
  };
36
23
 
37
24
  export const checkVersion = () => {
@@ -1,9 +1,11 @@
1
1
  import Emittery from "emittery";
2
+ import type { TeleBoxRect } from "@netless/telebox-insider";
2
3
  import type { AppInitState, CursorMovePayload } from "./index";
3
4
 
4
5
  export type RemoveSceneParams = {
5
- scenePath: string, index?: number
6
- }
6
+ scenePath: string;
7
+ index?: number;
8
+ };
7
9
 
8
10
  export type EmitterEvent = {
9
11
  onCreated: undefined;
@@ -14,14 +16,14 @@ export type EmitterEvent = {
14
16
  mainViewMounted: undefined;
15
17
  observerIdChange: number;
16
18
  boxStateChange: string;
17
- playgroundSizeChange: DOMRect;
19
+ playgroundSizeChange: TeleBoxRect;
18
20
  startReconnect: undefined;
19
21
  onReconnected: undefined;
20
22
  removeScenes: RemoveSceneParams;
21
23
  cursorMove: CursorMovePayload;
22
24
  updateManagerRect: undefined;
23
25
  focusedChange: { focused: string | undefined; prev: string | undefined };
24
- rootDirRemoved: undefined; // 根目录整个被删除
26
+ rootDirRemoved: undefined; // 根目录整个被删除
25
27
  rootDirSceneRemoved: string; // 根目录下的场景被删除
26
28
  setReadonly: boolean;
27
29
  changePageState: undefined;
@@ -0,0 +1,67 @@
1
+ import { AnimationMode } from "white-web-sdk";
2
+ import { delay, throttle } from "lodash";
3
+ import type { TeleBoxRect } from "@netless/telebox-insider";
4
+ import type { Camera, View, Size } from "white-web-sdk";
5
+ import type { MainViewSize } from "../AttributesDelegate";
6
+
7
+ export type SaveCamera = (camera: Camera) => void;
8
+
9
+ export class CameraSynchronizer {
10
+ protected remoteCamera?: Camera;
11
+ protected remoteSize?: MainViewSize;
12
+ protected rect?: TeleBoxRect;
13
+ protected view?: View;
14
+
15
+ constructor(protected saveCamera: SaveCamera) {}
16
+
17
+ public setRect(rect: TeleBoxRect) {
18
+ this.rect = rect;
19
+ if (this.remoteCamera && this.remoteSize) {
20
+ this.onRemoteUpdate(this.remoteCamera, this.remoteSize);
21
+ }
22
+ }
23
+
24
+ public setView(view: View) {
25
+ this.view = view;
26
+ }
27
+
28
+ // 远端 Camera 或者 size 更新
29
+ public onRemoteUpdate = throttle((camera: Camera, size: MainViewSize) => {
30
+ this.remoteCamera = camera;
31
+ this.remoteSize = size;
32
+ if (this.remoteSize && this.rect) {
33
+ let scale: number;
34
+ if (size.width < size.height) {
35
+ scale = this.rect.width / size.width;
36
+ } else {
37
+ scale = this.rect.height / size.height;
38
+ }
39
+ const nextScale = camera.scale * scale;
40
+ const moveCamera = () => this.view?.moveCamera({
41
+ centerX: camera.centerX,
42
+ centerY: camera.centerY,
43
+ scale: nextScale,
44
+ animationMode: AnimationMode.Immediately,
45
+ });
46
+ // TODO 直接调用 moveCamera 依然会出现 camera 错误的情况,这里暂时加一个 delay 保证 camera 是对的, 后续需要 SDK 进行修改
47
+ delay(moveCamera, 10);
48
+ }
49
+ }, 50);
50
+
51
+
52
+ public onLocalCameraUpdate(camera: Camera) {
53
+ this.saveCamera(camera);
54
+ }
55
+
56
+ // 本地 Size 更新, 先匹配 camera 到新的 size 然后再发送 camera 数据到远端
57
+ public onLocalSizeUpdate(size: Size) {
58
+ if (this.rect && this.view) {
59
+ const scale = this.rect.width / size.width;
60
+ const nextScale = this.view.camera.scale * scale;
61
+ this.view.moveCamera({
62
+ scale: nextScale,
63
+ animationMode: AnimationMode.Immediately
64
+ });
65
+ }
66
+ }
67
+ }
@@ -1,25 +1,29 @@
1
- import { AnimationMode, reaction } from "white-web-sdk";
1
+ import { reaction } from "white-web-sdk";
2
2
  import { callbacks } from "../callback";
3
3
  import { createView } from "./ViewManager";
4
- import { debounce, get, isEmpty, isEqual } from "lodash";
4
+ import { debounce, get, isEqual } from "lodash";
5
5
  import { emitter } from "../InternalEmitter";
6
+ import { Events } from "../constants";
6
7
  import { Fields } from "../AttributesDelegate";
7
8
  import { setViewFocusScenePath } from "../Utils/Common";
8
9
  import { SideEffectManager } from "side-effect-manager";
9
10
  import type { Camera, Size, View } from "white-web-sdk";
10
11
  import type { AppManager } from "../AppManager";
11
- import { Events } from "../constants";
12
+ import { CameraSynchronizer } from "./CameraSynchronizer";
12
13
 
13
14
  export class MainViewProxy {
14
- private scale?: number;
15
15
  private started = false;
16
16
  private mainViewIsAddListener = false;
17
17
  private mainView: View;
18
18
  private store = this.manager.store;
19
+ private synchronizer: CameraSynchronizer;
19
20
 
20
21
  private sideEffectManager = new SideEffectManager();
21
22
 
22
23
  constructor(private manager: AppManager) {
24
+ this.synchronizer = new CameraSynchronizer(
25
+ camera => this.store.setMainViewCamera({ ...camera, id: this.manager.uid })
26
+ );
23
27
  this.mainView = this.createMainView();
24
28
  this.moveCameraSizeByAttributes();
25
29
  emitter.once("mainViewMounted").then(() => {
@@ -28,18 +32,24 @@ export class MainViewProxy {
28
32
  this.ensureCameraAndSize();
29
33
  this.startListenWritableChange();
30
34
  });
31
- const playgroundSizeChangeListener = () => {
32
- this.sizeChangeHandler(this.mainViewSize);
33
- };
34
- this.sideEffectManager.add(() => {
35
- return emitter.on("playgroundSizeChange", playgroundSizeChangeListener);
36
- });
37
35
  this.sideEffectManager.add(() => {
38
36
  return emitter.on("containerSizeRatioUpdate", this.onUpdateContainerSizeRatio);
39
37
  });
40
38
  this.sideEffectManager.add(() => {
41
39
  return emitter.on("startReconnect", () => {
42
- this.mainView.release();
40
+ if (!(this.mainView as any).didRelease) {
41
+ this.mainView.release();
42
+ }
43
+ });
44
+ });
45
+ const rect = this.manager.boxManager?.stageRect;
46
+ if (rect) {
47
+ this.synchronizer.setRect(rect);
48
+ }
49
+ this.sideEffectManager.add(() => {
50
+ return emitter.on("playgroundSizeChange", rect => {
51
+ this.synchronizer.setRect(rect);
52
+ this.synchronizer.onLocalSizeUpdate(rect);
43
53
  });
44
54
  });
45
55
  }
@@ -52,7 +62,7 @@ export class MainViewProxy {
52
62
  }
53
63
  });
54
64
  });
55
- }
65
+ };
56
66
 
57
67
  public ensureCameraAndSize() {
58
68
  if (!this.mainViewCamera || !this.mainViewSize) {
@@ -74,8 +84,7 @@ export class MainViewProxy {
74
84
  }
75
85
 
76
86
  private moveCameraSizeByAttributes() {
77
- this.moveCameraToContian(this.mainViewSize);
78
- this.moveCamera(this.mainViewCamera);
87
+ this.synchronizer.onRemoteUpdate(this.mainViewCamera, this.mainViewSize);
79
88
  }
80
89
 
81
90
  public start() {
@@ -91,9 +100,12 @@ export class MainViewProxy {
91
100
  };
92
101
 
93
102
  public setCameraAndSize(): void {
94
- const camera = { ...this.mainView.camera, id: this.manager.uid };
95
- const size = { ...this.mainView.size, id: this.manager.uid };
96
- this.store.setMainViewCameraAndSize(camera, size);
103
+ const stageSize = this.getStageSize();
104
+ if (stageSize) {
105
+ const camera = { ...this.mainView.camera, id: this.manager.uid };
106
+ const size = { ...stageSize, id: this.manager.uid };
107
+ this.store.setMainViewCameraAndSize(camera, size);
108
+ }
97
109
  }
98
110
 
99
111
  private cameraReaction = () => {
@@ -101,8 +113,7 @@ export class MainViewProxy {
101
113
  () => this.mainViewCamera,
102
114
  camera => {
103
115
  if (camera && camera.id !== this.manager.uid) {
104
- this.moveCameraToContian(this.mainViewSize);
105
- this.moveCamera(camera);
116
+ this.synchronizer.onRemoteUpdate(camera, this.mainViewSize);
106
117
  }
107
118
  },
108
119
  { fireImmediately: true }
@@ -111,8 +122,7 @@ export class MainViewProxy {
111
122
 
112
123
  public sizeChangeHandler = debounce((size: Size) => {
113
124
  if (size) {
114
- this.moveCameraToContian(size);
115
- this.moveCamera(this.mainViewCamera);
125
+ this.synchronizer.onLocalSizeUpdate(size);
116
126
  }
117
127
  }, 30);
118
128
 
@@ -122,7 +132,7 @@ export class MainViewProxy {
122
132
  if (size.id === this.manager.uid) {
123
133
  this.setCameraAndSize();
124
134
  }
125
- }
135
+ };
126
136
 
127
137
  public get view(): View {
128
138
  return this.mainView;
@@ -138,6 +148,7 @@ export class MainViewProxy {
138
148
  if (mainViewScenePath) {
139
149
  setViewFocusScenePath(mainView, mainViewScenePath);
140
150
  }
151
+ this.synchronizer.setView(mainView);
141
152
  return mainView;
142
153
  }
143
154
 
@@ -172,12 +183,20 @@ export class MainViewProxy {
172
183
  }
173
184
 
174
185
  private onCameraUpdatedByDevice = (camera: Camera) => {
175
- this.store.setMainViewCamera({ ...camera, id: this.manager.uid });
176
- if (!isEqual(this.mainViewSize, { ...this.mainView.size, id: this.manager.uid })) {
177
- this.setMainViewSize(this.view.size);
186
+ this.synchronizer.onLocalCameraUpdate(camera);
187
+ const size = this.getStageSize();
188
+ if (size && !isEqual(size, this.mainViewSize)) {
189
+ this.setMainViewSize(size);
178
190
  }
179
191
  };
180
192
 
193
+ private getStageSize(): Size | undefined {
194
+ const stage = this.manager.boxManager?.stageRect;
195
+ if (stage) {
196
+ return { width: stage.width, height: stage.height };
197
+ }
198
+ }
199
+
181
200
  public addMainViewListener(): void {
182
201
  if (this.mainViewIsAddListener) return;
183
202
  if (this.view.divElement) {
@@ -205,7 +224,7 @@ export class MainViewProxy {
205
224
  this.manager.boxManager?.blurAllBox();
206
225
  }
207
226
 
208
- public setMainViewSize = debounce(size => {
227
+ public setMainViewSize = debounce((size: Size) => {
209
228
  this.store.setMainViewSize({ ...size, id: this.manager.uid });
210
229
  }, 50);
211
230
 
@@ -225,33 +244,6 @@ export class MainViewProxy {
225
244
  callbacks.emit("cameraStateChange", this.cameraState);
226
245
  };
227
246
 
228
- public moveCameraToContian(size: Size): void {
229
- if (!isEmpty(size)) {
230
- this.view.moveCameraToContain({
231
- width: size.width,
232
- height: size.height,
233
- originX: -size.width / 2,
234
- originY: -size.height / 2,
235
- animationMode: AnimationMode.Immediately,
236
- });
237
- this.scale = this.view.camera.scale;
238
- }
239
- }
240
-
241
- public moveCamera(camera: Camera): void {
242
- if (!isEmpty(camera)) {
243
- if (isEqual(camera, this.view.camera)) return;
244
- const { centerX, centerY, scale } = camera;
245
- const needScale = scale * (this.scale || 1);
246
- this.view.moveCamera({
247
- centerX: centerX,
248
- centerY: centerY,
249
- scale: needScale,
250
- animationMode: AnimationMode.Immediately,
251
- });
252
- }
253
- }
254
-
255
247
  public stop() {
256
248
  this.removeCameraListener();
257
249
  this.manager.refresher?.remove(Fields.MainViewCamera);