Enhance world duplication to use current world name as base (#4990) - Update `getFreeWorldName()` to accept optional `worldName` parameter and `stripIndex` option - Add JSDoc documentation for `getFreeWorldName()` parameters and return value - Modify world duplicate handler to pass current world name to `getFreeWorldName()` - Strip existing numbered suffix from base name before generating new numbered name - Default to "New World" when no world name is provided - Make "New World" translatable
Signed| @@ -2531,7 +2531,12 @@ async function displayWorldEntries(name, data, navigation = navigation_option.no | |||
| 2531 | }); | 2531 | }); |
| 2532 | 2532 | ||
| 2533 | $('#world_duplicate').off('click').on('click', async () => { | 2533 | $('#world_duplicate').off('click').on('click', async () => { |
| 2534 | const tempName = getFreeWorldName(); | 2534 | // Find current name for the world selected |
| 2535 | const selectedIndex = String($('#world_editor_select').find(':selected').val()); | ||
| 2536 | const worldName = world_names[selectedIndex] || null; | ||
| 2537 | |||
| 2538 | // Use the current name as default input, then ask user for the name | ||
| 2539 | const tempName = getFreeWorldName(worldName); | ||
| 2535 | const finalName = await Popup.show.input('Create a new World Info?', 'Enter a name for the new file:', tempName); | 2540 | const finalName = await Popup.show.input('Create a new World Info?', 'Enter a name for the new file:', tempName); |
| 2536 | 2541 | ||
| 2537 | if (finalName) { | 2542 | if (finalName) { |
| @@ -4180,10 +4185,25 @@ export function getFreeWorldEntryUid(data) { | |||
| 4180 | return null; | 4185 | return null; |
| 4181 | } | 4186 | } |
| 4182 | 4187 | ||
| 4183 | export function getFreeWorldName() { | 4188 | |
| 4189 | /** | ||
| 4190 | * Generates a free world name based on the given input name. | ||
| 4191 | * If the input name is null, a default name is used. | ||
| 4192 | * If the input name already exists, a numbered suffix is added. | ||
| 4193 | * | ||
| 4194 | * @param {string|null} worldName - The name to base the new world name on. If null, a default name is used. | ||
| 4195 | * @param {Object} [options={}] - Optional parameters. | ||
| 4196 | * @param {boolean} [options.stripIndex=true] - Whether to strip any numbered suffix from the input name before generating the new name. | ||
| 4197 | * @return {string|undefined} The generated free world name, or undefined if no free name could be found after trying 100,000 times. | ||
| 4198 | */ | ||
| 4199 | export function getFreeWorldName(worldName = null, { stripIndex = true } = {}) { | ||
| 4200 | worldName ??= t`New World`; | ||
| 4201 | if (stripIndex) { | ||
| 4202 | worldName = worldName.replace(/\s*\(\d+\)$/, ''); | ||
| 4203 | } | ||
| 4184 | const MAX_FREE_NAME = 100_000; | 4204 | const MAX_FREE_NAME = 100_000; |
| 4185 | for (let index = 1; index < MAX_FREE_NAME; index++) { | 4205 | for (let index = 1; index < MAX_FREE_NAME; index++) { |
| 4186 | const newName = `New World (${index})`; | 4206 | const newName = `${worldName} (${index})`; |
| 4187 | if (world_names.includes(newName)) { | 4207 | if (world_names.includes(newName)) { |
| 4188 | continue; | 4208 | continue; |
| 4189 | } | 4209 | } |