From b6547b463bbcdf0f7610c7d8bb53d3095ba311c4 Mon Sep 17 00:00:00 2001 From: Lewis Crichton Date: Mon, 16 Oct 2023 22:53:37 +0100 Subject: [PATCH] feat: @vc-requiredPlugins --- src/components/Icons.tsx | 18 +++++++++++++++++ src/components/ThemeSettings/ThemesTab.tsx | 23 +++++++++++++++++++--- src/utils/themes/usercss/index.ts | 10 ++++++++-- src/utils/themes/usercss/usercss-meta.d.ts | 23 +++++++++++++++++++++- 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/src/components/Icons.tsx b/src/components/Icons.tsx index 93b1323e..8ebe51a1 100644 --- a/src/components/Icons.tsx +++ b/src/components/Icons.tsx @@ -255,3 +255,21 @@ export function DeleteIcon(props: IconProps) { ); } + +/** + * A plugin icon, created by CorellanStoma. https://github.com/CreArts-Community/Settings-Icons + */ +export function PluginIcon(props: IconProps) { + return ( + + + + ); +} diff --git a/src/components/ThemeSettings/ThemesTab.tsx b/src/components/ThemeSettings/ThemesTab.tsx index 297fbfe3..e9543c20 100644 --- a/src/components/ThemeSettings/ThemesTab.tsx +++ b/src/components/ThemeSettings/ThemesTab.tsx @@ -21,7 +21,7 @@ import "./themesStyles.css"; import { Settings, useSettings } from "@api/Settings"; import { classNameFactory } from "@api/Styles"; import { Flex } from "@components/Flex"; -import { CogWheel, DeleteIcon } from "@components/Icons"; +import { CogWheel, DeleteIcon, PluginIcon } from "@components/Icons"; import { Link } from "@components/Link"; import { AddonCard } from "@components/VencordSettings/AddonCard"; import { SettingsTab, wrapTab } from "@components/VencordSettings/shared"; @@ -34,10 +34,11 @@ import type { ThemeHeader } from "@utils/themes"; import { getThemeInfo, stripBOM, type UserThemeHeader } from "@utils/themes/bd"; import { usercssParse } from "@utils/themes/usercss"; import { findByCodeLazy, findByPropsLazy, findLazy } from "@webpack"; -import { Button, Card, FluxDispatcher, Forms, React, showToast, TabBar, TextArea, useEffect, useRef, useState } from "@webpack/common"; -import type { ComponentType, Ref, SyntheticEvent } from "react"; +import { Button, Card, FluxDispatcher, Forms, React, showToast, TabBar, TextArea, Tooltip, useEffect, useMemo, useRef, useState } from "@webpack/common"; +import { type ComponentType, type Ref, type SyntheticEvent } from "react"; import type { UserstyleHeader } from "usercss-meta"; +import { isPluginEnabled } from "../../plugins"; import { UserCSSSettingsModal } from "./UserCSSModal"; type FileInput = ComponentType<{ @@ -118,6 +119,9 @@ interface UserCSSCardProps { } function UserCSSThemeCard({ theme, enabled, onChange, onDelete }: UserCSSCardProps) { + const missingPlugins = useMemo(() => + theme.requiredPlugins?.filter(p => !isPluginEnabled(p)), [theme]); + return ( + {missingPlugins && missingPlugins.length > 0 && ( + + {({ onMouseLeave, onMouseEnter }) => ( +
+ +
+ )} +
+ )} {theme.vars && (
openModal(modalProps => diff --git a/src/utils/themes/usercss/index.ts b/src/utils/themes/usercss/index.ts index a7d49a60..e4dbe234 100644 --- a/src/utils/themes/usercss/index.ts +++ b/src/utils/themes/usercss/index.ts @@ -10,16 +10,22 @@ import { parse as originalParse, UserstyleHeader } from "usercss-meta"; const UserCSSLogger = new Logger("UserCSS", "#d2acf5"); export async function usercssParse(text: string, fileName: string): Promise { - var { metadata, errors } = originalParse(text.replace(/\r/g, ""), { allowErrors: true }); + const { metadata, errors } = originalParse(text.replace(/\r/g, ""), { + allowErrors: true, + unknownKey: "assign" + }); if (errors.length) { UserCSSLogger.warn("Parsed", fileName, "with errors:", errors); } + const requiredPlugins = metadata["vc-requiredPlugins"]?.split(",").map(p => p.trim()); + return { ...metadata, fileName, - id: await getUserCssId(metadata) + id: await getUserCssId(metadata), + requiredPlugins }; } diff --git a/src/utils/themes/usercss/usercss-meta.d.ts b/src/utils/themes/usercss/usercss-meta.d.ts index 1a39bf6d..f4d29e63 100644 --- a/src/utils/themes/usercss/usercss-meta.d.ts +++ b/src/utils/themes/usercss/usercss-meta.d.ts @@ -55,6 +55,14 @@ declare module "usercss-meta" { */ fileName: string; + /** + * The required plugins for this style. + * + * @vencord Specific to Vencord, not part of the original module. + * @see {@link vc-requiredPlugins} + */ + requiredPlugins?: string[]; + /** * The name of your style. * @@ -107,7 +115,20 @@ declare module "usercss-meta" { * A list of variables the style defines. */ vars: Record; + + /** + * Required plugins for this style to work. Comma-separated list of plugin names. + * + * @vencord This is a Vencord-specific extension, however we wish for this to become a standard for client mods + * to implement, hence the more generic namespaced name. + */ + "vc-requiredPlugins"?: string; } - export function parse(text: string, options: { allowErrors: boolean; }): { metadata: UserstyleHeader; errors: { code: string; args: any; }[]; }; + type UserCSSParseOptions = { + allowErrors: boolean; + unknownKey: "assign"; + }; + + export function parse(text: string, options: UserCSSParseOptions): { metadata: UserstyleHeader; errors: { code: string; args: any; }[]; }; }