diff --git a/src/store/audio.ts b/src/store/audio.ts index 4e70af9838..9229c7fd30 100644 --- a/src/store/audio.ts +++ b/src/store/audio.ts @@ -129,6 +129,17 @@ function parseTextFile( return audioItems; } +async function changeFileTailToNonExistent(filePath: string) { + const EXTENSION = "wav"; + let tail = 1; + const name = filePath.slice(0, filePath.length - 1 - EXTENSION.length); + while (await window.electron.checkFileExists(filePath)) { + filePath = `${name}[${tail}].${EXTENSION}`; + tail += 1; + } + return filePath; +} + export async function writeTextFile(obj: { filePath: string; text: string; @@ -1336,12 +1347,7 @@ export const audioStore = createPartialStore({ } if (state.savingSetting.avoidOverwrite) { - let tail = 1; - const name = filePath.slice(0, filePath.length - 4); - while (await dispatch("CHECK_FILE_EXISTS", { file: filePath })) { - filePath = name + "[" + tail.toString() + "]" + ".wav"; - tail += 1; - } + filePath = await changeFileTailToNonExistent(filePath); } let blob = await dispatch("GET_AUDIO_CACHE", { audioKey }); @@ -1489,12 +1495,7 @@ export const audioStore = createPartialStore({ } if (state.savingSetting.avoidOverwrite) { - let tail = 1; - const name = filePath.slice(0, filePath.length - 4); - while (await dispatch("CHECK_FILE_EXISTS", { file: filePath })) { - filePath = name + "[" + tail.toString() + "]" + ".wav"; - tail += 1; - } + filePath = await changeFileTailToNonExistent(filePath); } const encodedBlobs: string[] = []; @@ -1613,7 +1614,7 @@ export const audioStore = createPartialStore({ CONNECT_AND_EXPORT_TEXT: { action: createUILockAction( async ( - { state, dispatch, getters }, + { state, getters }, { filePath }: { filePath?: string } ): Promise => { const defaultFileName = buildProjectFileName(state, "txt"); @@ -1634,12 +1635,7 @@ export const audioStore = createPartialStore({ } if (state.savingSetting.avoidOverwrite) { - let tail = 1; - const name = filePath.slice(0, filePath.length - 4); - while (await dispatch("CHECK_FILE_EXISTS", { file: filePath })) { - filePath = name + "[" + tail.toString() + "]" + ".wav"; - tail += 1; - } + filePath = await changeFileTailToNonExistent(filePath); } const characters = new Map(); @@ -1864,12 +1860,6 @@ export const audioStore = createPartialStore({ } }, }, - - CHECK_FILE_EXISTS: { - action(_, { file }: { file: string }) { - return window.electron.checkFileExists(file); - }, - }, }); export const audioCommandStoreState: AudioCommandStoreState = {}; diff --git a/src/store/type.ts b/src/store/type.ts index 0bc062f90c..dc312c1f20 100644 --- a/src/store/type.ts +++ b/src/store/type.ts @@ -465,10 +465,6 @@ export type AudioStoreTypes = { STOP_CONTINUOUSLY_AUDIO: { action(): void; }; - - CHECK_FILE_EXISTS: { - action(payload: { file: string }): Promise; - }; }; /*