@ysgroup/ui 0.1.53 → 0.1.55

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.53",
3
+ "version": "0.1.55",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -11,8 +11,8 @@
11
11
  },
12
12
  "peerDependencies": {
13
13
  "@ysgroup/contracts": ">=0.1.50",
14
- "@ysgroup/core": ">=0.1.29",
15
- "@ysgroup/theme": ">=0.3.2",
14
+ "@ysgroup/core": ">=0.1.30",
15
+ "@ysgroup/theme": ">=0.3.3",
16
16
  "element-plus": ">=2.9",
17
17
  "vue": ">=3.5"
18
18
  },
@@ -26,8 +26,8 @@
26
26
  },
27
27
  "devDependencies": {
28
28
  "@ysgroup/contracts": "^0.1.51",
29
- "@ysgroup/core": "^0.1.29",
30
- "@ysgroup/theme": "^0.3.2",
29
+ "@ysgroup/core": "^0.1.30",
30
+ "@ysgroup/theme": "^0.3.3",
31
31
  "docx-preview": "^0.4.1"
32
32
  }
33
33
  }
@@ -0,0 +1,63 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * 表格上方的状态勾选框:「显示已停用」「显示已归档」「显示软删除」,放在 YsTableToolbar 的下拉框后面。
4
+ * - 列表默认只显示启用的记录;勾一个就只看这一类,三者互斥,取消勾选回到启用(v-model 是当前查看的状态)。
5
+ * - 传 counts 时每个勾选框后面带数量,不勾也知道有几条被隐藏;为 0 时数字变淡。
6
+ * - options 只列表里有的字段:没有启用字段的表去掉 disabled,不能归档的去掉 archived。
7
+ * 判断、计数和提示文字见 @ysgroup/core 的 ysRecordStatus、ysStatusCounts、ysStatusRevealHint、ysStatusEmptyText。
8
+ */
9
+ import {
10
+ YS_STATUS_LABELS,
11
+ YS_STATUS_OPTIONS,
12
+ ysStatusToggle,
13
+ type YsRecordStatus,
14
+ type YsStatusCounts,
15
+ type YsStatusOption,
16
+ } from "@ysgroup/core";
17
+
18
+ withDefaults(
19
+ defineProps<{
20
+ modelValue: YsRecordStatus;
21
+ counts?: YsStatusCounts | null;
22
+ options?: readonly YsStatusOption[];
23
+ }>(),
24
+ { counts: null, options: () => YS_STATUS_OPTIONS },
25
+ );
26
+
27
+ const emit = defineEmits<{ "update:modelValue": [value: YsRecordStatus] }>();
28
+
29
+ function order(options: readonly YsStatusOption[]): YsStatusOption[] {
30
+ return YS_STATUS_OPTIONS.filter((option) => options.includes(option));
31
+ }
32
+ </script>
33
+
34
+ <template>
35
+ <el-checkbox
36
+ v-for="option in order(options)"
37
+ :key="option"
38
+ class="ys-status-filter"
39
+ :model-value="modelValue === option"
40
+ @update:model-value="
41
+ emit('update:modelValue', ysStatusToggle(option, Boolean($event)))
42
+ "
43
+ >
44
+ {{ YS_STATUS_LABELS[option]
45
+ }}<span
46
+ v-if="counts"
47
+ class="ys-status-filter__count"
48
+ :class="{ 'is-zero': !counts[option] }"
49
+ >{{ counts[option] }}</span
50
+ >
51
+ </el-checkbox>
52
+ </template>
53
+
54
+ <style scoped>
55
+ .ys-status-filter__count {
56
+ margin-left: 4px;
57
+ color: var(--ys-color-text-2);
58
+ font-variant-numeric: tabular-nums;
59
+ }
60
+ .ys-status-filter__count.is-zero {
61
+ color: var(--ys-color-text-3);
62
+ }
63
+ </style>
@@ -1,5 +1,6 @@
1
1
  <!-- YsUploadQueue —— 上传队列面板(配 useYsUploadQueue)。
2
- - 「待上传」:列出会怎么处理(无损转 WebP / 原样保存 / 转码 / 限制打开),能转换的图片可勾「原图」,有「全部原图」;
2
+ - 「待上传」:每个文件写明会按统一文件规范怎么处理(YS_FILE_RULES),悬停「为什么」看原因和建议;默认会被转换或转码的
3
+ 可勾「保留原文件」原样保存,多个时有「全部保留原文件」;视频这类在特定场景建议保留原文件的,醒目写出建议;
3
4
  立即上传模式下点「上传 N 个」开始,待提交模式(staged)显示「提交后上传」、不出确认按钮。
4
5
  - 进行中:校验中 / 上传中(从 N% 续传)/ 处理中;完成:秒传完成 / 已上传,带服务端的文件标签;
5
6
  已中断显示「继续」,失败显示原因和「重试」,移除会清掉服务端半截的上传。
@@ -7,8 +8,13 @@
7
8
  <script setup lang="ts" generic="T extends Record<string, unknown>">
8
9
  import { computed } from "vue";
9
10
  import { fileExtension, formatFileSize } from "../filePreview";
11
+ import { vTip } from "../vTip";
10
12
  import type { YsFileNote } from "../fileRules";
11
- import { uploadStateText, type YsUploadItem, type YsUploadQueue } from "../uploadQueue";
13
+ import {
14
+ uploadStateText,
15
+ type YsUploadItem,
16
+ type YsUploadQueue,
17
+ } from "../uploadQueue";
12
18
  import YsFileNotes from "./YsFileNotes.vue";
13
19
 
