@yxhl/specter-pui-vtk 1.0.87 → 1.0.89

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": "@yxhl/specter-pui-vtk",
3
- "version": "1.0.87",
3
+ "version": "1.0.89",
4
4
  "description": "雅心互联 Vue 3 + Vuetify3 组件库",
5
5
  "type": "module",
6
6
  "main": "./dist/specter-pui.umd.js",
@@ -41,9 +41,15 @@
41
41
  <v-toolbar-title>{{ currentImageTitle }}</v-toolbar-title>
42
42
  <VSpacer></VSpacer>
43
43
  <v-toolbar-items>
44
- <VBtn icon dark @click="zoomImage(-0.3)">
45
- <VIcon>mdi-magnify-minus-outline</VIcon>
46
- </VBtn>
44
+ <VBtn icon dark title="新窗口打开" @click="openCurrentImageInNewWindow">
45
+ <VIcon>mdi-open-in-new</VIcon>
46
+ </VBtn>
47
+ <VBtn icon dark title="下载图片" @click="downloadCurrentImage">
48
+ <VIcon>mdi-download</VIcon>
49
+ </VBtn>
50
+ <VBtn icon dark @click="zoomImage(-0.3)">
51
+ <VIcon>mdi-magnify-minus-outline</VIcon>
52
+ </VBtn>
47
53
  <VBtn icon dark @click="zoomImage(0.3)">
48
54
  <VIcon>mdi-magnify-plus-outline</VIcon>
49
55
  </VBtn>
@@ -102,22 +108,28 @@
102
108
  </div>
103
109
  </template>
104
110
 
105
- <script setup>
106
- import { ref, computed, watch } from 'vue';
111
+ <script setup>
112
+ import { ref, computed, watch, getCurrentInstance } from 'vue';
107
113
 
108
- defineOptions({
109
- name: 'VtkImg',
110
- inheritAttrs: true
111
- });
114
+ defineOptions({
115
+ name: 'VtkImg',
116
+ inheritAttrs: true
117
+ });
118
+
119
+ const { proxy } = getCurrentInstance();
112
120
 
113
121
  const props = defineProps({
114
- src: {
115
- type: String,
116
- default: null,
117
- },
118
- error: {
119
- type: String,
120
- default: new URL('../../assets/img/wxsq.png', import.meta.url).href,
122
+ src: {
123
+ type: String,
124
+ default: null,
125
+ },
126
+ downloadSrc: {
127
+ type: String,
128
+ default: '',
129
+ },
130
+ error: {
131
+ type: String,
132
+ default: new URL('../../assets/img/wxsq.png', import.meta.url).href,
121
133
  },
122
134
  preview: {
123
135
  type: Boolean,
@@ -215,9 +227,32 @@ const currentImageUrl = computed(() => {
215
227
  }
216
228
  }
217
229
  }
218
- return srcWithToken.value;
219
- });
220
-
230
+ return srcWithToken.value;
231
+ });
232
+
233
+ const currentImageRawUrl = computed(() => {
234
+ if (props.imageList.length > 0 && currentIndex.value < props.imageList.length) {
235
+ const currentImage = props.imageList[currentIndex.value];
236
+ const url = typeof currentImage === 'string' ? currentImage : currentImage?.url;
237
+ return url || props.src;
238
+ }
239
+
240
+ return props.src;
241
+ });
242
+
243
+ const currentImageDownloadUrl = computed(() => {
244
+ if (props.downloadSrc) return props.downloadSrc;
245
+
246
+ if (props.imageList.length > 0 && currentIndex.value < props.imageList.length) {
247
+ const currentImage = props.imageList[currentIndex.value];
248
+ if (currentImage && typeof currentImage === 'object') {
249
+ return currentImage.downloadUrl || currentImage.downloadSrc || currentImage.url || props.src;
250
+ }
251
+ }
252
+
253
+ return currentImageRawUrl.value;
254
+ });
255
+
221
256
  // 当前图片标题
