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

PresetKeyに型を付ける #1217

Merged
merged 7 commits into from
Mar 1, 2023
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
17 changes: 6 additions & 11 deletions src/components/AudioInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ import {
CharacterInfo,
MorphingInfo,
Preset,
PresetKey,
Voice,
} from "@/type/preload";
import { previewSliderHelper } from "@/helpers/previewSliderHelper";
Expand Down Expand Up @@ -821,24 +822,18 @@ const isChangedPreset = computed(() => {

type PresetSelectModelType = {
label: string;
key: string | undefined;
key: PresetKey | undefined;
};

// プリセットの変更
const changePreset = (
presetOrPresetKey: PresetSelectModelType | string
): void => {
const presetKey =
typeof presetOrPresetKey === "string"
? presetOrPresetKey
: presetOrPresetKey.key;
const changePreset = (presetKey: PresetKey | undefined): void => {
store.dispatch("COMMAND_SET_AUDIO_PRESET", {
audioKey: props.activeAudioKey,
presetKey,
});
};
Comment on lines 828 to 834
Copy link
Member Author

@Hiroshiba Hiroshiba Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeofで切り分けるのが危なそうだったのでロジックを変えています。
といっても、PresetSelectModelType型のときはその.keyPresetKeyなのでかなり簡単でした。


const presetList = computed<{ label: string; key: string }[]>(() =>
const presetList = computed<{ label: string; key: PresetKey }[]>(() =>
presetKeys.value
.filter((key) => presetItems.value[key] != undefined)
.map((key) => ({
Expand Down Expand Up @@ -875,7 +870,7 @@ const presetSelectModel = computed<PresetSelectModelType>({
};
},
set: (newVal) => {
changePreset(newVal);
changePreset(newVal.key);
},
});

Expand All @@ -895,7 +890,7 @@ const setPresetByScroll = (event: WheelEvent) => {

if (selectablePresetList.value[newIndex] === undefined) return;

changePreset(selectablePresetList.value[newIndex]);
changePreset(selectablePresetList.value[newIndex].key);
};

// プリセットの登録・再登録
Expand Down
6 changes: 3 additions & 3 deletions src/components/PresetManageDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { useQuasar } from "quasar";
import draggable from "vuedraggable";
import { useStore } from "@/store";

import { Preset } from "@/type/preload";
import { Preset, PresetKey } from "@/type/preload";

const props =
defineProps<{
Expand Down Expand Up @@ -90,7 +90,7 @@ const previewPresetList = computed(() =>
: presetList.value
);

const reorderPreset = (featurePresetList: (Preset & { key: string })[]) => {
const reorderPreset = (featurePresetList: (Preset & { key: PresetKey })[]) => {
const newPresetKeys = featurePresetList.map((item) => item.key);
previewPresetKeys.value = newPresetKeys;
isPreview.value = true;
Expand All @@ -101,7 +101,7 @@ const reorderPreset = (featurePresetList: (Preset & { key: string })[]) => {
.finally(() => (isPreview.value = false));
};

const deletePreset = (key: string) => {
const deletePreset = (key: PresetKey) => {
$q.dialog({
title: "プリセット削除の確認",
message: `プリセット "${presetItems.value[key].name}" を削除してもよろしいですか?`,
Expand Down
15 changes: 8 additions & 7 deletions src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
EngineId,
MoraDataType,
MorphingInfo,
PresetKey,
SpeakerId,
StyleId,
StyleInfo,
Expand Down Expand Up @@ -479,7 +480,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
payload: {
text?: string;
voice?: Voice;
presetKey?: string;
presetKey?: PresetKey;
baseAudioItem?: AudioItem;
}
) {
Expand Down Expand Up @@ -1784,7 +1785,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
{
audioKey,
presetKey,
}: { audioKey: AudioKey; presetKey: string | undefined }
}: { audioKey: AudioKey; presetKey: PresetKey | undefined }
) {
if (presetKey === undefined) {
delete state.audioItems[audioKey].presetKey;
Expand Down Expand Up @@ -2628,7 +2629,7 @@ export const audioCommandStore = transformCommandStore(
{
audioKey,
presetKey,
}: { audioKey: AudioKey; presetKey: string | undefined }
}: { audioKey: AudioKey; presetKey: PresetKey | undefined }
) {
audioStore.mutations.SET_AUDIO_PRESET_KEY(draft, {
audioKey,
Expand All @@ -2641,7 +2642,7 @@ export const audioCommandStore = transformCommandStore(
{
audioKey,
presetKey,
}: { audioKey: AudioKey; presetKey: string | undefined }
}: { audioKey: AudioKey; presetKey: PresetKey | undefined }
) {
commit("COMMAND_SET_AUDIO_PRESET", { audioKey, presetKey });
},
Expand All @@ -2657,15 +2658,15 @@ export const audioCommandStore = transformCommandStore(
},

COMMAND_FULLY_APPLY_AUDIO_PRESET: {
mutation(draft, { presetKey }: { presetKey: string }) {
mutation(draft, { presetKey }: { presetKey: PresetKey }) {
const targetAudioKeys = draft.audioKeys.filter(
(audioKey) => draft.audioItems[audioKey].presetKey === presetKey
);
for (const audioKey of targetAudioKeys) {
audioStore.mutations.APPLY_AUDIO_PRESET(draft, { audioKey });
}
},
action({ commit }, payload: { presetKey: string }) {
action({ commit }, payload: { presetKey: PresetKey }) {
commit("COMMAND_FULLY_APPLY_AUDIO_PRESET", payload);
},
},
Expand Down Expand Up @@ -2776,7 +2777,7 @@ export const audioCommandStore = transformCommandStore(
audioItem: AudioItem;
}[] = [];
let baseAudioItem: AudioItem | undefined = undefined;
let basePresetKey: string | undefined = undefined;
let basePresetKey: PresetKey | undefined = undefined;
if (state.inheritAudioInfo && state._activeAudioKey) {
baseAudioItem = state.audioItems[state._activeAudioKey];
basePresetKey = baseAudioItem.presetKey;
Expand Down
29 changes: 19 additions & 10 deletions src/store/preset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { v4 as uuidv4 } from "uuid";
import { createPartialStore } from "./vuex";
import { PresetStoreState, PresetStoreTypes } from "@/store/type";
import { Preset } from "@/type/preload";
import { Preset, PresetKey } from "@/type/preload";

export const presetStoreState: PresetStoreState = {
presetItems: {},
Expand All @@ -10,13 +10,16 @@ export const presetStoreState: PresetStoreState = {

export const presetStore = createPartialStore<PresetStoreTypes>({
SET_PRESET_ITEMS: {
mutation(state, { presetItems }: { presetItems: Record<string, Preset> }) {
mutation(
state,
{ presetItems }: { presetItems: Record<PresetKey, Preset> }
) {
state.presetItems = presetItems;
},
},

SET_PRESET_KEYS: {
mutation(state, { presetKeys }: { presetKeys: string[] }) {
mutation(state, { presetKeys }: { presetKeys: PresetKey[] }) {
state.presetKeys = presetKeys;
},
},
Expand All @@ -31,7 +34,9 @@ export const presetStore = createPartialStore<PresetStoreTypes>({
)
return;
commit("SET_PRESET_ITEMS", {
presetItems: presetConfig.items,
// z.BRAND型のRecordはPartialになる仕様なのでasで型を変換
// TODO: 将来的にzodのバージョンを上げてasを消す https://github.com/colinhacks/zod/pull/2097
presetItems: presetConfig.items as Record<PresetKey, Preset>,
});
commit("SET_PRESET_KEYS", {
presetKeys: presetConfig.keys,
Expand All @@ -40,7 +45,7 @@ export const presetStore = createPartialStore<PresetStoreTypes>({
},

SAVE_PRESET_ORDER: {
action({ state, dispatch }, { presetKeys }: { presetKeys: string[] }) {
action({ state, dispatch }, { presetKeys }: { presetKeys: PresetKey[] }) {
return dispatch("SAVE_PRESET_CONFIG", {
presetItems: state.presetItems,
presetKeys,
Expand All @@ -54,20 +59,24 @@ export const presetStore = createPartialStore<PresetStoreTypes>({
{
presetItems,
presetKeys,
}: { presetItems: Record<string, Preset>; presetKeys: string[] }
}: { presetItems: Record<PresetKey, Preset>; presetKeys: PresetKey[] }
) {
const result = await window.electron.setSetting("presets", {
items: JSON.parse(JSON.stringify(presetItems)),
keys: JSON.parse(JSON.stringify(presetKeys)),
});
context.commit("SET_PRESET_ITEMS", { presetItems: result.items });
context.commit("SET_PRESET_ITEMS", {
// z.BRAND型のRecordはPartialになる仕様なのでasで型を変換
// TODO: 将来的にzodのバージョンを上げてasを消す https://github.com/colinhacks/zod/pull/2097
presetItems: result.items as Record<PresetKey, Preset>,
});
context.commit("SET_PRESET_KEYS", { presetKeys: result.keys });
},
},

ADD_PRESET: {
async action(context, { presetData }: { presetData: Preset }) {
const newKey = uuidv4();
const newKey = PresetKey(uuidv4());
const newPresetItems = {
...context.state.presetItems,
[newKey]: presetData,
Expand All @@ -86,7 +95,7 @@ export const presetStore = createPartialStore<PresetStoreTypes>({
UPDATE_PRESET: {
async action(
context,
{ presetKey, presetData }: { presetData: Preset; presetKey: string }
{ presetKey, presetData }: { presetData: Preset; presetKey: PresetKey }
) {
const newPresetItems = {
...context.state.presetItems,
Expand All @@ -104,7 +113,7 @@ export const presetStore = createPartialStore<PresetStoreTypes>({
},

DELETE_PRESET: {
async action(context, { presetKey }: { presetKey: string }) {
async action(context, { presetKey }: { presetKey: PresetKey }) {
const newPresetKeys = context.state.presetKeys.filter(
(key) => key != presetKey
);
Expand Down
33 changes: 17 additions & 16 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
SpeakerId,
StyleId,
AudioKey,
PresetKey,
} from "@/type/preload";
import { IEngineConnectorFactory } from "@/infrastructures/EngineConnector";

Expand All @@ -62,7 +63,7 @@ export type AudioItem = {
text: string;
voice: Voice;
query?: EditorAudioQuery;
presetKey?: string;
presetKey?: PresetKey;
morphingInfo?: MorphingInfo;
};

Expand Down Expand Up @@ -200,7 +201,7 @@ export type AudioStoreTypes = {
action(payload: {
text?: string;
voice?: Voice;
presetKey?: string;
presetKey?: PresetKey;
baseAudioItem?: AudioItem;
}): Promise<AudioItem>;
};
Expand Down Expand Up @@ -447,7 +448,7 @@ export type AudioStoreTypes = {
SET_AUDIO_PRESET_KEY: {
mutation: {
audioKey: AudioKey;
presetKey: string | undefined;
presetKey: PresetKey | undefined;
};
};

Expand Down Expand Up @@ -634,11 +635,11 @@ export type AudioCommandStoreTypes = {
COMMAND_SET_AUDIO_PRESET: {
mutation: {
audioKey: AudioKey;
presetKey: string | undefined;
presetKey: PresetKey | undefined;
};
action(payload: {
audioKey: AudioKey;
presetKey: string | undefined;
presetKey: PresetKey | undefined;
}): void;
};

Expand All @@ -648,8 +649,8 @@ export type AudioCommandStoreTypes = {
};

COMMAND_FULLY_APPLY_AUDIO_PRESET: {
mutation: { presetKey: string };
action(payload: { presetKey: string }): void;
mutation: { presetKey: PresetKey };
action(payload: { presetKey: PresetKey }): void;
};

COMMAND_IMPORT_FROM_FILE: {
Expand Down Expand Up @@ -1232,41 +1233,41 @@ export type UiStoreTypes = {
*/

export type PresetStoreState = {
presetKeys: string[];
presetItems: Record<string, Preset>;
presetKeys: PresetKey[];
presetItems: Record<PresetKey, Preset>;
};

export type PresetStoreTypes = {
SET_PRESET_ITEMS: {
mutation: {
presetItems: Record<string, Preset>;
presetItems: Record<PresetKey, Preset>;
};
};
SET_PRESET_KEYS: {
mutation: {
presetKeys: string[];
presetKeys: PresetKey[];
};
};
HYDRATE_PRESET_STORE: {
action(): void;
};
SAVE_PRESET_ORDER: {
action(payload: { presetKeys: string[] }): void;
action(payload: { presetKeys: PresetKey[] }): void;
};
SAVE_PRESET_CONFIG: {
action(payload: {
presetItems: Record<string, Preset>;
presetKeys: string[];
presetKeys: PresetKey[];
}): void;
};
ADD_PRESET: {
action(payload: { presetData: Preset }): Promise<string>;
action(payload: { presetData: Preset }): Promise<PresetKey>;
};
UPDATE_PRESET: {
action(payload: { presetData: Preset; presetKey: string }): void;
action(payload: { presetData: Preset; presetKey: PresetKey }): void;
};
DELETE_PRESET: {
action(payload: { presetKey: string }): void;
action(payload: { presetKey: PresetKey }): void;
};
};

Expand Down
8 changes: 6 additions & 2 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export const audioKeySchema = z.string().uuid().brand<"AudioKey">();
export type AudioKey = z.infer<typeof audioKeySchema>;
export const AudioKey = (id: string): AudioKey => audioKeySchema.parse(id);

export const presetKeySchema = z.string().uuid().brand<"PresetKey">();
export type PresetKey = z.infer<typeof presetKeySchema>;
export const PresetKey = (id: string): PresetKey => presetKeySchema.parse(id);
Comment on lines +26 to +28
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここが型定義している場所です


// ホットキーを追加したときは設定のマイグレーションが必要
export const defaultHotkeySettings: HotkeySetting[] = [
{
Expand Down Expand Up @@ -525,7 +529,7 @@ export const electronStoreSchema = z
.object({
items: z
.record(
z.string().uuid(),
presetKeySchema,
z
.object({
name: z.string(),
Expand All @@ -548,7 +552,7 @@ export const electronStoreSchema = z
.passthrough()
)
.default({}),
keys: z.string().uuid().array().default([]),
keys: presetKeySchema.array().default([]),
})
.passthrough()
.default({}),
Expand Down
Loading