@aerogel/core 0.0.0-next.d824b40e5d06757cd9f47c9f771d916185df4f05 → 0.0.0-next.f0368a3107664018eb88652e719d2cb7d24b82e7

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