fix: cursor didnt focus on right position
This commit is contained in:
parent
30ab9e384a
commit
d10c793a60
@ -17,7 +17,7 @@ import { Textarea } from "@/components/ui/textarea";
|
|||||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "./components/ui/tabs";
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from "./components/ui/tabs";
|
||||||
import CodeMirror from "./components/CodeMirror";
|
import CodeMirror from "./components/CodeMirror";
|
||||||
import SettingsDialog from "./components/SettingsDialog";
|
import SettingsDialog from "./components/SettingsDialog";
|
||||||
import { Settings } from "./types";
|
import { Settings, EditorTheme } from "./types";
|
||||||
import { IS_RUNNING_ON_CLOUD } from "./config";
|
import { IS_RUNNING_ON_CLOUD } from "./config";
|
||||||
import { PicoBadge } from "./components/PicoBadge";
|
import { PicoBadge } from "./components/PicoBadge";
|
||||||
import { OnboardingNote } from "./components/OnboardingNote";
|
import { OnboardingNote } from "./components/OnboardingNote";
|
||||||
@ -34,12 +34,13 @@ function App() {
|
|||||||
const [executionConsole, setExecutionConsole] = useState<string[]>([]);
|
const [executionConsole, setExecutionConsole] = useState<string[]>([]);
|
||||||
const [updateInstruction, setUpdateInstruction] = useState("");
|
const [updateInstruction, setUpdateInstruction] = useState("");
|
||||||
const [history, setHistory] = useState<string[]>([]);
|
const [history, setHistory] = useState<string[]>([]);
|
||||||
|
console.log('history', history)
|
||||||
const [settings, setSettings] = usePersistedState<Settings>(
|
const [settings, setSettings] = usePersistedState<Settings>(
|
||||||
{
|
{
|
||||||
openAiApiKey: null,
|
openAiApiKey: null,
|
||||||
screenshotOneApiKey: null,
|
screenshotOneApiKey: null,
|
||||||
isImageGenerationEnabled: true,
|
isImageGenerationEnabled: true,
|
||||||
editorTheme: "cobalt",
|
editorTheme: EditorTheme.COBALT,
|
||||||
},
|
},
|
||||||
"setting"
|
"setting"
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { useRef, useEffect } from "react";
|
import { useRef, useEffect } from "react";
|
||||||
import { EditorState } from "@codemirror/state";
|
import { EditorState } from "@codemirror/state";
|
||||||
import { EditorView, keymap, lineNumbers } from "@codemirror/view";
|
import { EditorView, keymap, lineNumbers, ViewUpdate } from "@codemirror/view";
|
||||||
import { espresso, cobalt } from "thememirror";
|
import { espresso, cobalt } from "thememirror";
|
||||||
import {
|
import {
|
||||||
defaultKeymap,
|
defaultKeymap,
|
||||||
@ -11,25 +11,18 @@ import {
|
|||||||
} from "@codemirror/commands";
|
} from "@codemirror/commands";
|
||||||
import { bracketMatching } from "@codemirror/language";
|
import { bracketMatching } from "@codemirror/language";
|
||||||
import { html } from "@codemirror/lang-html";
|
import { html } from "@codemirror/lang-html";
|
||||||
|
import { EditorTheme } from "@/types";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
code: string;
|
code: string;
|
||||||
editorTheme: string;
|
editorTheme: EditorTheme;
|
||||||
onCodeChange: (code: string) => void;
|
onCodeChange: (code: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
function CodeMirror({ code, editorTheme, onCodeChange }: Props) {
|
function CodeMirror({ code, editorTheme, onCodeChange }: Props) {
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
const view = useRef<EditorView | null>(null);
|
const view = useRef<EditorView | null>(null);
|
||||||
|
const editorState = EditorState.create({
|
||||||
useEffect(() => {
|
|
||||||
let selectedTheme = cobalt;
|
|
||||||
if (editorTheme === "espresso") {
|
|
||||||
selectedTheme = espresso;
|
|
||||||
}
|
|
||||||
view.current = new EditorView({
|
|
||||||
state: EditorState.create({
|
|
||||||
doc: code,
|
|
||||||
extensions: [
|
extensions: [
|
||||||
history(),
|
history(),
|
||||||
keymap.of([
|
keymap.of([
|
||||||
@ -41,15 +34,19 @@ function CodeMirror({ code, editorTheme, onCodeChange }: Props) {
|
|||||||
lineNumbers(),
|
lineNumbers(),
|
||||||
bracketMatching(),
|
bracketMatching(),
|
||||||
html(),
|
html(),
|
||||||
selectedTheme,
|
editorTheme === EditorTheme.ESPRESSO ? espresso : cobalt,
|
||||||
EditorView.lineWrapping,
|
EditorView.lineWrapping,
|
||||||
EditorView.updateListener.of(update => {
|
EditorView.updateListener.of((update: ViewUpdate) => {
|
||||||
if (update.changes) {
|
if (update.docChanged) {
|
||||||
onCodeChange(view.current?.state.doc.toString() || "");
|
const updatedCode = update.state.doc.toString();
|
||||||
|
onCodeChange(updatedCode);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
}),
|
});
|
||||||
|
useEffect(() => {
|
||||||
|
view.current = new EditorView({
|
||||||
|
state: editorState,
|
||||||
parent: ref.current as Element,
|
parent: ref.current as Element,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -59,7 +56,7 @@ function CodeMirror({ code, editorTheme, onCodeChange }: Props) {
|
|||||||
view.current = null;
|
view.current = null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, [code, editorTheme]);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (view.current && view.current.state.doc.toString() !== code) {
|
if (view.current && view.current.state.doc.toString() !== code) {
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import {
|
|||||||
DialogTrigger,
|
DialogTrigger,
|
||||||
} from "@/components/ui/dialog";
|
} from "@/components/ui/dialog";
|
||||||
import { FaCog } from "react-icons/fa";
|
import { FaCog } from "react-icons/fa";
|
||||||
import { Settings } from "../types";
|
import { EditorTheme, Settings } from "../types";
|
||||||
import { Switch } from "./ui/switch";
|
import { Switch } from "./ui/switch";
|
||||||
import { Label } from "./ui/label";
|
import { Label } from "./ui/label";
|
||||||
import { Input } from "./ui/input";
|
import { Input } from "./ui/input";
|
||||||
@ -21,7 +21,7 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function SettingsDialog({ settings, setSettings }: Props) {
|
function SettingsDialog({ settings, setSettings }: Props) {
|
||||||
const handleThemeChange = (theme: string) => {
|
const handleThemeChange = (theme: EditorTheme) => {
|
||||||
setSettings((s) => ({
|
setSettings((s) => ({
|
||||||
...s,
|
...s,
|
||||||
editorTheme: theme,
|
editorTheme: theme,
|
||||||
@ -110,7 +110,7 @@ function SettingsDialog({ settings, setSettings }: Props) {
|
|||||||
id="editor-theme"
|
id="editor-theme"
|
||||||
value={settings.editorTheme}
|
value={settings.editorTheme}
|
||||||
onChange={(e: React.ChangeEvent<HTMLSelectElement>) =>
|
onChange={(e: React.ChangeEvent<HTMLSelectElement>) =>
|
||||||
handleThemeChange(e.target.value)
|
handleThemeChange(e.target.value as EditorTheme)
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<option value="cobalt">Cobalt</option>
|
<option value="cobalt">Cobalt</option>
|
||||||
|
|||||||
@ -1,6 +1,11 @@
|
|||||||
|
export enum EditorTheme {
|
||||||
|
ESPRESSO = "espresso",
|
||||||
|
COBALT = "cobalt",
|
||||||
|
}
|
||||||
|
|
||||||
export interface Settings {
|
export interface Settings {
|
||||||
openAiApiKey: string | null;
|
openAiApiKey: string | null;
|
||||||
screenshotOneApiKey: string | null;
|
screenshotOneApiKey: string | null;
|
||||||
isImageGenerationEnabled: boolean;
|
isImageGenerationEnabled: boolean;
|
||||||
editorTheme: string;
|
editorTheme: EditorTheme;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user