Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Snippet): preview & edit snippets #225

Merged
merged 2 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import AuthorsDiv from "./AuthorsDiv";
import TagsDiv from "./TagsDiv";
import Button from "../Button";

type CardProps = {
export type CardProps = {
// From `fetchExtensionManifest()`, `fetchThemeManifest()`, and snippets.json
item: CardItem | Snippet;

Expand Down Expand Up @@ -385,7 +385,17 @@ export default class Card extends React.Component<CardProps, {
}

return (
<div className={cardClasses.join(" ")} onClick={() => this.openReadme()}>
<div className={cardClasses.join(" ")} onClick={() => {
if (this.props.type === "snippet") {
const processedName = this.props.item.title.replace(/\n/g, "");

if (getLocalStorageDataFromKey(`marketplace:installed:snippet:${processedName}`)?.custom)
return openModal("EDIT_SNIPPET", undefined, undefined, this.props);

openModal("VIEW_SNIPPET", undefined, undefined, this.props);
} else this.openReadme();
}
}>
<div className="main-card-draggable" draggable="true">
<div className="main-card-imageContainer">
<div className="main-cardImage-imageWrapper">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,43 @@ import {
} from "../../../logic/Utils";
import { LOCALSTORAGE_KEYS } from "../../../constants";
import Button from "../../Button";
import { CardProps } from "../../Card/Card";
import { ModalType } from "../../../logic/LaunchModals";

const AddSnippetModal = () => {
const [code, setCode] = React.useState("");
const [name, setName] = React.useState("");
const [description, setDescription] = React.useState("");
const SnippetModal = (props: { content?: CardProps, type: ModalType }) => {
const [code, setCode] = React.useState(props.type === "ADD_SNIPPET" ? "" : props.content?.item.code || "");
const [name, setName] = React.useState(props.type === "ADD_SNIPPET" ? "" : props.content?.item.title || "");
const [description, setDescription] = React.useState(props.type === "ADD_SNIPPET" ? "" : props.content?.item.description || "");

const saveSnippet = () => {
const processedCode = code.replace(/\n/g, "");
const processedName = name.replace(/\n/g, "");
const processedDescription = description.trim();

const localStorageKey = `marketplace:installed:snippet:${processedName}`;
if (getLocalStorageDataFromKey(localStorageKey)) {
alert("That name is already taken!");
if (getLocalStorageDataFromKey(localStorageKey) && props.type !== "EDIT_SNIPPET") {
Spicetify.showNotification("That name is already taken!");
return;
}

console.log(`Installing snippet: ${processedName}`);
if (props.content && props.content.item.title !== processedName) {
// Remove from installed list
console.log(`Deleting outdated snippet: ${props.content.item.title}`);

localStorage.removeItem(`marketplace:installed:snippet:${props.content.item.title}`);
const installedSnippetKeys = getLocalStorageDataFromKey(LOCALSTORAGE_KEYS.installedSnippets, []);
const remainingInstalledSnippetKeys = installedSnippetKeys.filter((key: string) => key !== `marketplace:installed:snippet:${props.content?.item.title}`);
localStorage.setItem(LOCALSTORAGE_KEYS.installedSnippets, JSON.stringify(remainingInstalledSnippetKeys));
}

localStorage.setItem(
localStorageKey,
JSON.stringify({
code: processedCode,
description: processedDescription,
title: processedName,
custom: true,
}),
);

Expand All @@ -39,6 +52,7 @@ const AddSnippetModal = () => {
);
if (installedSnippetKeys.indexOf(localStorageKey) === -1) {
installedSnippetKeys.push(localStorageKey);
console.log(installedSnippetKeys);
localStorage.setItem(
LOCALSTORAGE_KEYS.installedSnippets,
JSON.stringify(installedSnippetKeys),
Expand All @@ -50,6 +64,7 @@ const AddSnippetModal = () => {
initializeSnippets(installedSnippets);

Spicetify.PopupModal.hide();
if (props.type === "EDIT_SNIPPET") location.reload();
};

return (
Expand All @@ -58,14 +73,20 @@ const AddSnippetModal = () => {
<label htmlFor="marketplace-custom-css">Custom CSS</label>
<textarea id="marketplace-custom-css"
rows={4} cols={50}
value={code} onChange={(e) => setCode(e.target.value)}
value={code} onChange={(e) => {
if (props.type !== "VIEW_SNIPPET")
setCode(e.target.value);
}}
placeholder="Input your own custom CSS here! You can find them in the installed tab for management."
/>
</div>
<div className="marketplace-customCSS-input-container">
<label htmlFor="marketplace-customCSS-name-submit">Snippet Name</label>
<input id="marketplace-customCSS-name-submit"
value={name} onChange={(e) => setName(e.target.value)}
value={name} onChange={(e) => {
if (props.type !== "VIEW_SNIPPET")
setName(e.target.value);
}}
placeholder="Enter a name for your custom snippet."
/>
</div>
Expand All @@ -74,15 +95,19 @@ const AddSnippetModal = () => {
Snippet Description
</label>
<input id="marketplace-customCSS-description-submit"
value={description} onChange={(e) => setDescription(e.target.value)}
value={description} onChange={(e) => {
if (props.type !== "VIEW_SNIPPET")
setDescription(e.target.value);
}}
placeholder="Enter a description for your custom snippet."
/>
</div>
<Button onClick={saveSnippet}>
Save CSS
</Button>
{props.type !== "VIEW_SNIPPET"
? <Button onClick={saveSnippet}>
Save CSS
</Button>
: <></>}
</div>
);
};

export default AddSnippetModal;
export default SnippetModal;
2 changes: 1 addition & 1 deletion src/extensions/extension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
}

// Show message on start.
Spicetify.showNotification("Loaded Marketplace extension!");
// Spicetify.showNotification("Loaded Marketplace extension!");

// Expose useful methods in global context
window.Marketplace = {
Expand Down
23 changes: 19 additions & 4 deletions src/logic/LaunchModals.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import React from "react";
import { Config } from "../types/marketplace-types";

import AddSnippetModal from "../components/Modals/AddSnippet";
import SnippetModal from "../components/Modals/Snippet";
import ReloadModal from "../components/Modals/Reload";
import SettingsModal from "../components/Modals/Settings";
import { CardProps } from "../components/Card/Card";

type ModalType = "ADD_SNIPPET" | "RELOAD" | "SETTINGS";
export type ModalType = "ADD_SNIPPET" | "EDIT_SNIPPET" | "VIEW_SNIPPET" | "RELOAD" | "SETTINGS";

const getModalSettings = (
modalType: ModalType,
CONFIG?: Config,
updateAppConfig?: (CONFIG: Config) => void,
props?: CardProps,
) => {
switch (modalType) {
case "ADD_SNIPPET":
return {
title: "Add Snippet",
content: <AddSnippetModal />,
content: <SnippetModal type={modalType} />,
isLarge: false,
};
case "EDIT_SNIPPET":
return {
title: "Edit Snippet",
content: <SnippetModal type={modalType} content={props as CardProps} />,
isLarge: false,
};
case "VIEW_SNIPPET":
return {
title: "View Snippet",
content: <SnippetModal type={modalType} content={props as CardProps} />,
isLarge: false,
};
case "RELOAD":
Expand Down Expand Up @@ -45,9 +59,10 @@ export const openModal = (
modal: ModalType,
CONFIG?: Config,
updateAppConfig?: (CONFIG: Config) => void,
props?: CardProps,
) => {
const triggerModal = () => {
const modalSettings = getModalSettings(modal, CONFIG, updateAppConfig);
const modalSettings = getModalSettings(modal, CONFIG, updateAppConfig, props);
Spicetify.PopupModal.display(modalSettings);
};

Expand Down