Skip to content

Commit

Permalink
選択しているトラックの下にトラックがインポートされるように
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Jul 30, 2024
1 parent 94e6449 commit cffed44
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/sing/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ const BEAT_TYPES = [2, 4, 8, 16];
const MIN_BPM = 40;
const MAX_SNAP_TYPE = 32;

export const isTracksEmpty = (tracks: Track[]) =>
tracks.length === 0 || (tracks.length === 1 && tracks[0].notes.length === 0);
export const isTrackEmpty = (track: Track) => track.notes.length === 0;

export const isValidTpqn = (tpqn: number) => {
return (
Expand Down
33 changes: 21 additions & 12 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import {
isValidTrack,
SEQUENCER_MIN_NUM_MEASURES,
getNumMeasures,
isTracksEmpty,
isTrackEmpty,
shouldPlayTracks,
} from "@/sing/domain";
import {
Expand Down Expand Up @@ -2748,42 +2748,51 @@ export const singingCommandStore = transformCommandStore(
singingStore.mutations.SET_TPQN(draft, { tpqn });
singingStore.mutations.SET_TEMPOS(draft, { tempos });
singingStore.mutations.SET_TIME_SIGNATURES(draft, { timeSignatures });
for (const { track, trackId, overwrite } of tracks) {
for (const { track, trackId, overwrite, prevTrackId } of tracks) {
if (overwrite) {
singingStore.mutations.SET_TRACK(draft, { track, trackId });
} else {
singingStore.mutations.INSERT_TRACK(draft, {
track,
trackId,
prevTrackId: undefined,
prevTrackId,
});
}
}
},
/**
* 複数のトラックを選択中のトラックの後ろに挿入し、一部の情報をインポートする。
* プロジェクトファイルのインポートで使う。
* 選択中のトラックが空の場合は最初のトラックで上書きする。
*/
async action(
{ state, commit, getters, dispatch },
{ tpqn, tempos, timeSignatures, tracks },
) {
const payload: {
track: Track;
trackId: TrackId;
overwrite: boolean;
}[] = [];
const payload: ({ track: Track; trackId: TrackId } & (
| { overwrite: true; prevTrackId?: undefined }
| { overwrite?: false; prevTrackId: TrackId }
))[] = [];
if (state.experimentalSetting.enableMultiTrack) {
let prevTrackId = getters.SELECTED_TRACK_ID;
for (const [i, track] of tracks.entries()) {
if (!isValidTrack(track)) {
throw new Error("The track is invalid.");
}
// 空のプロジェクトならトラックを上書きする
if (i === 0 && isTracksEmpty([...state.tracks.values()])) {
// 空のトラックなら上書きする
if (
i === 0 &&
isTrackEmpty(getOrThrow(state.tracks, prevTrackId))
) {
payload.push({
track,
trackId: getters.SELECTED_TRACK_ID,
trackId: prevTrackId,
overwrite: true,
});
} else {
const { trackId } = await dispatch("CREATE_TRACK");
payload.push({ track, trackId, overwrite: false });
payload.push({ track, trackId, prevTrackId });
prevTrackId = trackId;
}
}
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,10 @@ export type SingingCommandStoreTypes = {
tpqn: number;
tempos: Tempo[];
timeSignatures: TimeSignature[];
tracks: { track: Track; trackId: TrackId; overwrite: boolean }[];
tracks: ({ track: Track; trackId: TrackId } & (
| { overwrite: true; prevTrackId?: undefined }
| { overwrite?: false; prevTrackId: TrackId }
))[];
};
action(payload: {
tpqn: number;
Expand Down

0 comments on commit cffed44

Please sign in to comment.