@aerogel/core 0.0.0-next.b58141fee5d2fe7d25debdbca6b1d2bf1c13e48e → 0.0.0-next.bde642c4a8096c5fc3d5e676c2115da23f4bf1d8
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 +1297 -236
- package/dist/aerogel-core.esm.js +1 -1
- package/dist/aerogel-core.esm.js.map +1 -1
- package/histoire.config.ts +7 -0
- package/package.json +14 -5
- package/postcss.config.js +6 -0
- package/src/assets/histoire.css +3 -0
- package/src/bootstrap/bootstrap.test.ts +3 -3
- package/src/bootstrap/index.ts +25 -5
- package/src/bootstrap/options.ts +3 -0
- package/src/components/AGAppLayout.vue +7 -2
- package/src/components/AGAppOverlays.vue +5 -1
- package/src/components/AGAppSnackbars.vue +2 -2
- package/src/components/composition.ts +23 -0
- package/src/components/forms/AGCheckbox.vue +7 -1
- package/src/components/forms/AGForm.vue +9 -10
- package/src/components/forms/AGInput.vue +10 -6
- package/src/components/forms/AGSelect.story.vue +46 -0
- package/src/components/forms/AGSelect.vue +60 -0
- package/src/components/forms/index.ts +5 -6
- package/src/components/headless/forms/AGHeadlessButton.ts +3 -0
- package/src/components/headless/forms/AGHeadlessButton.vue +23 -12
- package/src/components/headless/forms/AGHeadlessInput.ts +29 -4
- package/src/components/headless/forms/AGHeadlessInput.vue +16 -7
- package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
- package/src/components/headless/forms/AGHeadlessInputInput.vue +43 -5
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
- package/src/components/headless/forms/AGHeadlessInputTextArea.vue +42 -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/composition.ts +10 -0
- package/src/components/headless/forms/index.ts +13 -1
- package/src/components/headless/modals/AGHeadlessModal.ts +27 -0
- package/src/components/headless/modals/AGHeadlessModal.vue +3 -5
- package/src/components/headless/modals/index.ts +4 -6
- package/src/components/headless/snackbars/index.ts +23 -8
- package/src/components/index.ts +3 -1
- package/src/components/interfaces.ts +24 -0
- package/src/components/{basic → lib}/AGErrorMessage.vue +2 -2
- package/src/components/{basic → lib}/AGMarkdown.vue +9 -4
- 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 -15
- package/src/components/modals/AGConfirmModal.ts +33 -0
- package/src/components/modals/AGConfirmModal.vue +9 -13
- package/src/components/modals/AGErrorReportModal.ts +27 -1
- package/src/components/modals/AGErrorReportModal.vue +8 -16
- package/src/components/modals/AGErrorReportModalButtons.vue +4 -2
- package/src/components/modals/AGErrorReportModalTitle.vue +1 -1
- package/src/components/modals/AGLoadingModal.ts +23 -0
- package/src/components/modals/AGLoadingModal.vue +4 -8
- package/src/components/modals/AGModal.ts +1 -1
- package/src/components/modals/AGModal.vue +14 -12
- package/src/components/modals/AGPromptModal.ts +36 -0
- package/src/components/modals/AGPromptModal.vue +34 -0
- package/src/components/modals/index.ts +13 -19
- package/src/components/snackbars/AGSnackbar.vue +3 -9
- package/src/components/utils.ts +10 -0
- package/src/directives/index.ts +5 -1
- package/src/directives/measure.ts +21 -0
- package/src/errors/Errors.ts +26 -24
- package/src/errors/index.ts +10 -23
- package/src/errors/utils.ts +19 -0
- package/src/forms/Form.ts +54 -6
- package/src/forms/index.ts +1 -0
- package/src/forms/utils.ts +15 -0
- package/src/jobs/Job.ts +5 -0
- package/src/jobs/index.ts +7 -0
- package/src/lang/DefaultLangProvider.ts +43 -0
- package/src/lang/Lang.state.ts +11 -0
- package/src/lang/Lang.ts +41 -30
- package/src/main.histoire.ts +1 -0
- package/src/main.ts +3 -0
- package/src/plugins/Plugin.ts +1 -0
- package/src/plugins/index.ts +19 -0
- package/src/services/App.state.ts +20 -5
- package/src/services/App.ts +38 -3
- package/src/services/Cache.ts +43 -0
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +100 -30
- package/src/services/Service.ts +59 -17
- package/src/services/index.ts +5 -2
- package/src/services/store.ts +8 -5
- package/src/testing/index.ts +25 -0
- package/src/testing/setup.ts +19 -0
- package/src/ui/UI.ts +132 -19
- package/src/ui/index.ts +8 -3
- package/src/utils/composition/events.ts +1 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/tailwindcss.test.ts +26 -0
- package/src/utils/tailwindcss.ts +7 -0
- package/src/utils/vue.ts +16 -5
- package/tailwind.config.js +4 -0
- package/tsconfig.json +1 -1
- package/vite.config.ts +4 -1
- package/.eslintrc.js +0 -3
- package/dist/virtual.d.ts +0 -11
- package/src/components/basic/index.ts +0 -5
- package/src/types/virtual.d.ts +0 -11
- /package/src/components/{basic → lib}/AGLink.vue +0 -0
package/src/errors/Errors.ts
CHANGED
|
@@ -7,7 +7,10 @@ import { translateWithDefault } from '@/lang/utils';
|
|
|
7
7
|
|
|
8
8
|
import Service from './Errors.state';
|
|
9
9
|
import { Colors } from '@/components/constants';
|
|
10
|
+
import { Events } from '@/services';
|
|
11
|
+
import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
|
|
10
12
|
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
13
|
+
import type { ModalComponent } from '@/ui/UI.state';
|
|
11
14
|
|
|
12
15
|
export class ErrorsService extends Service {
|
|
13
16
|
|
|
@@ -23,7 +26,7 @@ export class ErrorsService extends Service {
|
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
public async inspect(error: ErrorSource | ErrorReport[]): Promise<void> {
|
|
26
|
-
const reports = Array.isArray(error) ? error : [await this.createErrorReport(error)];
|
|
29
|
+
const reports = Array.isArray(error) ? (error as ErrorReport[]) : [await this.createErrorReport(error)];
|
|
27
30
|
|
|
28
31
|
if (reports.length === 0) {
|
|
29
32
|
UI.alert(translateWithDefault('errors.inspectEmpty', 'Nothing to inspect!'));
|
|
@@ -31,11 +34,19 @@ export class ErrorsService extends Service {
|
|
|
31
34
|
return;
|
|
32
35
|
}
|
|
33
36
|
|
|
34
|
-
UI.openModal(UI.requireComponent(UIComponents.ErrorReportModal), {
|
|
37
|
+
UI.openModal<ModalComponent<AGErrorReportModalProps>>(UI.requireComponent(UIComponents.ErrorReportModal), {
|
|
38
|
+
reports,
|
|
39
|
+
});
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
public async report(error: ErrorSource, message?: string): Promise<void> {
|
|
38
|
-
|
|
43
|
+
await Events.emit('error', { error, message });
|
|
44
|
+
|
|
45
|
+
if (App.testing) {
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (App.development) {
|
|
39
50
|
this.logError(error);
|
|
40
51
|
}
|
|
41
52
|
|
|
@@ -43,7 +54,7 @@ export class ErrorsService extends Service {
|
|
|
43
54
|
throw error;
|
|
44
55
|
}
|
|
45
56
|
|
|
46
|
-
if (!App.isMounted) {
|
|
57
|
+
if (!App.isMounted()) {
|
|
47
58
|
const startupError = await this.createStartupErrorReport(error);
|
|
48
59
|
|
|
49
60
|
if (startupError) {
|
|
@@ -70,9 +81,10 @@ export class ErrorsService extends Service {
|
|
|
70
81
|
text: translateWithDefault('errors.viewDetails', 'View details'),
|
|
71
82
|
dismiss: true,
|
|
72
83
|
handler: () =>
|
|
73
|
-
UI.openModal(
|
|
74
|
-
|
|
75
|
-
|
|
84
|
+
UI.openModal<ModalComponent<AGErrorReportModalProps>>(
|
|
85
|
+
UI.requireComponent(UIComponents.ErrorReportModal),
|
|
86
|
+
{ reports: [report] },
|
|
87
|
+
),
|
|
76
88
|
},
|
|
77
89
|
],
|
|
78
90
|
},
|
|
@@ -105,22 +117,6 @@ export class ErrorsService extends Service {
|
|
|
105
117
|
});
|
|
106
118
|
}
|
|
107
119
|
|
|
108
|
-
public getErrorMessage(error: ErrorSource): string {
|
|
109
|
-
if (typeof error === 'string') {
|
|
110
|
-
return error;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (error instanceof Error || error instanceof JSError) {
|
|
114
|
-
return error.message;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (isObject(error)) {
|
|
118
|
-
return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return translateWithDefault('errors.unknown', 'Unknown Error');
|
|
122
|
-
}
|
|
123
|
-
|
|
124
120
|
private logError(error: unknown): void {
|
|
125
121
|
// eslint-disable-next-line no-console
|
|
126
122
|
console.error(error);
|
|
@@ -180,4 +176,10 @@ export class ErrorsService extends Service {
|
|
|
180
176
|
|
|
181
177
|
}
|
|
182
178
|
|
|
183
|
-
export default facade(
|
|
179
|
+
export default facade(ErrorsService);
|
|
180
|
+
|
|
181
|
+
declare module '@/services/Events' {
|
|
182
|
+
export interface EventsPayload {
|
|
183
|
+
error: { error: ErrorSource; message?: string };
|
|
184
|
+
}
|
|
185
|
+
}
|
package/src/errors/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { App } from 'vue';
|
|
2
2
|
|
|
3
3
|
import { bootServices } from '@/services';
|
|
4
4
|
import { definePlugin } from '@/plugins';
|
|
@@ -6,33 +6,22 @@ import { definePlugin } from '@/plugins';
|
|
|
6
6
|
import Errors from './Errors';
|
|
7
7
|
import { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
8
8
|
|
|
9
|
+
export * from './utils';
|
|
9
10
|
export { Errors, ErrorSource, ErrorReport, ErrorReportLog };
|
|
10
11
|
|
|
11
12
|
const services = { $errors: Errors };
|
|
12
13
|
const frameworkHandler: ErrorHandler = (error) => {
|
|
13
|
-
if (!Errors.instance) {
|
|
14
|
-
// eslint-disable-next-line no-console
|
|
15
|
-
console.warn('Errors service hasn\'t been initialized properly!');
|
|
16
|
-
|
|
17
|
-
// eslint-disable-next-line no-console
|
|
18
|
-
console.error(error);
|
|
19
|
-
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
14
|
Errors.report(error);
|
|
24
15
|
|
|
25
16
|
return true;
|
|
26
17
|
};
|
|
27
18
|
|
|
28
|
-
function setUpErrorHandler(baseHandler: ErrorHandler = () => false):
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
},
|
|
35
|
-
);
|
|
19
|
+
function setUpErrorHandler(app: App, baseHandler: ErrorHandler = () => false): void {
|
|
20
|
+
const errorHandler: ErrorHandler = (error) => baseHandler(error) || frameworkHandler(error);
|
|
21
|
+
|
|
22
|
+
app.config.errorHandler = errorHandler;
|
|
23
|
+
globalThis.onerror = (event, _, __, ___, error) => errorHandler(error ?? event);
|
|
24
|
+
globalThis.onunhandledrejection = (event) => errorHandler(event.reason);
|
|
36
25
|
}
|
|
37
26
|
|
|
38
27
|
export type ErrorHandler = (error: ErrorSource) => boolean;
|
|
@@ -40,16 +29,14 @@ export type ErrorsServices = typeof services;
|
|
|
40
29
|
|
|
41
30
|
export default definePlugin({
|
|
42
31
|
async install(app, options) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
app.config.errorHandler = errorHandler;
|
|
32
|
+
setUpErrorHandler(app, options.handleError);
|
|
46
33
|
|
|
47
34
|
await bootServices(app, services);
|
|
48
35
|
},
|
|
49
36
|
});
|
|
50
37
|
|
|
51
38
|
declare module '@/bootstrap/options' {
|
|
52
|
-
interface AerogelOptions {
|
|
39
|
+
export interface AerogelOptions {
|
|
53
40
|
handleError?(error: ErrorSource): boolean;
|
|
54
41
|
}
|
|
55
42
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { JSError, isObject, toString } from '@noeldemartin/utils';
|
|
2
|
+
import { translateWithDefault } from '@/lang/utils';
|
|
3
|
+
import type { ErrorSource } from './Errors.state';
|
|
4
|
+
|
|
5
|
+
export function getErrorMessage(error: ErrorSource): string {
|
|
6
|
+
if (typeof error === 'string') {
|
|
7
|
+
return error;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (error instanceof Error || error instanceof JSError) {
|
|
11
|
+
return error.message;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (isObject(error)) {
|
|
15
|
+
return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return translateWithDefault('errors.unknown', 'Unknown Error');
|
|
19
|
+
}
|
package/src/forms/Form.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { MagicObject } from '@noeldemartin/utils';
|
|
2
|
-
import { computed, reactive, readonly, ref } from 'vue';
|
|
1
|
+
import { MagicObject, arrayRemove } from '@noeldemartin/utils';
|
|
2
|
+
import { computed, nextTick, reactive, readonly, ref } from 'vue';
|
|
3
3
|
import type { ObjectValues } from '@noeldemartin/utils';
|
|
4
4
|
import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
5
5
|
|
|
@@ -7,6 +7,8 @@ export const FormFieldTypes = {
|
|
|
7
7
|
String: 'string',
|
|
8
8
|
Number: 'number',
|
|
9
9
|
Boolean: 'boolean',
|
|
10
|
+
Object: 'object',
|
|
11
|
+
Date: 'date',
|
|
10
12
|
} as const;
|
|
11
13
|
|
|
12
14
|
export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
|
|
@@ -17,6 +19,7 @@ export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType
|
|
|
17
19
|
|
|
18
20
|
export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
|
|
19
21
|
export type FormFieldType = ObjectValues<typeof FormFieldTypes>;
|
|
22
|
+
export type FormFieldValue = GetFormFieldValue<FormFieldType>;
|
|
20
23
|
|
|
21
24
|
export type FormData<T> = {
|
|
22
25
|
-readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules>
|
|
@@ -36,17 +39,26 @@ export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.Strin
|
|
|
36
39
|
? number
|
|
37
40
|
: TType extends typeof FormFieldTypes.Boolean
|
|
38
41
|
? boolean
|
|
42
|
+
: TType extends typeof FormFieldTypes.Object
|
|
43
|
+
? object
|
|
44
|
+
: TType extends typeof FormFieldTypes.Date
|
|
45
|
+
? Date
|
|
39
46
|
: never;
|
|
40
47
|
|
|
48
|
+
const validForms: WeakMap<Form, ComputedRef<boolean>> = new WeakMap();
|
|
49
|
+
|
|
50
|
+
export type SubmitFormListener = () => unknown;
|
|
51
|
+
export type FocusFormListener = (input: string) => unknown;
|
|
52
|
+
|
|
41
53
|
export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
42
54
|
|
|
43
55
|
public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
|
|
44
56
|
|
|
45
57
|
private _fields: Fields;
|
|
46
58
|
private _data: FormData<Fields>;
|
|
47
|
-
private _valid: ComputedRef<boolean>;
|
|
48
59
|
private _submitted: Ref<boolean>;
|
|
49
60
|
private _errors: FormErrors<Fields>;
|
|
61
|
+
private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
|
|
50
62
|
|
|
51
63
|
constructor(fields: Fields) {
|
|
52
64
|
super();
|
|
@@ -55,13 +67,17 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
55
67
|
this._submitted = ref(false);
|
|
56
68
|
this._data = this.getInitialData(fields);
|
|
57
69
|
this._errors = this.getInitialErrors(fields);
|
|
58
|
-
|
|
70
|
+
|
|
71
|
+
validForms.set(
|
|
72
|
+
this,
|
|
73
|
+
computed(() => !Object.values(this._errors).some((error) => error !== null)),
|
|
74
|
+
);
|
|
59
75
|
|
|
60
76
|
this.errors = readonly(this._errors);
|
|
61
77
|
}
|
|
62
78
|
|
|
63
79
|
public get valid(): boolean {
|
|
64
|
-
return this
|
|
80
|
+
return !!validForms.get(this)?.value;
|
|
65
81
|
}
|
|
66
82
|
|
|
67
83
|
public get submitted(): boolean {
|
|
@@ -80,6 +96,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
80
96
|
return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
|
|
81
97
|
}
|
|
82
98
|
|
|
99
|
+
public data(): FormData<Fields> {
|
|
100
|
+
return { ...this._data };
|
|
101
|
+
}
|
|
102
|
+
|
|
83
103
|
public validate(): boolean {
|
|
84
104
|
const errors = Object.entries(this._fields).reduce((formErrors, [name, definition]) => {
|
|
85
105
|
formErrors[name] = this.getFieldErrors(name, definition);
|
|
@@ -102,7 +122,35 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
102
122
|
public submit(): boolean {
|
|
103
123
|
this._submitted.value = true;
|
|
104
124
|
|
|
105
|
-
|
|
125
|
+
const valid = this.validate();
|
|
126
|
+
|
|
127
|
+
valid && this._listeners['submit']?.forEach((listener) => listener());
|
|
128
|
+
|
|
129
|
+
return valid;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public on(event: 'focus', listener: FocusFormListener): () => void;
|
|
133
|
+
public on(event: 'submit', listener: SubmitFormListener): () => void;
|
|
134
|
+
public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
|
|
135
|
+
this._listeners[event] ??= [];
|
|
136
|
+
|
|
137
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
138
|
+
this._listeners[event]?.push(listener as any);
|
|
139
|
+
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
141
|
+
return () => this.off(event as any, listener);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public off(event: 'focus', listener: FocusFormListener): void;
|
|
145
|
+
public off(event: 'submit', listener: SubmitFormListener): void;
|
|
146
|
+
public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
|
|
147
|
+
arrayRemove(this._listeners[event] ?? [], listener);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public async focus(input: string): Promise<void> {
|
|
151
|
+
await nextTick();
|
|
152
|
+
|
|
153
|
+
this._listeners['focus']?.forEach((listener) => listener(input));
|
|
106
154
|
}
|
|
107
155
|
|
|
108
156
|
protected __get(property: string): unknown {
|
package/src/forms/index.ts
CHANGED
package/src/forms/utils.ts
CHANGED
|
@@ -8,6 +8,13 @@ export function booleanInput(defaultValue?: boolean): FormFieldDefinition<typeof
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
export function dateInput(defaultValue?: Date): FormFieldDefinition<typeof FormFieldTypes.Date> {
|
|
12
|
+
return {
|
|
13
|
+
default: defaultValue,
|
|
14
|
+
type: FormFieldTypes.Date,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
11
18
|
export function requiredBooleanInput(
|
|
12
19
|
defaultValue?: boolean,
|
|
13
20
|
): FormFieldDefinition<typeof FormFieldTypes.Boolean, 'required'> {
|
|
@@ -18,6 +25,14 @@ export function requiredBooleanInput(
|
|
|
18
25
|
};
|
|
19
26
|
}
|
|
20
27
|
|
|
28
|
+
export function requiredDateInput(defaultValue?: Date): FormFieldDefinition<typeof FormFieldTypes.Date> {
|
|
29
|
+
return {
|
|
30
|
+
default: defaultValue,
|
|
31
|
+
type: FormFieldTypes.Date,
|
|
32
|
+
rules: 'required',
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
21
36
|
export function requiredNumberInput(
|
|
22
37
|
defaultValue?: number,
|
|
23
38
|
): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
|
package/src/jobs/Job.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import App from '@/services/App';
|
|
2
|
+
|
|
3
|
+
import type { LangProvider } from './Lang';
|
|
4
|
+
|
|
5
|
+
export default class DefaultLangProvider implements LangProvider {
|
|
6
|
+
|
|
7
|
+
constructor(private locale: string, private fallbackLocale: string) {}
|
|
8
|
+
|
|
9
|
+
public getLocale(): string {
|
|
10
|
+
return this.locale;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public async setLocale(locale: string): Promise<void> {
|
|
14
|
+
this.locale = locale;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public getFallbackLocale(): string {
|
|
18
|
+
return this.fallbackLocale;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async setFallbackLocale(fallbackLocale: string): Promise<void> {
|
|
22
|
+
this.fallbackLocale = fallbackLocale;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public getLocales(): string[] {
|
|
26
|
+
return ['en'];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public translate(key: string): string {
|
|
30
|
+
// eslint-disable-next-line no-console
|
|
31
|
+
App.development && console.warn('Lang provider is missing');
|
|
32
|
+
|
|
33
|
+
return key;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public translateWithDefault(_: string, defaultMessage: string): string {
|
|
37
|
+
// eslint-disable-next-line no-console
|
|
38
|
+
App.development && console.warn('Lang provider is missing');
|
|
39
|
+
|
|
40
|
+
return defaultMessage;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defineServiceState } from '@/services/Service';
|
|
2
|
+
|
|
3
|
+
export default defineServiceState({
|
|
4
|
+
name: 'lang',
|
|
5
|
+
persist: ['locale', 'fallbackLocale'],
|
|
6
|
+
initialState: {
|
|
7
|
+
locale: null as string | null,
|
|
8
|
+
locales: ['en'],
|
|
9
|
+
fallbackLocale: 'en',
|
|
10
|
+
},
|
|
11
|
+
});
|
package/src/lang/Lang.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import { facade
|
|
1
|
+
import { facade } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import Service from '
|
|
3
|
+
import DefaultLangProvider from './DefaultLangProvider';
|
|
4
|
+
import Service from './Lang.state';
|
|
5
5
|
|
|
6
6
|
export interface LangProvider {
|
|
7
|
-
|
|
7
|
+
getLocale(): string;
|
|
8
|
+
setLocale(locale: string): Promise<void>;
|
|
9
|
+
getFallbackLocale(): string;
|
|
10
|
+
setFallbackLocale(fallbackLocale: string): Promise<void>;
|
|
11
|
+
getLocales(): string[];
|
|
12
|
+
translate(key: string, parameters?: Record<string, unknown> | number): string;
|
|
13
|
+
translateWithDefault(key: string, defaultMessage: string, parameters?: Record<string, unknown> | number): string;
|
|
8
14
|
}
|
|
9
15
|
|
|
10
16
|
export class LangService extends Service {
|
|
@@ -14,47 +20,52 @@ export class LangService extends Service {
|
|
|
14
20
|
constructor() {
|
|
15
21
|
super();
|
|
16
22
|
|
|
17
|
-
this.provider =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return key;
|
|
23
|
-
},
|
|
24
|
-
};
|
|
23
|
+
this.provider = new DefaultLangProvider(
|
|
24
|
+
this.getState('locale') ?? this.getBrowserLocale(),
|
|
25
|
+
this.getState('fallbackLocale'),
|
|
26
|
+
);
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
public setProvider(provider: LangProvider): void {
|
|
29
|
+
public async setProvider(provider: LangProvider): Promise<void> {
|
|
28
30
|
this.provider = provider;
|
|
31
|
+
this.locales = provider.getLocales();
|
|
32
|
+
|
|
33
|
+
await provider.setLocale(this.locale ?? this.getBrowserLocale());
|
|
34
|
+
await provider.setFallbackLocale(this.fallbackLocale);
|
|
29
35
|
}
|
|
30
36
|
|
|
31
|
-
public translate(key: string, parameters?: Record<string, unknown>): string {
|
|
37
|
+
public translate(key: string, parameters?: Record<string, unknown> | number): string {
|
|
32
38
|
return this.provider.translate(key, parameters) ?? key;
|
|
33
39
|
}
|
|
34
40
|
|
|
35
|
-
public translateWithDefault(key: string, defaultMessage: string): string;
|
|
36
|
-
public translateWithDefault(key: string, parameters: Record<string, unknown>, defaultMessage: string): string;
|
|
37
41
|
public translateWithDefault(
|
|
38
42
|
key: string,
|
|
39
|
-
|
|
40
|
-
|
|
43
|
+
defaultMessage: string,
|
|
44
|
+
parameters: Record<string, unknown> | number = {},
|
|
41
45
|
): string {
|
|
42
|
-
|
|
46
|
+
return this.provider.translateWithDefault(key, defaultMessage, parameters);
|
|
47
|
+
}
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
const
|
|
49
|
+
public getBrowserLocale(): string {
|
|
50
|
+
const locales = this.getState('locales');
|
|
46
51
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
)
|
|
53
|
-
|
|
52
|
+
return navigator.languages.find((locale) => locales.includes(locale)) ?? 'en';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected async boot(): Promise<void> {
|
|
56
|
+
this.requireStore().$subscribe(
|
|
57
|
+
async () => {
|
|
58
|
+
await this.provider.setLocale(this.locale ?? this.getBrowserLocale());
|
|
59
|
+
await this.provider.setFallbackLocale(this.fallbackLocale);
|
|
54
60
|
|
|
55
|
-
|
|
61
|
+
this.locale
|
|
62
|
+
? document.querySelector('html')?.setAttribute('lang', this.locale)
|
|
63
|
+
: document.querySelector('html')?.removeAttribute('lang');
|
|
64
|
+
},
|
|
65
|
+
{ immediate: true },
|
|
66
|
+
);
|
|
56
67
|
}
|
|
57
68
|
|
|
58
69
|
}
|
|
59
70
|
|
|
60
|
-
export default facade(
|
|
71
|
+
export default facade(LangService);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './assets/histoire.css';
|
package/src/main.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export * from './bootstrap';
|
|
2
2
|
export * from './components';
|
|
3
|
+
export * from './directives';
|
|
3
4
|
export * from './errors';
|
|
4
5
|
export * from './forms';
|
|
6
|
+
export * from './jobs';
|
|
5
7
|
export * from './lang';
|
|
6
8
|
export * from './plugins';
|
|
7
9
|
export * from './services';
|
|
10
|
+
export * from './testing';
|
|
8
11
|
export * from './ui';
|
|
9
12
|
export * from './utils';
|
package/src/plugins/Plugin.ts
CHANGED
package/src/plugins/index.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import type { GetClosureArgs } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import App from '@/services/App';
|
|
4
|
+
|
|
1
5
|
import type { Plugin } from './Plugin';
|
|
2
6
|
|
|
3
7
|
export * from './Plugin';
|
|
@@ -5,3 +9,18 @@ export * from './Plugin';
|
|
|
5
9
|
export function definePlugin<T extends Plugin>(plugin: T): T {
|
|
6
10
|
return plugin;
|
|
7
11
|
}
|
|
12
|
+
|
|
13
|
+
export async function installPlugins(plugins: Plugin[], ...args: GetClosureArgs<Plugin['install']>): Promise<void> {
|
|
14
|
+
App.setState(
|
|
15
|
+
'plugins',
|
|
16
|
+
plugins.reduce((pluginsMap, plugin) => {
|
|
17
|
+
if (plugin.name) {
|
|
18
|
+
pluginsMap[plugin.name] = plugin;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return pluginsMap;
|
|
22
|
+
}, {} as Record<string, Plugin>),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
await Promise.all(plugins.map((plugin) => plugin.install(...args)) ?? []);
|
|
26
|
+
}
|
|
@@ -1,16 +1,31 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Aerogel from 'virtual:aerogel';
|
|
2
2
|
|
|
3
3
|
import { defineServiceState } from '@/services/Service';
|
|
4
|
+
import type { Plugin } from '@/plugins/Plugin';
|
|
4
5
|
|
|
5
6
|
export default defineServiceState({
|
|
6
7
|
name: 'app',
|
|
7
8
|
initialState: {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
plugins: {} as Record<string, Plugin>,
|
|
10
|
+
environment: Aerogel.environment,
|
|
11
|
+
version: Aerogel.version,
|
|
12
|
+
sourceUrl: Aerogel.sourceUrl,
|
|
11
13
|
},
|
|
12
14
|
computed: {
|
|
13
15
|
development: (state) => state.environment === 'development',
|
|
14
|
-
testing: (state) => state.environment === 'testing',
|
|
16
|
+
testing: (state) => state.environment === 'test' || state.environment === 'testing',
|
|
17
|
+
versionName(state): string {
|
|
18
|
+
if (this.development) {
|
|
19
|
+
return 'dev.' + Aerogel.sourceHash.toString().substring(0, 7);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return `v${state.version}`;
|
|
23
|
+
},
|
|
24
|
+
versionUrl(state): string {
|
|
25
|
+
return (
|
|
26
|
+
state.sourceUrl +
|
|
27
|
+
(this.development ? `/tree/${Aerogel.sourceHash}` : `/releases/tag/${this.versionName}`)
|
|
28
|
+
);
|
|
29
|
+
},
|
|
15
30
|
},
|
|
16
31
|
});
|
package/src/services/App.ts
CHANGED
|
@@ -1,15 +1,50 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Aerogel from 'virtual:aerogel';
|
|
2
|
+
|
|
3
|
+
import { PromisedValue, facade, forever, updateLocationQueryParameters } from '@noeldemartin/utils';
|
|
2
4
|
|
|
3
5
|
import Events from '@/services/Events';
|
|
6
|
+
import type { Plugin } from '@/plugins';
|
|
4
7
|
|
|
5
8
|
import Service from './App.state';
|
|
6
9
|
|
|
7
10
|
export class AppService extends Service {
|
|
8
11
|
|
|
12
|
+
public readonly name = Aerogel.name;
|
|
13
|
+
public readonly ready = new PromisedValue<void>();
|
|
14
|
+
public readonly mounted = new PromisedValue<void>();
|
|
15
|
+
|
|
16
|
+
public isReady(): boolean {
|
|
17
|
+
return this.ready.isResolved();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public isMounted(): boolean {
|
|
21
|
+
return this.mounted.isResolved();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async whenReady<T>(callback: () => T): Promise<T> {
|
|
25
|
+
const result = await this.ready.then(callback);
|
|
26
|
+
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public async reload(queryParameters?: Record<string, string | undefined>): Promise<void> {
|
|
31
|
+
queryParameters && updateLocationQueryParameters(queryParameters);
|
|
32
|
+
|
|
33
|
+
location.reload();
|
|
34
|
+
|
|
35
|
+
// Stall until the reload happens
|
|
36
|
+
await forever();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public plugin<T extends Plugin = Plugin>(name: string): T | null {
|
|
40
|
+
return (this.plugins[name] as T) ?? null;
|
|
41
|
+
}
|
|
42
|
+
|
|
9
43
|
protected async boot(): Promise<void> {
|
|
10
|
-
Events.once('application-
|
|
44
|
+
Events.once('application-ready', () => this.ready.resolve());
|
|
45
|
+
Events.once('application-mounted', () => this.mounted.resolve());
|
|
11
46
|
}
|
|
12
47
|
|
|
13
48
|
}
|
|
14
49
|
|
|
15
|
-
export default facade(
|
|
50
|
+
export default facade(AppService);
|