Skip to content

Commit

Permalink
Refactor: デフォルトファイル名取得関数2つをgetterへ移動 (#1490)
Browse files Browse the repository at this point in the history
  • Loading branch information
thiramisu authored Aug 8, 2023
1 parent f67275d commit ee088a6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 65 deletions.
100 changes: 60 additions & 40 deletions src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import {
} from "./type";
import {
buildAudioFileNameFromRawData,
buildProjectFileName,
isAccentPhrasesTextDifferent,
convertHiraToKana,
convertLongVowel,
createKanaRegex,
currentDateString,
extractExportText,
extractYomiText,
sanitizeFileName,
} from "./utility";
import { convertAudioQueryFromEditorToEngine } from "./proxy";
import { createPartialStore } from "./vuex";
Expand Down Expand Up @@ -171,36 +171,6 @@ export async function writeTextFile(obj: {
});
}

// TODO: GETTERに移動する
function buildFileName(state: State, audioKey: AudioKey) {
const fileNamePattern = state.savingSetting.fileNamePattern;

const index = state.audioKeys.indexOf(audioKey);
const audioItem = state.audioItems[audioKey];

const character = getCharacterInfo(
state,
audioItem.voice.engineId,
audioItem.voice.styleId
);
if (character === undefined)
throw new Error("assert character !== undefined");

const style = character.metas.styles.find(
(style) => style.styleId === audioItem.voice.styleId
);
if (style === undefined) throw new Error("assert style !== undefined");

const styleName = style.styleName || "ノーマル";
return buildAudioFileNameFromRawData(fileNamePattern, {
characterName: character.metas.speakerName,
index,
styleName,
text: audioItem.text,
date: currentDateString(),
});
}

