Skip to content

Commit fa49345

Browse files
committed
refactor: 将导入的输入逻辑移到 composables 中
1 parent 50d9f14 commit fa49345

2 files changed

Lines changed: 41 additions & 25 deletions

File tree

src/components/layout/TopMenuBar.vue

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<Upload class="ui-icon" aria-hidden="true" />
8282
</button>
8383
<input
84-
ref="fileInput"
84+
:ref="setFileInput"
8585
type="file"
8686
accept=".txt,.json,application/json"
8787
multiple
@@ -118,23 +118,20 @@ import { useClipboardStore } from '@/stores/clipboardStore';
118118
import { useHistoryStore } from '@/stores/historyStore';
119119
import { useWindowStore } from '@/stores/windowStore';
120120
import { useActiveContext } from '@/composables/useActiveContext';
121-
import { useFileImport } from '@/composables/useImporter';
121+
import { useFileImportInput } from '@/composables/useImporter';
122122
import { useProjectManager } from '@/composables/useProjectManager';
123123
import { vClickOutside } from '@/directives/clickOutside';
124124
import type { ProjectFile } from '@/types/project';
125125
import StoredProjectsPopover from '@/components/popovers/StoredProjectsPopover.vue';
126126
import ExportPopover from '@/components/popovers/ExportPopover.vue';
127127
128-
const fileInput = ref<HTMLInputElement | null>(null);
129-
130128
const logStore = useLogStore();
131129
const styleStore = useStyleStore();
132130
const clipboardStore = useClipboardStore();
133131
const historyStore = useHistoryStore();
134132
const windowStore = useWindowStore();
135-
136133
const activeContext = useActiveContext();
137-
const { importAndApply } = useFileImport();
134+
const { setFileInput, triggerImport, handleFileChange } = useFileImportInput();
138135
const projectManager = useProjectManager();
139136
140137
const showExportPopover = ref(false);
@@ -172,25 +169,6 @@ const hasWorkspaceState = computed(() => {
172169
);
173170
});
174171
175-
function triggerImport() {
176-
fileInput.value?.click();
177-
}
178-
179-
async function handleFileChange(event: Event) {
180-
const target = event.target as HTMLInputElement;
181-
const files = target.files;
182-
if (!files?.length) return;
183-
184-
try {
185-
await importAndApply(Array.from(files));
186-
} catch (error) {
187-
console.error(error);
188-
alert(error instanceof Error ? error.message : '解析文件时发生错误');
189-
} finally {
190-
target.value = '';
191-
}
192-
}
193-
194172
function refreshStoredProjects() {
195173
storedProjects.value = projectManager.getStoredProjects();
196174
}

src/composables/useImporter.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ref } from 'vue';
2+
import type { ComponentPublicInstance } from 'vue';
13
import { useLogStore } from '@/stores/logStore';
24
import { useStyleStore } from '@/stores/styleStore';
35
import { useWindowStore } from '@/stores/windowStore';
@@ -149,3 +151,39 @@ export function useFileImport() {
149151
importAndApply,
150152
};
151153
}
154+
155+
export function useFileImportInput() {
156+
const fileInput = ref<HTMLInputElement | null>(null);
157+
const { importAndApply } = useFileImport();
158+
159+
function setFileInput(element: Element | ComponentPublicInstance | null) {
160+
fileInput.value = element instanceof HTMLInputElement ? element : null;
161+
}
162+
163+
function triggerImport() {
164+
fileInput.value?.click();
165+
}
166+
167+
async function handleFileChange(event: Event) {
168+
const target = event.target as HTMLInputElement;
169+
const files = target.files;
170+
if (!files?.length) return;
171+
172+
try {
173+
await importAndApply(Array.from(files));
174+
} catch (error) {
175+
console.error(error);
176+
alert(
177+
error instanceof Error ? error.message : '解析文件时发生错误',
178+
);
179+
} finally {
180+
target.value = '';
181+
}
182+
}
183+
184+
return {
185+
setFileInput,
186+
triggerImport,
187+
handleFileChange,
188+
};
189+
}

0 commit comments

Comments
 (0)