@netless/telebox-insider 0.2.26 → 1.0.0-alpha.3

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.
Files changed (48) hide show
  1. package/dist/TeleBox/constants.d.ts +0 -3
  2. package/dist/TeleBox/index.d.ts +118 -97
  3. package/dist/TeleBox/typings.d.ts +42 -55
  4. package/dist/TeleBoxCollector/index.d.ts +25 -29
  5. package/dist/TeleBoxManager/MaxTitleBar/index.d.ts +14 -18
  6. package/dist/TeleBoxManager/constants.d.ts +0 -3
  7. package/dist/TeleBoxManager/index.d.ts +64 -19
  8. package/dist/TeleBoxManager/typings.d.ts +35 -28
  9. package/dist/TeleStage/index.d.ts +21 -0
  10. package/dist/TeleTitleBar/DefaultTitleBar/index.d.ts +6 -8
  11. package/dist/TeleTitleBar/typings.d.ts +3 -5
  12. package/dist/index.d.ts +0 -1
  13. package/dist/style.css +1 -1
  14. package/dist/telebox-insider.cjs.js +1 -15
  15. package/dist/telebox-insider.cjs.js.map +1 -1
  16. package/dist/telebox-insider.es.js +1 -15
  17. package/dist/telebox-insider.es.js.map +1 -1
  18. package/dist/utils.d.ts +0 -1
  19. package/package.json +7 -19
  20. package/src/TeleBox/constants.ts +0 -3
  21. package/src/TeleBox/index.ts +500 -690
  22. package/src/TeleBox/style.scss +1 -0
  23. package/src/TeleBox/typings.ts +42 -75
  24. package/src/TeleBoxCollector/index.ts +136 -159
  25. package/src/TeleBoxManager/MaxTitleBar/index.ts +95 -108
  26. package/src/TeleBoxManager/constants.ts +0 -3
  27. package/src/TeleBoxManager/index.ts +325 -336
  28. package/src/TeleBoxManager/typings.ts +42 -52
  29. package/src/TeleStage/index.ts +178 -0
  30. package/src/TeleStage/style.scss +49 -0
  31. package/src/TeleTitleBar/DefaultTitleBar/index.ts +48 -61
  32. package/src/TeleTitleBar/typings.ts +3 -9
  33. package/src/index.ts +0 -1
  34. package/src/utils.ts +0 -6
  35. package/dist/TeleBoxCollector/TeleBoxCollector.stories.d.ts +0 -5
  36. package/dist/TeleBoxManager/MaxTitleBar/MaxTitleBar.stories.d.ts +0 -7
  37. package/dist/TeleBoxManager/MaxTitleBar/MaxTitleBarReact.d.ts +0 -4
  38. package/dist/TeleBoxReact/TeleBox.stories.d.ts +0 -9
  39. package/dist/TeleBoxReact/index.d.ts +0 -17
  40. package/dist/TeleBoxReact/utils.d.ts +0 -2
  41. package/dist/TeleTitleBar/DefaultTitleBar/DefaultTitleBar.stories.d.ts +0 -4
  42. package/src/TeleBoxCollector/TeleBoxCollector.stories.tsx +0 -23
  43. package/src/TeleBoxManager/MaxTitleBar/MaxTitleBar.stories.tsx +0 -66
  44. package/src/TeleBoxManager/MaxTitleBar/MaxTitleBarReact.tsx +0 -34
  45. package/src/TeleBoxReact/TeleBox.stories.tsx +0 -74
  46. package/src/TeleBoxReact/index.tsx +0 -175
  47. package/src/TeleBoxReact/utils.ts +0 -14
  48. package/src/TeleTitleBar/DefaultTitleBar/DefaultTitleBar.stories.tsx +0 -15
@@ -37,6 +37,7 @@
37
37
  display: flex;
38
38
  justify-content: center;
39
39
  align-items: center;
40
+ position: relative;
40
41
  }
