@aerogel/core 0.0.0-next.926bde19326fe7b6b24b277666936862b64d8295 → 0.0.0-next.a56c0f4966eb71571173f8502f3f36d357ceebc7
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 +860 -126
- 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 +14 -4
- package/postcss.config.js +6 -0
- package/src/assets/histoire.css +3 -0
- package/src/bootstrap/bootstrap.test.ts +1 -0
- package/src/bootstrap/index.ts +14 -2
- package/src/components/AGAppLayout.vue +6 -2
- package/src/components/AGAppModals.vue +15 -0
- package/src/components/AGAppOverlays.vue +5 -7
- package/src/components/AGAppSnackbars.vue +13 -0
- package/src/components/constants.ts +8 -0
- package/src/components/forms/AGButton.vue +33 -10
- package/src/components/forms/AGCheckbox.vue +35 -0
- package/src/components/forms/AGInput.vue +8 -4
- package/src/components/forms/AGSelect.story.vue +28 -0
- package/src/components/forms/AGSelect.vue +53 -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 +2 -2
- package/src/components/headless/forms/AGHeadlessInput.vue +7 -7
- package/src/components/headless/forms/AGHeadlessInputError.vue +1 -1
- package/src/components/headless/forms/AGHeadlessInputInput.vue +15 -3
- package/src/components/headless/forms/AGHeadlessSelect.ts +43 -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 +21 -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 +9 -1
- package/src/components/headless/index.ts +1 -0
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +5 -1
- package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
- package/src/components/headless/snackbars/index.ts +25 -0
- package/src/components/index.ts +3 -1
- 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/AGStartupCrash.vue +31 -0
- package/src/components/lib/index.ts +4 -0
- package/src/components/modals/AGAlertModal.vue +1 -2
- package/src/components/modals/AGConfirmModal.vue +4 -4
- package/src/components/modals/AGErrorReportModal.ts +20 -0
- package/src/components/modals/AGErrorReportModal.vue +62 -0
- package/src/components/modals/AGErrorReportModalButtons.vue +109 -0
- package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
- package/src/components/modals/AGLoadingModal.vue +19 -0
- package/src/components/modals/AGModal.ts +1 -1
- package/src/components/modals/AGModal.vue +23 -4
- package/src/components/modals/AGModalTitle.vue +9 -0
- package/src/components/modals/index.ts +18 -2
- package/src/components/snackbars/AGSnackbar.vue +42 -0
- package/src/components/snackbars/index.ts +3 -0
- package/src/directives/index.ts +16 -3
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +183 -0
- package/src/errors/index.ts +55 -0
- package/src/forms/Form.ts +26 -15
- package/src/forms/utils.ts +17 -0
- package/src/lang/Lang.ts +12 -4
- package/src/lang/index.ts +3 -5
- package/src/lang/utils.ts +4 -0
- package/src/main.histoire.ts +1 -0
- package/src/main.ts +1 -2
- package/src/plugins/Plugin.ts +1 -0
- package/src/plugins/index.ts +19 -0
- package/src/services/App.state.ts +10 -2
- package/src/services/App.ts +24 -2
- package/src/services/Service.ts +132 -45
- package/src/services/index.ts +21 -4
- package/src/services/store.ts +27 -0
- package/src/ui/UI.state.ts +11 -1
- package/src/ui/UI.ts +75 -9
- package/src/ui/index.ts +7 -1
- package/src/utils/composition/forms.ts +11 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/markdown.ts +11 -2
- package/src/utils/vue.ts +5 -3
- package/tailwind.config.js +4 -0
- package/tsconfig.json +1 -0
- package/vite.config.ts +2 -1
- package/.eslintrc.js +0 -3
- package/src/components/basic/AGMarkdown.vue +0 -35
- package/src/components/basic/index.ts +0 -3
- package/src/globals.ts +0 -6
package/src/services/Service.ts
CHANGED
|
@@ -1,27 +1,42 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { MagicObject, PromisedValue } from '@noeldemartin/utils';
|
|
3
|
-
import type { ComputedRef } from 'vue';
|
|
1
|
+
import { MagicObject, PromisedValue, Storage, isEmpty, objectDeepClone, objectOnly } from '@noeldemartin/utils';
|
|
4
2
|
import type { Constructor } from '@noeldemartin/utils';
|
|
3
|
+
import type { Store } from 'pinia';
|
|
5
4
|
|
|
6
5
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
6
|
+
import { defineServiceStore } from '@/services/store';
|
|
7
7
|
|
|
8
8
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
|
-
export type DefaultServiceState =
|
|
9
|
+
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
10
10
|
export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
|
|
11
11
|
|
|
12
12
|
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
13
13
|
[K in keyof TComputedState]: (state: TState) => TComputedState[K];
|
|
14
|
-
}
|
|
14
|
+
} & ThisType<{
|
|
15
|
+
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
16
|
+
}>;
|
|
15
17
|
|
|
16
18
|
export function defineServiceState<
|
|
17
19
|
State extends ServiceState = ServiceState,
|
|
18
20
|
ComputedState extends ServiceState = {}
|
|
19
21
|
>(options: {
|
|
22
|
+
name: string;
|
|
20
23
|
initialState: State;
|
|
24
|
+
persist?: (keyof State)[];
|
|
21
25
|
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
22
|
-
|
|
26
|
+
serialize?: (state: Partial<State>) => Partial<State>;
|
|
27
|
+
}): Constructor<State> & Constructor<ComputedState> & Constructor<Service<State, ComputedState, Partial<State>>> {
|
|
23
28
|
return class extends Service<State, ComputedState> {
|
|
24
29
|
|
|
30
|
+
public static persist = (options.persist as string[]) ?? [];
|
|
31
|
+
|
|
32
|
+
protected usesStore(): boolean {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
protected getName(): string | null {
|
|
37
|
+
return options.name ?? null;
|
|
38
|
+
}
|
|
39
|
+
|
|
25
40
|
protected getInitialState(): State {
|
|
26
41
|
return options.initialState;
|
|
27
42
|
}
|
|
@@ -29,47 +44,57 @@ export function defineServiceState<
|
|
|
29
44
|
protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
|
|
30
45
|
return options.computed ?? ({} as ComputedStateDefinition<State, ComputedState>);
|
|
31
46
|
}
|
|
47
|
+
|
|
48
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
49
|
+
return options.serialize?.(state) ?? state;
|
|
50
|
+
}
|
|
32
51
|
|
|
33
|
-
} as unknown as Constructor<State> &
|
|
52
|
+
} as unknown as Constructor<State> &
|
|
53
|
+
Constructor<ComputedState> &
|
|
54
|
+
Constructor<Service<State, ComputedState, Partial<State>>>;
|
|
34
55
|
}
|
|
35
56
|
|
|
36
57
|
export default class Service<
|
|
37
58
|
State extends ServiceState = DefaultServiceState,
|
|
38
|
-
ComputedState extends ServiceState = {}
|
|
59
|
+
ComputedState extends ServiceState = {},
|
|
60
|
+
ServiceStorage extends Partial<State> = Partial<State>
|
|
39
61
|
> extends MagicObject {
|
|
40
62
|
|
|
41
|
-
|
|
63
|
+
public static persist: string[] = [];
|
|
64
|
+
|
|
65
|
+
protected _name: string;
|
|
42
66
|
private _booted: PromisedValue<void>;
|
|
43
|
-
private
|
|
44
|
-
private
|
|
67
|
+
private _computedStateKeys: Set<keyof State>;
|
|
68
|
+
private _store?: Store | false;
|
|
45
69
|
|
|
46
70
|
constructor() {
|
|
47
71
|
super();
|
|
48
72
|
|
|
49
|
-
|
|
50
|
-
this._booted = new PromisedValue();
|
|
51
|
-
this._state = reactive(this.getInitialState());
|
|
52
|
-
this._computedState = Object.entries(this.getComputedStateDefinition()).reduce(
|
|
53
|
-
(computedState, [name, method]) => {
|
|
54
|
-
computedState[name as keyof ComputedState] = computed(() => method(this._state));
|
|
73
|
+
const getters = this.getComputedStateDefinition();
|
|
55
74
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
75
|
+
this._name = this.getName() ?? new.target.name;
|
|
76
|
+
this._booted = new PromisedValue();
|
|
77
|
+
this._computedStateKeys = new Set(Object.keys(getters));
|
|
78
|
+
this._store =
|
|
79
|
+
this.usesStore() &&
|
|
80
|
+
defineServiceStore(this._name, {
|
|
81
|
+
state: () => this.getInitialState(),
|
|
82
|
+
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
getters: getters as any,
|
|
85
|
+
});
|
|
60
86
|
}
|
|
61
87
|
|
|
62
88
|
public get booted(): PromisedValue<void> {
|
|
63
89
|
return this._booted;
|
|
64
90
|
}
|
|
65
91
|
|
|
66
|
-
public launch(
|
|
67
|
-
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this.
|
|
68
|
-
|
|
69
|
-
this._namespace = namespace ?? this._namespace;
|
|
92
|
+
public launch(): Promise<void> {
|
|
93
|
+
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
70
94
|
|
|
71
95
|
try {
|
|
72
|
-
this.
|
|
96
|
+
this.frameworkBoot()
|
|
97
|
+
.then(() => this.boot())
|
|
73
98
|
.then(() => this._booted.resolve())
|
|
74
99
|
.catch(handleError);
|
|
75
100
|
} catch (error) {
|
|
@@ -79,15 +104,48 @@ export default class Service<
|
|
|
79
104
|
return this._booted;
|
|
80
105
|
}
|
|
81
106
|
|
|
107
|
+
public hasState<P extends keyof State>(property: P): boolean {
|
|
108
|
+
if (!this._store) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return property in this._store.$state || this._computedStateKeys.has(property);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public getState(): State;
|
|
116
|
+
public getState<P extends keyof State>(property: P): State[P];
|
|
117
|
+
public getState<P extends keyof State>(property?: P): State | State[P] {
|
|
118
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
119
|
+
const store = this._store as any;
|
|
120
|
+
|
|
121
|
+
if (property) {
|
|
122
|
+
return store ? store[property] : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return store ? store : {};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public setState<P extends keyof State>(property: P, value: State[P]): void;
|
|
129
|
+
public setState(state: Partial<State>): void;
|
|
130
|
+
public setState<P extends keyof State>(stateOrProperty: P | Partial<State>, value?: State[P]): void {
|
|
131
|
+
if (!this._store) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const state = (
|
|
136
|
+
typeof stateOrProperty === 'string' ? { [stateOrProperty]: value } : stateOrProperty
|
|
137
|
+
) as Partial<State>;
|
|
138
|
+
|
|
139
|
+
Object.assign(this._store.$state, state);
|
|
140
|
+
|
|
141
|
+
this.onStateUpdated(state);
|
|
142
|
+
}
|
|
143
|
+
|
|
82
144
|
protected __get(property: string): unknown {
|
|
83
145
|
if (this.hasState(property)) {
|
|
84
146
|
return this.getState(property);
|
|
85
147
|
}
|
|
86
148
|
|
|
87
|
-
if (this.hasComputedState(property)) {
|
|
88
|
-
return this.getComputedState(property);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
149
|
return super.__get(property);
|
|
92
150
|
}
|
|
93
151
|
|
|
@@ -95,26 +153,29 @@ export default class Service<
|
|
|
95
153
|
this.setState({ [property]: value } as Partial<State>);
|
|
96
154
|
}
|
|
97
155
|
|
|
98
|
-
protected
|
|
99
|
-
|
|
100
|
-
|
|
156
|
+
protected onStateUpdated(state: Partial<State>): void {
|
|
157
|
+
// TODO fix this.static()
|
|
158
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
159
|
+
const persisted = objectOnly(state, persist);
|
|
101
160
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
161
|
+
if (isEmpty(persisted)) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
105
164
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
165
|
+
const storage = Storage.require<ServiceStorage>(this._name);
|
|
166
|
+
|
|
167
|
+
Storage.set(this._name, {
|
|
168
|
+
...storage,
|
|
169
|
+
...this.serializePersistedState(objectDeepClone(persisted) as Partial<State>),
|
|
170
|
+
});
|
|
110
171
|
}
|
|
111
172
|
|
|
112
|
-
protected
|
|
113
|
-
return
|
|
173
|
+
protected usesStore(): boolean {
|
|
174
|
+
return false;
|
|
114
175
|
}
|
|
115
176
|
|
|
116
|
-
protected
|
|
117
|
-
|
|
177
|
+
protected getName(): string | null {
|
|
178
|
+
return null;
|
|
118
179
|
}
|
|
119
180
|
|
|
120
181
|
protected getInitialState(): State {
|
|
@@ -125,8 +186,34 @@ export default class Service<
|
|
|
125
186
|
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
126
187
|
}
|
|
127
188
|
|
|
189
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
190
|
+
return state;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
protected async frameworkBoot(): Promise<void> {
|
|
194
|
+
this.restorePersistedState();
|
|
195
|
+
}
|
|
196
|
+
|
|
128
197
|
protected async boot(): Promise<void> {
|
|
129
|
-
//
|
|
198
|
+
// Override.
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
protected restorePersistedState(): void {
|
|
202
|
+
// TODO fix this.static()
|
|
203
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
204
|
+
|
|
205
|
+
if (!this.usesStore() || isEmpty(persist)) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (Storage.has(this._name)) {
|
|
210
|
+
const persisted = Storage.require<ServiceStorage>(this._name);
|
|
211
|
+
this.setState(persisted);
|
|
212
|
+
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
Storage.set(this._name, objectOnly(this.getState(), persist));
|
|
130
217
|
}
|
|
131
218
|
|
|
132
219
|
}
|
package/src/services/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { definePlugin } from '@/plugins';
|
|
|
5
5
|
import App from './App';
|
|
6
6
|
import Events from './Events';
|
|
7
7
|
import Service from './Service';
|
|
8
|
+
import { getPiniaStore } from './store';
|
|
8
9
|
|
|
9
10
|
export * from './App';
|
|
10
11
|
export * from './Events';
|
|
@@ -24,20 +25,36 @@ export interface Services extends DefaultServices {}
|
|
|
24
25
|
export async function bootServices(app: VueApp, services: Record<string, Service>): Promise<void> {
|
|
25
26
|
await Promise.all(
|
|
26
27
|
Object.entries(services).map(async ([name, service]) => {
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
await service
|
|
29
|
+
.launch()
|
|
30
|
+
.catch((error) => app.config.errorHandler?.(error, null, `Failed launching ${name}.`));
|
|
29
31
|
}),
|
|
30
32
|
);
|
|
31
33
|
|
|
32
34
|
Object.assign(app.config.globalProperties, services);
|
|
35
|
+
|
|
36
|
+
App.development && Object.assign(window, services);
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
export default definePlugin({
|
|
36
|
-
async install(app) {
|
|
37
|
-
|
|
40
|
+
async install(app, options) {
|
|
41
|
+
const services = {
|
|
42
|
+
...defaultServices,
|
|
43
|
+
...options.services,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
app.use(getPiniaStore());
|
|
47
|
+
|
|
48
|
+
await bootServices(app, services);
|
|
38
49
|
},
|
|
39
50
|
});
|
|
40
51
|
|
|
52
|
+
declare module '@/bootstrap/options' {
|
|
53
|
+
interface AerogelOptions {
|
|
54
|
+
services?: Record<string, Service>;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
41
58
|
declare module '@vue/runtime-core' {
|
|
42
59
|
interface ComponentCustomProperties extends Services {}
|
|
43
60
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createPinia, defineStore, setActivePinia } from 'pinia';
|
|
2
|
+
import type { DefineStoreOptions, Pinia, StateTree, Store, _GettersTree } from 'pinia';
|
|
3
|
+
|
|
4
|
+
let _store: Pinia | null = null;
|
|
5
|
+
|
|
6
|
+
function initializePiniaStore(): Pinia {
|
|
7
|
+
if (!_store) {
|
|
8
|
+
_store = createPinia();
|
|
9
|
+
|
|
10
|
+
setActivePinia(_store);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return _store;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getPiniaStore(): Pinia {
|
|
17
|
+
return _store ?? initializePiniaStore();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function defineServiceStore<Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(
|
|
21
|
+
name: Id,
|
|
22
|
+
options: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>,
|
|
23
|
+
): Store<Id, S, G, A> {
|
|
24
|
+
initializePiniaStore();
|
|
25
|
+
|
|
26
|
+
return defineStore(name, options)();
|
|
27
|
+
}
|
package/src/ui/UI.state.ts
CHANGED
|
@@ -17,6 +17,16 @@ export interface ModalComponent<
|
|
|
17
17
|
Result = unknown
|
|
18
18
|
> {}
|
|
19
19
|
|
|
20
|
+
export interface Snackbar {
|
|
21
|
+
id: string;
|
|
22
|
+
component: Component;
|
|
23
|
+
properties: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
|
|
20
26
|
export default defineServiceState({
|
|
21
|
-
|
|
27
|
+
name: 'ui',
|
|
28
|
+
initialState: {
|
|
29
|
+
modals: [] as Modal[],
|
|
30
|
+
snackbars: [] as Snackbar[],
|
|
31
|
+
},
|
|
22
32
|
});
|
package/src/ui/UI.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
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';
|
|
7
8
|
|
|
8
9
|
import Service from './UI.state';
|
|
9
|
-
import type { Modal, ModalComponent } from './UI.state';
|
|
10
|
+
import type { Modal, ModalComponent, Snackbar } from './UI.state';
|
|
10
11
|
|
|
11
12
|
interface ModalCallbacks<T = unknown> {
|
|
12
13
|
willClose(result: T | undefined): void;
|
|
@@ -21,15 +22,28 @@ type ModalResult<TComponent> = TComponent extends ModalComponent<Record<string,
|
|
|
21
22
|
export const UIComponents = {
|
|
22
23
|
AlertModal: 'alert-modal',
|
|
23
24
|
ConfirmModal: 'confirm-modal',
|
|
25
|
+
ErrorReportModal: 'error-report-modal',
|
|
26
|
+
LoadingModal: 'loading-modal',
|
|
27
|
+
Snackbar: 'snackbar',
|
|
24
28
|
} as const;
|
|
25
29
|
|
|
26
30
|
export type UIComponent = ObjectValues<typeof UIComponents>;
|
|
27
31
|
|
|
32
|
+
export interface ShowSnackbarOptions {
|
|
33
|
+
component?: Component;
|
|
34
|
+
color?: SnackbarColor;
|
|
35
|
+
actions?: SnackbarAction[];
|
|
36
|
+
}
|
|
37
|
+
|
|
28
38
|
export class UIService extends Service {
|
|
29
39
|
|
|
30
40
|
private modalCallbacks: Record<string, Partial<ModalCallbacks>> = {};
|
|
31
41
|
private components: Partial<Record<UIComponent, Component>> = {};
|
|
32
42
|
|
|
43
|
+
public requireComponent(name: UIComponent): Component {
|
|
44
|
+
return this.components[name] ?? fail(`UI Component '${name}' is not defined!`);
|
|
45
|
+
}
|
|
46
|
+
|
|
33
47
|
public alert(message: string): void;
|
|
34
48
|
public alert(title: string, message: string): void;
|
|
35
49
|
public alert(messageOrTitle: string, message?: string): void {
|
|
@@ -51,6 +65,42 @@ export class UIService extends Service {
|
|
|
51
65
|
return result ?? false;
|
|
52
66
|
}
|
|
53
67
|
|
|
68
|
+
public async loading<T>(operation: Promise<T>): Promise<T>;
|
|
69
|
+
public async loading<T>(message: string, operation: Promise<T>): Promise<T>;
|
|
70
|
+
public async loading<T>(messageOrOperation: string | Promise<T>, operation?: Promise<T>): Promise<T> {
|
|
71
|
+
operation = typeof messageOrOperation === 'string' ? (operation as Promise<T>) : messageOrOperation;
|
|
72
|
+
|
|
73
|
+
const message = typeof messageOrOperation === 'string' ? messageOrOperation : undefined;
|
|
74
|
+
const modal = await this.openModal(this.requireComponent(UIComponents.LoadingModal), { message });
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const [result] = await Promise.all([operation, after({ seconds: 1 })]);
|
|
78
|
+
|
|
79
|
+
return result;
|
|
80
|
+
} finally {
|
|
81
|
+
await this.closeModal(modal.id);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public showSnackbar(message: string, options: ShowSnackbarOptions = {}): void {
|
|
86
|
+
const snackbar: Snackbar = {
|
|
87
|
+
id: uuid(),
|
|
88
|
+
properties: { message, ...options },
|
|
89
|
+
component: options.component ?? markRaw(this.requireComponent(UIComponents.Snackbar)),
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
this.setState('snackbars', this.snackbars.concat(snackbar));
|
|
93
|
+
|
|
94
|
+
setTimeout(() => this.hideSnackbar(snackbar.id), 5000);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public hideSnackbar(id: string): void {
|
|
98
|
+
this.setState(
|
|
99
|
+
'snackbars',
|
|
100
|
+
this.snackbars.filter((snackbar) => snackbar.id !== id),
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
54
104
|
public registerComponent(name: UIComponent, component: Component): void {
|
|
55
105
|
this.components[name] = component;
|
|
56
106
|
}
|
|
@@ -90,13 +140,8 @@ export class UIService extends Service {
|
|
|
90
140
|
}
|
|
91
141
|
|
|
92
142
|
protected async boot(): Promise<void> {
|
|
93
|
-
await super.boot();
|
|
94
|
-
|
|
95
143
|
this.watchModalEvents();
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
private requireComponent(name: UIComponent): Component {
|
|
99
|
-
return this.components[name] ?? fail(`UI Component '${name}' is not defined!`);
|
|
144
|
+
this.watchMountedEvent();
|
|
100
145
|
}
|
|
101
146
|
|
|
102
147
|
private watchModalEvents(): void {
|
|
@@ -109,7 +154,10 @@ export class UIService extends Service {
|
|
|
109
154
|
});
|
|
110
155
|
|
|
111
156
|
Events.on('modal-closed', async ({ modal, result }) => {
|
|
112
|
-
this.setState(
|
|
157
|
+
this.setState(
|
|
158
|
+
'modals',
|
|
159
|
+
this.modals.filter((m) => m.id !== modal.id),
|
|
160
|
+
);
|
|
113
161
|
|
|
114
162
|
this.modalCallbacks[modal.id]?.closed?.(result);
|
|
115
163
|
|
|
@@ -121,6 +169,24 @@ export class UIService extends Service {
|
|
|
121
169
|
});
|
|
122
170
|
}
|
|
123
171
|
|
|
172
|
+
private watchMountedEvent(): void {
|
|
173
|
+
Events.once('application-mounted', async () => {
|
|
174
|
+
const splash = document.getElementById('splash');
|
|
175
|
+
|
|
176
|
+
if (!splash) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (window.getComputedStyle(splash).opacity !== '0') {
|
|
181
|
+
splash.style.opacity = '0';
|
|
182
|
+
|
|
183
|
+
await after({ ms: 600 });
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
splash.remove();
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
124
190
|
}
|
|
125
191
|
|
|
126
192
|
export default facade(new UIService());
|
package/src/ui/index.ts
CHANGED
|
@@ -6,6 +6,9 @@ import { definePlugin } from '@/plugins';
|
|
|
6
6
|
import UI, { UIComponents } from './UI';
|
|
7
7
|
import AGAlertModal from '../components/modals/AGAlertModal.vue';
|
|
8
8
|
import AGConfirmModal from '../components/modals/AGConfirmModal.vue';
|
|
9
|
+
import AGErrorReportModal from '../components/modals/AGErrorReportModal.vue';
|
|
10
|
+
import AGLoadingModal from '../components/modals/AGLoadingModal.vue';
|
|
11
|
+
import AGSnackbar from '../components/snackbars/AGSnackbar.vue';
|
|
9
12
|
import type { UIComponent } from './UI';
|
|
10
13
|
|
|
11
14
|
export { UI, UIComponents, UIComponent };
|
|
@@ -19,6 +22,9 @@ export default definePlugin({
|
|
|
19
22
|
const defaultComponents = {
|
|
20
23
|
[UIComponents.AlertModal]: AGAlertModal,
|
|
21
24
|
[UIComponents.ConfirmModal]: AGConfirmModal,
|
|
25
|
+
[UIComponents.ErrorReportModal]: AGErrorReportModal,
|
|
26
|
+
[UIComponents.LoadingModal]: AGLoadingModal,
|
|
27
|
+
[UIComponents.Snackbar]: AGSnackbar,
|
|
22
28
|
};
|
|
23
29
|
|
|
24
30
|
Object.entries({
|
|
@@ -37,5 +43,5 @@ declare module '@/bootstrap/options' {
|
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
declare module '@/services' {
|
|
40
|
-
interface Services extends UIServices {}
|
|
46
|
+
export interface Services extends UIServices {}
|
|
41
47
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { objectWithout } from '@noeldemartin/utils';
|
|
2
|
+
import { computed, useAttrs } from 'vue';
|
|
3
|
+
import type { ComputedRef } from 'vue';
|
|
4
|
+
|
|
5
|
+
export function useInputAttrs(): [ComputedRef<{}>, ComputedRef<unknown>] {
|
|
6
|
+
const attrs = useAttrs();
|
|
7
|
+
const className = computed(() => attrs.class);
|
|
8
|
+
const inputAttrs = computed(() => objectWithout(attrs, 'class'));
|
|
9
|
+
|
|
10
|
+
return [inputAttrs, className];
|
|
11
|
+
}
|
package/src/utils/index.ts
CHANGED
package/src/utils/markdown.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
|
+
import { tap } from '@noeldemartin/utils';
|
|
1
2
|
import DOMPurify from 'dompurify';
|
|
2
|
-
import { marked } from 'marked';
|
|
3
|
+
import { Renderer, marked } from 'marked';
|
|
4
|
+
|
|
5
|
+
function makeRenderer(): Renderer {
|
|
6
|
+
return tap(new Renderer(), (renderer) => {
|
|
7
|
+
renderer.link = function(href, title, text) {
|
|
8
|
+
return Renderer.prototype.link.apply(this, [href, title, text]).replace('<a', '<a target="_blank"');
|
|
9
|
+
};
|
|
10
|
+
});
|
|
11
|
+
}
|
|
3
12
|
|
|
4
13
|
export function renderMarkdown(markdown: string): string {
|
|
5
|
-
return safeHtml(marked(markdown, { mangle: false, headerIds: false }));
|
|
14
|
+
return safeHtml(marked(markdown, { mangle: false, headerIds: false, renderer: makeRenderer() }));
|
|
6
15
|
}
|
|
7
16
|
|
|
8
17
|
export function safeHtml(html: string): string {
|
package/src/utils/vue.ts
CHANGED
|
@@ -3,13 +3,15 @@ import { inject, reactive, ref } from 'vue';
|
|
|
3
3
|
import type { Directive, InjectionKey, PropType, Ref, UnwrapNestedRefs } from 'vue';
|
|
4
4
|
|
|
5
5
|
type BaseProp<T> = {
|
|
6
|
-
type
|
|
6
|
+
type?: PropType<T>;
|
|
7
7
|
validator?(value: unknown): boolean;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
type RequiredProp<T> = BaseProp<T> & { required: true };
|
|
11
11
|
type OptionalProp<T> = BaseProp<T> & { default: T | (() => T) | null };
|
|
12
12
|
|
|
13
|
+
export type ComponentProps = Record<string, unknown>;
|
|
14
|
+
|
|
13
15
|
export function arrayProp<T>(defaultValue?: () => T[]): OptionalProp<T[]> {
|
|
14
16
|
return {
|
|
15
17
|
type: Array as PropType<T[]>,
|
|
@@ -62,7 +64,7 @@ export function injectOrFail<T>(key: InjectionKey<T> | string, errorMessage?: st
|
|
|
62
64
|
return inject(key) ?? fail(errorMessage ?? `Could not resolve '${key}' injection key`);
|
|
63
65
|
}
|
|
64
66
|
|
|
65
|
-
export function mixedProp<T>(type
|
|
67
|
+
export function mixedProp<T>(type?: PropType<T>): OptionalProp<T | null> {
|
|
66
68
|
return {
|
|
67
69
|
type,
|
|
68
70
|
default: null,
|
|
@@ -106,7 +108,7 @@ export function requiredEnumProp<Enum extends Record<string, unknown>>(
|
|
|
106
108
|
};
|
|
107
109
|
}
|
|
108
110
|
|
|
109
|
-
export function requiredMixedProp<T>(type
|
|
111
|
+
export function requiredMixedProp<T>(type?: PropType<T>): RequiredProp<T> {
|
|
110
112
|
return {
|
|
111
113
|
type,
|
|
112
114
|
required: true,
|
package/tsconfig.json
CHANGED
package/vite.config.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import Aerogel from '@aerogel/vite';
|
|
2
|
+
import Icons from 'unplugin-icons/vite';
|
|
2
3
|
import { defineConfig } from 'vitest/config';
|
|
3
4
|
import { resolve } from 'path';
|
|
4
5
|
|
|
5
6
|
export default defineConfig({
|
|
6
7
|
test: { clearMocks: true },
|
|
7
|
-
plugins: [Aerogel()],
|
|
8
|
+
plugins: [Aerogel(), Icons()],
|
|
8
9
|
resolve: {
|
|
9
10
|
alias: {
|
|
10
11
|
'@': resolve(__dirname, './src'),
|
package/.eslintrc.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<root />
|
|
3
|
-
</template>
|
|
4
|
-
|
|
5
|
-
<script setup lang="ts">
|
|
6
|
-
import { computed, h } from 'vue';
|
|
7
|
-
|
|
8
|
-
import { renderMarkdown } from '@/utils/markdown';
|
|
9
|
-
import { booleanProp, stringProp } from '@/utils/vue';
|
|
10
|
-
import { translate } from '@/lang';
|
|
11
|
-
|
|
12
|
-
const props = defineProps({
|
|
13
|
-
as: stringProp('div'),
|
|
14
|
-
langKey: stringProp(),
|
|
15
|
-
text: stringProp(),
|
|
16
|
-
inline: booleanProp(),
|
|
17
|
-
raw: booleanProp(),
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const markdown = computed(() => props.text ?? (props.langKey && translate(props.langKey)));
|
|
21
|
-
const html = computed(() => {
|
|
22
|
-
if (!markdown.value) {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
let html = renderMarkdown(markdown.value);
|
|
27
|
-
|
|
28
|
-
if (props.inline) {
|
|
29
|
-
html = html.replace('<p>', '<span>').replace('</p>', '</span>');
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return html;
|
|
33
|
-
});
|
|
34
|
-
const root = () => h(props.as, { class: props.raw ? '' : 'prose', innerHTML: html.value });
|
|
35
|
-
</script>
|