Fix apply WI sorting to order field - Fix using wrong values because of displayIndex - Expand it to take any current sorting, not just the "custom" one

ad65fbc1fce2f76514a7817ced438f72ebb6a906

Wolfsblvt <wolfsblvt@gmail.com>

2 files changed, +17 -13Showing whitespace changes
public/index.html+2 -2
@@ -3662,8 +3662,8 @@
36623662 <div id="OpenAllWIEntries" class="menu_button fa-solid fa-expand" title="Open all Entries" data-i18n="[title]Open all Entries"></div>
36633663 <div id="CloseAllWIEntries" class="menu_button fa-solid fa-compress" title="Close all Entries" data-i18n="[title]Close all Entries"></div>
36643664 <div id="world_popup_new" class="menu_button fa-solid fa-plus" title="New Entry" data-i18n="[title]New Entry"></div>
36653665 <div id="world_backfill_memos" class="menu_button fa-solid fa-notes-medical" title="Fill empty Memo/Titles with Keywords" data-i18n="[title]Fill empty Memo/Titles with Keywords"></div><div id="world_apply_custom_sorting" class="menu_button fa-solid fa-solid fa-arrow-down-9-1"
36663666 <div id="world_apply_current_sorting" class="menu_button fa-solid fa-solid fa-arrow-down-9-1" title="Apply customcurrent sorting as Order" data-i18n="[title]Apply customcurrent sorting as Order"></div>
36673667 <div id="world_import_button" class="menu_button fa-solid fa-file-import" title="Import World Info" data-i18n="[title]Import World Info"></div>
36683668 <div id="world_popup_export" class="menu_button fa-solid fa-file-export" title="Export World Info" data-i18n="[title]Export World Info"></div>
36693669 <div id="world_duplicate" class="menu_button fa-solid fa-paste" title="Duplicate World Info" data-i18n="[title]Duplicate World Info"></div>
public/scripts/world-info.js+15 -11
@@ -1939,39 +1939,43 @@ function displayWorldEntries(name, data, navigation = navigation_option.none, fl
19391939 }
19401940 });
19411941
19421942 $('#world_apply_custom_sortingworld_apply_current_sorting').off('click').on('click', async () => {
19431943 const entryCount = Object.keys(data.entries).length;
19441944 const moreThan100 = entryCount > 100;
19451945
19461946 let content = '<span>Apply your customcurrent sorting to the "Order" field. The Order values will go down from the chosen number.</span>';
19471947 if (moreThan100) {
19481948 content += `<div class="m-t-1"><i class="fa-solid fa-triangle-exclamation" style="color: #FFD43B;"></i> More than 100 entries in this world. If you don't choose a number higher than that, the lower entries will default to 0.<br />(Usual default: 100)<br />Minimum: ${entryCount}</div>`;
19491949 }
19501950
19511951 const result = await Popup.show.input('Apply CustomCurrent Sorting', content, '100', { okButton: 'Apply', cancelButton: 'Cancel' });
19521952 if (!result) return;
19531953
19541954 const start = Number(result);
19551955 if (isNaN(start) || start < 0) {
19561956 toastr.error('Invalid number: ' + result, 'Apply CustomCurrent Sorting');
19571957 return;
19581958 }
19591959 if (start < entryCount) {
19601960 toastr.warning('A number lower than the entry count has been chosen. All entries below that will default to 0.', 'Apply CustomCurrent Sorting');
19611961 }
19621962
1963- let counter = 0;
1963+ // We need to sort the entries here, as the data source isn't sorted
19641964 for (const entryentries of= Object.values(data.entries)) {;
1965- const newOrder = Math.max(start - (entry.displayIndex ?? 0), 0);
1965+ sortEntries(entries);
1966+
1967+ let updated = 0, current = start;
1968+ for (const entry of entries) {
1969+ const newOrder = Math.max(current--, 0);
19661970 if (entry.order === newOrder) continue;
19671971
19681972 entry.order = newOrder;
19691973 setOriginalDataValue(data, entry.order, 'order', entry.order);
19701974 counterupdated++;
19711975 }
19721976
19731977 if (counterupdated > 0) {
19741978 toastr.info(`Updated ${counterupdated} Order values`, 'Apply Custom Sorting');
19751979 await saveWorldInfo(name, data, true);
19761980 updateEditor(navigation_option.previous);
19771981 } else {