@netless/telebox-insider 1.0.0-alpha.1 → 1.0.0-alpha.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netless/telebox-insider",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-alpha.2",
4
4
  "private": false,
5
5
  "description": "Window widget for Whiteboard",
6
6
  "license": "MIT",
@@ -50,6 +50,7 @@
50
50
  "minimist": "^1.2.5",
51
51
  "prettier": "^2.0.5",
52
52
  "rimraf": "^3.0.0",
53
+ "rollup-plugin-exclude-dependencies-from-bundle": "^1.1.22",
53
54
  "sass": "^1.36.0",
54
55
  "sass-loader": "^10.1.1",
55
56
  "typescript": "^4.1.2",
@@ -117,6 +117,7 @@ export class TeleBox {
117
117
  minimized$,
118
118
  maximized$,
119
119
  readonly$,
120
+ root$,
120
121
  rootRect$,
121
122
  managerStageRect$,
122
123
  stageRatio$,
@@ -220,12 +221,15 @@ export class TeleBox {
220
221
  : boxHighlightStage
221
222
  );
222
223
 
224
+ const contentRoot$ = new Val<HTMLElement | null>(null);
225
+
223
226
  const contentRect$ = new Val<TeleBoxRect>(rootRect$.value, {
224
227
  compare: shallowequal,
225
228
  });
226
229
 
227
230
  const teleStage = new TeleStage({
228
231
  namespace,
232
+ root$: contentRoot$,
229
233
  rootRect$: contentRect$,
230
234
  ratio$: stageRatio$,
231
235
  highlightStage$,
@@ -312,7 +316,7 @@ export class TeleBox {
312
316
  );
313
317
 
314
318
  this.$box = this.render();
315
- teleStage.root$.setValue(this.$content.parentElement);
319
+ contentRoot$.setValue(this.$content.parentElement);
316
320
 
317
321
  const watchValEvent = <E extends TeleBoxEvent>(
318
322
  val: ReadonlyVal<TeleBoxEventConfig[E], boolean>,
@@ -336,23 +340,27 @@ export class TeleBox {
336
340
  watchValEvent(intrinsicSize$, TELE_BOX_EVENT.IntrinsicResize);
337
341
  watchValEvent(intrinsicCoord$, TELE_BOX_EVENT.IntrinsicMove);
338
342
 
339
- this._sideEffect.addDisposer(
343
+ this._sideEffect.addDisposer([
340
344
  visible$.reaction((visible, skipUpdate) => {
341
345
  if (!skipUpdate && !visible) {
342
346
  this.events.emit(TELE_BOX_EVENT.Close);
343
347
  }
344
- })
345
- );
346
-
347
- this._sideEffect.addDisposer(
348
+ }),
348
349
  focus$.reaction((focus, skipUpdate) => {
349
350
  if (!skipUpdate) {
350
351
  this.events.emit(
351
352
  focus ? TELE_BOX_EVENT.Focus : TELE_BOX_EVENT.Blur
352
353
  );
353
354
  }
354
- })
355
- );
355
+ }),
356
+ root$.subscribe((root) => {
357
+ if (root) {
358
+ root.appendChild(this.$box);
359
+ } else if (this.$box.parentNode) {
360
+ this.$box.remove();
361
+ }
362
+ }),
363
+ ]);
356
364
  }
357
365
 
358
366
  public readonly id: string;
@@ -535,22 +543,6 @@ export class TeleBox {
535
543
  );
536
544
  }
537
545
 
538
- /**
539
- * Mount box to a container element.
540
- */
541
- public mount(container: HTMLElement): void {
542
- container.appendChild(this.$box);
543
- }
544
-
545
- /**
546
- * Unmount box from the container element.
547
- */
548
- public unmount(): void {
549
- if (this.$box.parentNode) {
550
- this.$box.remove();
551
- }
552
- }
553
-
554
546
  /**
555
547
  * Mount dom to box content.
556
548
  */
