Skip to content

Commit

Permalink
feat: update Spicetify.Config when loading things in (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
theRealPadster authored Sep 30, 2022
1 parent 4b85409 commit c9d611d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/marketplace/src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ class Card extends React.Component<CardProps, {
this.props.updateActiveTheme(this.localStorageKey);
// Update schemes in Grid, triggers state change and re-render
this.props.updateColourSchemes(parsedSchemes, activeScheme as string);

// Add to Spicetify.Config
const name = this.props.item.manifest?.name;
if (name) Spicetify.Config.current_theme = name;
if (activeScheme) Spicetify.Config.color_scheme = activeScheme;
}

this.setState({ installed: true });
Expand Down Expand Up @@ -314,6 +319,10 @@ class Card extends React.Component<CardProps, {
// Removes the current colour scheme
this.props.updateColourSchemes(null, null);

// Restore Spicetify.Config
Spicetify.Config.current_theme = Spicetify.Config.local_theme;
Spicetify.Config.color_scheme = Spicetify.Config.local_color_scheme;

this.setState({ installed: false });
}
}
Expand Down
16 changes: 16 additions & 0 deletions packages/marketplace/src/extensions/extension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
injectUserCSS,
addToSessionStorage,
sleep,
addExtensionToSpicetifyConfig,
} from "../logic/Utils";
import {
getBlacklist,
Expand Down Expand Up @@ -65,6 +66,9 @@ import {
script.src = `${script.src}?time=${Date.now()}`;

document.body.appendChild(script);

// Add to Spicetify.Config
addExtensionToSpicetifyConfig(extensionManifest.manifest?.main);
};

const initializeTheme = async (themeKey: string) => {
Expand All @@ -82,6 +86,9 @@ import {
const activeScheme = themeManifest.schemes[themeManifest.activeScheme];
injectColourScheme(activeScheme);

// Add to Spicetify.Config
Spicetify.Config.color_scheme = themeManifest.activeScheme;

if (localStorage.getItem(LOCALSTORAGE_KEYS.colorShift) === "true") {
initColorShiftLoop(themeManifest.schemes);
}
Expand All @@ -102,6 +109,9 @@ import {
const userCSS = await parseCSS(themeManifest);
injectUserCSS(userCSS);

// Add to Spicetify.Config
Spicetify.Config.current_theme = themeManifest.manifest?.name;

// Inject any included js
if (themeManifest.include && themeManifest.include.length) {
// console.log("Including js", installedThemeData.include);
Expand All @@ -119,12 +129,18 @@ import {
newScript.src = `${src}?time=${Date.now()}`;
newScript.classList.add("marketplaceScript");
document.body.appendChild(newScript);

// Add to Spicetify.Config
addExtensionToSpicetifyConfig(script);
});
}
};

console.log("Loaded Marketplace extension");

// Save to Spicetify.Config for use when removing a theme
Spicetify.Config.local_theme = Spicetify.Config.current_theme;
Spicetify.Config.local_color_scheme = Spicetify.Config.color_scheme;
const installedThemeKey = localStorage.getItem(LOCALSTORAGE_KEYS.themeInstalled);
if (installedThemeKey) initializeTheme(installedThemeKey);

Expand Down
9 changes: 9 additions & 0 deletions packages/marketplace/src/logic/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,12 @@ export const sanitizeUrl = (url: string) => {
return "about:blank";
return url;
};

export const addExtensionToSpicetifyConfig = (main?: string) => {
if (!main) return;

const name = main.split("/").pop();
if (name && Spicetify.Config.extensions.indexOf(name) === -1) {
Spicetify.Config.extensions.push(name);
}
};
10 changes: 7 additions & 3 deletions packages/marketplace/src/types/spicetify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1371,9 +1371,13 @@ declare namespace Spicetify {
*/
namespace Config {
const version: string;
const current_theme: string;
const color_scheme: string;
const extensions: string[];
let current_theme: string;
let color_scheme: string;
let extensions: string[];
const custom_apps: string[];

// These two are just added by Marketplace so we can save and restore them when removing a theme
let local_theme: string;
let local_color_scheme: string;
}
}

0 comments on commit c9d611d

Please sign in to comment.