14
20
  const props = withDefaults(
@@ -23,17 +29,51 @@ const props = withDefaults(
23
29
  );
24
30
 
25
31
  const items = computed(() =>
26
- props.showDone ? props.queue.items.value : props.queue.items.value.filter((item) => item.state !== "done"),
32
+ props.showDone
33
+ ? props.queue.items.value
34
+ : props.queue.items.value.filter((item) => item.state !== "done"),
27
35
  );
28
36
  const pending = computed(() => props.queue.pending.value);
29
- const convertible = computed(() => pending.value.filter((item) => item.handling.canKeepOriginal));
30
- const allAsIs = computed(() => convertible.value.length > 0 && convertible.value.every((item) => item.asIs));
31
- const someAsIs = computed(() => convertible.value.some((item) => item.asIs) && !allAsIs.value);
32
- const pendingSize = computed(() => pending.value.reduce((sum, item) => sum + item.file.size, 0));
37
+ const convertible = computed(() =>
38
+ pending.value.filter((item) => item.handling.canKeepOriginal),
39
+ );
40
+ const allAsIs = computed(
41
+ () =>
42
+ convertible.value.length > 0 &&
43
+ convertible.value.every((item) => item.asIs),
44
+ );
45
+ const someAsIs = computed(
46
+ () => convertible.value.some((item) => item.asIs) && !allAsIs.value,
47
+ );
48
+ const confirmHint = computed(
49
+ () =>
50
+ "上传后按统一文件规范处理,每个文件下面写着会怎么处理,悬停「为什么」看原因。" +
51
+ (convertible.value.length ? "需要原样保存的,勾选「保留原文件」。" : ""),
52
+ );
53
+ const pendingSize = computed(() =>
54
+ pending.value.reduce((sum, item) => sum + item.file.size, 0),
55
+ );
33
56
 
34
57
  function nameParts(name: string): [string, string] {
35
58
  const ext = fileExtension(name);
36
- return ext ? [name.slice(0, -(ext.length + 1)), `.${name.slice(-ext.length)}`] : [name, ""];
59
+ return ext
60
+ ? [name.slice(0, -(ext.length + 1)), `.${name.slice(-ext.length)}`]
61
+ : [name, ""];
62
+ }
63
+
64
+ /** 「为什么」悬停文字:原因,加上建议(有的话)。 */
65
+ function whyText(item: YsUploadItem<T>): string {
66
+ const lines = [item.handling.detail];
67
+ if (item.handling.advice) lines.push(`建议:${item.handling.advice}`);
68
+ return lines.filter(Boolean).join("\n");
69
+ }
70
+
71
+ /** 进行中的行在状态后面补一句会怎么处理(只对默认会被转换、转码的)。 */
72
+ function plannedText(item: YsUploadItem<T>): string {
73
+ if (!item.handling.canKeepOriginal) return "";
74
+ return item.asIs
75
+ ? "保留原文件"
76
+ : `将${item.handling.label.replace(/^后台/, "在后台")}`;
37
77
  }
38
78
 
39
79
  function badge(item: YsUploadItem<T>): string {
@@ -50,56 +90,119 @@ function resultName(item: YsUploadItem<T>): string {
50
90
  return typeof name === "string" && name !== item.file.name ? name : "";
51
91
  }
52
92
 
53
- const progressing = (item: YsUploadItem<T>) => item.state === "hashing" || item.state === "uploading";
93
+ const progressing = (item: YsUploadItem<T>) =>
94
+ item.state === "hashing" || item.state === "uploading";
54
95
  </script>
55
96
 
56
97
  <template>
57
- <div v-if="items.length || queue.notices.value.length" class="ys-upload-queue">
58
- <div v-for="(text, index) in queue.notices.value" :key="`${index}-${text}`" class="ys-upload-queue__notice" role="status">
98
+ <div
99
+ v-if="items.length || queue.notices.value.length"
100
+ class="ys-upload-queue"
101
+ >
102
+ <div
103
+ v-for="(text, index) in queue.notices.value"
104
+ :key="`${index}-${text}`"
105
+ class="ys-upload-queue__notice"
106
+ role="status"
107
+ >
59
108
  <span>{{ text }}</span>
60
- <button type="button" class="ys-upload-queue__close" aria-label="关闭提示" @click="queue.dismissNotice(index)">×</button>
109
+ <button
110
+ type="button"
111
+ class="ys-upload-queue__close"
112
+ aria-label="关闭提示"
113
+ @click="queue.dismissNotice(index)"
114
+ >
115
+ ×
116
+ </button>
61
117
  </div>
62
118
 
63
119
  <div v-if="pending.length" class="ys-upload-queue__confirm">
64
- <div class="ys-upload-queue__confirm-head">
65
- <strong>待上传 {{ pending.length }} 个</strong>
66
- <span class="ys-upload-queue__muted">
67
- {{ staged ? "提交后上传。" : "" }}无损图片默认转为无损 WebP(逐像素一致、体积更小),勾「原图」则原样保存
68
- </span>
69
- <el-checkbox
70
- v-if="convertible.length > 1"
71
- :model-value="allAsIs"
72
- :indeterminate="someAsIs"
73
- @change="queue.setAllAsIs(Boolean($event))"
120
+ <div class="ys-upload-queue__confirm-text">
121
+ <strong
122
+ >待上传 {{ pending.length }} 个{{
123
+ staged ? ",提交后上传" : ""
124
+ }}</strong
74
125
  >
75
- 全部原图
76
- </el-checkbox>
126
+ <p>{{ confirmHint }}</p>
77
127
  </div>
128
+ <el-checkbox
129
+ v-if="convertible.length > 1"
130
+ :model-value="allAsIs"
131
+ :indeterminate="someAsIs"
132
+ @change="queue.setAllAsIs(Boolean($event))"
133
+ >
134
+ 全部保留原文件
135
+ </el-checkbox>
78
136
  </div>
79
137
 
80
138
  <ul class="ys-upload-queue__list">
81
- <li v-for="item in items" :key="item.key" class="ys-upload-queue__row" :class="`is-${item.state}`">
82
- <span class="ys-upload-queue__badge" aria-hidden="true">{{ badge(item) }}</span>
139
+ <li
140
+ v-for="item in items"
141
+ :key="item.key"
142
+ class="ys-upload-queue__row"
143
+ :class="`is-${item.state}`"
144
+ >
145
+ <span class="ys-upload-queue__badge" aria-hidden="true">{{
146
+ badge(item)
147
+ }}</span>
83
148
  <div class="ys-upload-queue__main">
84
149
  <div class="ys-upload-queue__line">
85
150
  <span class="ys-upload-queue__name" :title="item.file.name">
86
- <span class="ys-upload-queue__stem">{{ nameParts(item.file.name)[0] }}</span><span>{{ nameParts(item.file.name)[1] }}</span>
151
+ <span class="ys-upload-queue__stem">{{
152
+ nameParts(item.file.name)[0]
153
+ }}</span
154
+ ><span>{{ nameParts(item.file.name)[1] }}</span>
87
155
  </span>
88
- <span class="ys-upload-queue__muted">{{ formatFileSize(item.file.size) }}</span>
156
+ <span class="ys-upload-queue__muted">{{
157
+ formatFileSize(item.file.size)
158
+ }}</span>
89
159
  </div>
90
160
  <div class="ys-upload-queue__line is-sub">
91
161
  <template v-if="item.state === 'pending'">
92
- <span class="ys-upload-queue__handling" :class="`is-${item.asIs ? 'keep' : item.handling.action}`">
93
- {{ item.asIs ? "原样保存" : item.handling.label }}
162
+ <span
163
+ class="ys-upload-queue__handling"
164
+ :class="`is-${item.asIs ? 'keep' : item.handling.action}`"
165
+ >
166
+ {{ item.asIs ? "保留原文件,原样保存" : item.handling.label }}
94
167
  </span>
95
- <span v-if="!item.asIs && item.handling.detail" class="ys-upload-queue__muted">{{ item.handling.detail }}</span>
168
+ <span
169
+ v-tip="whyText(item)"
170
+ class="ys-upload-queue__why"
171
+ tabindex="0"
172
+ >为什么</span
173
+ >
96
174
  </template>
97
175
  <template v-else>
98
- <span class="ys-upload-queue__state">{{ uploadStateText(item) }}</span>
99
- <span v-if="item.state === 'done' && resultName(item)" class="ys-upload-queue__muted">保存为 {{ resultName(item) }}</span>
100
- <YsFileNotes v-if="item.state === 'done'" :notes="resultNotes(item)" :max="2" />
176
+ <span class="ys-upload-queue__state">{{
177
+ uploadStateText(item)
178
+ }}</span>
179
+ <span
180
+ v-if="item.state !== 'done' && plannedText(item)"
181
+ class="ys-upload-queue__muted"
182
+ >· {{ plannedText(item) }}</span
183
+ >
184
+ <span
185
+ v-if="item.state === 'done' && resultName(item)"
186
+ class="ys-upload-queue__muted"
187
+ >保存为 {{ resultName(item) }}</span
188
+ >
189
+ <YsFileNotes
190
+ v-if="item.state === 'done'"
191
+ :notes="resultNotes(item)"
192
+ :max="2"
193
+ />
101
194
  </template>
102
195
  </div>
196
+ <p
197
+ v-if="
198
+ item.state === 'pending' &&
199
+ item.handling.recommendKeep &&
200
+ !item.asIs
201
+ "
202
+ class="ys-upload-queue__advice"
203
+ >
204
+ 建议:{{ item.handling.advice }}
205
+ </p>
103
206
  <el-progress
104
207
  v-if="progressing(item)"
105
208
  :percentage="Math.round(item.progress * 100)"
@@ -113,17 +216,35 @@ const progressing = (item: YsUploadItem<T>) => item.state === "hashing" || item.
113
216
  :model-value="item.asIs"
114
217
  @change="queue.setAsIs(item, Boolean($event))"
115
218
  >
116
- 原图
219
+ 保留原文件
117
220
  </el-checkbox>
118
- <el-button v-if="item.state === 'paused'" link type="primary" @click="queue.retry(item)">继续</el-button>
119
- <el-button v-if="item.state === 'failed'" link type="primary" @click="queue.retry(item)">重试</el-button>
221
+ <el-button
222
+ v-if="item.state === 'paused'"
223
+ link
224
+ type="primary"
225
+ @click="queue.retry(item)"
226
+ >继续</el-button
227
+ >
228
+ <el-button
229
+ v-if="item.state === 'failed'"
230
+ link
231
+ type="primary"
232
+ @click="queue.retry(item)"
233
+ >重试</el-button
234
+ >
120
235
  <el-button
121
236
  v-if="item.state !== 'done' && item.state !== 'processing'"
122
237
  link
123
238
  :type="item.state === 'paused' ? 'danger' : undefined"
124
239
  @click="queue.remove(item)"
125
240
  >
126
- {{ item.state === "hashing" || item.state === "uploading" ? "取消" : item.state === "paused" ? "放弃" : "移除" }}
241
+ {{
242
+ item.state === "hashing" || item.state === "uploading"
243
+ ? "取消"
244
+ : item.state === "paused"
245
+ ? "放弃"
246
+ : "移除"
247
+ }}
127
248
  </el-button>
128
249
  </div>
129
250
  </li>
@@ -131,15 +252,29 @@ const progressing = (item: YsUploadItem<T>) => item.state === "hashing" || item.
131
252
 
132
253
  <div class="ys-upload-queue__foot">
133
254
  <template v-if="pending.length && !staged">
134
- <span class="ys-upload-queue__muted">共 {{ pending.length }} 个 · {{ formatFileSize(pendingSize) }}</span>
255
+ <span class="ys-upload-queue__muted"
256
+ >共 {{ pending.length }} 个 · {{ formatFileSize(pendingSize) }}</span
257
+ >
135
258
  <el-button size="small" @click="queue.cancelPending()">取消</el-button>
136
- <el-button size="small" type="primary" @click="queue.start()">上传 {{ pending.length }} 个</el-button>
259
+ <el-button size="small" type="primary" @click="queue.start()"
260
+ >上传 {{ pending.length }} 个</el-button
261
+ >
137
262
  </template>
138
263
  <template v-else>
139
- <el-button v-if="queue.paused.value.length + queue.failed.value.length > 1" size="small" @click="queue.retryAll()">
264
+ <el-button
265
+ v-if="queue.paused.value.length + queue.failed.value.length > 1"
266
+ size="small"
267
+ @click="queue.retryAll()"
268
+ >
140
269
  全部继续
141
270
  </el-button>
142
- <el-button v-if="queue.done.value.length" size="small" link @click="queue.clearDone()">清除已完成</el-button>
271
+ <el-button
272
+ v-if="queue.done.value.length"
273
+ size="small"
274
+ link
275
+ @click="queue.clearDone()"
276
+ >清除已完成</el-button
277
+ >
143
278
  </template>
144
279
  </div>
145
280
  </div>
@@ -162,7 +297,11 @@ const progressing = (item: YsUploadItem<T>) => item.state === "hashing" || item.
162
297
  justify-content: space-between;
163
298
  padding: 6px 10px;
164
299
  border-radius: var(--ys-radius-s);
165
- color: color-mix(in srgb, var(--ys-color-warning) 80%, var(--ys-color-text-1));
300
+ color: color-mix(
301
+ in srgb,
302
+ var(--ys-color-warning) 80%,
303
+ var(--ys-color-text-1)
304
+ );
166
305
  background: color-mix(in srgb, var(--ys-color-warning) 10%, transparent);
167
306
  font-size: 13px;
168
307
  }
@@ -174,11 +313,50 @@ const progressing = (item: YsUploadItem<T>) => item.state === "hashing" || item.
174
313
  line-height: 1;
175
314
  cursor: pointer;
176
315
  }
