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

858b9e684e26db0972ed6b0de8493527fd132698

Wolfsblvt <wolfsblvt@gmail.com>

Signed
1 files changed, +23 -3Showing whitespace changes
public/scripts/world-info.js+23 -3
@@ -2531,7 +2531,12 @@ async function displayWorldEntries(name, data, navigation = navigation_option.no
25312531 });
25322532
25332533 $('#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);
25352540 const finalName = await Popup.show.input('Create a new World Info?', 'Enter a name for the new file:', tempName);
25362541
25372542 if (finalName) {
@@ -4180,10 +4185,25 @@ export function getFreeWorldEntryUid(data) {
41804185 return null;
41814186}
41824187
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+ }
41844204 const MAX_FREE_NAME = 100_000;
41854205 for (let index = 1; index < MAX_FREE_NAME; index++) {
41864206 const newName = `New World${worldName} (${index})`;
41874207 if (world_names.includes(newName)) {
41884208 continue;
41894209 }