@aerogel/core 0.0.0-next.7f6ed5a1f91688a86bf5ede2adc465e4fd6cfdea → 0.0.0-next.824cf5311c4335d119158f507dad094ed4f4f0b6
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/dist/aerogel-core.cjs.js +1 -1
- package/dist/aerogel-core.cjs.js.map +1 -1
- package/dist/aerogel-core.d.ts +1630 -204
- package/dist/aerogel-core.esm.js +1 -1
- package/dist/aerogel-core.esm.js.map +1 -1
- package/histoire.config.ts +7 -0
- package/noeldemartin.config.js +4 -1
- package/package.json +15 -8
- package/postcss.config.js +6 -0
- package/src/assets/histoire.css +3 -0
- package/src/bootstrap/bootstrap.test.ts +4 -59
- package/src/bootstrap/index.ts +31 -28
- package/src/bootstrap/options.ts +8 -1
- package/src/components/AGAppLayout.vue +7 -2
- package/src/components/AGAppModals.vue +15 -0
- package/src/components/AGAppOverlays.vue +10 -8
- package/src/components/AGAppSnackbars.vue +13 -0
- package/src/components/constants.ts +8 -0
- package/src/components/forms/AGButton.vue +36 -3
- package/src/components/forms/AGCheckbox.vue +41 -0
- package/src/components/forms/AGInput.vue +15 -9
- package/src/components/forms/AGSelect.story.vue +46 -0
- package/src/components/forms/AGSelect.vue +60 -0
- package/src/components/forms/index.ts +5 -5
- package/src/components/headless/forms/AGHeadlessButton.vue +7 -7
- package/src/components/headless/forms/AGHeadlessInput.ts +23 -3
- package/src/components/headless/forms/AGHeadlessInput.vue +13 -10
- package/src/components/headless/forms/AGHeadlessInputError.vue +9 -5
- package/src/components/headless/forms/AGHeadlessInputInput.vue +20 -4
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +22 -0
- package/src/components/headless/forms/AGHeadlessSelect.ts +42 -0
- package/src/components/headless/forms/AGHeadlessSelect.vue +77 -0
- package/src/components/headless/forms/AGHeadlessSelectButton.vue +24 -0
- package/src/components/headless/forms/AGHeadlessSelectError.vue +26 -0
- package/src/components/headless/forms/AGHeadlessSelectLabel.vue +24 -0
- package/src/components/headless/forms/AGHeadlessSelectOption.ts +4 -0
- package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
- package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
- package/src/components/headless/forms/index.ts +14 -4
- package/src/components/headless/index.ts +1 -0
- package/src/components/headless/modals/AGHeadlessModal.ts +27 -0
- package/src/components/headless/modals/AGHeadlessModal.vue +8 -6
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +10 -2
- package/src/components/headless/modals/index.ts +4 -6
- package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
- package/src/components/headless/snackbars/index.ts +40 -0
- package/src/components/index.ts +5 -2
- package/src/components/lib/AGErrorMessage.vue +16 -0
- package/src/components/lib/AGLink.vue +9 -0
- package/src/components/lib/AGMarkdown.vue +36 -0
- package/src/components/lib/AGMeasured.vue +15 -0
- package/src/components/lib/AGStartupCrash.vue +31 -0
- package/src/components/lib/index.ts +5 -0
- package/src/components/modals/AGAlertModal.ts +15 -0
- package/src/components/modals/AGAlertModal.vue +4 -5
- package/src/components/modals/AGConfirmModal.ts +27 -0
- package/src/components/modals/AGConfirmModal.vue +26 -0
- package/src/components/modals/AGErrorReportModal.ts +46 -0
- package/src/components/modals/AGErrorReportModal.vue +54 -0
- package/src/components/modals/AGErrorReportModalButtons.vue +111 -0
- package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
- package/src/components/modals/AGLoadingModal.ts +23 -0
- package/src/components/modals/AGLoadingModal.vue +15 -0
- package/src/components/modals/AGModal.ts +5 -1
- package/src/components/modals/AGModal.vue +26 -5
- package/src/components/modals/AGModalTitle.vue +9 -0
- package/src/components/modals/AGPromptModal.ts +30 -0
- package/src/components/modals/AGPromptModal.vue +34 -0
- package/src/components/modals/index.ts +16 -4
- package/src/components/snackbars/AGSnackbar.vue +36 -0
- package/src/components/snackbars/index.ts +3 -0
- package/src/components/utils.ts +10 -0
- package/src/directives/index.ts +21 -4
- package/src/directives/measure.ts +12 -0
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +192 -0
- package/src/errors/index.ts +45 -0
- package/src/forms/Form.test.ts +21 -0
- package/src/forms/Form.ts +48 -17
- package/src/forms/utils.ts +17 -0
- package/src/jobs/Job.ts +5 -0
- package/src/jobs/index.ts +7 -0
- package/src/lang/Lang.ts +48 -9
- package/src/lang/index.ts +17 -76
- package/src/lang/utils.ts +4 -0
- package/src/main.histoire.ts +1 -0
- package/src/main.ts +3 -0
- package/src/plugins/Plugin.ts +8 -0
- package/src/plugins/index.ts +26 -0
- package/src/services/App.state.ts +17 -0
- package/src/services/App.ts +47 -0
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +88 -30
- package/src/services/Service.ts +195 -33
- package/src/services/index.ts +33 -7
- package/src/services/store.ts +30 -0
- package/src/types/vite.d.ts +0 -2
- package/src/ui/UI.state.ts +12 -6
- package/src/ui/UI.ts +177 -12
- package/src/ui/index.ts +31 -16
- package/src/utils/composition/events.ts +1 -0
- package/src/utils/composition/forms.ts +11 -0
- package/src/utils/composition/hooks.ts +9 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/markdown.ts +11 -2
- package/src/utils/tailwindcss.test.ts +26 -0
- package/src/utils/tailwindcss.ts +7 -0
- package/src/utils/vue.ts +15 -4
- package/tailwind.config.js +4 -0
- package/tsconfig.json +2 -10
- package/vite.config.ts +3 -6
- package/.eslintrc.js +0 -3
- package/src/bootstrap/hooks.ts +0 -19
- package/src/components/basic/AGMarkdown.vue +0 -20
- package/src/components/basic/index.ts +0 -3
- package/src/lang/helpers.ts +0 -5
- package/src/models/index.ts +0 -18
- package/src/routing/index.ts +0 -33
- package/src/testing/stubs/lang/en.yaml +0 -1
- package/src/testing/stubs/models/User.ts +0 -3
package/src/services/Service.ts
CHANGED
|
@@ -1,50 +1,126 @@
|
|
|
1
|
-
import { MagicObject, PromisedValue } from '@noeldemartin/utils';
|
|
2
|
-
import { reactive } from 'vue';
|
|
1
|
+
import { MagicObject, PromisedValue, Storage, isEmpty, objectDeepClone, objectOnly } from '@noeldemartin/utils';
|
|
3
2
|
import type { Constructor } from '@noeldemartin/utils';
|
|
3
|
+
import type { MaybeRef } from 'vue';
|
|
4
|
+
import type { Store } from 'pinia';
|
|
4
5
|
|
|
5
6
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
7
|
+
import { defineServiceStore } from '@/services/store';
|
|
6
8
|
|
|
7
9
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
8
|
-
export type DefaultServiceState =
|
|
10
|
+
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
11
|
export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
|
|
12
|
+
export type UnrefServiceState<State extends ServiceState> = {
|
|
13
|
+
[K in keyof State]: State[K] extends MaybeRef<infer T> ? T : State[K];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
17
|
+
[K in keyof TComputedState]: (state: UnrefServiceState<TState>) => TComputedState[K];
|
|
18
|
+
} & ThisType<{
|
|
19
|
+
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
export function defineServiceState<
|
|
23
|
+
State extends ServiceState = ServiceState,
|
|
24
|
+
ComputedState extends ServiceState = {}
|
|
25
|
+
>(options: {
|
|
26
|
+
name: string;
|
|
27
|
+
initialState: State | (() => State);
|
|
28
|
+
persist?: (keyof State)[];
|
|
29
|
+
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
30
|
+
serialize?: (state: Partial<State>) => Partial<State>;
|
|
31
|
+
}): Constructor<UnrefServiceState<State>> &
|
|
32
|
+
Constructor<ComputedState> &
|
|
33
|
+
Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>> {
|
|
34
|
+
return class extends Service<UnrefServiceState<State>, ComputedState> {
|
|
35
|
+
|
|
36
|
+
public static persist = (options.persist as string[]) ?? [];
|
|
37
|
+
|
|
38
|
+
protected usesStore(): boolean {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected getName(): string | null {
|
|
43
|
+
return options.name ?? null;
|
|
44
|
+
}
|
|
10
45
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
46
|
+
protected getInitialState(): UnrefServiceState<State> {
|
|
47
|
+
if (typeof options.initialState === 'function') {
|
|
48
|
+
return options.initialState();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return Object.entries(options.initialState).reduce((state, [key, value]) => {
|
|
52
|
+
try {
|
|
53
|
+
value = structuredClone(value);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
// eslint-disable-next-line no-console
|
|
56
|
+
console.warn(
|
|
57
|
+
`Could not clone '${key}' state from ${this.getName()} service, ` +
|
|
58
|
+
'this may cause problems if you\'re using multiple instances of the service ' +
|
|
59
|
+
'(for example, in unit tests).\n' +
|
|
60
|
+
'To fix this problem, declare your initialState as a function instead.',
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
state[key as keyof State] = value;
|
|
65
|
+
|
|
66
|
+
return state;
|
|
67
|
+
}, {} as UnrefServiceState<State>);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<UnrefServiceState<State>, ComputedState> {
|
|
71
|
+
return (options.computed ?? {}) as ComputedStateDefinition<UnrefServiceState<State>, ComputedState>;
|
|
72
|
+
}
|
|
15
73
|
|
|
16
|
-
protected
|
|
17
|
-
return options.
|
|
74
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
75
|
+
return options.serialize?.(state) ?? state;
|
|
18
76
|
}
|
|
19
|
-
|
|
20
|
-
} as unknown as Constructor<State
|
|
77
|
+
|
|
78
|
+
} as unknown as Constructor<UnrefServiceState<State>> &
|
|
79
|
+
Constructor<ComputedState> &
|
|
80
|
+
Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>>;
|
|
21
81
|
}
|
|
22
82
|
|
|
23
|
-
export default class Service<
|
|
83
|
+
export default class Service<
|
|
84
|
+
State extends ServiceState = DefaultServiceState,
|
|
85
|
+
ComputedState extends ServiceState = {},
|
|
86
|
+
ServiceStorage extends Partial<State> = Partial<State>
|
|
87
|
+
> extends MagicObject {
|
|
88
|
+
|
|
89
|
+
public static persist: string[] = [];
|
|
24
90
|
|
|
25
|
-
protected
|
|
91
|
+
protected _name: string;
|
|
26
92
|
private _booted: PromisedValue<void>;
|
|
27
|
-
private
|
|
93
|
+
private _computedStateKeys: Set<keyof State>;
|
|
94
|
+
private _store: Store | false;
|
|
28
95
|
|
|
29
96
|
constructor() {
|
|
30
97
|
super();
|
|
31
98
|
|
|
32
|
-
|
|
99
|
+
const getters = this.getComputedStateDefinition();
|
|
100
|
+
|
|
101
|
+
this._name = this.getName() ?? new.target.name;
|
|
33
102
|
this._booted = new PromisedValue();
|
|
34
|
-
this.
|
|
103
|
+
this._computedStateKeys = new Set(Object.keys(getters));
|
|
104
|
+
this._store =
|
|
105
|
+
this.usesStore() &&
|
|
106
|
+
defineServiceStore(this._name, {
|
|
107
|
+
state: () => this.getInitialState(),
|
|
108
|
+
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
110
|
+
getters: getters as any,
|
|
111
|
+
});
|
|
35
112
|
}
|
|
36
113
|
|
|
37
114
|
public get booted(): PromisedValue<void> {
|
|
38
115
|
return this._booted;
|
|
39
116
|
}
|
|
40
117
|
|
|
41
|
-
public launch(
|
|
42
|
-
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this.
|
|
43
|
-
|
|
44
|
-
this._namespace = namespace ?? this._namespace;
|
|
118
|
+
public launch(): Promise<void> {
|
|
119
|
+
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
45
120
|
|
|
46
121
|
try {
|
|
47
|
-
this.
|
|
122
|
+
this.frameworkBoot()
|
|
123
|
+
.then(() => this.boot())
|
|
48
124
|
.then(() => this._booted.resolve())
|
|
49
125
|
.catch(handleError);
|
|
50
126
|
} catch (error) {
|
|
@@ -54,38 +130,124 @@ export default class Service<State extends ServiceState = DefaultServiceState> e
|
|
|
54
130
|
return this._booted;
|
|
55
131
|
}
|
|
56
132
|
|
|
133
|
+
public hasPersistedState(): boolean {
|
|
134
|
+
return Storage.has(this._name);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public hasState<P extends keyof State>(property: P): boolean {
|
|
138
|
+
if (!this._store) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return property in this._store.$state || this._computedStateKeys.has(property);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public getState(): State;
|
|
146
|
+
public getState<P extends keyof State>(property: P): State[P];
|
|
147
|
+
public getState<P extends keyof State>(property?: P): State | State[P] {
|
|
148
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
149
|
+
const store = this._store as any;
|
|
150
|
+
|
|
151
|
+
if (property) {
|
|
152
|
+
return store ? store[property] : undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return store ? store : {};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public setState<P extends keyof State>(property: P, value: State[P]): void;
|
|
159
|
+
public setState(state: Partial<State>): void;
|
|
160
|
+
public setState<P extends keyof State>(stateOrProperty: P | Partial<State>, value?: State[P]): void {
|
|
161
|
+
if (!this._store) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const state = (
|
|
166
|
+
typeof stateOrProperty === 'string' ? { [stateOrProperty]: value } : stateOrProperty
|
|
167
|
+
) as Partial<State>;
|
|
168
|
+
|
|
169
|
+
Object.assign(this._store.$state, state);
|
|
170
|
+
|
|
171
|
+
this.onStateUpdated(state);
|
|
172
|
+
}
|
|
173
|
+
|
|
57
174
|
protected __get(property: string): unknown {
|
|
58
|
-
if (
|
|
59
|
-
return
|
|
175
|
+
if (this.hasState(property)) {
|
|
176
|
+
return this.getState(property);
|
|
60
177
|
}
|
|
61
178
|
|
|
62
|
-
return
|
|
179
|
+
return super.__get(property);
|
|
63
180
|
}
|
|
64
181
|
|
|
65
182
|
protected __set(property: string, value: unknown): void {
|
|
66
183
|
this.setState({ [property]: value } as Partial<State>);
|
|
67
184
|
}
|
|
68
185
|
|
|
69
|
-
protected
|
|
70
|
-
|
|
186
|
+
protected onStateUpdated(state: Partial<State>): void {
|
|
187
|
+
// TODO fix this.static()
|
|
188
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
189
|
+
const persisted = objectOnly(state, persist);
|
|
190
|
+
|
|
191
|
+
if (isEmpty(persisted)) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const storage = Storage.get<ServiceStorage>(this._name);
|
|
196
|
+
|
|
197
|
+
if (!storage) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
Storage.set(this._name, {
|
|
202
|
+
...storage,
|
|
203
|
+
...this.serializePersistedState(objectDeepClone(persisted) as Partial<State>),
|
|
204
|
+
});
|
|
71
205
|
}
|
|
72
206
|
|
|
73
|
-
protected
|
|
74
|
-
|
|
75
|
-
protected getState<P extends keyof State>(property?: P): State | State[P] {
|
|
76
|
-
return property ? this._state[property] : this._state;
|
|
207
|
+
protected usesStore(): boolean {
|
|
208
|
+
return false;
|
|
77
209
|
}
|
|
78
210
|
|
|
79
|
-
protected
|
|
80
|
-
|
|
211
|
+
protected getName(): string | null {
|
|
212
|
+
return null;
|
|
81
213
|
}
|
|
82
214
|
|
|
83
215
|
protected getInitialState(): State {
|
|
84
216
|
return {} as State;
|
|
85
217
|
}
|
|
86
218
|
|
|
219
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
|
|
220
|
+
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
224
|
+
return state;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
protected async frameworkBoot(): Promise<void> {
|
|
228
|
+
this.initializePersistedState();
|
|
229
|
+
}
|
|
230
|
+
|
|
87
231
|
protected async boot(): Promise<void> {
|
|
88
|
-
//
|
|
232
|
+
// Placeholder for overrides, don't place any functionality here.
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
protected initializePersistedState(): void {
|
|
236
|
+
// TODO fix this.static()
|
|
237
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
238
|
+
|
|
239
|
+
if (!this.usesStore() || isEmpty(persist)) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (Storage.has(this._name)) {
|
|
244
|
+
const persisted = Storage.require<ServiceStorage>(this._name);
|
|
245
|
+
this.setState(persisted);
|
|
246
|
+
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
Storage.set(this._name, objectOnly(this.getState(), persist));
|
|
89
251
|
}
|
|
90
252
|
|
|
91
253
|
}
|
package/src/services/index.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
import type { App } from 'vue';
|
|
1
|
+
import type { App as VueApp } from 'vue';
|
|
2
2
|
|
|
3
|
+
import { definePlugin } from '@/plugins';
|
|
4
|
+
|
|
5
|
+
import App from './App';
|
|
3
6
|
import Events from './Events';
|
|
4
7
|
import Service from './Service';
|
|
5
|
-
import {
|
|
8
|
+
import { getPiniaStore } from './store';
|
|
6
9
|
|
|
10
|
+
export * from './App';
|
|
7
11
|
export * from './Events';
|
|
8
12
|
export * from './Service';
|
|
13
|
+
export * from './store';
|
|
9
14
|
|
|
10
|
-
export { Events, Service };
|
|
15
|
+
export { App, Events, Service };
|
|
11
16
|
|
|
12
17
|
const defaultServices = {
|
|
18
|
+
$app: App,
|
|
13
19
|
$events: Events,
|
|
14
20
|
};
|
|
15
21
|
|
|
@@ -17,18 +23,38 @@ export type DefaultServices = typeof defaultServices;
|
|
|
17
23
|
|
|
18
24
|
export interface Services extends DefaultServices {}
|
|
19
25
|
|
|
20
|
-
export async function bootServices(app:
|
|
26
|
+
export async function bootServices(app: VueApp, services: Record<string, Service>): Promise<void> {
|
|
21
27
|
await Promise.all(
|
|
22
28
|
Object.entries(services).map(async ([name, service]) => {
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
await service
|
|
30
|
+
.launch()
|
|
31
|
+
.catch((error) => app.config.errorHandler?.(error, null, `Failed launching ${name}.`));
|
|
25
32
|
}),
|
|
26
33
|
);
|
|
27
34
|
|
|
28
35
|
Object.assign(app.config.globalProperties, services);
|
|
36
|
+
|
|
37
|
+
App.development && Object.assign(window, services);
|
|
29
38
|
}
|
|
30
39
|
|
|
31
|
-
export default
|
|
40
|
+
export default definePlugin({
|
|
41
|
+
async install(app, options) {
|
|
42
|
+
const services = {
|
|
43
|
+
...defaultServices,
|
|
44
|
+
...options.services,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
app.use(getPiniaStore());
|
|
48
|
+
|
|
49
|
+
await bootServices(app, services);
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
declare module '@/bootstrap/options' {
|
|
54
|
+
export interface AerogelOptions {
|
|
55
|
+
services?: Record<string, Service>;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
32
58
|
|
|
33
59
|
declare module '@vue/runtime-core' {
|
|
34
60
|
interface ComponentCustomProperties extends Services {}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { tap } from '@noeldemartin/utils';
|
|
2
|
+
import { createPinia, defineStore, setActivePinia } from 'pinia';
|
|
3
|
+
import type { DefineStoreOptions, Pinia, StateTree, Store, _GettersTree } from 'pinia';
|
|
4
|
+
|
|
5
|
+
let _store: Pinia | null = null;
|
|
6
|
+
|
|
7
|
+
function initializePiniaStore(): Pinia {
|
|
8
|
+
return _store ?? resetPiniaStore();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function resetPiniaStore(): Pinia {
|
|
12
|
+
return tap(createPinia(), (store) => {
|
|
13
|
+
_store = store;
|
|
14
|
+
|
|
15
|
+
setActivePinia(store);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getPiniaStore(): Pinia {
|
|
20
|
+
return _store ?? initializePiniaStore();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function defineServiceStore<Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(
|
|
24
|
+
name: Id,
|
|
25
|
+
options: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>,
|
|
26
|
+
): Store<Id, S, G, A> {
|
|
27
|
+
initializePiniaStore();
|
|
28
|
+
|
|
29
|
+
return defineStore(name, options)();
|
|
30
|
+
}
|
package/src/types/vite.d.ts
CHANGED
package/src/ui/UI.state.ts
CHANGED
|
@@ -2,10 +2,6 @@ import type { Component } from 'vue';
|
|
|
2
2
|
|
|
3
3
|
import { defineServiceState } from '@/services/Service';
|
|
4
4
|
|
|
5
|
-
export interface State {
|
|
6
|
-
modals: Modal[];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
5
|
export interface Modal<T = unknown> {
|
|
10
6
|
id: string;
|
|
11
7
|
properties: Record<string, unknown>;
|
|
@@ -21,6 +17,16 @@ export interface ModalComponent<
|
|
|
21
17
|
Result = unknown
|
|
22
18
|
> {}
|
|
23
19
|
|
|
24
|
-
export
|
|
25
|
-
|
|
20
|
+
export interface Snackbar {
|
|
21
|
+
id: string;
|
|
22
|
+
component: Component;
|
|
23
|
+
properties: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default defineServiceState({
|
|
27
|
+
name: 'ui',
|
|
28
|
+
initialState: {
|
|
29
|
+
modals: [] as Modal[],
|
|
30
|
+
snackbars: [] as Snackbar[],
|
|
31
|
+
},
|
|
26
32
|
});
|
package/src/ui/UI.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { facade, fail, uuid } from '@noeldemartin/utils';
|
|
1
|
+
import { after, facade, fail, uuid } from '@noeldemartin/utils';
|
|
2
2
|
import { markRaw, nextTick } from 'vue';
|
|
3
3
|
import type { Component } from 'vue';
|
|
4
4
|
import type { ObjectValues } from '@noeldemartin/utils';
|
|
5
5
|
|
|
6
6
|
import Events from '@/services/Events';
|
|
7
|
+
import type { SnackbarAction, SnackbarColor } from '@/components/headless/snackbars';
|
|
8
|
+
import type { AGAlertModalProps, AGConfirmModalProps, AGLoadingModalProps, AGPromptModalProps } from '@/components';
|
|
7
9
|
|
|
8
10
|
import Service from './UI.state';
|
|
9
|
-
import type { Modal, ModalComponent } from './UI.state';
|
|
11
|
+
import type { Modal, ModalComponent, Snackbar } from './UI.state';
|
|
10
12
|
|
|
11
13
|
interface ModalCallbacks<T = unknown> {
|
|
12
14
|
willClose(result: T | undefined): void;
|
|
@@ -20,17 +22,164 @@ type ModalResult<TComponent> = TComponent extends ModalComponent<Record<string,
|
|
|
20
22
|
|
|
21
23
|
export const UIComponents = {
|
|
22
24
|
AlertModal: 'alert-modal',
|
|
25
|
+
ConfirmModal: 'confirm-modal',
|
|
26
|
+
ErrorReportModal: 'error-report-modal',
|
|
27
|
+
LoadingModal: 'loading-modal',
|
|
28
|
+
PromptModal: 'prompt-modal',
|
|
29
|
+
Snackbar: 'snackbar',
|
|
30
|
+
StartupCrash: 'startup-crash',
|
|
23
31
|
} as const;
|
|
24
32
|
|
|
25
33
|
export type UIComponent = ObjectValues<typeof UIComponents>;
|
|
26
34
|
|
|
35
|
+
export interface ConfirmOptions {
|
|
36
|
+
acceptText?: string;
|
|
37
|
+
cancelText?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface PromptOptions {
|
|
41
|
+
label?: string;
|
|
42
|
+
defaultValue?: string;
|
|
43
|
+
placeholder?: string;
|
|
44
|
+
acceptText?: string;
|
|
45
|
+
cancelText?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ShowSnackbarOptions {
|
|
49
|
+
component?: Component;
|
|
50
|
+
color?: SnackbarColor;
|
|
51
|
+
actions?: SnackbarAction[];
|
|
52
|
+
}
|
|
53
|
+
|
|
27
54
|
export class UIService extends Service {
|
|
28
55
|
|
|
29
56
|
private modalCallbacks: Record<string, Partial<ModalCallbacks>> = {};
|
|
30
57
|
private components: Partial<Record<UIComponent, Component>> = {};
|
|
31
58
|
|
|
32
|
-
public
|
|
33
|
-
this.
|
|
59
|
+
public requireComponent(name: UIComponent): Component {
|
|
60
|
+
return this.components[name] ?? fail(`UI Component '${name}' is not defined!`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public alert(message: string): void;
|
|
64
|
+
public alert(title: string, message: string): void;
|
|
65
|
+
public alert(messageOrTitle: string, message?: string): void {
|
|
66
|
+
const getProperties = (): AGAlertModalProps => {
|
|
67
|
+
if (typeof message !== 'string') {
|
|
68
|
+
return { message: messageOrTitle };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
title: messageOrTitle,
|
|
73
|
+
message,
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
this.openModal(this.requireComponent(UIComponents.AlertModal), getProperties());
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public async confirm(message: string, options?: ConfirmOptions): Promise<boolean>;
|
|
81
|
+
public async confirm(title: string, message: string, options?: ConfirmOptions): Promise<boolean>;
|
|
82
|
+
public async confirm(
|
|
83
|
+
messageOrTitle: string,
|
|
84
|
+
messageOrOptions?: string | ConfirmOptions,
|
|
85
|
+
options?: ConfirmOptions,
|
|
86
|
+
): Promise<boolean> {
|
|
87
|
+
const getProperties = (): AGConfirmModalProps => {
|
|
88
|
+
if (typeof messageOrOptions !== 'string') {
|
|
89
|
+
return {
|
|
90
|
+
message: messageOrTitle,
|
|
91
|
+
...(messageOrOptions ?? {}),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
title: messageOrTitle,
|
|
97
|
+
message: messageOrOptions,
|
|
98
|
+
...(options ?? {}),
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const modal = await this.openModal<ModalComponent<AGConfirmModalProps, boolean>>(
|
|
103
|
+
this.requireComponent(UIComponents.ConfirmModal),
|
|
104
|
+
getProperties(),
|
|
105
|
+
);
|
|
106
|
+
const result = await modal.beforeClose;
|
|
107
|
+
|
|
108
|
+
return result ?? false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public async prompt(message: string, options?: PromptOptions): Promise<string | null>;
|
|
112
|
+
public async prompt(title: string, message: string, options?: PromptOptions): Promise<string | null>;
|
|
113
|
+
public async prompt(
|
|
114
|
+
messageOrTitle: string,
|
|
115
|
+
messageOrOptions?: string | PromptOptions,
|
|
116
|
+
options?: PromptOptions,
|
|
117
|
+
): Promise<string | null> {
|
|
118
|
+
const getProperties = (): AGPromptModalProps => {
|
|
119
|
+
if (typeof messageOrOptions !== 'string') {
|
|
120
|
+
return {
|
|
121
|
+
message: messageOrTitle,
|
|
122
|
+
...(messageOrOptions ?? {}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
title: messageOrTitle,
|
|
128
|
+
message: messageOrOptions,
|
|
129
|
+
...(options ?? {}),
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const modal = await this.openModal<ModalComponent<AGPromptModalProps, string | null>>(
|
|
134
|
+
this.requireComponent(UIComponents.PromptModal),
|
|
135
|
+
getProperties(),
|
|
136
|
+
);
|
|
137
|
+
const result = await modal.beforeClose;
|
|
138
|
+
|
|
139
|
+
return result ?? null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public async loading<T>(operation: Promise<T>): Promise<T>;
|
|
143
|
+
public async loading<T>(message: string, operation: Promise<T>): Promise<T>;
|
|
144
|
+
public async loading<T>(messageOrOperation: string | Promise<T>, operation?: Promise<T>): Promise<T> {
|
|
145
|
+
const getProperties = (): AGLoadingModalProps => {
|
|
146
|
+
if (typeof messageOrOperation !== 'string') {
|
|
147
|
+
return {};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return { message: messageOrOperation };
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const modal = await this.openModal(this.requireComponent(UIComponents.LoadingModal), getProperties());
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
operation = typeof messageOrOperation === 'string' ? (operation as Promise<T>) : messageOrOperation;
|
|
157
|
+
|
|
158
|
+
const [result] = await Promise.all([operation, after({ seconds: 1 })]);
|
|
159
|
+
|
|
160
|
+
return result;
|
|
161
|
+
} finally {
|
|
162
|
+
await this.closeModal(modal.id);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
public showSnackbar(message: string, options: ShowSnackbarOptions = {}): void {
|
|
167
|
+
const snackbar: Snackbar = {
|
|
168
|
+
id: uuid(),
|
|
169
|
+
properties: { message, ...options },
|
|
170
|
+
component: options.component ?? markRaw(this.requireComponent(UIComponents.Snackbar)),
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
this.setState('snackbars', this.snackbars.concat(snackbar));
|
|
174
|
+
|
|
175
|
+
setTimeout(() => this.hideSnackbar(snackbar.id), 5000);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public hideSnackbar(id: string): void {
|
|
179
|
+
this.setState(
|
|
180
|
+
'snackbars',
|
|
181
|
+
this.snackbars.filter((snackbar) => snackbar.id !== id),
|
|
182
|
+
);
|
|
34
183
|
}
|
|
35
184
|
|
|
36
185
|
public registerComponent(name: UIComponent, component: Component): void {
|
|
@@ -72,13 +221,8 @@ export class UIService extends Service {
|
|
|
72
221
|
}
|
|
73
222
|
|
|
74
223
|
protected async boot(): Promise<void> {
|
|
75
|
-
await super.boot();
|
|
76
|
-
|
|
77
224
|
this.watchModalEvents();
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
private requireComponent(name: UIComponent): Component {
|
|
81
|
-
return this.components[name] ?? fail(`UI Component '${name}' is not defined!`);
|
|
225
|
+
this.watchMountedEvent();
|
|
82
226
|
}
|
|
83
227
|
|
|
84
228
|
private watchModalEvents(): void {
|
|
@@ -91,7 +235,10 @@ export class UIService extends Service {
|
|
|
91
235
|
});
|
|
92
236
|
|
|
93
237
|
Events.on('modal-closed', async ({ modal, result }) => {
|
|
94
|
-
this.setState(
|
|
238
|
+
this.setState(
|
|
239
|
+
'modals',
|
|
240
|
+
this.modals.filter((m) => m.id !== modal.id),
|
|
241
|
+
);
|
|
95
242
|
|
|
96
243
|
this.modalCallbacks[modal.id]?.closed?.(result);
|
|
97
244
|
|
|
@@ -103,9 +250,27 @@ export class UIService extends Service {
|
|
|
103
250
|
});
|
|
104
251
|
}
|
|
105
252
|
|
|
253
|
+
private watchMountedEvent(): void {
|
|
254
|
+
Events.once('application-mounted', async () => {
|
|
255
|
+
const splash = document.getElementById('splash');
|
|
256
|
+
|
|
257
|
+
if (!splash) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (window.getComputedStyle(splash).opacity !== '0') {
|
|
262
|
+
splash.style.opacity = '0';
|
|
263
|
+
|
|
264
|
+
await after({ ms: 600 });
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
splash.remove();
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
106
271
|
}
|
|
107
272
|
|
|
108
|
-
export default facade(
|
|
273
|
+
export default facade(UIService);
|
|
109
274
|
|
|
110
275
|
declare module '@/services/Events' {
|
|
111
276
|
export interface EventsPayload {
|