@aerogel/core 0.0.0-next.7f6ed5a1f91688a86bf5ede2adc465e4fd6cfdea → 0.0.0-next.88c59e62f64db70aedfbc4c31b5bbc287be44483
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 +830 -104
- package/dist/aerogel-core.esm.js +1 -1
- package/dist/aerogel-core.esm.js.map +1 -1
- package/dist/virtual.d.ts +11 -0
- package/noeldemartin.config.js +4 -1
- package/package.json +7 -9
- package/src/bootstrap/bootstrap.test.ts +0 -56
- package/src/bootstrap/index.ts +12 -25
- package/src/bootstrap/options.ts +5 -1
- package/src/components/AGAppModals.vue +15 -0
- package/src/components/AGAppOverlays.vue +5 -7
- package/src/components/AGAppSnackbars.vue +13 -0
- package/src/components/basic/AGErrorMessage.vue +16 -0
- package/src/components/basic/AGLink.vue +9 -0
- package/src/components/basic/AGMarkdown.vue +21 -5
- package/src/components/basic/index.ts +3 -1
- package/src/components/constants.ts +8 -0
- package/src/components/forms/AGButton.vue +36 -3
- package/src/components/forms/AGCheckbox.vue +35 -0
- package/src/components/forms/AGInput.vue +8 -4
- package/src/components/forms/index.ts +4 -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 +6 -6
- 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 +16 -0
- package/src/components/headless/forms/AGHeadlessSelect.ts +31 -0
- package/src/components/headless/forms/AGHeadlessSelect.vue +45 -0
- package/src/components/headless/forms/AGHeadlessSelectButton.ts +3 -0
- package/src/components/headless/forms/AGHeadlessSelectLabel.ts +3 -0
- package/src/components/headless/forms/AGHeadlessSelectOption.ts +8 -0
- package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
- package/src/components/headless/forms/index.ts +12 -4
- package/src/components/headless/index.ts +1 -0
- package/src/components/headless/modals/AGHeadlessModal.vue +5 -1
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +10 -2
- package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
- package/src/components/headless/snackbars/index.ts +25 -0
- package/src/components/index.ts +4 -1
- package/src/components/modals/AGAlertModal.vue +12 -2
- package/src/components/modals/AGConfirmModal.vue +30 -0
- 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 +4 -0
- package/src/components/modals/AGModal.vue +23 -4
- package/src/components/modals/AGModalTitle.vue +9 -0
- package/src/components/modals/index.ts +20 -2
- package/src/components/snackbars/AGSnackbar.vue +42 -0
- package/src/components/snackbars/index.ts +3 -0
- package/src/directives/index.ts +19 -4
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +183 -0
- package/src/errors/index.ts +59 -0
- package/src/forms/Form.test.ts +21 -0
- package/src/forms/Form.ts +41 -16
- package/src/forms/utils.ts +17 -0
- package/src/lang/Lang.ts +47 -8
- package/src/lang/index.ts +17 -76
- package/src/lang/utils.ts +4 -0
- package/src/main.ts +2 -0
- package/src/plugins/Plugin.ts +8 -0
- package/src/plugins/index.ts +26 -0
- package/src/services/App.state.ts +18 -0
- package/src/services/App.ts +20 -0
- package/src/services/Service.ts +157 -29
- package/src/services/index.ts +32 -7
- package/src/services/store.ts +27 -0
- package/src/types/virtual.d.ts +11 -0
- package/src/types/vite.d.ts +0 -2
- package/src/ui/UI.state.ts +12 -6
- package/src/ui/UI.ts +72 -10
- package/src/ui/index.ts +24 -14
- package/src/utils/composition/forms.ts +11 -0
- package/src/utils/composition/hooks.ts +9 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/markdown.ts +11 -2
- package/src/utils/vue.ts +4 -2
- package/tsconfig.json +2 -10
- package/vite.config.ts +3 -6
- package/src/bootstrap/hooks.ts +0 -19
- 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,100 @@
|
|
|
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 { Store } from 'pinia';
|
|
4
4
|
|
|
5
5
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
6
|
+
import { defineServiceStore } from '@/services/store';
|
|
6
7
|
|
|
7
8
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
8
|
-
export type DefaultServiceState =
|
|
9
|
+
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
10
|
export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
|
|
10
11
|
|
|
11
|
-
export
|
|
12
|
+
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
13
|
+
[K in keyof TComputedState]: (state: TState) => TComputedState[K];
|
|
14
|
+
} & ThisType<{
|
|
15
|
+
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
16
|
+
}>;
|
|
17
|
+
|
|
18
|
+
export function defineServiceState<
|
|
19
|
+
State extends ServiceState = ServiceState,
|
|
20
|
+
ComputedState extends ServiceState = {}
|
|
21
|
+
>(options: {
|
|
22
|
+
name: string;
|
|
12
23
|
initialState: State;
|
|
13
|
-
|
|
14
|
-
|
|
24
|
+
persist?: (keyof State)[];
|
|
25
|
+
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
26
|
+
serialize?: (state: Partial<State>) => Partial<State>;
|
|
27
|
+
}): Constructor<State> & Constructor<ComputedState> & Constructor<Service<State, ComputedState, Partial<State>>> {
|
|
28
|
+
return class extends Service<State, ComputedState> {
|
|
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
|
+
}
|
|
15
39
|
|
|
16
40
|
protected getInitialState(): State {
|
|
17
41
|
return options.initialState;
|
|
18
42
|
}
|
|
43
|
+
|
|
44
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
|
|
45
|
+
return options.computed ?? ({} as ComputedStateDefinition<State, ComputedState>);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
49
|
+
return options.serialize?.(state) ?? state;
|
|
50
|
+
}
|
|
19
51
|
|
|
20
|
-
} as unknown as Constructor<State> &
|
|
52
|
+
} as unknown as Constructor<State> &
|
|
53
|
+
Constructor<ComputedState> &
|
|
54
|
+
Constructor<Service<State, ComputedState, Partial<State>>>;
|
|
21
55
|
}
|
|
22
56
|
|
|
23
|
-
export default class Service<
|
|
57
|
+
export default class Service<
|
|
58
|
+
State extends ServiceState = DefaultServiceState,
|
|
59
|
+
ComputedState extends ServiceState = {},
|
|
60
|
+
ServiceStorage extends Partial<State> = Partial<State>
|
|
61
|
+
> extends MagicObject {
|
|
24
62
|
|
|
25
|
-
|
|
63
|
+
public static persist: string[] = [];
|
|
64
|
+
|
|
65
|
+
protected _name: string;
|
|
26
66
|
private _booted: PromisedValue<void>;
|
|
27
|
-
private
|
|
67
|
+
private _computedStateKeys: Set<keyof State>;
|
|
68
|
+
private _store?: Store | false;
|
|
28
69
|
|
|
29
70
|
constructor() {
|
|
30
71
|
super();
|
|
31
72
|
|
|
32
|
-
|
|
73
|
+
const getters = this.getComputedStateDefinition();
|
|
74
|
+
|
|
75
|
+
this._name = this.getName() ?? new.target.name;
|
|
33
76
|
this._booted = new PromisedValue();
|
|
34
|
-
this.
|
|
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
|
+
});
|
|
35
86
|
}
|
|
36
87
|
|
|
37
88
|
public get booted(): PromisedValue<void> {
|
|
38
89
|
return this._booted;
|
|
39
90
|
}
|
|
40
91
|
|
|
41
|
-
public launch(
|
|
42
|
-
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this.
|
|
43
|
-
|
|
44
|
-
this._namespace = namespace ?? this._namespace;
|
|
92
|
+
public launch(): Promise<void> {
|
|
93
|
+
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
45
94
|
|
|
46
95
|
try {
|
|
47
|
-
this.
|
|
96
|
+
this.frameworkBoot()
|
|
97
|
+
.then(() => this.boot())
|
|
48
98
|
.then(() => this._booted.resolve())
|
|
49
99
|
.catch(handleError);
|
|
50
100
|
} catch (error) {
|
|
@@ -54,38 +104,116 @@ export default class Service<State extends ServiceState = DefaultServiceState> e
|
|
|
54
104
|
return this._booted;
|
|
55
105
|
}
|
|
56
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
|
+
|
|
57
144
|
protected __get(property: string): unknown {
|
|
58
|
-
if (
|
|
59
|
-
return
|
|
145
|
+
if (this.hasState(property)) {
|
|
146
|
+
return this.getState(property);
|
|
60
147
|
}
|
|
61
148
|
|
|
62
|
-
return
|
|
149
|
+
return super.__get(property);
|
|
63
150
|
}
|
|
64
151
|
|
|
65
152
|
protected __set(property: string, value: unknown): void {
|
|
66
153
|
this.setState({ [property]: value } as Partial<State>);
|
|
67
154
|
}
|
|
68
155
|
|
|
69
|
-
protected
|
|
70
|
-
|
|
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);
|
|
160
|
+
|
|
161
|
+
if (isEmpty(persisted)) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
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
|
+
});
|
|
71
171
|
}
|
|
72
172
|
|
|
73
|
-
protected
|
|
74
|
-
|
|
75
|
-
protected getState<P extends keyof State>(property?: P): State | State[P] {
|
|
76
|
-
return property ? this._state[property] : this._state;
|
|
173
|
+
protected usesStore(): boolean {
|
|
174
|
+
return false;
|
|
77
175
|
}
|
|
78
176
|
|
|
79
|
-
protected
|
|
80
|
-
|
|
177
|
+
protected getName(): string | null {
|
|
178
|
+
return null;
|
|
81
179
|
}
|
|
82
180
|
|
|
83
181
|
protected getInitialState(): State {
|
|
84
182
|
return {} as State;
|
|
85
183
|
}
|
|
86
184
|
|
|
185
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
|
|
186
|
+
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
187
|
+
}
|
|
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
|
+
|
|
87
197
|
protected async boot(): Promise<void> {
|
|
88
|
-
//
|
|
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));
|
|
89
217
|
}
|
|
90
218
|
|
|
91
219
|
}
|
package/src/services/index.ts
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
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';
|
|
9
13
|
|
|
10
|
-
export { Events, Service };
|
|
14
|
+
export { App, Events, Service };
|
|
11
15
|
|
|
12
16
|
const defaultServices = {
|
|
17
|
+
$app: App,
|
|
13
18
|
$events: Events,
|
|
14
19
|
};
|
|
15
20
|
|
|
@@ -17,18 +22,38 @@ export type DefaultServices = typeof defaultServices;
|
|
|
17
22
|
|
|
18
23
|
export interface Services extends DefaultServices {}
|
|
19
24
|
|
|
20
|
-
export async function bootServices(app:
|
|
25
|
+
export async function bootServices(app: VueApp, services: Record<string, Service>): Promise<void> {
|
|
21
26
|
await Promise.all(
|
|
22
27
|
Object.entries(services).map(async ([name, service]) => {
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
await service
|
|
29
|
+
.launch()
|
|
30
|
+
.catch((error) => app.config.errorHandler?.(error, null, `Failed launching ${name}.`));
|
|
25
31
|
}),
|
|
26
32
|
);
|
|
27
33
|
|
|
28
34
|
Object.assign(app.config.globalProperties, services);
|
|
35
|
+
|
|
36
|
+
App.development && Object.assign(window, services);
|
|
29
37
|
}
|
|
30
38
|
|
|
31
|
-
export default
|
|
39
|
+
export default definePlugin({
|
|
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);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
declare module '@/bootstrap/options' {
|
|
53
|
+
interface AerogelOptions {
|
|
54
|
+
services?: Record<string, Service>;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
32
57
|
|
|
33
58
|
declare module '@vue/runtime-core' {
|
|
34
59
|
interface ComponentCustomProperties extends Services {}
|
|
@@ -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/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
|
@@ -4,9 +4,10 @@ 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;
|
|
@@ -20,17 +21,81 @@ type ModalResult<TComponent> = TComponent extends ModalComponent<Record<string,
|
|
|
20
21
|
|
|
21
22
|
export const UIComponents = {
|
|
22
23
|
AlertModal: 'alert-modal',
|
|
24
|
+
ConfirmModal: 'confirm-modal',
|
|
25
|
+
ErrorReportModal: 'error-report-modal',
|
|
26
|
+
LoadingModal: 'loading-modal',
|
|
27
|
+
Snackbar: 'snackbar',
|
|
23
28
|
} as const;
|
|
24
29
|
|
|
25
30
|
export type UIComponent = ObjectValues<typeof UIComponents>;
|
|
26
31
|
|
|
32
|
+
export interface ShowSnackbarOptions {
|
|
33
|
+
component?: Component;
|
|
34
|
+
color?: SnackbarColor;
|
|
35
|
+
actions?: SnackbarAction[];
|
|
36
|
+
}
|
|
37
|
+
|
|
27
38
|
export class UIService extends Service {
|
|
28
39
|
|
|
29
40
|
private modalCallbacks: Record<string, Partial<ModalCallbacks>> = {};
|
|
30
41
|
private components: Partial<Record<UIComponent, Component>> = {};
|
|
31
42
|
|
|
32
|
-
public
|
|
33
|
-
this.
|
|
43
|
+
public requireComponent(name: UIComponent): Component {
|
|
44
|
+
return this.components[name] ?? fail(`UI Component '${name}' is not defined!`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public alert(message: string): void;
|
|
48
|
+
public alert(title: string, message: string): void;
|
|
49
|
+
public alert(messageOrTitle: string, message?: string): void {
|
|
50
|
+
const options = typeof message === 'string' ? { title: messageOrTitle, message } : { message: messageOrTitle };
|
|
51
|
+
|
|
52
|
+
this.openModal(this.requireComponent(UIComponents.AlertModal), options);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public async confirm(message: string): Promise<boolean>;
|
|
56
|
+
public async confirm(title: string, message: string): Promise<boolean>;
|
|
57
|
+
public async confirm(messageOrTitle: string, message?: string): Promise<boolean> {
|
|
58
|
+
const options = typeof message === 'string' ? { title: messageOrTitle, message } : { message: messageOrTitle };
|
|
59
|
+
const modal = await this.openModal<ModalComponent<{ message: string }, boolean>>(
|
|
60
|
+
this.requireComponent(UIComponents.ConfirmModal),
|
|
61
|
+
options,
|
|
62
|
+
);
|
|
63
|
+
const result = await modal.beforeClose;
|
|
64
|
+
|
|
65
|
+
return result ?? false;
|
|
66
|
+
}
|
|
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
|
+
const result = await operation;
|
|
76
|
+
|
|
77
|
+
await this.closeModal(modal.id);
|
|
78
|
+
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public showSnackbar(message: string, options: ShowSnackbarOptions = {}): void {
|
|
83
|
+
const snackbar: Snackbar = {
|
|
84
|
+
id: uuid(),
|
|
85
|
+
properties: { message, ...options },
|
|
86
|
+
component: options.component ?? markRaw(this.requireComponent(UIComponents.Snackbar)),
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
this.setState('snackbars', this.snackbars.concat(snackbar));
|
|
90
|
+
|
|
91
|
+
setTimeout(() => this.hideSnackbar(snackbar.id), 5000);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public hideSnackbar(id: string): void {
|
|
95
|
+
this.setState(
|
|
96
|
+
'snackbars',
|
|
97
|
+
this.snackbars.filter((snackbar) => snackbar.id !== id),
|
|
98
|
+
);
|
|
34
99
|
}
|
|
35
100
|
|
|
36
101
|
public registerComponent(name: UIComponent, component: Component): void {
|
|
@@ -72,15 +137,9 @@ export class UIService extends Service {
|
|
|
72
137
|
}
|
|
73
138
|
|
|
74
139
|
protected async boot(): Promise<void> {
|
|
75
|
-
await super.boot();
|
|
76
|
-
|
|
77
140
|
this.watchModalEvents();
|
|
78
141
|
}
|
|
79
142
|
|
|
80
|
-
private requireComponent(name: UIComponent): Component {
|
|
81
|
-
return this.components[name] ?? fail(`UI Component '${name}' is not defined!`);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
143
|
private watchModalEvents(): void {
|
|
85
144
|
Events.on('modal-will-close', ({ modal, result }) => {
|
|
86
145
|
this.modalCallbacks[modal.id]?.willClose?.(result);
|
|
@@ -91,7 +150,10 @@ export class UIService extends Service {
|
|
|
91
150
|
});
|
|
92
151
|
|
|
93
152
|
Events.on('modal-closed', async ({ modal, result }) => {
|
|
94
|
-
this.setState(
|
|
153
|
+
this.setState(
|
|
154
|
+
'modals',
|
|
155
|
+
this.modals.filter((m) => m.id !== modal.id),
|
|
156
|
+
);
|
|
95
157
|
|
|
96
158
|
this.modalCallbacks[modal.id]?.closed?.(result);
|
|
97
159
|
|
package/src/ui/index.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import type { Component } from 'vue';
|
|
2
2
|
|
|
3
3
|
import { bootServices } from '@/services';
|
|
4
|
-
import {
|
|
4
|
+
import { definePlugin } from '@/plugins';
|
|
5
5
|
|
|
6
6
|
import UI, { UIComponents } from './UI';
|
|
7
7
|
import AGAlertModal from '../components/modals/AGAlertModal.vue';
|
|
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';
|
|
8
12
|
import type { UIComponent } from './UI';
|
|
9
13
|
|
|
10
14
|
export { UI, UIComponents, UIComponent };
|
|
@@ -13,25 +17,31 @@ const services = { $ui: UI };
|
|
|
13
17
|
|
|
14
18
|
export type UIServices = typeof services;
|
|
15
19
|
|
|
16
|
-
export default
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
export default definePlugin({
|
|
21
|
+
async install(app, options) {
|
|
22
|
+
const defaultComponents = {
|
|
23
|
+
[UIComponents.AlertModal]: AGAlertModal,
|
|
24
|
+
[UIComponents.ConfirmModal]: AGConfirmModal,
|
|
25
|
+
[UIComponents.ErrorReportModal]: AGErrorReportModal,
|
|
26
|
+
[UIComponents.LoadingModal]: AGLoadingModal,
|
|
27
|
+
[UIComponents.Snackbar]: AGSnackbar,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
Object.entries({
|
|
31
|
+
...defaultComponents,
|
|
32
|
+
...options.components,
|
|
33
|
+
}).forEach(([name, component]) => UI.registerComponent(name as UIComponent, component));
|
|
34
|
+
|
|
35
|
+
await bootServices(app, services);
|
|
36
|
+
},
|
|
27
37
|
});
|
|
28
38
|
|
|
29
39
|
declare module '@/bootstrap/options' {
|
|
30
|
-
interface
|
|
40
|
+
interface AerogelOptions {
|
|
31
41
|
components?: Partial<Record<UIComponent, Component>>;
|
|
32
42
|
}
|
|
33
43
|
}
|
|
34
44
|
|
|
35
45
|
declare module '@/services' {
|
|
36
|
-
interface Services extends UIServices {}
|
|
46
|
+
export interface Services extends UIServices {}
|
|
37
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
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { noop } from '@noeldemartin/utils';
|
|
2
|
+
import { onMounted, onUnmounted } from 'vue';
|
|
3
|
+
|
|
4
|
+
export function onCleanMounted(operation: () => Function): void {
|
|
5
|
+
let cleanUp: Function = noop;
|
|
6
|
+
|
|
7
|
+
onMounted(() => (cleanUp = operation()));
|
|
8
|
+
onUnmounted(() => cleanUp());
|
|
9
|
+
}
|
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,
|
package/tsconfig.json
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
2
3
|
"compilerOptions": {
|
|
3
|
-
"
|
|
4
|
-
"module": "esnext",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"strict": true,
|
|
7
|
-
"types": [],
|
|
8
|
-
"jsx": "preserve",
|
|
9
|
-
"noUncheckedIndexedAccess": true,
|
|
10
|
-
"resolveJsonModule": true,
|
|
11
|
-
"esModuleInterop": true,
|
|
12
|
-
"lib": ["esnext", "dom"],
|
|
4
|
+
"types": ["unplugin-icons/types/vue3"],
|
|
13
5
|
"baseUrl": ".",
|
|
14
6
|
"paths": {
|
|
15
7
|
"@/*": ["./src/*"]
|