Skip to content

Commit a62d586

Browse files
improved code
1 parent 91568bc commit a62d586

File tree

13 files changed

+121
-46
lines changed

13 files changed

+121
-46
lines changed

app/components/code-capture/code-image-preview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default function CodeImagePreview() {
1818
}}
1919
>
2020
<div
21-
className="window flex flex-col bg-[#353742af] border rounded-md border-[#82738E]"
21+
className="window flex flex-col border rounded-md border-[#82738E]"
2222
style={{
2323
width: windowWidth,
2424
}}

app/components/code-capture/controls/control-options.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import {
99
SelectValue,
1010
} from "@/components/ui/select";
1111
import useStore from "@/lib/store/store";
12-
import { setTheme } from "@/lib/editor/themes/change-theme";
1312

1413
import themeList from "monaco-themes/themes/themelist.json";
1514
import editorLanguages from "@/lib/editor/languages/editor-languages";
15+
import useEditorTheme from "@/hooks/editor-theme";
1616

1717
export default function ControlOptions() {
18+
// hook
19+
const { updateEditorWindowTheme } = useEditorTheme();
20+
1821
// store
1922
const changeEditorTheme = useStore((state) => state.changeEditorTheme);
2023
const changeEditorLanguage = useStore((state) => state.changeEditorLanguage);
@@ -49,7 +52,7 @@ export default function ControlOptions() {
4952
<Select
5053
onValueChange={(value) => {
5154
changeEditorTheme(value);
52-
setTheme(value);
55+
updateEditorWindowTheme();
5356
}}
5457
>
5558
<SelectTrigger>

app/components/code-capture/preview/window-content.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function WindowContent() {
1414
`;
1515

1616
return (
17-
<div className="content rounded-b-md opacity-90 h-60">
17+
<div className="content rounded-b-md h-60">
1818
<MonacoEditor />
1919
</div>
2020
);

app/components/code-capture/preview/window-header.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
import { cn } from "@/lib/utils";
12
import WindowButtons from "./window-buttons";
3+
import useStore from "@/lib/store/store";
24

35
export default function WindowHeader() {
6+
const editorThemeColors = useStore((state) => state.editorThemeColors);
7+
8+
console.log("edit", editorThemeColors);
9+
410
return (
5-
<div className="window-header rounded-t-md flex justify-between w-full py-3 px-4 items-center">
11+
<div
12+
className="window-header rounded-t-md flex justify-between w-full py-3 px-4 items-center"
13+
style={{
14+
background: editorThemeColors["editor.background"],
15+
}}
16+
>
617
<WindowButtons />
718

819
<div className="window-title">

hooks/editor-theme.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { applyTheme } from "@/lib/editor/themes/apply-theme";
2+
import useStore from "@/lib/store/store";
3+
4+
export default function useEditorTheme() {
5+
6+
const editorTheme = useStore((state) => state.editorTheme);
7+
const editorThemeColors = useStore((state) => state.editorThemeColors);
8+
const changeEditorThemeColors = useStore(
9+
(state) => state.changeEditorThemeColors
10+
);
11+
12+
const updateEditorWindowTheme = async () => {
13+
const colors = await applyTheme(editorTheme);
14+
changeEditorThemeColors(colors);
15+
13;
16+
};
17+
18+
return { updateEditorWindowTheme };
19+
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import { loader } from "@monaco-editor/react";
22

3-
const setTheme = async (themeName: string) => {
3+
const applyTheme = async (themeName: string) => {
44
console.log(themeName);
55
let themeData = await import(`monaco-themes/themes/${themeName}.json`);
6-
themeData = { ...themeData };
76

87
themeName &&
98
loader.init().then((monaco) => {
109
if (themeName.split(" ").length > 1) {
11-
console.log("yes");
1210
themeName = themeName.split(" ")[0] + "-" + themeName.split(" ")[1];
1311
}
14-
monaco.editor.defineTheme(themeName, themeData);
12+
monaco.editor.defineTheme(themeName, { ...themeData });
1513
monaco.editor.setTheme(themeName);
1614
});
1715

18-
return { ...themeData };
16+
return themeData.colors;
1917
};
2018

21-
export { setTheme };
19+
export { applyTheme };

lib/monoco-editor.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import { Editor, MonacoDiffEditor } from "@monaco-editor/react";
44
import React from "react";
55
import useStore from "./store/store";
6-
import { setTheme } from "./editor/themes/change-theme";
7-
import { Languages } from "lucide-react";
6+
import useEditorTheme from "@/hooks/editor-theme";
87

98
export default function MonacoEditor() {
109
const editorRef = React.useRef<MonacoDiffEditor>(null);
1110

11+
const { updateEditorWindowTheme } = useEditorTheme();
12+
1213
const windowWidth = useStore((state) => state.windowWidth);
1314
const changeWindowWidth = useStore((state) => state.changeWindowWidth);
1415

15-
const editorTheme = useStore((state) => state.editorTheme);
1616
const editorLanguage = useStore((state) => state.editorLanguage);
1717

1818
// to adjust editor width based on its content
@@ -27,7 +27,7 @@ export default function MonacoEditor() {
2727
}
2828
};
2929

30-
console.log('changed language',editorLanguage);
30+
console.log("changed language", editorLanguage);
3131

3232
return (
3333
<Editor
@@ -41,11 +41,10 @@ export default function MonacoEditor() {
4141
theme="vs-dark"
4242
onMount={(editor) => {
4343
editorRef.current = editor;
44-
setTheme(editorTheme);
44+
updateEditorWindowTheme();
4545
}}
46-
onChange={(editor, monaco) => {
46+
onChange={() => {
4747
controlEditorWidth();
48-
// detectModel()
4948
}}
5049
options={{
5150
lineNumbers: "off",

lib/store/slices/editor-slice.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import EditorSlice from "@/lib/types/store/slices/editor-slice";
2+
import { SetState } from "@/lib/types/store/store";
3+
4+
export default function createEditorSlice(set : SetState): EditorSlice {
5+
return {
6+
editorTheme: "Night Owl",
7+
changeEditorTheme: (theme) =>
8+
set(() => ({
9+
editorTheme: theme,
10+
})),
11+
12+
editorLanguage: "javascript",
13+
changeEditorLanguage: (language) =>
14+
set(() => ({
15+
editorLanguage: language,
16+
})),
17+
18+
editorThemeColors: {
19+
"editor.background": "",
20+
},
21+
changeEditorThemeColors: (themeColors) =>
22+
set(() => ({
23+
editorThemeColors: themeColors,
24+
})),
25+
};
26+
}

lib/store/slices/window-slice.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import WindowSlice from "@/lib/types/store/slices/window-slice";
2+
import { SetState } from "@/lib/types/store/store";
3+
4+
export default function createWindowSlice(set: SetState): WindowSlice {
5+
return {
6+
windowWidth: 600,
7+
changeWindowWidth: (newWidth) =>
8+
set(() => ({
9+
windowWidth: newWidth,
10+
})),
11+
};
12+
}

lib/store/store.tsx

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,11 @@
11
import { create } from "zustand";
2-
3-
interface StoreStates {
4-
windowWidth: number;
5-
changeWindowWidth: (newWidth: number) => void;
6-
7-
editorTheme: string;
8-
changeEditorTheme: (theme: string) => void;
9-
10-
editorLanguage: string;
11-
changeEditorLanguage: (language: string) => void;
12-
}
2+
import createWindowSlice from "./slices/window-slice";
3+
import createEditorSlice from "./slices/editor-slice";
4+
import { StoreStates } from "../types/store/store";
135

146
const useStore = create<StoreStates>()((set) => ({
15-
windowWidth: 600,
16-
changeWindowWidth: (newWidth) =>
17-
set(() => ({
18-
windowWidth: newWidth,
19-
})),
20-
21-
editorTheme: "Night Owl",
22-
changeEditorTheme: (theme) =>
23-
set(() => ({
24-
editorTheme: theme,
25-
})),
26-
27-
editorLanguage: "javascript",
28-
changeEditorLanguage: (language) =>
29-
set(() => ({
30-
editorLanguage: language,
31-
})),
7+
...createWindowSlice(set),
8+
...createEditorSlice(set),
329
}));
3310

3411
export default useStore;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export interface EditorThemeColors {
2+
"editor.background": string;
3+
}
4+
5+
export default interface EditorSlice {
6+
editorTheme: string;
7+
changeEditorTheme: (theme: string) => void;
8+
9+
editorLanguage: string;
10+
changeEditorLanguage: (language: string) => void;
11+
12+
editorThemeColors: EditorThemeColors;
13+
changeEditorThemeColors: (themeColors: EditorThemeColors) => void;
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default interface WindowSlice {
2+
windowWidth: number;
3+
changeWindowWidth: (newWidth: number) => void;
4+
}

lib/types/store/store.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import EditorSlice from "./slices/editor-slice";
2+
import WindowSlice from "./slices/window-slice";
3+
4+
export type StoreStates = WindowSlice & EditorSlice;
5+
6+
export type SetState = (
7+
partial:
8+
| StoreStates
9+
| Partial<StoreStates>
10+
| ((state: StoreStates) => StoreStates | Partial<StoreStates>),
11+
replace?: boolean | undefined
12+
) => void;

0 commit comments

Comments
 (0)