Merge pull request #2568 from SillyTavern/wi-apply-sorting-order-fix Fix apply WI sorting to order field

1ad57e6ff63b508d8d8b66daa1f80dc3dc5e7809

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
4 files changed, +25 -19Ignore whitespace
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/script.js+2 -2
@@ -10822,7 +10822,7 @@ jQuery(async function () {
1082210822 //newSlider.val(manualInput)
1082310823 //handleSlideEvent.call(newSlider, null, { value: parseFloat(manualInput) }, 'manual');
1082410824 valueBeforeManualInput = manualInput;
1082510825 $(masterElement).val($(this).val()).trigger('input', { forced: true });
1082610826 } else {
1082710827 //if value not ok, warn and reset to last known valid value
1082810828 toastr.warning(`Invalid value. Must be between ${$(this).attr('min')} and ${$(this).attr('max')}`);
@@ -10848,7 +10848,7 @@ jQuery(async function () {
1084810848 if (manualInput >= Number($(this).attr('min')) && manualInput <= Number($(this).attr('max'))) {
1084910849 valueBeforeManualInput = manualInput;
1085010850 //set the slider value to input value
1085110851 $(masterElement).val($(this).val()).trigger('input', { forced: true });
1085210852 } else {
1085310853 //if value not ok, warn and reset to last known valid value
1085410854 toastr.warning(`Invalid value. Must be between ${$(this).attr('min')} and ${$(this).attr('max')}`);
public/scripts/power-user.js+6 -4
@@ -3328,10 +3328,11 @@ $(document).ready(() => {
33283328
33293329 });
33303330
33313331 $('#chat_width_slider').on('input', function (e, data) {
3332+ const applyMode = data?.forced ? 'forced' : 'normal';
33323333 power_user.chat_width = Number(e.target.value);
33333334 localStorage.setItem(storage_keys.chat_width, power_user.chat_width);
33343335 applyChatWidth(applyMode);
33353336 setHotswapsDebounced();
33363337 });
33373338
@@ -3357,11 +3358,12 @@ $(document).ready(() => {
33573358 saveSettingsDebounced();
33583359 });
33593360
33603361 $('input[name="font_scale"]').on('input', async function (e, data) {
3362+ const applyMode = data?.forced ? 'forced' : 'normal';
33613363 power_user.font_scale = Number(e.target.value);
33623364 $('#font_scale_counter').val(power_user.font_scale);
33633365 localStorage.setItem(storage_keys.font_scale, power_user.font_scale);
33643366 await applyFontScale(applyMode);
33653367 saveSettingsDebounced();
33663368 });
33673369
public/scripts/world-info.js+15 -11
@@ -1931,39 +1931,43 @@ function displayWorldEntries(name, data, navigation = navigation_option.none, fl
19311931 }
19321932 });
19331933
19341934 $('#world_apply_custom_sortingworld_apply_current_sorting').off('click').on('click', async () => {
19351935 const entryCount = Object.keys(data.entries).length;
19361936 const moreThan100 = entryCount > 100;
19371937
19381938 let content = '<span>Apply your customcurrent sorting to the "Order" field. The Order values will go down from the chosen number.</span>';
19391939 if (moreThan100) {
19401940 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>`;
19411941 }
19421942
19431943 const result = await Popup.show.input('Apply CustomCurrent Sorting', content, '100', { okButton: 'Apply', cancelButton: 'Cancel' });
19441944 if (!result) return;
19451945
19461946 const start = Number(result);
19471947 if (isNaN(start) || start < 0) {
19481948 toastr.error('Invalid number: ' + result, 'Apply CustomCurrent Sorting');
19491949 return;
19501950 }
19511951 if (start < entryCount) {
19521952 toastr.warning('A number lower than the entry count has been chosen. All entries below that will default to 0.', 'Apply CustomCurrent Sorting');
19531953 }
19541954
1955- let counter = 0;
1955+ // We need to sort the entries here, as the data source isn't sorted
19561956 for (const entryentries of= Object.values(data.entries)) {;
1957- const newOrder = Math.max(start - (entry.displayIndex ?? 0), 0);
1957+ sortEntries(entries);
1958+
1959+ let updated = 0, current = start;
1960+ for (const entry of entries) {
1961+ const newOrder = Math.max(current--, 0);
19581962 if (entry.order === newOrder) continue;
19591963
19601964 entry.order = newOrder;
19611965 setOriginalDataValue(data, entry.order, 'order', entry.order);
19621966 counterupdated++;
19631967 }
19641968
19651969 if (counterupdated > 0) {
19661970 toastr.info(`Updated ${counterupdated} Order values`, 'Apply Custom Sorting');
19671971 await saveWorldInfo(name, data, true);
19681972 updateEditor(navigation_option.previous);
19691973 } else {