Merge pull request #2541 from SillyTavern/wi-update-order-from-custom WI update "Order" from custom sorting button
Signed| @@ -3630,7 +3630,8 @@ | |||
| 3630 | <div id="OpenAllWIEntries" class="menu_button fa-solid fa-expand" title="Open all Entries" data-i18n="[title]Open all Entries"></div> | 3630 | <div id="OpenAllWIEntries" class="menu_button fa-solid fa-expand" title="Open all Entries" data-i18n="[title]Open all Entries"></div> |
| 3631 | <div id="CloseAllWIEntries" class="menu_button fa-solid fa-compress" title="Close all Entries" data-i18n="[title]Close all Entries"></div> | 3631 | <div id="CloseAllWIEntries" class="menu_button fa-solid fa-compress" title="Close all Entries" data-i18n="[title]Close all Entries"></div> |
| 3632 | <div id="world_popup_new" class="menu_button fa-solid fa-plus" title="New Entry" data-i18n="[title]New Entry"></div> | 3632 | <div id="world_popup_new" class="menu_button fa-solid fa-plus" title="New Entry" data-i18n="[title]New Entry"></div> |
| 3633 | <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> | 3633 | <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" |
| 3634 | title="Apply custom sorting as Order" data-i18n="[title]Apply custom sorting as Order"></div> | ||
| 3634 | <div id="world_import_button" class="menu_button fa-solid fa-file-import" title="Import World Info" data-i18n="[title]Import World Info"></div> | 3635 | <div id="world_import_button" class="menu_button fa-solid fa-file-import" title="Import World Info" data-i18n="[title]Import World Info"></div> |
| 3635 | <div id="world_popup_export" class="menu_button fa-solid fa-file-export" title="Export World Info" data-i18n="[title]Export World Info"></div> | 3636 | <div id="world_popup_export" class="menu_button fa-solid fa-file-export" title="Export World Info" data-i18n="[title]Export World Info"></div> |
| 3636 | <div id="world_duplicate" class="menu_button fa-solid fa-paste" title="Duplicate World Info" data-i18n="[title]Duplicate World Info"></div> | 3637 | <div id="world_duplicate" class="menu_button fa-solid fa-paste" title="Duplicate World Info" data-i18n="[title]Duplicate World Info"></div> |
| @@ -1931,6 +1931,46 @@ function displayWorldEntries(name, data, navigation = navigation_option.none, fl | |||
| 1931 | } | 1931 | } |
| 1932 | }); | 1932 | }); |
| 1933 | 1933 | ||
| 1934 | $('#world_apply_custom_sorting').off('click').on('click', async () => { | ||
| 1935 | const entryCount = Object.keys(data.entries).length; | ||
| 1936 | const moreThan100 = entryCount > 100; | ||
| 1937 | |||
| 1938 | let content = '<span>Apply your custom sorting to the "Order" field. The Order values will go down from the chosen number.</span>'; | ||
| 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>`; | ||
| 1941 | } | ||
| 1942 | |||
| 1943 | const result = await Popup.show.input('Apply Custom Sorting', content, '100', { okButton: 'Apply', cancelButton: 'Cancel' }); | ||
| 1944 | if (!result) return; | ||
| 1945 | |||
| 1946 | const start = Number(result); | ||
| 1947 | if (isNaN(start) || start < 0) { | ||
| 1948 | toastr.error('Invalid number: ' + result, 'Apply Custom Sorting'); | ||
| 1949 | return; | ||
| 1950 | } | ||
| 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'); | ||
| 1953 | } | ||
| 1954 | |||
| 1955 | let counter = 0; | ||
| 1956 | for (const entry of Object.values(data.entries)) { | ||
| 1957 | const newOrder = Math.max(start - (entry.displayIndex ?? 0), 0); | ||
| 1958 | if (entry.order === newOrder) continue; | ||
| 1959 | |||
| 1960 | entry.order = newOrder; | ||
| 1961 | setOriginalDataValue(data, entry.order, 'order', entry.order); | ||
| 1962 | counter++; | ||
| 1963 | } | ||
| 1964 | |||
| 1965 | if (counter > 0) { | ||
| 1966 | toastr.info(`Updated ${counter} Order values`, 'Apply Custom Sorting'); | ||
| 1967 | await saveWorldInfo(name, data, true); | ||
| 1968 | updateEditor(navigation_option.previous); | ||
| 1969 | } else { | ||
| 1970 | toastr.info('All values up to date', 'Apply Custom Sorting'); | ||
| 1971 | } | ||
| 1972 | }); | ||
| 1973 | |||
| 1934 | $('#world_popup_export').off('click').on('click', () => { | 1974 | $('#world_popup_export').off('click').on('click', () => { |
| 1935 | if (name && data) { | 1975 | if (name && data) { |
| 1936 | const jsonValue = JSON.stringify(data); | 1976 | const jsonValue = JSON.stringify(data); |