@netless/window-manager 0.4.22 → 0.4.25

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/AppManager.ts CHANGED
@@ -585,6 +585,7 @@ export class AppManager {
585
585
  } else {
586
586
  this.mainView.disableCameraTransform = true;
587
587
  }
588
+ emitter.emit("writableChange", isWritable);
588
589
  };
589
590
 
590
591
  public safeSetAttributes(attributes: any) {
@@ -38,6 +38,11 @@ export type StoreContext = {
38
38
  safeUpdateAttributes: (keys: string[], value: any) => void;
39
39
  safeSetAttributes: (attributes: any) => void;
40
40
  }
41
+
42
+ export type ICamera = Camera & { id: string };
43
+
44
+ export type ISize = Size & { id: string };
45
+
41
46
  export class AttributesDelegate {
42
47
 
43
48
  constructor(private context: StoreContext) {}
@@ -152,14 +157,21 @@ export class AttributesDelegate {
152
157
  return get(this.attributes, [Fields.MainViewSize]);
153
158
  }
154
159
 
155
- public setMainViewCamera(camera: (Camera & { id: string }) | undefined) {
160
+ public setMainViewCamera(camera: ICamera) {
156
161
  this.context.safeSetAttributes({ [Fields.MainViewCamera]: { ...camera } });
157
162
  }
158
163
 
159
- public setMainViewSize(size: (Size & { id: string }) | undefined) {
164
+ public setMainViewSize(size: ISize) {
160
165
  this.context.safeSetAttributes({ [Fields.MainViewSize]: { ...size } });
161
166
  }
162
167
 
168
+ public setMainViewCameraAndSize(camera: ICamera, size: ISize) {
169
+ this.context.safeSetAttributes({
170
+ [Fields.MainViewCamera]: { ...camera },
171
+ [Fields.MainViewSize]: { ...size },
172
+ });
173
+ }
174
+
163
175
  public setAppFocus = (appId: string, focus: boolean) => {
164
176
  if (focus) {
165
177
  this.context.safeSetAttributes({ [Fields.Focus]: appId });
@@ -1,11 +1,14 @@
1
1
  import { ResizeObserver as ResizeObserverPolyfill } from "@juggle/resize-observer";
2
+ import { isFunction } from "lodash";
2
3
  import { WindowManager } from "./index";
3
4
  import type { EmitterType } from "./InternalEmitter";
5
+ import type { UnsubscribeFn } from "emittery";
4
6
 
5
7
  const ResizeObserver = window.ResizeObserver || ResizeObserverPolyfill;
6
8
 
7
9
  export class ContainerResizeObserver {
8
10
  private containerResizeObserver?: ResizeObserver;
11
+ private disposer?: UnsubscribeFn;
9
12
 
10
13
  constructor(private emitter: EmitterType) {}
11
14
 
@@ -35,10 +38,14 @@ export class ContainerResizeObserver {
35
38
  }
36
39
  });
37
40
 
41
+ this.disposer = this.emitter.on("containerSizeRatioUpdate", () => {
42
+ this.updateSizer(container.getBoundingClientRect(), sizer, wrapper);
43
+ });
44
+
38
45
  this.containerResizeObserver.observe(container);
39
46
  }
40
47
 
41
- private updateSizer(
48
+ public updateSizer(
42
49
  { width, height }: DOMRectReadOnly,
43
50
  sizer: HTMLElement,
44
51
  wrapper: HTMLDivElement
@@ -58,5 +65,9 @@ export class ContainerResizeObserver {
58
65
 
59
66
  public disconnect() {
60
67
  this.containerResizeObserver?.disconnect();
68
+ if (isFunction(this.disposer)) {
69
+ this.disposer();
70
+ this.disposer = undefined;
71
+ }
61
72
  }
62
73
  }
@@ -24,6 +24,8 @@ export type EmitterEvent = {
24
24
  rootDirRemoved: undefined;
25
25
  setReadonly: boolean;
26
26
  changePageState: undefined;
27
+ writableChange: boolean;
28
+ containerSizeRatioUpdate: number;
27
29
  };
28
30
 
29
31
  export type EmitterType = Emittery<EmitterEvent>;
@@ -25,20 +25,37 @@ export class MainViewProxy {
25
25
  emitter.once("mainViewMounted").then(() => {
26
26
  this.addMainViewListener();
27
27
  this.start();
28
- if (!this.mainViewCamera || !this.mainViewSize) {
29
- manager.dispatchInternalEvent(Events.InitMainViewCamera)
30
- this.setCameraAndSize();
31
- }
28
+ this.ensureCameraAndSize();
29
+ this.startListenWritableChange();
32
30
  });
33
31
  const playgroundSizeChangeListener = () => {
34
32
  this.sizeChangeHandler(this.mainViewSize);
35
33
  };
36
34
  this.sideEffectManager.add(() => {
37
- emitter.on("playgroundSizeChange", playgroundSizeChangeListener);
38
- return () => emitter.off("playgroundSizeChange", playgroundSizeChangeListener);
35
+ return emitter.on("playgroundSizeChange", playgroundSizeChangeListener);
36
+ });
37
+ this.sideEffectManager.add(() => {
38
+ return emitter.on("containerSizeRatioUpdate", this.onUpdateContainerSizeRatio);
39
+ });
40
+ }
41
+
42
+ private startListenWritableChange = () => {
43
+ this.sideEffectManager.add(() => {
44
+ return emitter.on("writableChange", isWritable => {
45
+ if (isWritable) {
46
+ this.ensureCameraAndSize();
47
+ }
48
+ });
39
49
  });
40
50
  }
41
51
 
52
+ public ensureCameraAndSize() {
53
+ if (!this.mainViewCamera || !this.mainViewSize) {
54
+ this.manager.dispatchInternalEvent(Events.InitMainViewCamera);
55
+ this.setCameraAndSize();
56
+ }
57
+ }
58
+
42
59
  private get mainViewCamera() {
43
60
  return this.store.getMainViewCamera();
44
61
  }
@@ -62,11 +79,12 @@ export class MainViewProxy {
62
79
 
63
80
  public addCameraReaction = () => {
64
81
  this.manager.refresher?.add(Fields.MainViewCamera, this.cameraReaction);
65
- }
82
+ };
66
83
 
67
84
  public setCameraAndSize(): void {
68
- this.store.setMainViewCamera({ ...this.mainView.camera, id: this.manager.uid });
69
- this.store.setMainViewSize({ ...this.mainView.size, id: this.manager.uid });
85
+ const camera = { ...this.mainView.camera, id: this.manager.uid };
86
+ const size = { ...this.mainView.size, id: this.manager.uid };
87
+ this.store.setMainViewCameraAndSize(camera, size);
70
88
  }
71
89
 
72
90
  private cameraReaction = () => {
@@ -77,17 +95,26 @@ export class MainViewProxy {
77
95
  this.moveCameraToContian(this.mainViewSize);
78
96
  this.moveCamera(camera);
79
97
  }
80
- }, { fireImmediately: true }
98
+ },
99
+ { fireImmediately: true }
81
100
  );
82
101
  };
83
102
 
84
- private sizeChangeHandler = debounce((size: Size) => {
103
+ public sizeChangeHandler = debounce((size: Size) => {
85
104
  if (size) {
86
105
  this.moveCameraToContian(size);
87
106
  this.moveCamera(this.mainViewCamera);
88
107
  }
89
108
  }, 30);
90
109
 
110
+ public onUpdateContainerSizeRatio = () => {
111
+ const size = this.store.getMainViewSize();
112
+ this.sizeChangeHandler(size);
113
+ if (size.id === this.manager.uid) {
114
+ this.setCameraAndSize();
115
+ }
116
+ }
117
+
91
118
  public get view(): View {
92
119
  return this.mainView;
93
120
  }
package/src/index.ts CHANGED
@@ -11,7 +11,7 @@ import { emitter } from "./InternalEmitter";
11
11
  import { Fields } from "./AttributesDelegate";
12
12
  import { initDb } from "./Register/storage";
13
13
  import { InvisiblePlugin, isPlayer, isRoom, RoomPhase, ViewMode } from "white-web-sdk";
14
- import { isEqual, isNull, isObject, omit } from "lodash";
14
+ import { isEqual, isNull, isObject, omit, isNumber } from "lodash";
15
15
  import { log } from "./Utils/log";
16
16
  import { PageStateImpl } from "./PageState";
17
17
  import { ReconnectRefresher } from "./ReconnectRefresher";
@@ -893,6 +893,15 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> imple
893
893
  this.appManager?.refresher?.refresh();
894
894
  }
895
895
 
896
+ public setContainerSizeRatio(ratio: number) {
897
+ if (!isNumber(ratio)) {
898
+ throw new Error(`[WindowManager]: updateContainerSizeRatio error, ratio must be a number. but got ${ratio}`);
899
+ }
900
+ WindowManager.containerSizeRatio = ratio;
901
+ this.containerSizeRatio = ratio;
902
+ emitter.emit("containerSizeRatioUpdate", ratio);
903
+ }
904
+
896
905
  private isDynamicPPT(scenes: SceneDefinition[]) {
897
906
  const sceneSrc = scenes[0]?.ppt?.src;
898
907
  return sceneSrc?.startsWith("pptx://");
package/vite.config.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import path from "path";
2
- import { defineConfig } from "vite";
2
+ import { defineConfig } from 'vitest/config'
3
3
  import { svelte } from "@sveltejs/vite-plugin-svelte";
4
4
  import { dependencies, peerDependencies, version, devDependencies } from "./package.json"
5
5
  import { omit } from "lodash";
@@ -8,6 +8,14 @@ export default defineConfig(({ mode }) => {
8
8
  const isProd = mode === "production";
9
9
 
10
10
  return {
11
+ test: {
12
+ environment: "jsdom",
13
+ deps: {
14
+ inline: [
15
+ "@juggle/resize-observer"
16
+ ]
17
+ }
18
+ },
11
19
  define: {
12
20
  __APP_VERSION__: JSON.stringify(version),
13
21
  __APP_DEPENDENCIES__: JSON.stringify({
@@ -26,7 +34,7 @@ export default defineConfig(({ mode }) => {
26
34
  lib: {
27
35
  // eslint-disable-next-line no-undef
28
36
  entry: path.resolve(__dirname, "src/index.ts"),
29
- formats: ["es", "umd"], // TODO cjs 版本待修复
37
+ formats: ["es", "umd", "cjs"],
30
38
  name: "WindowManager",
31
39
  fileName: "index"
32
40
  },