Skip to content

Commit c1f2b0e

Browse files
committed
feat: 文件即时预览支持展示报错的响应详情; 修复文件即时预览失败时, 显示的是上次成功结果的问题; age key helper 样式优化
1 parent bc16163 commit c1f2b0e

5 files changed

Lines changed: 75 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store-front-end",
3-
"version": "2.26.6",
3+
"version": "2.26.7",
44
"private": true,
55
"packageManager": "pnpm@11.0.9",
66
"scripts": {

src/components/AgeKeyHelper.vue

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ const copyValue = async (value: string) => {
386386
.age-key-textarea {
387387
width: 100%;
388388
background: transparent !important;
389+
padding: 0 !important;
389390
font-size: 11px;
390391
line-height: 1.4;
391392
color: var(--primary-text-color);
@@ -395,15 +396,21 @@ const copyValue = async (value: string) => {
395396
}
396397
397398
:deep(.nut-textarea__textarea) {
399+
box-sizing: border-box;
398400
background: transparent !important;
399401
color: var(--primary-text-color);
400402
caret-color: var(--primary-color);
401-
padding-right: 34px;
402-
font-size: 11px;
403-
line-height: 1.4;
403+
padding: 8px 34px 8px 10px;
404+
font-size: 12px;
405+
line-height: 1.45;
406+
text-align: left !important;
407+
word-break: break-word;
404408
405409
&::placeholder {
406410
color: var(--comment-text-color, #8a8a8a);
411+
font-size: 12px;
412+
line-height: 1.45;
413+
opacity: 1;
407414
}
408415
}
409416
}

src/components/FileListItem.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
import { useBackend } from "@/hooks/useBackend";
271271
import clashmetaIcon from '@/assets/icons/clashmeta_color.png';
272272
import { isMihomoConfigFileType } from "@/utils/fileType";
273+
import { formatPreviewError } from "@/utils/previewError";
273274
274275
const { copy, isSupported } = useClipboard();
275276
const { toClipboard: copyFallback } = useV3Clipboard();
@@ -429,11 +430,11 @@
429430
if (res?.data?.status === 'success') {
430431
previewData.value = res.data.data;
431432
} else {
432-
previewData.value = null;
433+
previewData.value = { processed: formatPreviewError(res) };
433434
}
434435
} catch (e) {
435436
console.error(e);
436-
previewData.value = null;
437+
previewData.value = { processed: formatPreviewError(e) };
437438
}
438439
Toast.hide('compare');
439440
};
@@ -466,7 +467,7 @@
466467
subsStore.setOneData('files', name, latestFile);
467468
} catch (e) {
468469
console.error(e);
469-
previewData.value = null;
470+
previewData.value = { processed: formatPreviewError(e) };
470471
Toast.hide('compare');
471472
}
472473
};

src/utils/previewError.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
type PreviewErrorResponse = {
2+
status?: number | string;
3+
statusText?: string;
4+
data?: unknown;
5+
};
6+
7+
const isObject = (value: unknown): value is Record<string, unknown> => {
8+
return typeof value === "object" && value !== null;
9+
};
10+
11+
const stringifyPreviewErrorData = (data: unknown) => {
12+
if (data == null) return "";
13+
14+
if (typeof data === "string") {
15+
try {
16+
return JSON.stringify(JSON.parse(data), null, 2);
17+
} catch {
18+
return data;
19+
}
20+
}
21+
22+
try {
23+
return JSON.stringify(data, null, 2);
24+
} catch {
25+
return String(data);
26+
}
27+
};
28+
29+
const getPreviewErrorResponse = (error: unknown): PreviewErrorResponse | undefined => {
30+
if (!isObject(error)) return undefined;
31+
32+
if (isObject(error.response)) {
33+
return error.response as PreviewErrorResponse;
34+
}
35+
36+
if ("data" in error) {
37+
return error as PreviewErrorResponse;
38+
}
39+
40+
return undefined;
41+
};
42+
43+
export const formatPreviewError = (error: unknown) => {
44+
const response = getPreviewErrorResponse(error);
45+
const data = stringifyPreviewErrorData(response?.data);
46+
47+
if (response) {
48+
const status = [response.status, response.statusText].filter(Boolean).join(" ");
49+
return `Error: ${status || "请求失败"}${data ? `\n\n${data}` : ""}`;
50+
}
51+
52+
const message = isObject(error) && typeof error.message === "string"
53+
? error.message
54+
: String(error ?? "请求失败");
55+
56+
return `Error: ${message}`;
57+
};

src/views/FileEditor.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ import {
574574
MIHOMO_CONFIG_FILE_TYPE,
575575
normalizeFileType,
576576
} from "@/utils/fileType";
577+
import { formatPreviewError } from "@/utils/previewError";
577578
578579
const cmStore = useCodeStore();
579580
const { t, locale } = useI18n();
@@ -1041,11 +1042,11 @@ const fetchPreviewData = async () => {
10411042
if (res?.data?.status === "success") {
10421043
previewData.value = res.data.data;
10431044
} else {
1044-
previewData.value = null;
1045+
previewData.value = { processed: formatPreviewError(res) };
10451046
}
10461047
} catch (e) {
10471048
console.error(e);
1048-
previewData.value = null;
1049+
previewData.value = { processed: formatPreviewError(e) };
10491050
}
10501051
Toast.hide("compare");
10511052
};

0 commit comments

Comments
 (0)