Skip to content

Commit 6a3285d

Browse files
committed
feat: 导入时将自动删除文件后缀名
- 增加了文件名提取的通用工具 - 将首次导入的 document / project 名改为删除后缀的名称
1 parent 3b29e50 commit 6a3285d

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/composables/useImporter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { Chunk, LogDocument } from '@/types/log';
77
import { buildLogDocument } from '@/io/import/parser';
88
import { dispatchAdapter } from '@/io/import/importAdapters';
99
import { tryParseProjectFile } from '@/io/localStorage/project';
10+
import { stripFileExtension } from '@/utils/fileName';
1011
import { useProjectManager } from './useProjectManager';
1112

1213
// 统一换行符并移除0宽字符,防止正则崩掉
@@ -133,7 +134,10 @@ export function useFileImport() {
133134
}
134135

135136
const documents = await importFiles(
136-
fileEntries.map((e) => ({ name: e.file.name, text: e.text })),
137+
fileEntries.map((e) => ({
138+
name: stripFileExtension(e.file.name),
139+
text: e.text,
140+
})),
137141
logStore.documents.length,
138142
);
139143

src/io/localStorage/project.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ColorRule, ViewSettings } from '@/types/style';
22
import type { LogDocument, MessageFilter } from '@/types/log';
33
import { isRoleType } from '@/types/log';
44
import type { ProjectFile } from '@/types/project';
5+
import { stripFileExtension } from '@/utils/fileName';
56
import { generateId } from '@/utils/id';
67

78
export const PROJECT_FILE_VERSION = 1 as const;
@@ -240,7 +241,7 @@ function normalizeDocuments(rawDocuments: unknown): LogDocument[] {
240241
docId: typeof doc.docId === 'string' ? doc.docId : generateId(),
241242
docName:
242243
typeof doc.docName === 'string'
243-
? doc.docName
244+
? stripFileExtension(doc.docName)
244245
: `Document-${docIndex + 1}`,
245246
docIndex:
246247
typeof doc.docIndex === 'number' ? doc.docIndex : docIndex,
@@ -402,7 +403,7 @@ export function normalizeProjectFile(
402403
: rawProject.projectId;
403404
const projectName =
404405
typeof rawProject.projectName === 'string'
405-
? rawProject.projectName.trim()
406+
? stripFileExtension(rawProject.projectName)
406407
: '';
407408
const time =
408409
typeof rawProject.time === 'string' &&

src/utils/fileName.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function stripFileExtension(fileName: string): string {
2+
const trimmedName = fileName.trim();
3+
const extensionStart = trimmedName.lastIndexOf('.');
4+
5+
if (extensionStart <= 0) {
6+
return trimmedName;
7+
}
8+
9+
return trimmedName.slice(0, extensionStart) || trimmedName;
10+
}

0 commit comments

Comments
 (0)