@netless/window-manager 0.4.13 → 0.4.16

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.
@@ -0,0 +1,111 @@
1
+ ## AppContext
2
+
3
+ - [api](#api)
4
+ - [events](#events)
5
+
6
+ <h2 id="api">API</h2>
7
+
8
+ ### appId
9
+
10
+ 插入 `app` 时生成的唯一 ID
11
+
12
+ ```ts
13
+ const appId = context.appId
14
+ ```
15
+
16
+ ### getDisplayer
17
+
18
+ 在默认情况下 `Displayer` 为白板的 `room` 实例
19
+
20
+ 回放时则为 `Player` 实例
21
+
22
+ ```ts
23
+ const displayer = context.getDisplayer()
24
+
25
+ assert(displayer, room) // 互动房间
26
+ assert(displayer, player) // 回放房间
27
+ ```
28
+
29
+ ### getScenes
30
+
31
+ `scenes` 在 `addApp` 时传入 `scenePath` 会由 `WindowManager` 创建
32
+
33
+ ```ts
34
+ const scenes = context.getScenes()
35
+ ```
36
+
37
+ ### getView
38
+
39
+ `View` 为白板中一块可标注部分
40
+
41
+ ```ts
42
+ const view = context.getView()
43
+ ```
44
+
45
+ ### getIsWritable
46
+
47
+ 获取当前状态是否可写
48
+
49
+ ```ts
50
+ // isWritable === (room.isWritable && box.readonly)
51
+ const isWritable = context.getIsWritable()
52
+ ```
53
+
54
+ ### getBox
55
+
56
+ 获取当前 app 的 box
57
+
58
+ ```ts
59
+ const box = context.getBox()
60
+
61
+ box.$content // box 的 main element
62
+ box.$footer
63
+ ```
64
+
65
+ ### setScenePath
66
+
67
+ 切换当前 `view` 的 `scenePath`
68
+
69
+ ```ts
70
+ context.setScenePath("/page/2")
71
+ ```
72
+
73
+ ### mountView
74
+
75
+ 挂载 view 到指定 dom
76
+
77
+ ```ts
78
+ context.mountView(ref)
79
+ ```
80
+
81
+ <h2 id="events">events</h2>
82
+
83
+ ### destroy
84
+
85
+ app 被关闭时发送的事件
86
+
87
+ ```ts
88
+ context.emitter.on("destroy", () => {
89
+ // release your listeners
90
+ })
91
+ ```
92
+
93
+ ### writableChange
94
+
95
+ 白板可写状态切换时触发
96
+
97
+ ```ts
98
+ context.emitter.on("writableChange", isWritable => {
99
+ //
100
+ })
101
+ ```
102
+
103
+ ### focus
104
+
105
+ 当前 app 获得焦点或者失去焦点时触发
106
+
107
+ ```ts
108
+ context.emitter.on("focus", focus => {
109
+ //
110
+ })
111
+ ```
@@ -1,5 +1,7 @@
1
1
  # 开发自定义 APP
2
2
 
3
+ - [AppContext](./app-context.md)
4
+
3
5
  ## official apps https://github.com/netless-io/netless-app
4
6
 
5
7
  <br>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netless/window-manager",
3
- "version": "0.4.13",
3
+ "version": "0.4.16",
4
4
  "description": "",
5
5
  "main": "dist/index.es.js",
6
6
  "module": "dist/index.es.js",
@@ -55,6 +55,10 @@ export class AppListeners {
55
55
  this.cursorMoveHandler(data.payload);
56
56
  break;
57
57
  }
58
+ case Events.RootDirRemoved: {
59
+ this.rootDirRemovedHandler();
60
+ break;
61
+ }
58
62
  default:
59
63
  break;
60
64
  }
@@ -93,4 +97,10 @@ export class AppListeners {
93
97
  private cursorMoveHandler = (payload: any) => {
94
98
  emitter.emit("cursorMove", payload);
95
99
  };
100
+
101
+ private rootDirRemovedHandler = () => {
102
+ this.manager.createRootDirScenesCallback();
103
+ this.manager.mainViewProxy.rebind();
104
+ emitter.emit("rootDirRemoved");
105
+ }
96
106
  }
package/src/AppManager.ts CHANGED
@@ -59,6 +59,8 @@ export class AppManager {
59
59
 
60
60
  public sceneState: SceneState | null = null;
61
61
 
62
+ public rootDirRemoving = false;
63
+
62
64
  constructor(public windowManger: WindowManager) {
63
65
  this.displayer = windowManger.displayer;
64
66
  this.store.setContext({
@@ -106,13 +108,11 @@ export class AppManager {
106
108
  });
107
109
  }
108
110
 
109
- private onRemoveScenes = (scenePath: string) => {
111
+ private onRemoveScenes = async (scenePath: string) => {
110
112
  // 如果移除根目录就把 scenePath 设置为初始值
111
113
  if (scenePath === ROOT_DIR) {
112
- this.setMainViewScenePath(INIT_DIR);
113
- this.createRootDirScenesCallback();
114
- this.onRootDirRemoved();
115
- emitter.emit("rootDirRemoved");
114
+ await this.onRootDirRemoved();
115
+ this.dispatchInternalEvent(Events.RootDirRemoved);
116
116
  return;
117
117
  }
118
118
  // 如果移除的 path 跟 MainViewScenePath 相同就取当前目录的当前 index
@@ -129,14 +129,22 @@ export class AppManager {
129
129
  * 根目录被删除时所有的 scene 都会被删除.
130
130
  * 所以需要关掉所有开启了 view 的 app
131
131
  */
132
- private onRootDirRemoved() {
133
- this.appProxies.forEach(appProxy => {
132
+ public async onRootDirRemoved(needClose = true) {
133
+ this.rootDirRemoving = true;
134
+ this.setMainViewScenePath(INIT_DIR);
135
+
136
+ this.createRootDirScenesCallback();
137
+
138
+ for (const [id, appProxy] of this.appProxies.entries()) {
134
139
  if (appProxy.view) {
135
- this.closeApp(appProxy.id);
140
+ await this.closeApp(id, needClose);
136
141
  }
137
- });
142
+ }
138
143
  // 删除了根目录的 scenes 之后 mainview 需要重新绑定, 否则主白板会不能渲染
139
144
  this.mainViewProxy.rebind();
145
+
146
+ emitter.emit("rootDirRemoved");
147
+ this.rootDirRemoving = false;
140
148
  }
141
149
 
142
150
  private onReadonlyChanged = () => {
@@ -156,7 +164,7 @@ export class AppManager {
156
164
  });
157
165
  };
158
166
 
159
- private createRootDirScenesCallback = () => {
167
+ public createRootDirScenesCallback = () => {
160
168
  let isRecreate = false;
161
169
  if (this.callbacksNode) {
162
170
  this.callbacksNode.dispose();
@@ -274,34 +282,13 @@ export class AppManager {
274
282
  this.refresher?.add("mainViewIndex", () => {
275
283
  return autorun(() => {
276
284
  const mainSceneIndex = get(this.attributes, "_mainSceneIndex");
277
- if (mainSceneIndex !== undefined && this._prevSceneIndex !== mainSceneIndex) {
278
- callbacks.emit("mainViewSceneIndexChange", mainSceneIndex);
279
- emitter.emit("changePageState");
280
- if (this.callbacksNode) {
281
- this.updateSceneState(this.callbacksNode);
282
- }
283
- this._prevSceneIndex = mainSceneIndex;
284
- }
285
+ this.onMainViewIndexChange(mainSceneIndex);
285
286
  });
286
287
  });
287
288
  this.refresher?.add("focusedChange", () => {
288
289
  return autorun(() => {
289
290
  const focused = get(this.attributes, "focus");
290
- if (this._prevFocused !== focused) {
291
- callbacks.emit("focusedChange", focused);
292
- emitter.emit("focusedChange", { focused, prev: this._prevFocused });
293
- this._prevFocused = focused;
294
- if (focused !== undefined) {
295
- this.boxManager?.focusBox({ appId: focused });
296
- // 确保 focus 修改的时候, appProxy 已经创建
297
- setTimeout(() => {
298
- const appProxy = this.appProxies.get(focused);
299
- if (appProxy) {
300
- appRegister.notifyApp(appProxy.kind, "focus", { appId: focused });
301
- }
302
- }, 0);
303
- }
304
- }
291
+ this.onFocusChange(focused);
305
292
  });
306
293
  });
307
294
  this.refresher?.add("registeredChange", () => {
@@ -329,6 +316,35 @@ export class AppManager {
329
316
  });
330
317
  }
331
318
 
319
+ private onMainViewIndexChange = (index: number) => {
320
+ if (index !== undefined && this._prevSceneIndex !== index) {
321
+ callbacks.emit("mainViewSceneIndexChange", index);
322
+ emitter.emit("changePageState");
323
+ if (this.callbacksNode) {
324
+ this.updateSceneState(this.callbacksNode);
325
+ }
326
+ this._prevSceneIndex = index;
327
+ }
328
+ }
329
+
330
+ private onFocusChange = (focused: string | undefined) => {
331
+ if (this._prevFocused !== focused) {
332
+ callbacks.emit("focusedChange", focused);
333
+ emitter.emit("focusedChange", { focused, prev: this._prevFocused });
334
+ this._prevFocused = focused;
335
+ if (focused !== undefined) {
336
+ this.boxManager?.focusBox({ appId: focused });
337
+ // 确保 focus 修改的时候, appProxy 已经创建
338
+ setTimeout(() => {
339
+ const appProxy = this.appProxies.get(focused);
340
+ if (appProxy) {
341
+ appRegister.notifyApp(appProxy.kind, "focus", { appId: focused });
342
+ }
343
+ }, 0);
344
+ }
345
+ }
346
+ }
347
+
332
348
  /**
333
349
  * 插件更新 attributes 时的回调
334
350
  *
@@ -350,14 +366,13 @@ export class AppManager {
350
366
  for (const { id } of orderBy(appsWithCreatedAt, "createdAt", "asc")) {
351
367
  if (!this.appProxies.has(id) && !this.appStatus.has(id)) {
352
368
  const app = apps[id];
353
-
354
- this.appStatus.set(id, AppStatus.StartCreate);
355
369
  try {
356
370
  const appAttributes = this.attributes[id];
357
371
  if (!appAttributes) {
358
372
  throw new Error("appAttributes is undefined");
359
373
  }
360
374
  this.appCreateQueue.push(() => {
375
+ this.appStatus.set(id, AppStatus.StartCreate);
361
376
  return this.baseInsertApp(
362
377
  {
363
378
  kind: app.kind,
@@ -494,10 +509,10 @@ export class AppManager {
494
509
  }
495
510
  }
496
511
 
497
- public async closeApp(appId: string) {
512
+ public async closeApp(appId: string, needClose = true) {
498
513
  const appProxy = this.appProxies.get(appId);
499
514
  if (appProxy) {
500
- appProxy.destroy(true, true, false);
515
+ appProxy.destroy(true, needClose, false);
501
516
  }
502
517
  }
503
518
 
@@ -717,7 +732,7 @@ export class AppManager {
717
732
  });
718
733
  }
719
734
 
720
- public dispatchInternalEvent(event: Events, payload: any) {
735
+ public dispatchInternalEvent(event: Events, payload?: any) {
721
736
  this.safeDispatchMagixEvent(MagixEventName, {
722
737
  eventName: event,
723
738
  payload: payload,
package/src/AppProxy.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import Emittery from "emittery";
2
- import { AppAttributes, AppEvents, Events } from "./constants";
2
+ import { AppAttributes, AppEvents, Events, SETUP_APP_DELAY } from "./constants";
3
3
  import { AppContext } from "./AppContext";
4
4
  import { appRegister } from "./Register";
5
5
  import { autorun } from "white-web-sdk";
@@ -168,7 +168,7 @@ export class AppProxy {
168
168
  appRegister.notifyApp(this.kind, "created", { appId, result });
169
169
  this.afterSetupApp(boxInitState);
170
170
  this.fixMobileSize();
171
- }, 50);
171
+ }, SETUP_APP_DELAY);
172
172
  });
173
173
  this.boxManager?.createBox({
174
174
  appId: appId,
@@ -346,6 +346,7 @@ export class AppProxy {
346
346
 
347
347
  public setViewFocusScenePath() {
348
348
  const fullPath = this.getFullScenePath();
349
+ console.log("setViewFocusScenePath", this.kind, this.id, fullPath)
349
350
  if (fullPath && this.view) {
350
351
  setViewFocusScenePath(this.view, fullPath);
351
352
  }
@@ -380,6 +381,7 @@ export class AppProxy {
380
381
  if (cleanAttrs) {
381
382
  this.store.cleanAppAttributes(this.id);
382
383
  if (this.scenePath) {
384
+ console.log("put closeApp", this.scenePath);
383
385
  removeScenes(this.manager.room, this.scenePath);
384
386
  }
385
387
  }
@@ -0,0 +1,47 @@
1
+ import type { ScenesCallbacks, ScenesCallbacksNode } from "white-web-sdk";
2
+ import type { AppManager } from "../AppManager";
3
+
4
+ export class ScenesCallbackManager {
5
+
6
+ private nodes: Map<string, ScenesCallbacksNode> = new Map();
7
+
8
+ constructor(private manager: AppManager) {}
9
+
10
+ public createNode(path: string, callbacks?: Partial<ScenesCallbacks>): ScenesCallbacksNode | null {
11
+ const node = this.manager.displayer.createScenesCallback(path, callbacks);
12
+ if (node) {
13
+ this.nodes.set(path, node);
14
+ }
15
+ return node;
16
+ }
17
+
18
+ public getScenes(path: string) {
19
+ const node = this.nodes.get(path);
20
+ return node?.scenes;
21
+ }
22
+
23
+ public getScenesOnce(path: string) {
24
+ let node = this.nodes.get(path);
25
+ if (!node) {
26
+ const created = this.createNode(path);
27
+ if (created) {
28
+ node = created;
29
+ }
30
+ }
31
+ const scenes = node?.scenes;
32
+ this.removeNode(path);
33
+ return scenes;
34
+ }
35
+
36
+ public removeNode(path: string) {
37
+ const node = this.nodes.get(path);
38
+ if (node) {
39
+ node.dispose();
40
+ this.nodes.delete(path);
41
+ }
42
+ }
43
+
44
+ public destroy(): void {
45
+ this.nodes.forEach(node => node.dispose());
46
+ }
47
+ }
@@ -1,5 +1,6 @@
1
1
  import { callbacks } from "../callback";
2
2
  import type { AppProxy } from "../AppProxy";
3
+ import { SETUP_APP_DELAY } from "../constants";
3
4
 
4
5
  export type Invoker = () => Promise<AppProxy | undefined>;
5
6
 
@@ -60,11 +61,18 @@ export class AppCreateQueue {
60
61
 
61
62
  public emitReady() {
62
63
  if (!this.isEmit) {
63
- callbacks.emit("ready");
64
+ setTimeout(() => {
65
+ callbacks.emit("ready");
66
+ }, SETUP_APP_DELAY);
64
67
  }
65
68
  this.isEmit = true;
66
69
  }
67
70
 
71
+ public empty() {
72
+ this.list = [];
73
+ this.clear();
74
+ }
75
+
68
76
  public destroy() {
69
77
  if (this.timer) {
70
78
  this.clear();
@@ -1,12 +1,12 @@
1
1
  import { appRegister } from "../Register";
2
2
  import { debounce } from "lodash";
3
3
  import { emitter } from "../InternalEmitter";
4
+ import { ROOT_DIR } from "../constants";
4
5
  import { ScenePathType } from "white-web-sdk";
5
6
  import { v4 } from "uuid";
6
7
  import type { PublicEvent } from "../callback";
7
- import type { Displayer, ViewVisionMode, Room, View } from "white-web-sdk";
8
+ import type { Displayer, ViewVisionMode, Room, View , SceneDefinition} from "white-web-sdk";
8
9
  import type Emittery from "emittery";
9
- import { ROOT_DIR } from "../constants";
10
10
 
11
11
  export const genAppId = async (kind: string) => {
12
12
  const impl = await appRegister.appClasses.get(kind)?.();
@@ -104,6 +104,9 @@ export const entireScenes = (displayer: Displayer) => {
104
104
  return displayer.entireScenes();
105
105
  };
106
106
 
107
+ export const putScenes = (room: Room | undefined, path: string, scenes: SceneDefinition[]) => {
108
+ return room?.putScenes(path, scenes);
109
+ }
107
110
 
108
111
  export const isValidScenePath = (scenePath: string) => {
109
112
  return scenePath.startsWith("/");
@@ -22,7 +22,6 @@ export class MainViewProxy {
22
22
  this.mainView = this.createMainView();
23
23
  this.moveCameraSizeByAttributes();
24
24
  emitter.once("mainViewMounted").then(() => {
25
- this.addMainViewListener();
26
25
  setTimeout(() => {
27
26
  this.start();
28
27
  if (!this.mainViewCamera || !this.mainViewSize) {
@@ -55,6 +54,7 @@ export class MainViewProxy {
55
54
  public start() {
56
55
  if (this.started) return;
57
56
  this.sizeChangeHandler(this.mainViewSize);
57
+ this.addMainViewListener();
58
58
  this.addCameraListener();
59
59
  this.manager.refresher?.add(Fields.MainViewCamera, this.cameraReaction);
60
60
  this.started = true;
@@ -143,6 +143,7 @@ export class MainViewProxy {
143
143
  this.view.divElement.removeEventListener("click", this.mainViewClickListener);
144
144
  this.view.divElement.removeEventListener("touchend", this.mainViewClickListener);
145
145
  }
146
+ this.mainViewIsAddListener = false;
146
147
  }
147
148
 
148
149
  private mainViewClickListener = () => {
package/src/constants.ts CHANGED
@@ -13,6 +13,7 @@ export enum Events {
13
13
  MoveCamera = "MoveCamera",
14
14
  MoveCameraToContain = "MoveCameraToContain",
15
15
  CursorMove = "CursorMove",
16
+ RootDirRemoved = "RootDirRemoved",
16
17
  }
17
18
 
18
19
  export const MagixEventName = "__WindowManger";
@@ -50,3 +51,5 @@ export const DEFAULT_CONTAINER_RATIO = 9 / 16;
50
51
 
51
52
  export const ROOT_DIR = "/";
52
53
  export const INIT_DIR = "/init";
54
+
55
+ export const SETUP_APP_DELAY = 50;
package/src/index.ts CHANGED
@@ -25,6 +25,7 @@ import {
25
25
  ensureValidScenePath,
26
26
  entireScenes,
27
27
  isValidScenePath,
28
+ putScenes,
28
29
  wait,
29
30
  } from "./Utils/Common";
30
31
  import type { TELE_BOX_STATE, BoxManager } from "./BoxManager";
@@ -387,6 +388,28 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
387
388
  * 创建一个 app 至白板
388
389
  */
389
390
  public async addApp<T = any>(params: AddAppParams<T>): Promise<string | undefined> {
391
+ if (this.appManager) {
392
+ // 移除根目录时需要做一些异步的释放操作 addApp 需要等待释放完成才可以继续添加
393
+ if (this.appManager.rootDirRemoving) {
394
+ return new Promise((resolve, reject) => {
395
+ emitter.once("rootDirRemoved").then(async () => {
396
+ try {
397
+ const appId = await this._addApp(params);
398
+ resolve(appId);
399
+ } catch (error) {
400
+ reject(error.message);
401
+ }
402
+ });
403
+ });
404
+ } else {
405
+ return this._addApp(params);
406
+ }
407
+ } else {
408
+ throw new AppManagerNotInitError();
409
+ }
410
+ }
411
+
412
+ private async _addApp<T = any>(params: AddAppParams<T>): Promise<string | undefined> {
390
413
  if (this.appManager) {
391
414
  if (!params.kind || typeof params.kind !== "string") {
392
415
  throw new ParamsInvalidError();
@@ -438,16 +461,16 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
438
461
  if (this.isDynamicPPT(scenes)) {
439
462
  isDynamicPPT = true;
440
463
  if (!entireScenes(this.displayer)[scenePath]) {
441
- this.room?.putScenes(scenePath, scenes);
464
+ putScenes(this.room, scenePath, scenes);
442
465
  }
443
466
  } else {
444
467
  if (!entireScenes(this.displayer)[scenePath]) {
445
- this.room?.putScenes(scenePath, [{ name: scenes[0].name }]);
468
+ putScenes(this.room, scenePath, [{ name: scenes[0].name }]);
446
469
  }
447
470
  }
448
471
  }
449
472
  if (scenePath && scenes === undefined) {
450
- this.room?.putScenes(scenePath, [{}]);
473
+ putScenes(this.room, scenePath, [{}]);
451
474
  }
452
475
  }
453
476
  return isDynamicPPT;