@aerogel/core 0.0.0-next.b85327579d32f21c6a9fa21142f0165cdd320d7e → 0.0.0-next.bb9dcdbb118a15d146d3a1c4cf861ca2f4f1eebd

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 (137) hide show
  1. package/dist/aerogel-core.cjs.js +1 -1
  2. package/dist/aerogel-core.cjs.js.map +1 -1
  3. package/dist/aerogel-core.d.ts +1880 -250
  4. package/dist/aerogel-core.esm.js +1 -1
  5. package/dist/aerogel-core.esm.js.map +1 -1
  6. package/histoire.config.ts +7 -0
  7. package/noeldemartin.config.js +4 -1
  8. package/package.json +13 -4
  9. package/postcss.config.js +6 -0
  10. package/src/assets/histoire.css +3 -0
  11. package/src/bootstrap/bootstrap.test.ts +3 -3
  12. package/src/bootstrap/index.ts +35 -5
  13. package/src/bootstrap/options.ts +3 -0
  14. package/src/components/AGAppLayout.vue +7 -2
  15. package/src/components/AGAppModals.vue +15 -0
  16. package/src/components/AGAppOverlays.vue +10 -8
  17. package/src/components/AGAppSnackbars.vue +13 -0
  18. package/src/components/composition.ts +23 -0
  19. package/src/components/constants.ts +8 -0
  20. package/src/components/forms/AGButton.vue +25 -15
  21. package/src/components/forms/AGCheckbox.vue +7 -1
  22. package/src/components/forms/AGForm.vue +9 -10
  23. package/src/components/forms/AGInput.vue +10 -6
  24. package/src/components/forms/AGSelect.story.vue +46 -0
  25. package/src/components/forms/AGSelect.vue +60 -0
  26. package/src/components/forms/index.ts +5 -6
  27. package/src/components/headless/forms/AGHeadlessButton.ts +3 -0
  28. package/src/components/headless/forms/AGHeadlessButton.vue +24 -12
  29. package/src/components/headless/forms/AGHeadlessInput.ts +30 -4
  30. package/src/components/headless/forms/AGHeadlessInput.vue +23 -7
  31. package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
  32. package/src/components/headless/forms/AGHeadlessInputInput.vue +44 -5
  33. package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
  34. package/src/components/headless/forms/AGHeadlessInputTextArea.vue +43 -0
  35. package/src/components/headless/forms/AGHeadlessSelect.ts +42 -0
  36. package/src/components/headless/forms/AGHeadlessSelect.vue +77 -0
  37. package/src/components/headless/forms/AGHeadlessSelectButton.vue +24 -0
  38. package/src/components/headless/forms/AGHeadlessSelectError.vue +26 -0
  39. package/src/components/headless/forms/AGHeadlessSelectLabel.vue +24 -0
  40. package/src/components/headless/forms/AGHeadlessSelectOption.ts +4 -0
  41. package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
  42. package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
  43. package/src/components/headless/forms/composition.ts +10 -0
  44. package/src/components/headless/forms/index.ts +13 -1
  45. package/src/components/headless/index.ts +1 -0
  46. package/src/components/headless/modals/AGHeadlessModal.ts +29 -0
  47. package/src/components/headless/modals/AGHeadlessModal.vue +13 -9
  48. package/src/components/headless/modals/AGHeadlessModalPanel.vue +10 -6
  49. package/src/components/headless/modals/AGHeadlessModalTitle.vue +14 -4
  50. package/src/components/headless/modals/index.ts +4 -6
  51. package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
  52. package/src/components/headless/snackbars/index.ts +40 -0
  53. package/src/components/index.ts +5 -1
  54. package/src/components/interfaces.ts +24 -0
  55. package/src/components/lib/AGErrorMessage.vue +16 -0
  56. package/src/components/lib/AGLink.vue +9 -0
  57. package/src/components/lib/AGMarkdown.vue +54 -0
  58. package/src/components/lib/AGMeasured.vue +16 -0
  59. package/src/components/lib/AGProgressBar.vue +30 -0
  60. package/src/components/lib/AGStartupCrash.vue +31 -0
  61. package/src/components/lib/index.ts +6 -0
  62. package/src/components/modals/AGAlertModal.ts +18 -0
  63. package/src/components/modals/AGAlertModal.vue +4 -16
  64. package/src/components/modals/AGConfirmModal.ts +41 -0
  65. package/src/components/modals/AGConfirmModal.vue +10 -14
  66. package/src/components/modals/AGErrorReportModal.ts +49 -0
  67. package/src/components/modals/AGErrorReportModal.vue +54 -0
  68. package/src/components/modals/AGErrorReportModalButtons.vue +111 -0
  69. package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
  70. package/src/components/modals/AGLoadingModal.ts +29 -0
  71. package/src/components/modals/AGLoadingModal.vue +4 -8
  72. package/src/components/modals/AGModal.ts +2 -1
  73. package/src/components/modals/AGModal.vue +16 -13
  74. package/src/components/modals/AGModalContext.vue +14 -4
  75. package/src/components/modals/AGModalTitle.vue +9 -0
  76. package/src/components/modals/AGPromptModal.ts +41 -0
  77. package/src/components/modals/AGPromptModal.vue +34 -0
  78. package/src/components/modals/index.ts +16 -7
  79. package/src/components/snackbars/AGSnackbar.vue +36 -0
  80. package/src/components/snackbars/index.ts +3 -0
  81. package/src/components/utils.ts +10 -0
  82. package/src/directives/index.ts +20 -3
  83. package/src/directives/measure.ts +40 -0
  84. package/src/errors/Errors.ts +65 -12
  85. package/src/errors/JobCancelledError.ts +3 -0
  86. package/src/errors/index.ts +26 -1
  87. package/src/errors/utils.ts +35 -0
  88. package/src/forms/Form.test.ts +28 -0
  89. package/src/forms/Form.ts +80 -14
  90. package/src/forms/index.ts +3 -1
  91. package/src/forms/utils.ts +34 -3
  92. package/src/forms/validation.ts +19 -0
  93. package/src/jobs/Job.ts +147 -0
  94. package/src/jobs/index.ts +10 -0
  95. package/src/jobs/listeners.ts +3 -0
  96. package/src/jobs/status.ts +4 -0
  97. package/src/lang/DefaultLangProvider.ts +43 -0
  98. package/src/lang/Lang.state.ts +11 -0
  99. package/src/lang/Lang.ts +44 -29
  100. package/src/main.histoire.ts +1 -0
  101. package/src/main.ts +3 -2
  102. package/src/plugins/Plugin.ts +1 -0
  103. package/src/plugins/index.ts +19 -0
  104. package/src/services/App.state.ts +25 -4
  105. package/src/services/App.ts +43 -5
  106. package/src/services/Cache.ts +43 -0
  107. package/src/services/Events.test.ts +39 -0
  108. package/src/services/Events.ts +111 -31
  109. package/src/services/Service.ts +151 -41
  110. package/src/services/Storage.ts +20 -0
  111. package/src/services/index.ts +16 -5
  112. package/src/services/store.ts +8 -5
  113. package/src/services/utils.ts +18 -0
  114. package/src/testing/index.ts +25 -0
  115. package/src/testing/setup.ts +27 -0
  116. package/src/ui/UI.state.ts +17 -1
  117. package/src/ui/UI.ts +267 -35
  118. package/src/ui/index.ts +13 -3
  119. package/src/ui/utils.ts +16 -0
  120. package/src/utils/composition/events.ts +1 -0
  121. package/src/utils/composition/persistent.test.ts +33 -0
  122. package/src/utils/composition/persistent.ts +11 -0
  123. package/src/utils/composition/state.test.ts +47 -0
  124. package/src/utils/composition/state.ts +24 -0
  125. package/src/utils/index.ts +2 -0
  126. package/src/utils/markdown.test.ts +50 -0
  127. package/src/utils/markdown.ts +26 -2
  128. package/src/utils/tailwindcss.test.ts +26 -0
  129. package/src/utils/tailwindcss.ts +7 -0
  130. package/src/utils/vue.ts +29 -6
  131. package/tailwind.config.js +4 -0
  132. package/tsconfig.json +1 -0
  133. package/vite.config.ts +6 -2
  134. package/.eslintrc.js +0 -3
  135. package/src/components/basic/AGMarkdown.vue +0 -35
  136. package/src/components/basic/index.ts +0 -3
  137. package/src/globals.ts +0 -6
