@netless/window-manager 0.4.26 → 0.4.28

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/dist/index.d.ts CHANGED
@@ -144,6 +144,7 @@ export declare class WindowManager extends InvisiblePlugin<WindowMangerAttribute
144
144
  nextPage(): Promise<boolean>;
145
145
  prevPage(): Promise<boolean>;
146
146
  addPage(params?: AddPageParams): Promise<void>;
147
+ removePage(index: number): Promise<boolean>;
147
148
  /**
148
149
  * 返回 mainView 的 ScenePath
149
150
  */
package/dist/index.es.js CHANGED
@@ -36,6 +36,7 @@ var Events = /* @__PURE__ */ ((Events2) => {
36
36
  Events2["WindowCreated"] = "WindowCreated";
37
37
  Events2["SetMainViewScenePath"] = "SetMainViewScenePath";
38
38
  Events2["SetMainViewSceneIndex"] = "SetMainViewSceneIndex";
39
+ Events2["SetAppFocusIndex"] = "SetAppFocusIndex";
39
40
  Events2["SwitchViewsToFreedom"] = "SwitchViewsToFreedom";
40
41
  Events2["MoveCamera"] = "MoveCamera";
41
42
  Events2["MoveCameraToContain"] = "MoveCameraToContain";
@@ -369,11 +370,11 @@ const getScenePath = (room, dir, index) => {
369
370
  }
370
371
  }
371
372
  };
372
- const removeScenes = (room, scenePath) => {
373
+ const removeScenes = (room, scenePath, index) => {
373
374
  if (room) {
374
375
  const type = room.scenePathType(scenePath);
375
376
  if (type !== ScenePathType.None) {
376
- room.removeScenes(scenePath);
377
+ room.removeScenes(scenePath, index);
377
378
  }
378
379
  }
379
380
  };
@@ -427,6 +428,15 @@ const getVersionNumber = (version) => {
427
428
  return parseInt(versionString);
428
429
  };
429
430
  const wait = (time2) => new Promise((resolve) => setTimeout(resolve, time2));
431
+ const isRootDirPage = (scenePath) => {
432
+ const delimiterCount = scenePath.split("").reduce((prev, cur) => {
433
+ if (cur === ROOT_DIR) {
434
+ prev += 1;
435
+ }
436
+ return prev;
437
+ }, 0);
438
+ return delimiterCount === 1;
439
+ };
430
440
  class AppListeners {
431
441
  constructor(manager) {
432
442
  this.manager = manager;
@@ -475,6 +485,10 @@ class AppListeners {
475
485
  this.initMainViewCameraHandler();
476
486
  break;
477
487
  }
488
+ case Events.SetAppFocusIndex: {
489
+ this.setAppFocusViewIndexHandler(data.payload);
490
+ break;
491
+ }
478
492
  }
479
493
  }
480
494
  };
@@ -516,6 +530,16 @@ class AppListeners {
516
530
  this.initMainViewCameraHandler = () => {
517
531
  this.manager.mainViewProxy.addCameraReaction();
518
532
  };
533
+ this.setAppFocusViewIndexHandler = (payload) => {
534
+ if (payload.type === "main") {
535
+ this.manager.setSceneIndexWithoutSync(payload.index);
536
+ } else if (payload.type === "app" && payload.appID) {
537
+ const app = this.manager.appProxies.get(payload.appID);
538
+ if (app) {
539
+ app.setSceneIndexWithoutSync(payload.index);
540
+ }
541
+ }
542
+ };
519
543
  }
520
544
  get boxManager() {
521
545
  return this.manager.boxManager;
@@ -533,6 +557,11 @@ class AppCreateError extends Error {
533
557
  this.message = "[WindowManager]: app duplicate exists and cannot be created again";
534
558
  }
535
559
  }