41
42
 
42
43
  .#{$namespace}-content {
@@ -1,4 +1,5 @@
1
- import type EventEmitter from "eventemitter3";
1
+ import type Emittery from "emittery";
2
+ import type { ReadonlyVal } from "value-enhancer";
2
3
  import type { TeleTitleBar } from "../TeleTitleBar";
3
4
  import type {
4
5
  TeleBoxDragHandleType,
@@ -29,40 +30,28 @@ export interface TeleBoxConfig {
29
30
  readonly id?: string;
30
31
  /** Box title. Default empty. */
31
32
  readonly title?: string;
32
- /** Prefers Box color scheme. Default light. */
33
- readonly prefersColorScheme?: TeleBoxColorScheme;
34
- /** Actual Box Dark Mode */
35
- readonly darkMode?: boolean;
36
33
  /** Box visible. Default true. */
37
34
  readonly visible?: boolean;
38
- /** Box width relative to root element. 0~1. Default 0.5. */
35
+ /** Box width relative to stage area. 0~1. Default 0.5. */
39
36
  readonly width?: number;
40
- /** Box height relative to root element. 0~1. Default 0.5. */
37
+ /** Box height relative to stage area. 0~1. Default 0.5. */
41
38
  readonly height?: number;
42
- /** Minimum box width relative to root element. 0~1. Default 0. */
39
+ /** Minimum box width relative to stage area. 0~1. Default 0. */
43
40
  readonly minWidth?: number;
44
- /** Minimum box height relative to root element. 0~1. Default 0. */
41
+ /** Minimum box height relative to stage area. 0~1. Default 0. */
45
42
  readonly minHeight?: number;
46
- /** x position relative to root element. 0~1. Default 0.1. */
43
+ /** x position relative to stage area. 0~1. Default 0.1. */
47
44
  readonly x?: number;
48
- /** y position relative to root element. 0~1. Default 0.1. */
45
+ /** y position relative to stage area. 0~1. Default 0.1. */
49
46
  readonly y?: number;
50
- /** Maximize box. Default false. */
51
- readonly maximized?: boolean;
52
- /** Minimize box. Overwrites maximized state. Default false. */
53
- readonly minimized?: boolean;
54
47
  /** The initial state of the box. Default normal. */
55
48
  readonly state?: TeleBoxState;
56
- /** Is box readonly */
57
- readonly readonly?: boolean;
58
49
  /** Able to resize box window. Default true. */
59
50
  readonly resizable?: boolean;
60
51
  /** Able to drag box window Default true. */
61
52
  readonly draggable?: boolean;
62
- /** Restrict box to always be within the containing area. Default true. */
63
- readonly fence?: boolean;
64
- /** Fixed width/height ratio for box window. Default false. */
65
- readonly fixRatio?: boolean;
53
+ /** Fixed height/width ratio for box window. No ratio limit if <= 0. Default -1. */
54
+ readonly ratio?: number;
66
55
  /** Box focused. */
67
56
  readonly focus?: boolean;
68
57
  /** Base z-index for box. */
@@ -77,10 +66,30 @@ export interface TeleBoxConfig {
77
66
  readonly footer?: HTMLElement;
78
67
  /** Box content styles. */
79
68
  readonly styles?: HTMLStyleElement;
80
- /** Position and dimension of container */
81
- readonly containerRect?: TeleBoxRect;
69
+ /** Show stage frame and grey-out the non-stage area. Inherit setting from manager if null. Default null. */
70
+ readonly highlightStage?: boolean | null;
71
+ /** Actual Box Dark Mode */
72
+ readonly darkMode$: ReadonlyVal<boolean, boolean>;
73
+ /** Restrict box to always be within the stage area. Default false. */
74
+ readonly fence$: ReadonlyVal<boolean, boolean>;
75
+ /** Maximize box. Default false. */
76
+ readonly maximized$: ReadonlyVal<boolean, boolean>;
77
+ /** Minimize box. Overwrites maximized state. Default false. */
78
+ readonly minimized$: ReadonlyVal<boolean, boolean>;
79
+ /** Is box readonly */
80
+ readonly readonly$: ReadonlyVal<boolean, boolean>;
81
+ /** Root element to mount boxes */
82
+ readonly root$: ReadonlyVal<HTMLElement | null>;
83
+ /** Position and dimension of root element */
84
+ readonly rootRect$: ReadonlyVal<TeleBoxRect>;
85
+ /** Position and dimension of stage area */
86
+ readonly managerStageRect$: ReadonlyVal<TeleBoxRect>;
87
+ /** Stage area height/with ratio. No ratio if <= 0. */
88
+ readonly stageRatio$: ReadonlyVal<number, boolean>;
82
89
  /** Position and dimension of collector */
83
- readonly collectorRect?: TeleBoxRect;
90
+ readonly collectorRect$: ReadonlyVal<TeleBoxRect | undefined>;
91
+ /** highlight stage settings from manager */
92
+ readonly managerHighlightStage$: ReadonlyVal<boolean>;
84
93
  }
85
94
 
86
95
  type CheckTeleBoxConfig<T extends Record<`${TELE_BOX_EVENT}`, any>> = T;
@@ -88,44 +97,22 @@ type CheckTeleBoxConfig<T extends Record<`${TELE_BOX_EVENT}`, any>> = T;
88
97
  export type TeleBoxEventConfig = CheckTeleBoxConfig<{
89
98
  dark_mode: boolean;
90
99
  prefers_color_scheme: TeleBoxColorScheme;
91
- close: void;
92
- focus: void;
93
- blur: void;
94
- move: { x: number; y: number };
95
- resize: { width: number; height: number };
100
+ close: undefined;
101
+ focus: undefined;
102
+ blur: undefined;
96
103
  intrinsic_move: { x: number; y: number };
97
104
  intrinsic_resize: { width: number; height: number };
98
- visual_resize: { width: number; height: number };
99
105
  z_index: number;
100
106
  state: TeleBoxState;
101
107
  minimized: boolean;
102
108
  maximized: boolean;
103
109
  readonly: boolean;
104
- destroyed: void;
110
+ destroyed: undefined;
105
111
  }>;
106
112
 
107
113
  export type TeleBoxEvent = keyof TeleBoxEventConfig;
108
114
 
109
- export interface TeleBoxEvents extends EventEmitter<TeleBoxEvent> {
110
- on<U extends TeleBoxEvent>(
111
- event: U,
112
- listener: (value: TeleBoxEventConfig[U]) => void
113
- ): this;
114
- once<U extends TeleBoxEvent>(
115
- event: U,
116
- listener: (value: TeleBoxEventConfig[U]) => void
117
- ): this;
118
- addListener<U extends TeleBoxEvent>(
119
- event: U,
120
- listener: (value: TeleBoxEventConfig[U]) => void
121
- ): this;
122
- emit<U extends TeleBoxEvent>(
123
- event: U,
124
- ...value: TeleBoxEventConfig[U] extends void
125
- ? []
126
- : [TeleBoxEventConfig[U]]
127
- ): boolean;
128
- }
115
+ export type TeleBoxEvents = Emittery<TeleBoxEventConfig>;
129
116
 
130
117
  export type TeleBoxHandleType =
131
118
  | TELE_BOX_RESIZE_HANDLE
@@ -136,31 +123,11 @@ type CheckTeleBoxDelegateConfig<
136
123
  > = T;
137
124
 
138
125
  export type TeleBoxDelegateEventConfig = CheckTeleBoxDelegateConfig<{
139
- close: void;
140
- maximize: void;
141
- minimize: void;
126
+ close: undefined;
127
+ maximize: undefined;
128
+ minimize: undefined;
142
129
  }>;
143
130
 
144
131
  export type TeleBoxDelegateEvent = keyof TeleBoxDelegateEventConfig;
145
132
 
146
- export interface TeleBoxDelegateEvents
147
- extends EventEmitter<TeleBoxDelegateEvent> {
148
- on<U extends TeleBoxDelegateEvent>(
149
- event: U,
150
- listener: (value: TeleBoxDelegateEventConfig[U]) => void
151
- ): this;
152
- once<U extends TeleBoxDelegateEvent>(
153
- event: U,
154
- listener: (value: TeleBoxDelegateEventConfig[U]) => void
155
- ): this;
156
- addListener<U extends TeleBoxDelegateEvent>(
157
- event: U,
158
- listener: (value: TeleBoxDelegateEventConfig[U]) => void
159
- ): this;
160
- emit<U extends TeleBoxDelegateEvent>(
161
- event: U,
162
- ...value: TeleBoxDelegateEventConfig[U] extends void
163
- ? []
164
- : [TeleBoxDelegateEventConfig[U]]
165
- ): boolean;
166
- }
133
+ export type TeleBoxDelegateEvents = Emittery<TeleBoxDelegateEventConfig>;
@@ -1,187 +1,164 @@
1
1
  import "./style.scss";
2
2
  import collectorSVG from "./icons/collector.svg";
3
- import { TeleStyles } from "../typings";
3
+ import type { TeleStyles } from "../typings";
4
+ import type {
5
+ ReadonlyVal,
6
+ ReadonlyValEnhancedResult,
7
+ ValEnhancedResult,
8
+ } from "value-enhancer";
9
+ import { withValueEnhancer } from "value-enhancer";
10
+ import { combine } from "value-enhancer";
11
+ import { Val } from "value-enhancer";
12
+ import { withReadonlyValueEnhancer } from "value-enhancer";
13
+ import { SideEffectManager } from "side-effect-manager";
14
+ import type { TeleBoxRect } from "../TeleBox/typings";
4
15
 
5
16
  export interface TeleBoxCollectorConfig {
6
- visible?: boolean;
7
- readonly?: boolean;
8
- darkMode?: boolean;
9
17
  namespace?: string;
10
18
  styles?: TeleStyles;
11
- onClick?: () => void;
19
+ root$: ReadonlyVal<HTMLElement | null>;
20
+ minimized$: Val<boolean>;
21
+ readonly$: ReadonlyVal<boolean>;
22
+ darkMode$: ReadonlyVal<boolean>;
23
+ rootRect$: ReadonlyVal<TeleBoxRect>;
12
24
  }
13
25
 
26
+ type ValConfig = {
27
+ styles: Val<TeleStyles>;
28
+ el: Val<HTMLElement>;
29
+ };
30
+
31
+ type ReadonlyValConfig = {
32
+ rect: ReadonlyVal<TeleBoxRect | undefined>;
33
+ visible: ReadonlyVal<boolean>;
34
+ root: ReadonlyVal<HTMLElement | null>;
35
+ };
36
+
37
+ type CombinedValEnhancedResult = ValEnhancedResult<ValConfig> &
38
+ ReadonlyValEnhancedResult<ReadonlyValConfig>;
39
+
40
+ export interface TeleBoxCollector extends CombinedValEnhancedResult {}
41
+
14
42
  export class TeleBoxCollector {
15
43
  public constructor({
16
- visible = true,
17
- readonly = false,
18
- darkMode = false,
44
+ minimized$,
45
+ readonly$,
46
+ darkMode$,
47
+ rootRect$,
19
48
  namespace = "telebox",
20
49
  styles = {},
21
- onClick,
22
- }: TeleBoxCollectorConfig = {}) {
23
- this._visible = visible;
24
- this._readonly = readonly;
25
- this._darkMode = darkMode;
50
+ root$,
51
+ }: TeleBoxCollectorConfig) {
26
52
  this.namespace = namespace;
27
- this.styles = styles;
28
- this.onClick = onClick;
29
- }
30
-
31
- public readonly styles: TeleStyles;
32
-
33
- public readonly namespace: string;
34
-
35
- public get visible(): boolean {
36
- return this._visible;
37
- }
38
-
39
- public get readonly(): boolean {
40
- return this._readonly;
41
- }
42
-
43
- public get darkMode(): boolean {
44
- return this._darkMode;
45
- }
46
-
47
- public onClick: (() => void) | undefined;
48
-
49
- public $collector: HTMLElement | undefined;
50
-
51
- /**
52
- * Mount collector to a root element.
53
- */
54
- public mount(root: HTMLElement): this {
55
- root.appendChild(this.render());
56
- return this;
57
- }
58
-
59
- /**
60
- * Unmount collector from the root element.
61
- */
62
- public unmount(): this {
63
- if (this.$collector) {
64
- this.$collector.remove();
65
- }
66
- return this;
67
- }
68
53
 
69
- public setVisible(visible: boolean): this {
70
- if (this._visible !== visible) {
71
- this._visible = visible;
72
- if (this.$collector) {
73
- this.$collector.classList.toggle(
74
- this.wrapClassName("collector-visible"),
75
- visible
54
+ const rect$ = new Val<TeleBoxRect | undefined>(void 0);
55
+ const visible$ = minimized$;
56
+ const styles$ = new Val(styles);
57
+ const el$ = new Val<HTMLElement>(document.createElement("button"));
58
+
59
+ const valConfig: ValConfig = {
60
+ styles: styles$,
61
+ el: el$,
62
+ };
63
+
64
+ withValueEnhancer(this, valConfig);
65
+
66
+ const readonlyValConfig: ReadonlyValConfig = {
67
+ rect: rect$,
68
+ visible: visible$,
69
+ root: root$,
70
+ };
71
+
72
+ withReadonlyValueEnhancer(this, readonlyValConfig);
73
+
74
+ el$.value.className = this.wrapClassName("collector");
75
+ el$.value.style.backgroundImage = `url('${collectorSVG}')`;
76
+
77
+ this._sideEffect.addDisposer(
78
+ el$.subscribe(($collector) => {
79
+ this._sideEffect.addEventListener(
80
+ $collector,
81
+ "click",
82
+ () => {
83
+ if (!readonly$.value) {
84
+ minimized$.setValue(false);
85
+ }
86
+ },
87
+ {},
88
+ "telebox-collector-click"
76
89
  );
77
- }
78
- }
79
- return this;
80
- }
81
-
82
- public setReadonly(readonly: boolean): this {
83
- if (this._readonly !== readonly) {
84
- this._readonly = readonly;
85
- if (this.$collector) {
86
- this.$collector.classList.toggle(
87
- this.wrapClassName("collector-readonly"),
88
- readonly
89
- );
90
- }
91
- }
92
- return this;
93
- }
94
90
 
95
- public setDarkMode(darkMode: boolean): this {
96
- if (this._darkMode !== darkMode) {
97
- this._darkMode = darkMode;
98
- if (this.$collector) {
99
- this.$collector.classList.toggle(
100
- this.wrapClassName("color-scheme-dark"),
101
- darkMode
91
+ this._sideEffect.addDisposer(
92
+ [
93
+ visible$.subscribe((visible) => {
94
+ $collector.classList.toggle(
95
+ this.wrapClassName("collector-visible"),
96
+ visible
97
+ );
98
+ }),
99
+ readonly$.subscribe((readonly) => {
100
+ $collector.classList.toggle(
101
+ this.wrapClassName("collector-readonly"),
102
+ readonly
103
+ );
104
+ }),
105
+ darkMode$.subscribe((darkMode) => {
106
+ $collector.classList.toggle(
107
+ this.wrapClassName("color-scheme-dark"),
108
+ darkMode
109
+ );
110
+ $collector.classList.toggle(
111
+ this.wrapClassName("color-scheme-light"),
112
+ !darkMode
113
+ );
114
+ }),
115
+ styles$.subscribe((styles) => {
116
+ Object.keys(styles).forEach((key) => {
117
+ const value = styles[
118
+ key as keyof TeleStyles
119
+ ] as string;
120
+ if (value != null) {
121
+ $collector.style[key as keyof TeleStyles] =
122
+ value;
123
+ }
124
+ });
125
+ }),
126
+ root$.subscribe((root) => {
127
+ if (root) {
128
+ root.appendChild($collector);
129
+ }
130
+ }),
131
+ // Place after $collector appended to the DOM so that rect calc works
132
+ combine([minimized$, rootRect$, root$]).subscribe(
133
+ ([minimized, rootRect, root]) => {
134
+ if (minimized && root) {
135
+ const { x, y, width, height } =
136
+ $collector.getBoundingClientRect();
137
+ rect$.setValue({
138
+ x: x - rootRect.x,
139
+ y: y - rootRect.y,
140
+ width,
141
+ height,
142
+ });
143
+ }
144
+ }
145
+ ),
146
+ ],
147
+ "telebox-collector-el"
102
148
  );
103
- this.$collector.classList.toggle(
104
- this.wrapClassName("color-scheme-light"),
105
- !darkMode
106
- );
107
- }
108
- }
109
- return this;
110
- }
111
-
112
- public setStyles(styles: TeleStyles): this {
113
- Object.assign(this.styles, styles);
114
- if (this.$collector) {
115
- const $collector = this.$collector;
116
- Object.keys(styles).forEach((key) => {
117
- const value = styles[key as keyof TeleStyles] as string;
118
- if (value != null) {
119
- $collector.style[key as keyof TeleStyles] = value;
120
- }
121
- });
122
- }
123
- return this;
149
+ })
150
+ );
124
151
  }
125
152
 
126
- public render(): HTMLElement {
127
- if (!this.$collector) {
128
- this.$collector = document.createElement("button");
129
- this.$collector.className = this.wrapClassName("collector");
130
- this.$collector.style.backgroundImage = `url('${collectorSVG}')`;
131
- this.$collector.addEventListener(
132
- "click",
133
- this.handleCollectorClick
134
- );
135
-
136
- if (this._visible) {
137
- this.$collector.classList.add(
138
- this.wrapClassName("collector-visible")
139
- );
140
- }
141
-
142
- if (this._readonly) {
143
- this.$collector.classList.add(
144
- this.wrapClassName("collector-readonly")
145
- );
146
- }
147
-
148
- this.$collector.classList.add(
149
- this.wrapClassName(
150
- this._darkMode ? "color-scheme-dark" : "color-scheme-light"
151
- )
152
- );
153
-
154
- this.setStyles(this.styles);
155
- }
153
+ public readonly namespace: string;
156
154
 
157
- return this.$collector;
158
- }
155
+ protected readonly _sideEffect = new SideEffectManager();
159
156
 
160
157
  public destroy(): void {
161
- if (this.$collector) {
162
- this.$collector.removeEventListener(
163
- "click",
164
- this.handleCollectorClick
165
- );
166
- this.$collector.remove();
167
- this.$collector = void 0;
168
- }
169
- this.onClick = void 0;
158
+ this._sideEffect.flushAll();
170
159
  }
171
160
 
172
161
  public wrapClassName(className: string): string {
173
162
  return `${this.namespace}-${className}`;
174
163
  }
175
-
176
- protected _visible: boolean;
177
-
178
- protected _readonly: boolean;
179
-
180
- protected _darkMode: boolean;
181
-
182
- protected handleCollectorClick = (): void => {
183
- if (!this._readonly && this.onClick) {
184
- this.onClick();
185
- }
186
- };
187
164
  }