@@ -0,0 +1,25 @@
1
+ <template>
2
+ <AGMarkdown :text="text" inline />
3
+ </template>
4
+
5
+ <script setup lang="ts">
6
+ import { computed } from 'vue';
7
+
8
+ import { numberProp, requiredObjectProp } from '@/utils/vue';
9
+ import type { ErrorReport } from '@/errors';
10
+
11
+ import AGMarkdown from '../lib/AGMarkdown.vue';
12
+
13
+ const props = defineProps({
14
+ report: requiredObjectProp<ErrorReport>(),
15
+ currentReport: numberProp(),
16
+ totalReports: numberProp(),
17
+ });
18
+ const text = computed(() => {
19
+ if (!props.totalReports || props.totalReports <= 1) {
20
+ return props.report.title;
21
+ }
22
+
23
+ return `${props.report.title} (${props.currentReport}/${props.totalReports})`;
24
+ });
25
+ </script>
@@ -0,0 +1,29 @@
1
+ import { computed } from 'vue';
2
+ import type { ExtractPropTypes } from 'vue';
3
+ import type { ObjectWithout } from '@noeldemartin/utils';
4
+
5
+ import { numberProp, stringProp } from '@/utils';
6
+ import { translateWithDefault } from '@/lang';
7
+ import type { AcceptRefs } from '@/utils';
8
+
9
+ export const loadingModalProps = {
10
+ title: stringProp(),
11
+ message: stringProp(),
12
+ progress: numberProp(),
13
+ };
14
+
15
+ export type AGLoadingModalProps = AcceptRefs<
16
+ ObjectWithout<ExtractPropTypes<typeof loadingModalProps>, null | undefined>
17
+ >;
18
+
19
+ export function useLoadingModalProps(): typeof loadingModalProps {
20
+ return loadingModalProps;
21
+ }
22
+
23
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
24
+ export function useLoadingModal(props: ExtractPropTypes<typeof loadingModalProps>) {
25
+ const renderedMessage = computed(() => props.message ?? translateWithDefault('ui.loading', 'Loading...'));
26
+ const showProgress = computed(() => typeof props.progress === 'number');
27
+
28
+ return { renderedMessage, showProgress };
29
+ }
@@ -5,15 +5,11 @@
5
5
  </template>
