mirror of
				https://github.com/SillyTavern/SillyTavern.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	Implement bulk operations for regex
This commit is contained in:
		@@ -21,13 +21,28 @@
 | 
			
		||||
                    <small data-i18n="ext_regex_import_script">Import</small>
 | 
			
		||||
                </div>
 | 
			
		||||
                <input type="file" id="import_regex_file" hidden accept="*.json" multiple />
 | 
			
		||||
                <div id="export_global_regex" class="menu_button menu_button_icon">
 | 
			
		||||
                    <i class="fa-solid fa-file-export"></i>
 | 
			
		||||
                    <small data-i18n="ext_regex_export_global">Export Global</small>
 | 
			
		||||
                <label for="regex_bulk_edit" class="menu_button menu_button_icon">
 | 
			
		||||
                    <input id="regex_bulk_edit" type="checkbox" class="displayNone" />
 | 
			
		||||
                    <i class="fa-solid fa-edit"></i>
 | 
			
		||||
                    <small data-i18n="ext_regex_bulk_edit">Bulk Edit</small>
 | 
			
		||||
                </label>
 | 
			
		||||
            </div>
 | 
			
		||||
            <div class="regex_bulk_operations flex-container justifyCenter">
 | 
			
		||||
                <div id="bulk_enable_regex" class="menu_button menu_button_icon">
 | 
			
		||||
                    <i class="fa-solid fa-toggle-on"></i>
 | 
			
		||||
                    <small data-i18n="Enable">Enable</small>
 | 
			
		||||
                </div>
 | 
			
		||||
                <div id="export_scoped_regex" class="menu_button menu_button_icon">
 | 
			
		||||
                <div id="bulk_disable_regex" class="menu_button menu_button_icon">
 | 
			
		||||
                    <i class="fa-solid fa-toggle-off"></i>
 | 
			
		||||
                    <small data-i18n="Disable">Disable</small>
 | 
			
		||||
                </div>
 | 
			
		||||
                <div id="bulk_export_regex" class="menu_button menu_button_icon">
 | 
			
		||||
                    <i class="fa-solid fa-file-export"></i>
 | 
			
		||||
                    <small data-i18n="ext_regex_export_scoped">Export Scoped</small>
 | 
			
		||||
                    <small data-i18n="Export">Export</small>
 | 
			
		||||
                </div>
 | 
			
		||||
                <div id="bulk_delete_regex" class="menu_button menu_button_icon">
 | 
			
		||||
                    <i class="fa-solid fa-trash"></i>
 | 
			
		||||
                    <small data-i18n="Delete">Delete</small>
 | 
			
		||||
                </div>
 | 
			
		||||
            </div>
 | 
			
		||||
            <hr />
 | 
			
		||||
 
 | 
			
		||||
