Skip to content

Commit e8cae27

Browse files
authored
Merge pull request #30 from wajeshubham/presets
Presets
2 parents f0cdb19 + 857a8ea commit e8cae27

File tree

7 files changed

+102
-45
lines changed

7 files changed

+102
-45
lines changed

__tests__/components/snippng_code_area.test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SnippngCodeArea, SnippngControlHeader } from "@/components";
22
import { SnippngEditorContext } from "@/context/SnippngEditorContext";
33
import { defaultEditorConfig } from "@/lib/constants";
4+
import { getEditorWrapperBg } from "@/utils";
45
import { render, screen, waitFor } from "@testing-library/react";
56
import { act } from "react-dom/test-utils";
67

@@ -222,5 +223,20 @@ describe("SnippngCodeArea", () => {
222223
expect(selectedBackgroundColor).toBe(wrapperBackgroundColor);
223224
});
224225
});
226+
227+
describe("Gradients property", () => {
228+
it("renders wrapper with gradient equals to the gradient array", async () => {
229+
let renderedBackground = getEditorWrapperBg(
230+
"#eee811",
231+
["#ba68c8", "#ffa7c4", "#e57373"],
232+
140
233+
);
234+
await waitFor(() => {
235+
expect(renderedBackground).toBe(
236+
`linear-gradient(140deg, #ba68c8, #ffa7c4, #e57373)`
237+
);
238+
});
239+
});
240+
});
225241
});
226242
});

__tests__/lib/color_picker.test.tsx

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import {
55
rgbToHsv,
66
hsvToRgb,
77
} from "@/lib/color-picker/utils";
8-
import { getEditorWrapperBg } from "@/utils";
9-
import { waitFor } from "@testing-library/react";
108

119
describe("ColoPicker", () => {
1210
describe("Utility functions", () => {
@@ -48,19 +46,4 @@ describe("ColoPicker", () => {
4846
expect(Math.floor(expectedRGB.b)).toBe(Math.floor(convertedRGB.b));
4947
});
5048
});
51-
52-
describe("Gradients property", () => {
53-
it("renders wrapper with gradient equals to the gradient array", async () => {
54-
let renderedBackground = getEditorWrapperBg(
55-
"#eee811",
56-
["#ba68c8", "#ffa7c4", "#e57373"],
57-
140
58-
);
59-
await waitFor(() => {
60-
expect(renderedBackground).toBe(
61-
`linear-gradient(140deg, #ba68c8, #ffa7c4, #e57373)`
62-
);
63-
});
64-
});
65-
});
6649
});

components/editor/SnippngCodeArea.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef, useState } from "react";
1+
import { useEffect, useRef, useState } from "react";
22

33
import { DEFAULT_BASE_SETUP } from "@/lib/constants";
44
import {
@@ -7,6 +7,7 @@ import {
77
getEditorWrapperBg,
88
getLanguage,
99
getTheme,
10+
LocalStorage,
1011
} from "@/utils";
1112

1213
import { langs, loadLanguage } from "@uiw/codemirror-extensions-langs";
@@ -107,6 +108,17 @@ const SnippngCodeArea = () => {
107108
}
108109
};
109110

