@geektr.cloud/acrux-ui 0.0.0-stage → 0.1.0

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 (156) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +21 -2
  3. package/package.json +70 -3
  4. package/src/components/acrux-ui/actions/ConfirmPopover.vue +107 -0
  5. package/src/components/acrux-ui/actions/FormShell.vue +96 -0
  6. package/src/components/acrux-ui/actions/RemovalButton.vue +65 -0
  7. package/src/components/acrux-ui/actions/Stepper.vue +116 -0
  8. package/src/components/acrux-ui/actions/form.ts +36 -0
  9. package/src/components/acrux-ui/actions/index.ts +70 -0
  10. package/src/components/acrux-ui/actions/stepper.ts +65 -0
  11. package/src/components/acrux-ui/actions/utils.ts +138 -0
  12. package/src/components/acrux-ui/base/CopyBtn.vue +85 -0
  13. package/src/components/acrux-ui/base/EmptyValue.vue +25 -0
  14. package/src/components/acrux-ui/base/Icon.vue +44 -0
  15. package/src/components/acrux-ui/base/IconBtn.vue +51 -0
  16. package/src/components/acrux-ui/base/Text.vue +85 -0
  17. package/src/components/acrux-ui/base/VSeparator.vue +21 -0
  18. package/src/components/acrux-ui/base/async.ts +35 -0
  19. package/src/components/acrux-ui/base/index.ts +7 -0
  20. package/src/components/acrux-ui/display/AsyncError.vue +46 -0
  21. package/src/components/acrux-ui/display/CodeBlock.vue +91 -0
  22. package/src/components/acrux-ui/display/CopyTag.vue +51 -0
  23. package/src/components/acrux-ui/display/DataView.ts +47 -0
  24. package/src/components/acrux-ui/display/DateFormatter.vue +61 -0
  25. package/src/components/acrux-ui/display/Disclosure.vue +45 -0
  26. package/src/components/acrux-ui/display/Link.vue +81 -0
  27. package/src/components/acrux-ui/display/MultiLine.vue +29 -0
  28. package/src/components/acrux-ui/display/Notice.vue +71 -0
  29. package/src/components/acrux-ui/display/Pem.vue +165 -0
  30. package/src/components/acrux-ui/display/QrBtn.vue +117 -0
  31. package/src/components/acrux-ui/display/QrModal.vue +76 -0
  32. package/src/components/acrux-ui/display/Route.vue +52 -0
  33. package/src/components/acrux-ui/display/Token.vue +106 -0
  34. package/src/components/acrux-ui/display/UUID.vue +46 -0
  35. package/src/components/acrux-ui/display/index.ts +17 -0
  36. package/src/components/acrux-ui/display/pem.ts +100 -0
  37. package/src/components/acrux-ui/display/qr.ts +20 -0
  38. package/src/components/acrux-ui/fields/EntitySelect.vue +253 -0
  39. package/src/components/acrux-ui/fields/FieldError.vue +11 -0
  40. package/src/components/acrux-ui/fields/JsonTextArea.vue +105 -0
  41. package/src/components/acrux-ui/fields/MultiLineInput.vue +145 -0
  42. package/src/components/acrux-ui/fields/Select.vue +106 -0
  43. package/src/components/acrux-ui/fields/fieldAccessibility.ts +38 -0
  44. package/src/components/acrux-ui/fields/index.ts +4 -0
  45. package/src/components/acrux-ui/page/BackButton.vue +70 -0
  46. package/src/components/acrux-ui/page/PageDetail.vue +125 -0
  47. package/src/components/acrux-ui/page/PageEntry.vue +130 -0
  48. package/src/components/acrux-ui/page/PageState.vue +108 -0
  49. package/src/components/acrux-ui/page/index.ts +3 -0
  50. package/src/components/acrux-ui/page/pageState.ts +18 -0
  51. package/src/components/ui/alert/Alert.vue +17 -0
  52. package/src/components/ui/alert/AlertAction.vue +17 -0
  53. package/src/components/ui/alert/AlertDescription.vue +22 -0
  54. package/src/components/ui/alert/AlertTitle.vue +22 -0
  55. package/src/components/ui/alert/index.ts +25 -0
  56. package/src/components/ui/badge/Badge.vue +29 -0
  57. package/src/components/ui/badge/index.ts +25 -0
  58. package/src/components/ui/button/Button.vue +31 -0
  59. package/src/components/ui/button/index.ts +39 -0
  60. package/src/components/ui/card/Card.vue +29 -0
  61. package/src/components/ui/card/CardAction.vue +17 -0
  62. package/src/components/ui/card/CardContent.vue +14 -0
  63. package/src/components/ui/card/CardDescription.vue +14 -0
  64. package/src/components/ui/card/CardFooter.vue +17 -0
  65. package/src/components/ui/card/CardHeader.vue +22 -0
  66. package/src/components/ui/card/CardTitle.vue +14 -0
  67. package/src/components/ui/card/index.ts +7 -0
  68. package/src/components/ui/collapsible/Collapsible.vue +15 -0
  69. package/src/components/ui/collapsible/CollapsibleContent.vue +12 -0
  70. package/src/components/ui/collapsible/CollapsibleTrigger.vue +12 -0
  71. package/src/components/ui/collapsible/index.ts +3 -0
  72. package/src/components/ui/combobox/Combobox.vue +15 -0
  73. package/src/components/ui/combobox/ComboboxAnchor.vue +19 -0
  74. package/src/components/ui/combobox/ComboboxEmpty.vue +26 -0
  75. package/src/components/ui/combobox/ComboboxInput.vue +40 -0
  76. package/src/components/ui/combobox/ComboboxItem.vue +29 -0
  77. package/src/components/ui/combobox/ComboboxList.vue +38 -0
  78. package/src/components/ui/combobox/ComboboxViewport.vue +28 -0
  79. package/src/components/ui/combobox/index.ts +9 -0
  80. package/src/components/ui/empty/Empty.vue +22 -0
  81. package/src/components/ui/empty/EmptyContent.vue +17 -0
  82. package/src/components/ui/empty/EmptyDescription.vue +22 -0
  83. package/src/components/ui/empty/EmptyHeader.vue +14 -0
  84. package/src/components/ui/empty/EmptyMedia.vue +22 -0
  85. package/src/components/ui/empty/EmptyTitle.vue +14 -0
  86. package/src/components/ui/empty/index.ts +26 -0
  87. package/src/components/ui/field/Field.vue +27 -0
  88. package/src/components/ui/field/FieldContent.vue +17 -0
  89. package/src/components/ui/field/FieldDescription.vue +24 -0
  90. package/src/components/ui/field/FieldError.vue +50 -0
  91. package/src/components/ui/field/FieldGroup.vue +22 -0
  92. package/src/components/ui/field/FieldLabel.vue +24 -0
  93. package/src/components/ui/field/FieldLegend.vue +24 -0
  94. package/src/components/ui/field/FieldSeparator.vue +26 -0
  95. package/src/components/ui/field/FieldSet.vue +22 -0
  96. package/src/components/ui/field/FieldTitle.vue +22 -0
  97. package/src/components/ui/field/index.ts +30 -0
  98. package/src/components/ui/input/Input.vue +33 -0
  99. package/src/components/ui/input/index.ts +1 -0
  100. package/src/components/ui/input-group/InputGroup.vue +23 -0
  101. package/src/components/ui/input-group/InputGroupAddon.vue +39 -0
  102. package/src/components/ui/input-group/InputGroupButton.vue +22 -0
  103. package/src/components/ui/input-group/InputGroupInput.vue +21 -0
  104. package/src/components/ui/input-group/InputGroupText.vue +21 -0
  105. package/src/components/ui/input-group/InputGroupTextarea.vue +21 -0
  106. package/src/components/ui/input-group/index.ts +53 -0
  107. package/src/components/ui/label/Label.vue +26 -0
  108. package/src/components/ui/label/index.ts +1 -0
  109. package/src/components/ui/popover/Popover.vue +15 -0
  110. package/src/components/ui/popover/PopoverAnchor.vue +12 -0
  111. package/src/components/ui/popover/PopoverContent.vue +38 -0
  112. package/src/components/ui/popover/PopoverDescription.vue +14 -0
  113. package/src/components/ui/popover/PopoverHeader.vue +14 -0
  114. package/src/components/ui/popover/PopoverTitle.vue +14 -0
  115. package/src/components/ui/popover/PopoverTrigger.vue +12 -0
  116. package/src/components/ui/popover/index.ts +7 -0
  117. package/src/components/ui/select/Select.vue +15 -0
  118. package/src/components/ui/select/SelectContent.vue +53 -0
  119. package/src/components/ui/select/SelectGroup.vue +17 -0
  120. package/src/components/ui/select/SelectItem.vue +40 -0
  121. package/src/components/ui/select/SelectItemText.vue +12 -0
  122. package/src/components/ui/select/SelectLabel.vue +14 -0
  123. package/src/components/ui/select/SelectScrollDownButton.vue +32 -0
  124. package/src/components/ui/select/SelectScrollUpButton.vue +32 -0
  125. package/src/components/ui/select/SelectSeparator.vue +19 -0
  126. package/src/components/ui/select/SelectTrigger.vue +36 -0
  127. package/src/components/ui/select/SelectValue.vue +12 -0
  128. package/src/components/ui/select/index.ts +11 -0
  129. package/src/components/ui/separator/Separator.vue +27 -0
  130. package/src/components/ui/separator/index.ts +1 -0
  131. package/src/components/ui/skeleton/Skeleton.vue +14 -0
  132. package/src/components/ui/skeleton/index.ts +1 -0
  133. package/src/components/ui/spinner/Spinner.vue +14 -0
  134. package/src/components/ui/spinner/index.ts +1 -0
  135. package/src/components/ui/switch/Switch.vue +47 -0
  136. package/src/components/ui/switch/index.ts +1 -0
  137. package/src/components/ui/table/Table.vue +24 -0
  138. package/src/components/ui/table/TableBody.vue +14 -0
  139. package/src/components/ui/table/TableCaption.vue +14 -0
  140. package/src/components/ui/table/TableCell.vue +17 -0
  141. package/src/components/ui/table/TableEmpty.vue +32 -0
  142. package/src/components/ui/table/TableFooter.vue +14 -0
  143. package/src/components/ui/table/TableHead.vue +29 -0
  144. package/src/components/ui/table/TableHeader.vue +14 -0
  145. package/src/components/ui/table/TableRow.vue +22 -0
  146. package/src/components/ui/table/index.ts +9 -0
  147. package/src/components/ui/tags-input/TagsInput.vue +31 -0
  148. package/src/components/ui/tags-input/TagsInputInput.vue +20 -0
  149. package/src/components/ui/tags-input/TagsInputItem.vue +28 -0
  150. package/src/components/ui/tags-input/TagsInputItemDelete.vue +23 -0
  151. package/src/components/ui/tags-input/TagsInputItemText.vue +17 -0
  152. package/src/components/ui/tags-input/index.ts +5 -0
  153. package/src/components/ui/textarea/Textarea.vue +33 -0
  154. package/src/components/ui/textarea/index.ts +1 -0
  155. package/src/index.ts +5 -0
  156. package/src/testing/renderer.ts +99 -0
