From 59e1f9cca1ec3cee9a568dc127824441eba82a3d Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 23 Jun 2024 19:43:56 +0300 Subject: [PATCH] Accept JSON-serialized string arrays for /setentryfield. Return JSON lists in /getentryfield --- public/scripts/utils.js | 23 +++++++++++++++++++++-- public/scripts/world-info.js | 6 +++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/public/scripts/utils.js b/public/scripts/utils.js index 5edf8e4e0..67203adc6 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -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'; } /** diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index fbdbd69c1..7b970c333 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -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') {