mirror of
https://github.com/Vendicated/Vencord.git
synced 2025-02-24 15:35:11 +00:00
implement a better way to convert string to array
This commit is contained in:
parent
6166a8ef17
commit
f5e455e041
7 changed files with 28 additions and 27 deletions
|
@ -224,19 +224,6 @@ export function migratePluginSettings(name: string, ...oldNames: string[]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function migrateSettingsToArrays(pluginName: string, settings: string[], stringSeparator: string | ((input: string) => string[]) = ",") {
|
|
||||||
const { plugins } = SettingsStore.plain;
|
|
||||||
if (plugins[pluginName] === undefined)
|
|
||||||
return logger.error(`Plugin '${pluginName}' does not exist and cannot be migrated! Did you spell it correctly?`);
|
|
||||||
for (const setting of settings) {
|
|
||||||
if (typeof plugins[pluginName][setting] !== "string") continue;
|
|
||||||
logger.info(`Migrating setting ${setting} from ${pluginName} to list`);
|
|
||||||
if (plugins[pluginName][setting] === "") plugins[pluginName][setting] = [];
|
|
||||||
else if (typeof stringSeparator === "string") plugins[pluginName][setting] = plugins[pluginName][setting].split(stringSeparator);
|
|
||||||
else plugins[pluginName][setting] = stringSeparator(plugins[pluginName][setting]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function definePluginSettings<
|
export function definePluginSettings<
|
||||||
Def extends SettingsDefinition,
|
Def extends SettingsDefinition,
|
||||||
Checks extends SettingsChecks<Def>,
|
Checks extends SettingsChecks<Def>,
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { definePluginSettings, migrateSettingsToArrays } from "@api/Settings";
|
import { definePluginSettings } from "@api/Settings";
|
||||||
import { Devs } from "@utils/constants";
|
import { Devs } from "@utils/constants";
|
||||||
import definePlugin, { OptionType, StartAt } from "@utils/types";
|
import definePlugin, { OptionType, StartAt } from "@utils/types";
|
||||||
|
|
||||||
|
@ -24,8 +24,6 @@ const NoopLogger = {
|
||||||
|
|
||||||
const logAllow = new Set();
|
const logAllow = new Set();
|
||||||
|
|
||||||
migrateSettingsToArrays("ConsoleJanitor", ["whitelistedLoggers"], s => s.split(";").map(x => x.trim()));
|
|
||||||
|
|
||||||
const settings = definePluginSettings({
|
const settings = definePluginSettings({
|
||||||
disableLoggers: {
|
disableLoggers: {
|
||||||
type: OptionType.BOOLEAN,
|
type: OptionType.BOOLEAN,
|
||||||
|
@ -43,6 +41,7 @@ const settings = definePluginSettings({
|
||||||
type: OptionType.ARRAY,
|
type: OptionType.ARRAY,
|
||||||
description: "List of loggers to allow even if others are hidden",
|
description: "List of loggers to allow even if others are hidden",
|
||||||
default: ["GatewaySocket", "Routing/Utils"],
|
default: ["GatewaySocket", "Routing/Utils"],
|
||||||
|
oldStringSeparator: s => s.split(";").map(x => x.trim()),
|
||||||
onChange(newVal: string[]) {
|
onChange(newVal: string[]) {
|
||||||
logAllow.clear();
|
logAllow.clear();
|
||||||
newVal.forEach(logAllow.add.bind(logAllow));
|
newVal.forEach(logAllow.add.bind(logAllow));
|
||||||
|
|
|
@ -21,7 +21,7 @@ import { addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu";
|
||||||
import { Settings } from "@api/Settings";
|
import { Settings } from "@api/Settings";
|
||||||
import { Logger } from "@utils/Logger";
|
import { Logger } from "@utils/Logger";
|
||||||
import { canonicalizeFind } from "@utils/patches";
|
import { canonicalizeFind } from "@utils/patches";
|
||||||
import { Patch, Plugin, ReporterTestable, StartAt } from "@utils/types";
|
import { OptionType, Patch, Plugin, ReporterTestable, StartAt } from "@utils/types";
|
||||||
import { FluxDispatcher } from "@webpack/common";
|
import { FluxDispatcher } from "@webpack/common";
|
||||||
import { FluxEvents } from "@webpack/types";
|
import { FluxEvents } from "@webpack/types";
|
||||||
|
|
||||||
|
@ -215,7 +215,21 @@ export function subscribeAllPluginsFluxEvents(fluxDispatcher: typeof FluxDispatc
|
||||||
}
|
}
|
||||||
|
|
||||||
export const startPlugin = traceFunction("startPlugin", function startPlugin(p: Plugin) {
|
export const startPlugin = traceFunction("startPlugin", function startPlugin(p: Plugin) {
|
||||||
const { name, commands, contextMenus } = p;
|
const { name, commands, contextMenus, settings } = 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) {
|
if (p.start) {
|
||||||
logger.info("Starting plugin", name);
|
logger.info("Starting plugin", name);
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
import { addChatBarButton, ChatBarButton } from "@api/ChatButtons";
|
import { addChatBarButton, ChatBarButton } from "@api/ChatButtons";
|
||||||
import { addButton, removeButton } from "@api/MessagePopover";
|
import { addButton, removeButton } from "@api/MessagePopover";
|
||||||
import { updateMessage } from "@api/MessageUpdater";
|
import { updateMessage } from "@api/MessageUpdater";
|
||||||
import { definePluginSettings, migrateSettingsToArrays } from "@api/Settings";
|
import { definePluginSettings } from "@api/Settings";
|
||||||
import ErrorBoundary from "@components/ErrorBoundary";
|
import ErrorBoundary from "@components/ErrorBoundary";
|
||||||
import { Devs } from "@utils/constants";
|
import { Devs } from "@utils/constants";
|
||||||
import { getStegCloak } from "@utils/dependencies";
|
import { getStegCloak } from "@utils/dependencies";
|
||||||
|
@ -93,13 +93,12 @@ const ChatBarIcon: ChatBarButton = ({ isMainChat }) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
migrateSettingsToArrays("InvisibleChat", ["savedPasswords"], s => s.split(",").map(s => s.trim()));
|
|
||||||
|
|
||||||
const settings = definePluginSettings({
|
const settings = definePluginSettings({
|
||||||
savedPasswords: {
|
savedPasswords: {
|
||||||
type: OptionType.ARRAY,
|
type: OptionType.ARRAY,
|
||||||
default: ["password", "Password"],
|
default: ["password", "Password"],
|
||||||
description: "Saved Passwords",
|
description: "Saved Passwords",
|
||||||
|
oldStringSeparator: s => s.split(",").map(s => s.trim()),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { definePluginSettings, migrateSettingsToArrays } from "@api/Settings";
|
import { definePluginSettings } from "@api/Settings";
|
||||||
import { Devs } from "@utils/constants";
|
import { Devs } from "@utils/constants";
|
||||||
import { Logger } from "@utils/Logger";
|
import { Logger } from "@utils/Logger";
|
||||||
import definePlugin, { OptionType } from "@utils/types";
|
import definePlugin, { OptionType } from "@utils/types";
|
||||||
|
@ -25,7 +25,6 @@ import presetQuotesText from "file://quotes.txt";
|
||||||
const presetQuotes = presetQuotesText.split("\n").map(quote => /^\s*[^#\s]/.test(quote) && quote.trim()).filter(Boolean) as string[];
|
const presetQuotes = presetQuotesText.split("\n").map(quote => /^\s*[^#\s]/.test(quote) && quote.trim()).filter(Boolean) as string[];
|
||||||
const noQuotesQuote = "Did you really disable all loading quotes? What a buffoon you are...";
|
const noQuotesQuote = "Did you really disable all loading quotes? What a buffoon you are...";
|
||||||
|
|
||||||
migrateSettingsToArrays("LoadingQuotes", ["additionalQuotes"], "|");
|
|
||||||
|
|
||||||
const settings = definePluginSettings({
|
const settings = definePluginSettings({
|
||||||
replaceEvents: {
|
replaceEvents: {
|
||||||
|
@ -46,6 +45,7 @@ const settings = definePluginSettings({
|
||||||
additionalQuotes: {
|
additionalQuotes: {
|
||||||
description: "Additional custom quotes to possibly appear",
|
description: "Additional custom quotes to possibly appear",
|
||||||
type: OptionType.ARRAY,
|
type: OptionType.ARRAY,
|
||||||
|
oldStringSeparator: "|",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ import "./messageLogger.css";
|
||||||
|
|
||||||
import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu";
|
import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu";
|
||||||
import { updateMessage } from "@api/MessageUpdater";
|
import { updateMessage } from "@api/MessageUpdater";
|
||||||
import { definePluginSettings, migrateSettingsToArrays, Settings } from "@api/Settings";
|
import { definePluginSettings, Settings } from "@api/Settings";
|
||||||
import { disableStyle, enableStyle } from "@api/Styles";
|
import { disableStyle, enableStyle } from "@api/Styles";
|
||||||
import ErrorBoundary from "@components/ErrorBoundary";
|
import ErrorBoundary from "@components/ErrorBoundary";
|
||||||
import { Devs } from "@utils/constants";
|
import { Devs } from "@utils/constants";
|
||||||
|
@ -36,9 +36,6 @@ import overlayStyle from "./deleteStyleOverlay.css?managed";
|
||||||
import textStyle from "./deleteStyleText.css?managed";
|
import textStyle from "./deleteStyleText.css?managed";
|
||||||
import { openHistoryModal } from "./HistoryModal";
|
import { openHistoryModal } from "./HistoryModal";
|
||||||
|
|
||||||
migrateSettingsToArrays("MessageLogger", ["ignoreChannels", "ignoreGuilds", "ignoreUsers"]);
|
|
||||||
|
|
||||||
|
|
||||||
const settings = definePluginSettings({
|
const settings = definePluginSettings({
|
||||||
deleteStyle: {
|
deleteStyle: {
|
||||||
type: OptionType.SELECT,
|
type: OptionType.SELECT,
|
||||||
|
|
|
@ -279,6 +279,11 @@ export interface PluginSettingArrayDef {
|
||||||
popoutText?: string;
|
popoutText?: string;
|
||||||
hidePopout?: boolean;
|
hidePopout?: boolean;
|
||||||
default?: any[];
|
default?: any[];
|
||||||
|
/**
|
||||||
|
* 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[]);
|
||||||
|
|
||||||
onChange?(newValue: any[]): void;
|
onChange?(newValue: any[]): void;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue