@netless/window-manager 0.3.17-canary.2 → 0.4.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.
package/src/BoxManager.ts CHANGED
@@ -1,22 +1,22 @@
1
- import { callbacks, emitter, WindowManager } from "./index";
2
- import { debounce, maxBy } from "lodash";
3
1
  import { AppAttributes, DEFAULT_COLLECTOR_STYLE, Events, MIN_HEIGHT, MIN_WIDTH } from "./constants";
2
+ import { debounce, maxBy } from "lodash";
4
3
  import {
5
4
  TELE_BOX_MANAGER_EVENT,
6
5
  TELE_BOX_STATE,
7
6
  TeleBoxCollector,
8
7
  TeleBoxManager,
9
8
  } from "@netless/telebox-insider";
10
- import type { AddAppOptions, AppInitState } from "./index";
9
+ import { WindowManager } from "./index";
10
+ import type { AddAppOptions, AppInitState, EmitterType, CallbacksType } from "./index";
11
11
  import type {
12
12
  TeleBoxManagerUpdateConfig,
13
13
  TeleBoxManagerCreateConfig,
14
14
  ReadonlyTeleBox,
15
15
  TeleBoxManagerConfig,
16
16
  TeleBoxColorScheme,
17
+ TeleBoxRect,
17
18
  } from "@netless/telebox-insider";
18
19
  import type Emittery from "emittery";
19
- import type { AppManager } from "./AppManager";
20
20
  import type { NetlessApp } from "./typings";
21
21
  import type { View } from "white-web-sdk";
22
22
 
@@ -48,31 +48,63 @@ export type CreateTeleBoxManagerConfig = {
48
48
  prefersColorScheme?: TeleBoxColorScheme;
49
49
  };
50
50
 
