@netless/telebox-insider 0.2.25 → 1.0.0-alpha.10

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 (53) hide show
  1. package/dist/TeleBox/constants.d.ts +0 -3
  2. package/dist/TeleBox/index.d.ts +120 -97
  3. package/dist/TeleBox/typings.d.ts +42 -55
  4. package/dist/TeleBoxCollector/index.d.ts +27 -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 +5 -9
  11. package/dist/TeleTitleBar/typings.d.ts +4 -6
  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 +518 -689
  22. package/src/TeleBox/style.scss +6 -0
  23. package/src/TeleBox/typings.ts +42 -75
  24. package/src/TeleBoxCollector/index.ts +151 -159
  25. package/src/TeleBoxCollector/style.scss +4 -1
  26. package/src/TeleBoxManager/MaxTitleBar/index.ts +88 -108
  27. package/src/TeleBoxManager/MaxTitleBar/style.scss +8 -3
  28. package/src/TeleBoxManager/constants.ts +0 -3
  29. package/src/TeleBoxManager/index.ts +330 -336
  30. package/src/TeleBoxManager/typings.ts +42 -52
  31. package/src/TeleStage/index.ts +174 -0
  32. package/src/TeleStage/style.scss +49 -0
  33. package/src/TeleTitleBar/DefaultTitleBar/index.ts +44 -65
  34. package/src/TeleTitleBar/DefaultTitleBar/style.scss +7 -0
  35. package/src/TeleTitleBar/typings.ts +4 -10
  36. package/src/index.ts +0 -1
  37. package/src/scss/mixins.scss +7 -0
  38. package/src/scss/variables.scss +3 -0
  39. package/src/utils.ts +0 -6
  40. package/dist/TeleBoxCollector/TeleBoxCollector.stories.d.ts +0 -5
  41. package/dist/TeleBoxManager/MaxTitleBar/MaxTitleBar.stories.d.ts +0 -7
  42. package/dist/TeleBoxManager/MaxTitleBar/MaxTitleBarReact.d.ts +0 -4
  43. package/dist/TeleBoxReact/TeleBox.stories.d.ts +0 -9
  44. package/dist/TeleBoxReact/index.d.ts +0 -17
  45. package/dist/TeleBoxReact/utils.d.ts +0 -2
  46. package/dist/TeleTitleBar/DefaultTitleBar/DefaultTitleBar.stories.d.ts +0 -4
  47. package/src/TeleBoxCollector/TeleBoxCollector.stories.tsx +0 -23
  48. package/src/TeleBoxManager/MaxTitleBar/MaxTitleBar.stories.tsx +0 -66
  49. package/src/TeleBoxManager/MaxTitleBar/MaxTitleBarReact.tsx +0 -34
  50. package/src/TeleBoxReact/TeleBox.stories.tsx +0 -74
  51. package/src/TeleBoxReact/index.tsx +0 -175
  52. package/src/TeleBoxReact/utils.ts +0 -14
  53. package/src/TeleTitleBar/DefaultTitleBar/DefaultTitleBar.stories.tsx +0 -15
@@ -1,4 +1,5 @@
1
1
  @import "../scss/variables.scss";
2
+ @import "../scss/mixins.scss";
2
3
 
3
4
  .#{$namespace}-box {
4
5
  position: absolute;
@@ -36,6 +37,7 @@
36
37
  display: flex;
37
38
  justify-content: center;
38
39
  align-items: center;
40
+ position: relative;
39
41
  }
40
42
 
41
43
  .#{$namespace}-content {
@@ -57,6 +59,8 @@
57
59
  }
58
60
 
59
61
  .#{$namespace}-resize-handle {
62
+ @include no-user-select;
63
+
60
64
  position: absolute;
61
65
  z-index: 2147483647;
62
66
  }
@@ -126,6 +130,8 @@
126
130
  }
127
131
 
128
132
  .#{$namespace}-track-mask {
133
+ @include no-user-select;
134
+
129
135
  position: fixed;
130
136
  top: 0;
131
137
  left: 0;
@@ -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,179 @@
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 { derive } from "value-enhancer";
10
+ import {
11
+ withValueEnhancer,
12
+ combine,
13
+ Val,
14
+ withReadonlyValueEnhancer,
15
+ ValManager,
16
+ } from "value-enhancer";
17
+ import { SideEffectManager } from "side-effect-manager";
18
+ import type { TeleBoxRect } from "../TeleBox/typings";
4
19
 
5
20
  export interface TeleBoxCollectorConfig {
6
- visible?: boolean;
7
- readonly?: boolean;
8
- darkMode?: boolean;
9
21
  namespace?: string;
10
22
  styles?: TeleStyles;
11
- onClick?: () => void;
23
+ root$: ReadonlyVal<HTMLElement | null>;
24
+ minimized$: Val<boolean>;
25
+ readonly$: ReadonlyVal<boolean>;
26
+ darkMode$: ReadonlyVal<boolean>;
27
+ rootRect$: ReadonlyVal<TeleBoxRect>;
12
28
  }
13
29
 
