From d2b2b1b4a60eeea56b38f340e8f0d08faf4e240e Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 31 Mar 2026 20:08:56 +0300 Subject: [PATCH] fix: require long press to open swipe picker on phones (#5382) * fix: require long press to open swipe picker on phones * fix: clarify parameter description in assignLorebookToChat function * fix: update event parameter type in onSwipeCounterClick to include TouchEvent * fix: update event parameter types in onSwipeCounterClick and addLongPressEvent --- public/scripts/personas.js | 6 +++--- public/scripts/swipe-picker.js | 22 +++++++++++++++------- public/scripts/utils.js | 3 ++- public/scripts/world-info.js | 4 +--- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/public/scripts/personas.js b/public/scripts/personas.js index a7d1e2b96..57a3aa37c 100644 --- a/public/scripts/personas.js +++ b/public/scripts/personas.js @@ -1171,9 +1171,9 @@ function onPersonaDescriptionDepthRoleInput() { /** * Opens a popup to set the lorebook for the current persona. - * @param {JQuery.ClickEvent} event Click event + * @param {Pick} event Click event */ -async function onPersonaLoreButtonClick(event) { +async function onPersonaLoreButtonClick({ shiftKey, altKey }) { const personaName = power_user.personas[user_avatar]; const selectedLorebook = power_user.persona_description_lorebook; @@ -1182,7 +1182,7 @@ async function onPersonaLoreButtonClick(event) { return; } - if (selectedLorebook && !event.shiftKey && !event.altKey) { + if (selectedLorebook && !shiftKey && !altKey) { openWorldInfoEditor(selectedLorebook); return; } diff --git a/public/scripts/swipe-picker.js b/public/scripts/swipe-picker.js index 783f5007f..843cc45d8 100644 --- a/public/scripts/swipe-picker.js +++ b/public/scripts/swipe-picker.js @@ -3,8 +3,9 @@ import { SWIPE_DIRECTION, SWIPE_SOURCE } from './constants.js'; import { t } from './i18n.js'; import { callGenericPopup, Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js'; import { power_user } from './power-user.js'; +import { isMobile } from './RossAscends-mods.js'; import { getTokenCountAsync } from './tokenizers.js'; -import { clamp, copyText, timestampToMoment } from './utils.js'; +import { addLongPressEvent, clamp, copyText, timestampToMoment } from './utils.js'; import { chat, deleteSwipe, ensureSwipes, isMessageSwipeable, isSwipingAllowed, swipe, syncMesToSwipe } from '/script.js'; /** @@ -409,22 +410,29 @@ async function openSwipePicker(messageId) { } export function initSwipePicker() { - $(document).on('click', '.swipes-counter.swipe-picker-enabled', async function (e) { + /** + * Click handler for opening the swipe picker when clicking on the swipe counter. + * @param {JQuery.Event | Event} e Event object + */ + async function onSwipeCounterClick(e) { e.preventDefault(); e.stopPropagation(); const mesId = Number($(this).closest('.mes').attr('mesid')); await openSwipePicker(mesId); - }); + } + + if (isMobile()) { + addLongPressEvent('.swipes-counter.swipe-picker-enabled', onSwipeCounterClick); + } else { + $(document).on('click', '.swipes-counter.swipe-picker-enabled', onSwipeCounterClick); + } $(document).on('keydown', '.swipes-counter.swipe-picker-enabled', async function (e) { if (e.key !== ' ') { return; } - e.preventDefault(); - e.stopPropagation(); - const mesId = Number($(this).closest('.mes').attr('mesid')); - await openSwipePicker(mesId); + onSwipeCounterClick.call(this, e); }); $(document).on('click', '.mes_swipe_picker', async function (e) { e.preventDefault(); diff --git a/public/scripts/utils.js b/public/scripts/utils.js index 1d8464d1c..8d6f4c5ad 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -2955,7 +2955,7 @@ export function createTimeout(ms, errorMessage = '') { * Registers a long-press (touch hold) event as an alternative to modifier+click. * Supports event delegation for dynamically created elements. * @param {string} selector CSS selector for target elements - * @param {function} callback Callback to invoke on long-press, `this` is the matched element + * @param {(e: TouchEvent) => void} callback Callback to invoke on long-press, `this` is the matched element * @param {number} [delay=500] Long-press duration in ms */ export function addLongPressEvent(selector, callback, delay = 500) { @@ -2964,6 +2964,7 @@ export function addLongPressEvent(selector, callback, delay = 500) { let target = null; document.addEventListener('touchstart', function (event) { + if (!(event.target instanceof Element)) return; const el = event.target.closest(selector); if (!el) return; target = el; diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index 2af83ca7b..d6a09907c 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -5808,9 +5808,7 @@ export function openWorldInfoEditor(worldName) { /** * Assigns a lorebook to the current chat. - * @param {Object} options - The options for assigning the lorebook. - * @param {boolean} options.shiftKey - Whether the Shift key is pressed. - * @param {boolean} options.altKey - Whether the Alt key is pressed. + * @param {Pick} event Click event * @returns {Promise} */ export async function assignLorebookToChat({ shiftKey, altKey }) {