updated the naming convention for new tracks and regions. New tracks now default to 'Track {i}', and new regions default to '{track_name} Region {i}'.
This commit is contained in:
@@ -9,6 +9,7 @@ import { KGMainContentState } from '../../core/state/KGMainContentState';
|
||||
import { isModifierKeyPressed } from '../../util/osUtil';
|
||||
import { CreateRegionCommand, ResizeRegionCommand, MoveRegionCommand } from '../../core/commands';
|
||||
import { KGCore } from '../../core/KGCore';
|
||||
import { generateNewRegionName } from '../../util/miscUtil';
|
||||
|
||||
interface TrackGridPanelProps {
|
||||
tracks: KGTrack[];
|
||||
@@ -97,7 +98,7 @@ const TrackGridPanel: React.FC<TrackGridPanelProps> = ({
|
||||
barNumber,
|
||||
1, // Default to 1 bar length
|
||||
beatsPerBar,
|
||||
`${track.getName()} Region`
|
||||
generateNewRegionName(trackId)
|
||||
);
|
||||
|
||||
KGCore.instance().executeCommand(command);
|
||||
|
||||
@@ -2,6 +2,7 @@ import { KGCommand } from '../KGCommand';
|
||||
import { KGCore } from '../../KGCore';
|
||||
import { KGMidiTrack, type InstrumentType } from '../../track/KGMidiTrack';
|
||||
import { KGAudioInterface } from '../../audio-interface/KGAudioInterface';
|
||||
import { generateNewTrackName } from '../../../util/miscUtil';
|
||||
|
||||
/**
|
||||
* Command to add a new track to the project
|
||||
@@ -28,7 +29,7 @@ export class AddTrackCommand extends KGCommand {
|
||||
this.trackId = trackId;
|
||||
}
|
||||
|
||||
this.trackName = trackName || `Track ${this.trackId}`;
|
||||
this.trackName = trackName || generateNewTrackName();
|
||||
this.instrument = instrument;
|
||||
|
||||
// Track index will be set during execution
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
* Miscellaneous utility functions
|
||||
*/
|
||||
|
||||
import { KGCore } from '../core/KGCore';
|
||||
|
||||
/**
|
||||
* Generates a unique ID with a consistent format
|
||||
* @param prefix - The prefix for the ID (typically class name like 'KGMidiNote')
|
||||
@@ -12,4 +14,56 @@ export const generateUniqueId = (prefix: string): string => {
|
||||
const timestamp = Date.now();
|
||||
const randomString = Math.random().toString(36).substring(2, 11); // 9 character random string
|
||||
return `${prefix}_${timestamp}_${randomString}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates a new sequential track name that doesn't conflict with existing tracks
|
||||
* @returns A track name in format "Track {number}" where number is the next available sequential number
|
||||
* @example generateNewTrackName() -> 'Track 1' (if no tracks exist)
|
||||
* @example generateNewTrackName() -> 'Track 3' (if 'Track 1' and 'Track 2' already exist)
|
||||
*/
|
||||
export const generateNewTrackName = (): string => {
|
||||
const currentProject = KGCore.instance().getCurrentProject();
|
||||
const existingTracks = currentProject.getTracks();
|
||||
const existingNames = existingTracks.map(track => track.getName());
|
||||
|
||||
let i = 1;
|
||||
while (true) {
|
||||
const candidateName = `Track ${i}`;
|
||||
if (!existingNames.includes(candidateName)) {
|
||||
return candidateName;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates a new sequential region name that doesn't conflict with existing regions on the same track
|
||||
* @param trackId - The ID of the track where the region will be created
|
||||
* @returns A region name in format "{trackName} Region {number}" where number is the next available sequential number
|
||||
* @example generateNewRegionName('1') -> 'Piano Region 1' (if no regions exist on track)
|
||||
* @example generateNewRegionName('1') -> 'Piano Region 3' (if 'Piano Region 1' and 'Piano Region 2' already exist)
|
||||
*/
|
||||
export const generateNewRegionName = (trackId: string): string => {
|
||||
const currentProject = KGCore.instance().getCurrentProject();
|
||||
const tracks = currentProject.getTracks();
|
||||
const targetTrack = tracks.find(track => track.getId().toString() === trackId);
|
||||
|
||||
if (!targetTrack) {
|
||||
// Fallback if track not found
|
||||
return 'Region 1';
|
||||
}
|
||||
|
||||
const trackName = targetTrack.getName();
|
||||
const existingRegions = targetTrack.getRegions();
|
||||
const existingNames = existingRegions.map(region => region.getName());
|
||||
|
||||
let i = 1;
|
||||
while (true) {
|
||||
const candidateName = `${trackName} Region ${i}`;
|
||||
if (!existingNames.includes(candidateName)) {
|
||||
return candidateName;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user