177
- .ys-upload-queue__confirm-head {
316
+ .ys-upload-queue__confirm {
178
317
  display: flex;
179
318
  flex-wrap: wrap;
180
- gap: 4px 12px;
319
+ gap: 6px 16px;
181
320
  align-items: center;
321
+ justify-content: space-between;
322
+ padding: 8px 10px;
323
+ border-radius: var(--ys-radius-s);
324
+ background: color-mix(in srgb, var(--ys-color-primary) 6%, transparent);
325
+ }
326
+ .ys-upload-queue__confirm-text {
327
+ flex: 1 1 320px;
328
+ min-width: 0;
329
+ }
330
+ .ys-upload-queue__confirm-text strong {
331
+ color: var(--ys-color-text-1);
332
+ font-size: 13px;
333
+ }
334
+ .ys-upload-queue__confirm-text p {
335
+ margin: 2px 0 0;
336
+ color: var(--ys-color-text-2);
337
+ font-size: 12px;
338
+ line-height: 1.6;
339
+ }
340
+ .ys-upload-queue__why {
341
+ color: var(--ys-color-primary);
342
+ font-size: 12px;
343
+ text-decoration: underline dotted;
344
+ text-underline-offset: 3px;
345
+ cursor: help;
346
+ }
347
+ .ys-upload-queue__why:focus-visible {
348
+ outline: var(--ys-focus-outline);
349
+ outline-offset: 1px;
350
+ }
351
+ .ys-upload-queue__advice {
352
+ margin: 2px 0 0;
353
+ color: color-mix(
354
+ in srgb,
355
+ var(--ys-color-warning) 80%,
356
+ var(--ys-color-text-1)
357
+ );
358
+ font-size: 12px;
359
+ line-height: 1.6;
182
360
  }
183
361
  .ys-upload-queue__list {
184
362
  display: flex;
@@ -249,7 +427,11 @@ const progressing = (item: YsUploadItem<T>) => item.state === "hashing" || item.
249
427
  color: var(--ys-color-danger);
250
428
  }
251
429
  .is-paused .ys-upload-queue__state {
252
- color: color-mix(in srgb, var(--ys-color-warning) 80%, var(--ys-color-text-1));
430
+ color: color-mix(
431
+ in srgb,
432
+ var(--ys-color-warning) 80%,
433
+ var(--ys-color-text-1)
434
+ );
253
435
  }
254
436
  .is-done .ys-upload-queue__state {
255
437
  color: var(--ys-color-success);
package/src/fileRules.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /* 文件规范(前端部分):上传前本地检查、可执行类型判断、处理方式预估、粘贴与拖入的识别、文件标签类型。
2
2
  * 服务端的权威规则在 ys_base.media(真实类型识别、无损转 WebP、视频转码、打标签);这里只做「上传前就能知道」的部分,
3
- * 两边口径一致:文件名不超过 120 个字符、不含控制字符,可内嵌脚本的类型限制打开(不给新标签页打开),无损图片转无损 WebP、有损图片原样保存。 */
3
+ * 两边口径一致:文件名不超过 120 个字符、不含控制字符,可内嵌脚本的类型限制打开(不给新标签页打开);各类文件默认怎么处理见 YS_FILE_RULES。 */
4
4
 
5
5
  import { fileExtension } from "./filePreview";
6
6
 
@@ -8,8 +8,21 @@ import { fileExtension } from "./filePreview";
8
8
  export const FILE_NAME_MAX = 120;
9
9
 
10
10
  /** 能内嵌脚本的扩展名:页面里不提供「新标签页打开」,服务端打「可内嵌脚本」标签。与 ys_base.media.ACTIVE_EXTENSIONS 一致。 */
11
- export const ACTIVE_CONTENT_EXT = new Set(["svg", "svgz", "html", "htm", "xhtml", "xht", "xml", "xsl", "xslt", "js", "mjs"]);
12
- const ACTIVE_MIME = /^(text\/html|application\/(xhtml\+xml|xml|javascript|ecmascript)|text\/(xml|javascript|ecmascript)|image\/svg\+xml)\b/i;
11
+ export const ACTIVE_CONTENT_EXT = new Set([
12
+ "svg",
13
+ "svgz",
14
+ "html",
15
+ "htm",
16
+ "xhtml",
17
+ "xht",
18
+ "xml",
19
+ "xsl",
20
+ "xslt",
21
+ "js",
22
+ "mjs",
23
+ ]);
24
+ const ACTIVE_MIME =
25
+ /^(text\/html|application\/(xhtml\+xml|xml|javascript|ecmascript)|text\/(xml|javascript|ecmascript)|image\/svg\+xml)\b/i;
13
26
 
14
27
  /** 文件标签(服务端 ys_base.files.file_media_fields 展开后的 notes 一项)。 */
15
28
  export type YsFileNote = {
@@ -35,9 +48,16 @@ export type YsFileMediaFields = {
35
48
  };
36
49
 
37
50
  /** 可内嵌脚本的文件:按扩展名或 MIME 判断。trusted 为真(系统自带素材)时不算。 */
38
- export function isActiveContent(file: { name: string; content_type?: string; trusted?: boolean }): boolean {
51
+ export function isActiveContent(file: {
52
+ name: string;
53
+ content_type?: string;
54
+ trusted?: boolean;
55
+ }): boolean {
39
56
  if (file.trusted) return false;
40
- return ACTIVE_CONTENT_EXT.has(fileExtension(file.name)) || ACTIVE_MIME.test(String(file.content_type || ""));
57
+ return (
58
+ ACTIVE_CONTENT_EXT.has(fileExtension(file.name)) ||
59
+ ACTIVE_MIME.test(String(file.content_type || ""))
60
+ );
41
61
  }
42
62
 
43
63
  /** 按码点数文件名长度("😀" 是 1 个字符,与 Python len 一致)。 */
@@ -55,7 +75,10 @@ export type YsFileCheckOptions = {
55
75
  * 上传前本地检查(与服务端 begin 的检查同口径),不通过返回原因,通过返回空字符串。
56
76
  * 顺序:空文件 → 超过大小 → 文件名为空 → 超长 → 控制字符。
57
77
  */
58
- export function checkUploadFile(file: { name: string; size: number }, options: YsFileCheckOptions = {}): string {
78
+ export function checkUploadFile(
79
+ file: { name: string; size: number },
80
+ options: YsFileCheckOptions = {},
81
+ ): string {
59
82
  const maxSize = options.maxSize ?? 50 * 1024 * 1024;
60
83
  const maxName = options.maxName ?? FILE_NAME_MAX;
61
84
  const name = String(file.name || "").trim();
@@ -63,46 +86,312 @@ export function checkUploadFile(file: { name: string; size: number }, options: Y
63
86
  if (file.size > maxSize) return `超过 ${sizeLimitText(maxSize)}`;
64
87
  if (!name || name === "." || name === "..") return "文件名不能为空";
65
88
  if (fileNameLength(name) > maxName) return `文件名超过 ${maxName} 个字符`;
66
- if ([...name].some((char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127)) return "文件名含换行等控制字符";
89
+ if (
90
+ [...name].some(
91
+ (char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127,
92
+ )
93
+ )
94
+ return "文件名含换行等控制字符";
67
95
  return "";
68
96
  }
69
97
 
70
98
  function sizeLimitText(bytes: number): string {
71
99
  const mb = bytes / 1024 / 1024;
72
- return mb >= 1 ? `${Number.isInteger(mb) ? mb : mb.toFixed(1)} MB` : `${Math.round(bytes / 1024)} KB`;
100
+ return mb >= 1
101
+ ? `${Number.isInteger(mb) ? mb : mb.toFixed(1)} MB`
102
+ : `${Math.round(bytes / 1024)} KB`;
73
103
  }
74
104
 
105
+ /**
106
+ * 统一文件规范:每类文件默认怎么处理、为什么、能不能保留原文件、有什么建议。
107
+ * 这是规范本身(与服务端 ys_base.media 同口径),上传面板、文件标签说明和各系统的说明页都从这里取,不各写一份。
108
+ * - action:convert 无损转换、video 视频转码、keep 原样保存、active 原样保存但限制打开。
109
+ * - keepOriginal:choice 默认按规范处理、用户可以勾「保留原文件」原样保存;always 规范本来就原样保存。
110
+ * - advice:建议。recommendKeep 为真时是「这类文件在某些场景下建议保留原文件」,上传面板醒目提示;
111
+ * 其余的建议只在「为什么」里给,不向所有人推。
112
+ */
113
+ export type YsFileRule = {
114
+ key: string;
115
+ /** 类别名,如「无损图片」。 */
116
+ title: string;
117
+ /** 常见格式(显示用)。 */
118
+ formats: string[];
119
+ action: "convert" | "keep" | "video" | "active";
120
+ /** 默认处理(短句)。 */
121
+ standard: string;
122
+ /** 为什么这样处理。 */
123
+ why: string;
124
+ keepOriginal: "choice" | "always";
125
+ advice?: string;
126
+ recommendKeep?: boolean;
127
+ };
128
+
129
+ export const YS_FILE_RULES: YsFileRule[] = [
130
+ {
131
+ key: "lossless-image",
132
+ title: "无损图片",
133
+ formats: ["PNG", "BMP", "静态 GIF"],
134
+ action: "convert",
135
+ standard: "转为无损 WebP",
136
+ why: "无损转换:每个像素都和原图一致,拍摄信息(EXIF)、色彩配置(ICC)、XMP 一并保留,体积通常只有原来的三到七成;转换后逐像素核对,不一致或没变小就保留原格式。",
137
+ keepOriginal: "choice",
138
+ advice:
139
+ "需要和原文件逐字节一致时(要核对文件哈希、交给只认 PNG 的外部系统、设计源文件)勾选「保留原文件」。",
140
+ },
141
+ {
142
+ key: "animated-image",
143
+ title: "动图",
144
+ formats: ["动态 GIF", "APNG"],
145
+ action: "keep",
146
+ standard: "原样保存",
147
+ why: "逐帧转换容易出差错,体积收益也不稳定,规范不转。",
148
+ keepOriginal: "always",
149
+ },
150
+ {
151
+ key: "lossy-image",
152
+ title: "照片(有损)",
153
+ formats: ["JPEG"],
154
+ action: "keep",
155
+ standard: "原样保存",
156
+ why: "JPEG 已经丢过一次细节,再编码只会更糊、不会更小;原样保存最安全。",
157
+ keepOriginal: "always",
158
+ },
159
+ {
160
+ key: "modern-image",
161
+ title: "高效图片",
162
+ formats: ["WebP", "AVIF", "JPEG XL"],
163
+ action: "keep",
164
+ standard: "原样保存",
165
+ why: "已经是现代高效格式,再转省不了多少。",
166
+ keepOriginal: "always",
167
+ },
168
+ {
169
+ key: "heic",
170
+ title: "苹果照片",
171
+ formats: ["HEIC", "HEIF"],
172
+ action: "keep",
173
+ standard: "原样保存",
174
+ why: "iPhone 默认格式,体积小,但多数浏览器不能显示;转码会再损失画质,规范不转,打标签提醒。",
175
+ keepOriginal: "always",
176
+ advice:
177
+ "需要在线预览的,手机「设置 › 相机 › 格式」选「兼容性最佳」后再拍,或导出为 JPEG 再上传。",
178
+ },
179
+ {
180
+ key: "tiff",
181
+ title: "扫描与印刷图",
182
+ formats: ["TIFF"],
183
+ action: "keep",
184
+ standard: "原样保存",
185
+ why: "常带多页和印刷专用标签,转换会丢信息。",
186
+ keepOriginal: "always",
187
+ advice: "扫描件建议直接扫成 PDF,多页合在一个文件里,浏览器也能预览。",
188
+ },
189
+ {
190
+ key: "video",
191
+ title: "视频",
192
+ formats: ["MP4", "MOV", "AVI", "MKV", "WMV"],
193
+ action: "video",
194
+ standard: "后台转为 {codec} MP4",
195
+ why: "H.264 等老编码转 {codec}:同样清晰,体积通常省一半以上,浏览器能直接播放;已经是 H.265、AV1、VP9 或 HDR 的原样保存。转码是有损的(肉眼难分),转好之前播放原视频。",
196
+ keepOriginal: "choice",
197
+ advice:
198
+ "作为证据、还要再剪辑、或要给老设备播放的视频,勾选「保留原文件」原样保存。",
199
+ recommendKeep: true,
200
+ },
201
+ {
202
+ key: "audio",
203
+ title: "音频",
204
+ formats: ["MP3", "M4A", "AMR", "WAV"],
205
+ action: "keep",
206
+ standard: "原样保存",
207
+ why: "语音编码本来就很小,再转没有意义。",
208
+ keepOriginal: "always",
209
+ },
210
+ {
211
+ key: "document",
212
+ title: "文档",
213
+ formats: ["PDF", "OFD", "Word", "Excel", "PPT", "CSV", "TXT"],
214
+ action: "keep",
215
+ standard: "原样保存",
216
+ why: "内部已经压缩过;电子发票、合同等带签章,改一个字节验签就失败。",
217
+ keepOriginal: "always",
218
+ },
219
+ {
220
+ key: "archive",
221
+ title: "压缩包",
222
+ formats: ["ZIP", "RAR", "7Z"],
223
+ action: "keep",
224
+ standard: "原样保存",
225
+ why: "已经压缩过,再压没有收益;不解开检查里面的内容。",
226
+ keepOriginal: "always",
227
+ },
228
+ {
229
+ key: "active",
230
+ title: "可内嵌脚本的文件",
231
+ formats: ["SVG", "HTML", "XML", "JS"],
232
+ action: "active",
233
+ standard: "原样保存,限制打开",
234
+ why: "可以内嵌脚本:页面里只按图片或源码显示、不在新标签页打开(同源打开时脚本会以登录用户的身份运行),并打「不符合规范」标签。",
235
+ keepOriginal: "always",
236
+ advice: "图形导出为 PNG 或 PDF,网页打印为 PDF 后再上传。",
237
+ },
238
+ {
239
+ key: "other",
240
+ title: "其他文件",
241
+ formats: [],
242
+ action: "keep",
243
+ standard: "原样保存",
244
+ why: "规范对这类文件不做转换,只检查文件名、大小和真实类型。",
245
+ keepOriginal: "always",
246
+ },
247
+ ];
248
+
75
249
  /** 上传前预估的处理方式(服务端按真实内容判断,结果以上传后的标签为准)。 */
76
250
  export type YsExpectedHandling = {
77
251
  /** convert 无损转 WebP、keep 原样保存、video 后台转码、active 可内嵌脚本、限制打开。 */
78
- action: "convert" | "keep" | "video" | "active";
252
+ action: YsFileRule["action"];
253
+ /** 默认处理(rule.standard,已代入视频编码名)。 */
79
254
  label: string;
255
+ /** 为什么(rule.why,已代入视频编码名)。 */
80
256
  detail: string;
81
- /** 能否勾「原图」(只有会被转换的才有意义)。 */
257
+ /** 能否勾「保留原文件」(只有默认会被转换或转码的才有意义)。 */
82
258
  canKeepOriginal: boolean;
259
+ advice: string;
260
+ /** 这类文件在某些场景下建议保留原文件(advice 醒目显示)。 */
261
+ recommendKeep: boolean;
262
+ rule: YsFileRule;
83
263
  };
84
264
 
85
265
  const LOSSLESS_EXT = new Set(["png", "bmp", "dib"]);
86
- const LOSSY_EXT = new Set(["jpg", "jpeg", "jpe", "jfif", "heic", "heif"]);
266
+ const LOSSY_EXT = new Set(["jpg", "jpeg", "jpe", "jfif"]);
267
+ const HEIC_EXT = new Set(["heic", "heif"]);
87
268
  const MODERN_EXT = new Set(["webp", "avif", "jxl"]);
88
- const VIDEO_EXT = new Set(["mp4", "mov", "m4v", "avi", "mkv", "webm", "flv", "wmv", "3gp", "mts", "m2ts", "ts"]);
269
+ const TIFF_EXT = new Set(["tif", "tiff"]);
270
+ const VIDEO_EXT = new Set([
271
+ "mp4",
272
+ "mov",
273
+ "m4v",
274
+ "avi",
275
+ "mkv",
276
+ "webm",
277
+ "flv",
278
+ "wmv",
279
+ "3gp",
280
+ "mts",
281
+ "m2ts",
282
+ "ts",
283
+ ]);
284
+ const AUDIO_EXT = new Set([
285
+ "mp3",
286
+ "m4a",
287
+ "aac",
288
+ "amr",
289
+ "wav",
290
+ "flac",
291
+ "ogg",
292
+ "opus",
293
+ "wma",
294
+ ]);
295
+ const DOCUMENT_EXT = new Set([
296
+ "pdf",
297
+ "ofd",
298
+ "doc",
299
+ "docx",
300
+ "xls",
301
+ "xlsx",
302
+ "xlsm",
303
+ "xlsb",
304
+ "ods",
305
+ "ppt",
306
+ "pptx",
307
+ "odt",
308
+ "odp",
309
+ "csv",
310
+ "txt",
311
+ "md",
312
+ "rtf",
313
+ "wps",
314
+ "et",
315
+ "dps",
316
+ ]);
317
+ const ARCHIVE_EXT = new Set([
318
+ "zip",
319
+ "rar",
320
+ "7z",
321
+ "tar",
322
+ "gz",
323
+ "tgz",
324
+ "bz2",
325
+ "xz",
326
+ ]);
327
+
328
+ function ruleKeyOf(ext: string, mime: string, active: boolean): string {
329
+ if (active) return "active";
330
+ if (
331
+ LOSSLESS_EXT.has(ext) ||
332
+ mime === "image/png" ||
333
+ mime === "image/bmp" ||
334
+ ext === "gif" ||
335
+ mime === "image/gif"
336
+ )
337
+ return "lossless-image";
338
+ if (LOSSY_EXT.has(ext) || mime === "image/jpeg") return "lossy-image";
339
+ if (HEIC_EXT.has(ext) || mime === "image/heic" || mime === "image/heif")
340
+ return "heic";
341
+ if (MODERN_EXT.has(ext) || mime === "image/webp" || mime === "image/avif")
342
+ return "modern-image";
343
+ if (TIFF_EXT.has(ext) || mime === "image/tiff") return "tiff";
344
+ if (VIDEO_EXT.has(ext) || mime.startsWith("video/")) return "video";
345
+ if (AUDIO_EXT.has(ext) || mime.startsWith("audio/")) return "audio";
346
+ if (DOCUMENT_EXT.has(ext) || mime === "application/pdf") return "document";
347
+ if (ARCHIVE_EXT.has(ext) || mime === "application/zip") return "archive";
348
+ return "other";
349
+ }
89
350
 
90
- /** 按扩展名和 MIME 预估服务端会怎么处理(给「待上传」面板显示,以及决定能否勾「原图」)。 */
91
- export function expectedHandling(file: { name: string; type?: string }, videoCodec = "AV1"): YsExpectedHandling {
351
+ /** 规范文字里的 {codec} 换成视频编码名。 */
352
+ export function fileRuleText(text: string, videoCodec = "AV1"): string {
353
+ return text.replaceAll("{codec}", videoCodec);
354
+ }
355
+
356
+ /** 这个文件适用哪条规范(按扩展名和 MIME)。 */
357
+ export function fileRuleOf(file: {
358
+ name: string;
359
+ type?: string;
360
+ content_type?: string;
361
+ }): YsFileRule {
92
362
  const ext = fileExtension(file.name);
93
- const mime = String(file.type || "").toLowerCase();
94
- if (isActiveContent({ name: file.name, content_type: mime }))
95
- return { action: "active", label: "限制打开", detail: "可内嵌脚本:只按图片或源码显示,不在新标签页打开", canKeepOriginal: false };
96
- if (LOSSLESS_EXT.has(ext) || mime === "image/png" || mime === "image/bmp")
97
- return { action: "convert", label: "无损转 WebP", detail: "逐像素一致,通常省三到七成", canKeepOriginal: true };
98
- if (ext === "gif" || mime === "image/gif")
99
- return { action: "convert", label: "静态图转 WebP", detail: "动图原样保存", canKeepOriginal: true };
100
- if (LOSSY_EXT.has(ext) || mime === "image/jpeg" || mime === "image/heic")
101
- return { action: "keep", label: "原样保存", detail: "有损格式,再编码会损失画质", canKeepOriginal: false };
102
- if (MODERN_EXT.has(ext)) return { action: "keep", label: "原样保存", detail: "已是高效格式", canKeepOriginal: false };
103
- if (VIDEO_EXT.has(ext) || mime.startsWith("video/"))
104
- return { action: "video", label: `转码为 ${videoCodec}`, detail: "上传后后台转码,转好前播放原视频", canKeepOriginal: false };
105
- return { action: "keep", label: "原样保存", detail: "", canKeepOriginal: false };
363
+ const mime = String(file.type || file.content_type || "").toLowerCase();
364
+ const key = ruleKeyOf(
365
+ ext,
366
+ mime,
367
+ isActiveContent({ name: file.name, content_type: mime }),
368
+ );
369
+ return (
370
+ YS_FILE_RULES.find((rule) => rule.key === key) ??
371
+ YS_FILE_RULES[YS_FILE_RULES.length - 1]
372
+ );
373
+ }
374
+
375
+ /** 按扩展名和 MIME 预估服务端会怎么处理(给「待上传」面板显示,以及决定能否勾「保留原文件」)。 */
376
+ export function expectedHandling(
377
+ file: { name: string; type?: string },
378
+ videoCodec = "AV1",
379
+ ): YsExpectedHandling {
380
+ const rule = fileRuleOf(file);
381
+ const gif =
382
+ fileExtension(file.name) === "gif" ||
383
+ String(file.type || "").toLowerCase() === "image/gif";
384
+ return {
385
+ action: rule.action,
386
+ label: gif
387
+ ? "静态图转为无损 WebP,动图原样"
388
+ : fileRuleText(rule.standard, videoCodec),
389
+ detail: fileRuleText(rule.why, videoCodec),
390
+ canKeepOriginal: rule.keepOriginal === "choice",
391
+ advice: rule.advice ?? "",
392
+ recommendKeep: Boolean(rule.recommendKeep),
393
+ rule,
394
+ };
106
395
  }
107
396
 
108
397
  function pad(value: number): string {
@@ -116,7 +405,8 @@ export function screenshotName(at: Date, index = 0, ext = "png"): string {
116
405
  }
117
406
 
118
407
  /** 剪贴板默认名(各浏览器、系统粘贴截图时给的名字),这类文件改成「截图 …」。 */
119
- const CLIPBOARD_NAMES = /^(image|clipboard|screenshot|截图|屏幕截图|pasted)[-_ ]?\d*\.(png|jpe?g|gif|webp|bmp)$/i;
408
+ const CLIPBOARD_NAMES =
409
+ /^(image|clipboard|screenshot|截图|屏幕截图|pasted)[-_ ]?\d*\.(png|jpe?g|gif|webp|bmp)$/i;
120
410
 
121
411
  /**
122
412
  * 从粘贴事件里取文件:剪贴板里有文件、且没有纯文本时才当附件(Excel 单元格同时带图和文字,照常粘贴文字)。
@@ -130,9 +420,15 @@ export function pastedFiles(event: ClipboardEvent, now = new Date()): File[] {
130
420
  const files = Array.from(data.files || []);
131
421
  let shot = 0;
132
422
  return files.map((file) => {
133
- if (!CLIPBOARD_NAMES.test(file.name || "image.png") && file.name) return file;
134
- const ext = fileExtension(file.name) || (file.type.split("/")[1] ?? "png").replace("jpeg", "jpg");
135
- const renamed = new File([file], screenshotName(now, shot, ext), { type: file.type, lastModified: file.lastModified });
423
+ if (!CLIPBOARD_NAMES.test(file.name || "image.png") && file.name)
424
+ return file;
425
+ const ext =
426
+ fileExtension(file.name) ||
427
+ (file.type.split("/")[1] ?? "png").replace("jpeg", "jpg");
428
+ const renamed = new File([file], screenshotName(now, shot, ext), {
429
+ type: file.type,
430
+ lastModified: file.lastModified,
431
+ });
136
432
  shot += 1;
137
433
  return renamed;
138
434
  });
@@ -145,10 +441,15 @@ export function hasDraggedFiles(event: DragEvent): boolean {
145
441
 
146
442
  /** 是不是视频(按扩展名或 MIME),用来给视频单独的大小上限。 */
147
443
  export function isVideoFile(file: { name: string; type?: string }): boolean {
148
- return VIDEO_EXT.has(fileExtension(file.name)) || String(file.type || "").startsWith("video/");
444
+ return (
445
+ VIDEO_EXT.has(fileExtension(file.name)) ||
446
+ String(file.type || "").startsWith("video/")
447
+ );
149
448
  }
150
449
 
151
450
  /** 两个本地文件是否「同一个选择」:名字、大小、修改时间都相同(重新选中断过的文件时复用原来那一行)。 */
152
451
  export function sameLocalFile(a: File, b: File): boolean {
153
- return a.name === b.name && a.size === b.size && a.lastModified === b.lastModified;
452
+ return (
453
+ a.name === b.name && a.size === b.size && a.lastModified === b.lastModified
454
+ );
154
455
  }
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  export { default as YsCrudTable } from "./components/YsCrudTable.vue";
4
4
  export { default as YsTableToolbar } from "./components/YsTableToolbar.vue";
5
+ export { default as YsStatusFilter } from "./components/YsStatusFilter.vue";
5
6
  export { default as YsTablePagination } from "./components/YsTablePagination.vue";
6
7
  export { default as YsRowActionMenu } from "./components/YsRowActionMenu.vue";
7
8
  export type { YsRowAction } from "./components/YsRowActionMenu.vue";
@@ -51,9 +52,12 @@ export {
51
52
  export {
52
53
  ACTIVE_CONTENT_EXT,
53
54
  FILE_NAME_MAX,
55
+ YS_FILE_RULES,
54
56
  checkUploadFile,
55
57
  expectedHandling,
56
58
  fileNameLength,
59
+ fileRuleOf,
60
+ fileRuleText,
57
61
  hasDraggedFiles,
58
62
  isActiveContent,
59
63
  isVideoFile,
@@ -64,6 +68,7 @@ export {
64
68
  type YsFileCheckOptions,
65
69
  type YsFileMediaFields,
66
70
  type YsFileNote,
71
+ type YsFileRule,
67
72
  } from "./fileRules";
68
73
  // 单独的查看器,业务页可直接内嵌(重型依赖都在打开时才按需加载)。
69
74
  export { default as YsPdfViewer } from "./components/YsPdfViewer.vue";
@@ -1,8 +1,8 @@
1
- /* 上传队列(无界面):选择、拖入、粘贴进来的文件排队上传,统一状态、进度、续传、重试、取消和「原图」选择。
1
+ /* 上传队列(无界面):选择、拖入、粘贴进来的文件排队上传,统一状态、进度、续传、重试、取消和「保留原文件」选择。
2
2
  * 传输用 @ysgroup/core 的 uploadChunked(整份 sha256 → 秒传 / 续传 / 分块),界面用 YsUploadQueue 显示。
3
3
  *
4
4
  * 两种模式:
5
- * - 立即上传(autoStart,默认):加入即传;批次里有会被转换的图片时先进「待上传」,确认后再传(confirm: "convertible")。
5
+ * - 立即上传(autoStart,默认):加入即传;批次里有默认会被转换或转码的文件(无损图片、视频)时先进「待上传」,让用户看到会怎么处理、按需勾「保留原文件」,确认后再传(confirm: "convertible")。
6
6
  * - 待提交(autoStart: false):文件留在本地,业务对象建好后调 start(extra) 再传(新建表单、回复框)。
7
7
  */
8
8
 
@@ -32,7 +32,7 @@ export type YsUploadItem<T = Record<string, unknown>> = {
32
32
  resumedFrom: number;
33
33
  instant: boolean;
34
34
  error: string;
35
- /** 选了「原图」:原样保存,不做格式转换。 */
35
+ /** 勾选了「保留原文件」:原样保存,图片不转换、视频不转码(begin 带 as_is)。 */
36
36
  asIs: boolean;
37
37
  handling: YsExpectedHandling;
38
38
  duplicates: YsFileDuplicate[];
@@ -72,7 +72,7 @@ export type YsUploadQueueOptions<T> = {
72
72
  /** 最多加入几个文件(含已完成的行),不限为 0。 */
73
73
  limit?: number;
74
74
  autoStart?: boolean;
75
- /** 立即上传模式下什么时候先进「待上传」让用户选「原图」:convertible 有会被转换的图片时(默认)、always、never。 */
75
+ /** 立即上传模式下什么时候先进「待上传」:convertible 有默认会被转换或转码的文件时(默认)、always、never。 */
76
76
  confirm?: "convertible" | "always" | "never";
77
77
  /** 视频转码目标的显示名,默认 AV1。 */
78
78
  videoCodec?: string;
@@ -191,7 +191,7 @@ export function useYsUploadQueue<T = Record<string, unknown>>(options: YsUploadQ
191
191
  if (item.state === "pending" || item.state === "waiting" || item.state === "failed") item.asIs = value;
192
192
  }
193
193
 
194
- /** 待上传里能勾「原图」的行全部勾上 / 全部取消。 */
194
+ /** 待上传里能勾「保留原文件」的行全部勾上 / 全部取消。 */
195
195
  function setAllAsIs(value: boolean) {
196
196
  for (const item of pending.value) if (item.handling.canKeepOriginal) item.asIs = value;
197
197
  }