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 @@
3662 <div id="OpenAllWIEntries" class="menu_button fa-solid fa-expand" title="Open all Entries" data-i18n="[title]Open all Entries"></div>3662 <div id="OpenAllWIEntries" class="menu_button fa-solid fa-expand" title="Open all Entries" data-i18n="[title]Open all Entries"></div>
3663 <div id="CloseAllWIEntries" class="menu_button fa-solid fa-compress" title="Close all Entries" data-i18n="[title]Close all Entries"></div>3663 <div id="CloseAllWIEntries" class="menu_button fa-solid fa-compress" title="Close all Entries" data-i18n="[title]Close all Entries"></div>
3664 <div id="world_popup_new" class="menu_button fa-solid fa-plus" title="New Entry" data-i18n="[title]New Entry"></div>3664 <div id="world_popup_new" class="menu_button fa-solid fa-plus" title="New Entry" data-i18n="[title]New Entry"></div>
3665 <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"3665 <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>
3666 title="Apply custom sorting as Order" data-i18n="[title]Apply custom sorting as Order"></div>3666 <div id="world_apply_current_sorting" class="menu_button fa-solid fa-solid fa-arrow-down-9-1" title="Apply current sorting as Order" data-i18n="[title]Apply current sorting as Order"></div>
3667 <div id="world_import_button" class="menu_button fa-solid fa-file-import" title="Import World Info" data-i18n="[title]Import World Info"></div>3667 <div id="world_import_button" class="menu_button fa-solid fa-file-import" title="Import World Info" data-i18n="[title]Import World Info"></div>
3668 <div id="world_popup_export" class="menu_button fa-solid fa-file-export" title="Export World Info" data-i18n="[title]Export World Info"></div>3668 <div id="world_popup_export" class="menu_button fa-solid fa-file-export" title="Export World Info" data-i18n="[title]Export World Info"></div>
3669 <div id="world_duplicate" class="menu_button fa-solid fa-paste" title="Duplicate World Info" data-i18n="[title]Duplicate World Info"></div>3669 <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 () {
10822 //newSlider.val(manualInput)10822 //newSlider.val(manualInput)
10823 //handleSlideEvent.call(newSlider, null, { value: parseFloat(manualInput) }, 'manual');10823 //handleSlideEvent.call(newSlider, null, { value: parseFloat(manualInput) }, 'manual');
10824 valueBeforeManualInput = manualInput;10824 valueBeforeManualInput = manualInput;
10825 $(masterElement).val($(this).val()).trigger('input');10825 $(masterElement).val($(this).val()).trigger('input', { forced: true });
10826 } else {10826 } else {
10827 //if value not ok, warn and reset to last known valid value10827 //if value not ok, warn and reset to last known valid value
10828 toastr.warning(`Invalid value. Must be between ${$(this).attr('min')} and ${$(this).attr('max')}`);10828 toastr.warning(`Invalid value. Must be between ${$(this).attr('min')} and ${$(this).attr('max')}`);
@@ -10848,7 +10848,7 @@ jQuery(async function () {
10848 if (manualInput >= Number($(this).attr('min')) && manualInput <= Number($(this).attr('max'))) {10848 if (manualInput >= Number($(this).attr('min')) && manualInput <= Number($(this).attr('max'))) {
10849 valueBeforeManualInput = manualInput;10849 valueBeforeManualInput = manualInput;
10850 //set the slider value to input value10850 //set the slider value to input value
10851 $(masterElement).val($(this).val()).trigger('input');10851 $(masterElement).val($(this).val()).trigger('input', { forced: true });
10852 } else {10852 } else {
10853 //if value not ok, warn and reset to last known valid value10853 //if value not ok, warn and reset to last known valid value
10854 toastr.warning(`Invalid value. Must be between ${$(this).attr('min')} and ${$(this).attr('max')}`);10854 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
3329 });3329 });
33303330
3331 $('#chat_width_slider').on('input', function (e) {3331 $('#chat_width_slider').on('input', function (e, data) {
3332 const applyMode = data?.forced ? 'forced' : 'normal';
3332 power_user.chat_width = Number(e.target.value);3333 power_user.chat_width = Number(e.target.value);
3333 localStorage.setItem(storage_keys.chat_width, power_user.chat_width);3334 localStorage.setItem(storage_keys.chat_width, power_user.chat_width);
3334 applyChatWidth();3335 applyChatWidth(applyMode);
3335 setHotswapsDebounced();3336 setHotswapsDebounced();
3336 });3337 });
33373338
@@ -3357,11 +3358,12 @@ $(document).ready(() => {
3357 saveSettingsDebounced();3358 saveSettingsDebounced();
3358 });3359 });
33593360
3360 $('input[name="font_scale"]').on('input', async function (e) {3361 $('input[name="font_scale"]').on('input', async function (e, data) {
3362 const applyMode = data?.forced ? 'forced' : 'normal';
3361 power_user.font_scale = Number(e.target.value);3363 power_user.font_scale = Number(e.target.value);
3362 $('#font_scale_counter').val(power_user.font_scale);3364 $('#font_scale_counter').val(power_user.font_scale);
3363 localStorage.setItem(storage_keys.font_scale, power_user.font_scale);3365 localStorage.setItem(storage_keys.font_scale, power_user.font_scale);
3364 await applyFontScale();3366 await applyFontScale(applyMode);
3365 saveSettingsDebounced();3367 saveSettingsDebounced();
3366 });3368 });
33673369
public/scripts/world-info.js+15 -11
@@ -1931,39 +1931,43 @@ function displayWorldEntries(name, data, navigation = navigation_option.none, fl
1931 }1931 }
1932 });1932 });
19331933
1934 $('#world_apply_custom_sorting').off('click').on('click', async () => {1934 $('#world_apply_current_sorting').off('click').on('click', async () => {
1935 const entryCount = Object.keys(data.entries).length;1935 const entryCount = Object.keys(data.entries).length;
1936 const moreThan100 = entryCount > 100;1936 const moreThan100 = entryCount > 100;
19371937
1938 let content = '<span>Apply your custom sorting to the "Order" field. The Order values will go down from the chosen number.</span>';1938 let content = '<span>Apply your current sorting to the "Order" field. The Order values will go down from the chosen number.</span>';
1939 if (moreThan100) {1939 if (moreThan100) {
1940 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>`;1940 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>`;
1941 }1941 }
19421942
1943 const result = await Popup.show.input('Apply Custom Sorting', content, '100', { okButton: 'Apply', cancelButton: 'Cancel' });1943 const result = await Popup.show.input('Apply Current Sorting', content, '100', { okButton: 'Apply', cancelButton: 'Cancel' });
1944 if (!result) return;1944 if (!result) return;
19451945
1946 const start = Number(result);1946 const start = Number(result);
1947 if (isNaN(start) || start < 0) {1947 if (isNaN(start) || start < 0) {
1948 toastr.error('Invalid number: ' + result, 'Apply Custom Sorting');1948 toastr.error('Invalid number: ' + result, 'Apply Current Sorting');
1949 return;1949 return;
1950 }1950 }
1951 if (start < entryCount) {1951 if (start < entryCount) {
1952 toastr.warning('A number lower than the entry count has been chosen. All entries below that will default to 0.', 'Apply Custom Sorting');1952 toastr.warning('A number lower than the entry count has been chosen. All entries below that will default to 0.', 'Apply Current Sorting');
1953 }1953 }
19541954
1955 let counter = 0;1955 // We need to sort the entries here, as the data source isn't sorted
1956 for (const entry of Object.values(data.entries)) {1956 const entries = 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);
1958 if (entry.order === newOrder) continue;1962 if (entry.order === newOrder) continue;
19591963
1960 entry.order = newOrder;1964 entry.order = newOrder;
1961 setOriginalDataValue(data, entry.order, 'order', entry.order);1965 setOriginalDataValue(data, entry.order, 'order', entry.order);
1962 counter++;1966 updated++;
1963 }1967 }
19641968
1965 if (counter > 0) {1969 if (updated > 0) {
1966 toastr.info(`Updated ${counter} Order values`, 'Apply Custom Sorting');1970 toastr.info(`Updated ${updated} Order values`, 'Apply Custom Sorting');
1967 await saveWorldInfo(name, data, true);1971 await saveWorldInfo(name, data, true);
1968 updateEditor(navigation_option.previous);1972 updateEditor(navigation_option.previous);
1969 } else {1973 } else {