@netless/telebox-insider 0.2.7 → 0.2.11

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": "0.2.7",
3
+ "version": "0.2.11",
4
4
  "private": false,
5
5
  "description": "Window widget for Whiteboard",
6
6
  "license": "MIT",
@@ -1,3 +1,9 @@
1
+ export enum TELE_BOX_COLOR_SCHEME {
2
+ Light = "light",
3
+ Dark = "dark",
4
+ Auto = "auto",
5
+ }
6
+
1
7
  export enum TELE_BOX_STATE {
2
8
  Normal = "normal",
3
9
  Minimized = "minimized",
@@ -5,6 +11,8 @@ export enum TELE_BOX_STATE {
5
11
  }
6
12
 
7
13
  export enum TELE_BOX_EVENT {
14
+ DarkMode = "dark_mode",
15
+ PrefersColorScheme = "prefers_color_scheme",
8
16
  Close = "close",
9
17
  Focus = "focus",
10
18
  Blur = "blur",
@@ -13,6 +21,7 @@ export enum TELE_BOX_EVENT {
13
21
  IntrinsicMove = "intrinsic_move",
14
22
  IntrinsicResize = "intrinsic_resize",
15
23
  VisualResize = "visual_resize",
24
+ ZIndex = "z_index",
16
25
  State = "state",
17
26
  Minimized = "minimized",
18
27
  Maximized = "maximized",
@@ -26,6 +26,7 @@ import {
26
26
  TELE_BOX_STATE,
27
27
  TELE_BOX_RESIZE_HANDLE,
28
28
  TELE_BOX_DELEGATE_EVENT,
29
+ TELE_BOX_COLOR_SCHEME,
29
30
  } from "./constants";
30
31
  import type {
31
32
  TeleBoxConfig,
@@ -36,12 +37,15 @@ import type {
36
37
  TeleBoxDelegateEvents,
37
38
  TeleBoxCoord,
38
39
  TeleBoxSize,
40
+ TeleBoxColorScheme,
39
41
  } from "./typings";
40
42
 
41
43
  export * from "./constants";
42
44
  export * from "./typings";
43
45
 
44
46
  type ValConfig = {
47
+ prefersColorScheme: Val<TeleBoxColorScheme, boolean>;
48
+ darkMode: Val<boolean, boolean>;
45
49
  containerRect: Val<TeleBoxRect, boolean>;
46
50
  collectorRect: Val<TeleBoxRect | undefined, boolean>;
47
51
  /** Box title. Default empty. */
@@ -74,6 +78,8 @@ export class TeleBox {
74
78
  public constructor({
75
79
  id = genUniqueKey(),
76
80
  title = getBoxDefaultName(),
81
+ prefersColorScheme = TELE_BOX_COLOR_SCHEME.Light,
82
+ darkMode,
77
83
  visible = true,
78
84
  width = 0.5,
79
85
  height = 0.5,
@@ -112,6 +118,62 @@ export class TeleBox {
112
118
  this.events = new EventEmitter();
113
119
  this._delegateEvents = new EventEmitter();
114
120
 
121
+ const prefersColorScheme$ = createVal<TeleBoxColorScheme, boolean>(
122
+ prefersColorScheme
123
+ );
124
+ prefersColorScheme$.reaction((prefersColorScheme, _, skipUpdate) => {
125
+ if (!skipUpdate) {
126
+ this.events.emit(
127
+ TELE_BOX_EVENT.PrefersColorScheme,
128
+ prefersColorScheme
129
+ );
130
+ }
131
+ });
132
+
133
+ const darkMode$ = createVal(Boolean(darkMode));
134
+
135
+ if (darkMode == null) {
136
+ prefersColorScheme$.subscribe(
137
+ (prefersColorScheme, _, skipUpdate) => {
138
+ this._sideEffect.add(() => {
139
+ if (prefersColorScheme === "auto") {
140
+ const prefersDark = window.matchMedia(
141
+ "(prefers-color-scheme: dark)"
142
+ );
143
+ if (prefersDark) {
144
+ darkMode$.setValue(
145
+ prefersDark.matches,
146
+ skipUpdate
147
+ );
148
+ const handler = (
149
+ evt: MediaQueryListEvent
150
+ ): void => {
151
+ darkMode$.setValue(evt.matches, skipUpdate);
152
+ };
153
+ prefersDark.addListener(handler);
154
+ return () =>
155
+ prefersDark.removeListener(handler);
156
+ } else {
157
+ return noop;
158
+ }
159
+ } else {
160
+ darkMode$.setValue(
161
+ prefersColorScheme === "dark",
162
+ skipUpdate
163
+ );
164
+ return noop;
165
+ }
166
+ }, "prefers-color-scheme");
167
+ }
168
+ );
169
+ }
170
+
171
+ darkMode$.reaction((darkMode, _, skipUpdate) => {
172
+ if (!skipUpdate) {
173
+ this.events.emit(TELE_BOX_EVENT.DarkMode, darkMode);
174
+ }
175
+ });
176
+
115
177
  const containerRect$ = createVal(containerRect, shallowequal);
116
178
  const collectorRect$ = createVal(collectorRect, shallowequal);
117
179
 
@@ -135,7 +197,13 @@ export class TeleBox {
135
197
  const draggable$ = createVal(draggable);
136
198
  const fence$ = createVal(fence);
137
199
  const fixRatio$ = createVal(fixRatio);
200
+
138
201
  const zIndex$ = createVal(zIndex);
202
+ zIndex$.reaction((zIndex, _, skipUpdate) => {
203
+ if (!skipUpdate) {
204
+ this.events.emit(TELE_BOX_EVENT.ZIndex, zIndex);
205
+ }
206
+ });
139
207
 
140
208
  const focus$ = createVal(focus);
141
209
  focus$.reaction((focus, _, skipUpdate) => {
@@ -354,6 +422,8 @@ export class TeleBox {
354
422
  const $userStyles$ = createVal(styles);
355
423
 
356
424
  const valConfig: ValConfig = {
425
+ prefersColorScheme: prefersColorScheme$,
426
+ darkMode: darkMode$,
357
427
  containerRect: containerRect$,
358
428
  collectorRect: collectorRect$,
359
429
  title: title$,
@@ -417,6 +487,10 @@ export class TeleBox {
417
487
  public _coord$: Val<TeleBoxCoord, boolean>;
418
488
  public _intrinsicCoord$: Val<TeleBoxCoord, boolean>;
419
489
 
490
+ public get darkMode(): boolean {
491
+ return this._darkMode$.value;
492
+ }
493
+
420
494
  public _state$: Val<TeleBoxState, boolean>;
421
495
 
422
496
  public get state(): TeleBoxState {
@@ -735,6 +809,13 @@ export class TeleBox {
735
809
  bindClassName(this.$box, this._draggable$, "no-drag", isFalsy);
736
810
  bindClassName(this.$box, this._resizable$, "no-resize", isFalsy);
737
811
  bindClassName(this.$box, this._focus$, "blur", isFalsy);
812
+ bindClassName(this.$box, this._darkMode$, "color-scheme-dark");
813
+ bindClassName(
814
+ this.$box,
815
+ this._darkMode$,
816
+ "color-scheme-light",
817
+ isFalsy
818
+ );
738
819
 
739
820
  this._renderSideEffect.add(() => {
740
821
  const minimizedClassName = this.wrapClassName("minimized");
@@ -1184,6 +1265,10 @@ export class TeleBox {
1184
1265
  }
1185
1266
  }
1186
1267
 
1268
+ function noop(): void {
1269
+ return;
1270
+ }
1271
+
1187
1272
  type PropKeys<K = keyof TeleBox> = K extends keyof TeleBox
1188
1273
  ? TeleBox[K] extends Function
1189
1274
  ? never
@@ -202,3 +202,13 @@
202
202
  pointer-events: none !important;
203
203
  }
204
204
  }
205
+
206
+ .#{$namespace}-color-scheme-dark {
207
+ color-scheme: dark;
208
+
209
+ .#{$namespace}-box-main {
210
+ color: #e9e9e9;
211
+ background: #212126;
212
+ border-color: #43434d;
213
+ }
214
+ }
@@ -6,8 +6,11 @@ import type {
6
6
  TELE_BOX_DELEGATE_EVENT,
7
7
  TELE_BOX_RESIZE_HANDLE,
8
8
  TELE_BOX_STATE,
9
+ TELE_BOX_COLOR_SCHEME,
9
10
  } from "./constants";
10
11
 
12
+ export type TeleBoxColorScheme = `${TELE_BOX_COLOR_SCHEME}`;
13
+
11
14
  export type TeleBoxCoord = { x: number; y: number };
12
15
 
13
16
  export type TeleBoxSize = { width: number; height: number };
@@ -26,6 +29,10 @@ export interface TeleBoxConfig {
26
29
  readonly id?: string;
27
30
  /** Box title. Default empty. */
28
31
  readonly title?: string;
32
+ /** Prefers Box color scheme. Default light. */
33
+ readonly prefersColorScheme?: TeleBoxColorScheme;
34
+ /** Actual Box Dark Mode */
35
+ readonly darkMode?: boolean;
29
36
  /** Box visible. Default true. */
30
37
  readonly visible?: boolean;
31
38
  /** Box width relative to root element. 0~1. Default 0.5. */
@@ -79,6 +86,8 @@ export interface TeleBoxConfig {
79
86
  type CheckTeleBoxConfig<T extends Record<`${TELE_BOX_EVENT}`, any>> = T;
80
87
 
81
88
  export type TeleBoxEventConfig = CheckTeleBoxConfig<{
89
+ dark_mode: boolean;
90
+ prefers_color_scheme: TeleBoxColorScheme;
82
91
  close: void;
83
92
  focus: void;
84
93
  blur: void;
@@ -87,6 +96,7 @@ export type TeleBoxEventConfig = CheckTeleBoxConfig<{
87
96
  intrinsic_move: { x: number; y: number };
88
97
  intrinsic_resize: { width: number; height: number };
89
98
  visual_resize: { width: number; height: number };
99
+ z_index: number;
90
100
  state: TeleBoxState;
91
101
  minimized: boolean;
92
102
  maximized: boolean;
@@ -5,6 +5,7 @@ import { TeleStyles } from "../typings";
5
5
  export interface TeleBoxCollectorConfig {
6
6
  visible?: boolean;
7
7
  readonly?: boolean;
8
+ darkMode?: boolean;
8
9
  namespace?: string;
9
10
  styles?: TeleStyles;
10
11
  onClick?: () => void;
@@ -14,12 +15,14 @@ export class TeleBoxCollector {
14
15
  public constructor({
15
16
  visible = true,
16
17
  readonly = false,
18
+ darkMode = false,
17
19
  namespace = "telebox",
18
20
  styles = {},
19
21
  onClick,
20
22
  }: TeleBoxCollectorConfig = {}) {
21
- this._visible = Boolean(visible);
22
- this._readonly = Boolean(readonly);
23
+ this._visible = visible;
24
+ this._readonly = readonly;
25
+ this._darkMode = darkMode;
23
26
  this.namespace = namespace;
24
27
  this.styles = styles;
25
28
  this.onClick = onClick;
@@ -37,6 +40,10 @@ export class TeleBoxCollector {
37
40
  return this._readonly;
38
41
  }
39
42
 
43
+ public get darkMode(): boolean {
44
+ return this._darkMode;
45
+ }
46
+
40
47
  public onClick: (() => void) | undefined;
41
48
 
42
49
  public $collector: HTMLElement | undefined;
@@ -85,6 +92,23 @@ export class TeleBoxCollector {
85
92
  return this;
86
93
  }
87
94
 
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
102
+ );
103
+ this.$collector.classList.toggle(
104
+ this.wrapClassName("color-scheme-light"),
105
+ !darkMode
106
+ );
107
+ }
108
+ }
109
+ return this;
110
+ }
111
+
88
112
  public setStyles(styles: TeleStyles): this {
89
113
  Object.assign(this.styles, styles);
90
114
  if (this.$collector) {
@@ -103,6 +127,7 @@ export class TeleBoxCollector {
103
127
  if (!this.$collector) {
104
128
  this.$collector = document.createElement("button");
105
129
  this.$collector.className = this.wrapClassName("collector");
130
+ this.$collector.style.backgroundImage = `url('${collectorSVG}')`;
106
131
  this.$collector.addEventListener(
107
132
  "click",
108
133
  this.handleCollectorClick
@@ -120,12 +145,11 @@ export class TeleBoxCollector {
120
145
  );
121
146
  }
122
147
 
123
- const $icon = document.createElement("img");
124
- $icon.draggable = false;
125
- $icon.className = this.wrapClassName("collector-icon");
126
- $icon.src = collectorSVG;
127
-
128
- this.$collector.appendChild($icon);
148
+ this.$collector.classList.add(
149
+ this.wrapClassName(
150
+ this._darkMode ? "color-scheme-dark" : "color-scheme-light"
151
+ )
152
+ );
129
153
 
130
154
  this.setStyles(this.styles);
131
155
  }
@@ -153,6 +177,8 @@ export class TeleBoxCollector {
153
177
 
154
178
  protected _readonly: boolean;
155
179
 
180
+ protected _darkMode: boolean;
181
+
156
182
  protected handleCollectorClick = (): void => {
157
183
  if (!this._readonly && this.onClick) {
158
184
  this.onClick();
@@ -18,11 +18,9 @@
18
18
  cursor: pointer;
19
19
  user-select: none;
20
20
  pointer-events: none;
21
- }
22
-
23
- .#{$namespace}-collector-icon {
24
- width: 18px;
25
- height: 16px;
21
+ background-repeat: no-repeat;
22
+ background-size: 18px 16px;
23
+ background-position: center;
26
24
  }
27
25
 
28
26
  .#{$namespace}-collector-visible {
@@ -33,3 +31,11 @@
33
31
  .#{$namespace}-collector-readonly {
34
32
  cursor: not-allowed;
35
33
  }
34
+
35
+ .#{$namespace}-color-scheme-dark {
36
+ color-scheme: dark;
37
+
38
+ &.#{$namespace}-collector {
39
+ background-color: #43434d;
40
+ }
41
+ }
@@ -8,6 +8,7 @@ import { TeleBoxRect, TeleBoxState } from "../../TeleBox/typings";
8
8
  export type MaxTitleBarTeleBox = Pick<TeleBox, "id" | "title" | "readonly">;
9
9
 
10
10
  export interface MaxTitleBarConfig extends DefaultTitleBarConfig {
11
+ darkMode: boolean;
11
12
  boxes: MaxTitleBarTeleBox[];
12
13
  containerRect: TeleBoxRect;
13
14
  focusedBox?: MaxTitleBarTeleBox;
@@ -20,13 +21,10 @@ export class MaxTitleBar extends DefaultTitleBar {
20
21
  this.boxes = config.boxes;
21
22
  this.focusedBox = config.focusedBox;
22
23
  this.containerRect = config.containerRect;
24
+ this.darkMode = config.darkMode;
23
25
  }
24
26
 
25
27
  public focusBox(box?: MaxTitleBarTeleBox): void {
26
- if (!box) {
27
- box = this.boxes[this.boxes.length - 1];
28
- }
29
-
30
28
  if (this.focusedBox && this.focusedBox === box) {
31
29
  return;
32
30
  }
@@ -89,6 +87,22 @@ export class MaxTitleBar extends DefaultTitleBar {
89
87
  }
90
88
  }
91
89
 
90
+ public setDarkMode(darkMode: boolean): void {
91
+ if (darkMode !== this.darkMode) {
92
+ this.darkMode = darkMode;
93
+ if (this.$titleBar) {
94
+ this.$titleBar.classList.toggle(
95
+ this.wrapClassName("color-scheme-dark"),
96
+ darkMode
97
+ );
98
+ this.$titleBar.classList.toggle(
99
+ this.wrapClassName("color-scheme-light"),
100
+ !darkMode
101
+ );
102
+ }
103
+ }
104
+ }
105
+
92
106
  public render(): HTMLElement {
93
107
  const $titleBar = super.render();
94
108
  const { x, y, width } = this.containerRect;
@@ -104,6 +118,11 @@ export class MaxTitleBar extends DefaultTitleBar {
104
118
  this.wrapClassName("readonly"),
105
119
  this.readonly
106
120
  );
121
+ $titleBar.classList.add(
122
+ this.wrapClassName(
123
+ this.darkMode ? "color-scheme-dark" : "color-scheme-light"
124
+ )
125
+ );
107
126
 
108
127
  this.updateTitles();
109
128
 
@@ -171,11 +190,13 @@ export class MaxTitleBar extends DefaultTitleBar {
171
190
  return this.$titles;
172
191
  }
173
192
 
193
+ protected darkMode: boolean;
194
+
174
195
  protected $titles: HTMLElement | undefined;
175
196
 
176
197
  protected boxes: MaxTitleBarTeleBox[];
177
198
 
178
- protected focusedBox: MaxTitleBarTeleBox | undefined;
199
+ public focusedBox: MaxTitleBarTeleBox | undefined;
179
200
 
180
201
  protected containerRect: TeleBoxRect;
181
202
  }
@@ -123,3 +123,21 @@ $scrollbar-color-active: rgba(238, 238, 247, 1);
123
123
  cursor: not-allowed;
124
124
  }
125
125
  }
126
+
127
+ .#{$namespace}-color-scheme-dark {
128
+ color-scheme: dark;
129
+
130
+ &.#{$namespace}-titlebar {
131
+ color: #e9e9e9;
132
+ background: #43434d;
133
+ border-bottom: none;
134
+ }
135
+
136
+ .#{$namespace}-titles-tab {
137
+ border-right-color: #7b88a0;
138
+ }
139
+
140
+ .#{$namespace}-title {
141
+ color: #e9e9e9;
142
+ }
143
+ }
@@ -1,5 +1,6 @@
1
1
  export enum TELE_BOX_MANAGER_EVENT {
2
2
  Focused = "focused",
3
+ Blurred = "blurred",
3
4
  Created = "created",
4
5
  Removed = "removed",
5
6
  State = "state",
@@ -10,4 +11,7 @@ export enum TELE_BOX_MANAGER_EVENT {
10
11
  IntrinsicMove = "intrinsic_move",
11
12
  IntrinsicResize = "intrinsic_resize",
12
13
  VisualResize = "visual_resize",
14
+ ZIndex = "z_index",
15
+ PrefersColorScheme = "prefers_color_scheme",
16
+ DarkMode = "dark_mode",
13
17
  }