Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEFAULT_FILE_NAME_TEMPLATEの拡張子無し版を定義 #1483

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 22 additions & 38 deletions src/components/FileNamePatternDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div class="col">
<q-input
ref="patternInput"
v-model="currentFileNamePattern"
v-model="currentBaseNamePattern"
dense
outlined
bg-color="background"
Expand Down Expand Up @@ -80,8 +80,8 @@ import { computed, ref, nextTick } from "vue";
import { QInput } from "quasar";
import { useStore } from "@/store";
import {
buildFileNameFromRawData,
DEFAULT_FILE_NAME_TEMPLATE,
buildAudioFileNameFromRawData,
DEFAULT_AUDIO_FILE_BASE_NAME_TEMPLATE,
replaceTagIdToTagString,
sanitizeFileName,
} from "@/store/utility";
Expand All @@ -105,40 +105,29 @@ const tagStrings = Object.values(replaceTagIdToTagString);

const savingSetting = computed(() => store.state.savingSetting);

const currentFileNamePattern = ref(savingSetting.value.fileNamePattern);
const sanitizedFileNamePattern = computed(() =>
sanitizeFileName(currentFileNamePattern.value)
const savedBaseNamePattern = computed(() => {
return savingSetting.value.fileNamePattern.replace(/\.wav$/, "");
});
const currentBaseNamePattern = ref(savedBaseNamePattern.value);
const currentNamePattern = computed(
() => `${currentBaseNamePattern.value}.wav`
);

const hasInvalidChar = computed(
() => currentFileNamePattern.value !== sanitizedFileNamePattern.value
);
const hasNotIndexTagString = computed(
() => !currentFileNamePattern.value.includes(replaceTagIdToTagString["index"])
() => !currentBaseNamePattern.value.includes(replaceTagIdToTagString["index"])
);
const invalidChar = computed(() => {
if (!hasInvalidChar.value) return "";

const a = currentFileNamePattern.value;
const b = sanitizedFileNamePattern.value;

let diffAt = "";
for (let i = 0; i < a.length; i++) {
if (b[i] !== a[i]) {
diffAt = a[i];
break;
}
}

return diffAt;
const current = currentBaseNamePattern.value;
const sanitized = sanitizeFileName(current);
return Array.from(current).find((char, i) => char !== sanitized[i]);
});
const errorMessage = computed(() => {
if (currentFileNamePattern.value.length === 0) {
if (currentBaseNamePattern.value === "") {
return "何か入力してください";
}

const result: string[] = [];
if (invalidChar.value !== "") {
if (invalidChar.value !== undefined) {
result.push(`使用できない文字が含まれています:「${invalidChar.value}」`);
}
if (previewFileName.value.includes("$")) {
Expand All @@ -152,23 +141,18 @@ const errorMessage = computed(() => {
const hasError = computed(() => errorMessage.value !== "");

const previewFileName = computed(() =>
buildFileNameFromRawData(currentFileNamePattern.value + ".wav")
buildAudioFileNameFromRawData(currentNamePattern.value)
);

const removeExtension = (str: string) => {
return str.replace(/\.wav$/, "");
};

const initializeInput = () => {
const pattern = savingSetting.value.fileNamePattern;
currentFileNamePattern.value = removeExtension(pattern);
currentBaseNamePattern.value = savedBaseNamePattern.value;

if (currentFileNamePattern.value.length === 0) {
currentFileNamePattern.value = removeExtension(DEFAULT_FILE_NAME_TEMPLATE);
if (currentBaseNamePattern.value === "") {
currentBaseNamePattern.value = DEFAULT_AUDIO_FILE_BASE_NAME_TEMPLATE;
}
};
const resetToDefault = () => {
currentFileNamePattern.value = removeExtension(DEFAULT_FILE_NAME_TEMPLATE);
currentBaseNamePattern.value = DEFAULT_AUDIO_FILE_BASE_NAME_TEMPLATE;
patternInput.value?.focus();
};

Expand All @@ -184,7 +168,7 @@ const insertTagToCurrentPosition = (tag: string) => {
const from = elem.selectionStart ?? 0;
const to = elem.selectionEnd ?? 0;
const newText = text.substring(0, from) + tag + text.substring(to);
currentFileNamePattern.value = newText;
currentBaseNamePattern.value = newText;

// キャレットの位置を挿入した後の位置にずらす
nextTick(() => {
Expand All @@ -199,7 +183,7 @@ const submit = async () => {
await store.dispatch("SET_SAVING_SETTING", {
data: {
...savingSetting.value,
fileNamePattern: currentFileNamePattern.value + ".wav",
fileNamePattern: currentNamePattern.value,
},
});
updateOpenDialog(false);
Expand Down
4 changes: 2 additions & 2 deletions src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
transformCommandStore,
} from "./type";
import {
buildFileNameFromRawData,
buildAudioFileNameFromRawData,
buildProjectFileName,
isAccentPhrasesTextDifferent,
convertHiraToKana,
Expand Down Expand Up @@ -192,7 +192,7 @@ function buildFileName(state: State, audioKey: AudioKey) {
if (style === undefined) throw new Error("assert style !== undefined");

const styleName = style.styleName || "ノーマル";
return buildFileNameFromRawData(fileNamePattern, {
return buildAudioFileNameFromRawData(fileNamePattern, {
characterName: character.metas.speakerName,
index,
styleName,
Expand Down
21 changes: 11 additions & 10 deletions src/store/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ const replaceTagStringToTagId: { [tagString: string]: string } = Object.entries(
replaceTagIdToTagString
).reduce((prev, [k, v]) => ({ ...prev, [v]: k }), {});

export const DEFAULT_FILE_NAME_TEMPLATE =
"$連番$_$キャラ$($スタイル$)_$テキスト$.wav";
const DEFAULT_FILE_NAME_VARIABLES = {
export const DEFAULT_AUDIO_FILE_BASE_NAME_TEMPLATE =
"$連番$_$キャラ$($スタイル$)_$テキスト$";
export const DEFAULT_AUDIO_FILE_NAME_TEMPLATE = `${DEFAULT_AUDIO_FILE_BASE_NAME_TEMPLATE}.wav`;
const DEFAULT_AUDIO_FILE_NAME_VARIABLES = {
index: 0,
characterName: "四国めたん",
text: "テキストテキストテキスト",
Expand Down Expand Up @@ -147,14 +148,14 @@ export function isAccentPhrasesTextDifferent(
return false;
}

export function buildFileNameFromRawData(
fileNamePattern = DEFAULT_FILE_NAME_TEMPLATE,
vars = DEFAULT_FILE_NAME_VARIABLES
export function buildAudioFileNameFromRawData(
fileNamePattern = DEFAULT_AUDIO_FILE_NAME_TEMPLATE,
vars = DEFAULT_AUDIO_FILE_NAME_VARIABLES
): string {
let pattern = fileNamePattern;
if (pattern === "") {
// ファイル名指定のオプションが初期値("")ならデフォルトテンプレートを使う
pattern = DEFAULT_FILE_NAME_TEMPLATE;
pattern = DEFAULT_AUDIO_FILE_NAME_TEMPLATE;
}

let text = sanitizeFileName(vars.text);
Expand All @@ -171,10 +172,10 @@ export function buildFileNameFromRawData(
const date = currentDateString();

return replaceTag(pattern, {
index,
characterName,
styleName: styleName,
text,
characterName,
index,
styleName,
date,
});
}
Expand Down