@netless/telebox-insider 0.2.1 → 0.2.5

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.
@@ -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,20 +57,21 @@ 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
  );
@@ -64,8 +80,8 @@ export class TeleBoxManager {
64
80
  }
65
81
  });
66
82
 
67
- this._state = new CombinedVal(
68
- [this._minimized, this._maximized],
83
+ const state$ = combine(
84
+ [minimized$, maximized$],
69
85
  ([minimized, maximized]): TeleBoxState =>
70
86
  minimized
71
87
  ? TELE_BOX_STATE.Minimized
@@ -73,61 +89,61 @@ export class TeleBoxManager {
73
89
  ? TELE_BOX_STATE.Maximized
74
90
  : TELE_BOX_STATE.Normal
75
91
  );
76
- this._state.reaction((state, skipUpdate) => {
92
+ state$.reaction((state, _, skipUpdate) => {
77
93
  this.maxTitleBar.setState(state);
78
94
  if (!skipUpdate) {
79
95
  this.events.emit(TELE_BOX_MANAGER_EVENT.State, state);
80
96
  }
81
97
  });
82
98
 
83
- this._fence = new Val(fence);
84
- this._fence.subscribe((fence, skipUpdate) => {
99
+ const fence$ = createVal(fence);
100
+ fence$.subscribe((fence, _, skipUpdate) => {
85
101
  this.boxes.forEach((box) => box.setFence(fence, skipUpdate));
86
102
  });
87
103
 
88
- this._containerRect = new Val(containerRect, shallowequal);
89
- this._containerRect.reaction((containerRect, skipUpdate) => {
104
+ const containerRect$ = createVal(containerRect, shallowequal);
105
+ containerRect$.reaction((containerRect, _, skipUpdate) => {
90
106
  this.boxes.forEach((box) =>
91
107
  box.setContainerRect(containerRect, skipUpdate)
92
108
  );
93
109
  this.maxTitleBar.setContainerRect(containerRect);
94
110
  });
95
111
 
96
- this._collector = new Val(
112
+ const collector$ = createVal(
97
113
  collector === null
98
114
  ? null
99
115
  : collector ||
100
- new TeleBoxCollector({
101
- visible: minimized,
102
- readonly: readonly,
103
- namespace,
104
- }).mount(root)
116
+ new TeleBoxCollector({
117
+ visible: minimized,
118
+ readonly: readonly,
119
+ namespace,
120
+ }).mount(root)
105
121
  );
106
- this._collector.subscribe((collector) => {
122
+ collector$.subscribe((collector) => {
107
123
  if (collector) {
108
- collector.setVisible(this.minimized);
109
- collector.setReadonly(this.readonly);
124
+ collector.setVisible(minimized$.value);
125
+ collector.setReadonly(readonly$.value);
110
126
  this._sideEffect.add(() => {
111
127
  collector.onClick = () => {
112
- if (!this.readonly) {
113
- this.setMinimized(false);
128
+ if (!readonly$.value) {
129
+ minimized$.setValue(false);
114
130
  }
115
131
  };
116
132
  return () => collector.destroy();
117
133
  }, "collect-onClick");
118
134
  }
119
135
  });
120
- this._readonly.subscribe((readonly) =>
121
- this.collector?.setReadonly(readonly)
136
+ readonly$.subscribe((readonly) =>
137
+ collector$.value?.setReadonly(readonly)
122
138
  );
123
139
 
124
- this._minimized.subscribe((minimized, skipUpdate) => {
125
- this.collector?.setVisible(minimized);
140
+ minimized$.subscribe((minimized, _, skipUpdate) => {
141
+ collector$.value?.setVisible(minimized);
126
142
 
127
143
  if (minimized) {
128
- if (this.collector?.$collector) {
144
+ if (collector$.value?.$collector) {
129
145
  const { x, y, width, height } =
130
- this.collector.$collector.getBoundingClientRect();
146
+ collector$.value.$collector.getBoundingClientRect();
131
147
  const rootRect = this.root.getBoundingClientRect();
132
148
  this.boxes.forEach((box) => {
133
149
  box.setCollectorRect(
@@ -155,7 +171,7 @@ export class TeleBoxManager {
155
171
  });
156
172
 
157
173
  const checkFocusBox = (ev: MouseEvent | TouchEvent): void => {
158
- if (this.readonly) {
174
+ if (readonly$.value) {
159
175
  return;
160
176
  }
161
177
 
@@ -194,20 +210,20 @@ export class TeleBoxManager {
194
210
  );
195
211
 
196
212
  this.maxTitleBar = new MaxTitleBar({
197
- readonly: this.readonly,
213
+ readonly: readonly$.value,
198
214
  namespace: this.namespace,
199
- state: this.state,
215
+ state: state$.value,
200
216
  boxes: this.boxes,
201
217
  focusedBox: this._focusedBox,
202
- containerRect: this.containerRect,
218
+ containerRect: containerRect$.value,
203
219
  onEvent: (event): void => {
204
220
  switch (event.type) {
205
221
  case TELE_BOX_DELEGATE_EVENT.Maximize: {
206
- this.setMaximized(!this.maximized);
222
+ maximized$.setValue(!maximized$.value);
207
223
  break;
208
224
  }
209
225
  case TELE_BOX_DELEGATE_EVENT.Minimize: {
210
- this.setMinimized(true);
226
+ minimized$.setValue(true);
211
227
  break;
212
228
  }
213
229
  case TELE_BOX_EVENT.Close: {
@@ -225,10 +241,23 @@ export class TeleBoxManager {
225
241
  }
226
242
  },
227
243
  });
228
- this._readonly.subscribe((readonly) =>
244
+ readonly$.subscribe((readonly) =>
229
245
  this.maxTitleBar.setReadonly(readonly)
230
246
  );
231
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
+
232
261
  this.root.appendChild(this.maxTitleBar.render());
233
262
  }
234
263
 
@@ -242,75 +271,31 @@ export class TeleBoxManager {
242
271
 
243
272
  public zIndex: number;
244
273
 
245
- protected _collector: Val<TeleBoxCollector | null>;
246
-
247
- public get collector(): TeleBoxCollector | null {
248
- return this._collector.value;
249
- }
250
-
251
- public setCollector(collector: TeleBoxCollector): void {
252
- this._collector.setValue(collector);
253
- }
254
-
255
- protected _readonly: Val<boolean, boolean>;
256
-
257
- /** Is box readonly */
258
- public get readonly(): boolean {
259
- return this._readonly.value;
260
- }
261
-
262
- public setReadonly(readonly: boolean, skipUpdate = false): this {
263
- this._readonly.setValue(readonly, skipUpdate);
264
- return this;
265
- }
266
-
267
- protected _minimized: Val<boolean, boolean>;
268
-
269
- public get minimized(): boolean {
270
- return this._minimized.value;
271
- }
272
-
273
- public setMinimized(minimized: boolean, skipUpdate = false): this {
274
- this._minimized.setValue(minimized, skipUpdate);
275
- return this;
276
- }
277
-
278
- protected _maximized: Val<boolean, boolean>;
279
-
280
- public get maximized(): boolean {
281
- return this._maximized.value;
282
- }
283
-
284
- public setMaximized(maximized: boolean, skipUpdate = false): this {
285
- this._maximized.setValue(maximized, skipUpdate);
286
- return this;
287
- }
288
-
289
- protected _state: Val<TeleBoxState, boolean>;
274
+ public _state$: Val<TeleBoxState, boolean>;
290
275
 
291
276
  public get state(): TeleBoxState {
292
- return this._state.value;
277
+ return this._state$.value;
293
278
  }
294
279
 
295
- protected _fence: Val<boolean, boolean>;
296
-
297
- public get fence(): boolean {
298
- return this._fence.value;
299
- }
300
-
301
- public setFence(fence: boolean, skipUpdate = false): this {
302
- this._fence.setValue(fence, skipUpdate);
303
- return this;
304
- }
305
-
306
- protected _containerRect: Val<TeleBoxRect, boolean>;
307
-
308
- public get containerRect(): TeleBoxRect {
309
- return this._containerRect.value;
310
- }
311
-
312
- public setContainerRect(rect: TeleBoxRect, skipUpdate = false): this {
313
- 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
+ }
314
299
  return this;
315
300
  }
316
301
 
@@ -451,28 +436,6 @@ export class TeleBoxManager {
451
436
  });
452
437
  }
453
438
 
454
- /** @deprecated use setMaximized and setMinimized instead */
455
- public setState(state: TeleBoxState, skipUpdate = false): this {
456
- switch (state) {
457
- case TELE_BOX_STATE.Maximized: {
458
- this.setMinimized(false, skipUpdate);
459
- this.setMaximized(true, skipUpdate);
460
- break;
461
- }
462
- case TELE_BOX_STATE.Minimized: {
463
- this.setMinimized(true, skipUpdate);
464
- this.setMaximized(false, skipUpdate);
465
- break;
466
- }
467
- default: {
468
- this.setMinimized(false, skipUpdate);
469
- this.setMaximized(false, skipUpdate);
470
- break;
471
- }
472
- }
473
- return this;
474
- }
475
-
476
439
  public wrapClassName(className: string): string {
477
440
  return `${this.namespace}-${className}`;
478
441
  }
@@ -502,15 +465,15 @@ export class TeleBoxManager {
502
465
  ): void {
503
466
  if (config.x != null || config.y != null) {
504
467
  box.move(
505
- config.x == null ? box.x : config.x,
506
- config.y == null ? box.y : config.y,
468
+ config.x == null ? box.intrinsicX : config.x,
469
+ config.y == null ? box.intrinsicY : config.y,
507
470
  skipUpdate
508
471
  );
509
472
  }
510
473
  if (config.width != null || config.height != null) {
511
474
  box.resize(
512
- config.width == null ? box.width : config.width,
513
- config.height == null ? box.height : config.height,
475
+ config.width == null ? box.intrinsicWidth : config.width,
476
+ config.height == null ? box.intrinsicHeight : config.height,
514
477
  skipUpdate
515
478
  );
516
479
  }
@@ -607,8 +570,8 @@ export class TeleBoxManager {
607
570
  let y = 20;
608
571
 
609
572
  if (upMostBox) {
610
- x = upMostBox.x * this.containerRect.width + 20;
611
- y = upMostBox.y * this.containerRect.height + 20;
573
+ x = upMostBox.intrinsicX * this.containerRect.width + 20;
574
+ y = upMostBox.intrinsicY * this.containerRect.height + 20;
612
575
 
613
576
  if (
614
577
  x >
@@ -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";