@@ -0,0 +1,47 @@
1
+ import type { HTMLAttributes } from "vue";
2
+ import { defineComponent, h } from "vue";
3
+ import { cn } from "cn";
4
+
5
+ export const DataView = defineComponent({
6
+ name: "DataView",
7
+ inheritAttrs: false,
8
+ setup(_, { slots, attrs }) {
9
+ return () => {
10
+ const { class: className, ...rest } = attrs;
11
+ return h(
12
+ "dl",
13
+ {
14
+ ...rest,
15
+ "data-slot": "data-view",
16
+ class: cn("divide-border divide-y text-xs", className as HTMLAttributes["class"]),
17
+ },
18
+ slots.default?.(),
19
+ );
20
+ };
21
+ },
22
+ });
23
+
24
+ export const DataItem = defineComponent({
25
+ name: "DataItem",
26
+ inheritAttrs: false,
27
+ props: {
28
+ label: { type: String, default: "" },
29
+ },
30
+ setup(props, { slots, attrs }) {
31
+ return () => {
32
+ const { class: className, ...rest } = attrs;
33
+ return h(
34
+ "div",
35
+ {
36
+ ...rest,
37
+ "data-slot": "data-item",
38
+ class: cn("flex gap-2 py-2 leading-6", className as HTMLAttributes["class"]),
39
+ },
40
+ [
41
+ h("dt", { class: "text-muted-foreground w-24 shrink-0 self-start" }, slots.label?.() ?? props.label),
42
+ h("dd", { class: "text-foreground flex min-w-0 flex-1 items-center gap-2" }, slots.default?.()),
43
+ ],
44
+ );
45
+ };
46
+ },
47
+ });
@@ -0,0 +1,61 @@
1
+ <script setup lang="ts">
2
+ import type { HTMLAttributes } from "vue";
3
+ import type { Locale } from "date-fns";
4
+ import { computed } from "vue";
5
+ import { format as formatDate, formatDistanceToNow } from "date-fns";
6
+ import { zhCN } from "date-fns/locale/zh-CN";
7
+ import { cn } from "cn";
8
+ import { EmptyValue, VSeparator } from "../base";
9
+
10
+ defineOptions({ name: "DateFormatter", inheritAttrs: false });
11
+
12
+ const props = withDefaults(
13
+ defineProps<{
14
+ value: Date | string | number;
15
+ format?: string;
16
+ additionDistance?: boolean;
17
+ locale?: Locale;
18
+ class?: HTMLAttributes["class"];
19
+ }>(),
20
+ {
21
+ format: "datetime",
22
+ additionDistance: false,
23
+ locale: () => zhCN,
24
+ class: undefined,
25
+ },
26
+ );
27
+
28
+ const dateFormatPresets: Record<string, string> = {
29
+ date: "yyyy-MM-dd",
30
+ datetime: "yyyy-MM-dd HH:mm:ss",
31
+ time: "HH:mm:ss",
32
+ timestamp: "T",
33
+ };
34
+
35
+ const date = computed(() => (props.value instanceof Date ? props.value : new Date(props.value)));
36
+ const invalid = computed(() => Number.isNaN(date.value.getTime()));
37
+
38
+ const distance = computed(() =>
39
+ invalid.value ? "" : formatDistanceToNow(date.value, { addSuffix: true, locale: props.locale }),
40
+ );
41
+
42
+ const dateString = computed(() => {
43
+ if (invalid.value) return "";
44
+ if (props.format === "distance") return distance.value;
45
+ return formatDate(date.value, dateFormatPresets[props.format] ?? props.format, { locale: props.locale });
46
+ });
47
+ </script>
48
+
49
+ <template>
50
+ <span v-bind="$attrs" data-slot="date-formatter" :class="cn('inline-flex items-center gap-1', props.class)">
51
+ <EmptyValue v-if="invalid" />
52
+ <span v-else-if="format === 'distance'">{{ distance }}</span>
53
+ <template v-else>
54
+ <span>{{ dateString }}</span>
55
+ <template v-if="additionDistance">
56
+ <VSeparator />
57
+ <span class="text-muted-foreground">{{ distance }}</span>
58
+ </template>
59
+ </template>
60
+ </span>
61
+ </template>
@@ -0,0 +1,45 @@
1
+ <!-- eslint-disable vue/multi-word-component-names -->
2
+ <script setup lang="ts">
3
+ import type { HTMLAttributes } from "vue";
4
+ import { cn } from "cn";
5
+ import { ChevronDown } from "@lucide/vue";
6
+ import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../../ui/collapsible";
7
+ import { Icon } from "../base";
8
+
9
+ defineOptions({ name: "Disclosure", inheritAttrs: false });
10
+
11
+ const props = withDefaults(
12
+ defineProps<{
13
+ summary: string;
14
+ class?: HTMLAttributes["class"];
15
+ }>(),
16
+ {
17
+ class: undefined,
18
+ },
19
+ );
20
+
21
+ const open = defineModel<boolean>("open", { default: false });
22
+ </script>
23
+
24
+ <template>
25
+ <Collapsible
26
+ v-model:open="open"
27
+ data-slot="disclosure"
28
+ v-bind="$attrs"
29
+ :class="cn('rounded-none border border-border text-xs', props.class)"
30
+ >
31
+ <CollapsibleTrigger
32
+ class="flex min-h-7 w-full items-center gap-1 px-2 py-1 text-left outline-none focus-visible:ring-2 focus-visible:ring-ring/50 focus-visible:ring-inset"
33
+ >
34
+ <Icon
35
+ :icon="ChevronDown"
36
+ size="xs"
37
+ :class="cn('text-muted-foreground motion-safe:transition-transform', open && '-rotate-180')"
38
+ />
39
+ <span class="min-w-0 flex-1">{{ summary }}</span>
40
+ </CollapsibleTrigger>
41
+ <CollapsibleContent class="border-t border-border px-2 py-2">
42
+ <slot />
43
+ </CollapsibleContent>
44
+ </Collapsible>
45
+ </template>
@@ -0,0 +1,81 @@
1
+ <!-- eslint-disable vue/multi-word-component-names, vue/no-reserved-component-names -->
2
+ <script setup lang="ts">
3
+ import type { HTMLAttributes } from "vue";
4
+ import { computed } from "vue";
5
+ import { cn } from "cn";
6
+ import { Badge, type BadgeVariants } from "../../ui/badge";
7
+ import { CopyBtn, EmptyValue, Icon, Text, VSeparator } from "../base";
8
+ import { ExternalLink } from "@lucide/vue";
9
+
10
+ defineOptions({ name: "Link", inheritAttrs: false });
11
+
12
+ const props = withDefaults(
13
+ defineProps<{
14
+ label?: string;
15
+ href?: string | null;
16
+ variant?: BadgeVariants["variant"] | "raw";
17
+ copyLabel?: string;
18
+ copiedText?: string;
19
+ copyFailedText?: string;
20
+ newWindowHint?: string;
21
+ class?: HTMLAttributes["class"];
22
+ }>(),
23
+ {
24
+ label: undefined,
25
+ href: undefined,
26
+ variant: "raw",
27
+ copyLabel: "复制链接",
28
+ copiedText: "已复制",
29
+ copyFailedText: "复制失败",
30
+ newWindowHint: "(在新窗口打开)",
31
+ class: undefined,
32
+ },
33
+ );
34
+
35
+ const isRaw = computed(() => props.variant === "raw");
36
+ const badgeVariant = computed(() => (props.variant === "raw" ? undefined : props.variant));
37
+ const safeHref = computed(() => {
38
+ if (!props.href) return undefined;
39
+ try {
40
+ const url = new URL(props.href, "https://acrux.invalid");
41
+ return url.protocol === "http:" || url.protocol === "https:" ? props.href : undefined;
42
+ } catch {
43
+ return undefined;
44
+ }
45
+ });
46
+ </script>
47
+
48
+ <template>
49
+ <span v-bind="$attrs" data-slot="link" :class="cn('inline-flex min-w-0 items-center gap-1', props.class)">
50
+ <template v-if="href">
51
+ <Badge v-if="!isRaw" as="span" :variant="badgeVariant" class="min-w-0">
52
+ <component
53
+ :is="safeHref ? 'a' : 'span'"
54
+ :href="safeHref"
55
+ :target="safeHref ? '_blank' : undefined"
56
+ :rel="safeHref ? 'noopener noreferrer' : undefined"
57
+ class="inline-flex min-w-0 items-center gap-1"
58
+ >
59
+ <Icon v-if="safeHref" :icon="ExternalLink" />
60
+ <Text variant="inlineCode" class="truncate">{{ label ?? href }}</Text>
61
+ <span v-if="safeHref" class="sr-only">{{ newWindowHint }}</span>
62
+ </component>
63
+ </Badge>
64
+ <component
65
+ :is="safeHref ? 'a' : 'span'"
66
+ v-else
67
+ :href="safeHref"
68
+ :target="safeHref ? '_blank' : undefined"
69
+ :rel="safeHref ? 'noopener noreferrer' : undefined"
70
+ class="inline-flex min-w-0 items-center gap-1 underline underline-offset-2"
71
+ >
72
+ <Icon v-if="safeHref" :icon="ExternalLink" />
73
+ <Text variant="inlineCode" class="truncate">{{ label ?? href }}</Text>
74
+ <span v-if="safeHref" class="sr-only">{{ newWindowHint }}</span>
75
+ </component>
76
+ <VSeparator />
77
+ <CopyBtn :value="href" :label="copyLabel" :copied-text="copiedText" :failed-text="copyFailedText" />
78
+ </template>
79
+ <EmptyValue v-else />
80
+ </span>
81
+ </template>
@@ -0,0 +1,29 @@
1
+ <script setup lang="ts">
2
+ import type { HTMLAttributes } from "vue";
3
+ import { computed } from "vue";
4
+ import { cn } from "cn";
5
+ import { EmptyValue, Text } from "../base";
6
+
7
+ defineOptions({ name: "MultiLine", inheritAttrs: false });
8
+
9
+ const props = withDefaults(
10
+ defineProps<{
11
+ value?: string | null;
12
+ class?: HTMLAttributes["class"];
13
+ }>(),
14
+ {
15
+ value: undefined,
16
+ class: undefined,
17
+ },
18
+ );
19
+
20
+ // 空串或纯空白不产生空段落,直接落到统一的 — 空值。
21
+ const lines = computed(() => (props.value?.trim() ? props.value.split("\n") : []));
22
+ </script>
23
+
24
+ <template>
25
+ <div v-bind="$attrs" data-slot="multi-line" :class="cn('flex flex-col gap-1 text-xs', props.class)">
26
+ <Text v-for="(line, index) in lines" :key="index" class="break-words whitespace-pre-wrap">{{ line }}</Text>
27
+ <EmptyValue v-if="!lines.length" />
28
+ </div>
29
+ </template>
@@ -0,0 +1,71 @@
1
+ <!-- eslint-disable vue/multi-word-component-names -->
2
+ <script setup lang="ts">
3
+ import type { Component, HTMLAttributes } from "vue";
4
+ import { computed } from "vue";
5
+ import { cn } from "cn";
6
+ import { CircleAlert, CircleCheck, Info, TriangleAlert } from "@lucide/vue";
7
+ import { Alert, AlertAction, AlertDescription, AlertTitle } from "../../ui/alert";
8
+ import { Icon } from "../base";
9
+
10
+ defineOptions({ name: "Notice", inheritAttrs: false });
11
+
12
+ type NoticeTone = "info" | "success" | "warning" | "error";
13
+
14
+ const props = withDefaults(
15
+ defineProps<{
16
+ tone?: NoticeTone;
17
+ filled?: boolean;
18
+ title?: string;
19
+ role?: "status" | "alert";
20
+ class?: HTMLAttributes["class"];
21
+ }>(),
22
+ {
23
+ tone: "info",
24
+ filled: false,
25
+ title: undefined,
26
+ role: undefined,
27
+ class: undefined,
28
+ },
29
+ );
30
+
31
+ const icons: Record<NoticeTone, Component> = {
32
+ info: Info,
33
+ success: CircleCheck,
34
+ warning: TriangleAlert,
35
+ error: CircleAlert,
36
+ };
37
+
38
+ // 状态色只落在边框、图标与标题,正文保持中性;filled 仅添加低透明度背景。
39
+ const toneClass: Record<NoticeTone, string> = {
40
+ info: "border-info/30 [&>svg]:text-info [&>[data-slot=alert-title]]:text-info",
41
+ success: "border-success/30 [&>svg]:text-success [&>[data-slot=alert-title]]:text-success",
42
+ warning: "border-warning/30 [&>svg]:text-warning [&>[data-slot=alert-title]]:text-warning",
43
+ error: "border-destructive/30 [&>svg]:text-destructive [&>[data-slot=alert-title]]:text-destructive",
44
+ };
45
+
46
+ const filledBackgroundClass: Record<NoticeTone, string> = {
47
+ info: "bg-info/1",
48
+ success: "bg-success/1",
49
+ warning: "bg-warning/1",
50
+ error: "bg-destructive/1",
51
+ };
52
+
53
+ const resolvedRole = computed(() => props.role ?? (props.tone === "error" ? "alert" : "status"));
54
+ const icon = computed(() => icons[props.tone]);
55
+ const backgroundClass = computed(() => (props.filled ? filledBackgroundClass[props.tone] : "bg-transparent"));
56
+ </script>
57
+
58
+ <template>
59
+ <Alert
60
+ v-bind="$attrs"
61
+ :role="resolvedRole"
62
+ :class="cn(toneClass[tone], backgroundClass, 'has-data-[slot=alert-action]:pr-2.5', props.class)"
63
+ >
64
+ <Icon :icon="icon" />
65
+ <AlertTitle v-if="title">{{ title }}</AlertTitle>
66
+ <AlertDescription v-if="$slots.default"><slot /></AlertDescription>
67
+ <AlertAction v-if="$slots.actions" class="static col-start-2 mt-1 flex flex-wrap items-center justify-end gap-1">
68
+ <slot name="actions" />
69
+ </AlertAction>
70
+ </Alert>
71
+ </template>
@@ -0,0 +1,165 @@
1
+ <!-- eslint-disable vue/multi-word-component-names -->
2
+ <script setup lang="ts">
3
+ import type { HTMLAttributes } from "vue";
4
+ import { computed, onBeforeUnmount, ref, useId, watch } from "vue";
5
+ import { cn } from "cn";
6
+ import { Download, Eye, EyeClosed } from "@lucide/vue";
7
+ import { CopyBtn, EmptyValue, IconBtn, Text, VSeparator } from "../base";
8
+ import { pemFingerprints, type PemEncode } from "./pem";
9
+
10
+ defineOptions({ name: "Pem", inheritAttrs: false });
11
+
12
+ const props = withDefaults(
13
+ defineProps<{
14
+ value?: string | null;
15
+ expand?: boolean;
16
+ copy?: boolean;
17
+ download?: string;
18
+ encode?: PemEncode;
19
+ copyLabel?: string;
20
+ copiedText?: string;
21
+ copyFailedText?: string;
22
+ downloadLabel?: string;
23
+ expandLabel?: string;
24
+ collapseLabel?: string;
25
+ fingerprintErrorText?: string;
26
+ class?: HTMLAttributes["class"];
27
+ }>(),
28
+ {
29
+ value: undefined,
30
+ expand: false,
31
+ copy: false,
32
+ download: "",
33
+ encode: "base64",
34
+ copyLabel: "复制 PEM",
35
+ copiedText: "已复制",
36
+ copyFailedText: "复制失败",
37
+ downloadLabel: "下载 PEM",
38
+ expandLabel: "展开 PEM",
39
+ collapseLabel: "收起 PEM",
40
+ fingerprintErrorText: "指纹计算失败",
41
+ class: undefined,
42
+ },
43
+ );
44
+
45
+ const contentId = useId();
46
+ const visible = ref(props.expand);
47
+ watch(
48
+ () => props.expand,
49
+ (value) => {
50
+ visible.value = value;
51
+ },
52
+ );
53
+
54
+ const hashes = ref<{ label: string; fingerprint: string }[]>([]);
55
+ const fingerprintError = ref("");
56
+ let fingerprintRun = 0;
57
+
58
+ watch(
59
+ [() => props.value, () => props.encode],
60
+ async ([value, encode]) => {
61
+ // 异步指纹可能乱序返回,用序号保证只有最新一次计算能写入结果。
62
+ const run = ++fingerprintRun;
63
+ fingerprintError.value = "";
64
+ if (!value) {
65
+ hashes.value = [];
66
+ return;
67
+ }
68
+ try {
69
+ const result = await pemFingerprints(value, encode);
70
+ if (run === fingerprintRun) hashes.value = result;
71
+ } catch (error) {
72
+ if (run !== fingerprintRun) return;
73
+ hashes.value = [];
74
+ fingerprintError.value =
75
+ error instanceof Error && error.message
76
+ ? `${props.fingerprintErrorText}:${error.message}`
77
+ : props.fingerprintErrorText;
78
+ }
79
+ },
80
+ { immediate: true },
81
+ );
82
+
83
+ let downloadUrl = "";
84
+
85
+ const releaseDownloadUrl = (url: string) => {
86
+ URL.revokeObjectURL(url);
87
+ if (downloadUrl === url) downloadUrl = "";
88
+ };
89
+
90
+ const downloadFile = () => {
91
+ if (!props.download || !props.value) return;
92
+ const blob = new Blob([props.value], { type: "application/x-pem-file" });
93
+ if (downloadUrl) releaseDownloadUrl(downloadUrl);
94
+ downloadUrl = URL.createObjectURL(blob);
95
+ const url = downloadUrl;
96
+ const anchor = document.createElement("a");
97
+ anchor.href = url;
98
+ anchor.download = props.download;
99
+ anchor.rel = "noopener";
100
+ document.body.appendChild(anchor);
101
+ anchor.click();
102
+ anchor.remove();
103
+ // 让浏览器先开始下载再释放;闭包持有本次 URL,避免连点下载时释放后来的地址。
104
+ setTimeout(() => releaseDownloadUrl(url), 1000);
105
+ };
106
+
107
+ const toggleLabel = computed(() => (visible.value ? props.collapseLabel : props.expandLabel));
108
+
109
+ onBeforeUnmount(() => {
110
+ if (downloadUrl) releaseDownloadUrl(downloadUrl);
111
+ });
112
+ </script>
113
+
114
+ <template>
115
+ <div v-bind="$attrs" data-slot="pem" :class="cn('flex min-w-0 flex-col gap-1 text-xs', props.class)">
116
+ <template v-if="value">
117
+ <div class="inline-flex min-w-0 items-center gap-1">
118
+ <div class="flex min-w-0 flex-col">
119
+ <Text
120
+ v-for="(hash, index) in hashes"
121
+ :key="`${hash.label}-${index}`"
122
+ variant="inlineCode"
123
+ class="truncate text-xs"
124
+ >
125
+ SHA256:{{ hash.fingerprint }}
126
+ </Text>
127
+ <span v-if="fingerprintError" role="alert" class="text-destructive text-xs">
128
+ {{ fingerprintError }}
129
+ </span>
130
+ </div>
131
+ <VSeparator />
132
+ <CopyBtn
133
+ v-if="copy"
134
+ :value="value"
135
+ :label="copyLabel"
136
+ :copied-text="copiedText"
137
+ :failed-text="copyFailedText"
138
+ />
139
+ <IconBtn
140
+ v-if="download"
141
+ :icon="Download"
142
+ size="icon-xs"
143
+ :aria-label="downloadLabel"
144
+ class="shrink-0"
145
+ @click="downloadFile"
146
+ />
147
+ <IconBtn
148
+ :icon="visible ? Eye : EyeClosed"
149
+ size="icon-xs"
150
+ :aria-label="toggleLabel"
151
+ :aria-expanded="visible"
152
+ :aria-controls="visible ? contentId : undefined"
153
+ class="shrink-0"
154
+ @click="visible = !visible"
155
+ />
156
+ </div>
157
+ <pre
158
+ v-if="visible"
159
+ :id="contentId"
160
+ class="bg-muted border-border overflow-x-auto border p-2 text-xs"
161
+ ><code class="whitespace-pre">{{ value }}</code></pre>
162
+ </template>
163
+ <EmptyValue v-else />
164
+ </div>
165
+ </template>
@@ -0,0 +1,117 @@
1
+ <script setup lang="ts">
2
+ import type { HTMLAttributes } from "vue";
3
+ import { defineAsyncComponent, defineComponent, onBeforeUnmount, ref } from "vue";
4
+ import { useModal } from "vue-final-modal";
5
+ import { QrCode } from "@lucide/vue";
6
+ import { cn } from "cn";
7
+ import { IconBtn, Text } from "../base";
8
+ import AsyncError from "./AsyncError.vue";
9
+ import { captureModalOpenError, failModalLoad } from "./qr";
10
+
11
+ defineOptions({ name: "QrBtn", inheritAttrs: false });
12
+
13
+ const props = withDefaults(
14
+ defineProps<{
15
+ value: string;
16
+ small?: boolean;
17
+ label?: string;
18
+ modalTitle?: string;
19
+ closeLabel?: string;
20
+ loadingText?: string;
21
+ errorText?: string;
22
+ retryLabel?: string;
23
+ class?: HTMLAttributes["class"];
24
+ }>(),
25
+ {
26
+ small: false,
27
+ label: "查看二维码",
28
+ modalTitle: "二维码",
29
+ closeLabel: "关闭二维码",
30
+ loadingText: "生成中…",
31
+ errorText: "二维码生成失败",
32
+ retryLabel: "重试",
33
+ class: undefined,
34
+ },
35
+ );
36
+
37
+ const opening = ref(false);
38
+ const openError = ref<unknown>(null);
39
+
40
+ // 懒加载分块:二维码编码器与模态标记不进入主 bundle。
41
+ const QrModal = defineAsyncComponent({
42
+ loader: () => import("./QrModal.vue"),
43
+ // 错误呈现在触发器旁,而不是脱离 VueFinalModal 的加载占位中。
44
+ errorComponent: defineComponent({ name: "QrModalLoadError", setup: () => () => null }),
45
+ onError: (error, _retry, fail) => {
46
+ failModalLoad(
47
+ error,
48
+ fail,
49
+ () => modal.destroy(),
50
+ (cause) => {
51
+ openError.value = cause;
52
+ },
53
+ );
54
+ },
55
+ });
56
+
57
+ // 在 setup 注册,卸载时才能连同仍打开的模态一起 destroy,避免路由切换后残留。
58
+ const modalValue = ref("");
59
+ const modal = useModal({
60
+ component: QrModal,
61
+ attrs: () => ({
62
+ value: modalValue.value,
63
+ title: props.modalTitle,
64
+ closeLabel: props.closeLabel,
65
+ loadingText: props.loadingText,
66
+ errorText: props.errorText,
67
+ onClose: () => {
68
+ void modal.close();
69
+ },
70
+ }),
71
+ });
72
+
73
+ const open = async () => {
74
+ if (!props.value || opening.value) return;
75
+ // 每次打开都写入当前值;关闭后模态卸载,重新打开会重新生成二维码。
76
+ modalValue.value = props.value;
77
+ openError.value = null;
78
+ opening.value = true;
79
+ try {
80
+ await captureModalOpenError(modal.open, (error) => {
81
+ openError.value = error;
82
+ });
83
+ } finally {
84
+ opening.value = false;
85
+ }
86
+ };
87
+
88
+ onBeforeUnmount(() => {
89
+ modal.destroy();
90
+ });
91
+ </script>
92
+
93
+ <template>
94
+ <div data-slot="qr-btn" class="inline-flex min-w-0 max-w-full flex-col items-start gap-2 align-top">
95
+ <IconBtn
96
+ v-bind="$attrs"
97
+ :icon="QrCode"
98
+ :size="small ? 'icon-xs' : 'icon'"
99
+ :aria-label="label"
100
+ :aria-busy="opening ? 'true' : undefined"
101
+ :disabled="!value || opening"
102
+ :class="cn('shrink-0', props.class)"
103
+ @click="open"
104
+ />
105
+ <Text v-if="opening && openError === null" variant="muted" role="status" class="max-w-md">
106
+ {{ loadingText }}
107
+ </Text>
108
+ <AsyncError
109
+ v-if="openError !== null"
110
+ :error="openError"
111
+ :title="errorText"
112
+ :retry-label="retryLabel"
113
+ class="max-w-md"
114
+ @retry="open"
115
+ />
116
+ </div>
117
+ </template>