@netless/window-manager 0.3.17-canary.1 → 0.4.0-canary.0

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/index.ts CHANGED
@@ -4,6 +4,7 @@ import Emittery from "emittery";
4
4
  import pRetry from "p-retry";
5
5
  import { AppManager } from "./AppManager";
6
6
  import { appRegister } from "./Register";
7
+ import { createBoxManager } from "./BoxManager";
7
8
  import { CursorManager } from "./Cursor";
8
9
  import { DEFAULT_CONTAINER_RATIO, REQUIRE_VERSION } from "./constants";
9
10
  import { Fields } from "./AttributesDelegate";
@@ -22,7 +23,7 @@ import {
22
23
  isValidScenePath,
23
24
  wait,
24
25
  } from "./Utils/Common";
25
- import type { TELE_BOX_STATE } from "./BoxManager";
26
+ import type { TELE_BOX_STATE, BoxManager } from "./BoxManager";
26
27
  import {
27
28
  AppCreateError,
28
29
  AppManagerNotInitError,
@@ -108,6 +109,7 @@ export type AppSyncAttributes = {
108
109
  state?: any;
109
110
  isDynamicPPT?: boolean;
110
111
  fullPath?: string;
112
+ createdAt?: number;
111
113
  };
112
114
 
113
115
  export type AppInitState = {
@@ -139,7 +141,8 @@ export type EmitterEvent = {
139
141
  playgroundSizeChange: DOMRect;
140
142
  };
141
143
 
142
- export const emitter: Emittery<EmitterEvent> = new Emittery();
144
+ export type EmitterType = Emittery<EmitterEvent>;
145
+ export const emitter: EmitterType = new Emittery();
143
146
 
144
147
  export type PublicEvent = {
145
148
  mainViewModeChange: ViewVisionMode;
@@ -151,7 +154,7 @@ export type PublicEvent = {
151
154
 
152
155
  export type MountParams = {
153
156
  room: Room;
154
- container: HTMLElement;
157
+ container?: HTMLElement;
155
158
  /** 白板高宽比例, 默认为 9 / 16 */
156
159
  containerSizeRatio?: number;
157
160
  /** 显示 PS 透明背景,默认 true */
@@ -165,7 +168,8 @@ export type MountParams = {
165
168
  prefersColorScheme?: TeleBoxColorScheme;
166
169
  };
167
170
 
168
- export const callbacks: Emittery<PublicEvent> = new Emittery();
171
+ export type CallbacksType = Emittery<PublicEvent>;
172
+ export const callbacks: CallbacksType = new Emittery();
169
173
 
170
174
  export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
171
175
  public static kind = "WindowManager";
@@ -177,7 +181,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
177
181
  public static containerSizeRatio = DEFAULT_CONTAINER_RATIO;
178
182
  private static isCreated = false;
179
183
 
180
- public version = "0.3.17-canary.1";
184
+ public version = "0.4.0-canary.0";
181
185
 
182
186
  public appListeners?: AppListeners;
183
187
 
@@ -188,75 +192,22 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
188
192
  public viewMode = ViewMode.Broadcaster;
189
193
  public isReplay = isPlayer(this.displayer);
190
194
 
195
+ private boxManager?: BoxManager;
196
+ private static params?: MountParams;
197
+
191
198
  constructor(context: InvisiblePluginContext) {
192
199
  super(context);
200
+ WindowManager.displayer = context.displayer;
193
201
  }
194
202
 
195
- /**
196
- * 挂载 WindowManager
197
- * @deprecated
198
- */
199
- public static async mount(
200
- room: Room,
201
- container: HTMLElement,
202
- collectorContainer?: HTMLElement,
203
- options?: {
204
- chessboard: boolean;
205
- containerSizeRatio: number;
206
- collectorStyles?: Partial<CSSStyleDeclaration>;
207
- debug?: boolean;
208
- overwriteStyles?: string;
209
- }
210
- ): Promise<WindowManager>;
211
-
212
- public static async mount(params: MountParams): Promise<WindowManager>;
213
-
214
- public static async mount(
215
- params: MountParams | Room,
216
- container?: HTMLElement,
217
- collectorContainer?: HTMLElement,
218
- options?: {
219
- chessboard?: boolean;
220
- containerSizeRatio: number;
221
- collectorStyles?: Partial<CSSStyleDeclaration>;
222
- debug?: boolean;
223
- overwriteStyles?: string;
224
- disableCameraTransform?: boolean;
225
- }
226
- ): Promise<WindowManager> {
227
- let room: Room;
228
- let containerSizeRatio: number | undefined;
229
- let collectorStyles: Partial<CSSStyleDeclaration> | undefined;
230
- let debug: boolean | undefined;
231
- let chessboard = true;
232
- let overwriteStyles: string | undefined;
233
- let cursor: boolean | undefined;
234
- let disableCameraTransform = false;
235
- let prefersColorScheme: TeleBoxColorScheme | undefined = "light";
236
- if ("room" in params) {
237
- room = params.room;
238
- container = params.container;
239
- collectorContainer = params.collectorContainer;
240
- containerSizeRatio = params.containerSizeRatio;
241
- collectorStyles = params.collectorStyles;
242
- debug = params.debug;
243
- if (params.chessboard != null) {
244
- chessboard = params.chessboard;
245
- }
246
- overwriteStyles = params.overwriteStyles;
247
- cursor = params.cursor;
248
- disableCameraTransform = Boolean(params?.disableCameraTransform);
249
- prefersColorScheme = params.prefersColorScheme;
250
- } else {
251
- room = params;
252
- containerSizeRatio = options?.containerSizeRatio;
253
- collectorStyles = options?.collectorStyles;
254
- debug = options?.debug;
255
- if (options?.chessboard != null) {
256
- chessboard = options.chessboard;
257
- }
258
- overwriteStyles = options?.overwriteStyles;
259
- }
203
+ public static async mount(params: MountParams): Promise<WindowManager> {
204
+ const room = params.room;
205
+ WindowManager.container = params.container;
206
+ const containerSizeRatio = params.containerSizeRatio;
207
+ const debug = params.debug;
208
+
209
+ const cursor = params.cursor;
210
+ WindowManager.params = params;
260
211
 
261
212
  this.checkVersion();
262
213
  if (isRoom(room)) {
@@ -264,9 +215,6 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
264
215
  throw new Error("[WindowManager]: Room only Connected can be mount");
265
216
  }
266
217
  }
267
- if (!container) {
268
- throw new Error("[WindowManager]: Container must provide");
269
- }
270
218
  if (WindowManager.isCreated) {
271
219
  throw new Error("[WindowManager]: Already created cannot be created again");
272
220
  }
@@ -297,29 +245,19 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
297
245
  if (containerSizeRatio) {
298
246
  WindowManager.containerSizeRatio = containerSizeRatio;
299
247
  }
300
- WindowManager.container = container;
301
- const { playground, wrapper, sizer, mainViewElement } = setupWrapper(container);
302
- WindowManager.playground = playground;
303
- if (chessboard) {
304
- sizer.classList.add("netless-window-manager-chess-sizer");
305
- }
306
- if (overwriteStyles) {
307
- const style = document.createElement("style");
308
- style.textContent = overwriteStyles;
309
- playground.appendChild(style);
310
- }
311
248
  await manager.ensureAttributes();
312
- manager.appManager = new AppManager(manager, {
313
- collectorContainer: collectorContainer,
314
- collectorStyles: collectorStyles,
315
- prefersColorScheme: prefersColorScheme,
316
- });
317
- manager.observePlaygroundSize(playground, sizer, wrapper);
249
+
250
+ manager.appManager = new AppManager(manager);
251
+
318
252
  if (cursor) {
319
253
  manager.cursorManager = new CursorManager(manager.appManager);
320
254
  }
321
- manager.bindMainView(mainViewElement, disableCameraTransform);
322
- replaceRoomFunction(room, manager.appManager);
255
+
256
+ if (params.container) {
257
+ manager.bindContainer(params.container, params.collectorContainer);
258
+ }
259
+
260
+ replaceRoomFunction(room, manager);
323
261
  emitter.emit("onCreated");
324
262
  WindowManager.isCreated = true;
325
263
  try {
@@ -359,6 +297,56 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
359
297
  return manager;
360
298
  }
361
299
 
300
+ private static initContainer(
301
+ manager: WindowManager,
302
+ container: HTMLElement,
303
+ chessboard: boolean | undefined,
304
+ overwriteStyles: string | undefined
305
+ ) {
306
+ if (!WindowManager.container) {
307
+ WindowManager.container = container;
308
+ }
309
+ const { playground, wrapper, sizer, mainViewElement } = setupWrapper(container);
310
+ WindowManager.playground = playground;
311
+ if (chessboard) {
312
+ sizer.classList.add("netless-window-manager-chess-sizer");
313
+ }
314
+ if (overwriteStyles) {
315
+ const style = document.createElement("style");
316
+ style.textContent = overwriteStyles;
317
+ playground.appendChild(style);
318
+ }
319
+ manager.observePlaygroundSize(playground, sizer, wrapper);
320
+ return mainViewElement;
321
+ }
322
+
323
+ public bindContainer(container: HTMLElement, collectorContainer?: HTMLElement) {
324
+ if (WindowManager.params) {
325
+ const params = WindowManager.params;
326
+ const mainViewElement = WindowManager.initContainer(
327
+ this,
328
+ container,
329
+ params.chessboard,
330
+ params.overwriteStyles
331
+ );
332
+ const boxManager = createBoxManager(this, callbacks, emitter, {
333
+ collectorContainer: collectorContainer,
334
+ collectorStyles: params.collectorStyles,
335
+ prefersColorScheme: params.prefersColorScheme,
336
+ });
337
+ this.boxManager = boxManager;
338
+ this.appManager?.setBoxManager(boxManager);
339
+ this.bindMainView(mainViewElement, params.disableCameraTransform);
340
+ this.boxManager.updateManagerRect();
341
+ this.appManager?.refresh();
342
+ this.appManager?.resetMaximized();
343
+ this.appManager?.resetMinimized();
344
+ if (WindowManager.wrapper) {
345
+ this.cursorManager?.setupWrapper(WindowManager.wrapper);
346
+ }
347
+ }
348
+ }
349
+
362
350
  /**
363
351
  * 注册插件
364
352
  */
@@ -368,6 +356,26 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
368
356
  return appRegister.register(params);
369
357
  }
370
358
 
359
+ public static setContainer(container: HTMLElement) {
360
+ if (this.isCreated && WindowManager.container) {
361
+ if (WindowManager.container.firstChild) {
362
+ container.appendChild(WindowManager.container.firstChild);
363
+ }
364
+ }
365
+ WindowManager.container = container;
366
+ }
367
+
368
+ public static setCollectorContainer(container: HTMLElement) {
369
+ const manager = this.displayer.getInvisiblePlugin(this.kind) as WindowManager;
370
+ if (this.isCreated && manager) {
371
+ manager.boxManager?.setCollectorContainer(container);
372
+ } else {
373
+ if (this.params) {
374
+ this.params.collectorContainer = container;
375
+ }
376
+ }
377
+ }
378
+
371
379
  /**
372
380
  * 创建一个 app 至白板
373
381
  */
@@ -470,7 +478,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
470
478
  public setReadonly(readonly: boolean): void {
471
479
  if (this.room?.isWritable) {
472
480
  this.readonly = readonly;
473
- this.appManager?.boxManager.setReadonly(readonly);
481
+ this.appManager?.boxManager?.setReadonly(readonly);
474
482
  }
475
483
  }
476
484
 
@@ -531,21 +539,21 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
531
539
  return this.appManager?.store.apps();
532
540
  }
533
541
 
534
- public get boxState(): TeleBoxState {
542
+ public get boxState(): TeleBoxState | undefined {
535
543
  if (this.appManager) {
536
- return this.appManager.boxManager.boxState;
544
+ return this.appManager.boxManager?.boxState;
537
545
  } else {
538
546
  throw new AppManagerNotInitError();
539
547
  }
540
548
  }
541
549
 
542
550
  public get darkMode(): boolean {
543
- return Boolean(this.appManager?.boxManager.darkMode);
551
+ return Boolean(this.appManager?.boxManager?.darkMode);
544
552
  }
545
553
 
546
- public get prefersColorScheme(): TeleBoxColorScheme {
554
+ public get prefersColorScheme(): TeleBoxColorScheme | undefined {
547
555
  if (this.appManager) {
548
- return this.appManager.boxManager.prefersColorScheme;
556
+ return this.appManager.boxManager?.prefersColorScheme;
549
557
  } else {
550
558
  throw new AppManagerNotInitError();
551
559
  }
@@ -613,12 +621,13 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
613
621
  if (WindowManager.playground) {
614
622
  WindowManager.playground.parentNode?.removeChild(WindowManager.playground);
615
623
  }
624
+ WindowManager.params = undefined;
616
625
  log("Destroyed");
617
626
  }
618
627
 
619
- private bindMainView(divElement: HTMLDivElement, disableCameraTransform: boolean) {
628
+ private bindMainView(divElement: HTMLDivElement, disableCameraTransform: boolean | undefined) {
620
629
  if (this.appManager) {
621
- this.appManager.bindMainView(divElement, disableCameraTransform);
630
+ this.appManager.bindMainView(divElement, Boolean(disableCameraTransform));
622
631
  this.cursorManager?.setMainViewDivElement(divElement);
623
632
  }
624
633
  }
@@ -651,7 +660,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
651
660
  }
652
661
 
653
662
  public setPrefersColorScheme(scheme: TeleBoxColorScheme): void {
654
- this.appManager?.boxManager.setPrefersColorScheme(scheme);
663
+ this.appManager?.boxManager?.setPrefersColorScheme(scheme);
655
664
  }
656
665
 
657
666
  private isDynamicPPT(scenes: SceneDefinition[]) {
@@ -701,7 +710,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
701
710
  if (containerRect) {
702
711
  this.updateSizer(containerRect, sizer, wrapper);
703
712
  this.cursorManager?.updateContainerRect();
704
- this.appManager?.boxManager.updateManagerRect();
713
+ this.boxManager?.updateManagerRect();
705
714
  emitter.emit("playgroundSizeChange", containerRect);
706
715
  }
707
716
  });
package/src/style.css CHANGED
@@ -115,7 +115,7 @@
115
115
  flex-direction: column;
116
116
  align-items: center;
117
117
  justify-content: center;
118
- position: fixed;
118
+ position: absolute;
119
119
  width: 26px;
120
120
  height: 26px;
121
121
  z-index: 2147483647;
@@ -103,7 +103,7 @@ export class ViewManager extends Base {
103
103
  setViewMode(this.mainView, ViewVisionMode.Freedom);
104
104
  }
105
105
  if (!this.mainView.focusScenePath) {
106
- this.store.setMainViewFocusPath();
106
+ this.store.setMainViewFocusPath(this.mainView);
107
107
  }
108
108
  }
109
109
 
@@ -116,7 +116,7 @@ export class ViewManager extends Base {
116
116
  this.appTimer = setTimeout(() => {
117
117
  const appProxy = this.manager.appProxies.get(id);
118
118
  if (appProxy) {
119
- if (this.manager.boxManager.minimized) return;
119
+ if (this.manager.boxManager?.minimized) return;
120
120
  appProxy.setScenePath();
121
121
  appProxy.switchToWritable();
122
122
  appProxy.focusBox();