@@ -607,22 +607,67 @@ jQuery(async () => {
 | 
			
		||||
        $('#import_regex_file').trigger('click');
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    $('#export_global_regex').on('click', async function () {
 | 
			
		||||
        const regexScripts = extension_settings?.regex ?? [];
 | 
			
		||||
        if (regexScripts.length) {
 | 
			
		||||
            const fileName = 'regex-global.json';
 | 
			
		||||
            const fileData = JSON.stringify(regexScripts, null, 4);
 | 
			
		||||
            download(fileData, fileName, 'application/json');
 | 
			
		||||
    function getSelectedScripts() {
 | 
			
		||||
        const scripts = getRegexScripts();
 | 
			
		||||
        const selector = '#regex_container .regex-script-label:has(.regex_bulk_checkbox:checked)';
 | 
			
		||||
        const selectedIds = Array.from(document.querySelectorAll(selector)).map(e => e.getAttribute('id')).filter(id => id);
 | 
			
		||||
        return scripts.filter(script => selectedIds.includes(script.id));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    $('#bulk_enable_regex').on('click', async function () {
 | 
			
		||||
        const scripts = getSelectedScripts().filter(script => script.disabled);
 | 
			
		||||
        if (scripts.length === 0) {
 | 
			
		||||
            toastr.warning(t`No regex scripts selected for enabling.`);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        for (const script of scripts) {
 | 
			
		||||
            script.disabled = false;
 | 
			
		||||
        }
 | 
			
		||||
        saveSettingsDebounced();
 | 
			
		||||
        await loadRegexScripts();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    $('#export_scoped_regex').on('click', async function () {
 | 
			
		||||
        const regexScripts = characters[this_chid]?.data?.extensions?.regex_scripts ?? [];
 | 
			
		||||
        if (regexScripts.length) {
 | 
			
		||||
            const fileName = `regex-${sanitizeFileName(characters[this_chid].name)}.json`;
 | 
			
		||||
            const fileData = JSON.stringify(regexScripts, null, 4);
 | 
			
		||||
            download(fileData, fileName, 'application/json');
 | 
			
		||||
    $('#bulk_disable_regex').on('click', async function () {
 | 
			
		||||
        const scripts = getSelectedScripts().filter(script => !script.disabled);
 | 
			
		||||
        if (scripts.length === 0) {
 | 
			
		||||
            toastr.warning(t`No regex scripts selected for disabling.`);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        for (const script of scripts) {
 | 
			
		||||
            script.disabled = true;
 | 
			
		||||
        }
 | 
			
		||||
        saveSettingsDebounced();
 | 
			
		||||
        await loadRegexScripts();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    $('#bulk_delete_regex').on('click', async function () {
 | 
			
		||||
        const scripts = getSelectedScripts();
 | 
			
		||||
        if (scripts.length === 0) {
 | 
			
		||||
            toastr.warning(t`No regex scripts selected for deletion.`);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        const confirm = await callGenericPopup('Are you sure you want to delete the selected regex scripts?', POPUP_TYPE.CONFIRM);
 | 
			
		||||
        if (!confirm) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        for (const script of scripts) {
 | 
			
		||||
            const isScoped = characters[this_chid]?.data?.extensions?.regex_scripts?.some(s => s.id === script.id);
 | 
			
		||||
            await deleteRegexScript({ id: script.id, isScoped: isScoped });
 | 
			
		||||
        }
 | 
			
		||||
        await reloadCurrentChat();
 | 
			
		||||
        saveSettingsDebounced();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    $('#bulk_export_regex').on('click', async function () {
 | 
			
		||||
        const scripts = getSelectedScripts();
 | 
			
		||||
        if (scripts.length === 0) {
 | 
			
		||||
            toastr.warning(t`No regex scripts selected for export.`);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        const fileName = `regex-${new Date().toISOString()}.json`;
 | 
			
		||||
        const fileData = JSON.stringify(scripts, null, 4);
 | 
			
		||||
        download(fileData, fileName, 'application/json');
 | 
			
		||||
        await loadRegexScripts();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let sortableDatas = [
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
<div class="regex-script-label flex-container flexnowrap">
 | 
			
		||||
    <input type="checkbox" class="regex_bulk_checkbox" />
 | 
			
		||||
    <span class="drag-handle menu-handle">☰</span>
 | 
			
		||||
    <div class="regex_script_name flexGrow overflow-hidden"></div>
 | 
			
		||||
    <div class="flex-container flexnowrap">
 | 
			
		||||
 
 | 
			
		||||
@@ -56,7 +56,7 @@
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.regex-script-label {
 | 
			
		||||
    align-items: center;
 | 
			
		||||
    align-items: baseline;
 | 
			
		||||
    border: 1px solid var(--SmartThemeBorderColor);
 | 
			
		||||
    border-radius: 10px;
 | 
			
		||||
    padding: 0 5px;
 | 
			
		||||
@@ -126,3 +126,25 @@ input.enable_scoped {
 | 
			
		||||
    right: 10px;
 | 
			
		||||
    transform: translateY(-50%);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.regex_settings label[for="regex_bulk_edit"]:has(#regex_bulk_edit:checked) {
 | 
			
		||||
    color: var(--golden);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.regex_settings .regex-script-container .regex-script-label .regex_bulk_checkbox {
 | 
			
		||||
    margin-left: 5px;
 | 
			
		||||
    margin-right: 5px;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.regex_settings .regex_bulk_operations,
 | 
			
		||||
.regex_settings .regex_bulk_checkbox {
 | 
			
		||||
    display: none;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.regex_settings:has(#regex_bulk_edit:checked) .regex_bulk_operations {
 | 
			
		||||
    display: flex;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.regex_settings:has(#regex_bulk_edit:checked) .regex_bulk_checkbox {
 | 
			
		||||
    display: inline-grid;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user