diff --git a/public/css/world-info.css b/public/css/world-info.css index ea78d774a..8b8f0afd4 100644 --- a/public/css/world-info.css +++ b/public/css/world-info.css @@ -157,8 +157,9 @@ width: 10em; } -#world_info_search { - width: 10em; +#world_info_search, +#world_info_sort_order { + width: 8em; } .wi-card-entry { diff --git a/public/index.html b/public/index.html index 2c35fc6ef..d5b7dfaac 100644 --- a/public/index.html +++ b/public/index.html @@ -2736,7 +2736,7 @@ or +
@@ -4780,4 +4795,4 @@ - \ No newline at end of file + diff --git a/public/script.js b/public/script.js index 162ff1bc5..41db24622 100644 --- a/public/script.js +++ b/public/script.js @@ -596,6 +596,7 @@ function getCurrentChatId() { const talkativeness_default = 0.5; const depth_prompt_depth_default = 4; +const per_page_default = 50; var is_advanced_char_open = false; @@ -922,7 +923,7 @@ async function printCharacters(fullRefresh = false) { const storageKey = 'Characters_PerPage'; $("#rm_print_characters_pagination").pagination({ dataSource: getEntitiesList({ doFilter: true }), - pageSize: Number(localStorage.getItem(storageKey)) || 50, + pageSize: Number(localStorage.getItem(storageKey)) || per_page_default, sizeChangerOptions: [10, 25, 50, 100, 250, 500, 1000], pageRange: 1, pageNumber: saveCharactersPage || 1, @@ -5487,7 +5488,7 @@ function select_rm_info(type, charId, previousCharId = null) { } try { - const perPage = Number(localStorage.getItem('Characters_PerPage')); + const perPage = Number(localStorage.getItem('Characters_PerPage')) || per_page_default; const page = Math.floor(charIndex / perPage) + 1; const selector = `#rm_print_characters_block [title^="${charId}"]`; $('#rm_print_characters_pagination').pagination('go', page); @@ -5522,7 +5523,7 @@ function select_rm_info(type, charId, previousCharId = null) { return; } - const perPage = Number(localStorage.getItem('Characters_PerPage')); + const perPage = Number(localStorage.getItem('Characters_PerPage')) || per_page_default; const page = Math.floor(charIndex / perPage) + 1; $('#rm_print_characters_pagination').pagination('go', page); const selector = `#rm_print_characters_block [grid="${charId}"]`; diff --git a/public/scripts/utils.js b/public/scripts/utils.js index db64431ba..9d001384d 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -12,7 +12,7 @@ export const PAGINATION_TEMPLATE = '<%= rangeStart %>-<%= rangeEnd %> of <%= tot * Navigation options for pagination. * @enum {number} */ -export const navigation_option = { none: 0, previous: 1, last: 2, }; +export const navigation_option = { none: -2000, previous: -1000, }; export function escapeHtml(str) { return String(str).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index c4929441e..a4df6e964 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -1,5 +1,5 @@ import { saveSettings, callPopup, substituteParams, getRequestHeaders, chat_metadata, this_chid, characters, saveCharacterDebounced, menu_type, eventSource, event_types } from "../script.js"; -import { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, getCharaFilename, getSortableDelay, escapeRegex, PAGINATION_TEMPLATE, navigation_option } from "./utils.js"; +import { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, getCharaFilename, getSortableDelay, escapeRegex, PAGINATION_TEMPLATE, navigation_option, waitUntilCondition } from "./utils.js"; import { getContext } from "./extensions.js"; import { NOTE_MODULE_NAME, metadata_keys, shouldWIAddPrompt } from "./authors-note.js"; import { registerSlashCommand } from "./slash-commands.js"; @@ -51,6 +51,7 @@ let updateEditor = (navigation) => { navigation; }; // Do not optimize. updateEditor is a function that is updated by the displayWorldEntries with new data. const worldInfoFilter = new FilterHelper(() => updateEditor()); +const SORT_ORDER_KEY = 'world_info_sort_order'; const InputWidthReference = $("#WIInputWidthReference"); @@ -163,6 +164,7 @@ function setWorldInfoSettings(settings, data) { $("#world_editor_select").append(``); }); + $('#world_info_sort_order').val(localStorage.getItem(SORT_ORDER_KEY) || '0'); $("#world_editor_select").trigger("change"); } @@ -234,6 +236,49 @@ function getWIElement(name) { return wiElement; } +/** + * @param {any[]} data WI entries + * @returns {any[]} Sorted data + */ +function sortEntries(data) { + const option = $('#world_info_sort_order').find(":selected"); + const sortField = option.data('field'); + const sortOrder = option.data('order'); + const sortRule = option.data('rule'); + const orderSign = sortOrder === 'asc' ? 1 : -1; + + if (sortRule === 'priority') { + // First constant, then normal, then disabled. Then sort by order + data.sort((a, b) => { + const aValue = a.constant ? 0 : a.disable ? 2 : 1; + const bValue = b.constant ? 0 : b.disable ? 2 : 1; + + return (aValue - bValue || b.order - a.order); + }); + } else { + data.sort((a, b) => { + const aValue = a[sortField]; + const bValue = b[sortField]; + + // Sort strings + if (typeof aValue === 'string' && typeof bValue === 'string') { + if (sortRule === 'length') { + // Sort by string length + return orderSign * (aValue.length - bValue.length); + } else { + // Sort by A-Z ordinal + return orderSign * aValue.localeCompare(bValue); + } + } + + // Sort numbers + return orderSign * (Number(aValue) - Number(bValue)); + }); + } + + return data; +} + function nullWorldInfo() { toastr.info("Create or import a new World Info file first.", "World Info is not set", { timeOut: 10000, preventDuplicates: true }); } @@ -263,8 +308,9 @@ function displayWorldEntries(name, data, navigation = navigation_option.none) { // Sort the entries array by displayIndex and uid entriesArray.sort((a, b) => a.displayIndex - b.displayIndex || a.uid - b.uid); + entriesArray = sortEntries(entriesArray); entriesArray = worldInfoFilter.applyFilters(entriesArray); - callback(entriesArray); + typeof callback === 'function' && callback(entriesArray); return entriesArray; } @@ -275,9 +321,10 @@ function displayWorldEntries(name, data, navigation = navigation_option.none) { } const storageKey = 'WI_PerPage'; + const perPageDefault = 25; $("#world_info_pagination").pagination({ dataSource: getDataArray, - pageSize: Number(localStorage.getItem(storageKey)) || 25, + pageSize: Number(localStorage.getItem(storageKey)) || perPageDefault, sizeChangerOptions: [10, 25, 50, 100], showSizeChanger: true, pageRange: 1, @@ -321,8 +368,28 @@ function displayWorldEntries(name, data, navigation = navigation_option.none) { } }); - if (navigation === navigation_option.last) { - $("#world_info_pagination").pagination('go', $("#world_info_pagination").pagination('getTotalPage')); + if (typeof navigation === 'number' && Number(navigation) >= 0) { + const selector = `#world_popup_entries_list [uid="${navigation}"]`; + const data = getDataArray(); + const uidIndex = data.findIndex(x => x.uid === navigation); + const perPage = Number(localStorage.getItem(storageKey)) || perPageDefault; + const page = Math.floor(uidIndex / perPage) + 1; + $("#world_info_pagination").pagination('go', page); + waitUntilCondition(() => document.querySelector(selector) !== null).finally(() => { + const element = $(selector); + + if (element.length === 0) { + console.log(`Could not find element for uid ${navigation}`); + return; + } + + const elementOffset = element.offset(); + const parentOffset = element.parent().offset(); + const scrollOffset = elementOffset.top - parentOffset.top; + $('#WorldInfo').scrollTop(scrollOffset); + element.addClass('flash animated'); + setTimeout(() => element.removeClass('flash animated'), 2000); + }); } $("#world_popup_new").off('click').on('click', () => { @@ -438,6 +505,7 @@ function deleteOriginalDataValue(data, uid) { function getWorldEntry(name, data, entry) { const template = $("#entry_edit_template .world_entry").clone(); template.data("uid", entry.uid); + template.attr("uid", entry.uid); // key const keyInput = template.find('textarea[name="key"]'); @@ -1005,7 +1073,7 @@ function createWorldInfoEntry(name, data) { const newEntry = { uid: newUid, ...newEntryTemplate }; data.entries[newUid] = newEntry; - updateEditor(navigation_option.last); + updateEditor(newUid); } async function _save(name, data) { @@ -1908,6 +1976,14 @@ jQuery(() => { worldInfoFilter.setFilterData(FILTER_TYPES.WORLD_INFO_SEARCH, term); }); + $('#world_info_sort_order').on('change', function(e) { + if (e.target instanceof HTMLOptionElement) { + localStorage.setItem(SORT_ORDER_KEY, e.target.value); + } + + updateEditor(navigation_option.none); + }) + // Not needed on mobile const deviceInfo = getDeviceInfo(); if (deviceInfo && deviceInfo.device.type === 'desktop') {