@aerogel/core 0.0.0-next.7f6ed5a1f91688a86bf5ede2adc465e4fd6cfdea → 0.0.0-next.8323c60b905020dcb3bd9d4b0bc8d9b6529e1082
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.css +1 -0
- package/dist/aerogel-core.d.ts +2547 -422
- package/dist/aerogel-core.js +3722 -0
- package/dist/aerogel-core.js.map +1 -0
- package/package.json +39 -37
- package/src/bootstrap/bootstrap.test.ts +7 -66
- package/src/bootstrap/index.ts +46 -33
- package/src/bootstrap/options.ts +8 -1
- package/src/components/AppLayout.vue +14 -0
- package/src/components/AppModals.vue +14 -0
- package/src/components/AppOverlays.vue +9 -0
- package/src/components/AppToasts.vue +16 -0
- package/src/components/contracts/AlertModal.ts +19 -0
- package/src/components/contracts/Button.ts +16 -0
- package/src/components/contracts/ConfirmModal.ts +48 -0
- package/src/components/contracts/DropdownMenu.ts +25 -0
- package/src/components/contracts/ErrorReportModal.ts +33 -0
- package/src/components/contracts/Input.ts +26 -0
- package/src/components/contracts/LoadingModal.ts +26 -0
- package/src/components/contracts/Modal.ts +21 -0
- package/src/components/contracts/PromptModal.ts +34 -0
- package/src/components/contracts/Select.ts +45 -0
- package/src/components/contracts/Toast.ts +15 -0
- package/src/components/contracts/index.ts +11 -0
- package/src/components/headless/HeadlessButton.vue +51 -0
- package/src/components/headless/HeadlessInput.vue +59 -0
- package/src/components/headless/HeadlessInputDescription.vue +27 -0
- package/src/components/headless/HeadlessInputError.vue +22 -0
- package/src/components/headless/HeadlessInputInput.vue +86 -0
- package/src/components/headless/HeadlessInputLabel.vue +18 -0
- package/src/components/headless/HeadlessInputTextArea.vue +40 -0
- package/src/components/headless/HeadlessModal.vue +57 -0
- package/src/components/headless/HeadlessModalContent.vue +30 -0
- package/src/components/headless/HeadlessModalDescription.vue +12 -0
- package/src/components/headless/HeadlessModalOverlay.vue +12 -0
- package/src/components/headless/HeadlessModalTitle.vue +12 -0
- package/src/components/headless/HeadlessSelect.vue +120 -0
- package/src/components/headless/HeadlessSelectError.vue +25 -0
- package/src/components/headless/HeadlessSelectLabel.vue +25 -0
- package/src/components/headless/HeadlessSelectOption.vue +34 -0
- package/src/components/headless/HeadlessSelectOptions.vue +42 -0
- package/src/components/headless/HeadlessSelectTrigger.vue +22 -0
- package/src/components/headless/HeadlessSelectValue.vue +18 -0
- package/src/components/headless/HeadlessSwitch.vue +96 -0
- package/src/components/headless/HeadlessToast.vue +18 -0
- package/src/components/headless/HeadlessToastAction.vue +13 -0
- package/src/components/headless/index.ts +20 -2
- package/src/components/index.ts +6 -6
- package/src/components/ui/AdvancedOptions.vue +18 -0
- package/src/components/ui/AlertModal.vue +17 -0
- package/src/components/ui/Button.vue +115 -0
- package/src/components/ui/Checkbox.vue +56 -0
- package/src/components/ui/ConfirmModal.vue +50 -0
- package/src/components/ui/DropdownMenu.vue +32 -0
- package/src/components/ui/DropdownMenuOption.vue +22 -0
- package/src/components/ui/DropdownMenuOptions.vue +44 -0
- package/src/components/ui/EditableContent.vue +82 -0
- package/src/components/ui/ErrorLogs.vue +19 -0
- package/src/components/ui/ErrorLogsModal.vue +48 -0
- package/src/components/ui/ErrorMessage.vue +15 -0
- package/src/components/ui/ErrorReportModal.vue +73 -0
- package/src/components/ui/ErrorReportModalButtons.vue +118 -0
- package/src/components/ui/ErrorReportModalTitle.vue +24 -0
- package/src/components/ui/Form.vue +24 -0
- package/src/components/ui/Input.vue +56 -0
- package/src/components/ui/Link.vue +12 -0
- package/src/components/ui/LoadingModal.vue +34 -0
- package/src/components/ui/Markdown.vue +97 -0
- package/src/components/ui/Modal.vue +131 -0
- package/src/components/ui/ModalContext.vue +31 -0
- package/src/components/ui/ProgressBar.vue +51 -0
- package/src/components/ui/PromptModal.vue +38 -0
- package/src/components/ui/Select.vue +27 -0
- package/src/components/ui/SelectLabel.vue +21 -0
- package/src/components/ui/SelectOption.vue +29 -0
- package/src/components/ui/SelectOptions.vue +35 -0
- package/src/components/ui/SelectTrigger.vue +29 -0
- package/src/components/ui/Setting.vue +31 -0
- package/src/components/ui/SettingsModal.vue +15 -0
- package/src/components/ui/StartupCrash.vue +31 -0
- package/src/components/ui/Switch.vue +11 -0
- package/src/components/ui/TextArea.vue +56 -0
- package/src/components/ui/Toast.vue +46 -0
- package/src/components/ui/index.ts +35 -0
- package/src/directives/index.ts +29 -6
- package/src/directives/measure.ts +46 -0
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +200 -0
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +53 -0
- package/src/errors/settings/Debug.vue +32 -0
- package/src/errors/settings/index.ts +10 -0
- package/src/errors/utils.ts +35 -0
- package/src/forms/FormController.test.ts +113 -0
- package/src/forms/FormController.ts +255 -0
- package/src/forms/index.ts +3 -2
- package/src/forms/utils.ts +87 -14
- package/src/forms/validation.ts +50 -0
- package/src/index.css +76 -0
- package/src/{main.ts → index.ts} +5 -0
- package/src/jobs/Job.ts +147 -0
- package/src/jobs/index.ts +10 -0
- package/src/jobs/listeners.ts +3 -0
- package/src/jobs/status.ts +4 -0
- package/src/lang/DefaultLangProvider.ts +46 -0
- package/src/lang/Lang.state.ts +11 -0
- package/src/lang/Lang.ts +63 -9
- package/src/lang/index.ts +22 -75
- package/src/lang/settings/Language.vue +48 -0
- package/src/lang/settings/index.ts +10 -0
- package/src/lang/utils.ts +4 -0
- package/src/plugins/Plugin.ts +8 -0
- package/src/plugins/index.ts +29 -0
- package/src/services/App.state.ts +50 -0
- package/src/services/App.ts +63 -0
- package/src/services/Cache.ts +43 -0
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +110 -36
- package/src/services/Service.ts +273 -35
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +45 -8
- package/src/services/store.ts +30 -0
- package/src/services/utils.ts +18 -0
- package/src/testing/index.ts +30 -0
- package/src/testing/setup.ts +11 -0
- package/src/types/vite.d.ts +0 -2
- package/src/ui/UI.state.ts +21 -13
- package/src/ui/UI.ts +350 -53
- package/src/ui/index.ts +40 -25
- package/src/ui/utils.ts +16 -0
- package/src/utils/app.ts +7 -0
- package/src/utils/classes.ts +41 -0
- package/src/utils/composition/events.ts +4 -5
- package/src/utils/composition/forms.ts +27 -0
- package/src/utils/composition/hooks.ts +9 -0
- package/src/utils/composition/persistent.test.ts +33 -0
- package/src/utils/composition/persistent.ts +11 -0
- package/src/utils/composition/state.test.ts +47 -0
- package/src/utils/composition/state.ts +33 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/markdown.test.ts +50 -0
- package/src/utils/markdown.ts +60 -4
- package/src/utils/types.ts +3 -0
- package/src/utils/vue.ts +38 -121
- package/.eslintrc.js +0 -3
- package/dist/aerogel-core.cjs.js +0 -2
- package/dist/aerogel-core.cjs.js.map +0 -1
- package/dist/aerogel-core.esm.js +0 -2
- package/dist/aerogel-core.esm.js.map +0 -1
- package/noeldemartin.config.js +0 -2
- package/src/bootstrap/hooks.ts +0 -19
- package/src/components/AGAppLayout.vue +0 -11
- package/src/components/AGAppOverlays.vue +0 -39
- package/src/components/basic/AGMarkdown.vue +0 -20
- package/src/components/basic/index.ts +0 -3
- package/src/components/forms/AGButton.vue +0 -11
- package/src/components/forms/AGForm.vue +0 -26
- package/src/components/forms/AGInput.vue +0 -32
- package/src/components/forms/index.ts +0 -5
- package/src/components/headless/forms/AGHeadlessButton.vue +0 -51
- package/src/components/headless/forms/AGHeadlessInput.ts +0 -8
- package/src/components/headless/forms/AGHeadlessInput.vue +0 -54
- package/src/components/headless/forms/AGHeadlessInputError.vue +0 -22
- package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -29
- package/src/components/headless/forms/index.ts +0 -4
- package/src/components/headless/modals/AGHeadlessModal.ts +0 -7
- package/src/components/headless/modals/AGHeadlessModal.vue +0 -84
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +0 -20
- package/src/components/headless/modals/AGHeadlessModalTitle.vue +0 -13
- package/src/components/headless/modals/index.ts +0 -6
- package/src/components/modals/AGAlertModal.vue +0 -15
- package/src/components/modals/AGModal.ts +0 -6
- package/src/components/modals/AGModal.vue +0 -18
- package/src/components/modals/AGModalContext.ts +0 -8
- package/src/components/modals/AGModalContext.vue +0 -22
- package/src/components/modals/index.ts +0 -5
- package/src/directives/initial-focus.ts +0 -11
- package/src/forms/Form.test.ts +0 -37
- package/src/forms/Form.ts +0 -154
- package/src/forms/composition.ts +0 -6
- 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/tsconfig.json +0 -19
- package/vite.config.ts +0 -17
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { computed, nextTick, reactive, readonly, ref } from 'vue';
|
|
2
|
+
import { MagicObject, arrayRemove, fail, toString } from '@noeldemartin/utils';
|
|
3
|
+
import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
4
|
+
|
|
5
|
+
import { validate, validateType } from './validation';
|
|
6
|
+
|
|
7
|
+
export const __valueType: unique symbol = Symbol();
|
|
8
|
+
|
|
9
|
+
export interface FormFieldDefinition<
|
|
10
|
+
TType extends FormFieldType = FormFieldType,
|
|
11
|
+
TRules extends string = string,
|
|
12
|
+
TValueType = unknown,
|
|
13
|
+
> {
|
|
14
|
+
type: TType;
|
|
15
|
+
trim?: boolean;
|
|
16
|
+
default?: GetFormFieldValue<TType>;
|
|
17
|
+
rules?: TRules;
|
|
18
|
+
values?: readonly TValueType[];
|
|
19
|
+
[__valueType]?: TValueType;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type FormFieldType = 'string' | 'enum' | 'number' | 'boolean' | 'object' | 'date';
|
|
23
|
+
export type FormFieldValue = GetFormFieldValue<FormFieldType>;
|
|
24
|
+
export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
|
|
25
|
+
|
|
26
|
+
export type FormData<T> = {
|
|
27
|
+
-readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules, infer TValueType>
|
|
28
|
+
? TRules extends 'required'
|
|
29
|
+
? GetFormFieldValue<TType, TValueType>
|
|
30
|
+
: GetFormFieldValue<TType, TValueType> | null
|
|
31
|
+
: never;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type FormErrors<T> = {
|
|
35
|
+
[k in keyof T]: string[] | null;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type GetFormFieldValue<TType, TValueType = unknown> = TType extends 'string'
|
|
39
|
+
? string
|
|
40
|
+
: TType extends 'number'
|
|
41
|
+
? number
|
|
42
|
+
: TType extends 'boolean'
|
|
43
|
+
? boolean
|
|
44
|
+
: TType extends 'enum'
|
|
45
|
+
? TValueType
|
|
46
|
+
: TType extends 'object'
|
|
47
|
+
? TValueType extends object
|
|
48
|
+
? TValueType
|
|
49
|
+
: object
|
|
50
|
+
: TType extends 'date'
|
|
51
|
+
? Date
|
|
52
|
+
: never;
|
|
53
|
+
|
|
54
|
+
const validForms: WeakMap<FormController, ComputedRef<boolean>> = new WeakMap();
|
|
55
|
+
|
|
56
|
+
export type SubmitFormListener = () => unknown;
|
|
57
|
+
export type FocusFormListener = (input: string) => unknown;
|
|
58
|
+
|
|
59
|
+
export default class FormController<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
60
|
+
|
|
61
|
+
public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
|
|
62
|
+
|
|
63
|
+
private _fields: Fields;
|
|
64
|
+
private _data: FormData<Fields>;
|
|
65
|
+
private _submitted: Ref<boolean>;
|
|
66
|
+
private _errors: FormErrors<Fields>;
|
|
67
|
+
private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
|
|
68
|
+
|
|
69
|
+
constructor(fields: Fields) {
|
|
70
|
+
super();
|
|
71
|
+
|
|
72
|
+
this._fields = fields;
|
|
73
|
+
this._submitted = ref(false);
|
|
74
|
+
this._data = this.getInitialData(fields);
|
|
75
|
+
this._errors = this.getInitialErrors(fields);
|
|
76
|
+
|
|
77
|
+
validForms.set(
|
|
78
|
+
this,
|
|
79
|
+
computed(() => !Object.values(this._errors).some((error) => error !== null)),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
this.errors = readonly(this._errors);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public get valid(): boolean {
|
|
86
|
+
return !!validForms.get(this)?.value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public get submitted(): boolean {
|
|
90
|
+
return this._submitted.value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public setFieldValue<T extends keyof Fields>(field: T, value: FormData<Fields>[T]): void {
|
|
94
|
+
const definition =
|
|
95
|
+
this._fields[field] ?? fail<FormFieldDefinition>(`Trying to set undefined '${toString(field)}' field`);
|
|
96
|
+
|
|
97
|
+
this._data[field] =
|
|
98
|
+
definition.type === 'string' && (definition.trim ?? true)
|
|
99
|
+
? (toString(value).trim() as FormData<Fields>[T])
|
|
100
|
+
: value;
|
|
101
|
+
|
|
102
|
+
if (this._submitted.value) {
|
|
103
|
+
this.validate();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public getFieldValue<T extends keyof Fields>(field: T): GetFormFieldValue<Fields[T]['type']> {
|
|
108
|
+
return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public getFieldRules<T extends keyof Fields>(field: T): string[] {
|
|
112
|
+
return this._fields[field]?.rules?.split('|') ?? [];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public getFieldType<T extends keyof Fields>(field: T): FormFieldType | null {
|
|
116
|
+
return this._fields[field]?.type ?? null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public data(): FormData<Fields> {
|
|
120
|
+
return { ...this._data };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public validate(): boolean {
|
|
124
|
+
const errors = Object.entries(this._fields).reduce(
|
|
125
|
+
(formErrors, [name, definition]) => {
|
|
126
|
+
formErrors[name] = this.getFieldErrors(name, definition);
|
|
127
|
+
|
|
128
|
+
return formErrors;
|
|
129
|
+
},
|
|
130
|
+
{} as Record<string, string[] | null>,
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
this.resetErrors(errors);
|
|
134
|
+
|
|
135
|
+
return this.valid;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public reset(options: { keepData?: boolean; keepErrors?: boolean } = {}): void {
|
|
139
|
+
this._submitted.value = false;
|
|
140
|
+
|
|
141
|
+
options.keepData || this.resetData();
|
|
142
|
+
options.keepErrors || this.resetErrors();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public submit(): boolean {
|
|
146
|
+
this._submitted.value = true;
|
|
147
|
+
|
|
148
|
+
const valid = this.validate();
|
|
149
|
+
|
|
150
|
+
valid && this._listeners['submit']?.forEach((listener) => listener());
|
|
151
|
+
|
|
152
|
+
return valid;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public on(event: 'focus', listener: FocusFormListener): () => void;
|
|
156
|
+
public on(event: 'submit', listener: SubmitFormListener): () => void;
|
|
157
|
+
public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
|
|
158
|
+
this._listeners[event] ??= [];
|
|
159
|
+
|
|
160
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
161
|
+
this._listeners[event]?.push(listener as any);
|
|
162
|
+
|
|
163
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
164
|
+
return () => this.off(event as any, listener);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public off(event: 'focus', listener: FocusFormListener): void;
|
|
168
|
+
public off(event: 'submit', listener: SubmitFormListener): void;
|
|
169
|
+
public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
|
|
170
|
+
arrayRemove(this._listeners[event] ?? [], listener);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
public async focus(input: string): Promise<void> {
|
|
174
|
+
await nextTick();
|
|
175
|
+
|
|
176
|
+
this._listeners['focus']?.forEach((listener) => listener(input));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
protected override __get(property: string): unknown {
|
|
180
|
+
if (!(property in this._fields)) {
|
|
181
|
+
return super.__get(property);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return this.getFieldValue(property);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
protected override __set(property: string, value: unknown): void {
|
|
188
|
+
if (!(property in this._fields)) {
|
|
189
|
+
super.__set(property, value);
|
|
190
|
+
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
this.setFieldValue(property, value as FormData<Fields>[string]);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private getFieldErrors(name: keyof Fields, definition: FormFieldDefinition): string[] | null {
|
|
198
|
+
const errors = [];
|
|
199
|
+
const value = this._data[name];
|
|
200
|
+
const rules = definition.rules?.split('|') ?? [];
|
|
201
|
+
|
|
202
|
+
errors.push(...validateType(value, definition));
|
|
203
|
+
|
|
204
|
+
for (const rule of rules) {
|
|
205
|
+
if (rule !== 'required' && (value === null || value === undefined)) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
errors.push(...validate(value, rule));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return errors.length > 0 ? errors : null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private getInitialData(fields: Fields): FormData<Fields> {
|
|
216
|
+
if (this.static().isConjuring()) {
|
|
217
|
+
return {} as FormData<Fields>;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const data = Object.entries(fields).reduce((initialData, [name, definition]) => {
|
|
221
|
+
initialData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
|
|
222
|
+
|
|
223
|
+
return initialData;
|
|
224
|
+
}, {} as FormData<Fields>);
|
|
225
|
+
|
|
226
|
+
return reactive(data) as FormData<Fields>;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private getInitialErrors(fields: Fields): FormErrors<Fields> {
|
|
230
|
+
if (this.static().isConjuring()) {
|
|
231
|
+
return {} as FormErrors<Fields>;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const errors = Object.keys(fields).reduce((formErrors, name) => {
|
|
235
|
+
formErrors[name as keyof Fields] = null;
|
|
236
|
+
|
|
237
|
+
return formErrors;
|
|
238
|
+
}, {} as FormErrors<Fields>);
|
|
239
|
+
|
|
240
|
+
return reactive(errors) as FormErrors<Fields>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private resetData(): void {
|
|
244
|
+
for (const [name, field] of Object.entries(this._fields)) {
|
|
245
|
+
this._data[name as keyof Fields] = (field.default ?? null) as FormData<Fields>[keyof Fields];
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private resetErrors(errors?: Record<string, string[] | null>): void {
|
|
250
|
+
Object.keys(this._errors).forEach((key) => delete this._errors[key as keyof Fields]);
|
|
251
|
+
|
|
252
|
+
errors && Object.assign(this._errors, errors);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
}
|
package/src/forms/index.ts
CHANGED
package/src/forms/utils.ts
CHANGED
|
@@ -1,36 +1,109 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { FormFieldDefinition } from './Form';
|
|
1
|
+
import type { FormFieldDefinition } from './FormController';
|
|
3
2
|
|
|
4
|
-
export function
|
|
5
|
-
defaultValue?: number,
|
|
6
|
-
): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
|
|
3
|
+
export function booleanInput(defaultValue?: boolean, options: { rules?: string } = {}): FormFieldDefinition<'boolean'> {
|
|
7
4
|
return {
|
|
8
5
|
default: defaultValue,
|
|
9
|
-
type:
|
|
6
|
+
type: 'boolean',
|
|
7
|
+
rules: options.rules,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function dateInput(defaultValue?: Date, options: { rules?: string } = {}): FormFieldDefinition<'date'> {
|
|
12
|
+
return {
|
|
13
|
+
default: defaultValue,
|
|
14
|
+
type: 'date',
|
|
15
|
+
rules: options.rules,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function enumInput<const T extends string>(
|
|
20
|
+
values: readonly T[],
|
|
21
|
+
defaultValue?: T,
|
|
22
|
+
options: { rules?: string } = {},
|
|
23
|
+
): FormFieldDefinition<'enum', string, T> {
|
|
24
|
+
return {
|
|
25
|
+
default: defaultValue,
|
|
26
|
+
type: 'enum',
|
|
27
|
+
rules: options.rules,
|
|
28
|
+
values,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function requiredBooleanInput(defaultValue?: boolean): FormFieldDefinition<'boolean', 'required'> {
|
|
33
|
+
return {
|
|
34
|
+
default: defaultValue,
|
|
35
|
+
type: 'boolean',
|
|
36
|
+
rules: 'required',
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function requiredDateInput(defaultValue?: Date): FormFieldDefinition<'date', 'required'> {
|
|
41
|
+
return {
|
|
42
|
+
default: defaultValue,
|
|
43
|
+
type: 'date',
|
|
44
|
+
rules: 'required',
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function requiredEnumInput<const T extends string>(
|
|
49
|
+
values: readonly T[],
|
|
50
|
+
defaultValue?: T,
|
|
51
|
+
): FormFieldDefinition<'enum', 'required', T> {
|
|
52
|
+
return {
|
|
53
|
+
default: defaultValue,
|
|
54
|
+
type: 'enum',
|
|
55
|
+
rules: 'required',
|
|
56
|
+
values,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function requiredNumberInput(defaultValue?: number): FormFieldDefinition<'number', 'required'> {
|
|
61
|
+
return {
|
|
62
|
+
default: defaultValue,
|
|
63
|
+
type: 'number',
|
|
10
64
|
rules: 'required',
|
|
11
65
|
};
|
|
12
66
|
}
|
|
13
67
|
|
|
14
|
-
export function
|
|
15
|
-
defaultValue?: string,
|
|
16
|
-
): FormFieldDefinition<typeof FormFieldTypes.String, 'required'> {
|
|
68
|
+
export function requiredObjectInput<T extends object>(defaultValue?: T): FormFieldDefinition<'object', 'required', T> {
|
|
17
69
|
return {
|
|
18
70
|
default: defaultValue,
|
|
19
|
-
type:
|
|
71
|
+
type: 'object',
|
|
20
72
|
rules: 'required',
|
|
21
73
|
};
|
|
22
74
|
}
|
|
23
75
|
|
|
24
|
-
export function
|
|
76
|
+
export function requiredStringInput(defaultValue?: string): FormFieldDefinition<'string', 'required'> {
|
|
77
|
+
return {
|
|
78
|
+
default: defaultValue,
|
|
79
|
+
type: 'string',
|
|
80
|
+
rules: 'required',
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function numberInput(defaultValue?: number, options: { rules?: string } = {}): FormFieldDefinition<'number'> {
|
|
85
|
+
return {
|
|
86
|
+
default: defaultValue,
|
|
87
|
+
type: 'number',
|
|
88
|
+
rules: options.rules,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function objectInput<T extends object>(
|
|
93
|
+
defaultValue?: T,
|
|
94
|
+
options: { rules?: string } = {},
|
|
95
|
+
): FormFieldDefinition<'object', string, T> {
|
|
25
96
|
return {
|
|
26
97
|
default: defaultValue,
|
|
27
|
-
type:
|
|
98
|
+
type: 'object',
|
|
99
|
+
rules: options.rules,
|
|
28
100
|
};
|
|
29
101
|
}
|
|
30
102
|
|
|
31
|
-
export function stringInput(defaultValue?: string): FormFieldDefinition<
|
|
103
|
+
export function stringInput(defaultValue?: string, options: { rules?: string } = {}): FormFieldDefinition<'string'> {
|
|
32
104
|
return {
|
|
33
105
|
default: defaultValue,
|
|
34
|
-
type:
|
|
106
|
+
type: 'string',
|
|
107
|
+
rules: options.rules,
|
|
35
108
|
};
|
|
36
109
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { arrayFrom } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import type { FormFieldDefinition } from '@aerogel/core/forms/FormController';
|
|
4
|
+
|
|
5
|
+
const builtInRules: Record<string, FormFieldValidator> = {
|
|
6
|
+
required: (value) => (value ? undefined : 'required'),
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
function isValidType(value: unknown, definition: FormFieldDefinition): boolean {
|
|
10
|
+
if (value === undefined || value === null) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
switch (definition.type) {
|
|
15
|
+
case 'string':
|
|
16
|
+
return typeof value === 'string';
|
|
17
|
+
case 'enum':
|
|
18
|
+
return !!definition.values?.includes(value);
|
|
19
|
+
case 'number':
|
|
20
|
+
return typeof value === 'number';
|
|
21
|
+
case 'boolean':
|
|
22
|
+
return typeof value === 'boolean';
|
|
23
|
+
case 'date':
|
|
24
|
+
return value instanceof Date;
|
|
25
|
+
case 'object':
|
|
26
|
+
return typeof value === 'object';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type FormFieldValidator<T = unknown> = (value: T) => string | string[] | undefined;
|
|
31
|
+
|
|
32
|
+
export const validators: Record<string, FormFieldValidator> = { ...builtInRules };
|
|
33
|
+
|
|
34
|
+
export function defineFormValidationRule<T>(rule: string, validator: FormFieldValidator<T>): void {
|
|
35
|
+
validators[rule] = validator as FormFieldValidator;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function validateType(value: unknown, definition: FormFieldDefinition): string[] {
|
|
39
|
+
if (isValidType(value, definition)) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return ['invalid_value'];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function validate(value: unknown, rule: string): string[] {
|
|
47
|
+
const errors = validators[rule]?.(value);
|
|
48
|
+
|
|
49
|
+
return errors ? arrayFrom(errors) : [];
|
|
50
|
+
}
|
package/src/index.css
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
@import 'tailwindcss';
|
|
2
|
+
|
|
3
|
+
@plugin '@tailwindcss/forms';
|
|
4
|
+
@plugin '@tailwindcss/typography';
|
|
5
|
+
|
|
6
|
+
@source './';
|
|
7
|
+
|
|
8
|
+
@theme {
|
|
9
|
+
--color-primary: oklch(0.205 0 0);
|
|
10
|
+
--color-primary-50: color-mix(in oklab, var(--color-primary-600) 5%, transparent);
|
|
11
|
+
--color-primary-100: color-mix(in oklab, var(--color-primary-600) 15%, transparent);
|
|
12
|
+
--color-primary-200: color-mix(in oklab, var(--color-primary-600) 30%, transparent);
|
|
13
|
+
--color-primary-300: color-mix(in oklab, var(--color-primary-600) 50%, transparent);
|
|
14
|
+
--color-primary-400: color-mix(in oklab, var(--color-primary-600) 65%, transparent);
|
|
15
|
+
--color-primary-500: color-mix(in oklab, var(--color-primary-600) 80%, transparent);
|
|
16
|
+
--color-primary-600: var(--color-primary);
|
|
17
|
+
--color-primary-700: color-mix(in oklab, var(--color-primary-600) 90%, black);
|
|
18
|
+
--color-primary-800: color-mix(in oklab, var(--color-primary-600) 80%, black);
|
|
19
|
+
--color-primary-900: color-mix(in oklab, var(--color-primary-600) 70%, black);
|
|
20
|
+
--color-primary-950: color-mix(in oklab, var(--color-primary-600) 50%, black);
|
|
21
|
+
|
|
22
|
+
--color-background: oklch(1 0 0);
|
|
23
|
+
--color-links: var(--color-primary);
|
|
24
|
+
|
|
25
|
+
--breakpoint-content: var(--breakpoint-md);
|
|
26
|
+
--spacing-edge: 1rem;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.clickable {
|
|
30
|
+
position: relative;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.clickable::after {
|
|
34
|
+
--clickable-size: 44px;
|
|
35
|
+
--clickable-inset-by: min(0px, calc((100% - var(--clickable-size)) / 2));
|
|
36
|
+
|
|
37
|
+
content: '';
|
|
38
|
+
position: absolute;
|
|
39
|
+
top: var(--clickable-inset-by);
|
|
40
|
+
left: var(--clickable-inset-by);
|
|
41
|
+
right: var(--clickable-inset-by);
|
|
42
|
+
bottom: var(--clickable-inset-by);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
input[type='number'].appearance-textfield {
|
|
46
|
+
appearance: textfield;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
input[type='number'].appearance-textfield::-webkit-outer-spin-button,
|
|
50
|
+
input[type='number'].appearance-textfield::-webkit-inner-spin-button {
|
|
51
|
+
appearance: none;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
button[data-markdown-action] {
|
|
55
|
+
color: var(--tw-prose-links);
|
|
56
|
+
text-decoration: underline;
|
|
57
|
+
font-weight: 500;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@keyframes fade-in {
|
|
61
|
+
0% {
|
|
62
|
+
opacity: 0;
|
|
63
|
+
}
|
|
64
|
+
100% {
|
|
65
|
+
opacity: 1;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@keyframes grow {
|
|
70
|
+
0% {
|
|
71
|
+
scale: 0;
|
|
72
|
+
}
|
|
73
|
+
100% {
|
|
74
|
+
opacity: 1;
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/{main.ts → index.ts}
RENAMED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
export * from './bootstrap';
|
|
2
2
|
export * from './components';
|
|
3
|
+
export * from './directives';
|
|
4
|
+
export * from './errors';
|
|
3
5
|
export * from './forms';
|
|
6
|
+
export * from './jobs';
|
|
4
7
|
export * from './lang';
|
|
8
|
+
export * from './plugins';
|
|
5
9
|
export * from './services';
|
|
10
|
+
export * from './testing';
|
|
6
11
|
export * from './ui';
|
|
7
12
|
export * from './utils';
|
package/src/jobs/Job.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { ListenersManager, PromisedValue, round, tap, toError } from '@noeldemartin/utils';
|
|
2
|
+
import type { Listeners } from '@noeldemartin/utils';
|
|
3
|
+
|
|
4
|
+
import JobCancelledError from '@aerogel/core/errors/JobCancelledError';
|
|
5
|
+
|
|
6
|
+
import type { JobListener } from './listeners';
|
|
7
|
+
import type { JobStatus } from './status';
|
|
8
|
+
|
|
9
|
+
export default abstract class Job<
|
|
10
|
+
Listener extends JobListener = JobListener,
|
|
11
|
+
Status extends JobStatus = JobStatus,
|
|
12
|
+
SerializedStatus extends JobStatus = JobStatus,
|
|
13
|
+
> {
|
|
14
|
+
|
|
15
|
+
protected status: Status;
|
|
16
|
+
protected _listeners: ListenersManager<JobListener>;
|
|
17
|
+
protected _progress?: number;
|
|
18
|
+
protected _cancelled?: PromisedValue<void>;
|
|
19
|
+
protected _started: PromisedValue<void>;
|
|
20
|
+
protected _completed: PromisedValue<void>;
|
|
21
|
+
|
|
22
|
+
constructor() {
|
|
23
|
+
this.status = this.getInitialStatus();
|
|
24
|
+
this._listeners = new ListenersManager();
|
|
25
|
+
this._started = new PromisedValue();
|
|
26
|
+
this._completed = new PromisedValue();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public async start(): Promise<void> {
|
|
30
|
+
this.beforeStart();
|
|
31
|
+
this._started.resolve();
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
await this.updateProgress();
|
|
35
|
+
await this.run();
|
|
36
|
+
await this.updateProgress();
|
|
37
|
+
|
|
38
|
+
this._completed.resolve();
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (error instanceof JobCancelledError) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
throw tap(toError(error), (realError) => {
|
|
45
|
+
this._completed.reject(realError);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public async cancel(): Promise<void> {
|
|
51
|
+
this._cancelled = new PromisedValue();
|
|
52
|
+
|
|
53
|
+
await this._cancelled;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public serialize(): SerializedStatus {
|
|
57
|
+
return this.serializeStatus(this.status);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public get listeners(): Listeners<Listener> {
|
|
61
|
+
return this._listeners;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public get progress(): number {
|
|
65
|
+
return this._progress ?? 0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public get cancelled(): boolean {
|
|
69
|
+
return !!this._cancelled?.isResolved();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public get started(): Promise<void> {
|
|
73
|
+
return this._started;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public get completed(): Promise<void> {
|
|
77
|
+
return this._completed;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
protected abstract run(): Promise<void>;
|
|
81
|
+
|
|
82
|
+
protected getInitialStatus(): Status {
|
|
83
|
+
return { completed: false } as Status;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected beforeStart(): void {
|
|
87
|
+
if (!this._started.isResolved()) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (this._cancelled) {
|
|
92
|
+
delete this._progress;
|
|
93
|
+
delete this._cancelled;
|
|
94
|
+
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
throw new Error('Job already started!');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
protected assertNotCancelled(): void {
|
|
102
|
+
if (!this._cancelled) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this._cancelled.resolve();
|
|
107
|
+
|
|
108
|
+
throw new JobCancelledError();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
protected calculateCurrentProgress(status?: JobStatus): number {
|
|
112
|
+
status ??= this.status;
|
|
113
|
+
|
|
114
|
+
if (status.completed) {
|
|
115
|
+
return 1;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!status.children) {
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return round(
|
|
123
|
+
status.children.reduce((total, child) => total + this.calculateCurrentProgress(child), 0) /
|
|
124
|
+
status.children.length,
|
|
125
|
+
2,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
protected async updateProgress(update?: (status: Status) => unknown): Promise<void> {
|
|
130
|
+
await update?.(this.status);
|
|
131
|
+
|
|
132
|
+
const progress = this.calculateCurrentProgress();
|
|
133
|
+
|
|
134
|
+
if (progress === this._progress) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
this._progress = progress;
|
|
139
|
+
|
|
140
|
+
await this._listeners.emit('onUpdated', progress);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
protected serializeStatus(status: Status): SerializedStatus {
|
|
144
|
+
return { ...status } as unknown as SerializedStatus;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
}
|