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

Update codemirror theme to match overall style/color scheme of the page #36

Merged
merged 5 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
editor theme selector
  • Loading branch information
kachbit committed Nov 19, 2023
commit 6f5c0cb0630ab3e16e37c28afa9bd0e647666f5c
3 changes: 2 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function App() {
const [settings, setSettings] = useState<Settings>({
openAiApiKey: null,
isImageGenerationEnabled: true,
editorTheme: "cobalt"
});

const downloadCode = () => {
Expand Down Expand Up @@ -231,7 +232,7 @@ function App() {
<Preview code={generatedCode} device="mobile" />
</TabsContent>
<TabsContent value="code">
<CodeMirror code={generatedCode} />
<CodeMirror code={generatedCode} editorTheme={settings.editorTheme} />
</TabsContent>
</Tabs>
</div>
Expand Down
23 changes: 16 additions & 7 deletions frontend/src/components/CodeMirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useRef, useEffect } from "react";
import { EditorState } from "@codemirror/state";
import { EditorView, keymap, lineNumbers } from "@codemirror/view";
import { espresso } from "thememirror";
import { cobalt } from "thememirror";

import {
defaultKeymap,
history,
Expand All @@ -14,14 +16,19 @@ import { html } from "@codemirror/lang-html";

interface Props {
code: string;
editorTheme: string;
}

function CodeMirror({ code }: Props) {
function CodeMirror({ code, editorTheme }: Props) {
const ref = useRef<HTMLDivElement>(null);
const view = useRef<EditorView | null>(null);

// Initialize the editor when the component mounts
useEffect(() => {
let selectedTheme = cobalt;
if (editorTheme === "espresso") {
selectedTheme = espresso;
}

view.current = new EditorView({
state: EditorState.create({
doc: code,
Expand All @@ -36,7 +43,7 @@ function CodeMirror({ code }: Props) {
lineNumbers(),
bracketMatching(),
html(),
espresso,
selectedTheme,
EditorView.lineWrapping,
],
}),
Expand All @@ -49,9 +56,8 @@ function CodeMirror({ code }: Props) {
view.current = null;
}
};
}, []);
}, [code, editorTheme]);

// Update the contents of the editor when the code changes
useEffect(() => {
if (view.current && view.current.state.doc.toString() !== code) {
view.current.dispatch({
Expand All @@ -60,6 +66,9 @@ function CodeMirror({ code }: Props) {
}
}, [code]);

return <div className="overflow-x-scroll overflow-y-scroll mx-2 border-[4px] border-black rounded-[20px]" ref={ref} />;
return (
<div className="overflow-x-scroll overflow-y-scroll mx-2 border-[4px] border-black rounded-[20px]" ref={ref} />
);
}
export default CodeMirror;

export default CodeMirror;
25 changes: 25 additions & 0 deletions frontend/src/components/SettingsDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from "react";
import {
Dialog,
DialogClose,
Expand All @@ -12,13 +13,21 @@ import { Settings } from "../types";
import { Switch } from "./ui/switch";
import { Label } from "./ui/label";
import { Input } from "./ui/input";
import { Select } from "./ui/select";

interface Props {
settings: Settings;
setSettings: React.Dispatch<React.SetStateAction<Settings>>;
}

function SettingsDialog({ settings, setSettings }: Props) {
const handleThemeChange = (theme: string) => {
setSettings((s) => ({
...s,
editorTheme: theme,
}));
};

return (
<Dialog>
<DialogTrigger>
Expand Down Expand Up @@ -65,7 +74,23 @@ function SettingsDialog({ settings, setSettings }: Props) {
}))
}
/>
<Label htmlFor="editor-theme">
<div>Editor Theme</div>
</Label>
<div>
<Select // Use the custom Select component here
id="editor-theme"
value={settings.editorTheme}
onChange={(e: React.ChangeEvent<HTMLSelectElement>) =>
handleThemeChange(e.target.value)
}
>
<option value="cobalt">Cobalt</option>
<option value="espresso">Espresso</option>
</Select>
</div>
</div>

<DialogFooter>
<DialogClose>Save</DialogClose>
</DialogFooter>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Settings {
openAiApiKey: string | null;
isImageGenerationEnabled: boolean;
editorTheme: string;
}