function generateWriteErrorMessage(writeFileResult: ResultError) {
if (writeFileResult.code) {
const code = writeFileResult.code.toUpperCase();
Expand All @@ -221,7 +191,7 @@ function generateWriteErrorMessage(writeFileResult: ResultError) {
return `何らかの理由で失敗しました。${writeFileResult.message}`;
}

// TODO: GETTERに移動する。buildFileNameから参照されているので、そちらも一緒に移動する。
// TODO: GETTERに移動する。
export function getCharacterInfo(
state: State,
engineId: EngineId,
Expand Down Expand Up @@ -1137,6 +1107,55 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
},
},

DEFAULT_PROJECT_FILE_BASE_NAME: {
getter: (state) => {
const headItemText = state.audioItems[state.audioKeys[0]].text;

const tailItemText =
state.audioItems[state.audioKeys[state.audioKeys.length - 1]].text;

const headTailItemText =
state.audioKeys.length === 1
? headItemText
: headItemText + "..." + tailItemText;

const defaultFileBaseName = sanitizeFileName(headTailItemText);

return defaultFileBaseName === "" ? "Untitled" : defaultFileBaseName;
},
},

DEFAULT_AUDIO_FILE_NAME: {
getter: (state) => (audioKey) => {
const fileNamePattern = state.savingSetting.fileNamePattern;

const index = state.audioKeys.indexOf(audioKey);
const audioItem = state.audioItems[audioKey];

const character = getCharacterInfo(
state,
audioItem.voice.engineId,
audioItem.voice.styleId
);
if (character === undefined)
throw new Error("assert character !== undefined");

const style = character.metas.styles.find(
(style) => style.styleId === audioItem.voice.styleId
);
if (style === undefined) throw new Error("assert style !== undefined");

const styleName = style.styleName || "ノーマル";
return buildAudioFileNameFromRawData(fileNamePattern, {
characterName: character.metas.speakerName,
index,
styleName,
text: audioItem.text,
date: currentDateString(),
});
},
},

GENERATE_LAB: {
action: createUILockAction(
async (
Expand Down Expand Up @@ -1321,7 +1340,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
GENERATE_AND_SAVE_AUDIO: {
action: createUILockAction(
async (
{ state, dispatch },
{ state, getters, dispatch },
{
audioKey,
filePath,
Expand All @@ -1330,15 +1349,16 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
filePath?: string;
}
): Promise<SaveResultObject> => {
const defaultAudioFileName = getters.DEFAULT_AUDIO_FILE_NAME(audioKey);
if (state.savingSetting.fixedExportEnabled) {
filePath = path.join(
state.savingSetting.fixedExportDir,
buildFileName(state, audioKey)
defaultAudioFileName
);
} else {
filePath ??= await window.electron.showAudioSaveDialog({
title: "音声を保存",
defaultPath: buildFileName(state, audioKey),
defaultPath: defaultAudioFileName,
});
}

Expand Down Expand Up @@ -1426,7 +1446,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
GENERATE_AND_SAVE_ALL_AUDIO: {
action: createUILockAction(
async (
{ state, dispatch },
{ state, getters, dispatch },
{
dirPath,
callback,
Expand All @@ -1449,7 +1469,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
let finishedCount = 0;

const promises = state.audioKeys.map((audioKey) => {
const name = buildFileName(state, audioKey);
const name = getters.DEFAULT_AUDIO_FILE_NAME(audioKey);
return dispatch("GENERATE_AND_SAVE_AUDIO", {
audioKey,
filePath: path.join(_dirPath, name),
Expand All @@ -1467,7 +1487,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
GENERATE_AND_CONNECT_AND_SAVE_AUDIO: {
action: createUILockAction(
async (
{ state, dispatch },
{ state, getters, dispatch },
{
filePath,
callback,
Expand All @@ -1476,7 +1496,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
callback?: (finishedCount: number, totalCount: number) => void;
}
): Promise<SaveResultObject> => {
const defaultFileName = buildProjectFileName(state, "wav");
const defaultFileName = `${getters.DEFAULT_PROJECT_FILE_BASE_NAME}.wav`;

if (state.savingSetting.fixedExportEnabled) {
filePath = path.join(
Expand Down Expand Up @@ -1617,7 +1637,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
{ state, getters },
{ filePath }: { filePath?: string }
): Promise<SaveResultObject> => {
const defaultFileName = buildProjectFileName(state, "txt");
const defaultFileName = `${getters.DEFAULT_PROJECT_FILE_BASE_NAME}.txt`;
if (state.savingSetting.fixedExportEnabled) {
filePath = path.join(
state.savingSetting.fixedExportDir,
Expand Down
4 changes: 2 additions & 2 deletions src/store/project.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import semver from "semver";
import { z } from "zod";
import { buildProjectFileName, getBaseName } from "./utility";
import { getBaseName } from "./utility";
import { createPartialStore } from "./vuex";
import { createUILockAction } from "@/store/ui";
import { AudioItem, ProjectStoreState, ProjectStoreTypes } from "@/store/type";
Expand Down Expand Up @@ -393,7 +393,7 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({

if (!filePath) {
// if new project: use generated name
defaultPath = buildProjectFileName(context.state, "vvproj");
defaultPath = `${context.getters.DEFAULT_PROJECT_FILE_BASE_NAME}.vvproj`;
} else {
// if saveAs for existing project: use current project path
defaultPath = filePath;
Expand Down
8 changes: 8 additions & 0 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,14 @@ export type AudioStoreTypes = {
}): Promise<AccentPhrase[]>;
};

DEFAULT_PROJECT_FILE_BASE_NAME: {
getter: string;
};

DEFAULT_AUDIO_FILE_NAME: {
getter(audioKey: AudioKey): string;
};

GENERATE_LAB: {
action(payload: {
audioKey: AudioKey;
Expand Down
23 changes: 0 additions & 23 deletions src/store/utility.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from "path";
import { Platform } from "quasar";
import { State } from "@/store/type";
import { ToolbarButtonTagType, isMac } from "@/type/preload";
import { AccentPhrase } from "@/openapi";

Expand All @@ -26,28 +25,6 @@ export function sanitizeFileName(fileName: string): string {
return fileName.replace(sanitizer, "");
}

export function buildProjectFileName(state: State, extension?: string): string {
const headItemText = state.audioItems[state.audioKeys[0]].text;

const tailItemText =
state.audioItems[state.audioKeys[state.audioKeys.length - 1]].text;

const headTailItemText =
state.audioKeys.length === 1
? headItemText
: headItemText + "..." + tailItemText;

let defaultFileNameStem = sanitizeFileName(headTailItemText);

if (defaultFileNameStem === "") {
defaultFileNameStem = "Untitled";
}

return extension
? `${defaultFileNameStem}.${extension}`
: defaultFileNameStem;
}

export const replaceTagIdToTagString = {
index: "連番",
characterName: "キャラ",
Expand Down

0 comments on commit ee088a6

Please sign in to comment.