111+
useEffect(() => {
112+
// if there is a uid means we are on edit page where we want to avoid persisting the editor config
113+
if (uid) return;
114+
// persist the editor config changes only when user is creating new snippet
115+
LocalStorage.set("config", {
116+
...editorConfig,
117+
uid: undefined,
118+
ownerUid: undefined,
119+
});
120+
}, [editorConfig, uid]);
121+
110122
return (
111123
<>
112124
<section
@@ -247,7 +259,7 @@ const SnippngCodeArea = () => {
247259
? "Fork snippet"
248260
: "Save snippet"}
249261
</Button>
250-
{user && user.uid === ownerUid ? (
262+
{uid && user && user.uid === ownerUid ? (
251263
<Button
252264
StartIcon={ArrowPathIcon}
253265
disabled={updating}

components/editor/SnippngControlHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ const SnippngControlHeader: React.FC<{
213213
leaveFrom="transform opacity-100 scale-100"
214214
leaveTo="transform opacity-0 scale-95"
215215
>
216-
<Menu.Items className="absolute right-0 max-h-[500px] z-30 mt-2 w-72 origin-top-right divide-y-[1px] dark:divide-zinc-400 divide-zinc-300 dark:bg-black bg-white overflow-auto text-sm rounded-sm outline outline-[1px] dark:outline-zinc-400 outline-zinc-300 dark:text-white text-zinc-900">
216+
<Menu.Items className="absolute right-0 max-h-[500px] z-30 mt-2 w-96 origin-top-right divide-y-[1px] dark:divide-zinc-400 divide-zinc-300 dark:bg-black bg-white overflow-auto text-sm rounded-sm outline outline-[1px] dark:outline-zinc-400 outline-zinc-300 dark:text-white text-zinc-900">
217217
<Menu.Button
218218
className="w-full text-left p-2 inline-flex items-center"
219219
onClick={() => {

lib/constants.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { SnippngEditorConfigInterface } from "@/types";
2+
import { LocalStorage } from "@/utils";
3+
4+
export const isBrowser = typeof window !== "undefined";
25

36
export const THEMES = [
47
{
@@ -514,31 +517,40 @@ export const PEXELS_QUERY_STRINGS = [
514517
"technology",
515518
];
516519

517-
export const defaultEditorConfig: SnippngEditorConfigInterface = {
518-
code: DEFAULT_CODE_SNIPPET,
519-
snippetsName: "",
520-
editorFontSize: 16,
521-
editorWindowControlsType: "mac-left",
522-
fileName: "@utils/debounce.ts",
523-
hasDropShadow: true,
524-
lineHeight: 19,
525-
paddingHorizontal: 70,
526-
paddingVertical: 70,
527-
rounded: true,
528-
selectedLang:
529-
LANGUAGES.find((language) => language.id === "typescript") || LANGUAGES[0],
530-
selectedTheme:
531-
THEMES.find((theme) => theme.id === "tokyoNightStorm") || THEMES[0],
532-
showFileName: true,
533-
showLineNumbers: true,
534-
wrapperBg: "#eee811",
535-
gradients: ["#ba68c8", "#ffa7c4", "#e57373"],
536-
gradientAngle: 140,
537-
editorWidth: 0,
538-
bgImageVisiblePatch: null,
539-
bgBlur: 0,
520+
const getConfig = (): SnippngEditorConfigInterface => {
521+
let persistedConfig = LocalStorage.get(
522+
"config"
523+
) as SnippngEditorConfigInterface;
524+
if (persistedConfig) return persistedConfig;
525+
return {
526+
code: DEFAULT_CODE_SNIPPET,
527+
snippetsName: "",
528+
editorFontSize: 16,
529+
editorWindowControlsType: "mac-left",
530+
fileName: "@utils/debounce.ts",
531+
hasDropShadow: true,
532+
lineHeight: 19,
533+
paddingHorizontal: 70,
534+
paddingVertical: 70,
535+
rounded: true,
536+
selectedLang:
537+
LANGUAGES.find((language) => language.id === "typescript") ||
538+
LANGUAGES[0],
539+
selectedTheme:
540+
THEMES.find((theme) => theme.id === "tokyoNightStorm") || THEMES[0],
541+
showFileName: true,
542+
showLineNumbers: true,
543+
wrapperBg: "#eee811",
544+
gradients: ["#ba68c8", "#ffa7c4", "#e57373"],
545+
gradientAngle: 140,
546+
editorWidth: 0,
547+
bgImageVisiblePatch: null,
548+
bgBlur: 0,
549+
};
540550
};
541551

552+
export const defaultEditorConfig: SnippngEditorConfigInterface = getConfig();
553+
542554
export const DEFAULT_BASE_SETUP = {
543555
foldGutter: false,
544556
highlightActiveLine: false,

pages/snippet/[uid].tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ const SavedSnippet = () => {
4747
if (!router.isReady) return;
4848
fetchCodeSnippet();
4949
return () => {
50-
setEditorConfig({ ...defaultEditorConfig });
50+
// set uid & ownerUid to undefined because we are leaving the snippet details page, the only page where uid is required
51+
setEditorConfig({
52+
...defaultEditorConfig,
53+
uid: undefined,
54+
ownerUid: undefined,
55+
});
5156
};
5257
}, [router.isReady]);
5358

utils/index.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DEFAULT_RANGES, DEFAULT_WIDTHS } from "@/lib/constants";
1+
import { DEFAULT_RANGES, DEFAULT_WIDTHS, isBrowser } from "@/lib/constants";
22
import {
33
exportedTypeSuite,
44
SnippngEditorConfigInterface,
@@ -137,3 +137,32 @@ export const validateSnippngConfig = (config: SnippngEditorConfigInterface) => {
137137
export const copyJSONText = async <T extends object>(data: T) => {
138138
return navigator.clipboard?.writeText(JSON.stringify(data));
139139
};
140+
141+
export class LocalStorage {
142+
static get(key: string) {
143+
if (!isBrowser) return;
144+
let value = localStorage.getItem(key);
145+
if (value) {
146+
try {
147+
return JSON.parse(value);
148+
} catch (err) {
149+
return null;
150+
}
151+
}
152+
return null;
153+
}
154+
static set(key: string, value: any) {
155+
if (!isBrowser) return;
156+
localStorage.setItem(key, JSON.stringify(value));
157+
}
158+
159+
static remove(key: string) {
160+
if (!isBrowser) return;
161+
localStorage.removeItem(key);
162+
}
163+
164+
static clear() {
165+
if (!isBrowser) return;
166+
localStorage.clear();
167+
}
168+
}

0 commit comments

Comments
 (0)