@@ -78,6 +78,8 @@ export interface TeleBoxConfig {
78
78
  readonly minimized$: ReadonlyVal<boolean, boolean>;
79
79
  /** Is box readonly */
80
80
  readonly readonly$: ReadonlyVal<boolean, boolean>;
81
+ /** Root element to mount boxes */
82
+ readonly root$: ReadonlyVal<HTMLElement | null>;
81
83
  /** Position and dimension of root element */
82
84
  readonly rootRect$: ReadonlyVal<TeleBoxRect>;
83
85
  /** Position and dimension of stage area */
@@ -16,7 +16,7 @@ import type { TeleBoxRect } from "../TeleBox/typings";
16
16
  export interface TeleBoxCollectorConfig {
17
17
  namespace?: string;
18
18
  styles?: TeleStyles;
19
- root?: HTMLElement;
19
+ root$: ReadonlyVal<HTMLElement | null>;
20
20
  minimized$: Val<boolean>;
21
21
  readonly$: ReadonlyVal<boolean>;
22
22
  darkMode$: ReadonlyVal<boolean>;
@@ -25,13 +25,13 @@ export interface TeleBoxCollectorConfig {
25
25
 
26
26
  type ValConfig = {
27
27
  styles: Val<TeleStyles>;
28
- root: Val<HTMLElement | undefined>;
29
28
  el: Val<HTMLElement>;
30
29
  };
31
30
 
32
31
  type ReadonlyValConfig = {
33
32
  rect: ReadonlyVal<TeleBoxRect | undefined>;
34
33
  visible: ReadonlyVal<boolean>;
34
+ root: ReadonlyVal<HTMLElement | null>;
35
35
  };
36
36
 
37
37
  type CombinedValEnhancedResult = ValEnhancedResult<ValConfig> &
@@ -47,19 +47,17 @@ export class TeleBoxCollector {
47
47
  rootRect$,
48
48
  namespace = "telebox",
49
49
  styles = {},
50
- root,
50
+ root$,
51
51
  }: TeleBoxCollectorConfig) {
52
52
  this.namespace = namespace;
53
53
 
54
54
  const rect$ = new Val<TeleBoxRect | undefined>(void 0);
55
55
  const visible$ = minimized$;
56
56
  const styles$ = new Val(styles);
57
- const root$ = new Val(root);
58
57
  const el$ = new Val<HTMLElement>(document.createElement("button"));
59
58
 
60
59
  const valConfig: ValConfig = {
61
60
  styles: styles$,
62
- root: root$,
63
61
  el: el$,
64
62
  };
65
63
 
@@ -68,6 +66,7 @@ export class TeleBoxCollector {
68
66
  const readonlyValConfig: ReadonlyValConfig = {
69
67
  rect: rect$,
70
68
  visible: visible$,
69
+ root: root$,
71
70
  };
72
71
 
73
72
  withReadonlyValueEnhancer(this, readonlyValConfig);
@@ -14,6 +14,7 @@ export interface MaxTitleBarConfig extends DefaultTitleBarConfig {
14
14
  darkMode$: ReadonlyVal<boolean>;
15
15
  boxes$: ReadonlyVal<TeleBox[]>;
16
16
  rootRect$: ReadonlyVal<TeleBoxRect>;
17
+ root$: ReadonlyVal<HTMLElement | null>;
17
18
  }
18
19
 
19
20
  export class MaxTitleBar extends DefaultTitleBar {
@@ -23,6 +24,16 @@ export class MaxTitleBar extends DefaultTitleBar {
23
24
  this.boxes$ = config.boxes$;
24
25
  this.rootRect$ = config.rootRect$;
25
26
  this.darkMode$ = config.darkMode$;
27
+
28
+ this.sideEffect.addDisposer(
29
+ config.root$.subscribe((root) => {
30
+ if (root) {
31
+ root.appendChild(this.render());
32
+ } else if (this.$titleBar?.parentNode) {
33
+ this.$titleBar.remove();
34
+ }
35
+ })
36
+ );
26
37
  }
27
38
 
28
39
  public focusBox(box?: TeleBox): void {
@@ -54,6 +65,10 @@ export class MaxTitleBar extends DefaultTitleBar {
54
65
  }
55
66
 
56
67
  public render(): HTMLElement {
68
+ if (this.$titleBar) {
69
+ return this.$titleBar;
70
+ }
71
+
57
72
  const $titleBar = super.render();
58
73
 
59
74
  $titleBar.classList.add(this.wrapClassName("max-titlebar"));
@@ -46,6 +46,7 @@ const ResizeObserver = window.ResizeObserver || ResizeObserverPolyfill;
46
46
  type ReadonlyValConfig = {
47
47
  darkMode: ReadonlyVal<boolean, boolean>;
48
48
  state: ReadonlyVal<TeleBoxState, boolean>;
49
+ root: Val<HTMLElement | null>;
49
50
  rootRect: Val<TeleBoxRect>;
50
51
  stageRect: ReadonlyVal<TeleBoxRect>;
51
52
  };
@@ -67,7 +68,7 @@ export interface TeleBoxManager extends CombinedValEnhancedResult {}
67
68
 
68
69
  export class TeleBoxManager {
69
70
  public constructor({
70
- root = document.body,
71
+ root = null,
71
72
  prefersColorScheme = TELE_BOX_COLOR_SCHEME.Light,
72
73
  minimized = false,
73
74
  maximized = false,
@@ -80,9 +81,9 @@ export class TeleBoxManager {
80
81
  }: TeleBoxManagerConfig = {}) {
81
82
  this._sideEffect = new SideEffectManager();
82
83
 
83
- this.root = root;
84
84
  this.namespace = namespace;
85
85
 
86
+ const root$ = new Val(root);
86
87
  const readonly$ = new Val(readonly);
87
88
  const minimized$ = new Val(minimized);
88
89
  const maximized$ = new Val(maximized);
@@ -99,28 +100,35 @@ export class TeleBoxManager {
99
100
  },
100
101
  { compare: shallowequal }
101
102
  );
102
- this._sideEffect.add(() => {
103
- const observer = new ResizeObserver((entries) => {
104
- this._sideEffect.setTimeout(
105
- // debounce
106
- () => {
107
- const rect = entries[0]?.contentRect;
108
- if (rect) {
109
- rootRect$.setValue({
110
- x: rect.x,
111
- y: rect.y,
112
- width: rect.width,
113
- height: rect.height,
114
- });
115
- }
116
- },
117
- 50,
118
- "root-resize-observer"
119
- );
120
- });
121
- observer.observe(root);
122
- return () => observer.disconnect();
123
- });
103
+ this._sideEffect.addDisposer(
104
+ root$.subscribe((root) => {
105
+ this._sideEffect.add(() => {
106
+ if (!root) {
107
+ return () => void 0;
108
+ }
109
+ const observer = new ResizeObserver((entries) => {
110
+ this._sideEffect.setTimeout(
111
+ // debounce
112
+ () => {
113
+ const rect = entries[0]?.contentRect;
114
+ if (rect) {
115
+ rootRect$.setValue({
116
+ x: rect.x,
117
+ y: rect.y,
118
+ width: rect.width,
119
+ height: rect.height,
120
+ });
121
+ }
122
+ },
123
+ 50,
124
+ "root-resize-observer"
125
+ );
126
+ });
127
+ observer.observe(root);
128
+ return () => observer.disconnect();
129
+ }, "calc-root-rect");
130
+ })
131
+ );
124
132
 
125
133
  this.boxes$ = new Val<TeleBox[]>([]);
126
134
  this.topBox$ = new Val<TeleBox | undefined>(undefined);
@@ -190,7 +198,7 @@ export class TeleBoxManager {
190
198
  readonly$: readonly$,
191
199
  darkMode$: darkMode$,
192
200
  namespace,
193
- root,
201
+ root$,
194
202
  rootRect$,
195
203
  });
196
204
 
@@ -198,7 +206,7 @@ export class TeleBoxManager {
198
206
  namespace,
199
207
  rootRect$,
200
208
  ratio$: stageRatio$,
201
- root,
209
+ root$,
202
210
  highlightStage$,
203
211
  });
204
212
  this._sideEffect.addDisposer(() => teleStage.destroy());
@@ -206,6 +214,7 @@ export class TeleBoxManager {
206
214
  const readonlyValConfig: ReadonlyValConfig = {
207
215
  darkMode: darkMode$,
208
216
  state: state$,
217
+ root: root$,
209
218
  rootRect: rootRect$,
210
219
  stageRect: teleStage.stageRect$,
211
220
  };
@@ -275,6 +284,7 @@ export class TeleBoxManager {
275
284
  darkMode$: darkMode$,
276
285
  readonly$: readonly$,
277
286
  state$: state$,
287
+ root$,
278
288
  rootRect$: rootRect$,
279
289
  onEvent: (event): void => {
280
290
  switch (event.type) {
@@ -298,8 +308,6 @@ export class TeleBoxManager {
298
308
  },
299
309
  });
300
310
 
301
- this.root.appendChild(this.titleBar.render());
302
-
303
311
  this._sideEffect.addDisposer([
304
312
  state$.reaction((state, skipUpdate) => {
305
313
  if (!skipUpdate) {
@@ -345,8 +353,6 @@ export class TeleBoxManager {
345
353
 
346
354
  protected _sideEffect: SideEffectManager;
347
355
 
348
- protected root: HTMLElement;
349
-
350
356
  public readonly namespace: string;
351
357
 
352
358
  /** @deprecated use setMaximized and setMinimized instead */
@@ -380,6 +386,7 @@ export class TeleBoxManager {
380
386
  ...config,
381
387
  ...(smartPosition ? this.smartPosition(config) : {}),
382
388
  namespace: this.namespace,
389
+ root$: this._root$,
383
390
  darkMode$: this._darkMode$,
384
391
  maximized$: this._maximized$,
385
392
  minimized$: this._minimized$,
@@ -392,8 +399,6 @@ export class TeleBoxManager {
392
399
  managerHighlightStage$: this._highlightStage$,
393
400
  });
394
401
 
395
- box.mount(this.root);
396
-
397
402
  if (box.focus) {
398
403
  this.focusBox(box);
399
404
  if (smartPosition) {
@@ -522,6 +527,20 @@ export class TeleBoxManager {
522
527
  return deletedBoxes;
523
528
  }
524
529
 
530
+ /**
531
+ * Mount manager to a container element.
532
+ */
533
+ public mount(root: HTMLElement): void {
534
+ this._root$.setValue(root);
535
+ }
536
+
537
+ /**
538
+ * Unmount manager from the container element.
539
+ */
540
+ public unmount(): void {
541
+ this._root$.setValue(null);
542
+ }
543
+
525
544
  public destroy(skipUpdate = false): void {
526
545
  this.events.clearListeners();
527
546
  this._sideEffect.flushAll();
@@ -6,7 +6,7 @@ import type { TELE_BOX_MANAGER_EVENT } from "./constants";
6
6
 
7
7
  export interface TeleBoxManagerConfig extends Pick<TeleBoxConfig, "namespace"> {
8
8
  /** Element to mount boxes. */
9
- readonly root?: HTMLElement;
9
+ readonly root?: HTMLElement | null;
10
10
  /** Stage area height/with ratio. No ratio if <= 0. Default -1. */
11
11
  readonly stageRatio?: number;
12
12
  /** Where the minimized boxes go. */
@@ -3,13 +3,12 @@ import "./style.scss";
3
3
  import shallowequal from "shallowequal";
4
4
  import { SideEffectManager } from "side-effect-manager";
5
5
  import type { ReadonlyVal } from "value-enhancer";
6
- import { Val } from "value-enhancer";
7
6
  import { combine } from "value-enhancer";
8
7
  import type { TeleBoxRect } from "../TeleBox/typings";
9
8
 
10
9
  export interface TeleStageConfig {
11
10
  namespace: string;
12
- root?: HTMLElement | null;
11
+ root$: ReadonlyVal<HTMLElement | null>;
13
12
  rootRect$: ReadonlyVal<TeleBoxRect>;
14
13
  ratio$: ReadonlyVal<number>;
15
14
  highlightStage$: ReadonlyVal<boolean>;
@@ -21,7 +20,6 @@ export class TeleStage {
21
20
  public readonly namespace: string;
22
21
  public readonly stageRect$: ReadonlyVal<TeleBoxRect>;
23
22
  public readonly highlightStage$: ReadonlyVal<boolean>;
24
- public readonly root$: Val<HTMLElement | null>;
25
23
 
26
24
  public $boxStage?: HTMLDivElement;
27
25
 
@@ -30,11 +28,10 @@ export class TeleStage {
30
28
  rootRect$,
31
29
  ratio$,
32
30
  highlightStage$,
33
- root = null,
31
+ root$,
34
32
  }: TeleStageConfig) {
35
33
  this.namespace = namespace;
36
34
  this.highlightStage$ = highlightStage$;
37
- this.root$ = new Val<HTMLElement | null>(root);
38
35
 
39
36
  this.stageRect$ = combine(
40
37
  [rootRect$, highlightStage$, ratio$],
@@ -73,7 +70,7 @@ export class TeleStage {
73
70
  );
74
71
 
75
72
  this._sideEffect.addDisposer(
76
- combine([this.root$, highlightStage$]).subscribe(
73
+ combine([root$, highlightStage$]).subscribe(
77
74
  ([root, highlightStage]) => {
78
75
  if (root && highlightStage) {
79
76
  root.appendChild(this.render());
@@ -171,14 +168,6 @@ export class TeleStage {
171
168
  return $boxStage;
172
169
  }
173
170
 
174
- public mount($root: HTMLElement): void {
175
- this.root$.setValue($root);
176
- }
177
-
178
- public unmount(): void {
179
- this.root$.setValue(null);
180
- }
181
-
182
171
  public destroy(): void {
183
172
  this._sideEffect.flushAll();
184
173
  }