@ysgroup/ui 0.1.38 → 0.1.40

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ysgroup/ui",
3
- "version": "0.1.38",
3
+ "version": "0.1.40",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -9,7 +9,7 @@
9
9
  "@ysgroup/contracts": "0.1.8"
10
10
  },
11
11
  "peerDependencies": {
12
- "@ysgroup/theme": ">=0.2.0",
12
+ "@ysgroup/theme": ">=0.2.4",
13
13
  "element-plus": ">=2.9",
14
14
  "vue": ">=3.5"
15
15
  },
@@ -20,5 +20,8 @@
20
20
  ],
21
21
  "publishConfig": {
22
22
  "access": "public"
23
+ },
24
+ "devDependencies": {
25
+ "@ysgroup/theme": "^0.2.4"
23
26
  }
24
- }
27
+ }
@@ -1,17 +1,28 @@
1
1
  <script setup lang="ts">
2
- import { ref } from "vue";
2
+ import { onBeforeUnmount, ref } from "vue";
3
3
  import { Moon, Setting, Sunny } from "@element-plus/icons-vue";
4
- import { applyTheme, currentMode, type ThemeMode } from "@ysgroup/theme";
4
+ import {
5
+ applyTheme,
6
+ currentMode,
7
+ resolvedMode,
8
+ THEME_CHANGE_EVENT,
9
+ type ThemeMode,
10
+ } from "@ysgroup/theme";
5
11
 
6
12
  defineProps<{ username?: string }>();
7
13
  const emit = defineEmits<{ settings: []; logout: [] }>();
8
- const mode = ref<ThemeMode>(
9
- document.documentElement.getAttribute("data-theme") === "dark"
10
- ? "dark"
11
- : "light",
12
- );
14
+ const mode = ref<ThemeMode>(resolvedMode());
13
15
  const dropdown = ref<{ handleClose: () => void } | null>(null);
14
- if (currentMode() === "auto") applyTheme(mode.value);
16
+ if (currentMode() === "auto") applyTheme(mode.value, "sync");
17
+
18
+ // 登录后按账号偏好切换、或别处切换时,图标跟着变。
19
+ function onThemeChange() {
20
+ mode.value = resolvedMode();
21
+ }
22
+ window.addEventListener(THEME_CHANGE_EVENT, onThemeChange);
23
+ onBeforeUnmount(() =>
24
+ window.removeEventListener(THEME_CHANGE_EVENT, onThemeChange),
25
+ );
15
26
 
