Skip to content

Commit 0b2df6f

Browse files
committed
feat: 导入后自动打开第一个 chunk 进行编辑
1 parent ecb862f commit 0b2df6f

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

src/composables/useImporter.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useLogStore } from '@/stores/logStore';
22
import { useStyleStore } from '@/stores/styleStore';
3-
import type { LogDocument } from '@/types/log';
3+
import { useWindowStore } from '@/stores/windowStore';
4+
import type { Chunk, LogDocument } from '@/types/log';
45
import { buildLogDocument } from '@/io/import/parser';
56
import { dispatchAdapter } from '@/io/import/importAdapters';
67
import { tryParseProjectFile } from '@/io/localStorage/project';
@@ -41,8 +42,46 @@ export async function importFiles(
4142
export function useFileImport() {
4243
const logStore = useLogStore();
4344
const styleStore = useStyleStore();
45+
const windowStore = useWindowStore();
4446
const projectManager = useProjectManager();
4547

48+
function getFirstChunk(): Chunk | null {
49+
let firstChunk: Chunk | null = null;
50+
let firstDocIndex = Number.POSITIVE_INFINITY;
51+
let firstChunkIndex = Number.POSITIVE_INFINITY;
52+
53+
for (const doc of logStore.documents) {
54+
for (const chunk of doc.chunks) {
55+
if (
56+
doc.docIndex < firstDocIndex ||
57+
(doc.docIndex === firstDocIndex &&
58+
chunk.chunkIndex < firstChunkIndex)
59+
) {
60+
firstChunk = chunk;
61+
firstDocIndex = doc.docIndex;
62+
firstChunkIndex = chunk.chunkIndex;
63+
}
64+
}
65+
}
66+
67+
return firstChunk;
68+
}
69+
70+
function hasOpenedChunkView(): boolean {
71+
return Array.from(windowStore.openWindows.values()).some(
72+
(win) => win.windowName === 'chunkView',
73+
);
74+
}
75+
76+
// 自动打开第一个场景进入编辑
77+
function openFirstChunkViewIfNeeded() {
78+
if (hasOpenedChunkView()) return;
79+
const firstChunk = getFirstChunk();
80+
if (firstChunk) {
81+
windowStore.setActiveChunk(firstChunk.chunkId);
82+
}
83+
}
84+
4685
async function importAndApply(files: File[]): Promise<number> {
4786
const fileEntries = await Promise.all(
4887
files.map(async (file) => {
@@ -85,6 +124,9 @@ export function useFileImport() {
85124
confirmIfNeeded: true,
86125
},
87126
);
127+
if (applied) {
128+
openFirstChunkViewIfNeeded();
129+
}
88130
return applied ? 1 : 0;
89131
}
90132

@@ -99,6 +141,7 @@ export function useFileImport() {
99141

100142
logStore.appendDocuments(documents);
101143
styleStore.syncSystemRulesFromMessages(logStore.allMessages);
144+
openFirstChunkViewIfNeeded();
102145
return documents.length;
103146
}
104147

0 commit comments

Comments
 (0)