@netless/app-slide 0.2.39 → 0.2.40
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/LICENSE +21 -0
- package/README-zh.md +53 -53
- package/README.md +68 -68
- package/dist/index.d.ts +294 -294
- package/dist/main.cjs.js +4 -4
- package/dist/main.cjs.js.map +1 -1
- package/dist/main.es.js +6 -6
- package/dist/main.es.js.map +1 -1
- package/dist/main.iife.js +4 -4
- package/dist/main.iife.js.map +1 -1
- package/package.json +9 -9
- package/src/DocsViewer/icons/arrow-left.svg +4 -4
- package/src/DocsViewer/icons/arrow-left.ts +18 -18
- package/src/DocsViewer/icons/arrow-right.svg +4 -4
- package/src/DocsViewer/icons/arrow-right.ts +18 -18
- package/src/DocsViewer/icons/pause.svg +4 -4
- package/src/DocsViewer/icons/pause.ts +18 -18
- package/src/DocsViewer/icons/play.svg +4 -4
- package/src/DocsViewer/icons/play.ts +18 -18
- package/src/DocsViewer/icons/sidebar.svg +4 -4
- package/src/DocsViewer/icons/sidebar.ts +18 -18
- package/src/DocsViewer/index.ts +381 -381
- package/src/DocsViewer/style.scss +228 -228
- package/src/DocsViewer/variables.scss +1 -1
- package/src/SlideController/helpers.ts +48 -48
- package/src/SlideController/index.ts +351 -351
- package/src/SlideDocsViewer/style.scss +28 -28
- package/src/SlidePreviewer/index.ts +225 -225
- package/src/index.ts +283 -283
- package/src/style.scss +14 -14
- package/src/typings.ts +21 -21
- package/src/utils/bgcolor.ts +45 -45
- package/src/utils/freezer.ts +116 -116
- package/src/utils/helpers.ts +32 -32
- package/src/utils/logger.ts +106 -106
package/src/utils/bgcolor.ts
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import ColorString from "color-string";
|
|
2
|
-
import { log } from "./logger";
|
|
3
|
-
|
|
4
|
-
export function guessBgColor(el: HTMLElement): string {
|
|
5
|
-
try {
|
|
6
|
-
const bg = window.getComputedStyle(el).backgroundColor;
|
|
7
|
-
if (bg !== "rgba(0, 0, 0, 0)" && bg !== "transparent") {
|
|
8
|
-
return bg;
|
|
9
|
-
}
|
|
10
|
-
if (el.parentElement) {
|
|
11
|
-
return guessBgColor(el.parentElement);
|
|
12
|
-
}
|
|
13
|
-
} catch {
|
|
14
|
-
// ignore any error
|
|
15
|
-
}
|
|
16
|
-
return "#ffffff";
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function toHex(color: string): string {
|
|
20
|
-
const result = ColorString.get(color);
|
|
21
|
-
if (result && result.model === "rgb") {
|
|
22
|
-
const args = result.value;
|
|
23
|
-
|
|
24
|
-
// https://github.com/Qix-/color-convert/blob/8dfdbbc6b46fa6a305bf394d942cc1b08e23fca5/conversions.js#L616
|
|
25
|
-
const integer =
|
|
26
|
-
((Math.round(args[0]) & 0xff) << 16) +
|
|
27
|
-
((Math.round(args[1]) & 0xff) << 8) +
|
|
28
|
-
(Math.round(args[2]) & 0xff);
|
|
29
|
-
|
|
30
|
-
const string = integer.toString(16);
|
|
31
|
-
return "#" + "000000".substring(string.length) + string;
|
|
32
|
-
} else {
|
|
33
|
-
return color;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let cachedBgColor = "";
|
|
38
|
-
|
|
39
|
-
export function cachedGetBgColor(el: HTMLElement): string {
|
|
40
|
-
if (!cachedBgColor) {
|
|
41
|
-
cachedBgColor = toHex(guessBgColor(el));
|
|
42
|
-
log("[Slide] guess bg color", cachedBgColor);
|
|
43
|
-
}
|
|
44
|
-
return cachedBgColor;
|
|
45
|
-
}
|
|
1
|
+
import ColorString from "color-string";
|
|
2
|
+
import { log } from "./logger";
|
|
3
|
+
|
|
4
|
+
export function guessBgColor(el: HTMLElement): string {
|
|
5
|
+
try {
|
|
6
|
+
const bg = window.getComputedStyle(el).backgroundColor;
|
|
7
|
+
if (bg !== "rgba(0, 0, 0, 0)" && bg !== "transparent") {
|
|
8
|
+
return bg;
|
|
9
|
+
}
|
|
10
|
+
if (el.parentElement) {
|
|
11
|
+
return guessBgColor(el.parentElement);
|
|
12
|
+
}
|
|
13
|
+
} catch {
|
|
14
|
+
// ignore any error
|
|
15
|
+
}
|
|
16
|
+
return "#ffffff";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function toHex(color: string): string {
|
|
20
|
+
const result = ColorString.get(color);
|
|
21
|
+
if (result && result.model === "rgb") {
|
|
22
|
+
const args = result.value;
|
|
23
|
+
|
|
24
|
+
// https://github.com/Qix-/color-convert/blob/8dfdbbc6b46fa6a305bf394d942cc1b08e23fca5/conversions.js#L616
|
|
25
|
+
const integer =
|
|
26
|
+
((Math.round(args[0]) & 0xff) << 16) +
|
|
27
|
+
((Math.round(args[1]) & 0xff) << 8) +
|
|
28
|
+
(Math.round(args[2]) & 0xff);
|
|
29
|
+
|
|
30
|
+
const string = integer.toString(16);
|
|
31
|
+
return "#" + "000000".substring(string.length) + string;
|
|
32
|
+
} else {
|
|
33
|
+
return color;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let cachedBgColor = "";
|
|
38
|
+
|
|
39
|
+
export function cachedGetBgColor(el: HTMLElement): string {
|
|
40
|
+
if (!cachedBgColor) {
|
|
41
|
+
cachedBgColor = toHex(guessBgColor(el));
|
|
42
|
+
log("[Slide] guess bg color", cachedBgColor);
|
|
43
|
+
}
|
|
44
|
+
return cachedBgColor;
|
|
45
|
+
}
|
package/src/utils/freezer.ts
CHANGED
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
import type { ReadonlyTeleBox, RegisterParams } from "@netless/window-manager";
|
|
2
|
-
import { log } from "./logger";
|
|
3
|
-
|
|
4
|
-
export interface FreezableSlide {
|
|
5
|
-
freeze: () => void;
|
|
6
|
-
unfreeze: () => void;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export let useFreezer = false;
|
|
10
|
-
export let FreezerLength = 2;
|
|
11
|
-
|
|
12
|
-
export function getFreezerLength() {
|
|
13
|
-
return FreezerLength;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** @param length - must be >= 1, default is 2 */
|
|
17
|
-
export function setFreezerLength(length: number) {
|
|
18
|
-
FreezerLength = length < 1 ? Infinity : length;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const inspect = (arr: string[]) => {
|
|
22
|
-
return "[" + arr + "]";
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export const apps = {
|
|
26
|
-
map: new Map<string, FreezableSlide>(),
|
|
27
|
-
boxes: new Map<string, ReadonlyTeleBox>(),
|
|
28
|
-
queue: [] as string[],
|
|
29
|
-
validateQueue() {
|
|
30
|
-
// queue = [5, 4, 3, 2, 1]
|
|
31
|
-
this.queue.sort((a, b) => {
|
|
32
|
-
const za = this.boxes.get(a)?.zIndex ?? 0;
|
|
33
|
-
const zb = this.boxes.get(b)?.zIndex ?? 0;
|
|
34
|
-
return -(za - zb);
|
|
35
|
-
});
|
|
36
|
-
log("[Slide] freezer: validate", inspect(this.queue));
|
|
37
|
-
while (this.queue.length > FreezerLength) {
|
|
38
|
-
const appId = this.queue.pop() as string;
|
|
39
|
-
const slide = this.map.get(appId);
|
|
40
|
-
if (slide) {
|
|
41
|
-
log("[Slide] freezer: validate-freeze", appId, inspect(this.queue));
|
|
42
|
-
slide.freeze();
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
set(appId: string, slide: FreezableSlide, box: ReadonlyTeleBox) {
|
|
47
|
-
log("[Slide] freezer: add", appId, inspect(this.queue));
|
|
48
|
-
this.map.set(appId, slide);
|
|
49
|
-
this.boxes.set(appId, box);
|
|
50
|
-
if (!this.queue.includes(appId)) {
|
|
51
|
-
this.queue.unshift(appId);
|
|
52
|
-
}
|
|
53
|
-
this.validateQueue();
|
|
54
|
-
},
|
|
55
|
-
delete(appId: string) {
|
|
56
|
-
this.map.delete(appId);
|
|
57
|
-
this.boxes.delete(appId);
|
|
58
|
-
this.queue = this.queue.filter(id => id !== appId);
|
|
59
|
-
log("[Slide] freezer: delete", appId, inspect(this.queue));
|
|
60
|
-
},
|
|
61
|
-
focus(appId: string) {
|
|
62
|
-
const slide = this.map.get(appId);
|
|
63
|
-
const index = this.queue.indexOf(appId);
|
|
64
|
-
if (index > -1) {
|
|
65
|
-
this.queue.splice(index, 1);
|
|
66
|
-
}
|
|
67
|
-
this.queue.unshift(appId);
|
|
68
|
-
this.validateQueue();
|
|
69
|
-
log("[Slide] freezer: focus", appId, inspect(this.queue));
|
|
70
|
-
if (slide) {
|
|
71
|
-
slide.unfreeze();
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
if (import.meta.env.DEV) {
|
|
77
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
78
|
-
(window as any).freezer = apps;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const on_created_callbacks = new Set<(appId: string) => void>();
|
|
82
|
-
|
|
83
|
-
export function onCreated(callback: (appId: string) => void) {
|
|
84
|
-
on_created_callbacks.add(callback);
|
|
85
|
-
return () => on_created_callbacks.delete(callback);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const on_destroyed_callbacks = new Set<(appId: string) => void>();
|
|
89
|
-
|
|
90
|
-
export function onDestroyed(callback: (appId: string) => void) {
|
|
91
|
-
on_destroyed_callbacks.add(callback);
|
|
92
|
-
return () => on_destroyed_callbacks.delete(callback);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export type AddHooks = NonNullable<RegisterParams["addHooks"]>;
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Put this function in your register code:
|
|
99
|
-
* `WindowManager.register({ kind: 'Slide', src: SlideApp, addHooks })`
|
|
100
|
-
* So that it will automatically freeze the app when it is not in focus.
|
|
101
|
-
*/
|
|
102
|
-
export const addHooks: AddHooks = emitter => {
|
|
103
|
-
useFreezer = true;
|
|
104
|
-
|
|
105
|
-
emitter.on("focus", ({ appId }) => {
|
|
106
|
-
apps.focus(appId);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
emitter.on("created", ({ appId }) => {
|
|
110
|
-
on_created_callbacks.forEach(callback => callback(appId));
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
emitter.on("destroy", ({ appId }) => {
|
|
114
|
-
on_destroyed_callbacks.forEach(callback => callback(appId));
|
|
115
|
-
});
|
|
116
|
-
};
|
|
1
|
+
import type { ReadonlyTeleBox, RegisterParams } from "@netless/window-manager";
|
|
2
|
+
import { log } from "./logger";
|
|
3
|
+
|
|
4
|
+
export interface FreezableSlide {
|
|
5
|
+
freeze: () => void;
|
|
6
|
+
unfreeze: () => void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export let useFreezer = false;
|
|
10
|
+
export let FreezerLength = 2;
|
|
11
|
+
|
|
12
|
+
export function getFreezerLength() {
|
|
13
|
+
return FreezerLength;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** @param length - must be >= 1, default is 2 */
|
|
17
|
+
export function setFreezerLength(length: number) {
|
|
18
|
+
FreezerLength = length < 1 ? Infinity : length;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const inspect = (arr: string[]) => {
|
|
22
|
+
return "[" + arr + "]";
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const apps = {
|
|
26
|
+
map: new Map<string, FreezableSlide>(),
|
|
27
|
+
boxes: new Map<string, ReadonlyTeleBox>(),
|
|
28
|
+
queue: [] as string[],
|
|
29
|
+
validateQueue() {
|
|
30
|
+
// queue = [5, 4, 3, 2, 1]
|
|
31
|
+
this.queue.sort((a, b) => {
|
|
32
|
+
const za = this.boxes.get(a)?.zIndex ?? 0;
|
|
33
|
+
const zb = this.boxes.get(b)?.zIndex ?? 0;
|
|
34
|
+
return -(za - zb);
|
|
35
|
+
});
|
|
36
|
+
log("[Slide] freezer: validate", inspect(this.queue));
|
|
37
|
+
while (this.queue.length > FreezerLength) {
|
|
38
|
+
const appId = this.queue.pop() as string;
|
|
39
|
+
const slide = this.map.get(appId);
|
|
40
|
+
if (slide) {
|
|
41
|
+
log("[Slide] freezer: validate-freeze", appId, inspect(this.queue));
|
|
42
|
+
slide.freeze();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
set(appId: string, slide: FreezableSlide, box: ReadonlyTeleBox) {
|
|
47
|
+
log("[Slide] freezer: add", appId, inspect(this.queue));
|
|
48
|
+
this.map.set(appId, slide);
|
|
49
|
+
this.boxes.set(appId, box);
|
|
50
|
+
if (!this.queue.includes(appId)) {
|
|
51
|
+
this.queue.unshift(appId);
|
|
52
|
+
}
|
|
53
|
+
this.validateQueue();
|
|
54
|
+
},
|
|
55
|
+
delete(appId: string) {
|
|
56
|
+
this.map.delete(appId);
|
|
57
|
+
this.boxes.delete(appId);
|
|
58
|
+
this.queue = this.queue.filter(id => id !== appId);
|
|
59
|
+
log("[Slide] freezer: delete", appId, inspect(this.queue));
|
|
60
|
+
},
|
|
61
|
+
focus(appId: string) {
|
|
62
|
+
const slide = this.map.get(appId);
|
|
63
|
+
const index = this.queue.indexOf(appId);
|
|
64
|
+
if (index > -1) {
|
|
65
|
+
this.queue.splice(index, 1);
|
|
66
|
+
}
|
|
67
|
+
this.queue.unshift(appId);
|
|
68
|
+
this.validateQueue();
|
|
69
|
+
log("[Slide] freezer: focus", appId, inspect(this.queue));
|
|
70
|
+
if (slide) {
|
|
71
|
+
slide.unfreeze();
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
if (import.meta.env.DEV) {
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
78
|
+
(window as any).freezer = apps;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const on_created_callbacks = new Set<(appId: string) => void>();
|
|
82
|
+
|
|
83
|
+
export function onCreated(callback: (appId: string) => void) {
|
|
84
|
+
on_created_callbacks.add(callback);
|
|
85
|
+
return () => on_created_callbacks.delete(callback);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const on_destroyed_callbacks = new Set<(appId: string) => void>();
|
|
89
|
+
|
|
90
|
+
export function onDestroyed(callback: (appId: string) => void) {
|
|
91
|
+
on_destroyed_callbacks.add(callback);
|
|
92
|
+
return () => on_destroyed_callbacks.delete(callback);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type AddHooks = NonNullable<RegisterParams["addHooks"]>;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Put this function in your register code:
|
|
99
|
+
* `WindowManager.register({ kind: 'Slide', src: SlideApp, addHooks })`
|
|
100
|
+
* So that it will automatically freeze the app when it is not in focus.
|
|
101
|
+
*/
|
|
102
|
+
export const addHooks: AddHooks = emitter => {
|
|
103
|
+
useFreezer = true;
|
|
104
|
+
|
|
105
|
+
emitter.on("focus", ({ appId }) => {
|
|
106
|
+
apps.focus(appId);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
emitter.on("created", ({ appId }) => {
|
|
110
|
+
on_created_callbacks.forEach(callback => callback(appId));
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
emitter.on("destroy", ({ appId }) => {
|
|
114
|
+
on_destroyed_callbacks.forEach(callback => callback(appId));
|
|
115
|
+
});
|
|
116
|
+
};
|
package/src/utils/helpers.ts
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
export function clamp(value: number, min: number, max: number): number {
|
|
2
|
-
return Math.min(Math.max(value, min), max);
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export function flattenEvent(ev: MouseEvent | TouchEvent): MouseEvent | Touch {
|
|
6
|
-
return (ev as TouchEvent).touches ? (ev as TouchEvent).touches[0] : (ev as MouseEvent);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function preventEvent(ev: Event): void {
|
|
10
|
-
ev.stopPropagation();
|
|
11
|
-
if (ev.cancelable) {
|
|
12
|
-
ev.preventDefault();
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function isObj(obj: unknown) {
|
|
17
|
-
return typeof obj === "object" && obj !== null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function wait(ms: number) {
|
|
21
|
-
return new Promise<void>(resolve => setTimeout(resolve, ms));
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function deepClone<T>(obj: T): T {
|
|
25
|
-
return JSON.parse(JSON.stringify(obj));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function isEditable(el: EventTarget | null) {
|
|
29
|
-
if (!el) return false;
|
|
30
|
-
const tagName = (el as HTMLElement).tagName;
|
|
31
|
-
return tagName === "INPUT" || tagName === "TEXTAREA" || tagName === "SELECT";
|
|
32
|
-
}
|
|
1
|
+
export function clamp(value: number, min: number, max: number): number {
|
|
2
|
+
return Math.min(Math.max(value, min), max);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function flattenEvent(ev: MouseEvent | TouchEvent): MouseEvent | Touch {
|
|
6
|
+
return (ev as TouchEvent).touches ? (ev as TouchEvent).touches[0] : (ev as MouseEvent);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function preventEvent(ev: Event): void {
|
|
10
|
+
ev.stopPropagation();
|
|
11
|
+
if (ev.cancelable) {
|
|
12
|
+
ev.preventDefault();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isObj(obj: unknown) {
|
|
17
|
+
return typeof obj === "object" && obj !== null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function wait(ms: number) {
|
|
21
|
+
return new Promise<void>(resolve => setTimeout(resolve, ms));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function deepClone<T>(obj: T): T {
|
|
25
|
+
return JSON.parse(JSON.stringify(obj));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function isEditable(el: EventTarget | null) {
|
|
29
|
+
if (!el) return false;
|
|
30
|
+
const tagName = (el as HTMLElement).tagName;
|
|
31
|
+
return tagName === "INPUT" || tagName === "TEXTAREA" || tagName === "SELECT";
|
|
32
|
+
}
|
package/src/utils/logger.ts
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import type { AppContext, Displayer } from "@netless/window-manager";
|
|
3
|
-
import type { SlideController } from "../SlideController";
|
|
4
|
-
import { isObj } from "./helpers";
|
|
5
|
-
|
|
6
|
-
type DebugMessage = { slide: boolean | "__instance" | "__debug" };
|
|
7
|
-
|
|
8
|
-
type RoomLogger = {
|
|
9
|
-
debug: (...args: any) => void;
|
|
10
|
-
info: (...args: any) => void;
|
|
11
|
-
warn: (...args: any) => void;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
class Logger {
|
|
15
|
-
public apps: Record<
|
|
16
|
-
string,
|
|
17
|
-
{ context?: AppContext | null; controller?: SlideController | null }
|
|
18
|
-
> = {};
|
|
19
|
-
|
|
20
|
-
setAppContext(appId: string, context: AppContext | null) {
|
|
21
|
-
(this.apps[appId] ||= {}).context = context;
|
|
22
|
-
this.log(`[Slide] new ${appId}`);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
setAppController(appId: string, controller: SlideController | null) {
|
|
26
|
-
(this.apps[appId] ||= {}).controller = controller;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
deleteApp(appId: string) {
|
|
30
|
-
delete this.apps[appId];
|
|
31
|
-
this.log(`[Slide] delete ${appId}`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public level: "debug" | "verbose" = import.meta.env.DEV ? "verbose" : "debug";
|
|
35
|
-
|
|
36
|
-
public roomLogger: RoomLogger | null = null;
|
|
37
|
-
|
|
38
|
-
constructor(public enable: boolean) {
|
|
39
|
-
this.initialize();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
initialize() {
|
|
43
|
-
window.addEventListener("message", this._onMessage);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
dispose() {
|
|
47
|
-
window.removeEventListener("message", this._onMessage);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
log(...args: any[]) {
|
|
51
|
-
if (this.roomLogger) {
|
|
52
|
-
this.roomLogger.info(...args);
|
|
53
|
-
} else if (this.enable) {
|
|
54
|
-
console.log(...args);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
verbose(...args: any[]) {
|
|
59
|
-
if (this.roomLogger) {
|
|
60
|
-
this.roomLogger.debug(...args);
|
|
61
|
-
} else if (this.enable && this.level === "verbose") {
|
|
62
|
-
console.log(...args);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
warn(...args: any[]) {
|
|
67
|
-
if (this.roomLogger) {
|
|
68
|
-
this.roomLogger.warn(...args);
|
|
69
|
-
} else {
|
|
70
|
-
console.warn(...args);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* @example
|
|
76
|
-
* window.postMessage({ slide: '__debug' })
|
|
77
|
-
* window.dispatchEvent(new MessageEvent('message', { data: { slide: '__debug' } }))
|
|
78
|
-
*/
|
|
79
|
-
_onMessage = (ev: MessageEvent<DebugMessage> | CustomEvent<DebugMessage>) => {
|
|
80
|
-
let data: DebugMessage | undefined;
|
|
81
|
-
if (ev instanceof CustomEvent) {
|
|
82
|
-
data = ev.detail;
|
|
83
|
-
} else if (isObj(ev.data)) {
|
|
84
|
-
data = ev.data;
|
|
85
|
-
}
|
|
86
|
-
if (data) {
|
|
87
|
-
if (typeof data.slide === "boolean") {
|
|
88
|
-
this.enable = data.slide;
|
|
89
|
-
} else if (data.slide === "__instance") {
|
|
90
|
-
console.log(this);
|
|
91
|
-
} else if (data.slide === "__debug") {
|
|
92
|
-
Object.values(this.apps).forEach(app => {
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
-
(app.controller?.slide as any)?.createController();
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export const logger = /** @__PURE__ */ new Logger(import.meta.env.DEV);
|
|
102
|
-
export const log = /** @__PURE__ */ logger.log.bind(logger);
|
|
103
|
-
export const verbose = /** @__PURE__ */ logger.verbose.bind(logger);
|
|
104
|
-
export const setRoomLogger = (displayer: Displayer) => {
|
|
105
|
-
logger.roomLogger = (displayer as any).logger;
|
|
106
|
-
};
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import type { AppContext, Displayer } from "@netless/window-manager";
|
|
3
|
+
import type { SlideController } from "../SlideController";
|
|
4
|
+
import { isObj } from "./helpers";
|
|
5
|
+
|
|
6
|
+
type DebugMessage = { slide: boolean | "__instance" | "__debug" };
|
|
7
|
+
|
|
8
|
+
type RoomLogger = {
|
|
9
|
+
debug: (...args: any) => void;
|
|
10
|
+
info: (...args: any) => void;
|
|
11
|
+
warn: (...args: any) => void;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
class Logger {
|
|
15
|
+
public apps: Record<
|
|
16
|
+
string,
|
|
17
|
+
{ context?: AppContext | null; controller?: SlideController | null }
|
|
18
|
+
> = {};
|
|
19
|
+
|
|
20
|
+
setAppContext(appId: string, context: AppContext | null) {
|
|
21
|
+
(this.apps[appId] ||= {}).context = context;
|
|
22
|
+
this.log(`[Slide] new ${appId}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
setAppController(appId: string, controller: SlideController | null) {
|
|
26
|
+
(this.apps[appId] ||= {}).controller = controller;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
deleteApp(appId: string) {
|
|
30
|
+
delete this.apps[appId];
|
|
31
|
+
this.log(`[Slide] delete ${appId}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public level: "debug" | "verbose" = import.meta.env.DEV ? "verbose" : "debug";
|
|
35
|
+
|
|
36
|
+
public roomLogger: RoomLogger | null = null;
|
|
37
|
+
|
|
38
|
+
constructor(public enable: boolean) {
|
|
39
|
+
this.initialize();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
initialize() {
|
|
43
|
+
window.addEventListener("message", this._onMessage);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
dispose() {
|
|
47
|
+
window.removeEventListener("message", this._onMessage);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
log(...args: any[]) {
|
|
51
|
+
if (this.roomLogger) {
|
|
52
|
+
this.roomLogger.info(...args);
|
|
53
|
+
} else if (this.enable) {
|
|
54
|
+
console.log(...args);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
verbose(...args: any[]) {
|
|
59
|
+
if (this.roomLogger) {
|
|
60
|
+
this.roomLogger.debug(...args);
|
|
61
|
+
} else if (this.enable && this.level === "verbose") {
|
|
62
|
+
console.log(...args);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
warn(...args: any[]) {
|
|
67
|
+
if (this.roomLogger) {
|
|
68
|
+
this.roomLogger.warn(...args);
|
|
69
|
+
} else {
|
|
70
|
+
console.warn(...args);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @example
|
|
76
|
+
* window.postMessage({ slide: '__debug' })
|
|
77
|
+
* window.dispatchEvent(new MessageEvent('message', { data: { slide: '__debug' } }))
|
|
78
|
+
*/
|
|
79
|
+
_onMessage = (ev: MessageEvent<DebugMessage> | CustomEvent<DebugMessage>) => {
|
|
80
|
+
let data: DebugMessage | undefined;
|
|
81
|
+
if (ev instanceof CustomEvent) {
|
|
82
|
+
data = ev.detail;
|
|
83
|
+
} else if (isObj(ev.data)) {
|
|
84
|
+
data = ev.data;
|
|
85
|
+
}
|
|
86
|
+
if (data) {
|
|
87
|
+
if (typeof data.slide === "boolean") {
|
|
88
|
+
this.enable = data.slide;
|
|
89
|
+
} else if (data.slide === "__instance") {
|
|
90
|
+
console.log(this);
|
|
91
|
+
} else if (data.slide === "__debug") {
|
|
92
|
+
Object.values(this.apps).forEach(app => {
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
+
(app.controller?.slide as any)?.createController();
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export const logger = /** @__PURE__ */ new Logger(import.meta.env.DEV);
|
|
102
|
+
export const log = /** @__PURE__ */ logger.log.bind(logger);
|
|
103
|
+
export const verbose = /** @__PURE__ */ logger.verbose.bind(logger);
|
|
104
|
+
export const setRoomLogger = (displayer: Displayer) => {
|
|
105
|
+
logger.roomLogger = (displayer as any).logger;
|
|
106
|
+
};
|