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 1 commit
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
15 changes: 8 additions & 7 deletions src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
EngineId,
MoraDataType,
MorphingInfo,
PresetKey,
SpeakerId,
StyleInfo,
Voice,
Expand Down Expand Up @@ -478,7 +479,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
payload: {
text?: string;
voice?: Voice;
presetKey?: string;
presetKey?: PresetKey;
baseAudioItem?: AudioItem;
}
) {
Expand Down Expand Up @@ -1783,7 +1784,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 @@ -2627,7 +2628,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 @@ -2640,7 +2641,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 @@ -2656,15 +2657,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 @@ -2775,7 +2776,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
19 changes: 11 additions & 8 deletions src/store/preset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PresetStoreState, PresetStoreTypes } from "@/store/type";
import { Preset } from "@/type/preload";
import { Preset, PresetKey } from "@/type/preload";
import { createPartialStore } from "./vuex";

import { v4 as uuidv4 } from "uuid";
Expand All @@ -11,13 +11,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 @@ -41,7 +44,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 @@ -55,7 +58,7 @@ 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)),
Expand All @@ -68,7 +71,7 @@ export const presetStore = createPartialStore<PresetStoreTypes>({

ADD_PRESET: {
async action(context, { presetData }: { presetData: Preset }) {
const newKey = uuidv4();
const newKey = PresetKey(uuidv4());
const newPresetItems = {
...context.state.presetItems,
[newKey]: presetData,
Expand All @@ -87,7 +90,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 @@ -105,7 +108,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
32 changes: 17 additions & 15 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import {
EngineId,
SpeakerId,
AudioKey,
PresetKey,
presetKeySchema,
} from "@/type/preload";
import { IEngineConnectorFactory } from "@/infrastructures/EngineConnector";
import { QVueGlobals } from "quasar";
Expand All @@ -61,7 +63,7 @@ export type AudioItem = {
text: string;
voice: Voice;
query?: EditorAudioQuery;
presetKey?: string;
presetKey?: PresetKey;
morphingInfo?: MorphingInfo;
};

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

Expand Down Expand Up @@ -633,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 @@ -647,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 @@ -1231,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>;
};
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 @@ -16,6 +16,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 @@ -517,7 +521,7 @@ export const electronStoreSchema = z
.object({
items: z
.record(
z.string().uuid(),
presetKeySchema,
z
.object({
name: z.string(),
Expand All @@ -540,7 +544,7 @@ export const electronStoreSchema = z
.passthrough()
)
.default({}),
keys: z.string().uuid().array().default([]),
keys: presetKeySchema.array().default([]),
})
.passthrough()
.default({}),
Expand Down
2 changes: 1 addition & 1 deletion src/views/EditorHome.vue
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ const activeAudioKey = computed<AudioKey | undefined>(
const addAudioItem = async () => {
const prevAudioKey = activeAudioKey.value;
let voice: Voice | undefined = undefined;
let presetKey: string | undefined = undefined;
let presetKey: PresetKey | undefined = undefined;
if (prevAudioKey !== undefined) {
voice = store.state.audioItems[prevAudioKey].voice;
presetKey = store.state.audioItems[prevAudioKey].presetKey;
Expand Down