51
+ export type BoxManagerContext = {
52
+ safeSetAttributes: (attributes: any) => void;
53
+ getMainView: () => View;
54
+ updateAppState: (appId: string, field: AppAttributes, value: any) => void;
55
+ emitter: EmitterType;
56
+ callbacks: CallbacksType;
57
+ canOperate: () => boolean;
58
+ notifyContainerRectUpdate: (rect: TeleBoxRect) => void;
59
+ cleanFocus: () => void;
60
+ };
61
+
62
+ export const createBoxManager = (
63
+ manager: WindowManager,
64
+ callbacks: CallbacksType,
65
+ emitter: EmitterType,
66
+ options: CreateTeleBoxManagerConfig
67
+ ) => {
68
+ return new BoxManager(
69
+ {
70
+ safeSetAttributes: (attributes: any) => manager.safeSetAttributes(attributes),
71
+ getMainView: () => manager.mainView,
72
+ updateAppState: (...args) => manager.appManager?.store.updateAppState(...args),
73
+ canOperate: () => manager.canOperate,
74
+ notifyContainerRectUpdate: (rect: TeleBoxRect) =>
75
+ manager.appManager?.notifyContainerRectUpdate(rect),
76
+ cleanFocus: () => manager.appManager?.store.cleanFocus(),
77
+ callbacks,
78
+ emitter,
79
+ },
80
+ options
81
+ );
82
+ };
83
+
51
84
  export class BoxManager {
52
85
  public teleBoxManager: TeleBoxManager;
53
- public appBoxMap: Map<string, string> = new Map();
54
- private mainView = this.manager.mainView;
55
86
 
56
87
  constructor(
57
- private manager: AppManager,
58
- createTeleBoxManagerConfig?: CreateTeleBoxManagerConfig
88
+ private context: BoxManagerContext,
89
+ private createTeleBoxManagerConfig?: CreateTeleBoxManagerConfig
59
90
  ) {
91
+ const { emitter, callbacks } = context;
60
92
  this.teleBoxManager = this.setupBoxManager(createTeleBoxManagerConfig);
61
93
  this.teleBoxManager.events.on(TELE_BOX_MANAGER_EVENT.State, state => {
62
94
  if (state) {
63
- callbacks.emit("boxStateChange", state);
64
- emitter.emit("boxStateChange", state);
95
+ this.context.callbacks.emit("boxStateChange", state);
96
+ this.context.emitter.emit("boxStateChange", state);
65
97
  }
66
98
  });
67
99
  this.teleBoxManager.events.on("minimized", minimized => {
68
- this.manager.safeSetAttributes({ minimized });
100
+ this.context.safeSetAttributes({ minimized });
69
101
  if (minimized) {
70
- this.manager.store.cleanFocus();
102
+ this.context.cleanFocus();
71
103
  this.blurAllBox();
72
104
  }
73
105
  });
74
106
  this.teleBoxManager.events.on("maximized", maximized => {
75
- this.manager.safeSetAttributes({ maximized });
107
+ this.context.safeSetAttributes({ maximized });
76
108
  });
77
109
  this.teleBoxManager.events.on("removed", boxes => {
78
110
  boxes.forEach(box => {
@@ -97,7 +129,7 @@ export class BoxManager {
97
129
  );
98
130
  this.teleBoxManager.events.on("focused", box => {
99
131
  if (box) {
100
- if (this.manager.canOperate) {
132
+ if (this.canOperate) {
101
133
  emitter.emit("focus", { appId: box.id });
102
134
  } else {
103
135
  this.teleBoxManager.blurBox(box.id);
@@ -111,10 +143,18 @@ export class BoxManager {
111
143
  callbacks.emit("prefersColorSchemeChange", colorScheme);
112
144
  });
113
145
  this.teleBoxManager.events.on("z_index", box => {
114
- this.manager.store.updateAppState(box.id, AppAttributes.ZIndex, box.zIndex);
146
+ this.context.updateAppState(box.id, AppAttributes.ZIndex, box.zIndex);
115
147
  });
116
148
  }
117
149
 
150
+ private get mainView() {
151
+ return this.context.getMainView();
152
+ }
153
+
154
+ private get canOperate() {
155
+ return this.context.canOperate();
156
+ }
157
+
118
158
  public get boxState() {
119
159
  return this.teleBoxManager.state;
120
160
  }
@@ -135,6 +175,10 @@ export class BoxManager {
135
175
  return this.teleBoxManager.prefersColorScheme;
136
176
  }
137
177
 
178
+ public get boxSize() {
179
+ return this.teleBoxManager.boxes.length;
180
+ }
181
+
138
182
  public createBox(params: CreateBoxParams): void {
139
183
  if (!this.teleBoxManager) return;
140
184
  let { minwidth = MIN_WIDTH, minheight = MIN_HEIGHT } = params.app.config ?? {};
@@ -159,14 +203,14 @@ export class BoxManager {
159
203
  id: params.appId,
160
204
  };
161
205
  this.teleBoxManager.create(createBoxConfig, params.smartPosition);
162
- emitter.emit(`${params.appId}${Events.WindowCreated}` as any);
206
+ this.context.emitter.emit(`${params.appId}${Events.WindowCreated}` as any);
163
207
  }
164
208
 
165
209
  public setBoxInitState(appId: string): void {
166
210
  const box = this.teleBoxManager.queryOne({ id: appId });
167
211
  if (box) {
168
212
  if (box.state === TELE_BOX_STATE.Maximized) {
169
- emitter.emit("resize", {
213
+ this.context.emitter.emit("resize", {
170
214
  appId: appId,
171
215
  x: box.x,
172
216
  y: box.y,
@@ -193,24 +237,30 @@ export class BoxManager {
193
237
  fence: false,
194
238
  prefersColorScheme: createTeleBoxManagerConfig?.prefersColorScheme,
195
239
  };
196
- const container = createTeleBoxManagerConfig?.collectorContainer || WindowManager.wrapper;
197
- const styles = {
198
- ...DEFAULT_COLLECTOR_STYLE,
199
- ...createTeleBoxManagerConfig?.collectorStyles,
200
- };
201
- const teleBoxCollector = new TeleBoxCollector({
202
- styles: styles,
203
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
204
- }).mount(container!);
205
- initManagerState.collector = teleBoxCollector;
240
+
206
241
  const manager = new TeleBoxManager(initManagerState);
207
242
  if (this.teleBoxManager) {
208
243
  this.teleBoxManager.destroy();
209
244
  }
210
245
  this.teleBoxManager = manager;
246
+ const container = createTeleBoxManagerConfig?.collectorContainer || WindowManager.wrapper;
247
+ if (container) {
248
+ this.setCollectorContainer(container);
249
+ }
211
250
  return manager;
212
251
  }
213
252
 
253
+ public setCollectorContainer(container: HTMLElement) {
254
+ const styles = {
255
+ ...DEFAULT_COLLECTOR_STYLE,
256
+ ...this.createTeleBoxManagerConfig?.collectorStyles,
257
+ };
258
+ const collector = new TeleBoxCollector({
259
+ styles
260
+ }).mount(container);
261
+ this.teleBoxManager.setCollector(collector);
262
+ }
263
+
214
264
  public getBox(appId: string): ReadonlyTeleBox | undefined {
215
265
  return this.teleBoxManager.queryOne({ id: appId });
216
266
  }
@@ -249,16 +299,18 @@ export class BoxManager {
249
299
  },
250
300
  true
251
301
  );
252
- if (state.maximized != null) {
253
- this.teleBoxManager.setMaximized(Boolean(state.maximized), true);
254
- this.teleBoxManager.setMinimized(Boolean(state.minimized), true);
255
- }
256
302
  setTimeout(() => {
257
303
  if (state.focus) {
258
- this.teleBoxManager.focusBox(box.id, true)
304
+ this.teleBoxManager.focusBox(box.id, true);
305
+ }
306
+ if (state.maximized != null) {
307
+ this.teleBoxManager.setMaximized(Boolean(state.maximized), true);
308
+ }
309
+ if (state.minimized != null) {
310
+ this.teleBoxManager.setMinimized(Boolean(state.minimized), true);
259
311
  }
260
312
  }, 50);
261
- callbacks.emit("boxStateChange", this.teleBoxManager.state);
313
+ this.context.callbacks.emit("boxStateChange", this.teleBoxManager.state);
262
314
  }
263
315
  }
264
316
 
@@ -267,7 +319,7 @@ export class BoxManager {
267
319
  if (rect && rect.width > 0 && rect.height > 0) {
268
320
  const containerRect = { x: 0, y: 0, width: rect.width, height: rect.height };
269
321
  this.teleBoxManager.setContainerRect(containerRect);
270
- this.manager.notifyContainerRectUpdate(this.teleBoxManager.containerRect);
322
+ this.context.notifyContainerRectUpdate(this.teleBoxManager.containerRect);
271
323
  }
272
324
  }
273
325
 
@@ -307,7 +359,9 @@ export class BoxManager {
307
359
  }
308
360
 
309
361
  public setMaximized(maximized: boolean) {
310
- this.teleBoxManager.setMaximized(maximized, true);
362
+ if (maximized !== this.maximized) {
363
+ this.teleBoxManager.setMaximized(maximized, true);
364
+ }
311
365
  }
312
366
 
313
367
  public setMinimized(minimized: boolean, skipUpdate = true) {
@@ -332,6 +386,10 @@ export class BoxManager {
332
386
  this.teleBoxManager.setPrefersColorScheme(colorScheme);
333
387
  }
334
388
 
389
+ public setZIndex(id: string, zIndex: number, skipUpdate = true) {
390
+ this.teleBoxManager.update(id, { zIndex }, skipUpdate);
391
+ }
392
+
335
393
  public destroy() {
336
394
  this.teleBoxManager.destroy();
337
395
  }
@@ -70,13 +70,20 @@ export class Cursor extends Base {
70
70
  }
71
71
 
72
72
  private moveCursor(cursor: Position, rect: DOMRect, view: any) {
73
- const { x, y } = cursor;
73
+ const { x, y, type } = cursor;
74
74
  const point = view?.screen.convertPointToScreen(x, y);
75
75
  if (point) {
76
- const translateX = point.x + rect.x - 2;
77
- const translateY = point.y + rect.y - 18;
76
+ let translateX = point.x - 2;
77
+ let translateY = point.y - 18;
78
+ if (type === "app") {
79
+ const wrapperRect = this.cursorManager.wrapperRect;
80
+ if (wrapperRect) {
81
+ translateX = translateX + rect.x - wrapperRect.x;
82
+ translateY = translateY + rect.y - wrapperRect.y;
83
+ }
84
+ }
78
85
  if (point.x < 0 || point.x > rect.width || point.y < 0 || point.y > rect.height) {
79
- this.component?.$set({ visible: false });
86
+ this.component?.$set({ visible: false, x: translateX, y: translateY });
80
87
  } else {
81
88
  this.component?.$set({ visible: true, x: translateX, y: translateY });
82
89
  }
@@ -149,7 +156,7 @@ export class Cursor extends Base {
149
156
  private async createCursor() {
150
157
  if (this.member && this.wrapper) {
151
158
  this.component = new App({
152
- target: document.documentElement,
159
+ target: this.wrapper,
153
160
  props: this.initProps(),
154
161
  });
155
162
  }
@@ -31,17 +31,24 @@ export class CursorManager extends Base {
31
31
  this.roomMembers = this.appManager.room?.state.roomMembers;
32
32
  const wrapper = WindowManager.wrapper;
33
33
  if (wrapper) {
34
- wrapper.addEventListener("mousemove", this.mouseMoveListener);
35
- wrapper.addEventListener("touchstart", this.touchMoveListener);
36
- wrapper.addEventListener("touchmove", this.touchMoveListener);
37
- wrapper.addEventListener("mouseleave", this.mouseLeaveListener);
38
- wrapper.addEventListener("touchend", this.mouseLeaveListener);
39
- this.initCursorAttributes();
40
- this.wrapperRect = wrapper.getBoundingClientRect();
41
- this.startReaction(wrapper);
34
+ this.setupWrapper(wrapper);
42
35
  }
43
36
  }
44
37
 
38
+ public setupWrapper(wrapper: HTMLElement) {
39
+ if (this.manager.refresher?.hasReactor("cursors")) {
40
+ this.destroy();
41
+ }
42
+ wrapper.addEventListener("mousemove", this.mouseMoveListener);
43
+ wrapper.addEventListener("touchstart", this.touchMoveListener);
44
+ wrapper.addEventListener("touchmove", this.touchMoveListener);
45
+ wrapper.addEventListener("mouseleave", this.mouseLeaveListener);
46
+ wrapper.addEventListener("touchend", this.mouseLeaveListener);
47
+ this.initCursorAttributes();
48
+ this.wrapperRect = wrapper.getBoundingClientRect();
49
+ this.startReaction(wrapper);
50
+ }
51
+
45
52
  public setMainViewDivElement(div: HTMLDivElement) {
46
53
  this.mainViewElement = div;
47
54
  }
package/src/MainView.ts CHANGED
@@ -5,6 +5,7 @@ import { createView } from "./ViewManager";
5
5
  import { debounce, isEmpty, isEqual } from "lodash";
6
6
  import { Fields } from "./AttributesDelegate";
7
7
  import { notifyMainViewModeChange, setViewFocusScenePath, setViewMode } from "./Utils/Common";
8
+ import { SideEffectManager } from "side-effect-manager";
8
9
  import type { Camera, Size, View } from "white-web-sdk";
9
10
  import type { AppManager } from "./AppManager";
10
11
 
@@ -16,6 +17,8 @@ export class MainViewProxy extends Base {
16
17
  private mainView: View;
17
18
  private viewId = "mainView";
18
19
 
20
+ private sideEffectManager = new SideEffectManager();
21
+
19
22
  constructor(manager: AppManager) {
20
23
  super(manager);
21
24
  this.mainView = this.createMainView();
@@ -29,8 +32,12 @@ export class MainViewProxy extends Base {
29
32
  }
30
33
  }, 200); // 等待 mainView 挂载完毕再进行监听,否则会触发不必要的 onSizeUpdated
31
34
  });
32
- emitter.on("playgroundSizeChange", () => {
35
+ const playgroundSizeChangeListener = () => {
33
36
  this.sizeChangeHandler(this.mainViewSize);
37
+ }
38
+ this.sideEffectManager.add(() => {
39
+ emitter.on("playgroundSizeChange", playgroundSizeChangeListener);
40
+ return () => emitter.off("playgroundSizeChange", playgroundSizeChangeListener);
34
41
  });
35
42
  }
36
43
 
@@ -75,7 +82,7 @@ export class MainViewProxy extends Base {
75
82
  );
76
83
  };
77
84
 
78
- private sizeChangeHandler = debounce((size: Size) => {
85
+ private sizeChangeHandler = debounce((size: Size) => {
79
86
  if (size) {
80
87
  this.moveCameraToContian(size);
81
88
  this.moveCamera(this.mainViewCamera);
@@ -104,7 +111,7 @@ export class MainViewProxy extends Base {
104
111
 
105
112
  private onCameraUpdatedByDevice = (camera: Camera) => {
106
113
  this.store.setMainViewCamera({ ...camera, id: this.context.uid });
107
- if (!isEqual(this.mainViewSize, {...this.mainView.size, id: this.context.uid})) {
114
+ if (!isEqual(this.mainViewSize, { ...this.mainView.size, id: this.context.uid })) {
108
115
  this.setMainViewSize(this.view.size);
109
116
  }
110
117
  };
@@ -204,5 +211,6 @@ export class MainViewProxy extends Base {
204
211
  public destroy() {
205
212
  this.stop();
206
213
  this.cameraStore.unregister(this.viewId, this.mainView);
214
+ this.sideEffectManager.flushAll();
207
215
  }
208
216
  }
@@ -1,8 +1,11 @@
1
- import { isFunction } from 'lodash';
2
- import { RoomPhase } from 'white-web-sdk';
1
+ import { isFunction, debounce } from "lodash";
2
+ import { log } from "./Utils/log";
3
+ import { RoomPhase } from "white-web-sdk";
3
4
  import type { Room } from "white-web-sdk";
4
- import type { AppManager } from './AppManager';
5
- import { log } from './Utils/log';
5
+
6
+ export type ReconnectRefresherContext = {
7
+ notifyReconnected: () => void;
8
+ }
6
9
 
7
10
  // 白板重连之后会刷新所有的对象,导致 listener 失效, 所以这里在重连之后重新对所有对象进行监听
8
11
  export class ReconnectRefresher {
@@ -11,7 +14,7 @@ export class ReconnectRefresher {
11
14
  private reactors: Map<string, any> = new Map();
12
15
  private disposers: Map<string, any> = new Map();
13
16
 
14
- constructor(room: Room | undefined, private manager: AppManager) {
17
+ constructor(room: Room | undefined, private ctx: ReconnectRefresherContext) {
15
18
  this.room = room;
16
19
  this.phase = room?.phase;
17
20
  room?.callbacks.on("onPhaseChanged", this.onPhaseChanged);
@@ -22,9 +25,9 @@ export class ReconnectRefresher {
22
25
  this.onReconnected();
23
26
  }
24
27
  this.phase = phase;
25
- }
28
+ };
26
29
 
27
- private onReconnected = () => {
30
+ private onReconnected = debounce(() => {
28
31
  log("onReconnected refresh reactors");
29
32
  this.releaseDisposers();
30
33
  this.reactors.forEach((func, id) => {
@@ -32,15 +35,15 @@ export class ReconnectRefresher {
32
35
  this.disposers.set(id, func());
33
36
  }
34
37
  });
35
- this.manager.notifyReconnected();
36
- }
38
+ this.ctx.notifyReconnected();
39
+ }, 3000);
37
40
 
38
41
  private releaseDisposers() {
39
42
  this.disposers.forEach(disposer => {
40
43
  if (isFunction(disposer)) {
41
44
  disposer();
42
45
  }
43
- })
46
+ });
44
47
  this.disposers.clear();
45
48
  }
46
49
 
@@ -64,6 +67,10 @@ export class ReconnectRefresher {
64
67
  }
65
68
  }
66
69
 
70
+ public hasReactor(id: string) {
71
+ return this.reactors.has(id);
72
+ }
73
+
67
74
  public destroy() {
68
75
  this.room?.callbacks.off("onPhaseChanged", this.onPhaseChanged);
69
76
  this.releaseDisposers();
@@ -1,10 +1,10 @@
1
- import { emitter } from '../index';
2
- import { isPlayer } from 'white-web-sdk';
1
+ import { emitter } from "../index";
2
+ import { isPlayer } from "white-web-sdk";
3
+ import type { WindowManager } from '../index';
3
4
  import type { Camera, Room , Player , PlayerSeekingResult } from "white-web-sdk";
4
- import type { AppManager } from "../AppManager";
5
5
 
6
6
  // 修改多窗口状态下一些失效的方法实现到 manager 的 mainview 上, 降低迁移成本
7
- export const replaceRoomFunction = (room: Room, manager: AppManager) => {
7
+ export const replaceRoomFunction = (room: Room, manager: WindowManager) => {
8
8
  if (isPlayer(room)) {
9
9
  const player = room as unknown as Player;
10
10
  const originSeek = player.seekToProgressTime;
@@ -30,7 +30,7 @@ export const replaceRoomFunction = (room: Room, manager: AppManager) => {
30
30
  });
31
31
 
32
32
  room.moveCamera = (camera: Camera) => manager.mainView.moveCamera(camera);
33
- room.moveCameraToContain = (...args) => manager.mainView.moveCameraToContain(...args);
33
+ room.moveCameraToContain = (...args) => manager.moveCameraToContain(...args);
34
34
  room.convertToPointInWorld = (...args) => manager.mainView.convertToPointInWorld(...args);
35
35
  room.setCameraBound = (...args) => manager.mainView.setCameraBound(...args);
36
36
  room.scenePreview = (...args) => manager.mainView.scenePreview(...args);
@@ -1,3 +1,4 @@
1
+
1
2
  export class AppCreateError extends Error {
2
3
  override message = "[WindowManager]: app duplicate exists and cannot be created again";
3
4
  }
@@ -28,4 +29,8 @@ export class BoxNotCreatedError extends Error {
28
29
 
29
30
  export class InvalidScenePath extends Error {
30
31
  override message = `[WindowManager]: ScenePath should start with "/"`;
31
- }
32
+ }
33
+
34
+ export class BoxManagerNotFoundError extends Error {
35
+ override message = "[WindowManager]: boxManager not found";
36
+ }
package/src/constants.ts CHANGED
@@ -10,6 +10,7 @@ export enum Events {
10
10
  SetMainViewScenePath = "SetMainViewScenePath",
11
11
  SetMainViewSceneIndex = "SetMainViewSceneIndex",
12
12
  SwitchViewsToFreedom = "SwitchViewsToFreedom",
13
+ MoveCameraToContain = "MoveCameraToContain"
13
14
  }
14
15
 
15
16
  export const MagixEventName = "__WindowManger";