feat: added shortcut for split and merge regions
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
import { KGCore } from '../core/KGCore';
|
||||
import { SplitRegionCommand } from '../core/commands/region/SplitRegionCommand';
|
||||
import { MergeMidiRegionsCommand } from '../core/commands/region/MergeMidiRegionsCommand';
|
||||
import { KGMidiRegion } from '../core/region/KGMidiRegion';
|
||||
import { showAlert, showConfirm } from './dialogUtil';
|
||||
|
||||
interface SplitSelectedRegionParams {
|
||||
selectedRegionIds: string[];
|
||||
playheadPosition: number;
|
||||
refreshProjectState: () => void;
|
||||
}
|
||||
|
||||
interface MergeSelectedMidiRegionsParams {
|
||||
selectedRegionIds: string[];
|
||||
refreshProjectState: () => void;
|
||||
}
|
||||
|
||||
export const splitSelectedRegionAtPlayhead = async ({
|
||||
selectedRegionIds,
|
||||
playheadPosition,
|
||||
refreshProjectState,
|
||||
}: SplitSelectedRegionParams): Promise<string | null> => {
|
||||
if (selectedRegionIds.length === 0) {
|
||||
await showAlert('Please select a region to split.');
|
||||
return null;
|
||||
}
|
||||
|
||||
if (selectedRegionIds.length > 1) {
|
||||
await showAlert('Please select exactly one region to split.');
|
||||
return null;
|
||||
}
|
||||
|
||||
const regionId = selectedRegionIds[selectedRegionIds.length - 1];
|
||||
const tracks = KGCore.instance().getCurrentProject().getTracks();
|
||||
let targetRegion = null;
|
||||
|
||||
for (const track of tracks) {
|
||||
const foundRegion = track.getRegions().find(region => region.getId() === regionId);
|
||||
if (foundRegion) {
|
||||
targetRegion = foundRegion;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!targetRegion) {
|
||||
await showAlert('Selected region not found.');
|
||||
return null;
|
||||
}
|
||||
|
||||
const regionStart = targetRegion.getStartFromBeat();
|
||||
const regionEnd = regionStart + targetRegion.getLength();
|
||||
|
||||
if (playheadPosition <= regionStart || playheadPosition >= regionEnd) {
|
||||
await showAlert('The playhead is not inside the selected region. Move the playhead inside the region before splitting.');
|
||||
return null;
|
||||
}
|
||||
|
||||
const command = new SplitRegionCommand(regionId, playheadPosition);
|
||||
KGCore.instance().executeCommand(command);
|
||||
refreshProjectState();
|
||||
|
||||
return `Split region at beat ${playheadPosition.toFixed(2)}`;
|
||||
};
|
||||
|
||||
export const mergeSelectedMidiRegions = async ({
|
||||
selectedRegionIds,
|
||||
refreshProjectState,
|
||||
}: MergeSelectedMidiRegionsParams): Promise<string | null> => {
|
||||
if (selectedRegionIds.length < 2) {
|
||||
await showAlert('Please select at least two MIDI regions on the same track to merge.');
|
||||
return null;
|
||||
}
|
||||
|
||||
const tracks = KGCore.instance().getCurrentProject().getTracks();
|
||||
const selectedRegionIdSet = new Set(selectedRegionIds);
|
||||
const selectedMidiRegions: KGMidiRegion[] = [];
|
||||
let targetTrackId: string | null = null;
|
||||
|
||||
for (const track of tracks) {
|
||||
for (const region of track.getRegions()) {
|
||||
if (!selectedRegionIdSet.has(region.getId())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(region instanceof KGMidiRegion)) {
|
||||
await showAlert('Only MIDI regions can be merged. Please adjust your selection and try again.');
|
||||
return null;
|
||||
}
|
||||
|
||||
const regionTrackId = track.getId().toString();
|
||||
if (targetTrackId && targetTrackId !== regionTrackId) {
|
||||
await showAlert('Please select only MIDI regions from a single track before merging.');
|
||||
return null;
|
||||
}
|
||||
|
||||
targetTrackId = regionTrackId;
|
||||
selectedMidiRegions.push(region);
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedMidiRegions.length !== selectedRegionIds.length || !targetTrackId) {
|
||||
await showAlert('Some selected regions could not be found. Please reselect the MIDI regions and try again.');
|
||||
return null;
|
||||
}
|
||||
|
||||
const sortedSelectedRegions = [...selectedMidiRegions].sort((a, b) => {
|
||||
const startDelta = a.getStartFromBeat() - b.getStartFromBeat();
|
||||
if (startDelta !== 0) {
|
||||
return startDelta;
|
||||
}
|
||||
return a.getLength() - b.getLength();
|
||||
});
|
||||
|
||||
let regionIdsToMerge = selectedRegionIds;
|
||||
const firstSelectedRegion = sortedSelectedRegions[0];
|
||||
const lastSelectedRegion = sortedSelectedRegions[sortedSelectedRegions.length - 1];
|
||||
const spanStart = firstSelectedRegion.getStartFromBeat();
|
||||
const spanEnd = lastSelectedRegion.getStartFromBeat() + lastSelectedRegion.getLength();
|
||||
|
||||
const targetTrack = tracks.find(track => track.getId().toString() === targetTrackId);
|
||||
const inBetweenRegions = targetTrack
|
||||
?.getRegions()
|
||||
.filter(region => (
|
||||
region instanceof KGMidiRegion &&
|
||||
!selectedRegionIdSet.has(region.getId()) &&
|
||||
region.getStartFromBeat() >= spanStart &&
|
||||
region.getStartFromBeat() <= spanEnd
|
||||
)) ?? [];
|
||||
|
||||
if (inBetweenRegions.length > 0) {
|
||||
const shouldIncludeInBetweenRegions = await showConfirm(
|
||||
'There are additional MIDI regions between the first and last selected regions on this track. Would you like KGStudio to merge those as well?',
|
||||
{
|
||||
confirmLabel: 'Merge All In Between',
|
||||
cancelLabel: 'Stop',
|
||||
}
|
||||
);
|
||||
|
||||
if (!shouldIncludeInBetweenRegions) {
|
||||
return null;
|
||||
}
|
||||
|
||||
regionIdsToMerge = Array.from(new Set([
|
||||
...selectedRegionIds,
|
||||
...inBetweenRegions.map(region => region.getId()),
|
||||
]));
|
||||
}
|
||||
|
||||
try {
|
||||
const command = new MergeMidiRegionsCommand(regionIdsToMerge);
|
||||
KGCore.instance().executeCommand(command, { rethrow: true });
|
||||
refreshProjectState();
|
||||
return `Merged ${regionIdsToMerge.length} MIDI regions`;
|
||||
} catch (error) {
|
||||
await showAlert(error instanceof Error ? error.message : 'Unable to merge the selected MIDI regions.');
|
||||
return null;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user