14
- export class TeleBoxCollector {
15
- public constructor({
16
- visible = true,
17
- readonly = false,
18
- darkMode = false,
19
- namespace = "telebox",
20
- styles = {},
21
- onClick,
22
- }: TeleBoxCollectorConfig = {}) {
23
- this._visible = visible;
24
- this._readonly = readonly;
25
- this._darkMode = darkMode;
26
- 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
- }
30
+ type ValConfig = {
31
+ styles: Val<TeleStyles>;
32
+ $collector: Val<HTMLElement>;
33
+ };
46
34
 
47
- public onClick: (() => void) | undefined;
35
+ type PropsValConfig = {
36
+ root: TeleBoxCollectorConfig["root$"];
37
+ };
48
38
 
49
- public $collector: HTMLElement | undefined;
39
+ type MyReadonlyValConfig = {
40
+ rect: ReadonlyVal<TeleBoxRect | undefined>;
41
+ visible: ReadonlyVal<boolean>;
42
+ };
50
43
 
51
- /**
52
- * Mount collector to a root element.
53
- */
54
- public mount(root: HTMLElement): this {
55
- root.appendChild(this.render());
56
- return this;
57
- }
44
+ type CombinedValEnhancedResult = ValEnhancedResult<ValConfig> &
45
+ ReadonlyValEnhancedResult<PropsValConfig & MyReadonlyValConfig>;
58
46
 
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
- }
47
+ export interface TeleBoxCollector extends CombinedValEnhancedResult {}
68
48
 
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
76
- );
77
- }
78
- }
79
- return this;
80
- }
49
+ export class TeleBoxCollector {
50
+ public constructor({
51
+ minimized$,
52
+ readonly$,
53
+ darkMode$,
54
+ rootRect$,
55
+ namespace = "telebox",
56
+ styles = {},
57
+ root$,
58
+ }: TeleBoxCollectorConfig) {
59
+ this.namespace = namespace;
81
60
 
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
61
+ const valManager = new ValManager();
62
+ this._sideEffect.addDisposer(() => valManager.destroy());
63
+
64
+ const rect$ = new Val<TeleBoxRect | undefined>(void 0);
65
+ const visible$ = derive(minimized$);
66
+ const styles$ = new Val(styles);
67
+ const el$ = new Val<HTMLElement>(document.createElement("button"));
68
+
69
+ const valConfig: ValConfig = {
70
+ styles: styles$,
71
+ $collector: el$,
72
+ };
73
+
74
+ withValueEnhancer(this, valConfig, valManager);
75
+
76
+ const propsValConfig: PropsValConfig = {
77
+ root: root$,
78
+ };
79
+
80
+ withReadonlyValueEnhancer(this, propsValConfig);
81
+
82
+ const myReadonlyValConfig: MyReadonlyValConfig = {
83
+ rect: rect$,
84
+ visible: visible$,
85
+ };
86
+
87
+ withReadonlyValueEnhancer(this, myReadonlyValConfig, valManager);
88
+
89
+ el$.value.className = this.wrapClassName("collector");
90
+ el$.value.style.backgroundImage = `url('${collectorSVG}')`;
91
+
92
+ this._sideEffect.addDisposer(
93
+ el$.subscribe(($collector) => {
94
+ this._sideEffect.addEventListener(
95
+ $collector,
96
+ "click",
97
+ () => {
98
+ if (!readonly$.value) {
99
+ minimized$.setValue(false);
100
+ }
101
+ },
102
+ {},
103
+ "telebox-collector-click"
89
104
  );
90
- }
91
- }
92
- return this;
93
- }
94
105
 
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
106
+ this._sideEffect.addDisposer(
107
+ [
108
+ visible$.subscribe((visible) => {
109
+ $collector.classList.toggle(
110
+ this.wrapClassName("collector-visible"),
111
+ visible
112
+ );
113
+ }),
114
+ readonly$.subscribe((readonly) => {
115
+ $collector.classList.toggle(
116
+ this.wrapClassName("collector-readonly"),
117
+ readonly
118
+ );
119
+ }),
120
+ darkMode$.subscribe((darkMode) => {
121
+ $collector.classList.toggle(
122
+ this.wrapClassName("color-scheme-dark"),
123
+ darkMode
124
+ );
125
+ $collector.classList.toggle(
126
+ this.wrapClassName("color-scheme-light"),
127
+ !darkMode
128
+ );
129
+ }),
130
+ styles$.subscribe((styles) => {
131
+ Object.keys(styles).forEach((key) => {
132
+ const value = styles[
133
+ key as keyof TeleStyles
134
+ ] as string;
135
+ if (value != null) {
136
+ $collector.style[key as keyof TeleStyles] =
137
+ value;
138
+ }
139
+ });
140
+ }),
141
+ root$.subscribe((root) => {
142
+ if (root) {
143
+ root.appendChild($collector);
144
+ }
145
+ }),
146
+ // Place after $collector appended to the DOM so that rect calc works
147
+ combine([minimized$, rootRect$, root$]).subscribe(
148
+ ([minimized, rootRect, root]) => {
149
+ if (minimized && root) {
150
+ const { x, y, width, height } =
151
+ $collector.getBoundingClientRect();
152
+ rect$.setValue({
153
+ x: x - rootRect.x,
154
+ y: y - rootRect.y,
155
+ width,
156
+ height,
157
+ });
158
+ }
159
+ }
160
+ ),
161
+ ],
162
+ "telebox-collector-el"
102
163
  );
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;
164
+ })
165
+ );
124
166
  }
125
167
 
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
- }
168
+ public readonly namespace: string;
156
169
 
157
- return this.$collector;
158
- }
170
+ protected readonly _sideEffect = new SideEffectManager();
159
171
 
160
172
  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;
173
+ this._sideEffect.flushAll();
170
174
  }
171
175
 
172
176
  public wrapClassName(className: string): string {
173
177
  return `${this.namespace}-${className}`;
174
178
  }
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
179
  }
@@ -1,10 +1,13 @@
1
1
  @import "../scss/variables.scss";
2
+ @import "../scss/mixins.scss";
2
3
 
3
4
  .#{$namespace}-collector {
5
+ @include no-user-select;
6
+
4
7
  visibility: hidden;
5
8
  display: block;
6
9
  position: absolute;
7
- z-index: $base-z-index * 2;
10
+ z-index: $collector-z-index;
8
11
  width: 40px;
9
12
  height: 40px;
10
13
  margin: 0;