Auto-set hotswap slot number based on the screen width.

This commit is contained in:
Cohee 2023-09-14 15:56:01 +03:00
parent 688551ffa6
commit 17a5d629ea
5 changed files with 34 additions and 11 deletions

View File

@ -3229,7 +3229,7 @@
</div> </div>
<div class="right_menu_button fa-solid fa-list-ul" id="rm_button_characters" title="Select/Create Characters" data-i18n="[title]Select/Create Characters"></div> <div class="right_menu_button fa-solid fa-list-ul" id="rm_button_characters" title="Select/Create Characters" data-i18n="[title]Select/Create Characters"></div>
</div> </div>
<div name="HotSwapWrapper" class="alignitemscenter flex-container margin0auto"> <div id="HotSwapWrapper" class="alignitemscenter flex-container margin0auto width100p">
<div class="hotswap flex-container flex1"></div> <div class="hotswap flex-container flex1"></div>
</div> </div>

View File

@ -8506,6 +8506,7 @@ jQuery(async function () {
jQuery(this).css('display', 'flex'); //flex needed to make charlist scroll jQuery(this).css('display', 'flex'); //flex needed to make charlist scroll
}, },
complete: async function () { complete: async function () {
favsToHotswap();
await delay(50); await delay(50);
$(this).closest('.drawer-content').removeClass('resizing'); $(this).closest('.drawer-content').removeClass('resizing');
$("#rm_print_characters_block").trigger("scroll"); $("#rm_print_characters_block").trigger("scroll");

View File

@ -279,10 +279,16 @@ export async function favsToHotswap() {
const entities = getEntitiesList({ doFilter: false }); const entities = getEntitiesList({ doFilter: false });
const container = $('#right-nav-panel .hotswap'); const container = $('#right-nav-panel .hotswap');
const template = $('#hotswap_template .hotswapAvatar'); const template = $('#hotswap_template .hotswapAvatar');
container.empty(); const DEFAULT_COUNT = 6;
const maxCount = 6; const WIDTH_PER_ITEM = 60; // 50px + 5px gap + 5px padding
const containerWidth = container.outerWidth();
const maxCount = containerWidth > 0 ? Math.floor(containerWidth / WIDTH_PER_ITEM) : DEFAULT_COUNT;
let count = 0; let count = 0;
const promises = [];
const newContainer = container.clone();
newContainer.empty();
for (const entity of entities) { for (const entity of entities) {
if (count >= maxCount) { if (count >= maxCount) {
break; break;
@ -315,13 +321,18 @@ export async function favsToHotswap() {
} }
if (isCharacter) { if (isCharacter) {
const imgLoadPromise = new Promise((resolve) => {
const avatarUrl = getThumbnailUrl('avatar', entity.item.avatar); const avatarUrl = getThumbnailUrl('avatar', entity.item.avatar);
$(slot).find('img').attr('src', avatarUrl); $(slot).find('img').attr('src', avatarUrl).on('load', resolve);
$(slot).attr('title', entity.item.avatar); $(slot).attr('title', entity.item.avatar);
});
// if the image doesn't load in 500ms, resolve the promise anyway
promises.push(Promise.race([imgLoadPromise, delay(500)]));
} }
$(slot).css('cursor', 'pointer'); $(slot).css('cursor', 'pointer');
container.append(slot); newContainer.append(slot);
count++; count++;
} }
@ -329,9 +340,12 @@ export async function favsToHotswap() {
if (count < maxCount) { //if any are left over if (count < maxCount) { //if any are left over
let leftOverSlots = maxCount - count; let leftOverSlots = maxCount - count;
for (let i = 1; i <= leftOverSlots; i++) { for (let i = 1; i <= leftOverSlots; i++) {
container.append(template.clone()); newContainer.append(template.clone());
} }
} }
await Promise.allSettled(promises);
container.replaceWith(newContainer);
} }
//changes input bar and send button display depending on connection status //changes input bar and send button display depending on connection status

View File

@ -16,7 +16,7 @@ import {
setEditedMessageId, setEditedMessageId,
renderTemplate, renderTemplate,
} from "../script.js"; } from "../script.js";
import { isMobile, initMovingUI } from "./RossAscends-mods.js"; import { isMobile, initMovingUI, favsToHotswap } from "./RossAscends-mods.js";
import { import {
groups, groups,
resetSelectedGroup, resetSelectedGroup,
@ -30,7 +30,7 @@ import {
import { registerSlashCommand } from "./slash-commands.js"; import { registerSlashCommand } from "./slash-commands.js";
import { tokenizers } from "./tokenizers.js"; import { tokenizers } from "./tokenizers.js";
import { countOccurrences, delay, isOdd, resetScrollHeight, sortMoments, timestampToMoment } from "./utils.js"; import { countOccurrences, debounce, delay, isOdd, resetScrollHeight, sortMoments, timestampToMoment } from "./utils.js";
export { export {
loadPowerUserSettings, loadPowerUserSettings,
@ -242,6 +242,8 @@ const storage_keys = {
let browser_has_focus = true; let browser_has_focus = true;
const debug_functions = []; const debug_functions = [];
const setHotswapsDebounced = debounce(favsToHotswap, 500);
export function switchSimpleMode() { export function switchSimpleMode() {
$('[data-newbie-hidden]').each(function () { $('[data-newbie-hidden]').each(function () {
$(this).toggleClass('displayNone', power_user.ui_mode === ui_mode.SIMPLE); $(this).toggleClass('displayNone', power_user.ui_mode === ui_mode.SIMPLE);
@ -1740,6 +1742,7 @@ $(document).ready(() => {
resetMovablePanels('resize'); resetMovablePanels('resize');
} }
// Adjust layout and styling here // Adjust layout and styling here
setHotswapsDebounced();
}); });
// Settings that go to settings.json // Settings that go to settings.json
@ -1864,6 +1867,7 @@ $(document).ready(() => {
power_user.chat_width = Number(e.target.value); power_user.chat_width = Number(e.target.value);
localStorage.setItem(storage_keys.chat_width, power_user.chat_width); localStorage.setItem(storage_keys.chat_width, power_user.chat_width);
applyChatWidth(); applyChatWidth();
setHotswapsDebounced();
}); });
$(`input[name="font_scale"]`).on('input', async function (e) { $(`input[name="font_scale"]`).on('input', async function (e) {

View File

@ -789,6 +789,10 @@ hr {
cursor: pointer; cursor: pointer;
} }
#HotSwapWrapper .hotswap {
justify-content: space-evenly;
}
.hotswapAvatar, .hotswapAvatar,
.hotswapAvatar .avatar { .hotswapAvatar .avatar {
width: 50px !important; width: 50px !important;