@netless/telebox-insider 0.2.0 → 0.2.4

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.
@@ -88,6 +88,8 @@ export type TeleBoxEventConfig = CheckTeleBoxConfig<{
88
88
  intrinsic_resize: { width: number; height: number };
89
89
  visual_resize: { width: number; height: number };
90
90
  state: TeleBoxState;
91
+ minimized: boolean;
92
+ maximized: boolean;
91
93
  readonly: boolean;
92
94
  destroyed: void;
93
95
  }>;
@@ -3,6 +3,8 @@ export enum TELE_BOX_MANAGER_EVENT {
3
3
  Created = "created",
4
4
  Removed = "removed",
5
5
  State = "state",
6
+ Maximized = "maximized",
7
+ Minimized = "minimized",
6
8
  Move = "move",
7
9
  Resize = "resize",
8
10
  IntrinsicMove = "intrinsic_move",
@@ -18,12 +18,27 @@ import type {
18
18
  } from "./typings";
19
19
  import { MaxTitleBar } from "./MaxTitleBar";
20
20
  import { TELE_BOX_DELEGATE_EVENT } from "..";
21
- import { Val, CombinedVal } from "../Val";
22
21
  import { SideEffectManager } from "side-effect-manager";
22
+ import {
23
+ createSideEffectBinder,
24
+ Val,
25
+ ValEnhancedResult,
26
+ withValueEnhancer,
27
+ } from "value-enhancer";
23
28
 
24
29
  export * from "./typings";
25
30
  export * from "./constants";
26
31
 
32
+ type ValConfig = {
33
+ containerRect: Val<TeleBoxRect, boolean>;
34
+ collector: Val<TeleBoxCollector | null>;
35
+ readonly: Val<boolean, boolean>;
36
+ minimized: Val<boolean, boolean>;
37
+ maximized: Val<boolean, boolean>;
38
+ fence: Val<boolean, boolean>;
39
+ };
40
+ export interface TeleBoxManager extends ValEnhancedResult<ValConfig> {}
41
+
27
42
  export class TeleBoxManager {
28
43
  public constructor({
29
44
  root = document.body,
@@ -42,27 +57,31 @@ export class TeleBoxManager {
42
57
  readonly = false,
43
58
  }: TeleBoxManagerConfig = {}) {
44
59
  this._sideEffect = new SideEffectManager();
60
+ const { combine, createVal } = createSideEffectBinder(this._sideEffect);
45
61
 
46
62
  this.root = root;
47
63
  this.namespace = namespace;
48
64
  this.zIndex = zIndex;
49
65
 
50
- this._readonly = new Val(readonly);
51
- this._readonly.reaction((readonly, skipUpdate) => {
66
+ const readonly$ = createVal(readonly);
67
+ readonly$.reaction((readonly, _, skipUpdate) => {
52
68
  this.boxes.forEach((box) => box.setReadonly(readonly, skipUpdate));
53
69
  });
54
70
 
55
- this._minimized = new Val(minimized);
71
+ const minimized$ = createVal(minimized);
56
72
 
57
- this._maximized = new Val(maximized);
58
- this._maximized.reaction((maximized, skipUpdate) => {
73
+ const maximized$ = createVal(maximized);
74
+ maximized$.reaction((maximized, _, skipUpdate) => {
59
75
  this.boxes.forEach((box) =>
60
76
  box.setMaximized(maximized, skipUpdate)
61
77
  );
78
+ if (!skipUpdate) {
79
+ this.events.emit(TELE_BOX_MANAGER_EVENT.Maximized, maximized);
80
+ }
62
81
  });
63
82
 
64
- this._state = new CombinedVal(
65
- [this._minimized, this._maximized],
83
+ const state$ = combine(
84
+ [minimized$, maximized$],
66
85
  ([minimized, maximized]): TeleBoxState =>
67
86
  minimized
68
87
  ? TELE_BOX_STATE.Minimized
@@ -70,61 +89,61 @@ export class TeleBoxManager {
70
89
  ? TELE_BOX_STATE.Maximized
71
90
  : TELE_BOX_STATE.Normal
72
91
  );
73
- this._state.reaction((state, skipUpdate) => {
92
+ state$.reaction((state, _, skipUpdate) => {
74
93
  this.maxTitleBar.setState(state);
75
94
  if (!skipUpdate) {
76
95
  this.events.emit(TELE_BOX_MANAGER_EVENT.State, state);
77
96
  }
78
97
  });
79
98
 
80
- this._fence = new Val(fence);
81
- this._fence.subscribe((fence, skipUpdate) => {
99
+ const fence$ = createVal(fence);
100
+ fence$.subscribe((fence, _, skipUpdate) => {
82
101
  this.boxes.forEach((box) => box.setFence(fence, skipUpdate));
83
102
  });
84
103
 
85
- this._containerRect = new Val(containerRect, shallowequal);
86
- this._containerRect.reaction((containerRect, skipUpdate) => {
104
+ const containerRect$ = createVal(containerRect, shallowequal);
105
+ containerRect$.reaction((containerRect, _, skipUpdate) => {
87
106
  this.boxes.forEach((box) =>
88
107
  box.setContainerRect(containerRect, skipUpdate)
89
108
  );
90
109
  this.maxTitleBar.setContainerRect(containerRect);
91
110
  });
92
111
 
93
- this._collector = new Val(
112
+ const collector$ = createVal(
94
113
  collector === null
95
114
  ? null
96
115
  : collector ||
97
- new TeleBoxCollector({
98
- visible: minimized,
99
- readonly: readonly,
100
- namespace,
101
- }).mount(root)
116
+ new TeleBoxCollector({
117
+ visible: minimized,
118
+ readonly: readonly,
119
+ namespace,
120
+ }).mount(root)
102
121
  );
103
- this._collector.subscribe((collector) => {
122
+ collector$.subscribe((collector) => {
104
123
  if (collector) {
105
- collector.setVisible(this.minimized);
106
- collector.setReadonly(this.readonly);
124
+ collector.setVisible(minimized$.value);
125
+ collector.setReadonly(readonly$.value);
107
126
  this._sideEffect.add(() => {
108
127
  collector.onClick = () => {
109
- if (!this.readonly) {
110
- this.setMinimized(false);
128
+ if (!readonly$.value) {
129
+ minimized$.setValue(false);
111
130
  }
112
131
  };
113
132
  return () => collector.destroy();
114
133
  }, "collect-onClick");
115
134
  }
116
135
  });
117
- this._readonly.subscribe((readonly) =>
118
- this.collector?.setReadonly(readonly)
136
+ readonly$.subscribe((readonly) =>
137
+ collector$.value?.setReadonly(readonly)
119
138
  );
120
139
 
121
- this._minimized.subscribe((minimized, skipUpdate) => {
122
- this.collector?.setVisible(minimized);
140
+ minimized$.subscribe((minimized, _, skipUpdate) => {
141
+ collector$.value?.setVisible(minimized);
123
142
 
124
143
  if (minimized) {
125
- if (this.collector?.$collector) {
144
+ if (collector$.value?.$collector) {
126
145
  const { x, y, width, height } =
127
- this.collector.$collector.getBoundingClientRect();
146
+ collector$.value.$collector.getBoundingClientRect();
128
147
  const rootRect = this.root.getBoundingClientRect();
129
148
  this.boxes.forEach((box) => {
130
149
  box.setCollectorRect(
@@ -145,10 +164,14 @@ export class TeleBoxManager {
145
164
  this.boxes.forEach((box) =>
146
165
  box.setMinimized(minimized, skipUpdate)
147
166
  );
167
+
168
+ if (!skipUpdate) {
169
+ this.events.emit(TELE_BOX_MANAGER_EVENT.Minimized, minimized);
170
+ }
148
171
  });
149
172
 
150
173
  const checkFocusBox = (ev: MouseEvent | TouchEvent): void => {
151
- if (this.readonly) {
174
+ if (readonly$.value) {
152
175
  return;
153
176
  }
154
177
 
@@ -187,20 +210,20 @@ export class TeleBoxManager {
187
210
  );
188
211
 
189
212
  this.maxTitleBar = new MaxTitleBar({
190
- readonly: this.readonly,
213
+ readonly: readonly$.value,
191
214
  namespace: this.namespace,
192
- state: this.state,
215
+ state: state$.value,
193
216
  boxes: this.boxes,
194
217
  focusedBox: this._focusedBox,
195
- containerRect: this.containerRect,
218
+ containerRect: containerRect$.value,
196
219
  onEvent: (event): void => {
197
220
  switch (event.type) {
198
221
  case TELE_BOX_DELEGATE_EVENT.Maximize: {
199
- this.setMaximized(!this.maximized);
222
+ maximized$.setValue(!maximized$.value);
200
223
  break;
201
224
  }
202
225
  case TELE_BOX_DELEGATE_EVENT.Minimize: {
203
- this.setMinimized(true);
226
+ minimized$.setValue(true);
204
227
  break;
205
228
  }
206
229
  case TELE_BOX_EVENT.Close: {
@@ -218,10 +241,23 @@ export class TeleBoxManager {
218
241
  }
219
242
  },
220
243
  });
221
- this._readonly.subscribe((readonly) =>
244
+ readonly$.subscribe((readonly) =>
222
245
  this.maxTitleBar.setReadonly(readonly)
223
246
  );
224
247
 
248
+ const valConfig: ValConfig = {
249
+ containerRect: containerRect$,
250
+ collector: collector$,
251
+ readonly: readonly$,
252
+ fence: fence$,
253
+ minimized: minimized$,
254
+ maximized: maximized$,
255
+ };
256
+
257
+ withValueEnhancer(this, valConfig);
258
+
259
+ this._state$ = state$;
260
+
225
261
  this.root.appendChild(this.maxTitleBar.render());
226
262
  }
227
263
 
@@ -235,75 +271,31 @@ export class TeleBoxManager {
235
271
 
236
272
  public zIndex: number;
237
273
 
238
- protected _collector: Val<TeleBoxCollector | null>;
239
-
240
- public get collector(): TeleBoxCollector | null {
241
- return this._collector.value;
242
- }
243
-
244
- public setCollector(collector: TeleBoxCollector): void {
245
- this._collector.setValue(collector);
246
- }
247
-
248
- protected _readonly: Val<boolean, boolean>;
249
-
250
- /** Is box readonly */
251
- public get readonly(): boolean {
252
- return this._readonly.value;
253
- }
254
-
255
- public setReadonly(readonly: boolean, skipUpdate = false): this {
256
- this._readonly.setValue(readonly, skipUpdate);
257
- return this;
258
- }
259
-
260
- protected _minimized: Val<boolean, boolean>;
261
-
262
- public get minimized(): boolean {
263
- return this._minimized.value;
264
- }
265
-
266
- public setMinimized(minimized: boolean, skipUpdate = false): this {
267
- this._minimized.setValue(minimized, skipUpdate);
268
- return this;
269
- }
270
-
271
- protected _maximized: Val<boolean, boolean>;
272
-
273
- public get maximized(): boolean {
274
- return this._maximized.value;
275
- }
276
-
277
- public setMaximized(maximized: boolean, skipUpdate = false): this {
278
- this._maximized.setValue(maximized, skipUpdate);
279
- return this;
280
- }
281
-
282
- protected _state: Val<TeleBoxState, boolean>;
274
+ public _state$: Val<TeleBoxState, boolean>;
283
275
 
284
276
  public get state(): TeleBoxState {
285
- return this._state.value;
286
- }
287
-
288
- protected _fence: Val<boolean, boolean>;
289
-
290
- public get fence(): boolean {
291
- return this._fence.value;
277
+ return this._state$.value;
292
278
  }
293
279
 
294
- public setFence(fence: boolean, skipUpdate = false): this {
295
- this._fence.setValue(fence, skipUpdate);
296
- return this;
297
- }
298
-
299
- protected _containerRect: Val<TeleBoxRect, boolean>;
300
-
301
- public get containerRect(): TeleBoxRect {
302
- return this._containerRect.value;
303
- }
304
-
305
- public setContainerRect(rect: TeleBoxRect, skipUpdate = false): this {
306
- this._containerRect.setValue(rect, skipUpdate);
280
+ /** @deprecated use setMaximized and setMinimized instead */
281
+ public setState(state: TeleBoxState, skipUpdate = false): this {
282
+ switch (state) {
283
+ case TELE_BOX_STATE.Maximized: {
284
+ this.setMinimized(false, skipUpdate);
285
+ this.setMaximized(true, skipUpdate);
286
+ break;
287
+ }
288
+ case TELE_BOX_STATE.Minimized: {
289
+ this.setMinimized(true, skipUpdate);
290
+ this.setMaximized(false, skipUpdate);
291
+ break;
292
+ }
293
+ default: {
294
+ this.setMinimized(false, skipUpdate);
295
+ this.setMaximized(false, skipUpdate);
296
+ break;
297
+ }
298
+ }
307
299
  return this;
308
300
  }
309
301
 
@@ -444,28 +436,6 @@ export class TeleBoxManager {
444
436
  });
445
437
  }
446
438
 
447
- /** @deprecated use setMaximized and setMinimized instead */
448
- public setState(state: TeleBoxState, skipUpdate = false): this {
449
- switch (state) {
450
- case TELE_BOX_STATE.Maximized: {
451
- this.setMinimized(false, skipUpdate);
452
- this.setMaximized(true, skipUpdate);
453
- break;
454
- }
455
- case TELE_BOX_STATE.Minimized: {
456
- this.setMinimized(true, skipUpdate);
457
- this.setMaximized(false, skipUpdate);
458
- break;
459
- }
460
- default: {
461
- this.setMinimized(false, skipUpdate);
462
- this.setMaximized(false, skipUpdate);
463
- break;
464
- }
465
- }
466
- return this;
467
- }
468
-
469
439
  public wrapClassName(className: string): string {
470
440
  return `${this.namespace}-${className}`;
471
441
  }
@@ -495,15 +465,15 @@ export class TeleBoxManager {
495
465
  ): void {
496
466
  if (config.x != null || config.y != null) {
497
467
  box.move(
498
- config.x == null ? box.x : config.x,
499
- config.y == null ? box.y : config.y,
468
+ config.x == null ? box.intrinsicX : config.x,
469
+ config.y == null ? box.intrinsicY : config.y,
500
470
  skipUpdate
501
471
  );
502
472
  }
503
473
  if (config.width != null || config.height != null) {
504
474
  box.resize(
505
- config.width == null ? box.width : config.width,
506
- config.height == null ? box.height : config.height,
475
+ config.width == null ? box.intrinsicWidth : config.width,
476
+ config.height == null ? box.intrinsicHeight : config.height,
507
477
  skipUpdate
508
478
  );
509
479
  }
@@ -600,8 +570,8 @@ export class TeleBoxManager {
600
570
  let y = 20;
601
571
 
602
572
  if (upMostBox) {
603
- x = upMostBox.x * this.containerRect.width + 20;
604
- y = upMostBox.y * this.containerRect.height + 20;
573
+ x = upMostBox.intrinsicX * this.containerRect.width + 20;
574
+ y = upMostBox.intrinsicY * this.containerRect.height + 20;
605
575
 
606
576
  if (
607
577
  x >
@@ -60,6 +60,8 @@ export type TeleBoxManagerEventConfig = CheckTeleBoxManagerConfig<{
60
60
  created: [ReadonlyTeleBox];
61
61
  removed: [ReadonlyTeleBox[]];
62
62
  state: [TeleBoxState];
63
+ maximized: [boolean];
64
+ minimized: [boolean];
63
65
  move: [ReadonlyTeleBox];
64
66
  resize: [ReadonlyTeleBox];
65
67
  intrinsic_move: [ReadonlyTeleBox];
@@ -1,11 +0,0 @@
1
- import { Val, ValCompare } from "./Val";
2
- export declare type CombinedValInputTuple<TArr, TMeta> = {
3
- [K in keyof TArr]: Val<TArr[K], TMeta>;
4
- };
5
- export declare type CombinedValTransform<TValue, TInputs extends readonly unknown[]> = (values: TInputs) => TValue;
6
- export declare class CombinedVal<TValue, TInputs extends readonly unknown[], TMeta = any> extends Val<TValue, TMeta> {
7
- private _disposers;
8
- private _values;
9
- constructor(vals: readonly [...CombinedValInputTuple<TInputs, TMeta>], transform: CombinedValTransform<TValue, TInputs>, compare?: ValCompare<TValue>);
10
- destroy(): void;
11
- }
@@ -1,7 +0,0 @@
1
- import { Val, ValCompare } from "./Val";
2
- export declare type ComputedValTransform<TValue, TUpstream> = (value: TUpstream) => TValue;
3
- export declare class ComputedVal<TValue, TUpstream = TValue, TMeta = void> extends Val<TValue, TMeta> {
4
- private _disposer;
5
- constructor(val: Val<TUpstream, TMeta>, transform: ComputedValTransform<TValue, TUpstream>, compare?: ValCompare<TValue>);
6
- destroy(): void;
7
- }
package/dist/Val/Val.d.ts DELETED
@@ -1,15 +0,0 @@
1
- export declare type ValCompare<TValue> = (newValue: TValue, oldValue: TValue) => boolean;
2
- export declare type ValSubscriber<TValue, TMeta> = (value: TValue, meta?: TMeta) => void;
3
- export declare type ValDisposer = () => void;
4
- export declare class Val<TValue, TMeta = any> {
5
- protected _value: TValue;
6
- constructor(value: TValue, compare?: ValCompare<TValue>);
7
- get value(): TValue;
8
- setValue(value: TValue, meta?: TMeta): void;
9
- reaction(subscriber: ValSubscriber<TValue, TMeta>): ValDisposer;
10
- /** reaction + immediate emission */
11
- subscribe(subscriber: ValSubscriber<TValue, TMeta>, meta?: TMeta): ValDisposer;
12
- destroy(): void;
13
- compare(newValue: TValue, oldValue: TValue): boolean;
14
- protected _subscribers?: Set<ValSubscriber<TValue, TMeta>>;
15
- }
@@ -1,3 +0,0 @@
1
- export * from "./Val";
2
- export * from "./CombinedVal";
3
- export * from "./ComputedVal";
@@ -1,39 +0,0 @@
1
- import { Val, ValCompare, ValDisposer } from "./Val";
2
-
3
- export type CombinedValInputTuple<TArr, TMeta> = {
4
- [K in keyof TArr]: Val<TArr[K], TMeta>;
5
- };
6
-
7
- export type CombinedValTransform<TValue, TInputs extends readonly unknown[]> = (
8
- values: TInputs
9
- ) => TValue;
10
-
11
- export class CombinedVal<
12
- TValue,
13
- TInputs extends readonly unknown[],
14
- TMeta = any
15
- > extends Val<TValue, TMeta> {
16
- private _disposers: ValDisposer[];
17
- private _values: [...TInputs];
18
-
19
- public constructor(
20
- vals: readonly [...CombinedValInputTuple<TInputs, TMeta>],
21
- transform: CombinedValTransform<TValue, TInputs>,
22
- compare?: ValCompare<TValue>
23
- ) {
24
- const initValues = vals.map((val) => val.value) as [...TInputs];
25
- super(transform(initValues), compare);
26
- this._values = initValues;
27
- this._disposers = vals.map((val, i) =>
28
- val.reaction((value, meta) => {
29
- this._values[i] = value;
30
- super.setValue(transform(this._values), meta);
31
- })
32
- );
33
- }
34
-
35
- public destroy(): void {
36
- this._disposers.forEach((disposer) => disposer());
37
- super.destroy();
38
- }
39
- }
@@ -1,28 +0,0 @@
1
- import { Val, ValCompare, ValDisposer } from "./Val";
2
-
3
- export type ComputedValTransform<TValue, TUpstream> = (
4
- value: TUpstream
5
- ) => TValue;
6
-
7
- export class ComputedVal<TValue, TUpstream = TValue, TMeta = void> extends Val<
8
- TValue,
9
- TMeta
10
- > {
11
- private _disposer: ValDisposer;
12
-
13
- public constructor(
14
- val: Val<TUpstream, TMeta>,
15
- transform: ComputedValTransform<TValue, TUpstream>,
16
- compare?: ValCompare<TValue>
17
- ) {
18
- super(transform(val.value), compare);
19
- this._disposer = val.reaction((value, meta) => {
20
- super.setValue(transform(value), meta);
21
- });
22
- }
23
-
24
- public destroy(): void {
25
- this._disposer();
26
- super.destroy();
27
- }
28
- }
package/src/Val/Val.ts DELETED
@@ -1,73 +0,0 @@
1
- export type ValCompare<TValue> = (
2
- newValue: TValue,
3
- oldValue: TValue
4
- ) => boolean;
5
-
6
- export type ValSubscriber<TValue, TMeta> = (
7
- value: TValue,
8
- meta?: TMeta
9
- ) => void;
10
-
11
- export type ValDisposer = () => void;
12
-
13
- export class Val<TValue, TMeta = any> {
14
- protected _value: TValue;
15
-
16
- public constructor(value: TValue, compare?: ValCompare<TValue>) {
17
- this._value = value;
18
- if (typeof compare === "function") {
19
- this.compare = compare;
20
- }
21
- }
22
-
23
- public get value(): TValue {
24
- return this._value;
25
- }
26
-
27
- public setValue(value: TValue, meta?: TMeta): void {
28
- if (!this.compare(value, this._value)) {
29
- this._value = value;
30
- if (this._subscribers) {
31
- this._subscribers.forEach((subscriber) =>
32
- subscriber(value, meta)
33
- );
34
- }
35
- }
36
- }
37
-
38
- public reaction(subscriber: ValSubscriber<TValue, TMeta>): ValDisposer {
39
- if (!this._subscribers) {
40
- this._subscribers = new Set();
41
- }
42
-
43
- this._subscribers.add(subscriber);
44
-
45
- return (): void => {
46
- if (this._subscribers) {
47
- this._subscribers.delete(subscriber);
48
- }
49
- };
50
- }
51
-
52
- /** reaction + immediate emission */
53
- public subscribe(
54
- subscriber: ValSubscriber<TValue, TMeta>,
55
- meta?: TMeta
56
- ): ValDisposer {
57
- const disposer = this.reaction(subscriber);
58
- subscriber(this._value, meta);
59
- return disposer;
60
- }
61
-
62
- public destroy(): void {
63
- if (this._subscribers) {
64
- this._subscribers.clear();
65
- }
66
- }
67
-
68
- public compare(newValue: TValue, oldValue: TValue): boolean {
69
- return newValue === oldValue;
70
- }
71
-
72
- protected _subscribers?: Set<ValSubscriber<TValue, TMeta>>;
73
- }
package/src/Val/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./Val";
2
- export * from "./CombinedVal";
3
- export * from "./ComputedVal";