222
257
  const currentImageTitle = computed(() => {
223
258
  if (props.imageList.length > 0 && currentIndex.value < props.imageList.length) {
@@ -250,9 +285,228 @@ const viewerImageStyle = computed(() => ({
250
285
  transform: `translate(${imageTranslateX.value}px, ${imageTranslateY.value}px) scale(${imageScale.value}) rotate(${imageRotation.value}deg)`,
251
286
  }));
252
287
 
253
- const resetImageTransform = () => {
254
- imageRotation.value = 0;
255
- imageScale.value = 1;
288
+ const openCurrentImageInNewWindow = () => {
289
+ if (!currentImageRawUrl.value) return;
290
+
291
+ window.open(currentImageRawUrl.value, '_blank', 'noopener');
292
+ };
293
+
294
+ const imageMimeExtensionMap = {
295
+ 'image/bmp': 'bmp',
296
+ 'image/gif': 'gif',
297
+ 'image/jpeg': 'jpg',
298
+ 'image/png': 'png',
299
+ 'image/svg+xml': 'svg',
300
+ 'image/webp': 'webp',
301
+ };
302
+
303
+ const imageExtensionMimeMap = {
304
+ bmp: 'image/bmp',
305
+ gif: 'image/gif',
306
+ jpeg: 'image/jpeg',
307
+ jpg: 'image/jpeg',
308
+ png: 'image/png',
309
+ svg: 'image/svg+xml',
310
+ webp: 'image/webp',
311
+ };
312
+
313
+ const getImageFileExtension = (blob) => {
314
+ const mimeType = blob?.type?.split(';')[0]?.toLowerCase();
315
+
316
+ if (mimeType && imageMimeExtensionMap[mimeType]) {
317
+ return imageMimeExtensionMap[mimeType];
318
+ }
319
+
320
+ try {
321
+ const { pathname } = new URL(currentImageRawUrl.value, window.location.href);
322
+ const matched = pathname.match(/\.([a-z0-9]+)$/i);
323
+ return matched?.[1]?.toLowerCase() || 'png';
324
+ } catch (error) {
325
+ return 'png';
326
+ }
327
+ };
328
+
329
+ const getCurrentImageFileName = (blob) => {
330
+ const extension = getImageFileExtension(blob);
331
+ const titleName = currentImageTitle.value?.trim();
332
+
333
+ if (titleName) {
334
+ const safeTitle = titleName.replace(/[\\/:*?"<>|]/g, '_').replace(/\.[a-z0-9]+$/i, '');
335
+ return `${safeTitle}.${extension}`;
336
+ }
337
+
338
+ try {
339
+ const { pathname } = new URL(currentImageRawUrl.value, window.location.href);
340
+ const urlName = decodeURIComponent(pathname.split('/').pop() || '')
341
+ .replace(/[\\/:*?"<>|]/g, '_')
342
+ .replace(/\.[a-z0-9]+$/i, '');
343
+ return urlName ? `${urlName}.${extension}` : `image.${extension}`;
344
+ } catch (error) {
345
+ return `image.${extension}`;
346
+ }
347
+ };
348
+
349
+ const triggerBrowserDownload = (blob, fileName) => {
350
+ const url = window.URL.createObjectURL(blob);
351
+ const link = document.createElement('a');
352
+ link.href = url;
353
+ link.download = fileName;
354
+ document.body.appendChild(link);
355
+ link.click();
356
+ document.body.removeChild(link);
357
+ window.URL.revokeObjectURL(url);
358
+ };
359
+
360
+ const triggerBrowserUrlDownload = (url, fileName) => {
361
+ const link = document.createElement('a');
362
+ link.href = url;
363
+ link.download = fileName;
364
+ link.target = '_blank';
365
+ link.rel = 'noopener';
366
+ document.body.appendChild(link);
367
+ link.click();
368
+ document.body.removeChild(link);
369
+ };
370
+
371
+ const showDownloadToast = (message, color = 'info') => {
372
+ try {
373
+ const toast = proxy?.$vtk?.message?.toast || window.$vtk?.message?.toast;
374
+ if (typeof toast === 'function') {
375
+ toast(message, { color });
376
+ return;
377
+ }
378
+ } catch (error) {
379
+ console.warn('VtkImg: failed to show message toast', error);
380
+ }
381
+
382
+ if (color === 'error' || color === 'warning') {
383
+ window.alert?.(message);
384
+ }
385
+ };
386
+
387
+ const isCrossOriginUrl = (url) => {
388
+ try {
389
+ return new URL(url, window.location.href).origin !== window.location.origin;
390
+ } catch (error) {
391
+ return false;
392
+ }
393
+ };
394
+
395
+ const getDownloadFileHandle = async (fileName, blob) => {
396
+ if (typeof window.showSaveFilePicker !== 'function') {
397
+ showDownloadToast('当前浏览器不支持选择保存目录,将使用默认下载方式。', 'warning');
398
+ return null;
399
+ }
400
+
401
+ const extension = getImageFileExtension(blob);
402
+ const mimeType = imageExtensionMimeMap[extension] || blob?.type || 'image/png';
403
+
404
+ try {
405
+ return await window.showSaveFilePicker({
406
+ suggestedName: fileName,
407
+ types: [
408
+ {
409
+ description: '图片文件',
410
+ accept: {
411
+ [mimeType]: [`.${extension}`],
412
+ },
413
+ },
414
+ ],
415
+ });
416
+ } catch (error) {
417
+ if (error?.name === 'AbortError') {
418
+ showDownloadToast('已取消下载。');
419
+ return false;
420
+ }
421
+
422
+ console.warn('VtkImg: showSaveFilePicker unavailable, fallback to browser download', error);
423
+ return null;
424
+ }
425
+ };
426
+
427
+ const isImageResponse = (response, blob, url) => {
428
+ const contentType = response.headers.get('content-type')?.split(';')[0]?.trim().toLowerCase();
429
+
430
+ if (contentType?.startsWith('image/')) return true;
431
+ if (blob.type?.startsWith('image/')) return true;
432
+
433
+ const canFallbackToExtension = !contentType
434
+ || contentType === 'application/octet-stream'
435
+ || contentType === 'binary/octet-stream';
436
+ if (!canFallbackToExtension) return false;
437
+
438
+ try {
439
+ const { pathname } = new URL(url, window.location.href);
440
+ const extension = pathname.match(/\.([a-z0-9]+)$/i)?.[1]?.toLowerCase();
441
+ return Boolean(extension && imageExtensionMimeMap[extension]);
442
+ } catch (error) {
443
+ return false;
444
+ }
445
+ };
446
+
447
+ const fetchImageBlob = async (url) => {
448
+ const response = await fetch(url);
449
+ if (!response.ok) {
450
+ throw new Error(`HTTP error! status: ${response.status}`);
451
+ }
452
+
453
+ const blob = await response.blob();
454
+ if (!isImageResponse(response, blob, url)) {
455
+ const contentType = response.headers.get('content-type') || blob.type || 'unknown';
456
+ throw new Error(`Response is not an image: ${contentType}`);
457
+ }
458
+
459
+ return blob;
460
+ };
461
+
462
+ const fetchCurrentImageBlob = async () => {
463
+ if (!currentImageDownloadUrl.value) {
464
+ throw new Error('Image download url is empty');
465
+ }
466
+
467
+ return fetchImageBlob(currentImageDownloadUrl.value);
468
+ };
469
+
470
+ const downloadCurrentImage = async () => {
471
+ const downloadUrl = currentImageDownloadUrl.value;
472
+
473
+ if (!downloadUrl) {
474
+ showDownloadToast('暂无可下载的图片地址。', 'warning');
475
+ return;
476
+ }
477
+
478
+ if (isCrossOriginUrl(downloadUrl)) {
479
+ triggerBrowserUrlDownload(downloadUrl, getCurrentImageFileName());
480
+ showDownloadToast('图片为跨域地址,已切换为浏览器下载。', 'warning');
481
+ return;
482
+ }
483
+
484
+ try {
485
+ const blob = await fetchCurrentImageBlob();
486
+ const fileName = getCurrentImageFileName(blob);
487
+ const fileHandle = await getDownloadFileHandle(fileName, blob);
488
+ if (fileHandle === false) return;
489
+
490
+ if (fileHandle) {
491
+ const writable = await fileHandle.createWritable();
492
+ await writable.write(blob);
493
+ await writable.close();
494
+ showDownloadToast('图片下载成功。', 'success');
495
+ return;
496
+ }
497
+
498
+ triggerBrowserDownload(blob, fileName);
499
+ showDownloadToast('图片已开始下载。', 'success');
500
+ } catch (error) {
501
+ console.error('下载图片失败:图片地址不允许跨域读取,请配置资源 CORS 或传入同源 downloadSrc。', error);
502
+ triggerBrowserUrlDownload(downloadUrl, getCurrentImageFileName());
503
+ showDownloadToast('图片下载失败,已切换为浏览器下载。', 'warning');
504
+ }
505
+ };
506
+
507
+ const resetImageTransform = () => {
508
+ imageRotation.value = 0;
509
+ imageScale.value = 1;
256
510
  imageTranslateX.value = 0;
257
511
  imageTranslateY.value = 0;
258
512
  imageDragState.value = null;