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