@netless/window-manager 0.4.0-canary.0 → 0.4.0-canary.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.
package/src/index.ts CHANGED
@@ -6,7 +6,7 @@ import { AppManager } from "./AppManager";
6
6
  import { appRegister } from "./Register";
7
7
  import { createBoxManager } from "./BoxManager";
8
8
  import { CursorManager } from "./Cursor";
9
- import { DEFAULT_CONTAINER_RATIO, REQUIRE_VERSION } from "./constants";
9
+ import { DEFAULT_CONTAINER_RATIO, Events, REQUIRE_VERSION } from "./constants";
10
10
  import { Fields } from "./AttributesDelegate";
11
11
  import { initDb } from "./Register/storage";
12
12
  import { isNull, isObject } from "lodash";
@@ -58,6 +58,7 @@ import type { AppListeners } from "./AppListener";
58
58
  import type { NetlessApp, RegisterParams } from "./typings";
59
59
  import type { TeleBoxColorScheme, TeleBoxState } from "@netless/telebox-insider";
60
60
  import type { AppProxy } from "./AppProxy";
61
+ import { ReconnectRefresher } from "./ReconnectRefresher";
61
62
 
62
63
  const ResizeObserver = window.ResizeObserver || ResizeObserverPolyfill;
63
64
 
@@ -81,14 +82,14 @@ export type AddAppOptions = {
81
82
 
82
83
  export type setAppOptions = AddAppOptions & { appOptions?: any };
83
84
 
84
- export type AddAppParams = {
85
+ export type AddAppParams<TAttributes = any> = {
85
86
  kind: string;
86
87
  // app 地址(本地 app 不需要传)
87
88
  src?: string;
88
89
  // 窗口配置
89
90
  options?: AddAppOptions;
90
91
  // 初始化 attributes
91
- attributes?: any;
92
+ attributes?: TAttributes;
92
93
  };
93
94
 
94
95
  export type BaseInsertParams = {
@@ -139,6 +140,7 @@ export type EmitterEvent = {
139
140
  observerIdChange: number;
140
141
  boxStateChange: string;
141
142
  playgroundSizeChange: DOMRect;
143
+ onReconnected: void;
142
144
  };
143
145
 
144
146
  export type EmitterType = Emittery<EmitterEvent>;
@@ -171,6 +173,8 @@ export type MountParams = {
171
173
  export type CallbacksType = Emittery<PublicEvent>;
172
174
  export const callbacks: CallbacksType = new Emittery();
173
175
 
176
+ export const reconnectRefresher = new ReconnectRefresher({ emitter });
177
+
174
178
  export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
175
179
  public static kind = "WindowManager";
176
180
  public static displayer: Displayer;
@@ -181,7 +185,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
181
185
  public static containerSizeRatio = DEFAULT_CONTAINER_RATIO;
182
186
  private static isCreated = false;
183
187
 
184
- public version = "0.4.0-canary.0";
188
+ public version = "0.4.0-canary.4";
185
189
 
186
190
  public appListeners?: AppListeners;
187
191
 
@@ -254,7 +258,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
254
258
  }
255
259
 
256
260
  if (params.container) {
257
- manager.bindContainer(params.container, params.collectorContainer);
261
+ manager.bindContainer(params.container);
258
262
  }
259
263
 
260
264
  replaceRoomFunction(room, manager);
@@ -317,32 +321,50 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
317
321
  playground.appendChild(style);
318
322
  }
319
323
  manager.observePlaygroundSize(playground, sizer, wrapper);
324
+ WindowManager.wrapper = wrapper;
320
325
  return mainViewElement;
321
326
  }
322
327
 
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);
328
+ public bindContainer(container: HTMLElement) {
329
+ if (WindowManager.isCreated && WindowManager.container) {
330
+ if (WindowManager.container.firstChild) {
331
+ container.appendChild(WindowManager.container.firstChild);
332
+ }
333
+ } else {
334
+ if (WindowManager.params) {
335
+ const params = WindowManager.params;
336
+ const mainViewElement = WindowManager.initContainer(
337
+ this,
338
+ container,
339
+ params.chessboard,
340
+ params.overwriteStyles
341
+ );
342
+ const boxManager = createBoxManager(this, callbacks, emitter, {
343
+ collectorContainer: params.collectorContainer,
344
+ collectorStyles: params.collectorStyles,
345
+ prefersColorScheme: params.prefersColorScheme,
346
+ });
347
+ this.boxManager = boxManager;
348
+ this.appManager?.setBoxManager(boxManager);
349
+ this.bindMainView(mainViewElement, params.disableCameraTransform);
350
+ if (WindowManager.wrapper) {
351
+ this.cursorManager?.setupWrapper(WindowManager.wrapper);
352
+ }
353
+ }
354
+ }
355
+ this.boxManager?.updateManagerRect();
356
+ this.appManager?.refresh();
357
+ this.appManager?.resetMaximized();
358
+ this.appManager?.resetMinimized();
359
+ WindowManager.container = container;
360
+ }
361
+
362
+ public bindCollectorContainer(container: HTMLElement) {
363
+ if (WindowManager.isCreated && this.boxManager) {
364
+ this.boxManager.setCollectorContainer(container);
365
+ } else {
366
+ if (WindowManager.params) {
367
+ WindowManager.params.collectorContainer = container;
346
368
  }
347
369
  }
348
370
  }
@@ -356,30 +378,10 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
356
378
  return appRegister.register(params);
357
379
  }
358
380
 
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
-
379
381
  /**
380
382
  * 创建一个 app 至白板
381
383
  */
382
- public async addApp(params: AddAppParams): Promise<string | undefined> {
384
+ public async addApp<T = any>(params: AddAppParams<T>): Promise<string | undefined> {
383
385
  if (this.appManager) {
384
386
  if (!params.kind || typeof params.kind !== "string") {
385
387
  throw new ParamsInvalidError();
@@ -593,6 +595,10 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
593
595
  }>
594
596
  ): void {
595
597
  this.mainView.moveCameraToContain(rectangle);
598
+ this.appManager?.dispatchInternalEvent(Events.MoveCameraToContain, rectangle);
599
+ setTimeout(() => {
600
+ this.appManager?.mainViewProxy.setCameraAndSize();
601
+ }, 1000);
596
602
  }
597
603
 
598
604
  public convertToPointInWorld(point: Point): Point {