16
27
  function selectTheme(next: ThemeMode) {
17
28
  if (next !== "light" && next !== "dark") return;
@@ -124,11 +135,15 @@ function settingsCommand(command: string) {
124
135
  color: inherit;
125
136
  cursor: pointer;
126
137
  }
127
- .ys-app-toolbar__button:hover,
128
- .ys-app-toolbar__button:focus-visible {
138
+ .ys-app-toolbar__button:hover {
129
139
  background: var(--ys-color-bg-hover);
130
140
  color: var(--ys-color-primary);
131
141
  }
142
+ /* 顶栏图标按钮不画聚焦框:菜单关闭后焦点会回到按钮,框时有时无反而像出错。 */
143
+ .ys-app-toolbar__button:focus,
144
+ .ys-app-toolbar__button:focus-visible {
145
+ outline: none;
146
+ }
132
147
  @media (max-width: 600px) {
133
148
  .ys-app-toolbar {
134
149
  padding-left: 4px;
@@ -0,0 +1,36 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * 数字角标:未读、待处理数量全平台只用它(样式在 @ysgroup/theme 的 count-badge.css)。
4
+ * 1~2 位是 20px 正圆,超过 max 显示「99+」胶囊;0 或空不渲染。
5
+ * 放在标签、图标右上角时传 corner,外层要 position: relative。
6
+ */
7
+ import { computed } from "vue";
8
+
9
+ const props = withDefaults(
10
+ defineProps<{
11
+ count?: number | null;
12
+ max?: number;
13
+ /** 压在右上角,带一圈底色描边。 */
14
+ corner?: boolean;
15
+ /** 选中的主色行里用白底主色字。 */
16
+ inverse?: boolean;
17
+ }>(),
18
+ { count: 0, max: 99, corner: false, inverse: false },
19
+ );
20
+
21
+ const text = computed(() => {
22
+ const value = Math.floor(Number(props.count) || 0);
23
+ if (value <= 0) return "";
24
+ return value > props.max ? `${props.max}+` : String(value);
25
+ });
26
+ </script>
27
+
28
+ <template>
29
+ <span
30
+ v-if="text"
31
+ class="ys-count-badge"
32
+ :class="{ 'ys-count-badge--corner': corner, 'ys-count-badge--inverse': inverse }"
33
+ aria-hidden="true"
34
+ >{{ text }}</span
35
+ >
36
+ </template>
@@ -0,0 +1,437 @@
1
+ <!-- YsFilePreview —— 文件预览弹窗,全平台统一的文件预览入口。
2
+ PDF 用浏览器自带阅读器(iframe 打开 blob 地址,支持翻页、缩放、搜索、打印),
3
+ 图片、文本、音视频直接显示,其他类型提示下载。文件内容由调用方的 load 取回(WS 分块、HTTP 都行),
4
+ 组件只负责显示、进度、出错重试、下载、新标签打开、全屏和 ← → 切换。关闭后回收 blob 地址。 -->
5
+ <script setup lang="ts">
6
+ import { computed, onBeforeUnmount, ref, watch } from "vue";
7
+ import {
8
+ ArrowLeft,
9
+ ArrowRight,
10
+ Close,
11
+ Download,
12
+ FullScreen,
13
+ Document,
14
+ TopRight,
15
+ } from "@element-plus/icons-vue";
16
+ import {
17
+ TEXT_PREVIEW_LIMIT,
18
+ fileTypeLabel,
19
+ formatFileSize,
20
+ previewKindOf,
21
+ saveBlob,
22
+ sniffPreviewKind,
23
+ type YsPreviewFile,
24
+ type YsPreviewKind,
25
+ } from "../filePreview";
26
+
27
+ const props = withDefaults(
28
+ defineProps<{
29
+ modelValue: boolean;
30
+ /** 可切换的文件列表;只预览一个时传一项。 */
31
+ files: YsPreviewFile[];
32
+ /** 当前文件下标,可 v-model:index。 */
33
+ index?: number;
34
+ /** 取文件内容。onProgress 传 0~1,取不到总量时可不调。 */
35
+ load: (file: YsPreviewFile, onProgress: (ratio: number) => void) => Promise<Blob>;
36
+ }>(),
37
+ { index: 0 },
38
+ );
39
+
40
+ const emit = defineEmits<{
41
+ "update:modelValue": [value: boolean];
42
+ "update:index": [value: number];
43
+ }>();
44
+
45
+ const current = ref(0);
46
+ const loading = ref(false);
47
+ const progress = ref(0);
48
+ const error = ref("");
49
+ const blob = ref<Blob | null>(null);
50
+ const url = ref("");
51
+ const text = ref("");
52
+ const textCut = ref(false);
53
+ const kind = ref<YsPreviewKind>("none");
54
+ const fullscreen = ref(false);
55
+ let token = 0;
56
+
57
+ const file = computed<YsPreviewFile | null>(() => props.files[current.value] ?? null);
58
+ const total = computed(() => props.files.length);
59
+ const canPrev = computed(() => current.value > 0);
60
+ const canNext = computed(() => current.value < total.value - 1);
61
+ const meta = computed(() => {
62
+ if (!file.value) return "";
63
+ const size = formatFileSize(file.value.size);
64
+ return [fileTypeLabel(file.value.name, file.value.content_type), size].filter(Boolean).join(" · ");
65
+ });
66
+
67
+ function release() {
68
+ if (url.value) URL.revokeObjectURL(url.value);
69
+ url.value = "";
70
+ blob.value = null;
71
+ text.value = "";
72
+ textCut.value = false;
73
+ }
74
+
75
+ async function open() {
76
+ const target = file.value;
77
+ const mine = ++token;
78
+ release();
79
+ error.value = "";
80
+ progress.value = 0;
81
+ if (!target) return;
82
+ kind.value = previewKindOf(target);
83
+ loading.value = true;
84
+ try {
85
+ const data = await props.load(target, (ratio) => {
86
+ if (mine === token) progress.value = Math.max(0, Math.min(1, ratio));
87
+ });
88
+ if (mine !== token) return;
89
+ if (kind.value === "none") kind.value = await sniffPreviewKind(data);
90
+ if (mine !== token) return;
91
+ // PDF 必须带 application/pdf 类型,浏览器才会用自带阅读器打开。
92
+ const typed =
93
+ kind.value === "pdf" && data.type !== "application/pdf"
94
+ ? new Blob([data], { type: "application/pdf" })
95
+ : data;
96
+ blob.value = typed;
97
+ if (kind.value === "text") {
98
+ textCut.value = typed.size > TEXT_PREVIEW_LIMIT;
99
+ text.value = await typed.slice(0, TEXT_PREVIEW_LIMIT).text();
100
+ } else if (kind.value !== "none") {
101
+ url.value = URL.createObjectURL(typed);
102
+ }
103
+ } catch (reason) {
104
+ if (mine !== token) return;
105
+ error.value = reason instanceof Error && reason.message ? reason.message : "文件读取失败";
106
+ } finally {
107
+ if (mine === token) loading.value = false;
108
+ }
109
+ }
110
+
111
+ watch(
112
+ () => [props.modelValue, props.index] as const,
113
+ ([visible, index]) => {
114
+ if (!visible) return;
115
+ const next = Math.max(0, Math.min(index ?? 0, Math.max(0, total.value - 1)));
116
+ if (next !== current.value || !blob.value) {
117
+ current.value = next;
118
+ void open();
119
+ }
120
+ },
121
+ { immediate: true },
122
+ );
123
+
124
+ watch(
125
+ () => props.modelValue,
126
+ (visible) => {
127
+ if (visible) {
128
+ window.addEventListener("keydown", onKeydown);
129
+ return;
130
+ }
131
+ window.removeEventListener("keydown", onKeydown);
132
+ token += 1;
133
+ loading.value = false;
134
+ fullscreen.value = false;
135
+ release();
136
+ },
137
+ { immediate: true },
138
+ );
139
+
140
+ onBeforeUnmount(() => {
141
+ window.removeEventListener("keydown", onKeydown);
142
+ token += 1;
143
+ release();
144
+ });
145
+
146
+ function go(step: number) {
147
+ const next = current.value + step;
148
+ if (next < 0 || next >= total.value) return;
149
+ current.value = next;
150
+ emit("update:index", next);
151
+ void open();
152
+ }
153
+
154
+ function onKeydown(event: KeyboardEvent) {
155
+ const target = event.target as HTMLElement | null;
156
+ if (target?.closest("input, textarea, [contenteditable='true']")) return;
157
+ if (event.key === "ArrowLeft") go(-1);
158
+ else if (event.key === "ArrowRight") go(1);
159
+ }
160
+
161
+ function close() {
162
+ emit("update:modelValue", false);
163
+ }
164
+
165
+ async function download() {
166
+ const target = file.value;
167
+ if (!target) return;
168
+ if (blob.value) {
169
+ saveBlob(blob.value, target.name);
170
+ return;
171
+ }
172
+ await open();
173
+ if (blob.value) saveBlob(blob.value, target.name);
174
+ }
175
+
176
+ function openInTab() {
177
+ if (url.value) window.open(url.value, "_blank", "noopener");
178
+ }
179
+ </script>
180
+
181
+ <template>
182
+ <el-dialog
183
+ :model-value="modelValue"
184
+ :fullscreen="fullscreen"
185
+ :show-close="false"
186
+ width="min(1120px, 92vw)"
187
+ top="4vh"
188
+ append-to-body
189
+ destroy-on-close
190
+ class="ys-file-preview"
191
+ @update:model-value="close"
192
+ >
193
+ <template #header>
194
+ <div class="ys-file-preview__head">
195
+ <el-icon class="ys-file-preview__icon" :size="18"><Document /></el-icon>
196
+ <div class="ys-file-preview__title">
197
+ <strong :title="file?.name">{{ file?.name || "文件预览" }}</strong>
198
+ <span>{{ meta }}<template v-if="total > 1"> · 第 {{ current + 1 }} / {{ total }} 个</template></span>
199
+ </div>
200
+ <div class="ys-file-preview__tools">
201
+ <template v-if="total > 1">
202
+ <el-tooltip content="上一个(←)" placement="bottom" :show-after="400">
203
+ <button type="button" class="ys-file-preview__tool" :disabled="!canPrev" aria-label="上一个" @click="go(-1)">
204
+ <el-icon :size="16"><ArrowLeft /></el-icon>
205
+ </button>
206
+ </el-tooltip>
207
+ <el-tooltip content="下一个(→)" placement="bottom" :show-after="400">
208
+ <button type="button" class="ys-file-preview__tool" :disabled="!canNext" aria-label="下一个" @click="go(1)">
209
+ <el-icon :size="16"><ArrowRight /></el-icon>
210
+ </button>
211
+ </el-tooltip>
212
+ <span class="ys-file-preview__divider" aria-hidden="true" />
213
+ </template>
214
+ <el-tooltip v-if="kind !== 'none' && kind !== 'text'" content="新标签页打开" placement="bottom" :show-after="400">
215
+ <button type="button" class="ys-file-preview__tool" :disabled="!url" aria-label="新标签页打开" @click="openInTab">
216
+ <el-icon :size="16"><TopRight /></el-icon>
217
+ </button>
218
+ </el-tooltip>
219
+ <el-tooltip :content="fullscreen ? '退出全屏' : '全屏'" placement="bottom" :show-after="400">
220
+ <button type="button" class="ys-file-preview__tool" :aria-label="fullscreen ? '退出全屏' : '全屏'" @click="fullscreen = !fullscreen">
221
+ <el-icon :size="16"><FullScreen /></el-icon>
222
+ </button>
223
+ </el-tooltip>
224
+ <el-tooltip content="下载" placement="bottom" :show-after="400">
225
+ <button type="button" class="ys-file-preview__tool" :disabled="loading" aria-label="下载" @click="download">
226
+ <el-icon :size="16"><Download /></el-icon>
227
+ </button>
228
+ </el-tooltip>
229
+ <span class="ys-file-preview__divider" aria-hidden="true" />
230
+ <button type="button" class="ys-file-preview__tool" aria-label="关闭" @click="close">
231
+ <el-icon :size="18"><Close /></el-icon>
232
+ </button>
233
+ </div>
234
+ </div>
235
+ </template>
236
+
237
+ <div class="ys-file-preview__stage" :class="{ 'is-fullscreen': fullscreen, 'is-dark': kind === 'image' || kind === 'video' }">
238
+ <div v-if="loading" class="ys-file-preview__state">
239
+ <el-progress
240
+ type="circle"
241
+ :width="72"
242
+ :stroke-width="5"
243
+ :percentage="Math.round(progress * 100)"
244
+ />
245
+ <span>正在读取文件…</span>
246
+ </div>
247
+ <div v-else-if="error" class="ys-file-preview__state">
248
+ <span class="ys-file-preview__error">{{ error }}</span>
249
+ <el-button type="primary" plain @click="open">重试</el-button>
250
+ </div>
251
+ <template v-else-if="file">
252
+ <iframe
253
+ v-if="kind === 'pdf' && url"
254
+ :key="url"
255
+ class="ys-file-preview__frame"
256
+ :src="url"
257
+ :title="file.name"
258
+ />
259
+ <img v-else-if="kind === 'image' && url" class="ys-file-preview__image" :src="url" :alt="file.name" />
260
+ <video v-else-if="kind === 'video' && url" class="ys-file-preview__media" :src="url" controls />
261
+ <audio v-else-if="kind === 'audio' && url" class="ys-file-preview__audio" :src="url" controls />
262
+ <div v-else-if="kind === 'text'" class="ys-file-preview__text">
263
+ <div v-if="textCut" class="ys-file-preview__notice">文件较大,只显示开头 {{ formatFileSize(TEXT_PREVIEW_LIMIT) }},完整内容请下载查看。</div>
264
+ <pre>{{ text }}</pre>
265
+ </div>
266
+ <div v-else class="ys-file-preview__state">
267
+ <el-icon :size="40" class="ys-file-preview__placeholder"><Document /></el-icon>
268
+ <strong>{{ fileTypeLabel(file.name, file.content_type) }} 文件暂不支持在线预览</strong>
269
+ <span>下载后用本地软件打开</span>
270
+ <el-button type="primary" :icon="Download" @click="download">下载</el-button>
271
+ </div>
272
+ </template>
273
+ </div>
274
+ </el-dialog>
275
+ </template>
276
+
277
+ <style>
278
+ /* 弹窗本体去掉默认内边距,让预览区铺满。不能 scoped:el-dialog 挂在 body 下。 */
279
+ .ys-file-preview.el-dialog {
280
+ display: flex;
281
+ flex-direction: column;
282
+ padding: 0;
283
+ overflow: hidden;
284
+ }
285
+ .ys-file-preview .el-dialog__header {
286
+ padding: 0;
287
+ margin: 0;
288
+ }
289
+ .ys-file-preview .el-dialog__body {
290
+ flex: 1;
291
+ min-height: 0;
292
+ padding: 0;
293
+ }
294
+ </style>
295
+
296
+ <style scoped>
297
+ .ys-file-preview__head {
298
+ display: flex;
299
+ align-items: center;
300
+ gap: 10px;
301
+ min-height: 52px;
302
+ padding: 8px 10px 8px 16px;
303
+ border-bottom: 1px solid var(--ys-color-border);
304
+ }
305
+ .ys-file-preview__icon {
306
+ flex: none;
307
+ color: var(--ys-color-primary);
308
+ }
309
+ .ys-file-preview__title {
310
+ display: flex;
311
+ flex: 1;
312
+ flex-direction: column;
313
+ min-width: 0;
314
+ line-height: 1.35;
315
+ }
316
+ .ys-file-preview__title strong {
317
+ overflow: hidden;
318
+ color: var(--ys-color-text-1);
319
+ font-size: 14px;
320
+ font-weight: 600;
321
+ text-overflow: ellipsis;
322
+ white-space: nowrap;
323
+ }
324
+ .ys-file-preview__title span {
325
+ color: var(--ys-color-text-3);
326
+ font-size: 12px;
327
+ font-variant-numeric: tabular-nums;
328
+ }
329
+ .ys-file-preview__tools {
330
+ display: flex;
331
+ flex: none;
332
+ align-items: center;
333
+ gap: 2px;
334
+ }
335
+ .ys-file-preview__tool {
336
+ display: inline-flex;
337
+ align-items: center;
338
+ justify-content: center;
339
+ width: 32px;
340
+ height: 32px;
341
+ padding: 0;
342
+ border: 0;
343
+ border-radius: var(--ys-radius-s);
344
+ background: transparent;
345
+ color: var(--ys-color-text-2);
346
+ cursor: pointer;
347
+ }
348
+ .ys-file-preview__tool:hover:not(:disabled) {
349
+ background: var(--ys-color-bg-hover);
350
+ color: var(--ys-color-primary);
351
+ }
352
+ /* 与顶栏图标按钮一致:不画聚焦框。 */
353
+ .ys-file-preview__tool:focus,
354
+ .ys-file-preview__tool:focus-visible {
355
+ outline: none;
356
+ }
357
+ .ys-file-preview__tool:disabled {
358
+ color: var(--ys-color-text-3);
359
+ cursor: not-allowed;
360
+ opacity: 0.5;
361
+ }
362
+ .ys-file-preview__divider {
363
+ width: 1px;
364
+ height: 18px;
365
+ margin: 0 4px;
366
+ background: var(--ys-color-border);
367
+ }
368
+ .ys-file-preview__stage {
369
+ display: flex;
370
+ align-items: center;
371
+ justify-content: center;
372
+ height: min(80vh, 900px);
373
+ background: var(--ys-color-bg);
374
+ }
375
+ .ys-file-preview__stage.is-fullscreen {
376
+ height: calc(100vh - 53px);
377
+ }
378
+ .ys-file-preview__stage.is-dark {
379
+ background: #1f1f1f;
380
+ }
381
+ .ys-file-preview__frame {
382
+ width: 100%;
383
+ height: 100%;
384
+ border: 0;
385
+ background: #fff;
386
+ }
387
+ .ys-file-preview__image,
388
+ .ys-file-preview__media {
389
+ max-width: 100%;
390
+ max-height: 100%;
391
+ object-fit: contain;
392
+ }
393
+ .ys-file-preview__audio {
394
+ width: min(520px, 90%);
395
+ }
396
+ .ys-file-preview__text {
397
+ align-self: stretch;
398
+ width: 100%;
399
+ overflow: auto;
400
+ background: var(--ys-color-bg-elevated);
401
+ }
402
+ .ys-file-preview__text pre {
403
+ margin: 0;
404
+ padding: 16px 20px;
405
+ color: var(--ys-color-text-1);
406
+ font-family: var(--ys-font-family-mono);
407
+ font-size: 13px;
408
+ line-height: 1.6;
409
+ white-space: pre-wrap;
410
+ word-break: break-word;
411
+ }
412
+ .ys-file-preview__notice {
413
+ padding: 8px 20px;
414
+ border-bottom: 1px solid var(--ys-color-border);
415
+ color: var(--ys-color-warning);
416
+ font-size: 12px;
417
+ }
418
+ .ys-file-preview__state {
419
+ display: flex;
420
+ flex-direction: column;
421
+ align-items: center;
422
+ gap: 12px;
423
+ color: var(--ys-color-text-3);
424
+ font-size: 13px;
425
+ }
426
+ .ys-file-preview__state strong {
427
+ color: var(--ys-color-text-1);
428
+ font-size: 15px;
429
+ font-weight: 600;
430
+ }
431
+ .ys-file-preview__placeholder {
432
+ color: var(--ys-color-text-3);
433
+ }
434
+ .ys-file-preview__error {
435
+ color: var(--ys-color-danger);
436
+ }
437
+ </style>
@@ -1,8 +1,13 @@
1
1
  <!-- YsLoginPage —— 统一登录页。口令由 @ysgroup/core 密封后提交。 -->
2
2
  <script setup lang="ts">
3
- import { ref } from "vue";
3
+ import { onBeforeUnmount, ref } from "vue";
4
4
  import { Loading, Moon, Sunny } from "@element-plus/icons-vue";
5
- import { applyTheme, type ThemeMode } from "@ysgroup/theme";
5
+ import {
6
+ applyTheme,
7
+ resolvedMode,
8
+ THEME_CHANGE_EVENT,
9
+ type ThemeMode,
10
+ } from "@ysgroup/theme";
6
11
 
7
12
  const props = withDefaults(
8
13
  defineProps<{
@@ -31,13 +36,17 @@ const emit = defineEmits<{
31
36
  const username = ref("");
32
37
  const password = ref("");
33
38
  const validationError = ref("");
34
- const mode = ref<ThemeMode>(
35
- document.documentElement.getAttribute("data-theme") === "dark"
36
- ? "dark"
37
- : "light",
38
- );
39
+ const mode = ref<ThemeMode>(resolvedMode());
39
40
  const themeMenu = ref<{ handleClose: () => void } | null>(null);
40
41
 
42
+ function onThemeChange() {
43
+ mode.value = resolvedMode();
44
+ }
45
+ window.addEventListener(THEME_CHANGE_EVENT, onThemeChange);
46
+ onBeforeUnmount(() =>
47
+ window.removeEventListener(THEME_CHANGE_EVENT, onThemeChange),
48
+ );
49
+
41
50
  function selectTheme(next: ThemeMode) {
42
51
  if (next !== "light" && next !== "dark") return;
43
52
  mode.value = next;
@@ -277,12 +286,14 @@ html[data-theme="dark"] .ys-login__card::after {
277
286
  color: color-mix(in srgb, var(--ys-color-text-2) 62%, var(--ys-color-text-3));
278
287
  cursor: pointer;
279
288
  }
280
- .ys-login__theme-button:hover,
281
- .ys-login__theme-button:focus-visible {
282
- outline: none;
289
+ .ys-login__theme-button:hover {
283
290
  background: var(--ys-color-bg-hover);
284
291
  color: var(--ys-color-primary);
285
292
  }
293
+ .ys-login__theme-button:focus,
294
+ .ys-login__theme-button:focus-visible {
295
+ outline: none;
296
+ }
286
297
  .ys-login__logo {
287
298
  width: 68px;
288
299
  height: 68px;
@@ -187,12 +187,14 @@ function toggleSidebar() {
187
187
  color: color-mix(in srgb, var(--ys-color-text-2) 62%, var(--ys-color-text-3));
188
188
  cursor: pointer;
189
189
  }
190
- .ys-layout__toggle:hover,
191
- .ys-layout__toggle:focus-visible {
192
- outline: none;
190
+ .ys-layout__toggle:hover {
193
191
  color: var(--ys-color-primary);
194
192
  background: var(--ys-color-bg-hover);
195
193
  }
194
+ .ys-layout__toggle:focus,
195
+ .ys-layout__toggle:focus-visible {
196
+ outline: none;
197
+ }
196
198
  .ys-layout__toggle-icon {
197
199
  width: 18px;
198
200
  height: 18px;
@@ -1,4 +1,5 @@
1
1
  <script setup lang="ts">
2
+ import YsCountBadge from "./YsCountBadge.vue";
2
3
  import { CaretBottom, CaretRight } from "@element-plus/icons-vue";
3
4
 
4
5
  export type YsSideMenuChild = { key: string; title: string; badge?: number };
@@ -45,12 +46,10 @@ const emit = defineEmits<{ navigate: [key: string]; toggle: [key: string] }>();
45
46
  @click="emit('navigate', item.key)"
46
47
  >
47
48
  <span class="ys-side-menu__label">{{ item.title }}</span>
48
- <span
49
- v-if="badgeText(item.badge)"
50
- class="ys-side-menu__badge"
51
- aria-hidden="true"
52
- >{{ badgeText(item.badge) }}</span
53
- >
49
+ <YsCountBadge
50
+ :count="item.badge"
51
+ :inverse="!item.children?.length && item.key === active"
52
+ />
54
53
  </button>
55
54
  <button
56
55
  v-if="item.children?.length"
@@ -81,12 +80,7 @@ const emit = defineEmits<{ navigate: [key: string]; toggle: [key: string] }>();
81
80
  >
82
81
  <span class="ys-side-menu__marker" aria-hidden="true" />
83
82
  <span class="ys-side-menu__label">{{ child.title }}</span>
84
- <span
85
- v-if="badgeText(child.badge)"
86
- class="ys-side-menu__badge"
87
- aria-hidden="true"
88
- >{{ badgeText(child.badge) }}</span
89
- >
83
+ <YsCountBadge :count="child.badge" />
90
84
  </button>
91
85
  </div>
92
86
  </div>
@@ -143,24 +137,6 @@ const emit = defineEmits<{ navigate: [key: string]; toggle: [key: string] }>();
143
137
  text-overflow: ellipsis;
144
138
  white-space: nowrap;
145
139
  }
146
- .ys-side-menu__badge {
147
- flex: none;
148
- min-width: 20px;
149
- height: 20px;
150
- box-sizing: border-box;
151
- padding: 0 5px;
152
- border-radius: 10px;
153
- background: var(--ys-color-danger);
154
- color: var(--ys-color-bg-elevated);
155
- font-size: 11px;
156
- font-weight: 600;
157
- line-height: 20px;
158
- text-align: center;
159
- }
160
- .ys-side-menu__row.is-current .ys-side-menu__badge {
161
- background: var(--ys-color-bg-elevated);
162
- color: var(--ys-color-primary);
163
- }
164
140
  .ys-side-menu__row.is-current .ys-side-menu__main {
165
141
  color: var(--ys-color-bg-elevated);
166
142
  font-weight: 600;
@@ -212,10 +188,6 @@ const emit = defineEmits<{ navigate: [key: string]; toggle: [key: string] }>();
212
188
  color: var(--ys-color-text-2);
213
189
  text-align: left;
214
190
  }
215
- .ys-side-menu__child.is-current .ys-side-menu__badge {
216
- background: var(--ys-color-primary);
217
- color: var(--ys-color-bg-elevated);
218
- }
219
191
  .ys-side-menu__child:hover {
220
192
  background: var(--ys-color-bg-hover);
221
193
  color: var(--ys-color-text-1);
@@ -0,0 +1,100 @@
1
+ /* 文件预览的辅助函数:判断能怎么预览、文件类型标签、大小格式化、把 Blob 存成下载。
2
+ * 与 YsFilePreview 配合使用,也可单独用在文件列表里。 */
3
+
4
+ export type YsPreviewKind = "pdf" | "image" | "text" | "video" | "audio" | "none";
5
+
6
+ export type YsPreviewFile = {
7
+ /** 文件名(含扩展名),用于判断类型和下载命名。 */
8
+ name: string;
9
+ /** 原始大小(字节),只用于展示。 */
10
+ size?: number;
11
+ /** 上传时记录的 MIME,可空。 */
12
+ content_type?: string;
13
+ };
14
+
15
+ const IMAGE_EXT = new Set(["png", "jpg", "jpeg", "gif", "webp", "bmp", "svg", "avif", "ico"]);
16
+ const TEXT_EXT = new Set([
17
+ "txt", "md", "markdown", "csv", "tsv", "log", "json", "xml", "yaml", "yml", "ini", "toml", "conf",
18
+ "py", "js", "ts", "vue", "html", "css", "sql", "sh", "java", "go", "c", "h", "cpp",
19
+ ]);
20
+ const VIDEO_EXT = new Set(["mp4", "webm", "ogv", "mov"]);
21
+ const AUDIO_EXT = new Set(["mp3", "wav", "ogg", "m4a", "aac", "flac"]);
22
+
23
+ /** 文本预览最多读这么多字节,再大只显示开头,避免卡住页面。 */
24
+ export const TEXT_PREVIEW_LIMIT = 2 * 1024 * 1024;
25
+
26
+ export function fileExtension(name: string): string {
27
+ const text = String(name || "");
28
+ const dot = text.lastIndexOf(".");
29
+ return dot > 0 && dot < text.length - 1 ? text.slice(dot + 1).toLowerCase() : "";
30
+ }
31
+
32
+ /** 按扩展名和 MIME 判断预览方式;判断不了返回 none(只能下载)。 */
33
+ export function previewKindOf(file: Pick<YsPreviewFile, "name" | "content_type">): YsPreviewKind {
34
+ const ext = fileExtension(file.name);
35
+ const mime = String(file.content_type || "").toLowerCase();
36
+ if (ext === "pdf" || mime === "application/pdf") return "pdf";
37
+ if (IMAGE_EXT.has(ext) || mime.startsWith("image/")) return "image";
38
+ if (VIDEO_EXT.has(ext) || mime.startsWith("video/")) return "video";
39
+ if (AUDIO_EXT.has(ext) || mime.startsWith("audio/")) return "audio";
40
+ if (TEXT_EXT.has(ext) || mime.startsWith("text/") || mime === "application/json") return "text";
41
+ return "none";
42
+ }
43
+
44
+ /** 扩展名或 MIME 认不出时看文件头:%PDF 按 PDF 预览。 */
45
+ export async function sniffPreviewKind(blob: Blob): Promise<YsPreviewKind> {
46
+ const head = new Uint8Array(await blob.slice(0, 5).arrayBuffer());
47
+ const text = String.fromCharCode(...head);
48
+ return text.startsWith("%PDF") ? "pdf" : "none";
49
+ }
50
+
51
+ const TYPE_LABELS: [string[], string][] = [
52
+ [["pdf"], "PDF"],
53
+ [["doc", "docx", "wps", "odt", "rtf"], "Word"],
54
+ [["xls", "xlsx", "et", "ods", "csv"], "Excel"],
55
+ [["ppt", "pptx", "dps", "odp"], "PPT"],
56
+ [["zip", "rar", "7z", "gz", "tgz", "tar", "bz2", "xz"], "压缩包"],
57
+ [["txt", "md", "markdown", "log"], "文本"],
58
+ ];
59
+
60
+ /** 文件列表「类型」列的显示文字。 */
61
+ export function fileTypeLabel(name: string, contentType = ""): string {
62
+ const ext = fileExtension(name);
63
+ for (const [exts, label] of TYPE_LABELS) if (exts.includes(ext)) return label;
64
+ const kind = previewKindOf({ name, content_type: contentType });
65
+ if (kind === "image") return "图片";
66
+ if (kind === "video") return "视频";
67
+ if (kind === "audio") return "音频";
68
+ if (kind === "text") return "文本";
69
+ return ext ? ext.toUpperCase() : "文件";
70
+ }
71
+
72
+ /** 字节数转成 B / KB / MB / GB,保留一位小数(整数不带 .0)。 */
73
+ export function formatFileSize(bytes: number | null | undefined): string {
74
+ const value = Number(bytes);
75
+ if (!Number.isFinite(value) || value < 0) return "";
76
+ if (value < 1024) return `${value} B`;
77
+ const units = ["KB", "MB", "GB", "TB"];
78
+ let size = value / 1024;
79
+ let unit = 0;
80
+ while (size >= 1024 && unit < units.length - 1) {
81
+ size /= 1024;
82
+ unit += 1;
83
+ }
84
+ const text = size >= 100 ? size.toFixed(0) : size.toFixed(1).replace(/\.0$/, "");
85
+ return `${text} ${units[unit]}`;
86
+ }
87
+
88
+ /** 把 Blob 存成本地文件。 */
89
+ export function saveBlob(blob: Blob, name: string): void {
90
+ const url = URL.createObjectURL(blob);
91
+ const link = document.createElement("a");
92
+ link.href = url;
93
+ link.download = name || "download";
94
+ link.rel = "noopener";
95
+ document.body.appendChild(link);
96
+ link.click();
97
+ link.remove();
98
+ // 留点时间给浏览器开始下载再回收。
99
+ window.setTimeout(() => URL.revokeObjectURL(url), 60_000);
100
+ }
package/src/index.ts CHANGED
@@ -21,6 +21,19 @@ export { default as YsPortalGrid } from "./components/YsPortalGrid.vue";
21
21
  export { default as YsRoleTag } from "./components/YsRoleTag.vue";
22
22
  export { default as YsBoolMark } from "./components/YsBoolMark.vue";
23
23
  export { default as YsSegmented } from "./components/YsSegmented.vue";
24
+ export { default as YsCountBadge } from "./components/YsCountBadge.vue";
25
+ export { default as YsFilePreview } from "./components/YsFilePreview.vue";
26
+ export {
27
+ previewKindOf,
28
+ sniffPreviewKind,
29
+ fileExtension,
30
+ fileTypeLabel,
31
+ formatFileSize,
32
+ saveBlob,
33
+ TEXT_PREVIEW_LIMIT,
34
+ type YsPreviewFile,
35
+ type YsPreviewKind,
36
+ } from "./filePreview";
24
37
  export { enumOptions, enumLabel, type EnumOption } from "./tokens-runtime";
25
38
  export {
26
39
  registerColumnWidths,