|
1 | | -import {gecutContext} from '@gecut/lit-helper/directives/context.js'; |
| 1 | +import {GecutState, mapObject} from '@gecut/lit-helper'; |
2 | 2 | import {GecutLogger} from '@gecut/logger'; |
3 | | -import {ContextSignal} from '@gecut/signal'; |
4 | | -import {map} from 'lit/directives/map.js'; |
5 | | -import {styleMap} from 'lit/directives/style-map.js'; |
| 3 | +import {uid} from '@gecut/utilities/uid.js'; |
| 4 | +import {untilEvent, untilNextFrame} from '@gecut/utilities/wait/wait.js'; |
| 5 | +import {createRef, type Ref} from 'lit/directives/ref.js'; |
| 6 | +import {styleMap, type StyleInfo} from 'lit/directives/style-map.js'; |
6 | 7 | import {html} from 'lit/html.js'; |
7 | 8 |
|
8 | | -import {gecutSnackBar, type SnackBarContent} from './snack-bar.js'; |
| 9 | +import {gecutSnackBar, type SnackBarContent} from './snack-bar'; |
9 | 10 |
|
10 | 11 | export interface SnackBarManagerContent { |
11 | | - position?: { |
12 | | - top?: string; |
13 | | - bottom?: string; |
14 | | - left?: string; |
15 | | - right?: string; |
16 | | - }; |
| 12 | + style?: Readonly<StyleInfo>; |
17 | 13 | } |
18 | 14 |
|
19 | 15 | export class SnackBarManager { |
20 | 16 | constructor(content: SnackBarManagerContent) { |
21 | 17 | this.content = content; |
22 | | - this.snackBars = {}; |
23 | | - this._$updaterContext.value = 'update'; |
24 | | - |
| 18 | + this.state = new GecutState<Record<string, {content: SnackBarContent; ref: Ref<HTMLDivElement>}>>( |
| 19 | + 'snack-bar-manager', |
| 20 | + ); |
25 | 21 | this.html = html` |
26 | | - <div class="flex flex-col absolute inset-x-0" style=${styleMap(this.content?.position ?? {})}> |
27 | | - ${gecutContext(this._$updaterContext, () => |
28 | | - map(Object.keys(this.snackBars), (k) => gecutSnackBar(this.snackBars[k])), |
| 22 | + <div style=${styleMap(this.content.style ?? {})}> |
| 23 | + ${this.state.hydrate((data) => |
| 24 | + mapObject(null, data, (snackBar) => gecutSnackBar(snackBar.content, snackBar.ref)), |
29 | 25 | )} |
30 | 26 | </div> |
31 | 27 | `; |
32 | 28 | } |
33 | 29 |
|
34 | | - content: SnackBarManagerContent = {}; |
35 | | - snackBars: Record<string, ContextSignal<SnackBarContent>> = {}; |
36 | | - html; |
| 30 | + readonly state: GecutState<Record<string, {content: SnackBarContent; ref: Ref<HTMLDivElement>}>>; |
| 31 | + readonly html: unknown; |
| 32 | + readonly content: SnackBarManagerContent; |
37 | 33 |
|
38 | | - protected _$log = new GecutLogger('gecut-snackbar-manager'); |
39 | | - protected _$updaterContext = new ContextSignal<'update'>('gecut-snackbar-updater', 'AnimationFrame'); |
| 34 | + private readonly logger = new GecutLogger('<snack-bar-manager>'); |
| 35 | + private timers: Record<string, NodeJS.Timeout> = {}; |
40 | 36 |
|
41 | 37 | connect(id: string, content: SnackBarContent) { |
42 | | - this._$log.methodArgs?.('connect', {id, content}); |
| 38 | + this.logger.methodArgs?.('connect', {id, content}); |
| 39 | + |
| 40 | + this.state.value = { |
| 41 | + ...(this.state.value ??= {}), |
43 | 42 |
|
44 | | - const context = new ContextSignal<SnackBarContent>(id, 'AnimationFrame'); |
45 | | - context.value = {...content, open: false}; |
| 43 | + [id]: { |
| 44 | + content, |
| 45 | + ref: createRef(), |
| 46 | + }, |
| 47 | + }; |
46 | 48 |
|
47 | | - this.snackBars[id] = context; |
48 | | - this.update(); |
| 49 | + return this.__$waitForRender(id); |
49 | 50 | } |
50 | | - disconnect(id: string) { |
51 | | - this._$log.methodArgs?.('disconnect', {id}); |
52 | 51 |
|
53 | | - this.close(id); |
| 52 | + async disconnect(id: string) { |
| 53 | + this.logger.methodArgs?.('disconnect', {id}); |
| 54 | + |
| 55 | + await this.close(id); |
| 56 | + |
| 57 | + delete this.state.value?.[id]; |
| 58 | + this.state.value = this.state.value ?? {}; |
54 | 59 |
|
55 | | - setTimeout(() => { |
56 | | - delete this.snackBars[id]; |
57 | | - this.update(); |
58 | | - }, 500); |
| 60 | + return untilNextFrame(); |
59 | 61 | } |
60 | 62 |
|
61 | 63 | open(id: string) { |
62 | | - this._$log.methodArgs?.('open', {id}); |
| 64 | + this.logger.methodArgs?.('open', {id}); |
63 | 65 |
|
64 | | - if (!this.snackBars[id]) return this._$log.warning('open', 'id_not_found', `'${id}' not found`); |
| 66 | + const state = (this.state.value ??= {})[id]; |
| 67 | + const element = state.ref.value; |
65 | 68 |
|
66 | | - this.snackBars[id].functionalValue((old) => { |
67 | | - return {...(old ?? {message: ''}), open: true}; |
68 | | - }); |
69 | | - this.update(); |
| 69 | + if (!state) return this.logger.warning('open', 'state_not_exist', `state '${id}' not exist`); |
| 70 | + if (!element) return this.logger.warning('open', 'element_not_exist', `element of state '${id}' not exist`); |
| 71 | + |
| 72 | + element.querySelector<HTMLDivElement>('.actions')?.addEventListener( |
| 73 | + 'click', |
| 74 | + () => { |
| 75 | + this.close(id); |
| 76 | + }, |
| 77 | + {once: true}, |
| 78 | + ); |
| 79 | + |
| 80 | + element.classList.remove('close'); |
| 81 | + element.classList.add('open'); |
70 | 82 | } |
| 83 | + |
71 | 84 | close(id: string) { |
72 | | - this._$log.methodArgs?.('close', {id}); |
| 85 | + this.logger.methodArgs?.('close', {id}); |
| 86 | + |
| 87 | + const state = (this.state.value ??= {})[id]; |
| 88 | + const element = state.ref.value; |
| 89 | + |
| 90 | + if (!state) return this.logger.warning('close', 'state_not_exist', `state '${id}' not exist`); |
| 91 | + if (!element) return this.logger.warning('close', 'element_not_exist', `element of state '${id}' not exist`); |
| 92 | + |
| 93 | + element.classList.remove('open'); |
| 94 | + element.classList.add('close'); |
| 95 | + |
| 96 | + clearTimeout(this.timers[id]); |
| 97 | + |
| 98 | + return untilEvent(element, 'animationend'); |
| 99 | + } |
| 100 | + |
| 101 | + async notify(content: SnackBarContent, timeout?: number) { |
| 102 | + const id = 'snack-bar_' + uid(); |
73 | 103 |
|
74 | | - if (!this.snackBars[id]) return this._$log.warning('close', 'id_not_found', `'${id}' not found`); |
| 104 | + await this.connect(id, content); |
75 | 105 |
|
76 | | - this.snackBars[id].functionalValue((old) => { |
77 | | - return {...(old ?? {message: ''}), open: false}; |
78 | | - }); |
79 | | - this.update(); |
| 106 | + this.open(id); |
| 107 | + |
| 108 | + this.timers[id] = setTimeout( |
| 109 | + async () => this.disconnect(id), |
| 110 | + timeout ?? this.__$readTimeCalc(content.message, (content.action?.label?.length ?? 0) > 10 ? 'high' : 'low'), |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + private __$readTimeCalc(message: string, priority: 'low' | 'medium' | 'high'): number { |
| 115 | + const wordCount = message.split(' ').length; |
| 116 | + |
| 117 | + let baseTime = 100; |
| 118 | + |
| 119 | + switch (priority) { |
| 120 | + case 'low': |
| 121 | + baseTime += 100; |
| 122 | + break; |
| 123 | + case 'medium': |
| 124 | + baseTime += 200; |
| 125 | + break; |
| 126 | + case 'high': |
| 127 | + baseTime += 300; |
| 128 | + break; |
| 129 | + } |
| 130 | + |
| 131 | + let readTime = Math.min(4_000 + baseTime * wordCount, 10_000); |
| 132 | + |
| 133 | + if (wordCount > 20) { |
| 134 | + readTime += (wordCount - 20) * 10; |
| 135 | + } |
| 136 | + |
| 137 | + this.logger.methodFull?.('__$readTimeCalc', {message, priority}, readTime); |
| 138 | + |
| 139 | + return readTime; |
80 | 140 | } |
| 141 | + private async __$waitForRender(id: string): Promise<void> { |
| 142 | + let element; |
| 143 | + |
| 144 | + this.logger.time?.('waitForRender-' + id); |
| 145 | + |
| 146 | + while (element == null) { |
| 147 | + this.logger.methodArgs?.('__$waitForRender', {id}); |
| 148 | + |
| 149 | + await untilNextFrame(); |
| 150 | + |
| 151 | + element = this.state.value?.[id].ref.value; |
| 152 | + } |
| 153 | + |
| 154 | + this.logger.timeEnd?.('waitForRender-' + id); |
81 | 155 |
|
82 | | - protected update() { |
83 | | - this._$log.methodArgs?.('update', {snackBars: this.snackBars}); |
84 | | - this._$updaterContext.renotify(); |
| 156 | + return; |
85 | 157 | } |
86 | 158 | } |
0 commit comments