@ysgroup/ui 0.1.50 → 0.1.52
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 +3 -3
- package/src/components/YsDialog.vue +6 -1
- package/src/components/YsFileDropZone.vue +138 -0
- package/src/components/YsFileNotes.vue +178 -0
- package/src/components/YsFilePreview.vue +20 -16
- package/src/components/YsImageViewer.vue +317 -0
- package/src/components/YsUploadQueue.vue +287 -0
- package/src/components/YsViewerIcon.vue +1 -0
- package/src/filePreview.ts +7 -0
- package/src/fileRules.ts +154 -0
- package/src/index.ts +33 -0
- package/src/preview/imageZoom.ts +107 -0
- package/src/uploadQueue.ts +399 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
<!-- YsImageViewer —— 图片查看器(YsFilePreview 的图片预览用它,也可单独用)。
|
|
2
|
+
工具条:缩小、当前比例(点一下回到适应窗口)、放大、适应窗口、1:1 实际大小;Ctrl/⌘ + 滚轮(含触控板双指)按指针位置缩放,
|
|
3
|
+
普通滚轮照常滚动;放大后按住拖动平移;双击在适应窗口与放大之间切换;获得焦点时 + - 缩放、0 适应窗口、1 实际大小。
|
|
4
|
+
缩放改的是 img 的宽高(不用 transform),SVG 放大仍是矢量清晰;位图适应窗口时不超过 1:1,SVG 适应窗口时放大铺满。
|
|
5
|
+
SVG 没有固有尺寸时按源码里的 width/height/viewBox 定基准。换图片(src 变化)时回到适应窗口。颜色只用 --ys-* 令牌。 -->
|
|
6
|
+
<script setup lang="ts">
|
|
7
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
|
8
|
+
import YsViewerIcon from "./YsViewerIcon.vue";
|
|
9
|
+
import YsViewerState from "./YsViewerState.vue";
|
|
10
|
+
import "../preview/viewer.css";
|
|
11
|
+
import {
|
|
12
|
+
IMAGE_STAGE_PADDING,
|
|
13
|
+
SVG_FALLBACK_SIZE,
|
|
14
|
+
anchoredScroll,
|
|
15
|
+
clampImageZoom,
|
|
16
|
+
doubleClickZoom,
|
|
17
|
+
fitImageScale,
|
|
18
|
+
formatImageZoom,
|
|
19
|
+
imageZoomIn,
|
|
20
|
+
imageZoomOut,
|
|
21
|
+
svgIntrinsicSize,
|
|
22
|
+
wheelZoom,
|
|
23
|
+
zoomLimits,
|
|
24
|
+
type ImageSize,
|
|
25
|
+
} from "../preview/imageZoom";
|
|
26
|
+
|
|
27
|
+
const props = withDefaults(
|
|
28
|
+
defineProps<{
|
|
29
|
+
/** 图片地址(blob:、data:、http 都行)。 */
|
|
30
|
+
src: string;
|
|
31
|
+
alt?: string;
|
|
32
|
+
/** 图片内容;是 SVG 时用来读固有尺寸,并按矢量图处理(适应窗口时放大铺满)。 */
|
|
33
|
+
source?: Blob | null;
|
|
34
|
+
/** 强制按矢量图处理;不传时按 source 的类型或地址判断是不是 SVG。 */
|
|
35
|
+
vector?: boolean;
|
|
36
|
+
}>(),
|
|
37
|
+
{ alt: "", source: null, vector: undefined },
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
type ZoomMode = "fit" | number;
|
|
41
|
+
|
|
42
|
+
const root = ref<HTMLElement | null>(null);
|
|
43
|
+
const scroller = ref<HTMLElement | null>(null);
|
|
44
|
+
const image = ref<HTMLImageElement | null>(null);
|
|
45
|
+
const status = ref<"loading" | "ready" | "error">("loading");
|
|
46
|
+
const natural = ref<ImageSize | null>(null);
|
|
47
|
+
const box = ref<ImageSize>({ width: 0, height: 0 });
|
|
48
|
+
const mode = ref<ZoomMode>("fit");
|
|
49
|
+
let token = 0;
|
|
50
|
+
let resizeObserver: ResizeObserver | null = null;
|
|
51
|
+
let drag: { id: number; x: number; y: number; left: number; top: number } | null = null;
|
|
52
|
+
const dragging = ref(false);
|
|
53
|
+
|
|
54
|
+
const isVector = computed(() => props.vector ?? (/svg/i.test(props.source?.type ?? "") || /^data:image\/svg\+xml/i.test(props.src) || /\.svg(?:[?#]|$)/i.test(props.src)));
|
|
55
|
+
const fit = computed(() => (natural.value ? fitImageScale(natural.value, box.value, isVector.value) : 1));
|
|
56
|
+
const limits = computed(() => zoomLimits(fit.value));
|
|
57
|
+
const scale = computed(() => (mode.value === "fit" ? fit.value : clampImageZoom(mode.value, limits.value)));
|
|
58
|
+
const display = computed(() => (natural.value ? { width: natural.value.width * scale.value, height: natural.value.height * scale.value } : null));
|
|
59
|
+
const zoomLabel = computed(() => formatImageZoom(scale.value));
|
|
60
|
+
const sizeLabel = computed(() => (natural.value ? `${Math.round(natural.value.width)} × ${Math.round(natural.value.height)}` : ""));
|
|
61
|
+
const isFit = computed(() => mode.value === "fit");
|
|
62
|
+
const isActual = computed(() => Math.abs(scale.value - 1) < 1e-3);
|
|
63
|
+
const canZoomIn = computed(() => scale.value < limits.value.max - 1e-3);
|
|
64
|
+
const canZoomOut = computed(() => scale.value > limits.value.min + 1e-3);
|
|
65
|
+
/** 图片比可视区大时才能拖动平移。 */
|
|
66
|
+
const canPan = computed(() => {
|
|
67
|
+
const size = display.value;
|
|
68
|
+
if (!size) return false;
|
|
69
|
+
return size.width + IMAGE_STAGE_PADDING * 2 > box.value.width + 1 || size.height + IMAGE_STAGE_PADDING * 2 > box.value.height + 1;
|
|
70
|
+
});
|
|
71
|
+
const imageStyle = computed(() => (display.value ? { width: `${display.value.width}px`, height: `${display.value.height}px` } : undefined));
|
|
72
|
+
|
|
73
|
+
function measure() {
|
|
74
|
+
const el = scroller.value;
|
|
75
|
+
if (!el) return;
|
|
76
|
+
box.value = { width: el.clientWidth, height: el.clientHeight };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function onLoad() {
|
|
80
|
+
const el = image.value;
|
|
81
|
+
if (!el) return;
|
|
82
|
+
const mine = token;
|
|
83
|
+
let size: ImageSize | null = el.naturalWidth > 0 && el.naturalHeight > 0 ? { width: el.naturalWidth, height: el.naturalHeight } : null;
|
|
84
|
+
if (isVector.value) {
|
|
85
|
+
// SVG 只写了 viewBox 时各浏览器给的 naturalWidth 不一致(0 或 300×150),以源码为准。
|
|
86
|
+
const text = props.source ? await props.source.text().catch(() => "") : "";
|
|
87
|
+
if (mine !== token) return;
|
|
88
|
+
size = (text && svgIntrinsicSize(text)) || size || { ...SVG_FALLBACK_SIZE };
|
|
89
|
+
}
|
|
90
|
+
if (!size) {
|
|
91
|
+
status.value = "error";
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
natural.value = size;
|
|
95
|
+
mode.value = "fit";
|
|
96
|
+
status.value = "ready";
|
|
97
|
+
// 加载中滚动区只占 1 像素,显示出来后再量可视区。
|
|
98
|
+
await nextTick();
|
|
99
|
+
measure();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function onError() {
|
|
103
|
+
status.value = "error";
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 缩放到 next;anchor 是相对可视区左上角的点(不传取中心),缩放后这一点下的图片内容不动。 */
|
|
107
|
+
async function setZoom(next: ZoomMode, anchor?: { x: number; y: number }) {
|
|
108
|
+
const el = scroller.value;
|
|
109
|
+
const img = image.value;
|
|
110
|
+
const before = scale.value;
|
|
111
|
+
const target = next === "fit" ? fit.value : clampImageZoom(next, limits.value);
|
|
112
|
+
if (!el || !img || !natural.value || before <= 0) {
|
|
113
|
+
mode.value = next === "fit" ? "fit" : target;
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const point = anchor ?? { x: el.clientWidth / 2, y: el.clientHeight / 2 };
|
|
117
|
+
const scroll = { left: el.scrollLeft, top: el.scrollTop, offsetLeft: img.offsetLeft, offsetTop: img.offsetTop };
|
|
118
|
+
mode.value = next === "fit" ? "fit" : target;
|
|
119
|
+
await nextTick();
|
|
120
|
+
const ratio = target / before;
|
|
121
|
+
el.scrollLeft = anchoredScroll(scroll.left, point.x, scroll.offsetLeft, ratio, img.offsetLeft);
|
|
122
|
+
el.scrollTop = anchoredScroll(scroll.top, point.y, scroll.offsetTop, ratio, img.offsetTop);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function reset() {
|
|
126
|
+
void setZoom("fit");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function zoomStep(direction: 1 | -1) {
|
|
130
|
+
void setZoom(direction > 0 ? imageZoomIn(scale.value, limits.value) : imageZoomOut(scale.value, limits.value));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function pointOf(event: MouseEvent): { x: number; y: number } | undefined {
|
|
134
|
+
const rect = scroller.value?.getBoundingClientRect();
|
|
135
|
+
return rect ? { x: event.clientX - rect.left, y: event.clientY - rect.top } : undefined;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function onWheel(event: WheelEvent) {
|
|
139
|
+
// 普通滚轮留给滚动;Ctrl/⌘ + 滚轮(触控板双指缩放也带 ctrlKey)才缩放。
|
|
140
|
+
if ((!event.ctrlKey && !event.metaKey) || status.value !== "ready") return;
|
|
141
|
+
event.preventDefault();
|
|
142
|
+
void setZoom(wheelZoom(scale.value, event.deltaY, limits.value), pointOf(event));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function onDblclick(event: MouseEvent) {
|
|
146
|
+
if (status.value !== "ready") return;
|
|
147
|
+
void setZoom(doubleClickZoom(scale.value, fit.value, limits.value), pointOf(event));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function onPointerdown(event: PointerEvent) {
|
|
151
|
+
// 触屏用原生滚动;鼠标、手写笔左键按住拖动。
|
|
152
|
+
if (event.button !== 0 || event.pointerType === "touch" || !canPan.value || !scroller.value) return;
|
|
153
|
+
drag = { id: event.pointerId, x: event.clientX, y: event.clientY, left: scroller.value.scrollLeft, top: scroller.value.scrollTop };
|
|
154
|
+
dragging.value = true;
|
|
155
|
+
scroller.value.setPointerCapture?.(event.pointerId);
|
|
156
|
+
event.preventDefault();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function onPointermove(event: PointerEvent) {
|
|
160
|
+
if (!drag || drag.id !== event.pointerId || !scroller.value) return;
|
|
161
|
+
scroller.value.scrollLeft = drag.left - (event.clientX - drag.x);
|
|
162
|
+
scroller.value.scrollTop = drag.top - (event.clientY - drag.y);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function onPointerup(event: PointerEvent) {
|
|
166
|
+
if (!drag || drag.id !== event.pointerId) return;
|
|
167
|
+
scroller.value?.releasePointerCapture?.(event.pointerId);
|
|
168
|
+
drag = null;
|
|
169
|
+
dragging.value = false;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function onKeydown(event: KeyboardEvent) {
|
|
173
|
+
if (event.ctrlKey || event.metaKey || event.altKey || status.value !== "ready") return;
|
|
174
|
+
if ((event.target as HTMLElement | null)?.closest("input, textarea")) return;
|
|
175
|
+
if (event.key === "+" || event.key === "=") zoomStep(1);
|
|
176
|
+
else if (event.key === "-" || event.key === "_") zoomStep(-1);
|
|
177
|
+
else if (event.key === "0") void setZoom("fit");
|
|
178
|
+
else if (event.key === "1") void setZoom(1);
|
|
179
|
+
else return;
|
|
180
|
+
event.preventDefault();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
watch(
|
|
184
|
+
() => props.src,
|
|
185
|
+
() => {
|
|
186
|
+
token += 1;
|
|
187
|
+
status.value = "loading";
|
|
188
|
+
natural.value = null;
|
|
189
|
+
mode.value = "fit";
|
|
190
|
+
drag = null;
|
|
191
|
+
dragging.value = false;
|
|
192
|
+
},
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
onMounted(() => {
|
|
196
|
+
measure();
|
|
197
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
198
|
+
resizeObserver = new ResizeObserver(() => measure());
|
|
199
|
+
if (scroller.value) resizeObserver.observe(scroller.value);
|
|
200
|
+
}
|
|
201
|
+
// 挂载前已从缓存读完的图片不会再触发 load。
|
|
202
|
+
if (image.value?.complete && image.value.naturalWidth > 0) void onLoad();
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
onBeforeUnmount(() => {
|
|
206
|
+
token += 1;
|
|
207
|
+
resizeObserver?.disconnect();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
defineExpose({ setZoom, reset });
|
|
211
|
+
</script>
|
|
212
|
+
|
|
213
|
+
<template>
|
|
214
|
+
<div ref="root" class="ys-viewer ys-image" tabindex="-1" @keydown="onKeydown">
|
|
215
|
+
<div v-if="status === 'ready'" class="ys-viewer__bar" role="toolbar" aria-label="图片工具条">
|
|
216
|
+
<span class="ys-viewer__pager ys-viewer__wide-only ys-image__size" :title="isVector ? '矢量图固有尺寸' : '图片原始尺寸'">{{ sizeLabel }}</span>
|
|
217
|
+
<span class="ys-viewer__spacer" />
|
|
218
|
+
<button type="button" class="ys-viewer__btn" :disabled="!canZoomOut" title="缩小(-)" aria-label="缩小" @click="zoomStep(-1)"><YsViewerIcon name="minus" /></button>
|
|
219
|
+
<button type="button" class="ys-viewer__btn ys-image__zoom" title="当前比例,点击适应窗口(0)" aria-label="当前比例,点击适应窗口" @click="reset">{{ zoomLabel }}</button>
|
|
220
|
+
<button type="button" class="ys-viewer__btn" :disabled="!canZoomIn" title="放大(+)" aria-label="放大" @click="zoomStep(1)"><YsViewerIcon name="plus" /></button>
|
|
221
|
+
<span class="ys-viewer__sep" />
|
|
222
|
+
<button type="button" class="ys-viewer__btn" :class="{ 'is-active': isFit }" :aria-pressed="isFit" title="适应窗口(0)" aria-label="适应窗口" @click="setZoom('fit')">
|
|
223
|
+
<YsViewerIcon name="fit-screen" />
|
|
224
|
+
</button>
|
|
225
|
+
<button type="button" class="ys-viewer__btn ys-image__actual" :class="{ 'is-active': !isFit && isActual }" :aria-pressed="!isFit && isActual" title="实际大小(1)" aria-label="实际大小" @click="setZoom(1)">1:1</button>
|
|
226
|
+
<slot name="toolbar" />
|
|
227
|
+
</div>
|
|
228
|
+
|
|
229
|
+
<YsViewerState v-if="status === 'loading'" title="正在打开图片…" />
|
|
230
|
+
<YsViewerState v-else-if="status === 'error'" type="error" title="无法显示此图片" detail="文件可能已损坏,或浏览器不支持这种图片格式。">
|
|
231
|
+
<template v-if="$slots['error-actions']" #actions><slot name="error-actions" /></template>
|
|
232
|
+
</YsViewerState>
|
|
233
|
+
|
|
234
|
+
<div
|
|
235
|
+
v-show="status !== 'error'"
|
|
236
|
+
ref="scroller"
|
|
237
|
+
class="ys-image__scroller"
|
|
238
|
+
:class="{ 'is-pannable': canPan, 'is-dragging': dragging, 'is-pending': status !== 'ready' }"
|
|
239
|
+
tabindex="0"
|
|
240
|
+
aria-label="图片"
|
|
241
|
+
@wheel="onWheel"
|
|
242
|
+
@dblclick="onDblclick"
|
|
243
|
+
@pointerdown="onPointerdown"
|
|
244
|
+
@pointermove="onPointermove"
|
|
245
|
+
@pointerup="onPointerup"
|
|
246
|
+
@pointercancel="onPointerup"
|
|
247
|
+
>
|
|
248
|
+
<div class="ys-image__canvas" :style="{ padding: `${IMAGE_STAGE_PADDING}px` }">
|
|
249
|
+
<img ref="image" class="ys-image__img" :class="{ 'is-svg': isVector }" :src="src" :alt="alt" :style="imageStyle" draggable="false" @load="onLoad" @error="onError" />
|
|
250
|
+
</div>
|
|
251
|
+
</div>
|
|
252
|
+
</div>
|
|
253
|
+
</template>
|
|
254
|
+
|
|
255
|
+
<style scoped>
|
|
256
|
+
.ys-image__scroller {
|
|
257
|
+
position: relative;
|
|
258
|
+
flex: 1;
|
|
259
|
+
min-width: 0;
|
|
260
|
+
min-height: 0;
|
|
261
|
+
overflow: auto;
|
|
262
|
+
overscroll-behavior: contain;
|
|
263
|
+
}
|
|
264
|
+
.ys-image__scroller:focus {
|
|
265
|
+
outline: none;
|
|
266
|
+
}
|
|
267
|
+
.ys-image__scroller:focus-visible {
|
|
268
|
+
outline: var(--ys-focus-outline);
|
|
269
|
+
outline-offset: -2px;
|
|
270
|
+
}
|
|
271
|
+
/* 加载中只占位:图片照常加载,但不显示、不撑出滚动条。 */
|
|
272
|
+
.ys-image__scroller.is-pending {
|
|
273
|
+
position: absolute;
|
|
274
|
+
width: 1px;
|
|
275
|
+
height: 1px;
|
|
276
|
+
overflow: hidden;
|
|
277
|
+
opacity: 0;
|
|
278
|
+
pointer-events: none;
|
|
279
|
+
}
|
|
280
|
+
.ys-image__scroller.is-pannable {
|
|
281
|
+
cursor: grab;
|
|
282
|
+
}
|
|
283
|
+
.ys-image__scroller.is-dragging {
|
|
284
|
+
cursor: grabbing;
|
|
285
|
+
user-select: none;
|
|
286
|
+
}
|
|
287
|
+
.ys-image__canvas {
|
|
288
|
+
display: flex;
|
|
289
|
+
box-sizing: border-box;
|
|
290
|
+
align-items: center;
|
|
291
|
+
justify-content: center;
|
|
292
|
+
width: max-content;
|
|
293
|
+
min-width: 100%;
|
|
294
|
+
height: max-content;
|
|
295
|
+
min-height: 100%;
|
|
296
|
+
}
|
|
297
|
+
.ys-image__img {
|
|
298
|
+
display: block;
|
|
299
|
+
flex: none;
|
|
300
|
+
max-width: none;
|
|
301
|
+
border-radius: 2px;
|
|
302
|
+
box-shadow: var(--ys-shadow-paper);
|
|
303
|
+
/* 透明图片垫一层浅色棋盘格,透明区域看得出来。 */
|
|
304
|
+
background: repeating-conic-gradient(color-mix(in srgb, var(--ys-color-text-3) 14%, transparent) 0 25%, transparent 0 50%) 0 0 / 16px 16px;
|
|
305
|
+
user-select: none;
|
|
306
|
+
-webkit-user-drag: none;
|
|
307
|
+
}
|
|
308
|
+
.ys-image__zoom {
|
|
309
|
+
min-width: 56px;
|
|
310
|
+
}
|
|
311
|
+
.ys-image__actual {
|
|
312
|
+
font-weight: 600;
|
|
313
|
+
}
|
|
314
|
+
.ys-image__size {
|
|
315
|
+
padding-left: 4px;
|
|
316
|
+
}
|
|
317
|
+
</style>
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
<!-- YsUploadQueue —— 上传队列面板(配 useYsUploadQueue)。
|
|
2
|
+
- 「待上传」:列出会怎么处理(无损转 WebP / 原样保存 / 转码 / 限制打开),能转换的图片可勾「原图」,有「全部原图」;
|
|
3
|
+
立即上传模式下点「上传 N 个」开始,待提交模式(staged)显示「提交后上传」、不出确认按钮。
|
|
4
|
+
- 进行中:校验中 / 上传中(从 N% 续传)/ 处理中;完成:秒传完成 / 已上传,带服务端的文件标签;
|
|
5
|
+
已中断显示「继续」,失败显示原因和「重试」,移除会清掉服务端半截的上传。
|
|
6
|
+
- 本地检查不通过、超过数量、重复加入的提示显示在顶部,可关闭。 -->
|
|
7
|
+
<script setup lang="ts" generic="T extends Record<string, unknown>">
|
|
8
|
+
import { computed } from "vue";
|
|
9
|
+
import { fileExtension, formatFileSize } from "../filePreview";
|
|
10
|
+
import type { YsFileNote } from "../fileRules";
|
|
11
|
+
import { uploadStateText, type YsUploadItem, type YsUploadQueue } from "../uploadQueue";
|
|
12
|
+
import YsFileNotes from "./YsFileNotes.vue";
|
|
13
|
+
|
|
14
|
+
const props = withDefaults(
|
|
15
|
+
defineProps<{
|
|
16
|
+
queue: YsUploadQueue<T>;
|
|
17
|
+
/** 待提交模式:待上传行显示「提交后上传」,不出「取消 / 上传」按钮(由业务的提交按钮触发 start)。 */
|
|
18
|
+
staged?: boolean;
|
|
19
|
+
/** 完成的行是否继续显示(默认显示,可「清除已完成」)。 */
|
|
20
|
+
showDone?: boolean;
|
|
21
|
+
}>(),
|
|
22
|
+
{ staged: false, showDone: true },
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const items = computed(() =>
|
|
26
|
+
props.showDone ? props.queue.items.value : props.queue.items.value.filter((item) => item.state !== "done"),
|
|
27
|
+
);
|
|
28
|
+
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));
|
|
33
|
+
|
|
34
|
+
function nameParts(name: string): [string, string] {
|
|
35
|
+
const ext = fileExtension(name);
|
|
36
|
+
return ext ? [name.slice(0, -(ext.length + 1)), `.${name.slice(-ext.length)}`] : [name, ""];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function badge(item: YsUploadItem<T>): string {
|
|
40
|
+
return (fileExtension(item.file.name) || "file").slice(0, 4).toUpperCase();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function resultNotes(item: YsUploadItem<T>): YsFileNote[] {
|
|
44
|
+
const notes = (item.result as { notes?: unknown } | null)?.notes;
|
|
45
|
+
return Array.isArray(notes) ? (notes as YsFileNote[]) : [];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function resultName(item: YsUploadItem<T>): string {
|
|
49
|
+
const name = (item.result as { name?: unknown } | null)?.name;
|
|
50
|
+
return typeof name === "string" && name !== item.file.name ? name : "";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const progressing = (item: YsUploadItem<T>) => item.state === "hashing" || item.state === "uploading";
|
|
54
|
+
</script>
|
|
55
|
+
|
|
56
|
+
<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">
|
|
59
|
+
<span>{{ text }}</span>
|
|
60
|
+
<button type="button" class="ys-upload-queue__close" aria-label="关闭提示" @click="queue.dismissNotice(index)">×</button>
|
|
61
|
+
</div>
|
|
62
|
+
|
|
63
|
+
<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))"
|
|
74
|
+
>
|
|
75
|
+
全部原图
|
|
76
|
+
</el-checkbox>
|
|
77
|
+
</div>
|
|
78
|
+
</div>
|
|
79
|
+
|
|
80
|
+
<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>
|
|
83
|
+
<div class="ys-upload-queue__main">
|
|
84
|
+
<div class="ys-upload-queue__line">
|
|
85
|
+
<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>
|
|
87
|
+
</span>
|
|
88
|
+
<span class="ys-upload-queue__muted">{{ formatFileSize(item.file.size) }}</span>
|
|
89
|
+
</div>
|
|
90
|
+
<div class="ys-upload-queue__line is-sub">
|
|
91
|
+
<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 }}
|
|
94
|
+
</span>
|
|
95
|
+
<span v-if="!item.asIs && item.handling.detail" class="ys-upload-queue__muted">{{ item.handling.detail }}</span>
|
|
96
|
+
</template>
|
|
97
|
+
<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" />
|
|
101
|
+
</template>
|
|
102
|
+
</div>
|
|
103
|
+
<el-progress
|
|
104
|
+
v-if="progressing(item)"
|
|
105
|
+
:percentage="Math.round(item.progress * 100)"
|
|
106
|
+
:stroke-width="3"
|
|
107
|
+
:show-text="false"
|
|
108
|
+
/>
|
|
109
|
+
</div>
|
|
110
|
+
<div class="ys-upload-queue__actions">
|
|
111
|
+
<el-checkbox
|
|
112
|
+
v-if="item.state === 'pending' && item.handling.canKeepOriginal"
|
|
113
|
+
:model-value="item.asIs"
|
|
114
|
+
@change="queue.setAsIs(item, Boolean($event))"
|
|
115
|
+
>
|
|
116
|
+
原图
|
|
117
|
+
</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>
|
|
120
|
+
<el-button
|
|
121
|
+
v-if="item.state !== 'done' && item.state !== 'processing'"
|
|
122
|
+
link
|
|
123
|
+
:type="item.state === 'paused' ? 'danger' : undefined"
|
|
124
|
+
@click="queue.remove(item)"
|
|
125
|
+
>
|
|
126
|
+
{{ item.state === "hashing" || item.state === "uploading" ? "取消" : item.state === "paused" ? "放弃" : "移除" }}
|
|
127
|
+
</el-button>
|
|
128
|
+
</div>
|
|
129
|
+
</li>
|
|
130
|
+
</ul>
|
|
131
|
+
|
|
132
|
+
<div class="ys-upload-queue__foot">
|
|
133
|
+
<template v-if="pending.length && !staged">
|
|
134
|
+
<span class="ys-upload-queue__muted">共 {{ pending.length }} 个 · {{ formatFileSize(pendingSize) }}</span>
|
|
135
|
+
<el-button size="small" @click="queue.cancelPending()">取消</el-button>
|
|
136
|
+
<el-button size="small" type="primary" @click="queue.start()">上传 {{ pending.length }} 个</el-button>
|
|
137
|
+
</template>
|
|
138
|
+
<template v-else>
|
|
139
|
+
<el-button v-if="queue.paused.value.length + queue.failed.value.length > 1" size="small" @click="queue.retryAll()">
|
|
140
|
+
全部继续
|
|
141
|
+
</el-button>
|
|
142
|
+
<el-button v-if="queue.done.value.length" size="small" link @click="queue.clearDone()">清除已完成</el-button>
|
|
143
|
+
</template>
|
|
144
|
+
</div>
|
|
145
|
+
</div>
|
|
146
|
+
</template>
|
|
147
|
+
|
|
148
|
+
<style scoped>
|
|
149
|
+
.ys-upload-queue {
|
|
150
|
+
display: flex;
|
|
151
|
+
flex-direction: column;
|
|
152
|
+
gap: 8px;
|
|
153
|
+
padding: 10px 12px;
|
|
154
|
+
border: 1px solid var(--ys-color-border);
|
|
155
|
+
border-radius: var(--ys-radius-m);
|
|
156
|
+
background: var(--ys-color-bg-elevated);
|
|
157
|
+
}
|
|
158
|
+
.ys-upload-queue__notice {
|
|
159
|
+
display: flex;
|
|
160
|
+
gap: 8px;
|
|
161
|
+
align-items: flex-start;
|
|
162
|
+
justify-content: space-between;
|
|
163
|
+
padding: 6px 10px;
|
|
164
|
+
border-radius: var(--ys-radius-s);
|
|
165
|
+
color: color-mix(in srgb, var(--ys-color-warning) 80%, var(--ys-color-text-1));
|
|
166
|
+
background: color-mix(in srgb, var(--ys-color-warning) 10%, transparent);
|
|
167
|
+
font-size: 13px;
|
|
168
|
+
}
|
|
169
|
+
.ys-upload-queue__close {
|
|
170
|
+
border: 0;
|
|
171
|
+
background: none;
|
|
172
|
+
color: inherit;
|
|
173
|
+
font-size: 16px;
|
|
174
|
+
line-height: 1;
|
|
175
|
+
cursor: pointer;
|
|
176
|
+
}
|
|
177
|
+
.ys-upload-queue__confirm-head {
|
|
178
|
+
display: flex;
|
|
179
|
+
flex-wrap: wrap;
|
|
180
|
+
gap: 4px 12px;
|
|
181
|
+
align-items: center;
|
|
182
|
+
}
|
|
183
|
+
.ys-upload-queue__list {
|
|
184
|
+
display: flex;
|
|
185
|
+
flex-direction: column;
|
|
186
|
+
gap: 2px;
|
|
187
|
+
margin: 0;
|
|
188
|
+
padding: 0;
|
|
189
|
+
list-style: none;
|
|
190
|
+
}
|
|
191
|
+
.ys-upload-queue__row {
|
|
192
|
+
display: flex;
|
|
193
|
+
gap: 10px;
|
|
194
|
+
align-items: center;
|
|
195
|
+
padding: 6px 4px;
|
|
196
|
+
border-radius: var(--ys-radius-s);
|
|
197
|
+
}
|
|
198
|
+
.ys-upload-queue__row:hover {
|
|
199
|
+
background: var(--ys-color-bg-hover);
|
|
200
|
+
}
|
|
201
|
+
.ys-upload-queue__badge {
|
|
202
|
+
flex: none;
|
|
203
|
+
width: 34px;
|
|
204
|
+
height: 34px;
|
|
205
|
+
border-radius: var(--ys-radius-s);
|
|
206
|
+
color: var(--ys-color-primary);
|
|
207
|
+
background: color-mix(in srgb, var(--ys-color-primary) 10%, transparent);
|
|
208
|
+
font-size: 11px;
|
|
209
|
+
font-weight: 600;
|
|
210
|
+
line-height: 34px;
|
|
211
|
+
text-align: center;
|
|
212
|
+
}
|
|
213
|
+
.ys-upload-queue__main {
|
|
214
|
+
flex: 1;
|
|
215
|
+
min-width: 0;
|
|
216
|
+
display: flex;
|
|
217
|
+
flex-direction: column;
|
|
218
|
+
gap: 2px;
|
|
219
|
+
}
|
|
220
|
+
.ys-upload-queue__line {
|
|
221
|
+
display: flex;
|
|
222
|
+
gap: 8px;
|
|
223
|
+
align-items: center;
|
|
224
|
+
min-width: 0;
|
|
225
|
+
}
|
|
226
|
+
.ys-upload-queue__line.is-sub {
|
|
227
|
+
flex-wrap: wrap;
|
|
228
|
+
font-size: 12px;
|
|
229
|
+
}
|
|
230
|
+
.ys-upload-queue__name {
|
|
231
|
+
display: flex;
|
|
232
|
+
min-width: 0;
|
|
233
|
+
color: var(--ys-color-text-1);
|
|
234
|
+
}
|
|
235
|
+
.ys-upload-queue__stem {
|
|
236
|
+
overflow: hidden;
|
|
237
|
+
white-space: nowrap;
|
|
238
|
+
text-overflow: ellipsis;
|
|
239
|
+
}
|
|
240
|
+
.ys-upload-queue__muted {
|
|
241
|
+
flex: none;
|
|
242
|
+
color: var(--ys-color-text-3);
|
|
243
|
+
font-size: 12px;
|
|
244
|
+
}
|
|
245
|
+
.ys-upload-queue__state {
|
|
246
|
+
color: var(--ys-color-text-2);
|
|
247
|
+
}
|
|
248
|
+
.is-failed .ys-upload-queue__state {
|
|
249
|
+
color: var(--ys-color-danger);
|
|
250
|
+
}
|
|
251
|
+
.is-paused .ys-upload-queue__state {
|
|
252
|
+
color: color-mix(in srgb, var(--ys-color-warning) 80%, var(--ys-color-text-1));
|
|
253
|
+
}
|
|
254
|
+
.is-done .ys-upload-queue__state {
|
|
255
|
+
color: var(--ys-color-success);
|
|
256
|
+
}
|
|
257
|
+
.ys-upload-queue__handling {
|
|
258
|
+
padding: 0 6px;
|
|
259
|
+
border-radius: var(--ys-radius-s);
|
|
260
|
+
color: var(--ys-color-text-2);
|
|
261
|
+
background: color-mix(in srgb, var(--ys-color-text-3) 10%, transparent);
|
|
262
|
+
}
|
|
263
|
+
.ys-upload-queue__handling.is-convert,
|
|
264
|
+
.ys-upload-queue__handling.is-video {
|
|
265
|
+
color: var(--ys-color-primary);
|
|
266
|
+
background: color-mix(in srgb, var(--ys-color-primary) 10%, transparent);
|
|
267
|
+
}
|
|
268
|
+
.ys-upload-queue__handling.is-active {
|
|
269
|
+
color: var(--ys-color-danger);
|
|
270
|
+
background: color-mix(in srgb, var(--ys-color-danger) 10%, transparent);
|
|
271
|
+
}
|
|
272
|
+
.ys-upload-queue__actions {
|
|
273
|
+
flex: none;
|
|
274
|
+
display: flex;
|
|
275
|
+
gap: 8px;
|
|
276
|
+
align-items: center;
|
|
277
|
+
}
|
|
278
|
+
.ys-upload-queue__foot {
|
|
279
|
+
display: flex;
|
|
280
|
+
gap: 8px;
|
|
281
|
+
align-items: center;
|
|
282
|
+
justify-content: flex-end;
|
|
283
|
+
}
|
|
284
|
+
.ys-upload-queue__foot:empty {
|
|
285
|
+
display: none;
|
|
286
|
+
}
|
|
287
|
+
</style>
|
|
@@ -23,6 +23,7 @@ const PATHS = {
|
|
|
23
23
|
alert: "M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18zM12 7.5v5.5M12 16.5v.01",
|
|
24
24
|
download: "M12 4v11M7 10.5l5 5 5-5M5 20h14",
|
|
25
25
|
"fit-width": "M3 12h18M7 8l-4 4 4 4M17 8l4 4-4 4",
|
|
26
|
+
"fit-screen": "M4 9V5a1 1 0 0 1 1-1h4M15 4h4a1 1 0 0 1 1 1v4M20 15v4a1 1 0 0 1-1 1h-4M9 20H5a1 1 0 0 1-1-1v-4M9 9h6v6H9z",
|
|
26
27
|
"fit-page": "M6 3h12a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1zM9 8h6M9 12h6M9 16h4",
|
|
27
28
|
external: "M14 4h6v6M20 4l-9 9M18 14v4a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4",
|
|
28
29
|
close: "M6 6l12 12M18 6L6 18",
|
package/src/filePreview.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/* 文件预览的辅助函数:判断能怎么预览、文件类型标签、大小格式化、把 Blob 存成下载。
|
|
2
2
|
* 与 YsFilePreview 配合使用,也可单独用在文件列表里。 */
|
|
3
3
|
|
|
4
|
+
import type { YsFileNote } from "./fileRules";
|
|
5
|
+
|
|
4
6
|
/** 预览方式。excel:xlsx/xlsm/xls/xlsb/ods/csv 表格;word:docx(旧版 .doc 不支持,为 none)。 */
|
|
5
7
|
export type YsPreviewKind = "pdf" | "image" | "text" | "video" | "audio" | "excel" | "word" | "none";
|
|
6
8
|
|
|
@@ -11,6 +13,10 @@ export type YsPreviewFile = {
|
|
|
11
13
|
size?: number;
|
|
12
14
|
/** 上传时记录的 MIME,可空。 */
|
|
13
15
|
content_type?: string;
|
|
16
|
+
/** 文件标签(服务端 file_media_fields 的 notes),预览头部显示。 */
|
|
17
|
+
notes?: YsFileNote[];
|
|
18
|
+
/** 系统自带、可信的文件(如仓库里的 SVG Logo):可内嵌脚本的类型也允许新标签页打开。用户上传的文件不要设。 */
|
|
19
|
+
trusted?: boolean;
|
|
14
20
|
};
|
|
15
21
|
|
|
16
22
|
const IMAGE_EXT = new Set(["png", "jpg", "jpeg", "gif", "webp", "bmp", "svg", "avif", "ico"]);
|
|
@@ -276,6 +282,7 @@ const TYPE_LABELS: [string[], string][] = [
|
|
|
276
282
|
[["xls", "xlsx", "xlsm", "xlsb", "et", "ods", "csv"], "Excel"],
|
|
277
283
|
[["ppt", "pptx", "dps", "odp"], "PPT"],
|
|
278
284
|
[["zip", "rar", "7z", "gz", "tgz", "tar", "bz2", "xz"], "压缩包"],
|
|
285
|
+
[["heic", "heif"], "HEIC 照片"],
|
|
279
286
|
[["txt", "md", "markdown", "log"], "文本"],
|
|
280
287
|
];
|
|
281
288
|
|