Merge pull request #2798 from SillyTavern/popup-command-styles Expand `/popup` command and refactor a bit

01b6ddbf8af179a3210082c8b0b7f3050bf28d92

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +67 -20Ignore whitespace
public/scripts/popup.js+2 -2
@@ -92,7 +92,7 @@ const showPopupHelper = {
9292 * @param {string?} header - The header text for the popup.
9393 * @param {string?} text - The main text for the popup.
9494 * @param {PopupOptions} [popupOptions={}] - Options for the popup.
9595 * @return {Promise<POPUP_RESULT?>} A Promise that resolves with the result of the user's interaction.
9696 */
9797 confirm: async (header, text, popupOptions = {}) => {
9898 const content = PopupUtils.BuildTextWithHeader(header, text);
@@ -107,7 +107,7 @@ const showPopupHelper = {
107107 * @param {string?} header - The header text for the popup.
108108 * @param {string?} text - The main text for the popup.
109109 * @param {PopupOptions} [popupOptions={}] - Options for the popup.
110110 * @return {Promise<POPUP_RESULT?>} A Promise that resolves with the result of the user's interaction.
111111 */
112112 text: async (header, text, popupOptions = {}) => {
113113 const content = PopupUtils.BuildTextWithHeader(header, text);
public/scripts/slash-commands.js+65 -18
@@ -1267,20 +1267,59 @@ export function initDefaultSlashCommands() {
12671267 callback: popupCallback,
12681268 returns: 'popup text',
12691269 namedArgumentList: [
12701270 new SlashCommandNamedArgument.fromProps({
1271- 'large', 'show large popup', [ARGUMENT_TYPE.BOOLEAN], false, false, null, commonEnumProviders.boolean('onOff')(),
1271+ name: 'large',
1272- ),
1272+ description: 'show large popup',
1273- new SlashCommandNamedArgument(
1273+ typeList: [ARGUMENT_TYPE.BOOLEAN],
1274- 'wide', 'show wide popup', [ARGUMENT_TYPE.BOOLEAN], false, false, null, commonEnumProviders.boolean('onOff')(),
1274+ enumList: commonEnumProviders.boolean('trueFalse')(),
1275- ),
1275+ defaultValue: 'false',
1276- new SlashCommandNamedArgument(
1276+ }),
1277- 'okButton', 'text for the OK button', [ARGUMENT_TYPE.STRING], false,
1277+ SlashCommandNamedArgument.fromProps({
1278- ),
1278+ name: 'wide',
1279+ description: 'show wide popup',
1280+ typeList: [ARGUMENT_TYPE.BOOLEAN],
1281+ enumList: commonEnumProviders.boolean('trueFalse')(),
1282+ defaultValue: 'false',
1283+ }),
1284+ SlashCommandNamedArgument.fromProps({
1285+ name: 'wider',
1286+ description: 'show wider popup',
1287+ typeList: [ARGUMENT_TYPE.BOOLEAN],
1288+ enumList: commonEnumProviders.boolean('trueFalse')(),
1289+ defaultValue: 'false',
1290+ }),
1291+ SlashCommandNamedArgument.fromProps({
1292+ name: 'transparent',
1293+ description: 'show transparent popup',
1294+ typeList: [ARGUMENT_TYPE.BOOLEAN],
1295+ enumList: commonEnumProviders.boolean('trueFalse')(),
1296+ defaultValue: 'false',
1297+ }),
1298+ SlashCommandNamedArgument.fromProps({
1299+ name: 'okButton',
1300+ description: 'text for the OK button',
1301+ typeList: [ARGUMENT_TYPE.STRING],
1302+ defaultValue: 'OK',
1303+ }),
1304+ SlashCommandNamedArgument.fromProps({
1305+ name: 'cancelButton',
1306+ description: 'text for the Cancel button',
1307+ typeList: [ARGUMENT_TYPE.STRING],
1308+ }),
1309+ SlashCommandNamedArgument.fromProps({
1310+ name: 'result',
1311+ description: 'if enabled, returns the popup result (as an integer) instead of the popup text. Resolves to 1 for OK and 0 cancel button, empty string for exiting out.',
1312+ typeList: [ARGUMENT_TYPE.BOOLEAN],
1313+ enumList: commonEnumProviders.boolean('trueFalse')(),
1314+ defaultValue: 'false',
1315+ }),
12791316 ],
12801317 unnamedArgumentList: [
12811318 new SlashCommandArgument.fromProps({
1282- 'text', [ARGUMENT_TYPE.STRING], true,
1319+ description: 'popup text',
1283- ),
1320+ typeList: [ARGUMENT_TYPE.STRING],
1321+ isRequired: true,
1322+ }),
12841323 ],
12851324 helpString: `
12861325 <div>
@@ -1291,7 +1330,10 @@ export function initDefaultSlashCommands() {
12911330 <strong>Example:</strong>
12921331 <ul>
12931332 <li>
12941333 <pre><code>/popup large=on wide=on okButton="SubmitConfirm" EnterPlease someconfirm text:this action.</code></pre>
1334+ </li>
1335+ <li>
1336+ <pre><code>/popup okButton="Left" cancelButton="Right" result=true Do you want to go left or right? | /echo 0 means right, 1 means left. Choice: {{pipe}}</code></pre>
12951337 </li>
12961338 </ul>
12971339 </div>
@@ -1882,16 +1924,21 @@ async function buttonsCallback(args, text) {
18821924}
18831925
18841926async function popupCallback(args, value) {
18851927 const safeValuesafeBody = DOMPurify.sanitize(value || '');
1928+ const safeHeader = args?.header && typeof args?.header === 'string' ? DOMPurify.sanitize(args.header) : null;
1929+ const requestedResult = isTrueBoolean(args?.result);
1930+
1931+ /** @type {import('./popup.js').PopupOptions} */
18861932 const popupOptions = {
18871933 large: isTrueBoolean(args?.large),
18881934 wide: isTrueBoolean(args?.wide),
1935+ wider: isTrueBoolean(args?.wider),
1936+ transparent: isTrueBoolean(args?.transparent),
18891937 okButton: args?.okButton !== undefined && typeof args?.okButton === 'string' ? args.okButton : 'Ok',
1938+ cancelButton: args?.cancelButton !== undefined && typeof args?.cancelButton === 'string' ? args.cancelButton : null,
18901939 };
1891- await delay(1);
1940+ const result = await Popup.show.text(safeHeader, safeBody, popupOptions);
1892- await callGenericPopup(safeValue, POPUP_TYPE.TEXT, '', popupOptions);
1941+ return String(requestedResult ? result ?? '' : value);
1893- await delay(1);
1894- return String(value);
18951942}
18961943
18971944async function getMessagesCallback(args, value) {