560
+ class AppNotRegisterError extends Error {
561
+ constructor(kind2) {
562
+ super(`[WindowManager]: app ${kind2} need register or provide src`);
563
+ }
564
+ }
536
565
  class AppManagerNotInitError extends Error {
537
566
  constructor() {
538
567
  super(...arguments);
@@ -568,6 +597,12 @@ class BoxManagerNotFoundError extends Error {
568
597
  this.message = "[WindowManager]: boxManager not found";
569
598
  }
570
599
  }
600
+ class BindContainerRoomPhaseInvalidError extends Error {
601
+ constructor() {
602
+ super(...arguments);
603
+ this.message = "[WindowManager]: room phase only Connected can be bindContainer";
604
+ }
605
+ }
571
606
  const onObjectByEvent = (event) => {
572
607
  return (object, func) => {
573
608
  if (object === void 0)
@@ -652,15 +687,21 @@ class Storage {
652
687
  this.id = id2 || null;
653
688
  this._state = {};
654
689
  const rawState = this._getRawState(this._state);
655
- if (this.id !== null && this._context.getIsWritable()) {
656
- if (rawState === this._state || !isObject(rawState)) {
657
- if (!get(this._context.getAttributes(), [STORAGE_NS])) {
658
- this._context.updateAttributes([STORAGE_NS], {});
690
+ if (this._context.getIsWritable()) {
691
+ if (this.id === null) {
692
+ if (context.isAddApp && defaultState) {
693
+ this.setState(defaultState);
694
+ }
695
+ } else {
696
+ if (rawState === this._state || !isObject(rawState)) {
697
+ if (!get(this._context.getAttributes(), [STORAGE_NS])) {
698
+ this._context.updateAttributes([STORAGE_NS], {});
699
+ }
700
+ this._context.updateAttributes([STORAGE_NS, this.id], this._state);
701
+ if (defaultState) {
702
+ this.setState(defaultState);
703
+ }
659
704
  }
660
- this._context.updateAttributes([STORAGE_NS, this.id], this._state);
661
- }
662
- if (defaultState) {
663
- this.setState(defaultState);
664
705
  }
665
706
  }
666
707
  plainObjectKeys(rawState).forEach((key) => {
@@ -885,6 +926,16 @@ class AppContext {
885
926
  this.getView = () => {
886
927
  return this.appProxy.view;
887
928
  };
929
+ this.mountView = (dom) => {
930
+ const view = this.getView();
931
+ if (view) {
932
+ view.divElement = dom;
933
+ setTimeout(() => {
934
+ var _a;
935
+ (_a = this.getRoom()) == null ? void 0 : _a.refreshViewSize();
936
+ }, 1e3);
937
+ }
938
+ };
888
939
  this.getInitScenePath = () => {
889
940
  return this.manager.getAppInitPath(this.appId);
890
941
  };
@@ -917,16 +968,6 @@ class AppContext {
917
968
  this.appProxy.setFullPath(scenePath);
918
969
  (_a = this.getRoom()) == null ? void 0 : _a.setScenePath(scenePath);
919
970
  };
920
- this.mountView = (dom) => {
921
- const view = this.getView();
922
- if (view) {
923
- view.divElement = dom;
924
- setTimeout(() => {
925
- var _a;
926
- (_a = this.getRoom()) == null ? void 0 : _a.refreshViewSize();
927
- }, 1e3);
928
- }
929
- };
930
971
  this.getAppOptions = () => {
931
972
  return typeof this.appOptions === "function" ? this.appOptions() : this.appOptions;
932
973
  };
@@ -979,6 +1020,14 @@ class AppContext {
979
1020
  putScenes(this.manager.room, scenePath, [scene || {}]);
980
1021
  }
981
1022
  };
1023
+ this.removePage = async (index) => {
1024
+ if (index < 0 || index >= this.pageState.length) {
1025
+ console.warn(`[WindowManager]: page index ${index} out of range`);
1026
+ return false;
1027
+ }
1028
+ this.appProxy.removeSceneByIndex(index);
1029
+ return true;
1030
+ };
982
1031
  this.emitter = appProxy.appEmitter;
983
1032
  this.isAddApp = appProxy.isAddApp;
984
1033
  }
@@ -1203,6 +1252,20 @@ const log = (...args) => {
1203
1252
  console.log(`[WindowManager]:`, ...args);
1204
1253
  }
1205
1254
  };
1255
+ const calculateNextIndex = (index, pageState) => {
1256
+ let nextIndex = 0;
1257
+ const maxIndex = pageState.length - 1;
1258
+ if (index === pageState.index) {
1259
+ if (index === maxIndex) {
1260
+ nextIndex = index - 1;
1261
+ } else {
1262
+ nextIndex = pageState.index + 1;
1263
+ }
1264
+ } else {
1265
+ nextIndex = pageState.index;
1266
+ }
1267
+ return nextIndex;
1268
+ };
1206
1269
  class AppProxy {
1207
1270
  constructor(params, manager, appId, isAddApp) {
1208
1271
  var _a;
@@ -1277,9 +1340,9 @@ class AppProxy {
1277
1340
  setViewFocusScenePath(this.view, fullPath);
1278
1341
  }
1279
1342
  }, 50);
1280
- this.notifyPageStateChange = () => {
1343
+ this.notifyPageStateChange = debounce(() => {
1281
1344
  this.appEmitter.emit("pageStateChange", this.pageState);
1282
- };
1345
+ }, 50);
1283
1346
  this.kind = params.kind;
1284
1347
  this.id = appId;
1285
1348
  this.stateKey = `${this.id}_state`;
@@ -1447,6 +1510,23 @@ class AppProxy {
1447
1510
  await appProxy.baseInsertApp(true);
1448
1511
  (_a = this.boxManager) == null ? void 0 : _a.updateBoxState(currentAppState);
1449
1512
  }
1513
+ async onRemoveScene(scenePath) {
1514
+ if (this.scenePath && scenePath.startsWith(this.scenePath + "/")) {
1515
+ let nextIndex = this.pageState.index;
1516
+ let fullPath = this._pageState.getFullPath(nextIndex);
1517
+ if (!fullPath) {
1518
+ nextIndex = 0;
1519
+ fullPath = this._pageState.getFullPath(nextIndex);
1520
+ }
1521
+ if (fullPath) {
1522
+ this.setFullPath(fullPath);
1523
+ }
1524
+ this.setViewFocusScenePath();
1525
+ if (this.view) {
1526
+ this.view.focusSceneIndex = nextIndex;
1527
+ }
1528
+ }
1529
+ }
1450
1530
  emitAppSceneStateChange(sceneState) {
1451
1531
  this.appEmitter.emit("sceneStateChange", sceneState);
1452
1532
  }
@@ -1520,6 +1600,32 @@ class AppProxy {
1520
1600
  get pageState() {
1521
1601
  return this._pageState.toObject();
1522
1602
  }
1603
+ async removeSceneByIndex(index) {
1604
+ const scenePath = this._pageState.getFullPath(index);
1605
+ if (scenePath) {
1606
+ if (this.pageState.length <= 1) {
1607
+ return false;
1608
+ }
1609
+ const nextIndex = calculateNextIndex(index, this.pageState);
1610
+ this.setSceneIndexWithoutSync(nextIndex);
1611
+ this.manager.dispatchInternalEvent(Events.SetAppFocusIndex, {
1612
+ type: "app",
1613
+ appID: this.id,
1614
+ index: nextIndex
1615
+ });
1616
+ setTimeout(() => {
1617
+ removeScenes(this.manager.room, scenePath, index);
1618
+ }, 100);
1619
+ return true;
1620
+ } else {
1621
+ return false;
1622
+ }
1623
+ }
1624
+ setSceneIndexWithoutSync(index) {
1625
+ if (this.view) {
1626
+ this.view.focusSceneIndex = index;
1627
+ }
1628
+ }
1523
1629
  setSceneIndex(index) {
1524
1630
  if (this.view) {
1525
1631
  this.view.focusSceneIndex = index;
@@ -1744,9 +1850,12 @@ class MainViewProxy {
1744
1850
  this.rebind();
1745
1851
  } else {
1746
1852
  const mainViewScenePath = this.store.getMainViewScenePath();
1747
- if (mainViewScenePath) {
1748
- setViewFocusScenePath(this.view, mainViewScenePath);
1749
- }
1853
+ this.setFocusScenePath(mainViewScenePath);
1854
+ }
1855
+ }
1856
+ setFocusScenePath(path) {
1857
+ if (path) {
1858
+ return setViewFocusScenePath(this.view, path);
1750
1859
  }
1751
1860
  }
1752
1861
  rebind() {
@@ -1907,19 +2016,29 @@ class AppManager {
1907
2016
  this.sideEffectManager = new SideEffectManager();
1908
2017
  this.sceneState = null;
1909
2018
  this.rootDirRemoving = false;
1910
- this.onRemoveScenes = async (scenePath) => {
1911
- var _a;
2019
+ this.onRemoveScenes = async (params) => {
2020
+ var _a, _b;
2021
+ const { scenePath } = params;
1912
2022
  if (scenePath === ROOT_DIR) {
1913
2023
  await this.onRootDirRemoved();
1914
2024
  this.dispatchInternalEvent(Events.RootDirRemoved);
1915
2025
  return;
1916
2026
  }
1917
- const mainViewScenePath = this.store.getMainViewScenePath();
1918
- if (this.room && mainViewScenePath) {
1919
- if (mainViewScenePath === scenePath) {
1920
- const nextPath = (_a = this.callbacksNode) == null ? void 0 : _a.scenes[this.store.getMainViewSceneIndex()];
1921
- this.setMainViewScenePath(`/${nextPath}` || INIT_DIR);
2027
+ if (isRootDirPage(scenePath)) {
2028
+ let nextIndex = this.mainView.focusSceneIndex || 0;
2029
+ let sceneName = (_a = this.callbacksNode) == null ? void 0 : _a.scenes[nextIndex];
2030
+ if (!sceneName) {
2031
+ nextIndex = 0;
2032
+ sceneName = (_b = this.callbacksNode) == null ? void 0 : _b.scenes[nextIndex];
1922
2033
  }
2034
+ if (sceneName) {
2035
+ this.setMainViewScenePath(`${ROOT_DIR}${sceneName}`);
2036
+ }
2037
+ await this.setMainViewSceneIndex(nextIndex);
2038
+ } else {
2039
+ this.appProxies.forEach((app) => {
2040
+ app.onRemoveScene(scenePath);
2041
+ });
1923
2042
  }
1924
2043
  };
1925
2044
  this.onReadonlyChanged = () => {
@@ -1954,6 +2073,26 @@ class AppManager {
1954
2073
  }
1955
2074
  }
1956
2075
  };
2076
+ this.removeSceneByIndex = async (index) => {
2077
+ const nextIndex = calculateNextIndex(index, this.windowManger.pageState);
2078
+ this.setSceneIndexWithoutSync(nextIndex);
2079
+ this.dispatchInternalEvent(Events.SetAppFocusIndex, { type: "main", index: nextIndex });
2080
+ setTimeout(() => {
2081
+ var _a;
2082
+ const scene = (_a = this.callbacksNode) == null ? void 0 : _a.scenes[index];
2083
+ if (scene) {
2084
+ removeScenes(this.room, `${ROOT_DIR}${scene}`, index);
2085
+ }
2086
+ }, 100);
2087
+ return true;
2088
+ };
2089
+ this.setSceneIndexWithoutSync = (index) => {
2090
+ var _a;
2091
+ const sceneName = (_a = this.callbacksNode) == null ? void 0 : _a.scenes[index];
2092
+ if (sceneName) {
2093
+ this.mainViewProxy.setFocusScenePath(`${ROOT_DIR}${sceneName}`);
2094
+ }
2095
+ };
1957
2096
  this.onSceneChange = (node) => {
1958
2097
  this.mainViewScenesLength = node.scenes.length;
1959
2098
  this.updateSceneState(node);
@@ -1965,7 +2104,10 @@ class AppManager {
1965
2104
  };
1966
2105
  this.updateSceneState = (node) => {
1967
2106
  const currentIndex = this.store.getMainViewSceneIndex() || 0;
1968
- const sceneName = node.scenes[currentIndex];
2107
+ let sceneName = node.scenes[currentIndex];
2108
+ if (!sceneName) {
2109
+ sceneName = node.scenes[this.mainView.focusSceneIndex || 0];
2110
+ }
1969
2111
  this.sceneState = {
1970
2112
  scenePath: `${ROOT_DIR}${sceneName}`,
1971
2113
  contextPath: node.path,
@@ -2349,10 +2491,11 @@ class AppManager {
2349
2491
  emitter.emit("mainViewMounted");
2350
2492
  }
2351
2493
  setMainViewFocusPath(scenePath) {
2494
+ var _a;
2352
2495
  const focusScenePath = scenePath || this.store.getMainViewScenePath();
2353
2496
  if (focusScenePath) {
2354
- const view = setViewFocusScenePath(this.mainView, focusScenePath);
2355
- return (view == null ? void 0 : view.focusScenePath) === focusScenePath;
2497
+ setViewFocusScenePath(this.mainView, focusScenePath);
2498
+ return ((_a = this.mainView) == null ? void 0 : _a.focusScenePath) === focusScenePath;
2356
2499
  }
2357
2500
  }
2358
2501
  resetScenePath(scenePath) {
@@ -2452,23 +2595,21 @@ class AppManager {
2452
2595
  }
2453
2596
  }
2454
2597
  async setMainViewSceneIndex(index) {
2598
+ var _a;
2455
2599
  if (this.room) {
2456
2600
  if (this.store.getMainViewSceneIndex() === index)
2457
2601
  return;
2458
- const mainViewScenePath = this.store.getMainViewScenePath();
2459
- if (mainViewScenePath) {
2460
- const sceneDir = parseSceneDir(mainViewScenePath);
2461
- const scenePath = makeValidScenePath(this.displayer, sceneDir, index);
2462
- if (scenePath) {
2463
- const success = this.setMainViewFocusPath(scenePath);
2464
- if (success) {
2465
- this.store.setMainViewScenePath(scenePath);
2466
- this.safeSetAttributes({ _mainSceneIndex: index });
2467
- this.dispatchSetMainViewScenePath(scenePath);
2468
- }
2469
- } else {
2470
- throw new Error(`[WindowManager]: ${sceneDir}: ${index} not valid index`);
2602
+ const sceneName = (_a = this.callbacksNode) == null ? void 0 : _a.scenes[index];
2603
+ const scenePath = `${ROOT_DIR}${sceneName}`;
2604
+ if (sceneName) {
2605
+ const success = this.setMainViewFocusPath(scenePath);
2606
+ if (success) {
2607
+ this.store.setMainViewScenePath(scenePath);
2608
+ this.safeSetAttributes({ _mainSceneIndex: index });
2609
+ this.dispatchSetMainViewScenePath(scenePath);
2471
2610
  }
2611
+ } else {
2612
+ throw new Error(`[WindowManager]: ${index} not valid index`);
2472
2613
  }
2473
2614
  }
2474
2615
  }
@@ -5579,8 +5720,9 @@ class PageStateImpl {
5579
5720
  return ((_a = this.manager) == null ? void 0 : _a.mainViewScenesLength) || 0;
5580
5721
  }
5581
5722
  toObject() {
5723
+ const index = this.index >= this.length ? this.length - 1 : this.index;
5582
5724
  return {
5583
- index: this.index,
5725
+ index,
5584
5726
  length: this.length
5585
5727
  };
5586
5728
  }
@@ -5716,13 +5858,13 @@ const replaceRoomFunction = (room, manager) => {
5716
5858
  };
5717
5859
  const delegateRemoveScenes = (room, manager) => {
5718
5860
  const originRemoveScenes = room.removeScenes;
5719
- room.removeScenes = (scenePath) => {
5861
+ room.removeScenes = (scenePath, index) => {
5720
5862
  var _a;
5721
5863
  if (scenePath === ROOT_DIR) {
5722
5864
  (_a = manager.appManager) == null ? void 0 : _a.updateRootDirRemoving(true);
5723
5865
  }
5724
5866
  const result = originRemoveScenes.call(room, scenePath);
5725
- emitter.emit("removeScenes", scenePath);
5867
+ emitter.emit("removeScenes", { scenePath, index });
5726
5868
  return result;
5727
5869
  };
5728
5870
  };
@@ -14905,7 +15047,7 @@ const reconnectRefresher = new ReconnectRefresher({ emitter });
14905
15047
  const _WindowManager = class extends InvisiblePlugin {
14906
15048
  constructor(context) {
14907
15049
  super(context);
14908
- this.version = "0.4.26";
15050
+ this.version = "0.4.28";
14909
15051
  this.dependencies = { "dependencies": { "@juggle/resize-observer": "^3.3.1", "@netless/telebox-insider": "0.2.26", "emittery": "^0.9.2", "lodash": "^4.17.21", "p-retry": "^4.6.1", "side-effect-manager": "^0.1.5", "uuid": "^7.0.3", "video.js": ">=7" }, "peerDependencies": { "white-web-sdk": "^2.16.0" }, "devDependencies": { "@netless/app-docs-viewer": "^0.2.9", "@netless/app-media-player": "0.1.0-beta.5", "@rollup/plugin-commonjs": "^20.0.0", "@rollup/plugin-node-resolve": "^13.0.4", "@rollup/plugin-url": "^6.1.0", "@sveltejs/vite-plugin-svelte": "^1.0.0-next.22", "@tsconfig/svelte": "^2.0.1", "@types/debug": "^4.1.7", "@types/lodash": "^4.14.182", "@types/lodash-es": "^4.17.4", "@types/uuid": "^8.3.1", "@typescript-eslint/eslint-plugin": "^4.30.0", "@typescript-eslint/parser": "^4.30.0", "cypress": "^8.7.0", "dotenv": "^10.0.0", "eslint": "^7.32.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-svelte3": "^3.2.0", "jsdom": "^19.0.0", "less": "^4.1.1", "prettier": "^2.3.2", "prettier-plugin-svelte": "^2.4.0", "rollup-plugin-analyzer": "^4.0.0", "rollup-plugin-styles": "^3.14.1", "svelte": "^3.42.4", "typescript": "^4.5.5", "vite": "^2.5.3", "vitest": "^0.12.4", "white-web-sdk": "2.16.10" } };
14910
15052
  this.emitter = callbacks$1;
14911
15053
  this.viewMode = ViewMode.Broadcaster;
@@ -15022,6 +15164,9 @@ const _WindowManager = class extends InvisiblePlugin {
15022
15164
  }
15023
15165
  bindContainer(container) {
15024
15166
  var _a, _b, _c, _d, _e2, _f;
15167
+ if (this.room.phase !== RoomPhase.Connected) {
15168
+ throw new BindContainerRoomPhaseInvalidError();
15169
+ }
15025
15170
  if (_WindowManager.isCreated && _WindowManager.container) {
15026
15171
  if (_WindowManager.container.firstChild) {
15027
15172
  container.appendChild(_WindowManager.container.firstChild);
@@ -15202,6 +15347,21 @@ const _WindowManager = class extends InvisiblePlugin {
15202
15347
  }
15203
15348
  }
15204
15349
  }
15350
+ async removePage(index) {
15351
+ if (this.appManager) {
15352
+ if (this.pageState.length === 1) {
15353
+ console.warn(`[WindowManager]: can not remove the last page`);
15354
+ return false;
15355
+ }
15356
+ if (index < 0 || index >= this.pageState.length) {
15357
+ console.warn(`[WindowManager]: index ${index} out of range`);
15358
+ return false;
15359
+ }
15360
+ return this.appManager.removeSceneByIndex(index);
15361
+ } else {
15362
+ return false;
15363
+ }
15364
+ }
15205
15365
  getMainViewScenePath() {
15206
15366
  var _a;
15207
15367
  return (_a = this.appManager) == null ? void 0 : _a.store.getMainViewScenePath();
@@ -15553,5 +15713,5 @@ WindowManager.debug = false;
15553
15713
  WindowManager.containerSizeRatio = DEFAULT_CONTAINER_RATIO;
15554
15714
  WindowManager.isCreated = false;
15555
15715
  setupBuiltin();
15556
- export { BuiltinApps, WindowManager, reconnectRefresher };
15716
+ export { AppCreateError, AppManagerNotInitError, AppNotRegisterError, BindContainerRoomPhaseInvalidError, BoxManagerNotFoundError, BoxNotCreatedError, BuiltinApps, InvalidScenePath, ParamsInvalidError, WhiteWebSDKInvalidError, WindowManager, calculateNextIndex, reconnectRefresher };
15557
15717
  //# sourceMappingURL=index.es.js.map