@netless/window-manager 0.4.73-beta.0 → 0.4.73
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 +1093 -40
- package/dist/index.js +15 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +266 -164
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -13
- package/src/App/AppContext.ts +13 -8
- package/src/App/AppProxy.ts +3 -8
- package/src/App/MagixEvent/index.ts +38 -38
- package/src/App/Storage/StorageEvent.ts +13 -13
- package/src/App/Storage/index.ts +265 -242
- package/src/App/Storage/typings.ts +4 -2
- package/src/App/Storage/utils.ts +3 -3
- package/src/AppListener.ts +5 -5
- package/src/AppManager.ts +6 -19
- package/src/AttributesDelegate.ts +5 -7
- package/src/BoxEmitter.ts +12 -6
- package/src/BoxManager.ts +1 -1
- package/src/ContainerResizeObserver.ts +1 -1
- package/src/Cursor/Cursor.svelte +3 -1
- package/src/Cursor/Cursor.ts +1 -0
- package/src/Cursor/index.ts +1 -1
- package/src/Helper.ts +67 -15
- package/src/InternalEmitter.ts +4 -3
- package/src/Page/index.ts +1 -1
- package/src/Register/index.ts +5 -7
- package/src/Register/loader.ts +1 -1
- package/src/Register/storage.ts +13 -13
- package/src/Utils/Common.ts +10 -5
- package/src/Utils/Reactive.ts +26 -25
- package/src/Utils/RoomHacker.ts +1 -1
- package/src/Utils/error.ts +0 -1
- package/src/View/IframeBridge.ts +627 -583
- package/src/View/MainView.ts +8 -6
- package/src/callback.ts +7 -1
- package/src/index.ts +34 -24
- package/src/typings.ts +11 -6
- package/dist/App/AppContext.d.ts +0 -79
- package/dist/App/AppPageStateImpl.d.ts +0 -17
- package/dist/App/AppProxy.d.ts +0 -67
- package/dist/App/MagixEvent/index.d.ts +0 -29
- package/dist/App/Storage/StorageEvent.d.ts +0 -8
- package/dist/App/Storage/index.d.ts +0 -39
- package/dist/App/Storage/typings.d.ts +0 -22
- package/dist/App/Storage/utils.d.ts +0 -5
- package/dist/App/index.d.ts +0 -2
- package/dist/AppListener.d.ts +0 -21
- package/dist/AppManager.d.ts +0 -110
- package/dist/AttributesDelegate.d.ts +0 -91
- package/dist/BoxEmitter.d.ts +0 -34
- package/dist/BoxManager.d.ts +0 -98
- package/dist/BuiltinApps.d.ts +0 -5
- package/dist/ContainerResizeObserver.d.ts +0 -11
- package/dist/Cursor/Cursor.d.ts +0 -43
- package/dist/Cursor/icons.d.ts +0 -3
- package/dist/Cursor/icons2.d.ts +0 -4
- package/dist/Cursor/index.d.ts +0 -55
- package/dist/Helper.d.ts +0 -11
- package/dist/InternalEmitter.d.ts +0 -34
- package/dist/Page/PageController.d.ts +0 -21
- package/dist/Page/index.d.ts +0 -3
- package/dist/PageState.d.ts +0 -9
- package/dist/ReconnectRefresher.d.ts +0 -24
- package/dist/RedoUndo.d.ts +0 -18
- package/dist/Register/index.d.ts +0 -28
- package/dist/Register/loader.d.ts +0 -4
- package/dist/Register/storage.d.ts +0 -8
- package/dist/Utils/AppCreateQueue.d.ts +0 -15
- package/dist/Utils/Common.d.ts +0 -22
- package/dist/Utils/Reactive.d.ts +0 -6
- package/dist/Utils/RoomHacker.d.ts +0 -3
- package/dist/Utils/error.d.ts +0 -27
- package/dist/Utils/log.d.ts +0 -1
- package/dist/View/IframeBridge.d.ts +0 -146
- package/dist/View/MainView.d.ts +0 -58
- package/dist/View/ViewManager.d.ts +0 -13
- package/dist/callback.d.ts +0 -38
- package/dist/constants.d.ts +0 -48
- package/dist/typings.d.ts +0 -84
package/src/View/IframeBridge.ts
CHANGED
@@ -10,627 +10,671 @@ import { log } from "../Utils/log";
|
|
10
10
|
|
11
11
|
// Note: typo below should not be fixed.
|
12
12
|
export enum IframeEvents {
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
13
|
+
Init = "Init",
|
14
|
+
AttributesUpdate = "AttributesUpdate",
|
15
|
+
SetAttributes = "SetAttributes",
|
16
|
+
RegisterMagixEvent = "RegisterMagixEvent",
|
17
|
+
RemoveMagixEvent = "RemoveMagixEvent",
|
18
|
+
RemoveAllMagixEvent = "RemoveAllMagixEvent",
|
19
|
+
RoomStateChanged = "RoomStateChanged",
|
20
|
+
DispatchMagixEvent = "DispatchMagixEvent",
|
21
|
+
ReciveMagixEvent = "ReciveMagixEvent",
|
22
|
+
NextPage = "NextPage",
|
23
|
+
PrevPage = "PrevPage",
|
24
|
+
SDKCreate = "SDKCreate",
|
25
|
+
OnCreate = "OnCreate",
|
26
|
+
SetPage = "SetPage",
|
27
|
+
GetAttributes = "GetAttributes",
|
28
|
+
Ready = "Ready",
|
29
|
+
Destory = "Destory",
|
30
|
+
StartCreate = "StartCreate",
|
31
|
+
WrapperDidUpdate = "WrapperDidUpdate",
|
32
|
+
DispayIframe = "DispayIframe",
|
33
|
+
HideIframe = "HideIframe",
|
34
|
+
GetRootRect = "GetRootRect",
|
35
|
+
ReplayRootRect = "ReplayRootRect",
|
36
|
+
PageTo = "PageTo",
|
37
37
|
}
|
38
38
|
|
39
39
|
export enum DomEvents {
|
40
|
-
|
41
|
-
|
40
|
+
WrapperDidMount = "WrapperDidMount",
|
41
|
+
IframeLoad = "IframeLoad",
|
42
42
|
}
|
43
43
|
|
44
44
|
export type IframeBridgeAttributes = {
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
45
|
+
readonly url: string;
|
46
|
+
readonly width: number;
|
47
|
+
readonly height: number;
|
48
|
+
readonly displaySceneDir: string;
|
49
|
+
readonly lastEvent?: { name: string; payload: any };
|
50
|
+
readonly useClicker?: boolean;
|
51
|
+
readonly useSelector?: boolean;
|
52
52
|
};
|
53
53
|
|
54
54
|
export type IframeBridgeEvents = {
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
}
|
55
|
+
created: undefined;
|
56
|
+
[IframeEvents.Ready]: undefined;
|
57
|
+
[IframeEvents.StartCreate]: undefined;
|
58
|
+
[IframeEvents.OnCreate]: IframeBridge;
|
59
|
+
[IframeEvents.Destory]: undefined;
|
60
|
+
[IframeEvents.GetRootRect]: undefined;
|
61
|
+
[IframeEvents.ReplayRootRect]: DOMRect;
|
62
|
+
[DomEvents.WrapperDidMount]: undefined;
|
63
|
+
[IframeEvents.WrapperDidUpdate]: undefined;
|
64
|
+
[DomEvents.IframeLoad]: Event;
|
65
|
+
[IframeEvents.HideIframe]: undefined;
|
66
|
+
[IframeEvents.DispayIframe]: undefined;
|
67
|
+
};
|
68
68
|
|
69
69
|
export type IframeSize = {
|
70
|
-
|
71
|
-
|
70
|
+
readonly width: number;
|
71
|
+
readonly height: number;
|
72
72
|
};
|
73
73
|
|
74
74
|
type BaseOption = {
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
}
|
75
|
+
readonly url: string;
|
76
|
+
readonly width: number;
|
77
|
+
readonly height: number;
|
78
|
+
readonly displaySceneDir: string;
|
79
|
+
};
|
80
80
|
|
81
81
|
export type InsertOptions = {
|
82
|
-
|
83
|
-
|
82
|
+
readonly useClicker?: boolean;
|
83
|
+
readonly useSelector?: boolean;
|
84
84
|
} & BaseOption;
|
85
85
|
|
86
86
|
export type OnCreateInsertOption = {
|
87
|
-
|
87
|
+
readonly displayer: Displayer;
|
88
88
|
} & BaseOption;
|
89
89
|
|
90
90
|
const RefreshIDs = {
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
91
|
+
Ready: IframeEvents.Ready,
|
92
|
+
RootRect: IframeEvents.ReplayRootRect,
|
93
|
+
Message: "message",
|
94
|
+
ComputeStyle: "computeStyle",
|
95
|
+
Load: "load",
|
96
|
+
DisplayerState: "displayerState",
|
97
|
+
Show: "show",
|
98
|
+
Hide: "hide",
|
99
99
|
};
|
100
100
|
|
101
101
|
const times = <T>(number: number, iteratee: (value: number) => T) => {
|
102
|
-
|
102
|
+
return new Array(number).fill(0).map((_, index) => iteratee(index));
|
103
103
|
};
|
104
104
|
|
105
105
|
/**
|
106
106
|
* {@link https://github.com/netless-io/netless-iframe-bridge @netless/iframe-bridge}
|
107
107
|
*/
|
108
108
|
export class IframeBridge {
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
109
|
+
public static readonly kind = "IframeBridge";
|
110
|
+
public static readonly hiddenClass = "netless-iframe-brdige-hidden";
|
111
|
+
public static emitter: Emittery<IframeBridgeEvents> = new Emittery();
|
112
|
+
private static displayer: Displayer | null = null;
|
113
|
+
private static alreadyCreate = false;
|
114
|
+
|
115
|
+
public displayer: Displayer;
|
116
|
+
public iframe: HTMLIFrameElement;
|
117
|
+
|
118
|
+
private readonly magixEventMap = new Map<string, any>();
|
119
|
+
private cssList: string[] = [];
|
120
|
+
private allowAppliances: string[] = ["clicker"];
|
121
|
+
private bridgeDisposer: () => void = noop;
|
122
|
+
private rootRect: DOMRect | null = null;
|
123
|
+
|
124
|
+
private sideEffectManager = new SideEffectManager();
|
125
|
+
|
126
|
+
constructor(readonly manager: WindowManager, readonly appManager: AppManager) {
|
127
|
+
this.displayer = IframeBridge.displayer = appManager.displayer;
|
128
|
+
|
129
|
+
this.iframe = this._createIframe();
|
130
|
+
|
131
|
+
this.sideEffectManager.addDisposer(
|
132
|
+
IframeBridge.emitter.on(IframeEvents.ReplayRootRect, rect => {
|
133
|
+
this.rootRect = rect;
|
134
|
+
}),
|
135
|
+
RefreshIDs.RootRect
|
136
|
+
);
|
137
|
+
|
138
|
+
this.sideEffectManager.addDisposer(
|
139
|
+
IframeBridge.emitter.on(IframeEvents.HideIframe, () => {
|
140
|
+
this.iframe.className = IframeBridge.hiddenClass;
|
141
|
+
}),
|
142
|
+
RefreshIDs.Hide
|
143
|
+
);
|
144
|
+
|
145
|
+
this.sideEffectManager.addDisposer(
|
146
|
+
IframeBridge.emitter.on(IframeEvents.DispayIframe, () => {
|
147
|
+
this.iframe.className = "";
|
148
|
+
}),
|
149
|
+
RefreshIDs.Show
|
150
|
+
);
|
151
|
+
|
152
|
+
this.sideEffectManager.addDisposer(
|
153
|
+
IframeBridge.emitter.on("created", () => {
|
154
|
+
this.bridgeDisposer();
|
155
|
+
this.bridgeDisposer = autorun(() => {
|
156
|
+
const attributes = this.attributes;
|
157
|
+
if (attributes.url) {
|
158
|
+
const iframeSrc = this.iframe?.src;
|
159
|
+
if (iframeSrc && iframeSrc !== attributes.url) {
|
160
|
+
this.execListenIframe(attributes);
|
161
|
+
}
|
162
|
+
}
|
163
|
+
if (attributes.displaySceneDir) {
|
164
|
+
this.computedIframeDisplay(this.displayer.state, attributes);
|
165
|
+
}
|
166
|
+
if ((attributes.width || attributes.height) && this.iframe) {
|
167
|
+
this.iframe.width = `${attributes.width}px`;
|
168
|
+
this.iframe.height = `${attributes.height}px`;
|
169
|
+
}
|
170
|
+
this.postMessage({ kind: IframeEvents.AttributesUpdate, payload: attributes });
|
171
|
+
});
|
172
|
+
})
|
173
|
+
);
|
174
|
+
|
175
|
+
this.sideEffectManager.addDisposer(
|
176
|
+
manager.emitter.on("cameraStateChange", () => {
|
177
|
+
this.computedStyle(this.displayer.state);
|
178
|
+
})
|
179
|
+
);
|
180
|
+
|
181
|
+
IframeBridge.onCreate(this);
|
182
|
+
}
|
183
|
+
|
184
|
+
public static onCreate(plugin: IframeBridge): void {
|
185
|
+
IframeBridge.emitter.emit(IframeEvents.StartCreate);
|
186
|
+
IframeBridge.emitter.emit(IframeEvents.OnCreate, plugin);
|
187
|
+
IframeBridge.emitter.emit("created");
|
188
|
+
}
|
189
|
+
|
190
|
+
public insert(options: InsertOptions): this {
|
191
|
+
const initAttributes: IframeBridgeAttributes = {
|
192
|
+
url: options.url,
|
193
|
+
width: options.width,
|
194
|
+
height: options.height,
|
195
|
+
displaySceneDir: options.displaySceneDir,
|
196
|
+
useClicker: options.useClicker || false,
|
197
|
+
useSelector: options.useSelector,
|
198
|
+
};
|
199
|
+
this.setAttributes(initAttributes);
|
200
|
+
|
201
|
+
const wrapperDidMountListener = () => {
|
202
|
+
this.getIframe();
|
203
|
+
this.listenIframe(this.attributes);
|
204
|
+
this.listenDisplayerState();
|
205
|
+
IframeBridge.emitter.emit(IframeEvents.GetRootRect);
|
206
|
+
};
|
207
|
+
|
208
|
+
if (this.getIframe()) {
|
209
|
+
wrapperDidMountListener();
|
152
210
|
}
|
153
|
-
|
154
|
-
|
211
|
+
// Code below will never be executed, just copying the old code...
|
212
|
+
else {
|
213
|
+
const didMount = this.sideEffectManager.addDisposer(
|
214
|
+
IframeBridge.emitter.on(DomEvents.WrapperDidMount, () => {
|
215
|
+
wrapperDidMountListener();
|
216
|
+
this.sideEffectManager.flush(didMount);
|
217
|
+
})
|
218
|
+
);
|
219
|
+
const didUpdate = this.sideEffectManager.addDisposer(
|
220
|
+
IframeBridge.emitter.on(IframeEvents.WrapperDidUpdate, () => {
|
221
|
+
wrapperDidMountListener();
|
222
|
+
this.sideEffectManager.flush(didUpdate);
|
223
|
+
})
|
224
|
+
);
|
155
225
|
}
|
156
|
-
if (
|
157
|
-
|
158
|
-
this.iframe.height = `${attributes.height}px`;
|
226
|
+
if (this.attributes.useSelector) {
|
227
|
+
this.allowAppliances.push("selector");
|
159
228
|
}
|
160
|
-
this.postMessage({ kind: IframeEvents.AttributesUpdate, payload: attributes });
|
161
|
-
});
|
162
|
-
}));
|
163
|
-
|
164
|
-
this.sideEffectManager.addDisposer(manager.emitter.on("cameraStateChange", () => {
|
165
|
-
this.computedStyle(this.displayer.state);
|
166
|
-
}));
|
167
|
-
|
168
|
-
IframeBridge.onCreate(this);
|
169
|
-
}
|
170
|
-
|
171
|
-
public static onCreate(plugin: IframeBridge): void {
|
172
|
-
IframeBridge.emitter.emit(IframeEvents.StartCreate);
|
173
|
-
IframeBridge.emitter.emit(IframeEvents.OnCreate, plugin);
|
174
|
-
IframeBridge.emitter.emit("created");
|
175
|
-
}
|
176
|
-
|
177
|
-
public insert(options: InsertOptions): this {
|
178
|
-
const initAttributes: IframeBridgeAttributes = {
|
179
|
-
url: options.url,
|
180
|
-
width: options.width,
|
181
|
-
height: options.height,
|
182
|
-
displaySceneDir: options.displaySceneDir,
|
183
|
-
useClicker: options.useClicker || false,
|
184
|
-
useSelector: options.useSelector,
|
185
|
-
};
|
186
|
-
this.setAttributes(initAttributes);
|
187
229
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
230
|
+
this.computedStyle(this.displayer.state);
|
231
|
+
this.listenDisplayerCallbacks();
|
232
|
+
this.getComputedIframeStyle();
|
233
|
+
this.sideEffectManager.addEventListener(
|
234
|
+
window,
|
235
|
+
"message",
|
236
|
+
this.messageListener.bind(this),
|
237
|
+
void 0,
|
238
|
+
RefreshIDs.Message
|
239
|
+
);
|
240
|
+
|
241
|
+
IframeBridge.alreadyCreate = true;
|
242
|
+
return this;
|
243
|
+
}
|
244
|
+
|
245
|
+
// 在某些安卓机型中会遇到 iframe 嵌套计算 bug,需要手动延迟触发一下重绘
|
246
|
+
private getComputedIframeStyle(): void {
|
247
|
+
this.sideEffectManager.setTimeout(
|
248
|
+
() => {
|
249
|
+
if (this.iframe) {
|
250
|
+
getComputedStyle(this.iframe);
|
251
|
+
}
|
252
|
+
},
|
253
|
+
200,
|
254
|
+
RefreshIDs.ComputeStyle
|
255
|
+
);
|
256
|
+
}
|
257
|
+
|
258
|
+
public destroy() {
|
259
|
+
this.sideEffectManager.flushAll();
|
260
|
+
IframeBridge.emitter.emit(IframeEvents.Destory);
|
261
|
+
IframeBridge.alreadyCreate = false;
|
262
|
+
IframeBridge.emitter.clearListeners();
|
263
|
+
}
|
264
|
+
|
265
|
+
private getIframe(): HTMLIFrameElement {
|
266
|
+
this.iframe || (this.iframe = this._createIframe());
|
267
|
+
return this.iframe;
|
268
|
+
}
|
269
|
+
|
270
|
+
public setIframeSize(params: IframeSize): void {
|
271
|
+
if (this.iframe) {
|
272
|
+
this.iframe.width = `${params.width}px`;
|
273
|
+
this.iframe.height = `${params.height}px`;
|
274
|
+
this.setAttributes({ width: params.width, height: params.height });
|
275
|
+
}
|
276
|
+
}
|
277
|
+
|
278
|
+
public get attributes(): Partial<IframeBridgeAttributes> {
|
279
|
+
return this.appManager.store.getIframeBridge();
|
280
|
+
}
|
281
|
+
|
282
|
+
public setAttributes(data: Partial<IframeBridgeAttributes>): void {
|
283
|
+
this.appManager.store.setIframeBridge(data);
|
284
|
+
}
|
285
|
+
|
286
|
+
private _createIframe() {
|
287
|
+
const iframe = document.createElement("iframe");
|
288
|
+
iframe.id = "IframeBridge";
|
289
|
+
iframe.className = IframeBridge.hiddenClass;
|
290
|
+
if (this.appManager.mainView.divElement) {
|
291
|
+
this.appManager.mainView.divElement.appendChild(iframe);
|
292
|
+
}
|
293
|
+
return iframe;
|
294
|
+
}
|
295
|
+
|
296
|
+
public scaleIframeToFit(animationMode: AnimationMode = AnimationMode.Immediately) {
|
297
|
+
if (!this.inDisplaySceneDir) {
|
298
|
+
return;
|
299
|
+
}
|
300
|
+
const { width = 1280, height = 720 } = this.attributes;
|
301
|
+
const x = width ? -width / 2 : 0;
|
302
|
+
const y = height ? -height / 2 : 0;
|
303
|
+
|
304
|
+
this.manager.moveCameraToContain({
|
305
|
+
originX: x,
|
306
|
+
originY: y,
|
307
|
+
width,
|
308
|
+
height,
|
309
|
+
animationMode,
|
310
|
+
});
|
311
|
+
}
|
312
|
+
|
313
|
+
public get isReplay(): boolean {
|
314
|
+
return this.manager.isReplay;
|
315
|
+
}
|
316
|
+
|
317
|
+
private handleSetPage(data: any): void {
|
318
|
+
if (this.isReplay || !this.attributes.displaySceneDir) {
|
319
|
+
return;
|
320
|
+
}
|
321
|
+
const page = data.payload;
|
322
|
+
const room = this.displayer as Room;
|
323
|
+
const scenes = room.entireScenes()[this.attributes.displaySceneDir];
|
324
|
+
if (!scenes || scenes.length !== page) {
|
325
|
+
const genScenes = times<{ name: string }>(page, (index: number) => ({
|
326
|
+
name: String(index + 1),
|
327
|
+
}));
|
328
|
+
room.putScenes(this.attributes.displaySceneDir, genScenes);
|
329
|
+
this.manager.setMainViewScenePath(this.attributes.displaySceneDir);
|
330
|
+
}
|
331
|
+
}
|
194
332
|
|
195
|
-
|
196
|
-
|
197
|
-
}
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
this.iframe.height = `${params.height}px`;
|
248
|
-
this.setAttributes({ width: params.width, height: params.height });
|
249
|
-
}
|
250
|
-
}
|
251
|
-
|
252
|
-
public get attributes(): Partial<IframeBridgeAttributes> {
|
253
|
-
return this.appManager.store.getIframeBridge();
|
254
|
-
}
|
255
|
-
|
256
|
-
public setAttributes(data: Partial<IframeBridgeAttributes>): void {
|
257
|
-
this.appManager.store.setIframeBridge(data);
|
258
|
-
}
|
259
|
-
|
260
|
-
private _createIframe() {
|
261
|
-
const iframe = document.createElement("iframe");
|
262
|
-
iframe.id = "IframeBridge";
|
263
|
-
iframe.className = IframeBridge.hiddenClass;
|
264
|
-
if (this.appManager.mainView.divElement) {
|
265
|
-
this.appManager.mainView.divElement.appendChild(iframe);
|
266
|
-
}
|
267
|
-
return iframe;
|
268
|
-
}
|
269
|
-
|
270
|
-
public scaleIframeToFit(animationMode: AnimationMode = AnimationMode.Immediately) {
|
271
|
-
if (!this.inDisplaySceneDir) {
|
272
|
-
return;
|
273
|
-
}
|
274
|
-
const { width = 1280, height = 720 } = this.attributes;
|
275
|
-
const x = width ? -width / 2 : 0;
|
276
|
-
const y = height ? -height / 2 : 0;
|
277
|
-
|
278
|
-
this.manager.moveCameraToContain({
|
279
|
-
originX: x,
|
280
|
-
originY: y,
|
281
|
-
width,
|
282
|
-
height,
|
283
|
-
animationMode,
|
284
|
-
});
|
285
|
-
}
|
286
|
-
|
287
|
-
public get isReplay(): boolean {
|
288
|
-
return this.manager.isReplay;
|
289
|
-
}
|
290
|
-
|
291
|
-
private handleSetPage(data: any): void {
|
292
|
-
if (this.isReplay || !this.attributes.displaySceneDir) {
|
293
|
-
return;
|
294
|
-
}
|
295
|
-
const page = data.payload;
|
296
|
-
const room = this.displayer as Room;
|
297
|
-
const scenes = room.entireScenes()[this.attributes.displaySceneDir];
|
298
|
-
if (!scenes || scenes.length !== page) {
|
299
|
-
const genScenes = times<{ name: string }>(page, (index: number) => ({ name: String(index + 1) }));
|
300
|
-
room.putScenes(this.attributes.displaySceneDir, genScenes);
|
301
|
-
this.manager.setMainViewScenePath(this.attributes.displaySceneDir);
|
302
|
-
}
|
303
|
-
}
|
304
|
-
|
305
|
-
private execListenIframe = debounce((options: Partial<IframeBridgeAttributes>) => {
|
306
|
-
this.listenIframe(options);
|
307
|
-
}, 50);
|
308
|
-
|
309
|
-
private src_url_equal_anchor?: HTMLAnchorElement
|
310
|
-
private listenIframe(options: Partial<IframeBridgeAttributes>): void {
|
311
|
-
const loadListener = (ev: Event) => {
|
312
|
-
this.postMessage({
|
313
|
-
kind: IframeEvents.Init, payload: {
|
314
|
-
attributes: this.attributes,
|
315
|
-
roomState: IframeBridge.displayer?.state,
|
316
|
-
currentPage: this.currentPage,
|
317
|
-
observerId: this.displayer.observerId
|
333
|
+
private execListenIframe = debounce((options: Partial<IframeBridgeAttributes>) => {
|
334
|
+
this.listenIframe(options);
|
335
|
+
}, 50);
|
336
|
+
|
337
|
+
private src_url_equal_anchor?: HTMLAnchorElement;
|
338
|
+
private listenIframe(options: Partial<IframeBridgeAttributes>): void {
|
339
|
+
const loadListener = (ev: Event) => {
|
340
|
+
this.postMessage({
|
341
|
+
kind: IframeEvents.Init,
|
342
|
+
payload: {
|
343
|
+
attributes: this.attributes,
|
344
|
+
roomState: IframeBridge.displayer?.state,
|
345
|
+
currentPage: this.currentPage,
|
346
|
+
observerId: this.displayer.observerId,
|
347
|
+
},
|
348
|
+
});
|
349
|
+
IframeBridge.emitter.emit(DomEvents.IframeLoad, ev);
|
350
|
+
this.sideEffectManager.addDisposer(
|
351
|
+
IframeBridge.emitter.on(IframeEvents.Ready, () => {
|
352
|
+
this.postMessage(this.attributes.lastEvent?.payload);
|
353
|
+
}),
|
354
|
+
RefreshIDs.Ready
|
355
|
+
);
|
356
|
+
this.computedStyleAndIframeDisplay();
|
357
|
+
// if ((this.displayer as Room).isWritable) {
|
358
|
+
// this.manager.moveCamera({
|
359
|
+
// scale: this.manager.camera.scale + 1e-6,
|
360
|
+
// animationMode: AnimationMode.Immediately,
|
361
|
+
// })
|
362
|
+
// }
|
363
|
+
};
|
364
|
+
if (options.url && this.iframe.src !== options.url) {
|
365
|
+
if (!this.src_url_equal_anchor) this.src_url_equal_anchor = document.createElement("a");
|
366
|
+
this.src_url_equal_anchor.href = options.url;
|
367
|
+
if (this.src_url_equal_anchor.href !== this.iframe.src) {
|
368
|
+
this.iframe.src = options.url;
|
369
|
+
}
|
370
|
+
}
|
371
|
+
this.iframe.width = `${options.width}px`;
|
372
|
+
this.iframe.height = `${options.height}px`;
|
373
|
+
this.sideEffectManager.addEventListener(
|
374
|
+
this.iframe,
|
375
|
+
"load",
|
376
|
+
loadListener,
|
377
|
+
void 0,
|
378
|
+
RefreshIDs.Load
|
379
|
+
);
|
380
|
+
}
|
381
|
+
|
382
|
+
private onPhaseChangedListener = (phase: PlayerPhase) => {
|
383
|
+
if (phase === PlayerPhase.Playing) {
|
384
|
+
this.computedStyleAndIframeDisplay();
|
318
385
|
}
|
319
|
-
})
|
320
|
-
IframeBridge.emitter.emit(DomEvents.IframeLoad, ev);
|
321
|
-
this.sideEffectManager.addDisposer(IframeBridge.emitter.on(IframeEvents.Ready, () => {
|
322
|
-
this.postMessage(this.attributes.lastEvent?.payload);
|
323
|
-
}), RefreshIDs.Ready)
|
324
|
-
this.computedStyleAndIframeDisplay();
|
325
|
-
// if ((this.displayer as Room).isWritable) {
|
326
|
-
// this.manager.moveCamera({
|
327
|
-
// scale: this.manager.camera.scale + 1e-6,
|
328
|
-
// animationMode: AnimationMode.Immediately,
|
329
|
-
// })
|
330
|
-
// }
|
331
386
|
};
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
private onPhaseChangedListener = (phase: PlayerPhase) => {
|
345
|
-
if (phase === PlayerPhase.Playing) {
|
346
|
-
this.computedStyleAndIframeDisplay();
|
347
|
-
}
|
348
|
-
}
|
349
|
-
|
350
|
-
private listenDisplayerState(): void {
|
351
|
-
if (this.isReplay) {
|
352
|
-
if ((this.displayer as any)._phase === PlayerPhase.Playing) {
|
387
|
+
|
388
|
+
private listenDisplayerState(): void {
|
389
|
+
if (this.isReplay) {
|
390
|
+
if ((this.displayer as any)._phase === PlayerPhase.Playing) {
|
391
|
+
this.computedStyleAndIframeDisplay();
|
392
|
+
}
|
393
|
+
this.sideEffectManager.add(() => {
|
394
|
+
this.displayer.callbacks.on("onPhaseChanged", this.onPhaseChangedListener);
|
395
|
+
return () =>
|
396
|
+
this.displayer.callbacks.off("onPhaseChanged", this.onPhaseChangedListener);
|
397
|
+
}, RefreshIDs.DisplayerState);
|
398
|
+
}
|
353
399
|
this.computedStyleAndIframeDisplay();
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
}
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
if (state.memberState) {
|
385
|
-
this.computedZindex();
|
386
|
-
this.updateStyle();
|
387
|
-
}
|
388
|
-
if (state.sceneState) {
|
389
|
-
this.computedIframeDisplay(state, this.attributes);
|
390
|
-
}
|
391
|
-
}
|
392
|
-
|
393
|
-
private computedStyle(_state: DisplayerState): void {
|
394
|
-
const cameraState = this.manager.cameraState;
|
395
|
-
const setWidth = this.attributes.width || 1280;
|
396
|
-
const setHeight = this.attributes.height || 720;
|
397
|
-
if (this.iframe) {
|
398
|
-
const { width, height, scale, centerX, centerY } = cameraState;
|
399
|
-
const rootRect = this.rootRect || { x: 0, y: 0 }
|
400
|
-
const transformOriginX = `${(width / 2) + rootRect.x}px`;
|
401
|
-
const transformOriginY = `${(height / 2) + rootRect.y}px`;
|
402
|
-
const transformOrigin = `transform-origin: ${transformOriginX} ${transformOriginY};`;
|
403
|
-
const iframeXDiff = ((width - setWidth) / 2) * scale;
|
404
|
-
const iframeYDiff = ((height - setHeight) / 2) * scale;
|
405
|
-
const x = - (centerX * scale) + iframeXDiff;
|
406
|
-
const y = - (centerY * scale) + iframeYDiff;
|
407
|
-
const transform = `transform: translate(${x}px,${y}px) scale(${scale}, ${scale});`;
|
408
|
-
const position = "position: absolute;";
|
409
|
-
// 在某些安卓机型, border-width 不为 0 时,才能正确计算 iframe 里嵌套 iframe 的大小
|
410
|
-
const borderWidth = "border: 0.1px solid rgba(0,0,0,0);";
|
411
|
-
const left = `left: 0px;`;
|
412
|
-
const top = `top: 0px;`;
|
413
|
-
const cssList = [position, borderWidth, top, left, transformOrigin, transform];
|
414
|
-
this.cssList = cssList;
|
415
|
-
this.computedZindex();
|
416
|
-
this.updateStyle();
|
417
|
-
}
|
418
|
-
}
|
419
|
-
|
420
|
-
private computedIframeDisplay(_state: DisplayerState, _attributes: Partial<IframeBridgeAttributes>): void {
|
421
|
-
if (this.inDisplaySceneDir) {
|
422
|
-
IframeBridge.emitter.emit(IframeEvents.DispayIframe);
|
423
|
-
} else {
|
424
|
-
IframeBridge.emitter.emit(IframeEvents.HideIframe);
|
425
|
-
}
|
426
|
-
}
|
427
|
-
|
428
|
-
public computedZindex(): void {
|
429
|
-
const zIndexString = "z-index: -1;";
|
430
|
-
const index = this.cssList.findIndex(css => css === zIndexString);
|
431
|
-
if (index !== -1) {
|
432
|
-
this.cssList.splice(index, 1);
|
433
|
-
}
|
434
|
-
if (!this.isClicker() || this.isDisableInput) {
|
435
|
-
this.cssList.push(zIndexString);
|
436
|
-
}
|
437
|
-
}
|
438
|
-
|
439
|
-
private updateStyle(): void {
|
440
|
-
this.iframe.style.cssText = this.cssList.join(" ");
|
441
|
-
}
|
442
|
-
|
443
|
-
private get iframeOrigin(): string | undefined {
|
444
|
-
if (this.iframe) {
|
445
|
-
try {
|
446
|
-
return new URL(this.iframe.src).origin;
|
447
|
-
} catch (err) {
|
448
|
-
console.warn(err);
|
449
|
-
}
|
450
|
-
}
|
451
|
-
}
|
452
|
-
|
453
|
-
private messageListener(event: MessageEvent): void {
|
454
|
-
log("<<<", JSON.stringify(event.data));
|
455
|
-
if (event.origin !== this.iframeOrigin) {
|
456
|
-
return;
|
457
|
-
}
|
458
|
-
const data = event.data;
|
459
|
-
switch (data.kind) {
|
460
|
-
case IframeEvents.SetAttributes: {
|
461
|
-
this.handleSetAttributes(data);
|
462
|
-
break;
|
463
|
-
}
|
464
|
-
case IframeEvents.RegisterMagixEvent: {
|
465
|
-
this.handleRegisterMagixEvent(data);
|
466
|
-
break;
|
467
|
-
}
|
468
|
-
case IframeEvents.RemoveMagixEvent: {
|
469
|
-
this.handleRemoveMagixEvent(data);
|
470
|
-
break;
|
471
|
-
}
|
472
|
-
case IframeEvents.DispatchMagixEvent: {
|
473
|
-
this.handleDispatchMagixEvent(data);
|
474
|
-
break;
|
475
|
-
}
|
476
|
-
case IframeEvents.RemoveAllMagixEvent: {
|
477
|
-
this.handleRemoveAllMagixEvent();
|
478
|
-
break;
|
479
|
-
}
|
480
|
-
case IframeEvents.NextPage: {
|
481
|
-
this.handleNextPage();
|
482
|
-
break;
|
483
|
-
}
|
484
|
-
case IframeEvents.PrevPage: {
|
485
|
-
this.handlePrevPage();
|
486
|
-
break;
|
487
|
-
}
|
488
|
-
case IframeEvents.SDKCreate: {
|
489
|
-
this.handleSDKCreate();
|
490
|
-
break;
|
491
|
-
}
|
492
|
-
case IframeEvents.SetPage: {
|
493
|
-
this.handleSetPage(data);
|
494
|
-
break;
|
495
|
-
}
|
496
|
-
case IframeEvents.GetAttributes: {
|
497
|
-
this.handleGetAttributes();
|
498
|
-
break;
|
499
|
-
}
|
500
|
-
case IframeEvents.PageTo: {
|
501
|
-
this.handlePageTo(data);
|
502
|
-
break
|
503
|
-
}
|
504
|
-
default: {
|
505
|
-
log(`${data.kind} not allow event.`);
|
506
|
-
break;
|
507
|
-
}
|
508
|
-
}
|
509
|
-
}
|
510
|
-
|
511
|
-
private handleSDKCreate(): void {
|
512
|
-
this.postMessage({
|
513
|
-
kind: IframeEvents.Init, payload: {
|
514
|
-
attributes: this.attributes,
|
515
|
-
roomState: this.displayer.state,
|
516
|
-
currentPage: this.currentPage,
|
517
|
-
observerId: this.displayer.observerId
|
518
|
-
}
|
519
|
-
});
|
520
|
-
}
|
521
|
-
|
522
|
-
private handleDispatchMagixEvent(data: any): void {
|
523
|
-
const eventPayload: { event: string, payload: any } = data.payload;
|
524
|
-
this.appManager.safeDispatchMagixEvent(eventPayload.event, eventPayload.payload);
|
525
|
-
}
|
526
|
-
|
527
|
-
private handleSetAttributes(data: any): void {
|
528
|
-
this.setAttributes(data.payload);
|
529
|
-
}
|
530
|
-
|
531
|
-
private handleRegisterMagixEvent(data: any): void {
|
532
|
-
const eventName = data.payload as string;
|
533
|
-
const listener = (event: any) => {
|
534
|
-
if (event.authorId === this.displayer.observerId) {
|
535
|
-
return;
|
536
|
-
}
|
537
|
-
this.postMessage({ kind: IframeEvents.ReciveMagixEvent, payload: event });
|
400
|
+
}
|
401
|
+
|
402
|
+
private computedStyleAndIframeDisplay(): void {
|
403
|
+
this.computedStyle(this.displayer.state);
|
404
|
+
this.computedIframeDisplay(this.displayer.state, this.attributes);
|
405
|
+
}
|
406
|
+
|
407
|
+
private listenDisplayerCallbacks(): void {
|
408
|
+
this.displayer.callbacks.on(this.callbackName as any, this.stateChangeListener);
|
409
|
+
}
|
410
|
+
|
411
|
+
private get callbackName(): string {
|
412
|
+
return this.isReplay ? "onPlayerStateChanged" : "onRoomStateChanged";
|
413
|
+
}
|
414
|
+
|
415
|
+
private stateChangeListener = (state: RoomState) => {
|
416
|
+
state = { ...state };
|
417
|
+
state.cameraState = this.manager.cameraState;
|
418
|
+
this.postMessage({ kind: IframeEvents.RoomStateChanged, payload: state });
|
419
|
+
if (state.cameraState) {
|
420
|
+
IframeBridge.emitter.emit(IframeEvents.GetRootRect);
|
421
|
+
this.computedStyle(state);
|
422
|
+
}
|
423
|
+
if (state.memberState) {
|
424
|
+
this.computedZindex();
|
425
|
+
this.updateStyle();
|
426
|
+
}
|
427
|
+
if (state.sceneState) {
|
428
|
+
this.computedIframeDisplay(state, this.attributes);
|
429
|
+
}
|
538
430
|
};
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
}
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
431
|
+
|
432
|
+
private computedStyle(_state: DisplayerState): void {
|
433
|
+
const cameraState = this.manager.cameraState;
|
434
|
+
const setWidth = this.attributes.width || 1280;
|
435
|
+
const setHeight = this.attributes.height || 720;
|
436
|
+
if (this.iframe) {
|
437
|
+
const { width, height, scale, centerX, centerY } = cameraState;
|
438
|
+
const rootRect = this.rootRect || { x: 0, y: 0 };
|
439
|
+
const transformOriginX = `${width / 2 + rootRect.x}px`;
|
440
|
+
const transformOriginY = `${height / 2 + rootRect.y}px`;
|
441
|
+
const transformOrigin = `transform-origin: ${transformOriginX} ${transformOriginY};`;
|
442
|
+
const iframeXDiff = ((width - setWidth) / 2) * scale;
|
443
|
+
const iframeYDiff = ((height - setHeight) / 2) * scale;
|
444
|
+
const x = -(centerX * scale) + iframeXDiff;
|
445
|
+
const y = -(centerY * scale) + iframeYDiff;
|
446
|
+
const transform = `transform: translate(${x}px,${y}px) scale(${scale}, ${scale});`;
|
447
|
+
const position = "position: absolute;";
|
448
|
+
// 在某些安卓机型, border-width 不为 0 时,才能正确计算 iframe 里嵌套 iframe 的大小
|
449
|
+
const borderWidth = "border: 0.1px solid rgba(0,0,0,0);";
|
450
|
+
const left = `left: 0px;`;
|
451
|
+
const top = `top: 0px;`;
|
452
|
+
const cssList = [position, borderWidth, top, left, transformOrigin, transform];
|
453
|
+
this.cssList = cssList;
|
454
|
+
this.computedZindex();
|
455
|
+
this.updateStyle();
|
456
|
+
}
|
457
|
+
}
|
458
|
+
|
459
|
+
private computedIframeDisplay(
|
460
|
+
_state: DisplayerState,
|
461
|
+
_attributes: Partial<IframeBridgeAttributes>
|
462
|
+
): void {
|
463
|
+
if (this.inDisplaySceneDir) {
|
464
|
+
IframeBridge.emitter.emit(IframeEvents.DispayIframe);
|
465
|
+
} else {
|
466
|
+
IframeBridge.emitter.emit(IframeEvents.HideIframe);
|
467
|
+
}
|
468
|
+
}
|
469
|
+
|
470
|
+
public computedZindex(): void {
|
471
|
+
const zIndexString = "z-index: -1;";
|
472
|
+
const index = this.cssList.findIndex(css => css === zIndexString);
|
473
|
+
if (index !== -1) {
|
474
|
+
this.cssList.splice(index, 1);
|
475
|
+
}
|
476
|
+
if (!this.isClicker() || this.isDisableInput) {
|
477
|
+
this.cssList.push(zIndexString);
|
478
|
+
}
|
479
|
+
}
|
480
|
+
|
481
|
+
private updateStyle(): void {
|
482
|
+
this.iframe.style.cssText = this.cssList.join(" ");
|
483
|
+
}
|
484
|
+
|
485
|
+
private get iframeOrigin(): string | undefined {
|
486
|
+
if (this.iframe) {
|
487
|
+
try {
|
488
|
+
return new URL(this.iframe.src).origin;
|
489
|
+
} catch (err) {
|
490
|
+
console.warn(err);
|
491
|
+
}
|
492
|
+
}
|
493
|
+
}
|
494
|
+
|
495
|
+
private messageListener(event: MessageEvent): void {
|
496
|
+
log("<<<", JSON.stringify(event.data));
|
497
|
+
if (event.origin !== this.iframeOrigin) {
|
498
|
+
return;
|
499
|
+
}
|
500
|
+
const data = event.data;
|
501
|
+
switch (data.kind) {
|
502
|
+
case IframeEvents.SetAttributes: {
|
503
|
+
this.handleSetAttributes(data);
|
504
|
+
break;
|
505
|
+
}
|
506
|
+
case IframeEvents.RegisterMagixEvent: {
|
507
|
+
this.handleRegisterMagixEvent(data);
|
508
|
+
break;
|
509
|
+
}
|
510
|
+
case IframeEvents.RemoveMagixEvent: {
|
511
|
+
this.handleRemoveMagixEvent(data);
|
512
|
+
break;
|
513
|
+
}
|
514
|
+
case IframeEvents.DispatchMagixEvent: {
|
515
|
+
this.handleDispatchMagixEvent(data);
|
516
|
+
break;
|
517
|
+
}
|
518
|
+
case IframeEvents.RemoveAllMagixEvent: {
|
519
|
+
this.handleRemoveAllMagixEvent();
|
520
|
+
break;
|
521
|
+
}
|
522
|
+
case IframeEvents.NextPage: {
|
523
|
+
this.handleNextPage();
|
524
|
+
break;
|
525
|
+
}
|
526
|
+
case IframeEvents.PrevPage: {
|
527
|
+
this.handlePrevPage();
|
528
|
+
break;
|
529
|
+
}
|
530
|
+
case IframeEvents.SDKCreate: {
|
531
|
+
this.handleSDKCreate();
|
532
|
+
break;
|
533
|
+
}
|
534
|
+
case IframeEvents.SetPage: {
|
535
|
+
this.handleSetPage(data);
|
536
|
+
break;
|
537
|
+
}
|
538
|
+
case IframeEvents.GetAttributes: {
|
539
|
+
this.handleGetAttributes();
|
540
|
+
break;
|
541
|
+
}
|
542
|
+
case IframeEvents.PageTo: {
|
543
|
+
this.handlePageTo(data);
|
544
|
+
break;
|
545
|
+
}
|
546
|
+
default: {
|
547
|
+
log(`${data.kind} not allow event.`);
|
548
|
+
break;
|
549
|
+
}
|
550
|
+
}
|
551
|
+
}
|
552
|
+
|
553
|
+
private handleSDKCreate(): void {
|
554
|
+
this.postMessage({
|
555
|
+
kind: IframeEvents.Init,
|
556
|
+
payload: {
|
557
|
+
attributes: this.attributes,
|
558
|
+
roomState: this.displayer.state,
|
559
|
+
currentPage: this.currentPage,
|
560
|
+
observerId: this.displayer.observerId,
|
561
|
+
},
|
562
|
+
});
|
563
|
+
}
|
564
|
+
|
565
|
+
private handleDispatchMagixEvent(data: any): void {
|
566
|
+
const eventPayload: { event: string; payload: any } = data.payload;
|
567
|
+
this.appManager.safeDispatchMagixEvent(eventPayload.event, eventPayload.payload);
|
568
|
+
}
|
569
|
+
|
570
|
+
private handleSetAttributes(data: any): void {
|
571
|
+
this.setAttributes(data.payload);
|
572
|
+
}
|
573
|
+
|
574
|
+
private handleRegisterMagixEvent(data: any): void {
|
575
|
+
const eventName = data.payload as string;
|
576
|
+
const listener = (event: any) => {
|
577
|
+
if (event.authorId === this.displayer.observerId) {
|
578
|
+
return;
|
579
|
+
}
|
580
|
+
this.postMessage({ kind: IframeEvents.ReciveMagixEvent, payload: event });
|
581
|
+
};
|
582
|
+
this.magixEventMap.set(eventName, listener);
|
583
|
+
this.displayer.addMagixEventListener(eventName, listener);
|
584
|
+
}
|
585
|
+
|
586
|
+
private handleRemoveMagixEvent(data: any): void {
|
587
|
+
const eventName = data.payload as string;
|
588
|
+
const listener = this.magixEventMap.get(eventName);
|
589
|
+
this.displayer.removeMagixEventListener(eventName, listener);
|
590
|
+
}
|
591
|
+
|
592
|
+
private handleNextPage(): void {
|
593
|
+
if (this.manager.canOperate) {
|
594
|
+
this.manager.nextPage();
|
595
|
+
this.dispatchMagixEvent(IframeEvents.NextPage, {});
|
596
|
+
}
|
597
|
+
}
|
598
|
+
|
599
|
+
private handlePrevPage(): void {
|
600
|
+
if (this.manager.canOperate) {
|
601
|
+
this.manager.prevPage();
|
602
|
+
this.dispatchMagixEvent(IframeEvents.PrevPage, {});
|
603
|
+
}
|
604
|
+
}
|
605
|
+
|
606
|
+
private handlePageTo(data: any): void {
|
607
|
+
if (this.manager.canOperate) {
|
608
|
+
const page = data.payload as number;
|
609
|
+
if (!Number.isSafeInteger(page) || page <= 0) {
|
610
|
+
return;
|
611
|
+
}
|
612
|
+
this.manager.setMainViewSceneIndex(page - 1);
|
613
|
+
this.dispatchMagixEvent(IframeEvents.PageTo, page - 1);
|
614
|
+
}
|
615
|
+
}
|
616
|
+
|
617
|
+
private handleRemoveAllMagixEvent(): void {
|
618
|
+
this.magixEventMap.forEach((listener, event) => {
|
619
|
+
this.displayer.removeMagixEventListener(event, listener);
|
620
|
+
});
|
621
|
+
this.magixEventMap.clear();
|
622
|
+
}
|
623
|
+
|
624
|
+
private handleGetAttributes(): void {
|
625
|
+
this.postMessage({
|
626
|
+
kind: IframeEvents.GetAttributes,
|
627
|
+
payload: this.attributes,
|
628
|
+
});
|
629
|
+
}
|
630
|
+
|
631
|
+
public postMessage(message: any): void {
|
632
|
+
if (this.iframe) {
|
633
|
+
this.iframe.contentWindow?.postMessage(JSON.parse(JSON.stringify(message)), "*");
|
634
|
+
}
|
635
|
+
}
|
636
|
+
|
637
|
+
public dispatchMagixEvent(event: string, payload: any): void {
|
638
|
+
if (this.manager.canOperate) {
|
639
|
+
this.setAttributes({ lastEvent: { name: event, payload } });
|
640
|
+
(this.displayer as Room).dispatchMagixEvent(event, payload);
|
641
|
+
}
|
642
|
+
}
|
643
|
+
|
644
|
+
private get currentIndex(): number {
|
645
|
+
return this.manager.mainViewSceneIndex;
|
646
|
+
}
|
647
|
+
|
648
|
+
private get currentPage(): number {
|
649
|
+
return this.currentIndex + 1;
|
650
|
+
}
|
651
|
+
|
652
|
+
private get totalPage(): number {
|
653
|
+
return this.manager.mainViewScenesLength;
|
654
|
+
}
|
655
|
+
|
656
|
+
private get readonly(): boolean {
|
657
|
+
return !(this.displayer as any).isWritable;
|
658
|
+
}
|
659
|
+
|
660
|
+
public get inDisplaySceneDir(): boolean {
|
661
|
+
return this.manager.mainViewSceneDir === this.attributes.displaySceneDir;
|
662
|
+
}
|
663
|
+
|
664
|
+
private isClicker(): boolean {
|
665
|
+
if (this.readonly) {
|
666
|
+
return false;
|
667
|
+
}
|
668
|
+
const currentApplianceName = (this.displayer as Room).state.memberState
|
669
|
+
.currentApplianceName;
|
670
|
+
return this.allowAppliances.includes(currentApplianceName);
|
671
|
+
}
|
672
|
+
|
673
|
+
private get isDisableInput(): boolean {
|
674
|
+
if ("disableDeviceInputs" in this.displayer) {
|
675
|
+
return (this.displayer as Room).disableDeviceInputs;
|
676
|
+
} else {
|
677
|
+
return true;
|
678
|
+
}
|
679
|
+
}
|
636
680
|
}
|