6
6
 
7
7
  <script setup lang="ts">
8
- import { computed } from 'vue';
9
-
10
- import { stringProp } from '@/utils/vue';
11
- import { translateWithDefault } from '@/lang/utils';
12
-
13
8
  import AGModal from './AGModal.vue';
9
+ import { useLoadingModal, useLoadingModalProps } from './AGLoadingModal';
14
10
 
15
- import AGMarkdown from '../basic/AGMarkdown.vue';
11
+ import AGMarkdown from '../lib/AGMarkdown.vue';
16
12
 
17
- const props = defineProps({ message: stringProp() });
18
- const renderedMessage = computed(() => props.message ?? translateWithDefault('ui.loading', 'Loading...'));
13
+ const props = defineProps(useLoadingModalProps());
14
+ const { renderedMessage } = useLoadingModal(props);
19
15
  </script>
@@ -1,8 +1,9 @@
1
1
  import type { Ref } from 'vue';
2
2
 
3
3
  export interface IAGModal {
4
+ inline: Ref<boolean>;
4
5
  cancellable: Ref<boolean>;
5
- close(): Promise<void>;
6
+ close(result?: unknown): Promise<void>;
6
7
  }
7
8
 
8
9
  export interface IAGModalDefaultSlotProps {
@@ -1,13 +1,16 @@
1
1
  <template>
2
2
  <AGHeadlessModal
3
- ref="$headlessModal"
3
+ ref="$modal"
4
4
  v-slot="{ close }: IAGHeadlessModalDefaultSlotProps"
5
- :cancellable="cancellable"
5
+ v-bind="props"
6
6
  class="relative z-50"
7
7
  >
8
- <div class="fixed inset-0 flex items-center justify-center">
9
- <AGHeadlessModalPanel class="flex max-h-full max-w-full flex-col overflow-hidden bg-white">
10
- <div class="flex max-h-full flex-col overflow-auto p-4">
8
+ <div class="fixed inset-0 flex items-center justify-center p-8">
9
+ <AGHeadlessModalPanel class="flex max-h-full max-w-full flex-col overflow-hidden bg-white p-4">
10
+ <AGHeadlessModalTitle v-if="title" class="mb-2 text-lg font-semibold">
11
+ <AGMarkdown :text="title" inline />
12
+ </AGHeadlessModalTitle>
13
+ <div class="flex max-h-full flex-col overflow-auto" v-bind="$attrs">
11
14
  <slot :close="close" />
12
15
  </div>
13
16
  </AGHeadlessModalPanel>
@@ -16,21 +19,21 @@
16
19
  </template>
17
20
 
18
21
  <script setup lang="ts">
19
- import { computed, ref } from 'vue';
22
+ import { ref } from 'vue';
20
23
 
21
- import { booleanProp } from '@/utils';
24
+ import { useModalExpose, useModalProps } from '@/components/headless/modals/AGHeadlessModal';
22
25
  import type { IAGHeadlessModal, IAGHeadlessModalDefaultSlotProps } from '@/components/headless/modals/AGHeadlessModal';
23
26
 
24
27
  import type { IAGModal } from './AGModal';
25
28
 
26
29
  import AGHeadlessModal from '../headless/modals/AGHeadlessModal.vue';
27
30
  import AGHeadlessModalPanel from '../headless/modals/AGHeadlessModalPanel.vue';
31
+ import AGHeadlessModalTitle from '../headless/modals/AGHeadlessModalTitle.vue';
32
+ import AGMarkdown from '../lib/AGMarkdown.vue';
28
33
 
29
- const $headlessModal = ref<IAGHeadlessModal>();
34
+ const props = defineProps(useModalProps());
35
+ const $modal = ref<IAGHeadlessModal>();
30
36
 
31
- defineProps({ cancellable: booleanProp(true) });
32
- defineExpose<IAGModal>({
33
- close: async () => $headlessModal.value?.close(),
34
- cancellable: computed(() => !!$headlessModal.value?.cancellable),
35
- });
37
+ defineOptions({ inheritAttrs: false });
38
+ defineExpose<IAGModal>(useModalExpose($modal));
36
39
  </script>
@@ -1,18 +1,28 @@
1
1
  <template>
2
- <component :is="modal.component" v-bind="modal.properties" />
2
+ <component :is="modal.component" v-bind="modalProperties" />
3
3
  </template>
4
4
 
5
5
  <script setup lang="ts">
6
- import { provide, toRef } from 'vue';
6
+ import { computed, provide, toRef, unref } from 'vue';
7
7
 
8
- import { requiredNumberProp, requiredObjectProp } from '@/utils/vue';
8
+ import { numberProp, requiredObjectProp } from '@/utils/vue';
9
9
  import type { Modal } from '@/ui/UI.state';
10
10
 
11
11
  import type { IAGModalContext } from './AGModalContext';
12
12
 
13
13
  const props = defineProps({
14
14
  modal: requiredObjectProp<Modal>(),
15
- childIndex: requiredNumberProp(),
15
+ childIndex: numberProp(0),
16
+ });
17
+
18
+ const modalProperties = computed(() => {
19
+ const properties = {} as typeof props.modal.properties;
20
+
21
+ for (const property in props.modal.properties) {
22
+ properties[property] = unref(props.modal.properties[property]);
23
+ }
24
+
25
+ return properties;
16
26
  });
17
27
 
18
28
  provide<IAGModalContext>('modal', {
@@ -0,0 +1,9 @@
1
+ <template>
2
+ <AGHeadlessModalTitle class="mb-2 font-semibold">
3
+ <slot />
4
+ </AGHeadlessModalTitle>
5
+ </template>
6
+
7
+ <script setup lang="ts">
8
+ import AGHeadlessModalTitle from '../headless/modals/AGHeadlessModalTitle.vue';
9
+ </script>
@@ -0,0 +1,41 @@
1
+ import { computed } from 'vue';
2
+ import type { ExtractPropTypes } from 'vue';
3
+ import type { ObjectWithout, Pretty, SubPartial } from '@noeldemartin/utils';
4
+
5
+ import { Colors } from '@/components/constants';
6
+ import { enumProp, requiredStringProp, stringProp } from '@/utils';
7
+ import { translateWithDefault } from '@/lang';
8
+ import type { AcceptRefs } from '@/utils';
9
+
10
+ export const promptModalProps = {
11
+ title: stringProp(),
12
+ message: requiredStringProp(),
13
+ label: stringProp(),
14
+ defaultValue: stringProp(),
15
+ placeholder: stringProp(),
16
+ acceptText: stringProp(),
17
+ acceptColor: enumProp(Colors, Colors.Primary),
18
+ cancelText: stringProp(),
19
+ cancelColor: enumProp(Colors, Colors.Clear),
20
+ };
21
+
22
+ export type AGPromptModalProps = Pretty<
23
+ AcceptRefs<
24
+ SubPartial<
25
+ ObjectWithout<ExtractPropTypes<typeof promptModalProps>, null | undefined>,
26
+ 'acceptColor' | 'cancelColor'
27
+ >
28
+ >
29
+ >;
30
+
31
+ export function usePromptModalProps(): typeof promptModalProps {
32
+ return promptModalProps;
33
+ }
34
+
35
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
36
+ export function usePromptModal(props: ExtractPropTypes<typeof promptModalProps>) {
37
+ const renderedAcceptText = computed(() => props.acceptText ?? translateWithDefault('ui.accept', 'Ok'));
38
+ const renderedCancelText = computed(() => props.cancelText ?? translateWithDefault('ui.cancel', 'Cancel'));
39
+
40
+ return { renderedAcceptText, renderedCancelText };
41
+ }
@@ -0,0 +1,34 @@
1
+ <template>
2
+ <AGModal v-slot="{ close }: IAGModalDefaultSlotProps" :cancellable="false" :title="title">
3
+ <AGMarkdown :text="message" />
4
+
5
+ <AGForm :form="form" @submit="close(form.draft)">
6
+ <AGInput name="draft" :placeholder="placeholder" :label="label" />
7
+
8
+ <div class="mt-2 flex flex-row-reverse gap-2">
9
+ <AGButton :color="acceptColor" submit>
10
+ {{ renderedAcceptText }}
11
+ </AGButton>
12
+ <AGButton :color="cancelColor" @click="close()">
13
+ {{ renderedCancelText }}
14
+ </AGButton>
15
+ </div>
16
+ </AGForm>
17
+ </AGModal>
18
+ </template>
19
+
20
+ <script setup lang="ts">
21
+ import AGModal from './AGModal.vue';
22
+ import { usePromptModal, usePromptModalProps } from './AGPromptModal';
23
+ import type { IAGModalDefaultSlotProps } from './AGModal';
24
+
25
+ import AGButton from '../forms/AGButton.vue';
26
+ import AGForm from '../forms/AGForm.vue';
27
+ import AGInput from '../forms/AGInput.vue';
28
+ import AGMarkdown from '../lib/AGMarkdown.vue';
29
+ import { requiredStringInput, useForm } from '../../forms';
30
+
31
+ const props = defineProps(usePromptModalProps());
32
+ const form = useForm({ draft: requiredStringInput(props.defaultValue ?? '') });
33
+ const { renderedAcceptText, renderedCancelText } = usePromptModal(props);
34
+ </script>
@@ -1,8 +1,17 @@
1
- import AGAlertModal from './AGAlertModal.vue';
2
- import AGConfirmModal from './AGConfirmModal.vue';
3
- import AGLoadingModal from './AGLoadingModal.vue';
4
- import AGModal from './AGModal.vue';
5
- import AGModalContext from './AGModalContext.vue';
6
- import { IAGModal } from './AGModal';
1
+ export * from './AGAlertModal';
2
+ export * from './AGConfirmModal';
3
+ export * from './AGErrorReportModal';
4
+ export * from './AGLoadingModal';
5
+ export * from './AGModal';
6
+ export * from './AGModalContext';
7
+ export * from './AGPromptModal';
7
8
 
8
- export { AGAlertModal, AGConfirmModal, AGModal, AGModalContext, AGLoadingModal, IAGModal };
9
+ export { default as AGAlertModal } from './AGAlertModal.vue';
10
+ export { default as AGConfirmModal } from './AGConfirmModal.vue';
11
+ export { default as AGErrorReportModalButtons } from './AGErrorReportModalButtons.vue';
12
+ export { default as AGErrorReportModalTitle } from './AGErrorReportModalTitle.vue';
13
+ export { default as AGLoadingModal } from './AGLoadingModal.vue';
14
+ export { default as AGModal } from './AGModal.vue';
15
+ export { default as AGModalContext } from './AGModalContext.vue';
16
+ export { default as AGModalTitle } from './AGModalTitle.vue';
17
+ export { default as AGPromptModal } from './AGPromptModal.vue';
@@ -0,0 +1,36 @@
1
+ <template>
2
+ <AGHeadlessSnackbar class="flex flex-row items-center justify-center gap-3 p-4" :class="styleClasses">
3
+ <AGMarkdown :text="message" inline />
4
+ <AGButton
5
+ v-for="(action, i) of actions"
6
+ :key="i"
7
+ :color="color"
8
+ @click="activate(action)"
9
+ >
10
+ {{ action.text }}
11
+ </AGButton>
12
+ </AGHeadlessSnackbar>
13
+ </template>
14
+
15
+ <script setup lang="ts">
16
+ import { computed } from 'vue';
17
+
18
+ import { Colors } from '@/components/constants';
19
+ import { useSnackbar, useSnackbarProps } from '@/components/headless/snackbars';
20
+
21
+ import AGButton from '../forms/AGButton.vue';
22
+ import AGHeadlessSnackbar from '../headless/snackbars/AGHeadlessSnackbar.vue';
23
+ import AGMarkdown from '../lib/AGMarkdown.vue';
24
+
25
+ const props = defineProps(useSnackbarProps());
26
+ const { activate } = useSnackbar(props);
27
+ const styleClasses = computed(() => {
28
+ switch (props.color) {
29
+ case Colors.Danger:
30
+ return 'bg-red-200 text-red-900';
31
+ default:
32
+ case Colors.Secondary:
33
+ return 'bg-gray-900 text-white';
34
+ }
35
+ });
36
+ </script>
@@ -0,0 +1,3 @@
1
+ import AGSnackbar from './AGSnackbar.vue';
2
+
3
+ export { AGSnackbar };
@@ -0,0 +1,10 @@
1
+ export function extractComponentProps<T extends Record<string, unknown>>(
2
+ values: Record<string, unknown>,
3
+ definitions: Record<string, unknown>,
4
+ ): T {
5
+ return Object.keys(definitions).reduce((extracted, prop) => {
6
+ extracted[prop] = values[prop];
7
+
8
+ return extracted;
9
+ }, {} as Record<string, unknown>) as T;
10
+ }
@@ -3,13 +3,30 @@ import type { Directive } from 'vue';
3
3
  import { definePlugin } from '@/plugins';
4
4
 
5
5
  import initialFocus from './initial-focus';
6
+ import measure from './measure';
6
7
 
7
- const directives: Record<string, Directive> = {
8
+ const builtInDirectives: Record<string, Directive> = {
8
9
  'initial-focus': initialFocus,
10
+ 'measure': measure,
9
11
  };
10
12
 
13
+ export * from './measure';
14
+
11
15
  export default definePlugin({
12
- install(app) {
13
- Object.entries(directives).forEach(([name, directive]) => app.directive(name, directive));
16
+ install(app, options) {
17
+ const directives = {
18
+ ...builtInDirectives,
19
+ ...options.directives,
20
+ };
21
+
22
+ for (const [name, directive] of Object.entries(directives)) {
23
+ app.directive(name, directive);
24
+ }
14
25
  },
15
26
  });
27
+
28
+ declare module '@/bootstrap/options' {
29
+ export interface AerogelOptions {
30
+ directives?: Record<string, Directive>;
31
+ }
32
+ }
@@ -0,0 +1,40 @@
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;
12
+
13
+ export default defineDirective({
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();
21
+
22
+ if (modifiers.css) {
23
+ element.style.setProperty('--width', `${sizes.width}px`);
24
+ element.style.setProperty('--height', `${sizes.height}px`);
25
+ }
26
+
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);
39
+ },
40
+ });
@@ -1,12 +1,16 @@
1
- import { JSError, facade, isObject } from '@noeldemartin/utils';
1
+ import { JSError, facade, isObject, objectWithoutEmpty, toString } from '@noeldemartin/utils';
2
2
 
3
3
  import App from '@/services/App';
4
4
  import ServiceBootError from '@/errors/ServiceBootError';
5
- import UI from '@/ui/UI';
6
- import { translate } from '@/lang/utils';
5
+ import UI, { UIComponents } from '@/ui/UI';
6
+ import { translateWithDefault } from '@/lang/utils';
7
7
 
8
8
  import Service from './Errors.state';
9
+ import { Colors } from '@/components/constants';
10
+ import { Events } from '@/services';
11
+ import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
9
12
  import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
13
+ import type { ModalComponent } from '@/ui/UI.state';
10
14
 
11
15
  export class ErrorsService extends Service {
12
16
 
@@ -22,14 +26,27 @@ export class ErrorsService extends Service {
22
26
  }
23
27
 
24
28
  public async inspect(error: ErrorSource | ErrorReport[]): Promise<void> {
25
- const reports = Array.isArray(error) ? error : [await this.createErrorReport(error)];
29
+ const reports = Array.isArray(error) ? (error as ErrorReport[]) : [await this.createErrorReport(error)];
26
30
 
27
- // TODO open errors modal
28
- reports;
31
+ if (reports.length === 0) {
32
+ UI.alert(translateWithDefault('errors.inspectEmpty', 'Nothing to inspect!'));
33
+
34
+ return;
35
+ }
36
+
37
+ UI.openModal<ModalComponent<AGErrorReportModalProps>>(UI.requireComponent(UIComponents.ErrorReportModal), {
38
+ reports,
39
+ });
29
40
  }
30
41
 
31
42
  public async report(error: ErrorSource, message?: string): Promise<void> {
32
- if (App.isDevelopment || App.isTesting) {
43
+ await Events.emit('error', { error, message });
44
+
45
+ if (App.testing) {
46
+ throw error;
47
+ }
48
+
49
+ if (App.development) {
33
50
  this.logError(error);
34
51
  }
35
52
 
@@ -37,7 +54,7 @@ export class ErrorsService extends Service {
37
54
  throw error;
38
55
  }
39
56
 
40
- if (!App.isMounted) {
57
+ if (!App.isMounted()) {
41
58
  const startupError = await this.createStartupErrorReport(error);
42
59
 
43
60
  if (startupError) {
@@ -54,8 +71,24 @@ export class ErrorsService extends Service {
54
71
  date: new Date(),
55
72
  };
56
73
 
57
- // TODO open error snackbar
58
- UI.alert(message ?? 'Something went wrong, but it\'s not your fault! (look at the console for details)');
74
+ UI.showSnackbar(
75
+ message ??
76
+ translateWithDefault('errors.notice', 'Something went wrong, but it\'s not your fault. Try again!'),
77
+ {
78
+ color: Colors.Danger,
79
+ actions: [
80
+ {
81
+ text: translateWithDefault('errors.viewDetails', 'View details'),
82
+ dismiss: true,
83
+ handler: () =>
84
+ UI.openModal<ModalComponent<AGErrorReportModalProps>>(
85
+ UI.requireComponent(UIComponents.ErrorReportModal),
86
+ { reports: [report] },
87
+ ),
88
+ },
89
+ ],
90
+ },
91
+ );
59
92
 
60
93
  this.setState({ logs: [log].concat(this.logs) });
61
94
  }
@@ -102,8 +135,22 @@ export class ErrorsService extends Service {
102
135
  return this.createErrorReportFromError(error);
103
136
  }
104
137
 
138
+ if (isObject(error)) {
139
+ return objectWithoutEmpty({
140
+ title: toString(
141
+ error['name'] ?? error['title'] ?? translateWithDefault('errors.unknown', 'Unknown Error'),
142
+ ),
143
+ description: toString(
144
+ error['message'] ??
145
+ error['description'] ??
146
+ translateWithDefault('errors.unknownDescription', 'Unknown error object'),
147
+ ),
148
+ error,
149
+ });
150
+ }
151
+
105
152
  return {
106
- title: translate('errors.unknown'),
153
+ title: translateWithDefault('errors.unknown', 'Unknown Error'),
107
154
  error,
108
155
  };
109
156
  }
@@ -129,4 +176,10 @@ export class ErrorsService extends Service {
129
176
 
130
177
  }
131
178
 
132
- export default facade(new ErrorsService());
179
+ export default facade(ErrorsService);
180
+
181
+ declare module '@/services/Events' {
182
+ export interface EventsPayload {
183
+ error: { error: ErrorSource; message?: string };
184
+ }
185
+ }
@@ -0,0 +1,3 @@
1
+ import { JSError } from '@noeldemartin/utils';
2
+
3
+ export default class JobCancelledError extends JSError {}
@@ -1,21 +1,46 @@
1
+ import type { App } from 'vue';
2
+
1
3
  import { bootServices } from '@/services';
2
4
  import { definePlugin } from '@/plugins';
3
5
 
4
6
  import Errors from './Errors';
5
7
  import { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
6
8
 
9
+ export * from './utils';
7
10
  export { Errors, ErrorSource, ErrorReport, ErrorReportLog };
8
11
 
9
12
  const services = { $errors: Errors };
13
+ const frameworkHandler: ErrorHandler = (error) => {
14
+ Errors.report(error);
15
+
16
+ return true;
17
+ };
18
+
19
+ function setUpErrorHandler(app: App, baseHandler: ErrorHandler = () => false): void {
20
+ const errorHandler: ErrorHandler = (error) => baseHandler(error) || frameworkHandler(error);
21
+
22
+ app.config.errorHandler = errorHandler;
23
+ globalThis.onerror = (event, _, __, ___, error) => errorHandler(error ?? event);
24
+ globalThis.onunhandledrejection = (event) => errorHandler(event.reason);
25
+ }
10
26
 
27
+ export type ErrorHandler = (error: ErrorSource) => boolean;
11
28
  export type ErrorsServices = typeof services;
12
29
 
13
30
  export default definePlugin({
14
- async install(app) {
31
+ async install(app, options) {
32
+ setUpErrorHandler(app, options.handleError);
33
+
15
34
  await bootServices(app, services);
16
35
  },
17
36
  });
18
37
 
38
+ declare module '@/bootstrap/options' {
39
+ export interface AerogelOptions {
40
+ handleError?(error: ErrorSource): boolean;
41
+ }
42
+ }
43
+
19
44
  declare module '@/services' {
20
45
  export interface Services extends ErrorsServices {}
21
46
  }
@@ -0,0 +1,35 @@
1
+ import { JSError, isObject, toString } from '@noeldemartin/utils';
2
+ import { translateWithDefault } from '@/lang/utils';
3
+ import type { ErrorSource } from './Errors.state';
4
+
5
+ const handlers: ErrorHandler[] = [];
6
+
7
+ export type ErrorHandler = (error: ErrorSource) => string | undefined;
8
+
9
+ export function registerErrorHandler(handler: ErrorHandler): void {
10
+ handlers.push(handler);
11
+ }
12
+
13
+ export function getErrorMessage(error: ErrorSource): string {
14
+ for (const handler of handlers) {
15
+ const result = handler(error);
16
+
17
+ if (result) {
18
+ return result;
19
+ }
20
+ }
21
+
22
+ if (typeof error === 'string') {
23
+ return error;
24
+ }
25
+
26
+ if (error instanceof Error || error instanceof JSError) {
27
+ return error.message;
28
+ }
29
+
30
+ if (isObject(error)) {
31
+ return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
32
+ }
33
+
34
+ return translateWithDefault('errors.unknown', 'Unknown Error');
35
+ }
@@ -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
  });