Accept JSON-serialized string arrays for /setentryfield. Return JSON lists in /getentryfield

This commit is contained in:
Cohee 2024-06-23 19:43:56 +03:00
parent 7c57132710
commit 59e1f9cca1
2 changed files with 24 additions and 5 deletions

View File

@ -623,6 +623,25 @@ export function isFalseBoolean(arg) {
return ['off', 'false', '0'].includes(arg?.trim()?.toLowerCase());
}
/**
* Parses an array either as a comma-separated string or as a JSON array.
* @param {string} value String to parse
* @returns {string[]} The parsed array.
*/
export function parseStringArray(value) {
if (!value || typeof value !== 'string') return [];
try {
const parsedValue = JSON.parse(value);
if (!Array.isArray(parsedValue)) {
throw new Error('Not an array');
}
return parsedValue.map(x => String(x));
} catch (e) {
return value.split(',').map(x => x.trim()).filter(x => x);
}
}
/**
* Checks if a number is odd.
* @param {number} number The number to check.
@ -1539,8 +1558,8 @@ export function flashHighlight(element, timespan = 2000) {
* @returns {boolean} Whether the control has an animation applied
*/
export function hasAnimation(control) {
const animatioName = getComputedStyle(control, null)["animation-name"];
return animatioName != "none";
const animatioName = getComputedStyle(control, null)['animation-name'];
return animatioName != 'none';
}
/**

View File

@ -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 { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, getCharaFilename, getSortableDelay, escapeRegex, PAGINATION_TEMPLATE, navigation_option, waitUntilCondition, isTrueBoolean, setValueByPath, flashHighlight, select2ModifyOptions, getSelect2OptionId, dynamicSelect2DataViaAjax, highlightRegex, select2ChoiceClickSubscribe, isFalseBoolean, equalsIgnoreCaseAndAccents, getSanitizedFilename, checkOverwriteExistingData } from './utils.js';
import { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, getCharaFilename, getSortableDelay, escapeRegex, PAGINATION_TEMPLATE, navigation_option, waitUntilCondition, isTrueBoolean, setValueByPath, flashHighlight, select2ModifyOptions, getSelect2OptionId, dynamicSelect2DataViaAjax, highlightRegex, select2ChoiceClickSubscribe, isFalseBoolean, equalsIgnoreCaseAndAccents, getSanitizedFilename, checkOverwriteExistingData, parseStringArray } from './utils.js';
import { extension_settings, getContext } from './extensions.js';
import { NOTE_MODULE_NAME, metadata_keys, shouldWIAddPrompt } from './authors-note.js';
import { isMobile } from './RossAscends-mods.js';
@ -617,7 +617,7 @@ function registerWorldInfoSlashCommands() {
}
if (Array.isArray(fieldValue)) {
return fieldValue.map(x => substituteParams(x)).join(', ');
return JSON.stringify(fieldValue.map(x => substituteParams(x)));
}
return substituteParams(String(fieldValue));
@ -684,7 +684,7 @@ function registerWorldInfoSlashCommands() {
}
if (Array.isArray(entry[field])) {
entry[field] = value.split(',').map(x => x.trim()).filter(x => x);
entry[field] = parseStringArray(value);
} else if (typeof entry[field] === 'boolean') {
entry[field] = isTrueBoolean(value);
} else if (typeof entry[field] === 'number') {