From f0bf8ab2e9a962270014700c85ba7db270057bb9 Mon Sep 17 00:00:00 2001 From: Elvyra <88881326+EepyElvyra@users.noreply.github.com> Date: Wed, 5 Feb 2025 14:24:07 +0100 Subject: [PATCH] minor improvements and fixes for plugin migration Co-authored-by: sadan4 <117494111+sadan4@users.noreply.github.com> --- src/plugins/index.ts | 46 +++++++++++++++++++----------- src/plugins/loadingQuotes/index.ts | 9 +++++- src/utils/types.ts | 4 ++- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/plugins/index.ts b/src/plugins/index.ts index 27a25659d..06ed6a001 100644 --- a/src/plugins/index.ts +++ b/src/plugins/index.ts @@ -23,7 +23,14 @@ import { addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu"; import { addMemberListDecorator, removeMemberListDecorator } from "@api/MemberListDecorators"; import { addMessageAccessory, removeMessageAccessory } from "@api/MessageAccessories"; import { addMessageDecoration, removeMessageDecoration } from "@api/MessageDecorations"; -import { addMessageClickListener, addMessagePreEditListener, addMessagePreSendListener, removeMessageClickListener, removeMessagePreEditListener, removeMessagePreSendListener } from "@api/MessageEvents"; +import { + addMessageClickListener, + addMessagePreEditListener, + addMessagePreSendListener, + removeMessageClickListener, + removeMessagePreEditListener, + removeMessagePreSendListener +} from "@api/MessageEvents"; import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover"; import { Settings, SettingsStore } from "@api/Settings"; import { disableStyle, enableStyle } from "@api/Styles"; @@ -149,6 +156,27 @@ for (const p of pluginsValues) { const def = p.settings.def[name]; const checks = p.settings.checks?.[name]; p.options[name] = { ...def, ...checks }; + + if ( + (def.type === OptionType.ARRAY || def.type === OptionType.USERS || def.type === OptionType.GUILDS || def.type === OptionType.CHANNELS) + && typeof p.settings.store[name] === "string" + ) { + if (p.settings.store[name] === "") + p.settings.store[name] = def.default ?? []; + else { + logger.info(`Converting string values of setting ${name} of plugin ${p.name} to array`); + + const sep = def.oldStringSeparator ?? ","; + let newVal: string[]; + if (typeof sep === "string" || sep instanceof RegExp) newVal = p.settings.store[name].split(sep); + else newVal = sep(p.settings.store[name]); + + // additional safeguard to prevent the new array to be an empty string, looks weird in the UI. + if (newVal.length > 1 || newVal[0] !== "") p.settings.store[name] = newVal; + else p.settings.store[name] = []; + + } + } } } @@ -255,25 +283,11 @@ export function subscribeAllPluginsFluxEvents(fluxDispatcher: typeof FluxDispatc export const startPlugin = traceFunction("startPlugin", function startPlugin(p: Plugin) { const { - name, commands, contextMenus, settings, managedStyle, userProfileBadge, + name, commands, contextMenus, managedStyle, userProfileBadge, onBeforeMessageEdit, onBeforeMessageSend, onMessageClick, renderChatBarButton, renderMemberListDecorator, renderMessageAccessory, renderMessageDecoration, renderMessagePopoverButton } = p; - if (settings != null) - for (const setting of Object.keys(settings.def)) { - const { type } = settings.def[setting]; - if (type === OptionType.ARRAY || type === OptionType.USERS || type === OptionType.GUILDS || type === OptionType.CHANNELS) { - if (typeof settings.store[setting] === "string") { - logger.info(`Converting string values of setting ${setting} of plugin ${name} to array`); - - const sep = settings.def[setting].oldStringSeparator ?? ","; - if (typeof sep === "string") settings.store[setting] = settings.store[setting].split(sep); - else settings.store[setting] = sep(settings.store[setting]); - } - } - } - if (p.start) { logger.info("Starting plugin", name); if (p.started) { diff --git a/src/plugins/loadingQuotes/index.ts b/src/plugins/loadingQuotes/index.ts index 7f7a25087..840438177 100644 --- a/src/plugins/loadingQuotes/index.ts +++ b/src/plugins/loadingQuotes/index.ts @@ -45,7 +45,14 @@ const settings = definePluginSettings({ additionalQuotes: { description: "Additional custom quotes to possibly appear", type: OptionType.ARRAY, - oldStringSeparator: "|", + oldStringSeparator: s => { + if ("additionalQuotesDelimiter" in settings.store) { + const deli = settings.store.additionalQuotesDelimiter ?? "|"; + delete settings.store.additionalQuotesDelimiter; + return s.split(deli); + } + return s.split("|"); + }, }, }); diff --git a/src/utils/types.ts b/src/utils/types.ts index 112f146a5..9e94e32d7 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -315,10 +315,12 @@ export interface PluginSettingArrayDef { /** * The text to show in the context-menu. * If not specified, the setting name will be used. + * Only applies to User, Channel, and Guild arrays. */ popoutText?: string; /** * If the context-menu entry should be hidden. + * Only applies to User, Channel, and Guild arrays. */ hidePopout?: boolean; default?: any[]; @@ -326,7 +328,7 @@ export interface PluginSettingArrayDef { * If the setting used to be a string with a custom delimiter, you can specify the delimiter or a function to split the string * @default "," */ - oldStringSeparator?: string | ((value: string) => string[]); + oldStringSeparator?: string | ((value: string) => string[]) | RegExp; onChange?(newValue: any[]): void; }