mirror of
				https://github.com/SillyTavern/SillyTavern.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	Refactor select2 choice click event to utils
This commit is contained in:
		| @@ -3310,7 +3310,7 @@ | |||||||
|                             <span data-i18n="Active World(s) for all chats"><small>Active World(s) for all chats</small></span> |                             <span data-i18n="Active World(s) for all chats"><small>Active World(s) for all chats</small></span> | ||||||
|                         </div> |                         </div> | ||||||
|                         <div class="range-block-range"> |                         <div class="range-block-range"> | ||||||
|                             <select id="world_info" class="select2_multi_sameline select2_choice_clickable" multiple> |                             <select id="world_info" class="select2_multi_sameline" multiple> | ||||||
|                                 <option value="" data-i18n="-- World Info not found --">-- World Info not found -- </option> |                                 <option value="" data-i18n="-- World Info not found --">-- World Info not found -- </option> | ||||||
|                             </select> |                             </select> | ||||||
|                         </div> |                         </div> | ||||||
|   | |||||||
| @@ -1525,6 +1525,36 @@ export function dynamicSelect2DataViaAjax(dataProvider) { | |||||||
|     return ajax; |     return ajax; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Subscribes a 'click' event handler to the choice elements of a select2 multi-select control | ||||||
|  |  * | ||||||
|  |  * @param {JQuery<HTMLElement>} control The original control the select2 was applied to | ||||||
|  |  * @param {function(EventTarget):void} action - The action to execute when a choice element is clicked | ||||||
|  |  * @param {object} options - Optional parameters | ||||||
|  |  * @param {boolean} [options.closeDrawer=false] - Whether the drawer should be closed and focus removed after the choice item was clicked | ||||||
|  |  */ | ||||||
|  | export function select2ChoiceClickSubscribe(control, action, { closeDrawer = false } = {}) { | ||||||
|  |     // Add class for styling (hover color, changed cursor, etc) | ||||||
|  |     control.addClass('select2_choice_clickable'); | ||||||
|  |  | ||||||
|  |     // Get the real container below and create a click handler on that one | ||||||
|  |     const select2Container = control.next('span.select2-container'); | ||||||
|  |     select2Container.on('click', function (event) { | ||||||
|  |         if ($(event.target).hasClass('select2-selection__choice__display')) { | ||||||
|  |             event.preventDefault(); | ||||||
|  |  | ||||||
|  |             // select2 still bubbles the event to open the dropdown. So we close it here and remove focus if we want that | ||||||
|  |             if (closeDrawer) { | ||||||
|  |                 control.select2('close'); | ||||||
|  |                 setTimeout(() => select2Container.find('textarea').trigger('blur'), debounce_timeout.quick); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             // Now execute the actual action that was subscribed | ||||||
|  |             action(event.target); | ||||||
|  |         } | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Applies syntax highlighting to a given regex string by generating HTML with classes |  * Applies syntax highlighting to a given regex string by generating HTML with classes | ||||||
|  * |  * | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| import { saveSettings, callPopup, substituteParams, getRequestHeaders, chat_metadata, this_chid, characters, saveCharacterDebounced, menu_type, eventSource, event_types, getExtensionPromptByName, saveMetadata, getCurrentChatId, extension_prompt_roles } from '../script.js'; | import { saveSettings, callPopup, substituteParams, getRequestHeaders, chat_metadata, this_chid, characters, saveCharacterDebounced, menu_type, eventSource, event_types, getExtensionPromptByName, saveMetadata, getCurrentChatId, extension_prompt_roles } from '../script.js'; | ||||||
| import { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, getCharaFilename, getSortableDelay, escapeRegex, PAGINATION_TEMPLATE, navigation_option, waitUntilCondition, isTrueBoolean, setValueByPath, flashHighlight, select2ModifyOptions, getStringHash, getSelect2OptionId, dynamicSelect2DataViaAjax, highlightRegex } from './utils.js'; | import { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, getCharaFilename, getSortableDelay, escapeRegex, PAGINATION_TEMPLATE, navigation_option, waitUntilCondition, isTrueBoolean, setValueByPath, flashHighlight, select2ModifyOptions, getStringHash, getSelect2OptionId, dynamicSelect2DataViaAjax, highlightRegex, select2ChoiceClickSubscribe } from './utils.js'; | ||||||
| import { extension_settings, getContext } from './extensions.js'; | import { extension_settings, getContext } from './extensions.js'; | ||||||
| import { NOTE_MODULE_NAME, metadata_keys, shouldWIAddPrompt } from './authors-note.js'; | import { NOTE_MODULE_NAME, metadata_keys, shouldWIAddPrompt } from './authors-note.js'; | ||||||
| import { registerSlashCommand } from './slash-commands.js'; | import { registerSlashCommand } from './slash-commands.js'; | ||||||
| @@ -3525,22 +3525,14 @@ jQuery(() => { | |||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         // Subscribe world loading to the select2 multiselect items (We need to target the specific select2 control) |         // Subscribe world loading to the select2 multiselect items (We need to target the specific select2 control) | ||||||
|         $('#world_info + span.select2-container').on('click', function (event) { |         select2ChoiceClickSubscribe($('#world_info'), (target) => { | ||||||
|             if ($(event.target).hasClass('select2-selection__choice__display')) { |             const name = $(target).text(); | ||||||
|                 event.preventDefault(); |  | ||||||
|  |  | ||||||
|                 // select2 still bubbles the event to open the dropdown. So we close it here and remove focus |  | ||||||
|                 $('#world_info').select2('close'); |  | ||||||
|                 setTimeout(() => $('#world_info + span.select2-container textarea').trigger('blur'), debounce_timeout.quick); |  | ||||||
|  |  | ||||||
|                 const name = $(event.target).text(); |  | ||||||
|             const selectedIndex = world_names.indexOf(name); |             const selectedIndex = world_names.indexOf(name); | ||||||
|             if (selectedIndex !== -1) { |             if (selectedIndex !== -1) { | ||||||
|                 $('#world_editor_select').val(selectedIndex).trigger('change'); |                 $('#world_editor_select').val(selectedIndex).trigger('change'); | ||||||
|                 console.log('Quick selection of world', name); |                 console.log('Quick selection of world', name); | ||||||
|             } |             } | ||||||
|             } |         }, { closeDrawer: true }); | ||||||
|         }); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     $('#WorldInfo').on('scroll', () => { |     $('#WorldInfo').on('scroll', () => { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user