Add info block for find regex flags

#3647
This commit is contained in:
Cohee
2025-03-09 17:41:38 +02:00
parent 6f0f32d83d
commit eef9c3ef62
3 changed files with 72 additions and 2 deletions

View File

@ -7,7 +7,7 @@ import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '
import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashCommandEnumValue.js';
import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
import { download, getFileText, getSortableDelay, uuidv4 } from '../../utils.js';
import { download, getFileText, getSortableDelay, regexFromString, uuidv4 } from '../../utils.js';
import { regex_placement, runRegexScript, substitute_find_regex } from './engine.js';
import { t } from '../../i18n.js';
import { accountStorage } from '../../util/AccountStorage.js';
@ -258,6 +258,8 @@ async function onRegexEditorOpenClick(existingId, isScoped) {
});
function updateTestResult() {
updateInfoBlock(editorHtml);
if (!editorHtml.find('#regex_test_mode').is(':visible')) {
return;
}
@ -276,6 +278,7 @@ async function onRegexEditorOpenClick(existingId, isScoped) {
}
editorHtml.find('input, textarea, select').on('input', updateTestResult);
updateInfoBlock(editorHtml);
const popupResult = await callPopup(editorHtml, 'confirm', undefined, { okButton: t`Save` });
if (popupResult) {
@ -305,6 +308,48 @@ async function onRegexEditorOpenClick(existingId, isScoped) {
}
}
/**
* Updates the info block in the regex editor with hints regarding the find regex.
* @param {JQuery<HTMLElement>} editorHtml The editor HTML
*/
function updateInfoBlock(editorHtml) {
const infoBlock = editorHtml.find('.info-block');
const infoBlockTextSpan = infoBlock.find('.info-block-text');
const infoBlockFlagsHint = infoBlock.find('.info-block-flags-hint');
const findRegex = String(editorHtml.find('.find_regex').val());
infoBlock.removeClass('error hint info warning');
infoBlockFlagsHint.hide();
// Clear the info block if the find regex is empty
if (!findRegex) {
infoBlock.addClass('info');
infoBlockTextSpan.text(t`Find Regex is empty`);
return;
}
try {
const regex = regexFromString(findRegex);
if (!regex) {
throw new Error(t`Invalid Find Regex`);
}
const flagInfo = [];
// Check flags
flagInfo.push(regex.flags.includes('g') ? t`Applies to all matches` : t`Applies to the first match`);
flagInfo.push(regex.flags.includes('i') ? t`Case insensitive` : t`Case sensitive`);
infoBlock.addClass('hint');
infoBlockTextSpan.text(flagInfo.join('. '));
infoBlockFlagsHint.show();
} catch (error) {
infoBlock.addClass('error');
infoBlockTextSpan.text(error.message);
return;
}
}
// Common settings migration function. Some parts will eventually be removed
// TODO: Maybe migrate placement to strings?
function migrateSettings() {
@ -418,7 +463,7 @@ async function onRegexImportFileChange(file, isScoped) {
}
}
function purgeEmbeddedRegexScripts( { character }){
function purgeEmbeddedRegexScripts({ character }) {
const avatar = character?.avatar;
if (avatar && extension_settings.character_allowed_regex?.includes(avatar)) {