@aerogel/core 0.0.0-next.a68f133e2c9a1ae9ba84b4e2e42df909289e5fba → 0.0.0-next.b243de4e2590f02709edeebd8c13b74087592c04
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 +383 -48
- package/dist/aerogel-core.esm.js +1 -1
- package/dist/aerogel-core.esm.js.map +1 -1
- package/package.json +3 -3
- package/src/bootstrap/bootstrap.test.ts +0 -1
- package/src/bootstrap/index.ts +5 -4
- package/src/components/AGAppSnackbars.vue +1 -1
- package/src/components/composition.ts +23 -0
- package/src/components/forms/AGForm.vue +9 -10
- package/src/components/forms/AGInput.vue +2 -0
- 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 +10 -4
- package/src/components/headless/forms/AGHeadlessInput.vue +18 -5
- package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
- package/src/components/headless/forms/AGHeadlessInputInput.vue +44 -5
- package/src/components/headless/forms/AGHeadlessInputTextArea.vue +43 -0
- package/src/components/headless/forms/composition.ts +10 -0
- package/src/components/headless/forms/index.ts +4 -0
- package/src/components/index.ts +2 -0
- package/src/components/interfaces.ts +24 -0
- package/src/components/lib/AGErrorMessage.vue +2 -2
- package/src/components/lib/AGMarkdown.vue +9 -4
- package/src/components/lib/AGMeasured.vue +1 -0
- package/src/components/modals/AGConfirmModal.ts +9 -3
- package/src/components/modals/AGConfirmModal.vue +2 -2
- package/src/components/modals/AGPromptModal.ts +9 -3
- package/src/components/modals/AGPromptModal.vue +2 -2
- package/src/directives/index.ts +2 -0
- package/src/directives/measure.ts +33 -5
- package/src/errors/Errors.ts +9 -16
- package/src/errors/index.ts +1 -0
- package/src/errors/utils.ts +19 -0
- package/src/forms/Form.test.ts +28 -0
- package/src/forms/Form.ts +65 -8
- package/src/forms/index.ts +3 -1
- package/src/forms/utils.ts +34 -3
- package/src/forms/validation.ts +19 -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 +43 -28
- package/src/main.ts +3 -0
- package/src/services/App.state.ts +14 -0
- package/src/services/App.ts +3 -0
- package/src/services/Cache.ts +43 -0
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +99 -29
- package/src/services/Service.ts +15 -3
- package/src/services/index.ts +3 -1
- package/src/testing/index.ts +25 -0
- package/src/testing/setup.ts +19 -0
- package/src/ui/UI.state.ts +7 -0
- package/src/ui/UI.ts +40 -10
- package/src/ui/index.ts +1 -0
- package/src/ui/utils.ts +16 -0
- package/src/utils/vue.ts +11 -2
- package/vite.config.ts +4 -1
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { computed } from 'vue';
|
|
2
2
|
import type { ExtractPropTypes } from 'vue';
|
|
3
|
-
import type { ObjectWithoutEmpty } from '@noeldemartin/utils';
|
|
3
|
+
import type { ObjectWithoutEmpty, SubPartial } from '@noeldemartin/utils';
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Colors } from '@/components/constants';
|
|
6
|
+
import { enumProp, requiredStringProp, stringProp } from '@/utils';
|
|
6
7
|
import { translateWithDefault } from '@/lang';
|
|
7
8
|
|
|
8
9
|
export const promptModalProps = {
|
|
@@ -12,10 +13,15 @@ export const promptModalProps = {
|
|
|
12
13
|
defaultValue: stringProp(),
|
|
13
14
|
placeholder: stringProp(),
|
|
14
15
|
acceptText: stringProp(),
|
|
16
|
+
acceptColor: enumProp(Colors, Colors.Primary),
|
|
15
17
|
cancelText: stringProp(),
|
|
18
|
+
cancelColor: enumProp(Colors, Colors.Clear),
|
|
16
19
|
};
|
|
17
20
|
|
|
18
|
-
export type AGPromptModalProps =
|
|
21
|
+
export type AGPromptModalProps = SubPartial<
|
|
22
|
+
ObjectWithoutEmpty<ExtractPropTypes<typeof promptModalProps>>,
|
|
23
|
+
'acceptColor' | 'cancelColor'
|
|
24
|
+
>;
|
|
19
25
|
|
|
20
26
|
export function usePromptModalProps(): typeof promptModalProps {
|
|
21
27
|
return promptModalProps;
|
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
<AGInput name="draft" :placeholder="placeholder" :label="label" />
|
|
7
7
|
|
|
8
8
|
<div class="mt-2 flex flex-row-reverse gap-2">
|
|
9
|
-
<AGButton submit>
|
|
9
|
+
<AGButton :color="acceptColor" submit>
|
|
10
10
|
{{ renderedAcceptText }}
|
|
11
11
|
</AGButton>
|
|
12
|
-
<AGButton color="
|
|
12
|
+
<AGButton :color="cancelColor" @click="close()">
|
|
13
13
|
{{ renderedCancelText }}
|
|
14
14
|
</AGButton>
|
|
15
15
|
</div>
|
package/src/directives/index.ts
CHANGED
|
@@ -1,12 +1,40 @@
|
|
|
1
1
|
import { defineDirective } from '@/utils/vue';
|
|
2
|
+
import { tap } from '@noeldemartin/utils';
|
|
3
|
+
|
|
4
|
+
const resizeObservers: WeakMap<HTMLElement, ResizeObserver> = new WeakMap();
|
|
5
|
+
|
|
6
|
+
export interface ElementSize {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type MeasureDirectiveListener = (size: ElementSize) => unknown;
|
|
2
12
|
|
|
3
13
|
export default defineDirective({
|
|
4
|
-
mounted(element: HTMLElement, { value }
|
|
5
|
-
|
|
14
|
+
mounted(element: HTMLElement, { value }) {
|
|
15
|
+
// TODO replace with argument when typed properly
|
|
16
|
+
const modifiers = { css: true, watch: true };
|
|
17
|
+
|
|
18
|
+
const listener = typeof value === 'function' ? (value as MeasureDirectiveListener) : null;
|
|
19
|
+
const update = () => {
|
|
20
|
+
const sizes = element.getBoundingClientRect();
|
|
6
21
|
|
|
7
|
-
|
|
8
|
-
|
|
22
|
+
if (modifiers.css) {
|
|
23
|
+
element.style.setProperty('--width', `${sizes.width}px`);
|
|
24
|
+
element.style.setProperty('--height', `${sizes.height}px`);
|
|
25
|
+
}
|
|
9
26
|
|
|
10
|
-
|
|
27
|
+
listener?.({ width: sizes.width, height: sizes.height });
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
if (modifiers.watch) {
|
|
31
|
+
resizeObservers.set(element, tap(new ResizeObserver(update)).observe(element));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
update();
|
|
35
|
+
},
|
|
36
|
+
unmounted(element) {
|
|
37
|
+
resizeObservers.get(element)?.unobserve(element);
|
|
38
|
+
resizeObservers.delete(element);
|
|
11
39
|
},
|
|
12
40
|
});
|
package/src/errors/Errors.ts
CHANGED
|
@@ -7,6 +7,7 @@ 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';
|
|
10
11
|
import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
|
|
11
12
|
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
12
13
|
import type { ModalComponent } from '@/ui/UI.state';
|
|
@@ -39,6 +40,8 @@ export class ErrorsService extends Service {
|
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
public async report(error: ErrorSource, message?: string): Promise<void> {
|
|
43
|
+
await Events.emit('error', { error, message });
|
|
44
|
+
|
|
42
45
|
if (App.testing) {
|
|
43
46
|
throw error;
|
|
44
47
|
}
|
|
@@ -114,22 +117,6 @@ export class ErrorsService extends Service {
|
|
|
114
117
|
});
|
|
115
118
|
}
|
|
116
119
|
|
|
117
|
-
public getErrorMessage(error: ErrorSource): string {
|
|
118
|
-
if (typeof error === 'string') {
|
|
119
|
-
return error;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (error instanceof Error || error instanceof JSError) {
|
|
123
|
-
return error.message;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (isObject(error)) {
|
|
127
|
-
return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return translateWithDefault('errors.unknown', 'Unknown Error');
|
|
131
|
-
}
|
|
132
|
-
|
|
133
120
|
private logError(error: unknown): void {
|
|
134
121
|
// eslint-disable-next-line no-console
|
|
135
122
|
console.error(error);
|
|
@@ -190,3 +177,9 @@ export class ErrorsService extends Service {
|
|
|
190
177
|
}
|
|
191
178
|
|
|
192
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
|
@@ -6,6 +6,7 @@ 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 };
|
|
@@ -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.test.ts
CHANGED
|
@@ -55,4 +55,32 @@ describe('Form', () => {
|
|
|
55
55
|
expect(form.name).toBeNull();
|
|
56
56
|
});
|
|
57
57
|
|
|
58
|
+
it('trims values', () => {
|
|
59
|
+
// Arrange
|
|
60
|
+
const form = useForm({
|
|
61
|
+
trimmed: {
|
|
62
|
+
type: FormFieldTypes.String,
|
|
63
|
+
rules: 'required',
|
|
64
|
+
},
|
|
65
|
+
untrimmed: {
|
|
66
|
+
type: FormFieldTypes.String,
|
|
67
|
+
rules: 'required',
|
|
68
|
+
trim: false,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Act
|
|
73
|
+
form.trimmed = ' ';
|
|
74
|
+
form.untrimmed = ' ';
|
|
75
|
+
|
|
76
|
+
form.submit();
|
|
77
|
+
|
|
78
|
+
// Assert
|
|
79
|
+
expect(form.valid).toBe(false);
|
|
80
|
+
expect(form.submitted).toBe(true);
|
|
81
|
+
expect(form.trimmed).toEqual('');
|
|
82
|
+
expect(form.untrimmed).toEqual(' ');
|
|
83
|
+
expect(form.errors).toEqual({ trimmed: ['required'], untrimmed: null });
|
|
84
|
+
});
|
|
85
|
+
|
|
58
86
|
});
|
package/src/forms/Form.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { computed, nextTick, reactive, readonly, ref } from 'vue';
|
|
2
|
+
import { MagicObject, arrayRemove, fail, toString } from '@noeldemartin/utils';
|
|
3
|
+
import { validate } from './validation';
|
|
3
4
|
import type { ObjectValues } from '@noeldemartin/utils';
|
|
4
5
|
import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
5
6
|
|
|
@@ -8,10 +9,12 @@ export const FormFieldTypes = {
|
|
|
8
9
|
Number: 'number',
|
|
9
10
|
Boolean: 'boolean',
|
|
10
11
|
Object: 'object',
|
|
12
|
+
Date: 'date',
|
|
11
13
|
} as const;
|
|
12
14
|
|
|
13
15
|
export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
|
|
14
16
|
type: TType;
|
|
17
|
+
trim?: boolean;
|
|
15
18
|
default?: GetFormFieldValue<TType>;
|
|
16
19
|
rules?: TRules;
|
|
17
20
|
}
|
|
@@ -40,10 +43,15 @@ export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.Strin
|
|
|
40
43
|
? boolean
|
|
41
44
|
: TType extends typeof FormFieldTypes.Object
|
|
42
45
|
? object
|
|
46
|
+
: TType extends typeof FormFieldTypes.Date
|
|
47
|
+
? Date
|
|
43
48
|
: never;
|
|
44
49
|
|
|
45
50
|
const validForms: WeakMap<Form, ComputedRef<boolean>> = new WeakMap();
|
|
46
51
|
|
|
52
|
+
export type SubmitFormListener = () => unknown;
|
|
53
|
+
export type FocusFormListener = (input: string) => unknown;
|
|
54
|
+
|
|
47
55
|
export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
48
56
|
|
|
49
57
|
public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
|
|
@@ -52,6 +60,7 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
52
60
|
private _data: FormData<Fields>;
|
|
53
61
|
private _submitted: Ref<boolean>;
|
|
54
62
|
private _errors: FormErrors<Fields>;
|
|
63
|
+
private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
|
|
55
64
|
|
|
56
65
|
constructor(fields: Fields) {
|
|
57
66
|
super();
|
|
@@ -78,7 +87,13 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
78
87
|
}
|
|
79
88
|
|
|
80
89
|
public setFieldValue<T extends keyof Fields>(field: T, value: FormData<Fields>[T]): void {
|
|
81
|
-
|
|
90
|
+
const definition =
|
|
91
|
+
this._fields[field] ?? fail<FormFieldDefinition>(`Trying to set undefined '${toString(field)}' field`);
|
|
92
|
+
|
|
93
|
+
this._data[field] =
|
|
94
|
+
definition.type === FormFieldTypes.String && (definition.trim ?? true)
|
|
95
|
+
? (toString(value).trim() as FormData<Fields>[T])
|
|
96
|
+
: value;
|
|
82
97
|
|
|
83
98
|
if (this._submitted.value) {
|
|
84
99
|
this.validate();
|
|
@@ -89,6 +104,14 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
89
104
|
return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
|
|
90
105
|
}
|
|
91
106
|
|
|
107
|
+
public getFieldRules<T extends keyof Fields>(field: T): string[] {
|
|
108
|
+
return this._fields[field]?.rules?.split('|') ?? [];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public data(): FormData<Fields> {
|
|
112
|
+
return { ...this._data };
|
|
113
|
+
}
|
|
114
|
+
|
|
92
115
|
public validate(): boolean {
|
|
93
116
|
const errors = Object.entries(this._fields).reduce((formErrors, [name, definition]) => {
|
|
94
117
|
formErrors[name] = this.getFieldErrors(name, definition);
|
|
@@ -111,7 +134,35 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
111
134
|
public submit(): boolean {
|
|
112
135
|
this._submitted.value = true;
|
|
113
136
|
|
|
114
|
-
|
|
137
|
+
const valid = this.validate();
|
|
138
|
+
|
|
139
|
+
valid && this._listeners['submit']?.forEach((listener) => listener());
|
|
140
|
+
|
|
141
|
+
return valid;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public on(event: 'focus', listener: FocusFormListener): () => void;
|
|
145
|
+
public on(event: 'submit', listener: SubmitFormListener): () => void;
|
|
146
|
+
public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
|
|
147
|
+
this._listeners[event] ??= [];
|
|
148
|
+
|
|
149
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
150
|
+
this._listeners[event]?.push(listener as any);
|
|
151
|
+
|
|
152
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
153
|
+
return () => this.off(event as any, listener);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public off(event: 'focus', listener: FocusFormListener): void;
|
|
157
|
+
public off(event: 'submit', listener: SubmitFormListener): void;
|
|
158
|
+
public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
|
|
159
|
+
arrayRemove(this._listeners[event] ?? [], listener);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public async focus(input: string): Promise<void> {
|
|
163
|
+
await nextTick();
|
|
164
|
+
|
|
165
|
+
this._listeners['focus']?.forEach((listener) => listener(input));
|
|
115
166
|
}
|
|
116
167
|
|
|
117
168
|
protected __get(property: string): unknown {
|
|
@@ -119,7 +170,7 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
119
170
|
return super.__get(property);
|
|
120
171
|
}
|
|
121
172
|
|
|
122
|
-
return this.
|
|
173
|
+
return this.getFieldValue(property);
|
|
123
174
|
}
|
|
124
175
|
|
|
125
176
|
protected __set(property: string, value: unknown): void {
|
|
@@ -129,14 +180,20 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
129
180
|
return;
|
|
130
181
|
}
|
|
131
182
|
|
|
132
|
-
|
|
183
|
+
this.setFieldValue(property, value as FormData<Fields>[string]);
|
|
133
184
|
}
|
|
134
185
|
|
|
135
186
|
private getFieldErrors(name: keyof Fields, definition: FormFieldDefinition): string[] | null {
|
|
136
187
|
const errors = [];
|
|
188
|
+
const value = this._data[name];
|
|
189
|
+
const rules = definition.rules?.split('|') ?? [];
|
|
190
|
+
|
|
191
|
+
for (const rule of rules) {
|
|
192
|
+
if (rule !== 'required' && (value === null || value === undefined)) {
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
137
195
|
|
|
138
|
-
|
|
139
|
-
errors.push('required');
|
|
196
|
+
errors.push(...validate(value, rule));
|
|
140
197
|
}
|
|
141
198
|
|
|
142
199
|
return errors.length > 0 ? errors : null;
|
package/src/forms/index.ts
CHANGED
package/src/forms/utils.ts
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
1
|
import { FormFieldTypes } from './Form';
|
|
2
2
|
import type { FormFieldDefinition } from './Form';
|
|
3
3
|
|
|
4
|
-
export function booleanInput(
|
|
4
|
+
export function booleanInput(
|
|
5
|
+
defaultValue?: boolean,
|
|
6
|
+
options: { rules?: string } = {},
|
|
7
|
+
): FormFieldDefinition<typeof FormFieldTypes.Boolean> {
|
|
5
8
|
return {
|
|
6
9
|
default: defaultValue,
|
|
7
10
|
type: FormFieldTypes.Boolean,
|
|
11
|
+
rules: options.rules,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function dateInput(
|
|
16
|
+
defaultValue?: Date,
|
|
17
|
+
options: { rules?: string } = {},
|
|
18
|
+
): FormFieldDefinition<typeof FormFieldTypes.Date> {
|
|
19
|
+
return {
|
|
20
|
+
default: defaultValue,
|
|
21
|
+
type: FormFieldTypes.Date,
|
|
22
|
+
rules: options.rules,
|
|
8
23
|
};
|
|
9
24
|
}
|
|
10
25
|
|
|
@@ -18,6 +33,14 @@ export function requiredBooleanInput(
|
|
|
18
33
|
};
|
|
19
34
|
}
|
|
20
35
|
|
|
36
|
+
export function requiredDateInput(defaultValue?: Date): FormFieldDefinition<typeof FormFieldTypes.Date> {
|
|
37
|
+
return {
|
|
38
|
+
default: defaultValue,
|
|
39
|
+
type: FormFieldTypes.Date,
|
|
40
|
+
rules: 'required',
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
21
44
|
export function requiredNumberInput(
|
|
22
45
|
defaultValue?: number,
|
|
23
46
|
): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
|
|
@@ -38,16 +61,24 @@ export function requiredStringInput(
|
|
|
38
61
|
};
|
|
39
62
|
}
|
|
40
63
|
|
|
41
|
-
export function numberInput(
|
|
64
|
+
export function numberInput(
|
|
65
|
+
defaultValue?: number,
|
|
66
|
+
options: { rules?: string } = {},
|
|
67
|
+
): FormFieldDefinition<typeof FormFieldTypes.Number> {
|
|
42
68
|
return {
|
|
43
69
|
default: defaultValue,
|
|
44
70
|
type: FormFieldTypes.Number,
|
|
71
|
+
rules: options.rules,
|
|
45
72
|
};
|
|
46
73
|
}
|
|
47
74
|
|
|
48
|
-
export function stringInput(
|
|
75
|
+
export function stringInput(
|
|
76
|
+
defaultValue?: string,
|
|
77
|
+
options: { rules?: string } = {},
|
|
78
|
+
): FormFieldDefinition<typeof FormFieldTypes.String> {
|
|
49
79
|
return {
|
|
50
80
|
default: defaultValue,
|
|
51
81
|
type: FormFieldTypes.String,
|
|
82
|
+
rules: options.rules,
|
|
52
83
|
};
|
|
53
84
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { arrayFrom } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
const builtInRules: Record<string, FormFieldValidator> = {
|
|
4
|
+
required: (value) => (value ? undefined : 'required'),
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type FormFieldValidator<T = unknown> = (value: T) => string | string[] | undefined;
|
|
8
|
+
|
|
9
|
+
export const validators: Record<string, FormFieldValidator> = { ...builtInRules };
|
|
10
|
+
|
|
11
|
+
export function defineFormValidationRule<T>(rule: string, validator: FormFieldValidator<T>): void {
|
|
12
|
+
validators[rule] = validator as FormFieldValidator;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function validate(value: unknown, rule: string): string[] {
|
|
16
|
+
const errors = validators[rule]?.(value);
|
|
17
|
+
|
|
18
|
+
return errors ? arrayFrom(errors) : [];
|
|
19
|
+
}
|
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,45 +20,54 @@ 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
|
-
|
|
52
|
+
return navigator.languages.find((locale) => locales.includes(locale)) ?? 'en';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected async boot(): Promise<void> {
|
|
56
|
+
if (!globalThis.document) {
|
|
57
|
+
return;
|
|
53
58
|
}
|
|
54
59
|
|
|
55
|
-
|
|
60
|
+
this.requireStore().$subscribe(
|
|
61
|
+
async () => {
|
|
62
|
+
await this.provider.setLocale(this.locale ?? this.getBrowserLocale());
|
|
63
|
+
await this.provider.setFallbackLocale(this.fallbackLocale);
|
|
64
|
+
|
|
65
|
+
this.locale
|
|
66
|
+
? document.querySelector('html')?.setAttribute('lang', this.locale)
|
|
67
|
+
: document.querySelector('html')?.removeAttribute('lang');
|
|
68
|
+
},
|
|
69
|
+
{ immediate: true },
|
|
70
|
+
);
|
|
56
71
|
}
|
|
57
72
|
|
|
58
73
|
}
|
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';
|
|
@@ -8,10 +8,24 @@ export default defineServiceState({
|
|
|
8
8
|
initialState: {
|
|
9
9
|
plugins: {} as Record<string, Plugin>,
|
|
10
10
|
environment: Aerogel.environment,
|
|
11
|
+
version: Aerogel.version,
|
|
11
12
|
sourceUrl: Aerogel.sourceUrl,
|
|
12
13
|
},
|
|
13
14
|
computed: {
|
|
14
15
|
development: (state) => state.environment === 'development',
|
|
15
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
|
+
},
|
|
16
30
|
},
|
|
17
31
|
});
|